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