aboutsummaryrefslogtreecommitdiffstats
path: root/src/webview
diff options
context:
space:
mode:
Diffstat (limited to 'src/webview')
-rw-r--r--src/webview/ime.js10
-rw-r--r--src/webview/lib/RecipeWebview.js6
-rw-r--r--src/webview/notifications.js4
-rw-r--r--src/webview/plugin.js29
-rw-r--r--src/webview/spellchecker.js71
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 @@
1const { ipcRenderer } = require('electron');
2const { claimDocumentFocus } = require('../helpers/webview-ime-focus-helpers');
3
4ipcRenderer.on('claim-document-focus', claimDocumentFocus);
5
6window.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 @@
1const { ipcRenderer } = require('electron'); 1import { ipcRenderer } from 'electron';
2const path = require('path'); 2import { ContextMenuListener, ContextMenuBuilder } from 'electron-spellchecker';
3import path from 'path';
3 4
4const RecipeWebview = require('./lib/RecipeWebview'); 5import { isDevMode } from '../environment';
6import RecipeWebview from './lib/RecipeWebview';
5 7
6require('./notifications.js'); 8import Spellchecker from './spellchecker.js';
7require('./spellchecker.js'); 9import './notifications.js';
8require('./ime.js');
9 10
10ipcRenderer.on('initializeRecipe', (e, data) => { 11ipcRenderer.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
23const spellchecker = new Spellchecker();
24spellchecker.initialize();
25
26const contextMenuBuilder = new ContextMenuBuilder(spellchecker.handler, null, isDevMode);
27
28new ContextMenuListener((info) => { // eslint-disable-line
29 contextMenuBuilder.showPopupMenu(info);
30});
31
32ipcRenderer.on('settings-update', (e, data) => {
33 console.log('settings-update', data);
34 spellchecker.toggleSpellchecker(data.enableSpellchecking);
35});
36
37// initSpellche
38
22document.addEventListener('DOMContentLoaded', () => { 39document.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 @@
1import { SpellCheckHandler, ContextMenuListener, ContextMenuBuilder } from 'electron-spellchecker'; 1import { SpellCheckHandler } from 'electron-spellchecker';
2 2
3window.spellCheckHandler = new SpellCheckHandler(); 3import { isMac } from '../environment';
4setTimeout(() => {
5 window.spellCheckHandler.attachToInput();
6}, 1000);
7 4
8// TODO: should we set the language to user settings? 5export 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
11const contextMenuBuilder = new ContextMenuBuilder(window.spellCheckHandler);
12const contextMenuListener = new ContextMenuListener((info) => { // eslint-disable-line
13 contextMenuBuilder.showPopupMenu(info);
14});