diff options
Diffstat (limited to 'src/webview/recipe.js')
-rw-r--r-- | src/webview/recipe.js | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/src/webview/recipe.js b/src/webview/recipe.js index c718b348e..38a65276e 100644 --- a/src/webview/recipe.js +++ b/src/webview/recipe.js | |||
@@ -1,10 +1,12 @@ | |||
1 | import { ipcRenderer } from 'electron'; | 1 | import { ipcRenderer } from 'electron'; |
2 | import path from 'path'; | 2 | import path from 'path'; |
3 | import { autorun, computed, observable } from 'mobx'; | 3 | import { autorun, computed, observable } from 'mobx'; |
4 | import { loadModule } from 'cld3-asm'; | ||
5 | import { debounce } from 'lodash'; | ||
4 | 6 | ||
5 | import RecipeWebview from './lib/RecipeWebview'; | 7 | import RecipeWebview from './lib/RecipeWebview'; |
6 | 8 | ||
7 | import spellchecker, { switchDict, disable as disableSpellchecker } from './spellchecker'; | 9 | import spellchecker, { switchDict, disable as disableSpellchecker, getSpellcheckerLocaleByFuzzyIdentifier } from './spellchecker'; |
8 | import { injectDarkModeStyle, isDarkModeStyleInjected, removeDarkModeStyle } from './darkmode'; | 10 | import { injectDarkModeStyle, isDarkModeStyleInjected, removeDarkModeStyle } from './darkmode'; |
9 | import contextMenu from './contextMenu'; | 11 | import contextMenu from './contextMenu'; |
10 | import './notifications'; | 12 | import './notifications'; |
@@ -60,6 +62,39 @@ class RecipeController { | |||
60 | ); | 62 | ); |
61 | 63 | ||
62 | autorun(() => this.update()); | 64 | autorun(() => this.update()); |
65 | |||
66 | console.log(JSON.parse(JSON.stringify(this.settings))); | ||
67 | |||
68 | const cldFactory = await loadModule(); | ||
69 | const identifier = cldFactory.create(0, 1000); | ||
70 | |||
71 | window.addEventListener('keyup', debounce((e) => { | ||
72 | const elem = e.target; | ||
73 | |||
74 | let value = ''; | ||
75 | if (elem.isContentEditable) { | ||
76 | value = elem.textContent; | ||
77 | } else { | ||
78 | // | ||
79 | } | ||
80 | |||
81 | // Force a minimum length to get better detection results | ||
82 | if (value.length < 30) return; | ||
83 | |||
84 | debug('Detecting language for', value); | ||
85 | const findResult = identifier.findLanguage(value); | ||
86 | |||
87 | debug('Language detection result', findResult); | ||
88 | |||
89 | if (findResult.is_reliable) { | ||
90 | debug('Language detected reliably, setting spellchecker language to', findResult.language); | ||
91 | const spellcheckerLocale = getSpellcheckerLocaleByFuzzyIdentifier(findResult.language); | ||
92 | debug('reported back', spellcheckerLocale); | ||
93 | if (spellcheckerLocale) { | ||
94 | switchDict(spellcheckerLocale); | ||
95 | } | ||
96 | } | ||
97 | }, 200)); | ||
63 | } | 98 | } |
64 | 99 | ||
65 | loadRecipeModule(event, config, recipe) { | 100 | loadRecipeModule(event, config, recipe) { |
@@ -87,7 +122,12 @@ class RecipeController { | |||
87 | 122 | ||
88 | if (this.settings.app.enableSpellchecking) { | 123 | if (this.settings.app.enableSpellchecking) { |
89 | debug('Setting spellchecker language to', this.spellcheckerLanguage); | 124 | debug('Setting spellchecker language to', this.spellcheckerLanguage); |
90 | switchDict(this.spellcheckerLanguage); | 125 | let { spellcheckerLanguage } = this; |
126 | if (spellcheckerLanguage === 'automatic') { | ||
127 | debug('Found `automatic` locale, falling back to user locale until detected', this.settings.app.locale); | ||
128 | spellcheckerLanguage = this.settings.app.locale; | ||
129 | } | ||
130 | switchDict(spellcheckerLanguage); | ||
91 | } else { | 131 | } else { |
92 | debug('Disable spellchecker'); | 132 | debug('Disable spellchecker'); |
93 | disableSpellchecker(); | 133 | disableSpellchecker(); |