diff options
author | Bennett <hello@vantezzen.io> | 2020-06-21 09:19:59 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-21 12:49:59 +0530 |
commit | 0d6d623d1e34cdbff2d46229165b49289a9a0619 (patch) | |
tree | 83f6a22a08e354da58adf3ed0393f4d39bca6124 /src/webview/contextMenuBuilder.js | |
parent | Prepare and Release/5.6.0 beta.1 (#820) (diff) | |
download | ferdium-app-0d6d623d1e34cdbff2d46229165b49289a9a0619.tar.gz ferdium-app-0d6d623d1e34cdbff2d46229165b49289a9a0619.tar.zst ferdium-app-0d6d623d1e34cdbff2d46229165b49289a9a0619.zip |
Add FAB to service dashboard (#824)
* Implement #387
* Fix lint
* Upgrade to Electron 9
* Remove dependency on electron-spellchecker
* Allow multiple languages to be selected
* Fix lint
* Don't show spellchecker language chooser for macOS
* Fix _requireAuthenticatedUser throwing error on startup
* Add FAB
Diffstat (limited to 'src/webview/contextMenuBuilder.js')
-rw-r--r-- | src/webview/contextMenuBuilder.js | 419 |
1 files changed, 419 insertions, 0 deletions
diff --git a/src/webview/contextMenuBuilder.js b/src/webview/contextMenuBuilder.js new file mode 100644 index 000000000..ee27877b9 --- /dev/null +++ b/src/webview/contextMenuBuilder.js | |||
@@ -0,0 +1,419 @@ | |||
1 | /** | ||
2 | * Context Menu builder. | ||
3 | * | ||
4 | * Based on "electron-spellchecker"'s ContextMenuBuilder but customized for Ferdi | ||
5 | * and for usage with Electron's build-in spellchecker | ||
6 | * | ||
7 | * Source: https://github.com/electron-userland/electron-spellchecker/blob/master/src/context-menu-builder.js | ||
8 | */ | ||
9 | const { | ||
10 | clipboard, nativeImage, remote, shell, | ||
11 | } = require('electron'); | ||
12 | |||
13 | const { Menu, MenuItem } = remote; | ||
14 | |||
15 | function matchesWord(string) { | ||
16 | const regex = /[\u0041-\u005A\u0061-\u007A\u00AA\u00B5\u00BA\u00C0-\u00D6\u00D8-\u00F6\u00F8-\u02C1\u02C6-\u02D1\u02E0-\u02E4\u02EC\u02EE\u0370-\u0374\u0376\u0377\u037A-\u037D\u0386\u0388-\u038A\u038C\u038E-\u03A1\u03A3-\u03F5\u03F7-\u0481\u048A-\u0527\u0531-\u0556\u0559\u0561-\u0587\u05D0-\u05EA\u05F0-\u05F2\u0620-\u064A\u066E\u066F\u0671-\u06D3\u06D5\u06E5\u06E6\u06EE\u06EF\u06FA-\u06FC\u06FF\u0710\u0712-\u072F\u074D-\u07A5\u07B1\u07CA-\u07EA\u07F4\u07F5\u07FA\u0800-\u0815\u081A\u0824\u0828\u0840-\u0858\u08A0\u08A2-\u08AC\u0904-\u0939\u093D\u0950\u0958-\u0961\u0971-\u0977\u0979-\u097F\u0985-\u098C\u098F\u0990\u0993-\u09A8\u09AA-\u09B0\u09B2\u09B6-\u09B9\u09BD\u09CE\u09DC\u09DD\u09DF-\u09E1\u09F0\u09F1\u0A05-\u0A0A\u0A0F\u0A10\u0A13-\u0A28\u0A2A-\u0A30\u0A32\u0A33\u0A35\u0A36\u0A38\u0A39\u0A59-\u0A5C\u0A5E\u0A72-\u0A74\u0A85-\u0A8D\u0A8F-\u0A91\u0A93-\u0AA8\u0AAA-\u0AB0\u0AB2\u0AB3\u0AB5-\u0AB9\u0ABD\u0AD0\u0AE0\u0AE1\u0B05-\u0B0C\u0B0F\u0B10\u0B13-\u0B28\u0B2A-\u0B30\u0B32\u0B33\u0B35-\u0B39\u0B3D\u0B5C\u0B5D\u0B5F-\u0B61\u0B71\u0B83\u0B85-\u0B8A\u0B8E-\u0B90\u0B92-\u0B95\u0B99\u0B9A\u0B9C\u0B9E\u0B9F\u0BA3\u0BA4\u0BA8-\u0BAA\u0BAE-\u0BB9\u0BD0\u0C05-\u0C0C\u0C0E-\u0C10\u0C12-\u0C28\u0C2A-\u0C33\u0C35-\u0C39\u0C3D\u0C58\u0C59\u0C60\u0C61\u0C85-\u0C8C\u0C8E-\u0C90\u0C92-\u0CA8\u0CAA-\u0CB3\u0CB5-\u0CB9\u0CBD\u0CDE\u0CE0\u0CE1\u0CF1\u0CF2\u0D05-\u0D0C\u0D0E-\u0D10\u0D12-\u0D3A\u0D3D\u0D4E\u0D60\u0D61\u0D7A-\u0D7F\u0D85-\u0D96\u0D9A-\u0DB1\u0DB3-\u0DBB\u0DBD\u0DC0-\u0DC6\u0E01-\u0E30\u0E32\u0E33\u0E40-\u0E46\u0E81\u0E82\u0E84\u0E87\u0E88\u0E8A\u0E8D\u0E94-\u0E97\u0E99-\u0E9F\u0EA1-\u0EA3\u0EA5\u0EA7\u0EAA\u0EAB\u0EAD-\u0EB0\u0EB2\u0EB3\u0EBD\u0EC0-\u0EC4\u0EC6\u0EDC-\u0EDF\u0F00\u0F40-\u0F47\u0F49-\u0F6C\u0F88-\u0F8C\u1000-\u102A\u103F\u1050-\u1055\u105A-\u105D\u1061\u1065\u1066\u106E-\u1070\u1075-\u1081\u108E\u10A0-\u10C5\u10C7\u10CD\u10D0-\u10FA\u10FC-\u1248\u124A-\u124D\u1250-\u1256\u1258\u125A-\u125D\u1260-\u1288\u128A-\u128D\u1290-\u12B0\u12B2-\u12B5\u12B8-\u12BE\u12C0\u12C2-\u12C5\u12C8-\u12D6\u12D8-\u1310\u1312-\u1315\u1318-\u135A\u1380-\u138F\u13A0-\u13F4\u1401-\u166C\u166F-\u167F\u1681-\u169A\u16A0-\u16EA\u1700-\u170C\u170E-\u1711\u1720-\u1731\u1740-\u1751\u1760-\u176C\u176E-\u1770\u1780-\u17B3\u17D7\u17DC\u1820-\u1877\u1880-\u18A8\u18AA\u18B0-\u18F5\u1900-\u191C\u1950-\u196D\u1970-\u1974\u1980-\u19AB\u19C1-\u19C7\u1A00-\u1A16\u1A20-\u1A54\u1AA7\u1B05-\u1B33\u1B45-\u1B4B\u1B83-\u1BA0\u1BAE\u1BAF\u1BBA-\u1BE5\u1C00-\u1C23\u1C4D-\u1C4F\u1C5A-\u1C7D\u1CE9-\u1CEC\u1CEE-\u1CF1\u1CF5\u1CF6\u1D00-\u1DBF\u1E00-\u1F15\u1F18-\u1F1D\u1F20-\u1F45\u1F48-\u1F4D\u1F50-\u1F57\u1F59\u1F5B\u1F5D\u1F5F-\u1F7D\u1F80-\u1FB4\u1FB6-\u1FBC\u1FBE\u1FC2-\u1FC4\u1FC6-\u1FCC\u1FD0-\u1FD3\u1FD6-\u1FDB\u1FE0-\u1FEC\u1FF2-\u1FF4\u1FF6-\u1FFC\u2071\u207F\u2090-\u209C\u2102\u2107\u210A-\u2113\u2115\u2119-\u211D\u2124\u2126\u2128\u212A-\u212D\u212F-\u2139\u213C-\u213F\u2145-\u2149\u214E\u2183\u2184\u2C00-\u2C2E\u2C30-\u2C5E\u2C60-\u2CE4\u2CEB-\u2CEE\u2CF2\u2CF3\u2D00-\u2D25\u2D27\u2D2D\u2D30-\u2D67\u2D6F\u2D80-\u2D96\u2DA0-\u2DA6\u2DA8-\u2DAE\u2DB0-\u2DB6\u2DB8-\u2DBE\u2DC0-\u2DC6\u2DC8-\u2DCE\u2DD0-\u2DD6\u2DD8-\u2DDE\u2E2F\u3005\u3006\u3031-\u3035\u303B\u303C\u3041-\u3096\u309D-\u309F\u30A1-\u30FA\u30FC-\u30FF\u3105-\u312D\u3131-\u318E\u31A0-\u31BA\u31F0-\u31FF\u3400-\u4DB5\u4E00-\u9FCC\uA000-\uA48C\uA4D0-\uA4FD\uA500-\uA60C\uA610-\uA61F\uA62A\uA62B\uA640-\uA66E\uA67F-\uA697\uA6A0-\uA6E5\uA717-\uA71F\uA722-\uA788\uA78B-\uA78E\uA790-\uA793\uA7A0-\uA7AA\uA7F8-\uA801\uA803-\uA805\uA807-\uA80A\uA80C-\uA822\uA840-\uA873\uA882-\uA8B3\uA8F2-\uA8F7\uA8FB\uA90A-\uA925\uA930-\uA946\uA960-\uA97C\uA984-\uA9B2\uA9CF\uAA00-\uAA28\uAA40-\uAA42\uAA44-\uAA4B\uAA60-\uAA76\uAA7A\uAA80-\uAAAF\uAAB1\uAAB5\uAAB6\uAAB9-\uAABD\uAAC0\uAAC2\uAADB-\uAADD\uAAE0-\uAAEA\uAAF2-\uAAF4\uAB01-\uAB06\uAB09-\uAB0E\uAB11-\uAB16\uAB20-\uAB26\uAB28-\uAB2E\uABC0-\uABE2\uAC00-\uD7A3\uD7B0-\uD7C6\uD7CB-\uD7FB\uF900-\uFA6D\uFA70-\uFAD9\uFB00-\uFB06\uFB13-\uFB17\uFB1D\uFB1F-\uFB28\uFB2A-\uFB36\uFB38-\uFB3C\uFB3E\uFB40\uFB41\uFB43\uFB44\uFB46-\uFBB1\uFBD3-\uFD3D\uFD50-\uFD8F\uFD92-\uFDC7\uFDF0-\uFDFB\uFE70-\uFE74\uFE76-\uFEFC\uFF21-\uFF3A\uFF41-\uFF5A\uFF66-\uFFBE\uFFC2-\uFFC7\uFFCA-\uFFCF\uFFD2-\uFFD7\uFFDA-\uFFDC]+/g; | ||
17 | |||
18 | return string.match(regex); | ||
19 | } | ||
20 | |||
21 | const contextMenuStringTable = { | ||
22 | copyMail: () => 'Copy Email Address', | ||
23 | copyLinkUrl: () => 'Copy Link', | ||
24 | openLinkUrl: () => 'Open Link', | ||
25 | copyImageUrl: () => 'Copy Image URL', | ||
26 | copyImage: () => 'Copy Image', | ||
27 | addToDictionary: () => 'Add to Dictionary', | ||
28 | lookUpDefinition: ({ word }) => `Look Up "${word}"`, | ||
29 | searchGoogle: () => 'Search with Google', | ||
30 | cut: () => 'Cut', | ||
31 | copy: () => 'Copy', | ||
32 | paste: () => 'Paste', | ||
33 | inspectElement: () => 'Inspect Element', | ||
34 | }; | ||
35 | |||
36 | /** | ||
37 | * ContextMenuBuilder creates context menus based on the content clicked - this | ||
38 | * information is derived from | ||
39 | * https://github.com/electron/electron/blob/master/docs/api/web-contents.md#event-context-menu, | ||
40 | * which we use to generate the menu. We also use the spell-check information to | ||
41 | * generate suggestions. | ||
42 | */ | ||
43 | module.exports = class ContextMenuBuilder { | ||
44 | /** | ||
45 | * Creates an instance of ContextMenuBuilder | ||
46 | * | ||
47 | * @param {webContents} webContents Current webContents | ||
48 | * @param {Boolean} debugMode If true, display the "Inspect Element" menu item. | ||
49 | * @param {function} processMenu If passed, this method will be passed the menu to change | ||
50 | * it prior to display. Signature: (menu, info) => menu | ||
51 | */ | ||
52 | constructor(webContents, debugMode = false, processMenu = m => m) { | ||
53 | this.debugMode = debugMode; | ||
54 | this.processMenu = processMenu; | ||
55 | this.menu = null; | ||
56 | this.stringTable = Object.assign({}, contextMenuStringTable); | ||
57 | this.getWebContents = () => webContents; | ||
58 | } | ||
59 | |||
60 | /** | ||
61 | * Specify alternate string formatter for each context menu. | ||
62 | * String table consist of string formatter as function instead per each context menu item, | ||
63 | * allows to change string in runtime. All formatters are simply typeof () => string, except | ||
64 | * lookUpDefinition provides word, ({word}) => string. | ||
65 | * | ||
66 | * @param {Object} stringTable The object contains string foramtter function for context menu. | ||
67 | * It is allowed to specify only certain menu string as necessary, which will makes other string | ||
68 | * fall backs to default. | ||
69 | * | ||
70 | */ | ||
71 | setAlternateStringFormatter(stringTable) { | ||
72 | this.stringTable = Object.assign(this.stringTable, stringTable); | ||
73 | } | ||
74 | |||
75 | /** | ||
76 | * Shows a popup menu given the information returned from the context-menu | ||
77 | * event. This is probably the only method you need to call in this class. | ||
78 | * | ||
79 | * @param {Object} contextInfo The object returned from the 'context-menu' | ||
80 | * Electron event. | ||
81 | * | ||
82 | * @return {Promise} Completion | ||
83 | */ | ||
84 | async showPopupMenu(contextInfo) { | ||
85 | const menu = await this.buildMenuForElement(contextInfo); | ||
86 | if (!menu) return; | ||
87 | menu.popup({}); | ||
88 | } | ||
89 | |||
90 | /** | ||
91 | * Builds a context menu specific to the given info that _would_ be shown | ||
92 | * immediately by {{showPopupMenu}}. Use this to add your own menu items to | ||
93 | * the list but use most of the default behavior. | ||
94 | * | ||
95 | * @return {Promise<Menu>} The newly created `Menu` | ||
96 | */ | ||
97 | async buildMenuForElement(info) { | ||
98 | if (info.linkURL && info.linkURL.length > 0) { | ||
99 | return this.buildMenuForLink(info); | ||
100 | } | ||
101 | |||
102 | if (info.hasImageContents && info.srcURL && info.srcURL.length > 1) { | ||
103 | return this.buildMenuForImage(info); | ||
104 | } | ||
105 | |||
106 | if (info.isEditable || (info.inputFieldType && info.inputFieldType !== 'none')) { | ||
107 | return this.buildMenuForTextInput(info); | ||
108 | } | ||
109 | |||
110 | return this.buildMenuForText(info); | ||
111 | } | ||
112 | |||
113 | /** | ||
114 | * Builds a menu applicable to a text input field. | ||
115 | * | ||
116 | * @return {Menu} The `Menu` | ||
117 | */ | ||
118 | buildMenuForTextInput(menuInfo) { | ||
119 | const menu = new Menu(); | ||
120 | |||
121 | this.addSpellingItems(menu, menuInfo); | ||
122 | this.addSearchItems(menu, menuInfo); | ||
123 | |||
124 | this.addCut(menu, menuInfo); | ||
125 | this.addCopy(menu, menuInfo); | ||
126 | this.addPaste(menu, menuInfo); | ||
127 | this.addPastePlain(menu, menuInfo); | ||
128 | this.addInspectElement(menu, menuInfo); | ||
129 | this.processMenu(menu, menuInfo); | ||
130 | |||
131 | return menu; | ||
132 | } | ||
133 | |||
134 | /** | ||
135 | * Builds a menu applicable to a link element. | ||
136 | * | ||
137 | * @return {Menu} The `Menu` | ||
138 | */ | ||
139 | buildMenuForLink(menuInfo) { | ||
140 | const menu = new Menu(); | ||
141 | const isEmailAddress = menuInfo.linkURL.startsWith('mailto:'); | ||
142 | |||
143 | const copyLink = new MenuItem({ | ||
144 | label: isEmailAddress ? this.stringTable.copyMail() : this.stringTable.copyLinkUrl(), | ||
145 | click: () => { | ||
146 | // Omit the mailto: portion of the link; we just want the address | ||
147 | clipboard.writeText(isEmailAddress | ||
148 | ? menuInfo.linkText : menuInfo.linkURL); | ||
149 | }, | ||
150 | }); | ||
151 | |||
152 | const openLink = new MenuItem({ | ||
153 | label: this.stringTable.openLinkUrl(), | ||
154 | click: () => { | ||
155 | shell.openExternal(menuInfo.linkURL); | ||
156 | }, | ||
157 | }); | ||
158 | |||
159 | const openInFerdiLink = new MenuItem({ | ||
160 | label: 'Open Link in Ferdi', | ||
161 | click: () => { | ||
162 | window.location.href = menuInfo.linkURL; | ||
163 | }, | ||
164 | }); | ||
165 | |||
166 | menu.append(copyLink); | ||
167 | menu.append(openLink); | ||
168 | menu.append(openInFerdiLink); | ||
169 | |||
170 | if (this.isSrcUrlValid(menuInfo)) { | ||
171 | this.addSeparator(menu); | ||
172 | this.addImageItems(menu, menuInfo); | ||
173 | } | ||
174 | |||
175 | this.addInspectElement(menu, menuInfo); | ||
176 | this.processMenu(menu, menuInfo); | ||
177 | |||
178 | return menu; | ||
179 | } | ||
180 | |||
181 | /** | ||
182 | * Builds a menu applicable to a text field. | ||
183 | * | ||
184 | * @return {Menu} The `Menu` | ||
185 | */ | ||
186 | buildMenuForText(menuInfo) { | ||
187 | const menu = new Menu(); | ||
188 | |||
189 | this.addSearchItems(menu, menuInfo); | ||
190 | this.addCopy(menu, menuInfo); | ||
191 | this.addInspectElement(menu, menuInfo); | ||
192 | this.processMenu(menu, menuInfo); | ||
193 | |||
194 | return menu; | ||
195 | } | ||
196 | |||
197 | /** | ||
198 | * Builds a menu applicable to an image. | ||
199 | * | ||
200 | * @return {Menu} The `Menu` | ||
201 | */ | ||
202 | buildMenuForImage(menuInfo) { | ||
203 | const menu = new Menu(); | ||
204 | |||
205 | if (this.isSrcUrlValid(menuInfo)) { | ||
206 | this.addImageItems(menu, menuInfo); | ||
207 | } | ||
208 | this.addInspectElement(menu, menuInfo); | ||
209 | this.processMenu(menu, menuInfo); | ||
210 | |||
211 | return menu; | ||
212 | } | ||
213 | |||
214 | /** | ||
215 | * Checks if the current text selection contains a single misspelled word and | ||
216 | * if so, adds suggested spellings as individual menu items. | ||
217 | */ | ||
218 | addSpellingItems(menu, menuInfo) { | ||
219 | const target = this.getWebContents(); | ||
220 | // Add each spelling suggestion | ||
221 | for (const suggestion of menuInfo.dictionarySuggestions) { | ||
222 | menu.append(new MenuItem({ | ||
223 | label: suggestion, | ||
224 | // eslint-disable-next-line no-loop-func | ||
225 | click: () => target.replaceMisspelling(suggestion), | ||
226 | })); | ||
227 | } | ||
228 | |||
229 | // Allow users to add the misspelled word to the dictionary | ||
230 | if (menuInfo.misspelledWord) { | ||
231 | menu.append( | ||
232 | new MenuItem({ | ||
233 | label: 'Add to dictionary', | ||
234 | click: () => target.session.addWordToSpellCheckerDictionary(menuInfo.misspelledWord), | ||
235 | }), | ||
236 | ); | ||
237 | } | ||
238 | |||
239 | return menu; | ||
240 | } | ||
241 | |||
242 | /** | ||
243 | * Adds search-related menu items. | ||
244 | */ | ||
245 | addSearchItems(menu, menuInfo) { | ||
246 | if (!menuInfo.selectionText || menuInfo.selectionText.length < 1) { | ||
247 | return menu; | ||
248 | } | ||
249 | |||
250 | const match = matchesWord(menuInfo.selectionText); | ||
251 | if (!match || match.length === 0) { | ||
252 | return menu; | ||
253 | } | ||
254 | |||
255 | if (process.platform === 'darwin') { | ||
256 | const target = this.getWebContents(); | ||
257 | |||
258 | const lookUpDefinition = new MenuItem({ | ||
259 | label: this.stringTable.lookUpDefinition({ word: menuInfo.selectionText.trim() }), | ||
260 | click: () => target.showDefinitionForSelection(), | ||
261 | }); | ||
262 | |||
263 | menu.append(lookUpDefinition); | ||
264 | } | ||
265 | |||
266 | const search = new MenuItem({ | ||
267 | label: this.stringTable.searchGoogle(), | ||
268 | click: () => { | ||
269 | const url = `https://www.google.com/#q=${encodeURIComponent(menuInfo.selectionText)}`; | ||
270 | |||
271 | shell.openExternal(url); | ||
272 | }, | ||
273 | }); | ||
274 | |||
275 | menu.append(search); | ||
276 | this.addSeparator(menu); | ||
277 | |||
278 | return menu; | ||
279 | } | ||
280 | |||
281 | isSrcUrlValid(menuInfo) { | ||
282 | return menuInfo.srcURL && menuInfo.srcURL.length > 0; | ||
283 | } | ||
284 | |||
285 | /** | ||
286 | * Adds "Copy Image" and "Copy Image URL" items when `src` is valid. | ||
287 | */ | ||
288 | addImageItems(menu, menuInfo) { | ||
289 | const copyImage = new MenuItem({ | ||
290 | label: this.stringTable.copyImage(), | ||
291 | click: () => this.convertImageToBase64(menuInfo.srcURL, | ||
292 | dataURL => clipboard.writeImage(nativeImage.createFromDataURL(dataURL))), | ||
293 | }); | ||
294 | |||
295 | menu.append(copyImage); | ||
296 | |||
297 | const copyImageUrl = new MenuItem({ | ||
298 | label: this.stringTable.copyImageUrl(), | ||
299 | click: () => clipboard.writeText(menuInfo.srcURL), | ||
300 | }); | ||
301 | |||
302 | menu.append(copyImageUrl); | ||
303 | return menu; | ||
304 | } | ||
305 | |||
306 | /** | ||
307 | * Adds the Cut menu item | ||
308 | */ | ||
309 | addCut(menu, menuInfo) { | ||
310 | const target = this.getWebContents(); | ||
311 | menu.append(new MenuItem({ | ||
312 | label: this.stringTable.cut(), | ||
313 | accelerator: 'CommandOrControl+X', | ||
314 | enabled: menuInfo.editFlags.canCut, | ||
315 | click: () => target.cut(), | ||
316 | })); | ||
317 | |||
318 | return menu; | ||
319 | } | ||
320 | |||
321 | /** | ||
322 | * Adds the Copy menu item. | ||
323 | */ | ||
324 | addCopy(menu, menuInfo) { | ||
325 | const target = this.getWebContents(); | ||
326 | menu.append(new MenuItem({ | ||
327 | label: this.stringTable.copy(), | ||
328 | accelerator: 'CommandOrControl+C', | ||
329 | enabled: menuInfo.editFlags.canCopy, | ||
330 | click: () => target.copy(), | ||
331 | })); | ||
332 | |||
333 | return menu; | ||
334 | } | ||
335 | |||
336 | /** | ||
337 | * Adds the Paste menu item. | ||
338 | */ | ||
339 | addPaste(menu, menuInfo) { | ||
340 | const target = this.getWebContents(); | ||
341 | menu.append(new MenuItem({ | ||
342 | label: this.stringTable.paste(), | ||
343 | accelerator: 'CommandOrControl+V', | ||
344 | enabled: menuInfo.editFlags.canPaste, | ||
345 | click: () => target.paste(), | ||
346 | })); | ||
347 | |||
348 | return menu; | ||
349 | } | ||
350 | |||
351 | addPastePlain(menu, menuInfo) { | ||
352 | if ( | ||
353 | menuInfo.editFlags.canPaste | ||
354 | && !menuInfo.linkText | ||
355 | && !menuInfo.hasImageContents | ||
356 | ) { | ||
357 | const target = this.getWebContents(); | ||
358 | menu.append( | ||
359 | new MenuItem({ | ||
360 | label: 'Paste as plain text', | ||
361 | accelerator: 'CommandOrControl+Shift+V', | ||
362 | click: () => target.pasteAndMatchStyle(), | ||
363 | }), | ||
364 | ); | ||
365 | } | ||
366 | } | ||
367 | |||
368 | /** | ||
369 | * Adds a separator item. | ||
370 | */ | ||
371 | addSeparator(menu) { | ||
372 | menu.append(new MenuItem({ type: 'separator' })); | ||
373 | return menu; | ||
374 | } | ||
375 | |||
376 | /** | ||
377 | * Adds the "Inspect Element" menu item. | ||
378 | */ | ||
379 | addInspectElement(menu, menuInfo, needsSeparator = true) { | ||
380 | const target = this.getWebContents(); | ||
381 | if (!this.debugMode) return menu; | ||
382 | if (needsSeparator) this.addSeparator(menu); | ||
383 | |||
384 | const inspect = new MenuItem({ | ||
385 | label: this.stringTable.inspectElement(), | ||
386 | click: () => target.inspectElement(menuInfo.x, menuInfo.y), | ||
387 | }); | ||
388 | |||
389 | menu.append(inspect); | ||
390 | return menu; | ||
391 | } | ||
392 | |||
393 | /** | ||
394 | * Converts an image to a base-64 encoded string. | ||
395 | * | ||
396 | * @param {String} url The image URL | ||
397 | * @param {Function} callback A callback that will be invoked with the result | ||
398 | * @param {String} outputFormat The image format to use, defaults to 'image/png' | ||
399 | */ | ||
400 | convertImageToBase64(url, callback, outputFormat = 'image/png') { | ||
401 | let canvas = document.createElement('CANVAS'); | ||
402 | const ctx = canvas.getContext('2d'); | ||
403 | // eslint-disable-next-line no-undef | ||
404 | const img = new Image(); | ||
405 | img.crossOrigin = 'Anonymous'; | ||
406 | |||
407 | img.onload = () => { | ||
408 | canvas.height = img.height; | ||
409 | canvas.width = img.width; | ||
410 | ctx.drawImage(img, 0, 0); | ||
411 | |||
412 | const dataURL = canvas.toDataURL(outputFormat); | ||
413 | canvas = null; | ||
414 | callback(dataURL); | ||
415 | }; | ||
416 | |||
417 | img.src = url; | ||
418 | } | ||
419 | }; | ||