askrobots / dbbasic-browser
Public
A desktop browser with x402 built in: when a page asks for payment, it pays in stablecoins and shows the content.
danq9
site icons (favicons) in tabs, address bar suggestions and the bookmarks panel
7249579
15 hours, 50 minutes ago
History
context-menu.ts · 4.5 KB · 106 lines
| 1 | /** |
| 2 | * The right-click menu: which items to show for what was clicked. Electron-free (main.ts turns the items into a native |
| 3 | * menu and runs them), so it is unit-tested. |
| 4 | * |
| 5 | * on a link Open Link in New Tab, Copy Link Address |
| 6 | * on an image Open Image in New Tab, Copy Image, Copy Image Address, Save Image As… |
| 7 | * in a text field spelling suggestions, Undo, Redo, Cut, Copy, Paste, Select All |
| 8 | * on selected text Copy, Search askrobots for "…", Ask the Assistant About This |
| 9 | * anywhere else Back, Forward, Reload |
| 10 | * and last Inspect Element (pages only) |
| 11 | */ |
| 12 | |
| 13 | /** The parts of Electron's context-menu params this uses. */ |
| 14 | export interface ClickedOn { |
| 15 | linkURL?: string; |
| 16 | linkText?: string; |
| 17 | selectionText?: string; |
| 18 | isEditable?: boolean; |
| 19 | editFlags?: { canUndo?: boolean; canRedo?: boolean; canCut?: boolean; canCopy?: boolean; canPaste?: boolean; canSelectAll?: boolean }; |
| 20 | mediaType?: string; |
| 21 | srcURL?: string; |
| 22 | hasImageContents?: boolean; |
| 23 | misspelledWord?: string; |
| 24 | dictionarySuggestions?: string[]; |
| 25 | } |
| 26 | |
| 27 | export interface MenuContext { |
| 28 | /** A web page (true) or the browser's own toolbar and panels (false: only the text items). */ |
| 29 | page: boolean; |
| 30 | canGoBack: boolean; |
| 31 | canGoForward: boolean; |
| 32 | /** False when new tabs are off (the caller-supplied network mode opens none). */ |
| 33 | newTabs: boolean; |
| 34 | } |
| 35 | |
| 36 | export type MenuItem = { id: string; label: string; enabled?: boolean } | { type: "separator" }; |
| 37 | |
| 38 | const web = (u: string | undefined): u is string => !!u && /^https?:\/\//i.test(u); |
| 39 | /** At most n characters of selected text, on one line, for a menu label. */ |
| 40 | export function clipLabel(s: string, n = 24): string { |
| 41 | const one = s.replace(/\s+/g, " ").trim(); |
| 42 | return one.length > n ? one.slice(0, n - 1).trimEnd() + "…" : one; |
| 43 | } |
| 44 | |
| 45 | export function contextMenuItems(p: ClickedOn, c: MenuContext): MenuItem[] { |
| 46 | const out: MenuItem[] = []; |
| 47 | const sep = () => out.push({ type: "separator" }); |
| 48 | const f = p.editFlags ?? {}; |
| 49 | const link = p.linkURL && !/^javascript:/i.test(p.linkURL) ? p.linkURL : ""; |
| 50 | // Right-clicking a link can select the word under the pointer (macOS does): that is not a selection worth searching for. |
| 51 | const raw = (p.selectionText ?? "").trim(); |
| 52 | const selection = link && raw && (p.linkText ?? "").includes(raw) ? "" : raw; |
| 53 | const image = p.mediaType === "image" && !!p.srcURL; |
| 54 | |
| 55 | if (p.isEditable && p.misspelledWord) { |
| 56 | const sug = (p.dictionarySuggestions ?? []).slice(0, 5); |
| 57 | if (sug.length) sug.forEach((w, i) => out.push({ id: `spell:${i}`, label: w })); |
| 58 | else out.push({ id: "no-suggestions", label: "No Guesses Found", enabled: false }); |
| 59 | sep(); |
| 60 | } |
| 61 | if (c.page && link) { |
| 62 | if (web(link) && c.newTabs) out.push({ id: "open-link-tab", label: "Open Link in New Tab" }); |
| 63 | out.push({ id: "copy-link", label: "Copy Link Address" }); |
| 64 | sep(); |
| 65 | } |
| 66 | if (c.page && image) { |
| 67 | if (web(p.srcURL) && c.newTabs) out.push({ id: "open-image-tab", label: "Open Image in New Tab" }); |
| 68 | out.push({ id: "copy-image", label: "Copy Image" }); |
| 69 | if (web(p.srcURL)) out.push({ id: "copy-image-address", label: "Copy Image Address" }, { id: "save-image", label: "Save Image As…" }); |
| 70 | sep(); |
| 71 | } |
| 72 | if (p.isEditable) { |
| 73 | out.push( |
| 74 | { id: "undo", label: "Undo", enabled: f.canUndo !== false }, |
| 75 | { id: "redo", label: "Redo", enabled: f.canRedo !== false }, |
| 76 | { type: "separator" }, |
| 77 | { id: "cut", label: "Cut", enabled: f.canCut !== false }, |
| 78 | { id: "copy", label: "Copy", enabled: f.canCopy !== false }, |
| 79 | { id: "paste", label: "Paste", enabled: f.canPaste !== false }, |
| 80 | { id: "select-all", label: "Select All", enabled: f.canSelectAll !== false }, |
| 81 | ); |
| 82 | sep(); |
| 83 | } else if (selection) { |
| 84 | out.push({ id: "copy", label: "Copy" }); |
| 85 | if (c.page) { |
| 86 | if (c.newTabs) out.push({ id: "search-selection", label: `Search askrobots for “${clipLabel(selection)}”` }); |
| 87 | out.push({ id: "ask-selection", label: "Ask the Assistant About This" }); |
| 88 | } |
| 89 | sep(); |
| 90 | } |
| 91 | if (c.page && !link && !image && !p.isEditable && !selection) { |
| 92 | out.push( |
| 93 | { id: "back", label: "Back", enabled: c.canGoBack }, |
| 94 | { id: "forward", label: "Forward", enabled: c.canGoForward }, |
| 95 | { id: "reload", label: "Reload" }, |
| 96 | ); |
| 97 | sep(); |
| 98 | } |
| 99 | if (c.page) out.push({ id: "inspect", label: "Inspect Element" }); |
| 100 | |
| 101 | // no separator first, last, or twice in a row |
| 102 | const tidy: MenuItem[] = []; |
| 103 | for (const it of out) if (!("type" in it) || (tidy.length && !("type" in tidy[tidy.length - 1]!))) tidy.push(it); |
| 104 | while (tidy.length && "type" in tidy[tidy.length - 1]!) tidy.pop(); |
| 105 | return tidy; |
| 106 | } |