diff options
author | Stefan Malzner <stefan@adlk.io> | 2018-12-07 22:39:12 +0100 |
---|---|---|
committer | Stefan Malzner <stefan@adlk.io> | 2018-12-07 22:39:12 +0100 |
commit | 65aaac06beac7f070a3a81adeffb8e1887d9f12b (patch) | |
tree | e2b4f452eef8c17198845e7a59c49b4fe28b1823 /src/webview/recipe.js | |
parent | feat(Service): Add option to change spellchecking language by service (diff) | |
download | ferdium-app-65aaac06beac7f070a3a81adeffb8e1887d9f12b.tar.gz ferdium-app-65aaac06beac7f070a3a81adeffb8e1887d9f12b.tar.zst ferdium-app-65aaac06beac7f070a3a81adeffb8e1887d9f12b.zip |
chore(Recipe): Refactor recipe plugin
Diffstat (limited to 'src/webview/recipe.js')
-rw-r--r-- | src/webview/recipe.js | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/src/webview/recipe.js b/src/webview/recipe.js new file mode 100644 index 000000000..a2c157af8 --- /dev/null +++ b/src/webview/recipe.js | |||
@@ -0,0 +1,124 @@ | |||
1 | import { ipcRenderer } from 'electron'; | ||
2 | import path from 'path'; | ||
3 | import { autorun, computed, observable } from 'mobx'; | ||
4 | |||
5 | import RecipeWebview from './lib/RecipeWebview'; | ||
6 | |||
7 | import spellchecker, { switchDict, disable as disableSpellchecker } from './spellchecker'; | ||
8 | import { injectDarkModeStyle, isDarkModeStyleInjected, removeDarkModeStyle } from './darkmode'; | ||
9 | import contextMenu from './contextMenu'; | ||
10 | import './notifications'; | ||
11 | |||
12 | const debug = require('debug')('Franz:Plugin'); | ||
13 | |||
14 | class RecipeController { | ||
15 | @observable settings = { | ||
16 | overrideSpellcheckerLanguage: false, | ||
17 | app: {}, | ||
18 | service: {}, | ||
19 | }; | ||
20 | |||
21 | spellcheckProvider = null; | ||
22 | |||
23 | ipcEvents = { | ||
24 | 'initialize-recipe': 'loadRecipeModule', | ||
25 | 'settings-update': 'updateAppSettings', | ||
26 | 'service-settings-update': 'updateServiceSettings', | ||
27 | 'get-service-id': 'serviceIdEcho', | ||
28 | } | ||
29 | |||
30 | constructor() { | ||
31 | this.initialize(); | ||
32 | } | ||
33 | |||
34 | @computed get spellcheckerLanguage() { | ||
35 | return this.settings.service.spellcheckerLanguage || this.settings.app.spellcheckerLanguage; | ||
36 | } | ||
37 | |||
38 | async initialize() { | ||
39 | Object.keys(this.ipcEvents).forEach((channel) => { | ||
40 | ipcRenderer.on(channel, (event, data) => { | ||
41 | debug('Received IPC event for channel', channel, 'with', data); | ||
42 | this[this.ipcEvents[channel]](event, data); | ||
43 | }); | ||
44 | }); | ||
45 | |||
46 | debug('Send "hello" to host'); | ||
47 | setTimeout(() => ipcRenderer.sendToHost('hello'), 100); | ||
48 | |||
49 | this.spellcheckingProvider = await spellchecker(); | ||
50 | contextMenu( | ||
51 | this.spellcheckingProvider, | ||
52 | () => this.settings.app.spellcheckerLanguage, | ||
53 | () => this.spellcheckerLanguage); | ||
54 | |||
55 | autorun(() => this.update()); | ||
56 | } | ||
57 | |||
58 | loadRecipeModule(event, data) { | ||
59 | debug('loadRecipeModule'); | ||
60 | const modulePath = path.join(data.recipe.path, 'webview.js'); | ||
61 | // Delete module from cache | ||
62 | delete require.cache[require.resolve(modulePath)]; | ||
63 | try { | ||
64 | // eslint-disable-next-line | ||
65 | require(modulePath)(new RecipeWebview(), data); | ||
66 | debug('Initialize Recipe', data); | ||
67 | |||
68 | this.settings.service = data; | ||
69 | } catch (err) { | ||
70 | console.error('Recipe initialization failed', err); | ||
71 | } | ||
72 | } | ||
73 | |||
74 | update() { | ||
75 | debug('enableSpellchecking', this.settings.app.enableSpellchecking); | ||
76 | debug('isDarkModeEnabled', this.settings.service.isDarkModeEnabled); | ||
77 | debug('System spellcheckerLanguage', this.settings.app.spellcheckerLanguage); | ||
78 | debug('Service spellcheckerLanguage', this.settings.service.spellcheckerLanguage); | ||
79 | |||
80 | if (this.settings.app.enableSpellchecking) { | ||
81 | debug('Setting spellchecker language to', this.spellcheckerLanguage); | ||
82 | switchDict(this.spellcheckerLanguage); | ||
83 | } else { | ||
84 | disableSpellchecker(); | ||
85 | } | ||
86 | |||
87 | console.log(this.settings.service); | ||
88 | if (this.settings.service.isDarkModeEnabled) { | ||
89 | debug('Enable dark mode'); | ||
90 | injectDarkModeStyle(this.settings.service.recipe.path); | ||
91 | } else if (isDarkModeStyleInjected()) { | ||
92 | debug('Remove dark mode'); | ||
93 | removeDarkModeStyle(); | ||
94 | } | ||
95 | } | ||
96 | |||
97 | updateAppSettings(event, data) { | ||
98 | this.settings.app = Object.assign(this.settings.app, data); | ||
99 | } | ||
100 | |||
101 | updateServiceSettings(event, data) { | ||
102 | this.settings.service = Object.assign(this.settings.service, data); | ||
103 | } | ||
104 | |||
105 | serviceIdEcho(event) { | ||
106 | event.sender.send('service-id', this.settings.service.id); | ||
107 | } | ||
108 | } | ||
109 | |||
110 | /* eslint-disable no-new */ | ||
111 | new RecipeController(); | ||
112 | /* eslint-enable no-new */ | ||
113 | |||
114 | // Patching window.open | ||
115 | const originalWindowOpen = window.open; | ||
116 | |||
117 | window.open = (url, frameName, features) => { | ||
118 | // We need to differentiate if the link should be opened in a popup or in the systems default browser | ||
119 | if (!frameName && !features) { | ||
120 | return ipcRenderer.sendToHost('new-window', url); | ||
121 | } | ||
122 | |||
123 | return originalWindowOpen(url, frameName, features); | ||
124 | }; | ||