From 58cda9cc7fb79ca9df6746de7f9662bc08dc156a Mon Sep 17 00:00:00 2001 From: Stefan Malzner Date: Fri, 13 Oct 2017 12:29:40 +0200 Subject: initial commit --- src/helpers/webview-ime-focus-helpers.js | 38 ++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 src/helpers/webview-ime-focus-helpers.js (limited to 'src/helpers/webview-ime-focus-helpers.js') diff --git a/src/helpers/webview-ime-focus-helpers.js b/src/helpers/webview-ime-focus-helpers.js new file mode 100644 index 000000000..2593a5f26 --- /dev/null +++ b/src/helpers/webview-ime-focus-helpers.js @@ -0,0 +1,38 @@ +module.exports.releaseDocumentFocus = () => { + const element = document.createElement('span'); + document.body.appendChild(element); + + const range = document.createRange(); + range.setStart(element, 0); + + const selection = window.getSelection(); + selection.removeAllRanges(); + selection.addRange(range); + selection.removeAllRanges(); + + document.body.removeChild(element); +}; + +module.exports.claimDocumentFocus = () => { + const { activeElement } = document; + const selection = window.getSelection(); + + let selectionStart; + let selectionEnd; + let range; + + if (activeElement) ({ selectionStart, selectionEnd } = activeElement); + if (selection.rangeCount) range = selection.getRangeAt(0); + + const restoreOriginalSelection = () => { + if (selectionStart >= 0 && selectionEnd >= 0) { + activeElement.selectionStart = selectionStart; + activeElement.selectionEnd = selectionEnd; + } else if (range) { + selection.addRange(range); + } + }; + + exports.releaseDocumentFocus(); + window.requestAnimationFrame(restoreOriginalSelection); +}; -- cgit v1.2.3-70-g09d2