git ssb

0+

alanz / patchwork



forked from Matt McKegg / patchwork

Tree: eab27f622457908e54a6aae69afd87d09e3c52d5

Files: eab27f622457908e54a6aae69afd87d09e3c52d5 / lib / context-menu-and-spellcheck.js

2859 bytesRaw
1var {remote, shell, clipboard, ipcRenderer} = require('electron')
2var {SpellCheckHandler, ContextMenuListener, ContextMenuBuilder} = require('electron-spellchecker')
3var {MenuItem, Menu} = remote
4var ref = require('ssb-ref')
5
6window.spellCheckHandler = new SpellCheckHandler()
7window.spellCheckHandler.attachToInput()
8
9// Start off as US English, America #1 (lol)
10window.spellCheckHandler.switchLanguage('en-US')
11
12var contextMenuBuilder = new ContextMenuBuilder(window.spellCheckHandler, null, true)
13
14contextMenuBuilder.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
62module.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 }
100 menu.popup(remote.getCurrentWindow())
101 }).catch((err) => {
102 throw err
103 })
104})
105

Built with git-ssb-web