Skip to content

Commit

Permalink
feat: menu item to ipfs.addFromURL and keep filename
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Jul 5, 2018
1 parent 6913c96 commit 35f1311
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
10 changes: 7 additions & 3 deletions add-on/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,13 @@
"message": "Non-IPFS resource",
"description": "Default label for icon hidden in Page Action menu (pageAction_titleNonIpfs)"
},
"contextMenu_uploadToIpfs": {
"message": "Upload to IPFS",
"description": "An item in right-click context menu (contextMenu_uploadToIpfs)"
"contextMenu_AddToIpfsRawCid": {
"message": "Add to IPFS",
"description": "An item in right-click context menu (contextMenu_AddToIpfsRawCid)"
},
"contextMenu_AddToIpfsKeepFilename": {
"message": "Add to IPFS (Keep Filename)",
"description": "An item in right-click context menu (contextMenu_AddToIpfsKeepFilename)"
},
"notify_addonIssueTitle": {
"message": "IPFS Add-on Issue",
Expand Down
24 changes: 18 additions & 6 deletions add-on/src/lib/context-menus.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,34 @@ async function findUrlForContext (context) {

module.exports.findUrlForContext = findUrlForContext

const contextMenuUploadToIpfs = 'contextMenu_UploadToIpfs'
const contextMenuAddToIpfsRawCid = 'contextMenu_AddToIpfsRawCid'
const contextMenuAddToIpfsKeepFilename = 'contextMenu_AddToIpfsKeepFilename'
const contextMenuCopyCanonicalAddress = 'panelCopy_currentIpfsAddress'
const contextMenuCopyAddressAtPublicGw = 'panel_copyCurrentPublicGwUrl'

function createContextMenus (getState, runtime, ipfsPathValidator, { onUploadToIpfs, onCopyCanonicalAddress, onCopyAddressAtPublicGw }) {
function createContextMenus (getState, runtime, ipfsPathValidator, { onAddToIpfsRawCid, onAddToIpfsKeepFilename, onCopyCanonicalAddress, onCopyAddressAtPublicGw }) {
let copyAddressContexts = ['page', 'image', 'video', 'audio', 'link']
if (runtime.isFirefox) {
// https://github.com/ipfs-shipyard/ipfs-companion/issues/398
copyAddressContexts.push('page_action')
}
try {
browser.contextMenus.create({
id: contextMenuUploadToIpfs,
title: browser.i18n.getMessage(contextMenuUploadToIpfs),
id: contextMenuAddToIpfsRawCid,
title: browser.i18n.getMessage(contextMenuAddToIpfsRawCid),
contexts: ['image', 'video', 'audio', 'link'],
documentUrlPatterns: ['<all_urls>'],
enabled: false,
onclick: onUploadToIpfs
onclick: onAddToIpfsRawCid
})

browser.contextMenus.create({
id: contextMenuAddToIpfsKeepFilename,
title: browser.i18n.getMessage(contextMenuAddToIpfsKeepFilename),
contexts: ['image', 'video', 'audio', 'link'],
documentUrlPatterns: ['<all_urls>'],
enabled: false,
onclick: onAddToIpfsKeepFilename
})

browser.contextMenus.create({
Expand Down Expand Up @@ -76,7 +86,9 @@ function createContextMenus (getState, runtime, ipfsPathValidator, { onUploadToI
return {
async update (changedTabId) {
try {
await browser.contextMenus.update(contextMenuUploadToIpfs, {enabled: getState().peerCount > 0})
const canUpload = getState().peerCount > 0
await browser.contextMenus.update(contextMenuAddToIpfsRawCid, {enabled: canUpload})
await browser.contextMenus.update(contextMenuAddToIpfsKeepFilename, {enabled: canUpload})
if (changedTabId) {
// recalculate tab-dependant menu items
const currentTab = await browser.tabs.query({active: true, currentWindow: true}).then(tabs => tabs[0])
Expand Down
3 changes: 2 additions & 1 deletion add-on/src/lib/ipfs-companion.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ module.exports = async function init () {
dnsLink = createDnsLink(getState)
ipfsPathValidator = createIpfsPathValidator(getState, dnsLink)
contextMenus = createContextMenus(getState, runtime, ipfsPathValidator, {
onUploadToIpfs: addFromURL,
onAddToIpfsRawCid: addFromURL,
onAddToIpfsKeepFilename: (info) => addFromURL(info, {wrapWithDirectory: true}),
onCopyCanonicalAddress: () => copier.copyCanonicalAddress(),
onCopyAddressAtPublicGw: () => copier.copyAddressAtPublicGw()
})
Expand Down

0 comments on commit 35f1311

Please sign in to comment.