reuse setInputValue

This commit is contained in:
tophf 2022-09-05 22:41:17 +03:00
parent ef998e423e
commit 48d90544f6
3 changed files with 17 additions and 24 deletions

View File

@ -1,4 +1,4 @@
/* global $$ $ $create messageBoxProxy setupLivePrefs */// dom.js
/* global $$ $ $create messageBoxProxy setInputValue setupLivePrefs */// dom.js
/* global API */// msg.js
/* global CODEMIRROR_THEMES */
/* global CodeMirror */
@ -136,15 +136,8 @@ function EditorHeader() {
});
resetEl.hidden = !editor.style.customName;
resetEl.onclick = () => {
const {style} = editor;
nameEl.focus();
nameEl.select();
// trying to make it undoable via Ctrl-Z
if (!document.execCommand('insertText', false, style.name)) {
nameEl.value = style.name;
editor.updateName(true);
}
style.customName = null; // to delete it from db
editor.style.customName = null; // to delete it from db
setInputValue(nameEl, editor.style.name);
resetEl.hidden = true;
};
const enabledEl = $('#enabled');

View File

@ -1,4 +1,4 @@
/* global $ $$ $create $remove focusAccessibility toggleDataset */// dom.js
/* global $ $$ $create $remove focusAccessibility setInputValue toggleDataset */// dom.js
/* global CodeMirror */
/* global chromeLocal */// storage-util.js
/* global colorMimicry */
@ -930,18 +930,5 @@
})));
}
function setInputValue(input, value) {
input.focus();
input.select();
// using execCommand to add to the input's undo history
document.execCommand(value ? 'insertText' : 'delete', false, value);
// some versions of Firefox ignore execCommand
if (input.value !== value) {
input.value = value;
input.dispatchEvent(new Event('input', {bubbles: true}));
}
}
//endregion
})();

View File

@ -13,6 +13,7 @@
messageBoxProxy
moveFocus
scrollElementIntoView
setInputValue
setupLivePrefs
showSpinner
toggleDataset
@ -296,6 +297,18 @@ function scrollElementIntoView(element, {invalidMarginRatio = 0} = {}) {
}
}
function setInputValue(input, value) {
input.focus();
input.select();
// using execCommand to add to the input's undo history
document.execCommand(value ? 'insertText' : 'delete', false, value);
// some versions of Firefox ignore execCommand
if (input.value !== value) {
input.value = value;
input.dispatchEvent(new Event('input', {bubbles: true}));
}
}
/**
* Accepts an array of pref names (values are fetched via prefs.get)
* and establishes a two-way connection between the document elements and the actual prefs