Skip to content

Commit

Permalink
✨ Link context menus
Browse files Browse the repository at this point in the history
  • Loading branch information
trickypr committed Nov 14, 2023
1 parent bdf1e27 commit ebf000b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/actors/ContextMenu.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export interface ContextMenuInfo {
}

textSelection?: string
href?: string
}
12 changes: 12 additions & 0 deletions src/actors/ContextMenuChild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ const lazy = lazyESModuleGetters({
})

export class ContextMenuChild extends JSWindowActorChild {
getHrefIfExists(target: Node): string | undefined {
if ((target as HTMLAnchorElement).href) {
return (target as HTMLAnchorElement).href
}

if (target.parentElement) {
return this.getHrefIfExists(target.parentElement)
}
}

handleEvent(event: MouseEvent & { inputSource: number }) {
const data: {
position: ContextMenuInfo['position']
Expand All @@ -29,6 +39,8 @@ export class ContextMenuChild extends JSWindowActorChild {
data.textSelection = selectionInfo.fullText
}

if (event.target) data.href = this.getHrefIfExists(event.target as Node)

this.sendAsyncMessage('contextmenu', data satisfies ContextMenuInfo)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,38 @@
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<script lang="ts">
import { browserContextMenuInfo } from '../../lib/globalApi'
import {
browserContextMenuInfo,
openTab,
setCurrentTab,
} from '../../lib/globalApi'
import { resource } from '../../lib/resources'
import { getClipboardHelper } from '../../lib/xul/ccWrapper'
$: textSelection = $browserContextMenuInfo.textSelection
$: href = $browserContextMenuInfo.href
</script>

<xul:menupopup id="browser_context_menu">
<xul:menuitem label={'Test'} />
{#if href}
<xul:menuitem
label="Open link in new tab"
on:command={() => {
const tab = openTab(resource.NetUtil.newURI(href))
if (Services.prefs.getBoolPref('browser.tabs.newTabFocus', false)) {
queueMicrotask(() => setCurrentTab(tab))
}
}}
/>
<xul:menuitem
label="Copy link location"
on:command={() => {
const clipboardHelper = getClipboardHelper()
if (href) clipboardHelper.copyString(href, 0)
}}
/>
{/if}
{#if textSelection}
<xul:menuitem
label={'Copy'}
Expand Down
26 changes: 17 additions & 9 deletions src/content/browser/lib/globalApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ const newWindowUri = uriPref('browser.newwindow.default')
export const tabs = viewableWritable([new Tab(newWindowUri())])

export function openTab(uri: nsIURIType = newTabUri()) {
const newTab = new Tab(uri)
tabs.update((tabs) => {
const newTab = new Tab(uri)
selectedTab.set(newTab.getId())
return [...tabs, newTab]
})
return newTab
}

export function closeTab(tab: Tab) {
Expand Down Expand Up @@ -56,6 +57,11 @@ function getCurrent(): Tab | undefined {
return tabs.readOnce().find((t) => t.getId() == internalSelectedTab)
}

export function setCurrentTab(tab: Tab) {
const index = tabs.readOnce().findIndex((t) => t.getId() == tab.getId())
setCurrentTabIndex(index)
}

export function runOnCurrentTab<R>(method: (tab: Tab) => R): R | void {
const currentTab = getCurrent()
if (currentTab) return method(currentTab)
Expand Down Expand Up @@ -120,14 +126,16 @@ export const windowApi = {
showContextMenu: (menuInfo: ContextMenuInfo) => {
browserContextMenuInfo.set(menuInfo)

const contextMenu = document.getElementById(
'browser_context_menu',
) as XULMenuPopup
contextMenu.openPopupAtScreen(
menuInfo.position.screenX,
menuInfo.position.screenY,
true,
)
queueMicrotask(() => {
const contextMenu = document.getElementById(
'browser_context_menu',
) as XULMenuPopup
contextMenu.openPopupAtScreen(
menuInfo.position.screenX,
menuInfo.position.screenY,
true,
)
})
},
}

Expand Down
3 changes: 3 additions & 0 deletions src/prefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ pref('browser.keybinds.tab7', 'accel+7');
pref('browser.keybinds.tab8', 'accel+8');
pref('browser.keybinds.findInPage', 'accel+F');

// Behaviour prefs
pref('browser.tabs.newTabFocus', false);

// =======================================================
// Gecko prefs

Expand Down

0 comments on commit ebf000b

Please sign in to comment.