Commit 89d7179cbe01cfd1b8fbbf423e3c690d4d74a2b6
Merge pull request #515 from ssbc/copy-external-link
add "Copy External Link" to message context menuMatt McKegg authored on 4/25/2017, 10:14:27 AM
GitHub committed on 4/25/2017, 10:14:27 AM
Parent: a96d9b655853661334b1be9e78994f0a237f7063
Parent: 731f7fef3b396f2bcfd92c8d02413ac6908684d4
Files changed
lib/context-menu-and-spellcheck.js | changed |
main-window.js | changed |
lib/context-menu-and-spellcheck.js | ||
---|---|---|
@@ -2,111 +2,125 @@ | ||
2 | 2 | var {SpellCheckHandler, ContextMenuListener, ContextMenuBuilder} = require('electron-spellchecker') |
3 | 3 | var {MenuItem, Menu} = remote |
4 | 4 | var ref = require('ssb-ref') |
5 | 5 | |
6 | -window.spellCheckHandler = new SpellCheckHandler() | |
7 | -window.spellCheckHandler.attachToInput() | |
6 | +module.exports = setupContextMenuAndSpellCheck | |
8 | 7 | |
9 | -// Start off as US English, America #1 (lol) | |
10 | -window.spellCheckHandler.switchLanguage('en-US') | |
8 | +function setupContextMenuAndSpellCheck (config) { | |
9 | + window.spellCheckHandler = new SpellCheckHandler() | |
10 | + window.spellCheckHandler.attachToInput() | |
11 | 11 | |
12 | -var contextMenuBuilder = new ContextMenuBuilder(window.spellCheckHandler, null, true) | |
12 | + // Start off as US English, America #1 (lol) | |
13 | + window.spellCheckHandler.switchLanguage('en-US') | |
13 | 14 | |
14 | -contextMenuBuilder.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) | |
15 | + var contextMenuBuilder = new ContextMenuBuilder(window.spellCheckHandler, null, true) | |
19 | 16 | |
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 | - }) | |
17 | + contextMenuBuilder.buildMenuForLink = function (menuInfo) { | |
18 | + var menu = new Menu() | |
19 | + var isEmailAddress = menuInfo.linkURL.startsWith('mailto:') | |
20 | + var isFile = menuInfo.linkURL.startsWith('file:') | |
21 | + var extractedRef = ref.extract(menuInfo.linkURL) | |
28 | 22 | |
29 | - var openLink = new MenuItem({ | |
30 | - label: this.stringTable.openLinkUrl(), | |
31 | - click: () => { | |
32 | - shell.openExternal(menuInfo.linkURL) | |
33 | - } | |
34 | - }) | |
23 | + if (!isFile) { | |
24 | + var copyLink = new MenuItem({ | |
25 | + label: isEmailAddress ? this.stringTable.copyMail() : this.stringTable.copyLinkUrl(), | |
26 | + click: () => { | |
27 | + // Omit the mailto: portion of the link; we just want the address | |
28 | + clipboard.writeText(isEmailAddress ? menuInfo.linkText : menuInfo.linkURL) | |
29 | + } | |
30 | + }) | |
35 | 31 | |
36 | - menu.append(copyLink) | |
37 | - menu.append(openLink) | |
38 | - } | |
32 | + var openLink = new MenuItem({ | |
33 | + label: this.stringTable.openLinkUrl(), | |
34 | + click: () => { | |
35 | + shell.openExternal(menuInfo.linkURL) | |
36 | + } | |
37 | + }) | |
39 | 38 | |
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 | - } | |
39 | + menu.append(copyLink) | |
40 | + menu.append(openLink) | |
41 | + } | |
50 | 42 | |
51 | - if (this.isSrcUrlValid(menuInfo)) { | |
52 | - if (!isFile) this.addSeparator(menu) | |
53 | - this.addImageItems(menu, menuInfo) | |
54 | - } | |
43 | + if (extractedRef) { | |
44 | + var copyRef = new MenuItem({ | |
45 | + label: `Copy Link Ref (${extractedRef.slice(0, 10)}...)`, | |
46 | + click: () => { | |
47 | + // Omit the mailto: portion of the link; we just want the address | |
48 | + clipboard.writeText(extractedRef) | |
49 | + } | |
50 | + }) | |
51 | + menu.append(copyRef) | |
52 | + } | |
55 | 53 | |
56 | - this.addInspectElement(menu, menuInfo) | |
57 | - this.processMenu(menu, menuInfo) | |
54 | + if (this.isSrcUrlValid(menuInfo)) { | |
55 | + if (!isFile) this.addSeparator(menu) | |
56 | + this.addImageItems(menu, menuInfo) | |
57 | + } | |
58 | 58 | |
59 | - return menu | |
60 | -} | |
59 | + this.addInspectElement(menu, menuInfo) | |
60 | + this.processMenu(menu, menuInfo) | |
61 | 61 | |
62 | -module.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 | - } | |
62 | + return menu | |
63 | + } | |
68 | 64 | |
69 | - menu.append(new MenuItem({ | |
70 | - label: 'Inspect Server Process', | |
71 | - click: function () { | |
72 | - ipcRenderer.send('open-background-devtools') | |
65 | + module.exports = new ContextMenuListener((info) => { | |
66 | + contextMenuBuilder.buildMenuForElement(info).then((menu) => { | |
67 | + var element = document.elementFromPoint(info.x, info.y) | |
68 | + while (element && !element.msg) { | |
69 | + element = element.parentNode | |
73 | 70 | } |
74 | - })) | |
75 | 71 | |
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() | |
72 | + menu.append(new MenuItem({ | |
73 | + label: 'Inspect Server Process', | |
74 | + click: function () { | |
75 | + ipcRenderer.send('open-background-devtools') | |
85 | 76 | } |
86 | - } | |
87 | - })) | |
77 | + })) | |
88 | 78 | |
89 | - if (element && element.msg) { | |
90 | 79 | menu.append(new MenuItem({ |
91 | 80 | type: 'separator' |
92 | 81 | })) |
82 | + | |
93 | 83 | menu.append(new MenuItem({ |
94 | - label: 'Copy Message ID', | |
95 | - click: function () { | |
96 | - clipboard.writeText(element.msg.key) | |
84 | + label: 'Reload', | |
85 | + click: function (item, focusedWindow) { | |
86 | + if (focusedWindow) { | |
87 | + focusedWindow.reload() | |
88 | + } | |
97 | 89 | } |
98 | 90 | })) |
99 | - if (element.msg.value.content.text) { | |
91 | + | |
92 | + if (element && element.msg) { | |
100 | 93 | menu.append(new MenuItem({ |
101 | - label: 'Copy Message Text', | |
94 | + type: 'separator' | |
95 | + })) | |
96 | + menu.append(new MenuItem({ | |
97 | + label: 'Copy Message ID', | |
102 | 98 | click: function () { |
103 | - clipboard.writeText(element.msg.value.content.text) | |
99 | + clipboard.writeText(element.msg.key) | |
104 | 100 | } |
105 | 101 | })) |
102 | + if (element.msg.value.content.text) { | |
103 | + menu.append(new MenuItem({ | |
104 | + label: 'Copy Message Text', | |
105 | + click: function () { | |
106 | + clipboard.writeText(element.msg.value.content.text) | |
107 | + } | |
108 | + })) | |
109 | + } | |
110 | + menu.append(new MenuItem({ | |
111 | + label: 'Copy External Link', | |
112 | + click: function () { | |
113 | + const key = element.msg.key | |
114 | + const gateway = config.gateway || | |
115 | + 'https://viewer.scuttlebot.io' | |
116 | + const url = `${gateway}/${encodeURIComponent(key)}` | |
117 | + clipboard.writeText(url) | |
118 | + } | |
119 | + })) | |
106 | 120 | } |
107 | - } | |
108 | - menu.popup(remote.getCurrentWindow()) | |
109 | - }).catch((err) => { | |
110 | - throw err | |
121 | + menu.popup(remote.getCurrentWindow()) | |
122 | + }).catch((err) => { | |
123 | + throw err | |
124 | + }) | |
111 | 125 | }) |
112 | -}) | |
126 | +} |
main-window.js | ||
---|---|---|
@@ -9,11 +9,10 @@ | ||
9 | 9 | var insertCss = require('insert-css') |
10 | 10 | var nest = require('depnest') |
11 | 11 | var LatestUpdate = require('./lib/latest-update') |
12 | 12 | var ref = require('ssb-ref') |
13 | +var setupContextMenuAndSpellCheck = require('./lib/context-menu-and-spellcheck') | |
13 | 14 | |
14 | -require('./lib/context-menu-and-spellcheck.js') | |
15 | - | |
16 | 15 | module.exports = function (config) { |
17 | 16 | var sockets = combine( |
18 | 17 | overrideConfig(config), |
19 | 18 | addCommand('app.navigate', setView), |
@@ -23,8 +22,9 @@ | ||
23 | 22 | require('./overrides') |
24 | 23 | ) |
25 | 24 | |
26 | 25 | var api = entry(sockets, nest({ |
26 | + 'config.sync.load': 'first', | |
27 | 27 | 'keys.sync.id': 'first', |
28 | 28 | 'sbot.obs.connection': 'first', |
29 | 29 | 'sbot.async.get': 'first', |
30 | 30 | 'blob.sync.url': 'first', |
@@ -36,8 +36,10 @@ | ||
36 | 36 | 'profile.sheet.edit': 'first', |
37 | 37 | 'app.navigate': 'first' |
38 | 38 | })) |
39 | 39 | |
40 | + setupContextMenuAndSpellCheck(api.config.sync.load()) | |
41 | + | |
40 | 42 | var id = api.keys.sync.id() |
41 | 43 | var latestUpdate = LatestUpdate() |
42 | 44 | |
43 | 45 | // prompt to setup profile on first use |
Built with git-ssb-web