From 796b2b132d39f34f4b9bec985694b38987814def Mon Sep 17 00:00:00 2001 From: tophf Date: Fri, 6 Jul 2018 12:37:35 +0300 Subject: [PATCH] FF execCommand bug workaround needs 'input' event dispatched fixes #419 --- edit/global-search.js | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/edit/global-search.js b/edit/global-search.js index d55923d9..6d2ebe7f 100644 --- a/edit/global-search.js +++ b/edit/global-search.js @@ -93,11 +93,7 @@ onDOMready().then(() => { replaceAll: () => doReplaceAll(), undo: () => doUndo(), clear() { - this._input.focus(); - document.execCommand('selectAll', false, null); - document.execCommand('delete', false, null); - // some versions of Firefox ignore the above - this._input.value = ''; + setInputValue(this._input, ''); }, case() { state.icase = !state.icase; @@ -545,11 +541,7 @@ onDOMready().then(() => { destroyDialog(); createDialog(type); } else if (sel) { - state.input.focus(); - state.input.select(); - document.execCommand('insertText', false, sel); - // some versions of Firefox ignore the above - state.input.value = sel; + setInputValue(state.input, sel); } state.input.focus(); @@ -933,4 +925,19 @@ onDOMready().then(() => { }) })); } + + + 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 });