forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspellcheck.js
More file actions
64 lines (60 loc) · 2.06 KB
/
spellcheck.js
File metadata and controls
64 lines (60 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { ipcMain, MenuItem } from 'electron'
import log from 'electron-log'
import { isOsx } from '../../config'
import { addToDictionary } from '../../spellchecker'
import { SEPARATOR } from './menuItems'
/**
* Build the spell checker menu depending on input.
*
* @param {boolean} isMisspelled Whether a the selected word is misspelled.
* @param {[string]} misspelledWord The selected word.
* @param {[string[]]} wordSuggestions Suggestions for `selectedWord`.
* @returns {MenuItem[]}
*/
export default (isMisspelled, misspelledWord, wordSuggestions) => {
const spellingSubmenu = []
spellingSubmenu.push(new MenuItem({
label: 'Change Language...',
// NB: On macOS the OS spell checker is used and will detect the language automatically.
visible: !isOsx,
click (menuItem, targetWindow) {
targetWindow.webContents.send('mt::spelling-show-switch-language')
}
}))
// Handle misspelled word if wordSuggestions is set, otherwise word is correct.
if (isMisspelled && misspelledWord && wordSuggestions) {
spellingSubmenu.push({
label: 'Add to Dictionary',
click (menuItem, targetWindow) {
if (!addToDictionary(targetWindow, misspelledWord)) {
log.error(`Error while adding "${misspelledWord}" to dictionary.`)
return
}
// Need to notify Chromium to invalidate the spelling underline.
targetWindow.webContents.replaceMisspelling(misspelledWord)
}
})
if (wordSuggestions.length > 0) {
spellingSubmenu.push(SEPARATOR)
for (const word of wordSuggestions) {
spellingSubmenu.push({
label: word,
click (menuItem, targetWindow) {
targetWindow.webContents.send('mt::spelling-replace-misspelling', {
word: misspelledWord,
replacement: word
})
}
})
}
}
} else {
spellingSubmenu.push({
label: 'Edit Dictionary...',
click (menuItem, targetWindow) {
ipcMain.emit('app-create-settings-window', 'spelling')
}
})
}
return spellingSubmenu
}