Skip to content

Commit ee0db65

Browse files
authored
Merge pull request #1231 from atomgomba/feature-copy-cli-history-to-clipboard
Add button to copy CLI contents to clipboard
2 parents eb4a0d7 + e779834 commit ee0db65

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

locales/en/messages.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2256,6 +2256,12 @@
22562256
"cliClearOutputHistoryBtn": {
22572257
"message": "Clear output history"
22582258
},
2259+
"cliCopyToClipboardBtn": {
2260+
"message": "Copy to clipboard"
2261+
},
2262+
"cliCopySuccessful": {
2263+
"message": "Copied!"
2264+
},
22592265

22602266
"loggingNote": {
22612267
"message": "Data will be logged in this tab <span class=\"message-negative\">only</span>, leaving the tab will <span class=\"message-negative\">cancel</span> logging and application will return to its normal <strong>\"configurator\"</strong> state.<br /> You are free to select the global update period, data will be written into the log file every <strong>1</strong> second for performance reasons."

src/js/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ function startProcess() {
283283
TABS.onboard_logging.initialize(content_ready);
284284
break;
285285
case 'cli':
286-
TABS.cli.initialize(content_ready);
286+
TABS.cli.initialize(content_ready, nwGui);
287287
break;
288288

289289
default:

src/js/tabs/cli.js

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,49 @@ function getCliCommand(command, cliBuffer) {
4141
return commandWithBackSpaces(command, buffer, noOfCharsToDelete);
4242
}
4343

44-
TABS.cli.initialize = function (callback) {
44+
function copyToClipboard(text, nwGui) {
45+
function onCopySuccessful() {
46+
const button = $('.tab-cli .copy');
47+
const origText = button.text();
48+
const origWidth = button.css("width");
49+
button.text(i18n.getMessage("cliCopySuccessful"));
50+
button.css({
51+
width: origWidth,
52+
textAlign: "center",
53+
});
54+
setTimeout(() => {
55+
button.text(origText);
56+
button.css({
57+
width: "",
58+
textAlign: "",
59+
});
60+
}, 1500);
61+
}
62+
63+
function onCopyFailed(ex) {
64+
console.warn(ex);
65+
}
66+
67+
function nwCopy(text) {
68+
try {
69+
let clipboard = nwGui.Clipboard.get();
70+
clipboard.set(text, "text");
71+
onCopySuccessful();
72+
} catch (ex) {
73+
onCopyFailed(ex);
74+
}
75+
}
76+
77+
function webCopy(text) {
78+
navigator.clipboard.writeText(text)
79+
.then(onCopySuccessful, onCopyFailed);
80+
}
81+
82+
let copyFunc = nwGui ? nwCopy : webCopy;
83+
copyFunc(text);
84+
}
85+
86+
TABS.cli.initialize = function (callback, nwGui) {
4587
var self = this;
4688

4789
if (GUI.active_tab != 'cli') {
@@ -51,6 +93,9 @@ TABS.cli.initialize = function (callback) {
5193
self.outputHistory = "";
5294
self.cliBuffer = "";
5395

96+
// nwGui variable is set in main.js
97+
const clipboardCopySupport = !(nwGui == null && !navigator.clipboard);
98+
5499
$('#content').load("./tabs/cli.html", function () {
55100
// translate to user-selected language
56101
i18n.localizePage();
@@ -107,6 +152,14 @@ TABS.cli.initialize = function (callback) {
107152
$('.tab-cli .window .wrapper').empty();
108153
});
109154

155+
if (clipboardCopySupport) {
156+
$('.tab-cli .copy').click(function() {
157+
copyToClipboard(self.outputHistory, nwGui);
158+
});
159+
} else {
160+
$('.tab-cli .copy').hide();
161+
}
162+
110163
// Tab key detection must be on keydown,
111164
// `keypress`/`keyup` happens too late, as `textarea` will have already lost focus.
112165
textarea.keydown(function (event) {

src/tabs/cli.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
<div class="btn save_btn pull-right">
1818
<a class="save" href="#" i18n="cliSaveToFileBtn"></a>
1919
<a class="clear" href="#" i18n="cliClearOutputHistoryBtn"></a>
20+
<a class="copy" href="#" i18n="cliCopyToClipboardBtn"></a>
2021
</div>
2122
</div>
2223
</div>

0 commit comments

Comments
 (0)