diff --git a/edit/base.js b/edit/base.js index b983f525..948f1475 100644 --- a/edit/base.js +++ b/edit/base.js @@ -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'); diff --git a/edit/global-search.js b/edit/global-search.js index 359add49..64d0cee9 100644 --- a/edit/global-search.js +++ b/edit/global-search.js @@ -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 })(); diff --git a/js/dom.js b/js/dom.js index 1237f6b9..4e08b331 100644 --- a/js/dom.js +++ b/js/dom.js @@ -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