git ssb

10+

Matt McKegg / patchwork



Tree: ed68746e557dd7a29e7bf0327cdef4794c412633

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

4485 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 if (ref.isBlob(extractedRef) && menuInfo.hasImageContents) {
65 var copyEmbed = new MenuItem({
66 label: `Copy Embed Markdown`,
67 click: () => {
68 // Omit the mailto: portion of the link; we just want the address
69 clipboard.writeText(`![${menuInfo.titleText}](${extractedRef})`)
70 }
71 })
72 menu.append(copyEmbed)
73 }
74 }
75
76 if (this.isSrcUrlValid(menuInfo)) {
77 if (!isFile) this.addSeparator(menu)
78 this.addImageItems(menu, menuInfo)
79 }
80
81 this.addInspectElement(menu, menuInfo)
82 this.processMenu(menu, menuInfo)
83
84 return menu
85 }
86
87 module.exports.menu = new ContextMenuListener((info) => {
88 contextMenuBuilder.buildMenuForElement(info).then((menu) => {
89 var element = document.elementFromPoint(info.x, info.y)
90 while (element && !element.msg) {
91 element = element.parentNode
92 }
93
94 menu.append(new MenuItem({
95 label: 'Inspect Server Process',
96 click: function () {
97 ipcRenderer.send('open-background-devtools')
98 }
99 }))
100
101 menu.append(new MenuItem({
102 type: 'separator'
103 }))
104
105 menu.append(new MenuItem({
106 label: 'Reload',
107 click: function (item, focusedWindow) {
108 if (focusedWindow) {
109 focusedWindow.reload()
110 }
111 }
112 }))
113
114 if (element && element.msg) {
115 menu.append(new MenuItem({
116 type: 'separator'
117 }))
118 menu.append(new MenuItem({
119 label: 'Copy Message ID',
120 click: function () {
121 clipboard.writeText(element.msg.key)
122 }
123 }))
124 if (element.msg.value.content && element.msg.value.content.text) {
125 menu.append(new MenuItem({
126 label: 'Copy Message Text',
127 click: function () {
128 clipboard.writeText(element.msg.value.content.text)
129 }
130 }))
131 }
132 menu.append(new MenuItem({
133 label: 'Copy External Link',
134 click: function () {
135 const key = element.msg.key
136 const gateway = config.gateway ||
137 'https://viewer.scuttlebot.io'
138 const url = `${gateway}/${encodeURIComponent(key)}`
139 clipboard.writeText(url)
140 }
141 }))
142 }
143 menu.popup(remote.getCurrentWindow())
144 }).catch((err) => {
145 throw err
146 })
147 })
148}
149

Built with git-ssb-web