From 2c809abf642005ed179b5f222a775943df4ce4dc Mon Sep 17 00:00:00 2001 From: jcesarmobile Date: Thu, 12 Mar 2020 19:22:23 +0100 Subject: [PATCH] fix(electron): various clipboard fixes (#2566) --- electron/src/electron/clipboard.ts | 39 ++++++++++++++++-------------- 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/electron/src/electron/clipboard.ts b/electron/src/electron/clipboard.ts index a70cd0917..0469f6877 100644 --- a/electron/src/electron/clipboard.ts +++ b/electron/src/electron/clipboard.ts @@ -1,4 +1,4 @@ -import { WebPlugin, ClipboardPlugin,ClipboardWrite,ClipboardRead,ClipboardReadResult } from "@capacitor/core"; +import { WebPlugin, ClipboardPlugin, ClipboardWrite, ClipboardReadResult } from "@capacitor/core"; const { clipboard , nativeImage } = require('electron'); @@ -16,7 +16,7 @@ export class ClipboardPluginElectron extends WebPlugin implements ClipboardPlugi async write(options: ClipboardWrite): Promise { return new Promise((resolve) => { - if (options.string) { + if (options.string !== undefined) { clipboard.writeText(options.string); } else if(options.url){ @@ -37,26 +37,29 @@ export class ClipboardPluginElectron extends WebPlugin implements ClipboardPlugi } - async read(_options: ClipboardRead): Promise { + async read(): Promise { - return new Promise((resolve, reject)=>{ + return new Promise((resolve)=>{ const availableFormats = clipboard.availableFormats(); - - for(const format of availableFormats){ - if(format === "text/plain"){ - return resolve({"value": clipboard.readText()}); - } - else if (format === "text/html"){ - return resolve({"value": clipboard.readHTML()}); - } - else if (format === "image/png" || format === "image/jpeg" ){ - - - return resolve({"value": clipboard.readImage().toDataURL()}); + if (availableFormats.length > 0) { + let format = availableFormats[availableFormats.length-1]; + if (format.includes("image")) { + return resolve({"value": clipboard.readImage().toDataURL(), "type": format}); + } else { + format = availableFormats[0]; + if (format === undefined) { + return resolve({"value": "", "type": "text/plain"}); + } + else if(format === "text/plain"){ + return resolve({"value": clipboard.readText(), "type": format}); + } + else if (format === "text/html"){ + return resolve({"value": clipboard.readHTML(), "type": format}); + } } + } else { + return resolve({"value": "", "type": "text/plain"}); } - - return reject('Unable to get data from clipboard'); }); }