diff options
Diffstat (limited to 'src/webview')
-rw-r--r-- | src/webview/ime.js | 10 | ||||
-rw-r--r-- | src/webview/lib/RecipeWebview.js | 6 | ||||
-rw-r--r-- | src/webview/notifications.js | 4 | ||||
-rw-r--r-- | src/webview/plugin.js | 29 | ||||
-rw-r--r-- | src/webview/spellchecker.js | 71 |
5 files changed, 88 insertions, 32 deletions
diff --git a/src/webview/ime.js b/src/webview/ime.js deleted file mode 100644 index 43df6267c..000000000 --- a/src/webview/ime.js +++ /dev/null | |||
@@ -1,10 +0,0 @@ | |||
1 | const { ipcRenderer } = require('electron'); | ||
2 | const { claimDocumentFocus } = require('../helpers/webview-ime-focus-helpers'); | ||
3 | |||
4 | ipcRenderer.on('claim-document-focus', claimDocumentFocus); | ||
5 | |||
6 | window.addEventListener('DOMContentLoaded', () => { | ||
7 | if (document.querySelector('[autofocus]')) { | ||
8 | ipcRenderer.sendToHost('autofocus'); | ||
9 | } | ||
10 | }); | ||
diff --git a/src/webview/lib/RecipeWebview.js b/src/webview/lib/RecipeWebview.js index b8acc1258..be29142af 100644 --- a/src/webview/lib/RecipeWebview.js +++ b/src/webview/lib/RecipeWebview.js | |||
@@ -40,8 +40,8 @@ class RecipeWebview { | |||
40 | && this.countCache.indirect === indirect) return; | 40 | && this.countCache.indirect === indirect) return; |
41 | 41 | ||
42 | const count = { | 42 | const count = { |
43 | direct, | 43 | direct: direct > 0 ? direct : 0, |
44 | indirect, | 44 | indirect: indirect > 0 ? indirect : 0, |
45 | }; | 45 | }; |
46 | 46 | ||
47 | ipcRenderer.sendToHost('messages', count); | 47 | ipcRenderer.sendToHost('messages', count); |
@@ -66,7 +66,7 @@ class RecipeWebview { | |||
66 | 66 | ||
67 | onNotify(fn) { | 67 | onNotify(fn) { |
68 | if (typeof fn === 'function') { | 68 | if (typeof fn === 'function') { |
69 | window.Notification.onNotify = fn; | 69 | window.Notification.prototype.onNotify = fn; |
70 | } | 70 | } |
71 | } | 71 | } |
72 | 72 | ||
diff --git a/src/webview/notifications.js b/src/webview/notifications.js index 4055b10de..4f602bfdb 100644 --- a/src/webview/notifications.js +++ b/src/webview/notifications.js | |||
@@ -10,9 +10,9 @@ class Notification { | |||
10 | this.notificationId = uuidV1(); | 10 | this.notificationId = uuidV1(); |
11 | 11 | ||
12 | ipcRenderer.sendToHost('notification', this.onNotify({ | 12 | ipcRenderer.sendToHost('notification', this.onNotify({ |
13 | title: this.title, | ||
14 | options: this.options, | ||
13 | notificationId: this.notificationId, | 15 | notificationId: this.notificationId, |
14 | title, | ||
15 | options, | ||
16 | })); | 16 | })); |
17 | 17 | ||
18 | ipcRenderer.once(`notification-onclick:${this.notificationId}`, () => { | 18 | ipcRenderer.once(`notification-onclick:${this.notificationId}`, () => { |
diff --git a/src/webview/plugin.js b/src/webview/plugin.js index ffc9084e4..9903ee07a 100644 --- a/src/webview/plugin.js +++ b/src/webview/plugin.js | |||
@@ -1,11 +1,12 @@ | |||
1 | const { ipcRenderer } = require('electron'); | 1 | import { ipcRenderer } from 'electron'; |
2 | const path = require('path'); | 2 | import { ContextMenuListener, ContextMenuBuilder } from 'electron-spellchecker'; |
3 | import path from 'path'; | ||
3 | 4 | ||
4 | const RecipeWebview = require('./lib/RecipeWebview'); | 5 | import { isDevMode } from '../environment'; |
6 | import RecipeWebview from './lib/RecipeWebview'; | ||
5 | 7 | ||
6 | require('./notifications.js'); | 8 | import Spellchecker from './spellchecker.js'; |
7 | require('./spellchecker.js'); | 9 | import './notifications.js'; |
8 | require('./ime.js'); | ||
9 | 10 | ||
10 | ipcRenderer.on('initializeRecipe', (e, data) => { | 11 | ipcRenderer.on('initializeRecipe', (e, data) => { |
11 | const modulePath = path.join(data.recipe.path, 'webview.js'); | 12 | const modulePath = path.join(data.recipe.path, 'webview.js'); |
@@ -19,6 +20,22 @@ ipcRenderer.on('initializeRecipe', (e, data) => { | |||
19 | } | 20 | } |
20 | }); | 21 | }); |
21 | 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 | |||
32 | ipcRenderer.on('settings-update', (e, data) => { | ||
33 | console.log('settings-update', data); | ||
34 | spellchecker.toggleSpellchecker(data.enableSpellchecking); | ||
35 | }); | ||
36 | |||
37 | // initSpellche | ||
38 | |||
22 | document.addEventListener('DOMContentLoaded', () => { | 39 | document.addEventListener('DOMContentLoaded', () => { |
23 | ipcRenderer.sendToHost('hello'); | 40 | ipcRenderer.sendToHost('hello'); |
24 | }, false); | 41 | }, false); |
diff --git a/src/webview/spellchecker.js b/src/webview/spellchecker.js index ec8807874..a504a4039 100644 --- a/src/webview/spellchecker.js +++ b/src/webview/spellchecker.js | |||
@@ -1,14 +1,63 @@ | |||
1 | import { SpellCheckHandler, ContextMenuListener, ContextMenuBuilder } from 'electron-spellchecker'; | 1 | import { SpellCheckHandler } from 'electron-spellchecker'; |
2 | 2 | ||
3 | window.spellCheckHandler = new SpellCheckHandler(); | 3 | import { isMac } from '../environment'; |
4 | setTimeout(() => { | ||
5 | window.spellCheckHandler.attachToInput(); | ||
6 | }, 1000); | ||
7 | 4 | ||
8 | // TODO: should we set the language to user settings? | 5 | export default class Spellchecker { |
9 | // window.spellCheckHandler.switchLanguage('en-US'); | 6 | isInitialized = false; |
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(); | ||
17 | |||
18 | if (!isMac) { | ||
19 | this.attach(); | ||
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; | ||
31 | } | ||
32 | |||
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 | } | ||
41 | |||
42 | if (!initFailed) { | ||
43 | this.isInitialized = true; | ||
44 | } | ||
45 | } | ||
46 | |||
47 | toggleSpellchecker(enable = false) { | ||
48 | this.inputs.forEach((input) => { | ||
49 | input.setAttribute('spellcheck', enable); | ||
50 | }); | ||
51 | |||
52 | this.intervalHandler(enable); | ||
53 | } | ||
54 | |||
55 | intervalHandler(enable) { | ||
56 | clearInterval(this.DOMCheckInterval); | ||
57 | |||
58 | if (enable) { | ||
59 | this.DOMCheckInterval = setInterval(() => this.toggleSpellchecker(enable), 30000); | ||
60 | } | ||
61 | } | ||
62 | } | ||
10 | 63 | ||
11 | const contextMenuBuilder = new ContextMenuBuilder(window.spellCheckHandler); | ||
12 | const contextMenuListener = new ContextMenuListener((info) => { // eslint-disable-line | ||
13 | contextMenuBuilder.showPopupMenu(info); | ||
14 | }); | ||