Files: ed0d0518717fe16a686399588766fb35b3e6e37a / app / sync / initialise / electron-context-menu-and-spellcheck.js
5760 bytesRaw
1 | const nest = require('depnest') |
2 | const { remote, shell, clipboard, ipcRenderer } = require('electron') |
3 | const { SpellCheckHandler, ContextMenuListener, ContextMenuBuilder } = require('electron-spellchecker') |
4 | const { MenuItem, Menu } = remote |
5 | const ref = require('ssb-ref') |
6 | |
7 | var navigateHandler = null |
8 | |
9 | exports.gives = nest('app.sync.initialise') |
10 | |
11 | exports.needs = nest({ |
12 | 'app.sync.goTo': 'first', |
13 | 'sbot.async.get': 'first', |
14 | 'settings.sync.get': 'first' |
15 | }) |
16 | |
17 | exports.create = function (api) { |
18 | return nest('app.sync.initialise', function (_, _config) { |
19 | const { gateway } = _config |
20 | |
21 | const config = { |
22 | gateway: gateway || 'https://viewer.scuttlebot.io', |
23 | localeCode: api.settings.sync.get('patchbay.localeCode') || 'en-GB' |
24 | } |
25 | |
26 | setupContextMenuAndSpellCheck(config, { |
27 | navigate: api.app.sync.goTo, |
28 | get: api.sbot.async.get |
29 | }) |
30 | }) |
31 | } |
32 | |
33 | function setupContextMenuAndSpellCheck (config, { navigate, get }) { |
34 | navigateHandler = navigate |
35 | window.spellCheckHandler = new SpellCheckHandler() |
36 | window.spellCheckHandler.attachToInput() |
37 | |
38 | window.spellCheckHandler.automaticallyIdentifyLanguages = false |
39 | window.spellCheckHandler.switchLanguage(config.localeCode) |
40 | |
41 | var contextMenuBuilder = new ContextMenuBuilder(window.spellCheckHandler, null, true) |
42 | |
43 | contextMenuBuilder.buildMenuForLink = function (menuInfo) { |
44 | var element = document.elementFromPoint(menuInfo.x, menuInfo.y) |
45 | |
46 | var menu = new Menu() |
47 | var isEmailAddress = menuInfo.linkURL.startsWith('mailto:') |
48 | var isFile = menuInfo.linkURL.startsWith('file:') |
49 | |
50 | // use the anchor of a link if it directly references an ID |
51 | var extractedRef = element && ref.isLink(element.anchor) ? element.anchor : ref.extract(menuInfo.linkURL) |
52 | |
53 | if (!isFile) { |
54 | var copyLink = new MenuItem({ |
55 | label: isEmailAddress ? this.stringTable.copyMail() : this.stringTable.copyLinkUrl(), |
56 | click: () => { |
57 | // Omit the mailto: portion of the link; we just want the address |
58 | clipboard.writeText(isEmailAddress ? menuInfo.linkText : menuInfo.linkURL) |
59 | } |
60 | }) |
61 | |
62 | var openLink = new MenuItem({ |
63 | label: this.stringTable.openLinkUrl(), |
64 | click: () => { |
65 | shell.openExternal(menuInfo.linkURL) |
66 | } |
67 | }) |
68 | |
69 | menu.append(copyLink) |
70 | menu.append(openLink) |
71 | } |
72 | |
73 | if (extractedRef) { |
74 | if (navigateHandler) { |
75 | menu.append(new MenuItem({ |
76 | label: 'Find Link References', |
77 | click: function () { |
78 | console.log(extractedRef) |
79 | navigateHandler('?' + extractedRef) |
80 | } |
81 | })) |
82 | this.addSeparator(menu) |
83 | } |
84 | var copyRef = new MenuItem({ |
85 | label: `Copy Link Ref (${extractedRef.slice(0, 10)}...)`, |
86 | click: () => { |
87 | // Omit the mailto: portion of the link; we just want the address |
88 | clipboard.writeText(extractedRef) |
89 | } |
90 | }) |
91 | menu.append(copyRef) |
92 | |
93 | if (ref.isBlob(extractedRef) && menuInfo.hasImageContents) { |
94 | var copyEmbed = new MenuItem({ |
95 | label: `Copy Embed Markdown`, |
96 | click: () => { |
97 | // Omit the mailto: portion of the link; we just want the address |
98 | clipboard.writeText(`![${menuInfo.titleText}](${extractedRef})`) |
99 | } |
100 | }) |
101 | menu.append(copyEmbed) |
102 | } |
103 | } |
104 | |
105 | if (this.isSrcUrlValid(menuInfo)) { |
106 | if (!isFile) this.addSeparator(menu) |
107 | this.addImageItems(menu, menuInfo) |
108 | } |
109 | |
110 | this.addInspectElement(menu, menuInfo) |
111 | this.processMenu(menu, menuInfo) |
112 | |
113 | return menu |
114 | } |
115 | |
116 | setupContextMenuAndSpellCheck.menu = new ContextMenuListener((info) => { |
117 | contextMenuBuilder.buildMenuForElement(info).then((menu) => { |
118 | var element = document.elementFromPoint(info.x, info.y) |
119 | |
120 | menu.append(new MenuItem({ |
121 | label: 'Inspect Server Process', |
122 | click: function () { |
123 | ipcRenderer.send('open-background-devtools') |
124 | } |
125 | })) |
126 | |
127 | menu.append(new MenuItem({ |
128 | type: 'separator' |
129 | })) |
130 | |
131 | menu.append(new MenuItem({ |
132 | label: 'Reload', |
133 | click: function (item, focusedWindow) { |
134 | if (focusedWindow) { |
135 | focusedWindow.reload() |
136 | } |
137 | } |
138 | })) |
139 | |
140 | // message related elements are decorated by message/html/decorate/context-menus.js |
141 | while (element && !element.msg) { |
142 | element = element.parentNode |
143 | } |
144 | |
145 | if (element && element.msg) { |
146 | menu.append(new MenuItem({ |
147 | type: 'separator' |
148 | })) |
149 | menu.append(new MenuItem({ |
150 | label: 'Copy Message ID', |
151 | click: function () { |
152 | clipboard.writeText(element.msg.key) |
153 | } |
154 | })) |
155 | menu.append(new MenuItem({ |
156 | label: 'Copy Message Text', |
157 | click: function () { |
158 | get(element.msg.key, (err, value) => { |
159 | if (!err && value.content && value.content.text) { |
160 | clipboard.writeText(value.content.text) |
161 | } else { |
162 | showDialog({ |
163 | type: 'error', |
164 | title: 'Error', |
165 | buttons: ['OK'], |
166 | message: 'Could not get message text.', |
167 | detail: err && err.message |
168 | }) |
169 | } |
170 | }) |
171 | } |
172 | })) |
173 | menu.append(new MenuItem({ |
174 | label: 'Copy External Link', |
175 | click: function () { |
176 | const key = element.msg.key |
177 | const url = `${config.gateway}/${encodeURIComponent(key)}` |
178 | clipboard.writeText(url) |
179 | } |
180 | })) |
181 | } |
182 | |
183 | menu.popup(remote.getCurrentWindow()) |
184 | }).catch((err) => { |
185 | throw err |
186 | }) |
187 | }) |
188 | } |
189 | |
190 | function showDialog (opts) { |
191 | remote.dialog.showMessageBox(remote.getCurrentWindow(), opts) |
192 | } |
193 |
Built with git-ssb-web