From 3560d7b716a049e4ae39b0276b993b3b8fbfc831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A1roly=20Kiripolszky?= Date: Tue, 20 Nov 2018 11:27:05 +0100 Subject: [PATCH] add button to CLI for loading commands from file --- locales/en/messages.json | 6 ++++++ src/js/tabs/cli.js | 36 ++++++++++++++++++++++++++++++++++++ src/tabs/cli.html | 1 + 3 files changed, 43 insertions(+) diff --git a/locales/en/messages.json b/locales/en/messages.json index 32c7d722..801ca27f 100644 --- a/locales/en/messages.json +++ b/locales/en/messages.json @@ -2289,6 +2289,12 @@ "cliCopySuccessful": { "message": "Copied!" }, + "cliLoadFromFileBtn": { + "message": "Load from file" + }, + "cliCommandsLoadedNotice": { + "message": "Commands loaded from `{{filename}}`. Press ENTER to confirm execution!" + }, "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 bb239193..041e54cf 100644 --- a/src/js/tabs/cli.js +++ b/src/js/tabs/cli.js @@ -173,6 +173,42 @@ TABS.cli.initialize = function (callback, nwGui) { } else { $('.tab-cli .copy').hide(); } + + $('.tab-cli .load').click(function() { + var suffix = 'txt', + accepts = [{ + description: suffix.toUpperCase() + ' files', extensions: [suffix], + }]; + + chrome.fileSystem.chooseEntry({type: 'openFile', accepts: accepts}, function(entry) { + if (chrome.runtime.lastError) { + console.error(chrome.runtime.lastError.message); + return; + } + + if (!entry) { + console.log('No file selected'); + return; + } + + function setCommands(result) { + let notice = i18n.getMessage("cliCommandsLoadedNotice", { + filename: entry.name, + }); + textarea.val(result + "\n" + "# " + notice); + // scroll to the last line to show confirmation notice + textarea.scrollTop(textarea[0].scrollHeight); + } + + entry.file((file) => { + let reader = new FileReader(); + reader.onload = + () => setCommands(reader.result); + reader.onerror = () => console.error(reader.error); + reader.readAsText(file); + }); + }); + }); // Tab key detection must be on keydown, // `keypress`/`keyup` happens too late, as `textarea` will have already lost focus. diff --git a/src/tabs/cli.html b/src/tabs/cli.html index b25699ec..06b4b200 100644 --- a/src/tabs/cli.html +++ b/src/tabs/cli.html @@ -16,6 +16,7 @@
+