diff options
Diffstat (limited to 'src/webview/recipe.js')
-rw-r--r-- | src/webview/recipe.js | 76 |
1 files changed, 62 insertions, 14 deletions
diff --git a/src/webview/recipe.js b/src/webview/recipe.js index 1a22542d8..bad5a93b2 100644 --- a/src/webview/recipe.js +++ b/src/webview/recipe.js | |||
@@ -1,10 +1,11 @@ | |||
1 | /* eslint-disable import/first */ | 1 | /* eslint-disable import/first */ |
2 | import { ipcRenderer } from 'electron'; | 2 | import { ipcRenderer, remote } from 'electron'; |
3 | import path from 'path'; | 3 | import path from 'path'; |
4 | import { autorun, computed, observable } from 'mobx'; | 4 | import { autorun, computed, observable } from 'mobx'; |
5 | import fs from 'fs-extra'; | 5 | import fs from 'fs-extra'; |
6 | import { loadModule } from 'cld3-asm'; | 6 | import { loadModule } from 'cld3-asm'; |
7 | import { debounce } from 'lodash'; | 7 | import { debounce } from 'lodash'; |
8 | import { FindInPage } from 'electron-find'; | ||
8 | 9 | ||
9 | // For some services darkreader tries to use the chrome extension message API | 10 | // For some services darkreader tries to use the chrome extension message API |
10 | // This will cause the service to fail loading | 11 | // This will cause the service to fail loading |
@@ -23,7 +24,6 @@ import RecipeWebview from './lib/RecipeWebview'; | |||
23 | 24 | ||
24 | import spellchecker, { switchDict, disable as disableSpellchecker, getSpellcheckerLocaleByFuzzyIdentifier } from './spellchecker'; | 25 | import spellchecker, { switchDict, disable as disableSpellchecker, getSpellcheckerLocaleByFuzzyIdentifier } from './spellchecker'; |
25 | import { injectDarkModeStyle, isDarkModeStyleInjected, removeDarkModeStyle } from './darkmode'; | 26 | import { injectDarkModeStyle, isDarkModeStyleInjected, removeDarkModeStyle } from './darkmode'; |
26 | import contextMenu from './contextMenu'; | ||
27 | import './notifications'; | 27 | import './notifications'; |
28 | 28 | ||
29 | import { DEFAULT_APP_SETTINGS } from '../config'; | 29 | import { DEFAULT_APP_SETTINGS } from '../config'; |
@@ -48,10 +48,15 @@ class RecipeController { | |||
48 | 'settings-update': 'updateAppSettings', | 48 | 'settings-update': 'updateAppSettings', |
49 | 'service-settings-update': 'updateServiceSettings', | 49 | 'service-settings-update': 'updateServiceSettings', |
50 | 'get-service-id': 'serviceIdEcho', | 50 | 'get-service-id': 'serviceIdEcho', |
51 | 'find-in-page': 'openFindInPage', | ||
51 | }; | 52 | }; |
52 | 53 | ||
53 | universalDarkModeInjected = false; | 54 | universalDarkModeInjected = false; |
54 | 55 | ||
56 | recipe = null; | ||
57 | |||
58 | hasUpdatedBeforeRecipeLoaded = false; | ||
59 | |||
55 | constructor() { | 60 | constructor() { |
56 | this.initialize(); | 61 | this.initialize(); |
57 | } | 62 | } |
@@ -62,6 +67,8 @@ class RecipeController { | |||
62 | 67 | ||
63 | cldIdentifier = null; | 68 | cldIdentifier = null; |
64 | 69 | ||
70 | findInPage = null; | ||
71 | |||
65 | async initialize() { | 72 | async initialize() { |
66 | Object.keys(this.ipcEvents).forEach((channel) => { | 73 | Object.keys(this.ipcEvents).forEach((channel) => { |
67 | ipcRenderer.on(channel, (...args) => { | 74 | ipcRenderer.on(channel, (...args) => { |
@@ -72,16 +79,15 @@ class RecipeController { | |||
72 | 79 | ||
73 | debug('Send "hello" to host'); | 80 | debug('Send "hello" to host'); |
74 | setTimeout(() => ipcRenderer.sendToHost('hello'), 100); | 81 | setTimeout(() => ipcRenderer.sendToHost('hello'), 100); |
75 | 82 | await spellchecker(); | |
76 | this.spellcheckingProvider = await spellchecker(); | ||
77 | contextMenu( | ||
78 | this.spellcheckingProvider, | ||
79 | () => this.settings.app.enableSpellchecking, | ||
80 | () => this.settings.app.spellcheckerLanguage, | ||
81 | () => this.spellcheckerLanguage, | ||
82 | ); | ||
83 | |||
84 | autorun(() => this.update()); | 83 | autorun(() => this.update()); |
84 | |||
85 | document.addEventListener('DOMContentLoaded', () => { | ||
86 | this.findInPage = new FindInPage(remote.getCurrentWebContents(), { | ||
87 | inputFocusColor: '#CE9FFC', | ||
88 | textColor: '#212121', | ||
89 | }); | ||
90 | }); | ||
85 | } | 91 | } |
86 | 92 | ||
87 | loadRecipeModule(event, config, recipe) { | 93 | loadRecipeModule(event, config, recipe) { |
@@ -91,11 +97,15 @@ class RecipeController { | |||
91 | // Delete module from cache | 97 | // Delete module from cache |
92 | delete require.cache[require.resolve(modulePath)]; | 98 | delete require.cache[require.resolve(modulePath)]; |
93 | try { | 99 | try { |
100 | this.recipe = new RecipeWebview(); | ||
94 | // eslint-disable-next-line | 101 | // eslint-disable-next-line |
95 | require(modulePath)(new RecipeWebview(), {...config, recipe,}); | 102 | require(modulePath)(this.recipe, {...config, recipe,}); |
96 | debug('Initialize Recipe', config, recipe); | 103 | debug('Initialize Recipe', config, recipe); |
97 | 104 | ||
98 | this.settings.service = Object.assign(config, { recipe }); | 105 | this.settings.service = Object.assign(config, { recipe }); |
106 | |||
107 | // Make sure to update the WebView, otherwise the custom darkmode handler may not be used | ||
108 | this.update(); | ||
99 | } catch (err) { | 109 | } catch (err) { |
100 | console.error('Recipe initialization failed', err); | 110 | console.error('Recipe initialization failed', err); |
101 | } | 111 | } |
@@ -134,6 +144,10 @@ class RecipeController { | |||
134 | } | 144 | } |
135 | } | 145 | } |
136 | 146 | ||
147 | openFindInPage() { | ||
148 | this.findInPage.openFindWindow(); | ||
149 | } | ||
150 | |||
137 | update() { | 151 | update() { |
138 | debug('enableSpellchecking', this.settings.app.enableSpellchecking); | 152 | debug('enableSpellchecking', this.settings.app.enableSpellchecking); |
139 | debug('isDarkModeEnabled', this.settings.service.isDarkModeEnabled); | 153 | debug('isDarkModeEnabled', this.settings.service.isDarkModeEnabled); |
@@ -160,12 +174,25 @@ class RecipeController { | |||
160 | } | 174 | } |
161 | } | 175 | } |
162 | 176 | ||
177 | if (!this.recipe) { | ||
178 | this.hasUpdatedBeforeRecipeLoaded = true; | ||
179 | } | ||
180 | |||
163 | console.log( | 181 | console.log( |
164 | 'Darkmode enabled?', | 182 | 'Darkmode enabled?', |
165 | this.settings.service.isDarkModeEnabled, | 183 | this.settings.service.isDarkModeEnabled, |
166 | 'Dark theme active?', | 184 | 'Dark theme active?', |
167 | this.settings.app.isDarkThemeActive, | 185 | this.settings.app.isDarkThemeActive, |
168 | ); | 186 | ); |
187 | |||
188 | const handlerConfig = { | ||
189 | removeDarkModeStyle, | ||
190 | disableDarkMode, | ||
191 | enableDarkMode, | ||
192 | injectDarkModeStyle: () => injectDarkModeStyle(this.settings.service.recipe.path), | ||
193 | isDarkModeStyleInjected, | ||
194 | }; | ||
195 | |||
169 | if (this.settings.service.isDarkModeEnabled && this.settings.app.isDarkThemeActive !== false) { | 196 | if (this.settings.service.isDarkModeEnabled && this.settings.app.isDarkThemeActive !== false) { |
170 | debug('Enable dark mode'); | 197 | debug('Enable dark mode'); |
171 | 198 | ||
@@ -175,7 +202,19 @@ class RecipeController { | |||
175 | 202 | ||
176 | console.log('darkmode.css exists? ', darkModeExists ? 'Yes' : 'No'); | 203 | console.log('darkmode.css exists? ', darkModeExists ? 'Yes' : 'No'); |
177 | 204 | ||
178 | if (darkModeExists) { | 205 | // Check if recipe has a custom dark mode handler |
206 | if (this.recipe && this.recipe.darkModeHandler) { | ||
207 | console.log('Using custom dark mode handler'); | ||
208 | |||
209 | // Remove other dark mode styles if they were already loaded | ||
210 | if (this.hasUpdatedBeforeRecipeLoaded) { | ||
211 | this.hasUpdatedBeforeRecipeLoaded = false; | ||
212 | removeDarkModeStyle(); | ||
213 | disableDarkMode(); | ||
214 | } | ||
215 | |||
216 | this.recipe.darkModeHandler(true, handlerConfig); | ||
217 | } else if (darkModeExists) { | ||
179 | console.log('Injecting darkmode.css'); | 218 | console.log('Injecting darkmode.css'); |
180 | injectDarkModeStyle(this.settings.service.recipe.path); | 219 | injectDarkModeStyle(this.settings.service.recipe.path); |
181 | 220 | ||
@@ -195,7 +234,16 @@ class RecipeController { | |||
195 | debug('Remove dark mode'); | 234 | debug('Remove dark mode'); |
196 | console.log('DarkMode disabled - removing remaining styles'); | 235 | console.log('DarkMode disabled - removing remaining styles'); |
197 | 236 | ||
198 | if (isDarkModeStyleInjected()) { | 237 | if (this.recipe && this.recipe.darkModeHandler) { |
238 | // Remove other dark mode styles if they were already loaded | ||
239 | if (this.hasUpdatedBeforeRecipeLoaded) { | ||
240 | this.hasUpdatedBeforeRecipeLoaded = false; | ||
241 | removeDarkModeStyle(); | ||
242 | disableDarkMode(); | ||
243 | } | ||
244 | |||
245 | this.recipe.darkModeHandler(false, handlerConfig); | ||
246 | } else if (isDarkModeStyleInjected()) { | ||
199 | console.log('Removing injected darkmode.css'); | 247 | console.log('Removing injected darkmode.css'); |
200 | removeDarkModeStyle(); | 248 | removeDarkModeStyle(); |
201 | } else { | 249 | } else { |