Files: bed484f1df0e3d56efaf7d40ae488bd6fd78cd87 / lib / context-menu-and-spellcheck.js
3103 bytesRaw
1 | var {remote, shell, clipboard, ipcRenderer} = require('electron') |
2 | var {SpellCheckHandler, ContextMenuListener, ContextMenuBuilder} = require('electron-spellchecker') |
3 | var {MenuItem, Menu} = remote |
4 | var ref = require('ssb-ref') |
5 | |
6 | window.spellCheckHandler = new SpellCheckHandler() |
7 | window.spellCheckHandler.attachToInput() |
8 | |
9 | // Start off as US English, America #1 (lol) |
10 | window.spellCheckHandler.switchLanguage('en-US') |
11 | |
12 | var contextMenuBuilder = new ContextMenuBuilder(window.spellCheckHandler, null, true) |
13 | |
14 | contextMenuBuilder.buildMenuForLink = function (menuInfo) { |
15 | var menu = new Menu() |
16 | var isEmailAddress = menuInfo.linkURL.startsWith('mailto:') |
17 | var isFile = menuInfo.linkURL.startsWith('file:') |
18 | var extractedRef = ref.extract(menuInfo.linkURL) |
19 | |
20 | if (!isFile) { |
21 | var copyLink = new MenuItem({ |
22 | label: isEmailAddress ? this.stringTable.copyMail() : this.stringTable.copyLinkUrl(), |
23 | click: () => { |
24 | // Omit the mailto: portion of the link; we just want the address |
25 | clipboard.writeText(isEmailAddress ? menuInfo.linkText : menuInfo.linkURL) |
26 | } |
27 | }) |
28 | |
29 | var openLink = new MenuItem({ |
30 | label: this.stringTable.openLinkUrl(), |
31 | click: () => { |
32 | shell.openExternal(menuInfo.linkURL) |
33 | } |
34 | }) |
35 | |
36 | menu.append(copyLink) |
37 | menu.append(openLink) |
38 | } |
39 | |
40 | if (extractedRef) { |
41 | var copyRef = new MenuItem({ |
42 | label: `Copy Link Ref (${extractedRef.slice(0, 10)}...)`, |
43 | click: () => { |
44 | // Omit the mailto: portion of the link; we just want the address |
45 | clipboard.writeText(extractedRef) |
46 | } |
47 | }) |
48 | menu.append(copyRef) |
49 | } |
50 | |
51 | if (this.isSrcUrlValid(menuInfo)) { |
52 | if (!isFile) this.addSeparator(menu) |
53 | this.addImageItems(menu, menuInfo) |
54 | } |
55 | |
56 | this.addInspectElement(menu, menuInfo) |
57 | this.processMenu(menu, menuInfo) |
58 | |
59 | return menu |
60 | } |
61 | |
62 | module.exports = new ContextMenuListener((info) => { |
63 | contextMenuBuilder.buildMenuForElement(info).then((menu) => { |
64 | var element = document.elementFromPoint(info.x, info.y) |
65 | while (element && !element.msg) { |
66 | element = element.parentNode |
67 | } |
68 | |
69 | menu.append(new MenuItem({ |
70 | label: 'Inspect Server Process', |
71 | click: function () { |
72 | ipcRenderer.send('open-background-devtools') |
73 | } |
74 | })) |
75 | |
76 | menu.append(new MenuItem({ |
77 | type: 'separator' |
78 | })) |
79 | |
80 | menu.append(new MenuItem({ |
81 | label: 'Reload', |
82 | click: function (item, focusedWindow) { |
83 | if (focusedWindow) { |
84 | focusedWindow.reload() |
85 | } |
86 | } |
87 | })) |
88 | |
89 | if (element && element.msg) { |
90 | menu.append(new MenuItem({ |
91 | type: 'separator' |
92 | })) |
93 | menu.append(new MenuItem({ |
94 | label: 'Copy Message ID', |
95 | click: function () { |
96 | clipboard.writeText(element.msg.key) |
97 | } |
98 | })) |
99 | if (element.msg.value.content.text) { |
100 | menu.append(new MenuItem({ |
101 | label: 'Copy Message Text', |
102 | click: function () { |
103 | clipboard.writeText(element.msg.value.content.text) |
104 | } |
105 | })) |
106 | } |
107 | } |
108 | menu.popup(remote.getCurrentWindow()) |
109 | }).catch((err) => { |
110 | throw err |
111 | }) |
112 | }) |
113 |
Built with git-ssb-web