forked from marktext/marktext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
50 lines (45 loc) · 1.68 KB
/
index.js
File metadata and controls
50 lines (45 loc) · 1.68 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
import { getCurrentWindow, Menu as RemoteMenu, MenuItem as RemoteMenuItem } from '@electron/remote'
import {
CUT,
COPY,
PASTE,
COPY_AS_MARKDOWN,
COPY_AS_HTML,
PASTE_AS_PLAIN_TEXT,
SEPARATOR,
INSERT_BEFORE,
INSERT_AFTER
} from './menuItems'
import spellcheckMenuBuilder from './spellcheck'
const CONTEXT_ITEMS = [INSERT_BEFORE, INSERT_AFTER, SEPARATOR, CUT, COPY, PASTE, SEPARATOR, COPY_AS_MARKDOWN, COPY_AS_HTML, PASTE_AS_PLAIN_TEXT]
/**
* Show editor context menu.
*
* @param {MouseEvent} event The native mouse event.
* @param {*} selection The editor line with start and end offset.
* @param {[SpellChecker]} spellchecker The spellcheck wrapper.
* @param {[string]} selectedWord The selected word.
* @param {[string[]]} wordSuggestions Suggestions for `word`.
* @param {*} replaceCallback The callback to replace the word by a replacement.
*/
export const showContextMenu = (event, selection, spellchecker, selectedWord, wordSuggestions, replaceCallback) => {
const { start, end } = selection
const menu = new RemoteMenu()
const win = getCurrentWindow()
const disableCutAndCopy = start.key === end.key && start.offset === end.offset
const spellingSubmenu = spellcheckMenuBuilder(spellchecker, selectedWord, wordSuggestions, replaceCallback)
if (spellingSubmenu) {
menu.append(new RemoteMenuItem({
label: 'Spelling...',
submenu: spellingSubmenu
}))
menu.append(new RemoteMenuItem(SEPARATOR))
}
[CUT, COPY, COPY_AS_HTML, COPY_AS_MARKDOWN].forEach(item => {
item.enabled = !disableCutAndCopy
})
CONTEXT_ITEMS.forEach(item => {
menu.append(new RemoteMenuItem(item))
})
menu.popup([{ window: win, x: event.clientX, y: event.clientY }])
}