diff options
author | 2018-12-07 22:39:12 +0100 | |
---|---|---|
committer | 2018-12-07 22:39:12 +0100 | |
commit | 65aaac06beac7f070a3a81adeffb8e1887d9f12b (patch) | |
tree | e2b4f452eef8c17198845e7a59c49b4fe28b1823 /src/webview/contextMenu.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/contextMenu.js')
-rw-r--r-- | src/webview/contextMenu.js | 57 |
1 files changed, 54 insertions, 3 deletions
diff --git a/src/webview/contextMenu.js b/src/webview/contextMenu.js index ad156128c..f9afa1913 100644 --- a/src/webview/contextMenu.js +++ b/src/webview/contextMenu.js | |||
@@ -4,6 +4,7 @@ | |||
4 | import { clipboard, remote, ipcRenderer, shell } from 'electron'; | 4 | import { clipboard, remote, ipcRenderer, shell } from 'electron'; |
5 | 5 | ||
6 | import { isDevMode, isMac } from '../environment'; | 6 | import { isDevMode, isMac } from '../environment'; |
7 | import { SPELLCHECKER_LOCALES } from '../i18n/languages'; | ||
7 | 8 | ||
8 | const debug = require('debug')('Franz:contextMenu'); | 9 | const debug = require('debug')('Franz:contextMenu'); |
9 | 10 | ||
@@ -21,7 +22,7 @@ function delUnusedElements(menuTpl) { | |||
21 | }); | 22 | }); |
22 | } | 23 | } |
23 | 24 | ||
24 | const buildMenuTpl = (props, suggestions) => { | 25 | const buildMenuTpl = (props, suggestions, defaultSpellcheckerLanguage, spellcheckerLanguage) => { |
25 | const { editFlags } = props; | 26 | const { editFlags } = props; |
26 | const textSelection = props.selectionText.trim(); | 27 | const textSelection = props.selectionText.trim(); |
27 | const hasText = textSelection.length > 0; | 28 | const hasText = textSelection.length > 0; |
@@ -190,6 +191,49 @@ const buildMenuTpl = (props, suggestions) => { | |||
190 | }); | 191 | }); |
191 | } | 192 | } |
192 | 193 | ||
194 | const spellcheckingLanguages = []; | ||
195 | Object.keys(SPELLCHECKER_LOCALES).sort(Intl.Collator().compare).forEach((key) => { | ||
196 | spellcheckingLanguages.push({ | ||
197 | id: `lang-${key}`, | ||
198 | label: SPELLCHECKER_LOCALES[key], | ||
199 | type: 'radio', | ||
200 | checked: spellcheckerLanguage === key, | ||
201 | click() { | ||
202 | debug('Setting service spellchecker to', key); | ||
203 | ipcRenderer.sendToHost('set-service-spellchecker-language', key); | ||
204 | }, | ||
205 | }); | ||
206 | }); | ||
207 | |||
208 | menuTpl.push({ | ||
209 | type: 'separator', | ||
210 | }, { | ||
211 | id: 'spellchecker', | ||
212 | label: 'Spellchecker', | ||
213 | submenu: [ | ||
214 | { | ||
215 | id: 'spellchecker', | ||
216 | label: 'Available Languages', | ||
217 | enabled: false, | ||
218 | }, { | ||
219 | type: 'separator', | ||
220 | }, | ||
221 | { | ||
222 | id: 'resetToDefault', | ||
223 | label: `Reset to system default (${SPELLCHECKER_LOCALES[defaultSpellcheckerLanguage]})`, | ||
224 | type: 'radio', | ||
225 | click() { | ||
226 | debug('Resetting service spellchecker to system default'); | ||
227 | ipcRenderer.sendToHost('set-service-spellchecker-language', 'reset'); | ||
228 | }, | ||
229 | }, | ||
230 | { | ||
231 | type: 'separator', | ||
232 | }, | ||
233 | ...spellcheckingLanguages], | ||
234 | }); | ||
235 | |||
236 | |||
193 | if (isDevMode) { | 237 | if (isDevMode) { |
194 | menuTpl.push({ | 238 | menuTpl.push({ |
195 | type: 'separator', | 239 | type: 'separator', |
@@ -205,7 +249,7 @@ const buildMenuTpl = (props, suggestions) => { | |||
205 | return delUnusedElements(menuTpl); | 249 | return delUnusedElements(menuTpl); |
206 | }; | 250 | }; |
207 | 251 | ||
208 | export default function contextMenu(spellcheckProvider) { | 252 | export default function contextMenu(spellcheckProvider, getDefaultSpellcheckerLanguage, getSpellcheckerLanguage) { |
209 | webContents.on('context-menu', (e, props) => { | 253 | webContents.on('context-menu', (e, props) => { |
210 | e.preventDefault(); | 254 | e.preventDefault(); |
211 | 255 | ||
@@ -216,7 +260,14 @@ export default function contextMenu(spellcheckProvider) { | |||
216 | debug('Suggestions', suggestions); | 260 | debug('Suggestions', suggestions); |
217 | } | 261 | } |
218 | 262 | ||
219 | const menu = Menu.buildFromTemplate(buildMenuTpl(props, suggestions.slice(0, 5))); | 263 | const menu = Menu.buildFromTemplate( |
264 | buildMenuTpl( | ||
265 | props, | ||
266 | suggestions.slice(0, 5), | ||
267 | getDefaultSpellcheckerLanguage(), | ||
268 | getSpellcheckerLanguage(), | ||
269 | ), | ||
270 | ); | ||
220 | 271 | ||
221 | menu.popup(remote.getCurrentWindow()); | 272 | menu.popup(remote.getCurrentWindow()); |
222 | }); | 273 | }); |