diff options
Diffstat (limited to 'src/webview/recipe.js')
-rw-r--r-- | src/webview/recipe.js | 61 |
1 files changed, 28 insertions, 33 deletions
diff --git a/src/webview/recipe.js b/src/webview/recipe.js index 675f8e311..d29f3edbd 100644 --- a/src/webview/recipe.js +++ b/src/webview/recipe.js | |||
@@ -3,7 +3,6 @@ import { ipcRenderer, remote, desktopCapturer } 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'; | ||
7 | import { debounce } from 'lodash'; | 6 | import { debounce } from 'lodash'; |
8 | import { FindInPage } from 'electron-find'; | 7 | import { FindInPage } from 'electron-find'; |
9 | 8 | ||
@@ -23,8 +22,9 @@ import customDarkModeCss from './darkmode/custom'; | |||
23 | import RecipeWebview from './lib/RecipeWebview'; | 22 | import RecipeWebview from './lib/RecipeWebview'; |
24 | import Userscript from './lib/Userscript'; | 23 | import Userscript from './lib/Userscript'; |
25 | 24 | ||
26 | import spellchecker, { switchDict, disable as disableSpellchecker, getSpellcheckerLocaleByFuzzyIdentifier } from './spellchecker'; | 25 | import { switchDict, getSpellcheckerLocaleByFuzzyIdentifier } from './spellchecker'; |
27 | import { injectDarkModeStyle, isDarkModeStyleInjected, removeDarkModeStyle } from './darkmode'; | 26 | import { injectDarkModeStyle, isDarkModeStyleInjected, removeDarkModeStyle } from './darkmode'; |
27 | import contextMenu from './contextMenu'; | ||
28 | import './notifications'; | 28 | import './notifications'; |
29 | 29 | ||
30 | import { DEFAULT_APP_SETTINGS } from '../config'; | 30 | import { DEFAULT_APP_SETTINGS } from '../config'; |
@@ -156,7 +156,14 @@ class RecipeController { | |||
156 | 156 | ||
157 | debug('Send "hello" to host'); | 157 | debug('Send "hello" to host'); |
158 | setTimeout(() => ipcRenderer.sendToHost('hello'), 100); | 158 | setTimeout(() => ipcRenderer.sendToHost('hello'), 100); |
159 | await spellchecker(); | 159 | |
160 | this.spellcheckingProvider = null; | ||
161 | contextMenu( | ||
162 | () => this.settings.app.enableSpellchecking, | ||
163 | () => this.settings.app.spellcheckerLanguage, | ||
164 | () => this.spellcheckerLanguage, | ||
165 | ); | ||
166 | |||
160 | autorun(() => this.update()); | 167 | autorun(() => this.update()); |
161 | 168 | ||
162 | document.addEventListener('DOMContentLoaded', () => { | 169 | document.addEventListener('DOMContentLoaded', () => { |
@@ -240,22 +247,15 @@ class RecipeController { | |||
240 | 247 | ||
241 | if (this.settings.app.enableSpellchecking) { | 248 | if (this.settings.app.enableSpellchecking) { |
242 | debug('Setting spellchecker language to', this.spellcheckerLanguage); | 249 | debug('Setting spellchecker language to', this.spellcheckerLanguage); |
243 | const { spellcheckerLanguage } = this; | 250 | let { spellcheckerLanguage } = this; |
244 | if (spellcheckerLanguage.includes('automatic')) { | 251 | if (spellcheckerLanguage.includes('automatic')) { |
245 | this.automaticLanguageDetection(); | 252 | this.automaticLanguageDetection(); |
246 | debug('Found `automatic` locale, falling back to user locale until detected', this.settings.app.locale); | 253 | debug('Found `automatic` locale, falling back to user locale until detected', this.settings.app.locale); |
247 | spellcheckerLanguage.push(this.settings.app.locale); | 254 | spellcheckerLanguage = this.settings.app.locale; |
248 | } else if (this.cldIdentifier) { | ||
249 | this.cldIdentifier.destroy(); | ||
250 | } | 255 | } |
251 | switchDict(spellcheckerLanguage); | 256 | switchDict(spellcheckerLanguage); |
252 | } else { | 257 | } else { |
253 | debug('Disable spellchecker'); | 258 | debug('Disable spellchecker'); |
254 | disableSpellchecker(); | ||
255 | |||
256 | if (this.cldIdentifier) { | ||
257 | this.cldIdentifier.destroy(); | ||
258 | } | ||
259 | } | 259 | } |
260 | 260 | ||
261 | if (!this.recipe) { | 261 | if (!this.recipe) { |
@@ -366,10 +366,7 @@ class RecipeController { | |||
366 | } | 366 | } |
367 | 367 | ||
368 | async automaticLanguageDetection() { | 368 | async automaticLanguageDetection() { |
369 | const cldFactory = await loadModule(); | 369 | window.addEventListener('keyup', debounce(async (e) => { |
370 | this.cldIdentifier = cldFactory.create(0, 1000); | ||
371 | |||
372 | window.addEventListener('keyup', debounce((e) => { | ||
373 | const element = e.target; | 370 | const element = e.target; |
374 | 371 | ||
375 | if (!element) return; | 372 | if (!element) return; |
@@ -382,22 +379,15 @@ class RecipeController { | |||
382 | } | 379 | } |
383 | 380 | ||
384 | // Force a minimum length to get better detection results | 381 | // Force a minimum length to get better detection results |
385 | if (value.length < 30) return; | 382 | if (value.length < 25) return; |
386 | 383 | ||
387 | debug('Detecting language for', value); | 384 | debug('Detecting language for', value); |
388 | const findResult = this.cldIdentifier.findLanguage(value); | 385 | const locale = await ipcRenderer.invoke('detect-language', { sample: value }); |
389 | 386 | ||
390 | debug('Language detection result', findResult); | 387 | const spellcheckerLocale = getSpellcheckerLocaleByFuzzyIdentifier(locale); |
391 | 388 | debug('Language detected reliably, setting spellchecker language to', spellcheckerLocale); | |
392 | if (findResult.is_reliable) { | 389 | if (spellcheckerLocale) { |
393 | const spellcheckerLocale = getSpellcheckerLocaleByFuzzyIdentifier(findResult.language); | 390 | switchDict(spellcheckerLocale); |
394 | debug('Language detected reliably, setting spellchecker language to', spellcheckerLocale); | ||
395 | if (spellcheckerLocale) { | ||
396 | switchDict([ | ||
397 | ...this.spellcheckerLanguage, | ||
398 | spellcheckerLocale, | ||
399 | ]); | ||
400 | } | ||
401 | } | 391 | } |
402 | }, 225)); | 392 | }, 225)); |
403 | } | 393 | } |
@@ -411,7 +401,8 @@ new RecipeController(); | |||
411 | const originalWindowOpen = window.open; | 401 | const originalWindowOpen = window.open; |
412 | 402 | ||
413 | window.open = (url, frameName, features) => { | 403 | window.open = (url, frameName, features) => { |
414 | if (!url && !frameName && !features) { | 404 | debug('window.open', url, frameName, features); |
405 | if (!url) { | ||
415 | // The service hasn't yet supplied a URL (as used in Skype). | 406 | // The service hasn't yet supplied a URL (as used in Skype). |
416 | // Return a new dummy window object and wait for the service to change the properties | 407 | // Return a new dummy window object and wait for the service to change the properties |
417 | const newWindow = { | 408 | const newWindow = { |
@@ -423,8 +414,12 @@ window.open = (url, frameName, features) => { | |||
423 | const checkInterval = setInterval(() => { | 414 | const checkInterval = setInterval(() => { |
424 | // Has the service changed the URL yet? | 415 | // Has the service changed the URL yet? |
425 | if (newWindow.location.href !== '') { | 416 | if (newWindow.location.href !== '') { |
426 | // Open the new URL | 417 | if (features) { |
427 | ipcRenderer.sendToHost('new-window', newWindow.location.href); | 418 | originalWindowOpen(newWindow.location.href, frameName, features); |
419 | } else { | ||
420 | // Open the new URL | ||
421 | ipcRenderer.sendToHost('new-window', newWindow.location.href); | ||
422 | } | ||
428 | clearInterval(checkInterval); | 423 | clearInterval(checkInterval); |
429 | } | 424 | } |
430 | }, 0); | 425 | }, 0); |