diff options
author | 2017-12-15 14:44:46 +0100 | |
---|---|---|
committer | 2017-12-15 14:44:46 +0100 | |
commit | dc1dd2e857114fac2462f18ea774ddacb287fa81 (patch) | |
tree | 32b701de50c505abc95ceddc7c429df85c81f041 /src/webview | |
parent | Remove IME handlers (diff) | |
parent | Merge pull request #475 from meetfranz/feature/service-improvements (diff) | |
download | ferdium-app-dc1dd2e857114fac2462f18ea774ddacb287fa81.tar.gz ferdium-app-dc1dd2e857114fac2462f18ea774ddacb287fa81.tar.zst ferdium-app-dc1dd2e857114fac2462f18ea774ddacb287fa81.zip |
Merge branch 'develop' into feature/macOS-copy-paste
Diffstat (limited to 'src/webview')
-rw-r--r-- | src/webview/plugin.js | 28 | ||||
-rw-r--r-- | src/webview/spellchecker.js | 65 |
2 files changed, 64 insertions, 29 deletions
diff --git a/src/webview/plugin.js b/src/webview/plugin.js index e2daf09dd..9903ee07a 100644 --- a/src/webview/plugin.js +++ b/src/webview/plugin.js | |||
@@ -1,13 +1,13 @@ | |||
1 | import { ipcRenderer } from 'electron'; | 1 | import { ipcRenderer } from 'electron'; |
2 | import { ContextMenuListener, ContextMenuBuilder } from 'electron-spellchecker'; | ||
2 | import path from 'path'; | 3 | import path from 'path'; |
3 | 4 | ||
5 | import { isDevMode } from '../environment'; | ||
4 | import RecipeWebview from './lib/RecipeWebview'; | 6 | import RecipeWebview from './lib/RecipeWebview'; |
5 | 7 | ||
6 | import Spellchecker from './spellchecker.js'; | 8 | import Spellchecker from './spellchecker.js'; |
7 | import './notifications.js'; | 9 | import './notifications.js'; |
8 | 10 | ||
9 | const spellchecker = new Spellchecker(); | ||
10 | |||
11 | ipcRenderer.on('initializeRecipe', (e, data) => { | 11 | ipcRenderer.on('initializeRecipe', (e, data) => { |
12 | const modulePath = path.join(data.recipe.path, 'webview.js'); | 12 | const modulePath = path.join(data.recipe.path, 'webview.js'); |
13 | // Delete module from cache | 13 | // Delete module from cache |
@@ -20,20 +20,22 @@ ipcRenderer.on('initializeRecipe', (e, data) => { | |||
20 | } | 20 | } |
21 | }); | 21 | }); |
22 | 22 | ||
23 | const spellchecker = new Spellchecker(); | ||
24 | spellchecker.initialize(); | ||
25 | |||
26 | const contextMenuBuilder = new ContextMenuBuilder(spellchecker.handler, null, isDevMode); | ||
27 | |||
28 | new ContextMenuListener((info) => { // eslint-disable-line | ||
29 | contextMenuBuilder.showPopupMenu(info); | ||
30 | }); | ||
31 | |||
23 | ipcRenderer.on('settings-update', (e, data) => { | 32 | ipcRenderer.on('settings-update', (e, data) => { |
24 | if (data.enableSpellchecking) { | 33 | console.log('settings-update', data); |
25 | if (!spellchecker.isEnabled) { | 34 | spellchecker.toggleSpellchecker(data.enableSpellchecking); |
26 | spellchecker.enable(); | ||
27 | |||
28 | // TODO: this does not work yet, needs more testing | ||
29 | // if (data.spellcheckingLanguage !== 'auto') { | ||
30 | // console.log('set spellchecking language to', data.spellcheckingLanguage); | ||
31 | // spellchecker.switchLanguage(data.spellcheckingLanguage); | ||
32 | // } | ||
33 | } | ||
34 | } | ||
35 | }); | 35 | }); |
36 | 36 | ||
37 | // initSpellche | ||
38 | |||
37 | document.addEventListener('DOMContentLoaded', () => { | 39 | document.addEventListener('DOMContentLoaded', () => { |
38 | ipcRenderer.sendToHost('hello'); | 40 | ipcRenderer.sendToHost('hello'); |
39 | }, false); | 41 | }, false); |
diff --git a/src/webview/spellchecker.js b/src/webview/spellchecker.js index 5beb77e03..a504a4039 100644 --- a/src/webview/spellchecker.js +++ b/src/webview/spellchecker.js | |||
@@ -1,30 +1,63 @@ | |||
1 | import { SpellCheckHandler, ContextMenuListener, ContextMenuBuilder } from 'electron-spellchecker'; | 1 | import { SpellCheckHandler } from 'electron-spellchecker'; |
2 | 2 | ||
3 | import { isMac } from '../environment'; | 3 | import { isMac } from '../environment'; |
4 | 4 | ||
5 | export default class Spellchecker { | 5 | export default class Spellchecker { |
6 | isEnabled = false; | 6 | isInitialized = false; |
7 | spellchecker = null; | 7 | handler = null; |
8 | initRetries = 0; | ||
9 | DOMCheckInterval = null; | ||
10 | |||
11 | get inputs() { | ||
12 | return document.querySelectorAll('input[type="text"], [contenteditable="true"], textarea'); | ||
13 | } | ||
14 | |||
15 | initialize() { | ||
16 | this.handler = new SpellCheckHandler(); | ||
8 | 17 | ||
9 | enable() { | ||
10 | this.spellchecker = new SpellCheckHandler(); | ||
11 | if (!isMac) { | 18 | if (!isMac) { |
12 | this.spellchecker.attachToInput(); | 19 | this.attach(); |
13 | this.spellchecker.switchLanguage(navigator.language); | 20 | } else { |
21 | this.isInitialized = true; | ||
22 | } | ||
23 | } | ||
24 | |||
25 | attach() { | ||
26 | let initFailed = false; | ||
27 | |||
28 | if (this.initRetries > 3) { | ||
29 | console.error('Could not initialize spellchecker'); | ||
30 | return; | ||
14 | } | 31 | } |
15 | 32 | ||
16 | const contextMenuBuilder = new ContextMenuBuilder(this.spellchecker); | 33 | try { |
34 | this.handler.attachToInput(); | ||
35 | this.handler.switchLanguage(navigator.language); | ||
36 | } catch (err) { | ||
37 | initFailed = true; | ||
38 | this.initRetries = +1; | ||
39 | setTimeout(() => { this.attach(); console.warn('Spellchecker init failed, trying again in 5s'); }, 5000); | ||
40 | } | ||
17 | 41 | ||
18 | new ContextMenuListener((info) => { // eslint-disable-line | 42 | if (!initFailed) { |
19 | contextMenuBuilder.showPopupMenu(info); | 43 | this.isInitialized = true; |
44 | } | ||
45 | } | ||
46 | |||
47 | toggleSpellchecker(enable = false) { | ||
48 | this.inputs.forEach((input) => { | ||
49 | input.setAttribute('spellcheck', enable); | ||
20 | }); | 50 | }); |
51 | |||
52 | this.intervalHandler(enable); | ||
21 | } | 53 | } |
22 | 54 | ||
23 | // TODO: this does not work yet, needs more testing | 55 | intervalHandler(enable) { |
24 | // switchLanguage(language) { | 56 | clearInterval(this.DOMCheckInterval); |
25 | // if (language !== 'auto') { | 57 | |
26 | // this.spellchecker.switchLanguage(language); | 58 | if (enable) { |
27 | // } | 59 | this.DOMCheckInterval = setInterval(() => this.toggleSpellchecker(enable), 30000); |
28 | // } | 60 | } |
61 | } | ||
29 | } | 62 | } |
30 | 63 | ||