diff options
author | Stefan Malzner <stefan@adlk.io> | 2017-10-13 12:29:40 +0200 |
---|---|---|
committer | Stefan Malzner <stefan@adlk.io> | 2017-10-13 12:29:40 +0200 |
commit | 58cda9cc7fb79ca9df6746de7f9662bc08dc156a (patch) | |
tree | 1211600c2a5d3b5f81c435c6896618111a611720 /src/helpers/webview-ime-focus-helpers.js | |
download | ferdium-app-58cda9cc7fb79ca9df6746de7f9662bc08dc156a.tar.gz ferdium-app-58cda9cc7fb79ca9df6746de7f9662bc08dc156a.tar.zst ferdium-app-58cda9cc7fb79ca9df6746de7f9662bc08dc156a.zip |
initial commit
Diffstat (limited to 'src/helpers/webview-ime-focus-helpers.js')
-rw-r--r-- | src/helpers/webview-ime-focus-helpers.js | 38 |
1 files changed, 38 insertions, 0 deletions
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 @@ | |||
1 | module.exports.releaseDocumentFocus = () => { | ||
2 | const element = document.createElement('span'); | ||
3 | document.body.appendChild(element); | ||
4 | |||
5 | const range = document.createRange(); | ||
6 | range.setStart(element, 0); | ||
7 | |||
8 | const selection = window.getSelection(); | ||
9 | selection.removeAllRanges(); | ||
10 | selection.addRange(range); | ||
11 | selection.removeAllRanges(); | ||
12 | |||
13 | document.body.removeChild(element); | ||
14 | }; | ||
15 | |||
16 | module.exports.claimDocumentFocus = () => { | ||
17 | const { activeElement } = document; | ||
18 | const selection = window.getSelection(); | ||
19 | |||
20 | let selectionStart; | ||
21 | let selectionEnd; | ||
22 | let range; | ||
23 | |||
24 | if (activeElement) ({ selectionStart, selectionEnd } = activeElement); | ||
25 | if (selection.rangeCount) range = selection.getRangeAt(0); | ||
26 | |||
27 | const restoreOriginalSelection = () => { | ||
28 | if (selectionStart >= 0 && selectionEnd >= 0) { | ||
29 | activeElement.selectionStart = selectionStart; | ||
30 | activeElement.selectionEnd = selectionEnd; | ||
31 | } else if (range) { | ||
32 | selection.addRange(range); | ||
33 | } | ||
34 | }; | ||
35 | |||
36 | exports.releaseDocumentFocus(); | ||
37 | window.requestAnimationFrame(restoreOriginalSelection); | ||
38 | }; | ||