lib/context-menu-and-spellcheck.jsView |
---|
2 | 2 | var {SpellCheckHandler, ContextMenuListener, ContextMenuBuilder} = require('electron-spellchecker') |
3 | 3 | var {MenuItem, Menu} = remote |
4 | 4 | var ref = require('ssb-ref') |
5 | 5 | |
6 | | -window.spellCheckHandler = new SpellCheckHandler() |
7 | | -window.spellCheckHandler.attachToInput() |
| 6 | +module.exports = setupContextMenuAndSpellCheck |
8 | 7 | |
9 | | - |
10 | | -window.spellCheckHandler.switchLanguage('en-US') |
| 8 | +function setupContextMenuAndSpellCheck (config) { |
| 9 | + window.spellCheckHandler = new SpellCheckHandler() |
| 10 | + window.spellCheckHandler.attachToInput() |
11 | 11 | |
12 | | -var contextMenuBuilder = new ContextMenuBuilder(window.spellCheckHandler, null, true) |
| 12 | + |
| 13 | + window.spellCheckHandler.switchLanguage('en-US') |
13 | 14 | |
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) |
| 15 | + var contextMenuBuilder = new ContextMenuBuilder(window.spellCheckHandler, null, true) |
19 | 16 | |
20 | | - if (!isFile) { |
21 | | - var copyLink = new MenuItem({ |
22 | | - label: isEmailAddress ? this.stringTable.copyMail() : this.stringTable.copyLinkUrl(), |
23 | | - click: () => { |
24 | | - |
25 | | - clipboard.writeText(isEmailAddress ? menuInfo.linkText : menuInfo.linkURL) |
26 | | - } |
27 | | - }) |
| 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) |
28 | 22 | |
29 | | - var openLink = new MenuItem({ |
30 | | - label: this.stringTable.openLinkUrl(), |
31 | | - click: () => { |
32 | | - shell.openExternal(menuInfo.linkURL) |
33 | | - } |
34 | | - }) |
| 23 | + if (!isFile) { |
| 24 | + var copyLink = new MenuItem({ |
| 25 | + label: isEmailAddress ? this.stringTable.copyMail() : this.stringTable.copyLinkUrl(), |
| 26 | + click: () => { |
| 27 | + |
| 28 | + clipboard.writeText(isEmailAddress ? menuInfo.linkText : menuInfo.linkURL) |
| 29 | + } |
| 30 | + }) |
35 | 31 | |
36 | | - menu.append(copyLink) |
37 | | - menu.append(openLink) |
38 | | - } |
| 32 | + var openLink = new MenuItem({ |
| 33 | + label: this.stringTable.openLinkUrl(), |
| 34 | + click: () => { |
| 35 | + shell.openExternal(menuInfo.linkURL) |
| 36 | + } |
| 37 | + }) |
39 | 38 | |
40 | | - if (extractedRef) { |
41 | | - var copyRef = new MenuItem({ |
42 | | - label: `Copy Link Ref (${extractedRef.slice(0, 10)}...)`, |
43 | | - click: () => { |
44 | | - |
45 | | - clipboard.writeText(extractedRef) |
46 | | - } |
47 | | - }) |
48 | | - menu.append(copyRef) |
49 | | - } |
| 39 | + menu.append(copyLink) |
| 40 | + menu.append(openLink) |
| 41 | + } |
50 | 42 | |
51 | | - if (this.isSrcUrlValid(menuInfo)) { |
52 | | - if (!isFile) this.addSeparator(menu) |
53 | | - this.addImageItems(menu, menuInfo) |
54 | | - } |
| 43 | + if (extractedRef) { |
| 44 | + var copyRef = new MenuItem({ |
| 45 | + label: `Copy Link Ref (${extractedRef.slice(0, 10)}...)`, |
| 46 | + click: () => { |
| 47 | + |
| 48 | + clipboard.writeText(extractedRef) |
| 49 | + } |
| 50 | + }) |
| 51 | + menu.append(copyRef) |
| 52 | + } |
55 | 53 | |
56 | | - this.addInspectElement(menu, menuInfo) |
57 | | - this.processMenu(menu, menuInfo) |
| 54 | + if (this.isSrcUrlValid(menuInfo)) { |
| 55 | + if (!isFile) this.addSeparator(menu) |
| 56 | + this.addImageItems(menu, menuInfo) |
| 57 | + } |
58 | 58 | |
59 | | - return menu |
60 | | -} |
| 59 | + this.addInspectElement(menu, menuInfo) |
| 60 | + this.processMenu(menu, menuInfo) |
61 | 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 | | - } |
| 62 | + return menu |
| 63 | + } |
68 | 64 | |
69 | | - menu.append(new MenuItem({ |
70 | | - label: 'Inspect Server Process', |
71 | | - click: function () { |
72 | | - ipcRenderer.send('open-background-devtools') |
| 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 |
73 | 70 | } |
74 | | - })) |
75 | 71 | |
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() |
| 72 | + menu.append(new MenuItem({ |
| 73 | + label: 'Inspect Server Process', |
| 74 | + click: function () { |
| 75 | + ipcRenderer.send('open-background-devtools') |
85 | 76 | } |
86 | | - } |
87 | | - })) |
| 77 | + })) |
88 | 78 | |
89 | | - if (element && element.msg) { |
90 | 79 | menu.append(new MenuItem({ |
91 | 80 | type: 'separator' |
92 | 81 | })) |
| 82 | + |
93 | 83 | menu.append(new MenuItem({ |
94 | | - label: 'Copy Message ID', |
95 | | - click: function () { |
96 | | - clipboard.writeText(element.msg.key) |
| 84 | + label: 'Reload', |
| 85 | + click: function (item, focusedWindow) { |
| 86 | + if (focusedWindow) { |
| 87 | + focusedWindow.reload() |
| 88 | + } |
97 | 89 | } |
98 | 90 | })) |
99 | | - if (element.msg.value.content.text) { |
| 91 | + |
| 92 | + if (element && element.msg) { |
100 | 93 | menu.append(new MenuItem({ |
101 | | - label: 'Copy Message Text', |
| 94 | + type: 'separator' |
| 95 | + })) |
| 96 | + menu.append(new MenuItem({ |
| 97 | + label: 'Copy Message ID', |
102 | 98 | click: function () { |
103 | | - clipboard.writeText(element.msg.value.content.text) |
| 99 | + clipboard.writeText(element.msg.key) |
104 | 100 | } |
105 | 101 | })) |
| 102 | + if (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 | + })) |
106 | 120 | } |
107 | | - } |
108 | | - menu.popup(remote.getCurrentWindow()) |
109 | | - }).catch((err) => { |
110 | | - throw err |
| 121 | + menu.popup(remote.getCurrentWindow()) |
| 122 | + }).catch((err) => { |
| 123 | + throw err |
| 124 | + }) |
111 | 125 | }) |
112 | | -}) |
| 126 | +} |