git ssb

10+

Matt McKegg / patchwork



Tree: 7e44e13afc44b6b906290606afe84a7c45b33eff

Files: 7e44e13afc44b6b906290606afe84a7c45b33eff / lib / context-menu-and-spellcheck.js

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

Built with git-ssb-web