From 4bb9d30d05bb99b94bdcd7a9e7a92616eada9b09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A1roly=20Kiripolszky?= Date: Wed, 21 Nov 2018 11:23:28 +0100 Subject: [PATCH] use standard Clipboard API when available --- locales/en/messages.json | 3 +++ src/js/tabs/cli.js | 37 ++++++++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/locales/en/messages.json b/locales/en/messages.json index 3614ec4e..70fb277f 100644 --- a/locales/en/messages.json +++ b/locales/en/messages.json @@ -2259,6 +2259,9 @@ "cliCopyToClipboardBtn": { "message": "Copy to clipboard" }, + "cliCopySuccessful": { + "message": "Content has been copied to clipboard!" + }, "loggingNote": { "message": "Data will be logged in this tab only, leaving the tab will cancel logging and application will return to its normal \"configurator\" state.
You are free to select the global update period, data will be written into the log file every 1 second for performance reasons." diff --git a/src/js/tabs/cli.js b/src/js/tabs/cli.js index d618fcc5..a865d137 100644 --- a/src/js/tabs/cli.js +++ b/src/js/tabs/cli.js @@ -41,6 +41,35 @@ function getCliCommand(command, cliBuffer) { return commandWithBackSpaces(command, buffer, noOfCharsToDelete); } +function copyToClipboard(text) { + function onCopySuccessful() { + writeLineToOutput("* " + i18n.getMessage("cliCopySuccessful")); + } + + function onCopyFailed(ex) { + console.warn(ex); + } + + function nwCopy(text) { + try { + let gui = require('nw.gui'), + clipboard = gui.Clipboard.get(); + clipboard.set(text, "text"); + onCopySuccessful(); + } catch (ex) { + onCopyFailed(ex); + } + } + + function webCopy(text) { + navigator.clipboard.writeText(text) + .then(onCopySuccessful, onCopyFailed); + } + + let copyFunc = !navigator.clipboard ? nwCopy : webCopy; + copyFunc(text); +} + TABS.cli.initialize = function (callback) { var self = this; @@ -108,13 +137,7 @@ TABS.cli.initialize = function (callback) { }); $('.tab-cli .copy').click(function() { - try { - let gui = require('nw.gui'), - clipboard = gui.Clipboard.get(); - clipboard.set(self.outputHistory, "text"); - } catch (ex) { - console.warn(ex); - } + copyToClipboard(self.outputHistory); }); // Tab key detection must be on keydown,