refactoring

This commit is contained in:
Devaev Maxim 2018-09-09 20:13:03 +03:00
parent 4dfbf5aa17
commit 6e9a3222ce

View File

@ -43,7 +43,7 @@ function Hid() {
if (window.navigator.clipboard && window.navigator.clipboard.readText) { if (window.navigator.clipboard && window.navigator.clipboard.readText) {
__chars_to_codes = __buildCharsToCodes(); __chars_to_codes = __buildCharsToCodes();
tools.setOnClick($("pak-button"), __pasteAsKeys); tools.setOnClick($("pak-button"), __pasteAsKeysFromClipboard);
} else { } else {
$("pak-button").title = $("pak-led").title = "Your browser does not support the Clipboard API.\nUse Google Chrome or Chromium."; $("pak-button").title = $("pak-led").title = "Your browser does not support the Clipboard API.\nUse Google Chrome or Chromium.";
} }
@ -131,53 +131,55 @@ function Hid() {
return chars_to_codes; return chars_to_codes;
}; };
var __pasteAsKeys = function() { var __pasteAsKeysFromClipboard = function() {
window.navigator.clipboard.readText().then(function(clipboard_text) { window.navigator.clipboard.readText().then(__pasteAsKeys);
clipboard_text = clipboard_text.replace(/[^\x00-\x7F]/g, ""); // eslint-disable-line no-control-regex };
if (clipboard_text) {
var clipboard_codes = [];
var codes_count = 0;
[...clipboard_text].forEach(function(ch) {
var codes = __chars_to_codes[ch];
if (codes) {
codes_count += codes.length;
clipboard_codes.push(codes);
}
});
var confirm_msg = ( var __pasteAsKeys = function(text) {
"You are going to automatically type " + codes_count text = text.replace(/[^\x00-\x7F]/g, ""); // eslint-disable-line no-control-regex
+ " characters from the system clipboard." if (text) {
+ " It will take " + (__codes_delay * codes_count * 2 / 1000) + " seconds.<br>" var clipboard_codes = [];
+ "<br>Are you sure you want to continue?<br>" var codes_count = 0;
); [...text].forEach(function(ch) {
var codes = __chars_to_codes[ch];
if (codes) {
codes_count += codes.length;
clipboard_codes.push(codes);
}
});
ui.confirm(confirm_msg).then(function(ok) { var confirm_msg = (
if (ok) { "You are going to automatically type " + codes_count
$("pak-button").disabled = true; + " characters from the system clipboard."
$("pak-led").className = "led-pak-typing"; + " It will take " + (__codes_delay * codes_count * 2 / 1000) + " seconds.<br>"
$("pak-led").title = "Autotyping..."; + "<br>Are you sure you want to continue?<br>"
);
tools.debug("Paste-as-keys:", clipboard_text); ui.confirm(confirm_msg).then(function(ok) {
if (ok) {
$("pak-button").disabled = true;
$("pak-led").className = "led-pak-typing";
$("pak-led").title = "Autotyping...";
var index = 0; tools.debug("Paste-as-keys:", text);
var iterate = function() {
__emitShortcut(clipboard_codes[index]).then(function() { var index = 0;
++index; var iterate = function() {
if (index < clipboard_codes.length && __ws) { __emitShortcut(clipboard_codes[index]).then(function() {
iterate(); ++index;
} else { if (index < clipboard_codes.length && __ws) {
$("pak-button").disabled = false; iterate();
$("pak-led").className = "led-off"; } else {
$("pak-led").title = ""; $("pak-button").disabled = false;
} $("pak-led").className = "led-off";
}); $("pak-led").title = "";
}; }
iterate(); });
} };
}); iterate();
} }
}); });
}
}; };
__init__(); __init__();