Skip to content

Commit dde266a

Browse files
authored
Add paste from clipboard option to VTX tab (#1650)
Add paste from clipboard option to VTX tab
2 parents 8a475c5 + abb9997 commit dde266a

File tree

8 files changed

+172
-25
lines changed

8 files changed

+172
-25
lines changed

locales/en/messages.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5163,6 +5163,14 @@
51635163
"message": "<span class=\"message-negative\">Error</span> while loading the VTX Config file",
51645164
"description": "Message in the GUI log when the VTX Config file is loaded"
51655165
},
5166+
"vtxLoadClipboardOk": {
5167+
"message": "VTX Config info <span class=\"message-positive\">loaded</span> from clipboard",
5168+
"description": "Message in the GUI log when the VTX Config file is pasted from clipboard"
5169+
},
5170+
"vtxLoadClipboardKo": {
5171+
"message": "<span class=\"message-negative\">Error</span> while loading the VTX Config info from clipboard. Maybe the contents are not correct",
5172+
"description": "Message in the GUI log when the VTX Config file is pasted from clipboard"
5173+
},
51665174
"vtxButtonSaveFile": {
51675175
"message": "Save to file",
51685176
"description": "Save to file button in the VTX tab"
@@ -5171,6 +5179,10 @@
51715179
"message": "Load from file",
51725180
"description": "Load to file button in the VTX tab"
51735181
},
5182+
"vtxButtonLoadClipboard": {
5183+
"message": "Load from clipboard",
5184+
"description": "Paste from clipboard button in the VTX tab"
5185+
},
51745186
"vtxButtonSave": {
51755187
"message": "Save",
51765188
"description": "Save button in the VTX tab"

manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"serial",
2929
"usb",
3030
"storage",
31+
"clipboardRead",
3132
"fileSystem",
3233
"fileSystem.write",
3334
"fileSystem.retainEntries",

src/css/main.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,7 @@ dialog {
11521152

11531153
/** Hack to change the "display: none" by a "visibility:hidden", to apply
11541154
the margin-left: auto needed by the first element to align to the right the buttons */
1155-
.toolbar_fixed_bottom .content_toolbar div[style='display: none;'] {
1155+
.toolbar_fixed_bottom .content_toolbar div[style='display: none;']:first-child {
11561156
visibility: hidden;
11571157
display: block !important;
11581158
}

src/js/Clipboard.js

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
'use strict';
2+
3+
/**
4+
* Encapsulates the Clipboard logic, depending on web or nw
5+
*
6+
*/
7+
var Clipboard = {
8+
_nwClipboard: null,
9+
available : null,
10+
readAvailable : null,
11+
writeAvailable : null,
12+
writeText : null,
13+
readText : null
14+
};
15+
16+
Clipboard._configureClipboardAsNwJs = function(nwGui) {
17+
18+
console.log('NW GUI Clipboard available');
19+
20+
this.available = true;
21+
this.readAvailable = true;
22+
this.writeAvailable = true;
23+
this._nwClipboard = nwGui.Clipboard.get();
24+
25+
this.writeText = function(text, onSuccess, onError) {
26+
try {
27+
28+
this._nwClipboard.set(text, "text");
29+
30+
} catch (err) {
31+
if (onError) {
32+
onError(err);
33+
}
34+
}
35+
36+
if (onSuccess) {
37+
onSuccess(text);
38+
}
39+
}
40+
41+
this.readText = function(onSuccess, onError) {
42+
43+
let text = '';
44+
try {
45+
46+
text = this._nwClipboard.get("text");
47+
48+
} catch (err) {
49+
if (onError) {
50+
onError(err);
51+
}
52+
}
53+
54+
if (onSuccess) {
55+
onSuccess(text);
56+
}
57+
}
58+
}
59+
60+
Clipboard._configureClipboardAsChrome = function() {
61+
62+
console.log('Chrome Clipboard available');
63+
64+
this.available = true;
65+
this.readAvailable = false; // FIXME: for some reason the read is not working
66+
this.writeAvailable = true;
67+
68+
this.writeText = function(text, onSuccess, onError) {
69+
navigator.clipboard.writeText(text)
70+
.then(onSuccess)
71+
.catch(onError);
72+
}
73+
74+
this.readText = function(onSuccess, onError) {
75+
navigator.clipboard.readText()
76+
.then(onSuccess)
77+
.catch(onError);
78+
}
79+
80+
}
81+
82+
Clipboard._configureClipboardAsOther = function() {
83+
84+
console.warn('NO Clipboard available');
85+
86+
this.available = false;
87+
this.readAvailable = false;
88+
this.writeAvailable = false;
89+
90+
this.writeText = function(text, onSuccess, onError) {
91+
onError('Clipboard not available');
92+
}
93+
94+
this.readText = function(onSuccess, onError) {
95+
onError('Clipboard not available');
96+
}
97+
}
98+
99+
100+
switch (GUI.Mode) {
101+
case GUI_Modes.NWJS:
102+
Clipboard._configureClipboardAsNwJs(GUI.nwGui);
103+
break;
104+
105+
case GUI_Modes.ChromeApp:
106+
Clipboard._configureClipboardAsChrome();
107+
break;
108+
109+
default:
110+
Clipboard._configureClipboardAsOther();
111+
}

src/js/tabs/cli.js

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ function getCliCommand(command, cliBuffer) {
4444
return commandWithBackSpaces(command, buffer, noOfCharsToDelete);
4545
}
4646

47-
function copyToClipboard(text, nwGui) {
47+
function copyToClipboard(text) {
4848
function onCopySuccessful() {
4949
const button = $('.tab-cli .copy');
5050
const origText = button.text();
@@ -67,26 +67,10 @@ function copyToClipboard(text, nwGui) {
6767
console.warn(ex);
6868
}
6969

70-
function nwCopy(text) {
71-
try {
72-
let clipboard = nwGui.Clipboard.get();
73-
clipboard.set(text, "text");
74-
onCopySuccessful();
75-
} catch (ex) {
76-
onCopyFailed(ex);
77-
}
78-
}
79-
80-
function webCopy(text) {
81-
navigator.clipboard.writeText(text)
82-
.then(onCopySuccessful, onCopyFailed);
83-
}
84-
85-
let copyFunc = nwGui ? nwCopy : webCopy;
86-
copyFunc(text);
70+
Clipboard.writeText(text, onCopySuccessful, onCopyFailed);
8771
}
8872

89-
TABS.cli.initialize = function (callback, nwGui) {
73+
TABS.cli.initialize = function (callback) {
9074
var self = this;
9175

9276
if (GUI.active_tab != 'cli') {
@@ -96,8 +80,6 @@ TABS.cli.initialize = function (callback, nwGui) {
9680
self.outputHistory = "";
9781
self.cliBuffer = "";
9882

99-
// nwGui variable is set in main.js
100-
const clipboardCopySupport = !(nwGui == null && !navigator.clipboard);
10183
const enterKeyCode = 13;
10284

10385
function executeCommands(out_string) {
@@ -194,14 +176,14 @@ TABS.cli.initialize = function (callback, nwGui) {
194176
$('.tab-cli .window .wrapper').empty();
195177
});
196178

197-
if (clipboardCopySupport) {
179+
if (Clipboard.available) {
198180
$('.tab-cli .copy').click(function() {
199-
copyToClipboard(self.outputHistory, nwGui);
181+
copyToClipboard(self.outputHistory);
200182
});
201183
} else {
202184
$('.tab-cli .copy').hide();
203185
}
204-
186+
205187
$('.tab-cli .load').click(function() {
206188
var accepts = [
207189
{

src/js/tabs/vtx.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ TABS.vtx.initialize = function (callback) {
160160
$(".vtx_table_not_configured").toggle(vtxTableNotConfigured);
161161
$(".vtx_table_save_pending").toggle(TABS.vtx.vtxTableSavePending);
162162

163+
// Buttons
164+
$('.clipboard_available').toggle(Clipboard.available && Clipboard.readAvailable);
165+
163166
// Insert actual values in the fields
164167
// Values of the selected mode
165168
$("#vtx_frequency").val(VTX_CONFIG.vtx_frequency);
@@ -484,6 +487,10 @@ TABS.vtx.initialize = function (callback) {
484487
load_json();
485488
});
486489

490+
$('a.load_clipboard').click(function () {
491+
load_clipboard_json();
492+
});
493+
487494
$('a.save').click(function () {
488495
save_vtx();
489496
});
@@ -595,6 +602,36 @@ TABS.vtx.initialize = function (callback) {
595602
});
596603
}
597604

605+
function load_clipboard_json() {
606+
607+
try {
608+
609+
Clipboard.readText(
610+
function(text) {
611+
612+
console.log('Pasted content: ', text);
613+
614+
let vtxConfig = JSON.parse(text);
615+
read_vtx_config_json(vtxConfig, load_html);
616+
617+
TABS.vtx.vtxTableSavePending = true;
618+
619+
console.log('Load VTX clipboard end');
620+
GUI.log(i18n.getMessage('vtxLoadClipboardOk'));
621+
622+
}, function(err) {
623+
GUI.log(i18n.getMessage('vtxLoadClipboardKo'));
624+
console.error('Failed to read clipboard contents: ', err);
625+
}
626+
);
627+
628+
} catch (err) {
629+
console.error('Failed loading VTX file config: ' + err);
630+
GUI.log(i18n.getMessage('vtxLoadClipboardKo'));
631+
}
632+
633+
}
634+
598635
// Save all the values from the tab to MSP
599636
function save_vtx() {
600637

src/main.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
<script type="text/javascript" src="./js/jenkins_loader.js"></script>
142142
<script type="text/javascript" src="./js/Analytics.js"></script>
143143
<script type="text/javascript" src="./js/main.js"></script>
144+
<script type="text/javascript" src="./js/Clipboard.js"></script>
144145
<script type="text/javascript" src="./js/tabs/static_tab.js"></script>
145146
<script type="text/javascript" src="./js/tabs/landing.js"></script>
146147
<script type="text/javascript" src="./js/tabs/setup.js"></script>

src/tabs/vtx.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@
271271
<div class="btn load_file_btn">
272272
<a class="load_file" id="load_file_button" href="#" i18n="vtxButtonLoadFile"></a>
273273
</div>
274+
<div class="btn load_cliboard_btn clipboard_available">
275+
<a class="load_clipboard" id="load_clipboard_button" href="#" i18n="vtxButtonLoadClipboard"></a>
276+
</div>
274277
<div class="btn save_btn">
275278
<a class="save" id="save_button" href="#" i18n="vtxButtonSave"></a>
276279
</div>

0 commit comments

Comments
 (0)