git ssb

16+

Dominic / patchbay



Tree: 5b17ba2a85122774945850eeb89c50eaded5c446

Files: 5b17ba2a85122774945850eeb89c50eaded5c446 / app / sync / initialise / electron-context-menu-and-spellcheck.js

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

Built with git-ssb-web