// Pure model for the command palette (no Tauri/DOM imports, so it is unit-tested under node). // Every item is navigation only: tabs are focused, profiles open a tab, decks and workspaces open, // the app's panel opens at a section, plugin commands run in their tab; the site adds its own // pages and completions (`site.palette`). Nothing here performs an account action on the user's behalf. import { rankItems } from '../palette.ts'; import { t } from './i18n.ts'; import type { IconName } from './icons.tsx'; import { menuText } from './menuModel.ts'; import type { PaletteSite } from './site.ts'; export { fuzzyScore, moveSelection } from '../palette.ts'; export type PaletteAction = | { type: 'focus-tab'; id: string } | { type: 'new-tab'; profile: string } | { type: 'panel'; section: string } | { type: 'open'; url: string } | { type: 'media' } /** Decks (navigation only): open a deck, make a new one, add or focus a column. */ | { type: 'deck'; deckId?: string } | { type: 'deck-new' } | { type: 'deck-add'; deckId: string } | { type: 'deck-focus'; deckId: string; columnId: string } /** Replaces the palette query (a completion); never navigates. */ | { type: 'complete'; text: string } /** A plugin's command: the shell asks tab `tab` to run it in its page (`tab_run_command`). */ | { type: 'plugin-command'; tab: string; command: string } /** Workspaces: restore one, or save the current layout under a name. */ | { type: 'workspace'; id: string } | { type: 'workspace-save' } /** Opens the app menu (the titlebar's "Settings and more"). */ | { type: 'menu' } /** Opens the find bar of the tab in front. */ | { type: 'find' }; /** The frame's groups; the app's items are grouped under its product name, plugin commands under the plugin's. */ export type PaletteGroup = 'Tabs' | 'Deck' | 'Workspaces' | 'Profiles'; export type PaletteItem = { id: string; /** A fixed group, the site's group, or a plugin's name for its commands. */ group: PaletteGroup | (string & {}); label: string; hint?: string; keywords?: string; /** The row's glyph (tabs show their favicon, profiles their avatar). */ icon?: IconName; action: PaletteAction; }; export type PaletteContext = { /** `favicon` and `color` only decorate the list; matching ignores them. */ tabs: Array<{ id: string; title: string; url: string; favicon?: string }>; profiles: Array<{ id: string; name: string; color?: string }>; activeProfile: string; /** Decks and their columns (titles only); absent when the app has no decks. */ decks?: Array<{ id: string; name: string; columns: Array<{ id: string; title: string; profile: string }> }>; /** The deck tab in front, if any. */ activeDeck?: string; /** Saved workspaces (names only) and the one opened last. */ workspaces?: Array<{ id: string; name: string; tabs: number; isDefault?: boolean }>; activeWorkspace?: string; /** Plugin commands of the focused site tab, already named by plugin (`pluginCommandItems`). */ commands?: { tab: string; items: PluginCommandItem[] }; }; /** What the palette knows about the app: its site items, its name and which frame features it has. */ export type PaletteSetup = { site: PaletteSite; product: string; decks: boolean; media: boolean; workspaces?: boolean; find?: boolean }; /** A plugin command as the palette lists it: the tab's report plus the plugin's display name. */ export type PluginCommandItem = { id: string; title: string; keywords: string; plugin: string }; /** Commands at most listed from one tab (the runtime and native code cap them too). */ export const MAX_PLUGIN_COMMANDS = 48; /** * Names each reported command (`:`) after its plugin (`@name` from the Plugins list); * commands registered outside a plugin's start are listed under "Plugins". Bounded. */ export function pluginCommandItems(reported: ReadonlyArray<{ id: string; title: string; keywords?: string }>, pluginNames: Readonly>): PluginCommandItem[] { const out: PluginCommandItem[] = []; for (const command of reported) { if (out.length >= MAX_PLUGIN_COMMANDS) break; if (typeof command?.id !== 'string' || typeof command.title !== 'string') continue; const at = command.id.indexOf(':'); if (at <= 0) continue; const owner = command.id.slice(0, at); const plugin = (owner !== '_' && pluginNames[owner]) || (owner === '_' ? t('wb.palette.plugins') : owner.replace(/\.plugin\.js$/, '')); out.push({ id: command.id, title: command.title.slice(0, 80), keywords: (command.keywords ?? '').slice(0, 160), plugin }); } return out; } /** The "Workspaces" group: switch to a saved workspace, save the current layout. */ function workspaceItems(context: PaletteContext): PaletteItem[] { const items: PaletteItem[] = []; for (const workspace of context.workspaces ?? []) { items.push({ id: `ws:${workspace.id}`, group: 'Workspaces', label: t('wb.palette.switchTo', { name: workspace.name }), hint: workspace.id === context.activeWorkspace ? t('wb.palette.active') : `${t('wb.ws.tabs', { count: workspace.tabs })}${workspace.isDefault ? ` ยท ${t('wb.palette.onStart')}` : ''}`, keywords: 'workspace layout restore session switch', action: { type: 'workspace', id: workspace.id }, }); } items.push({ id: 'ws-save', group: 'Workspaces', label: t('wb.ws.save'), keywords: 'workspace layout save session tabs panes', action: { type: 'workspace-save' } }); return items; } /** The "Deck" group: open/switch decks, add a column, focus a column of the active deck. */ function deckItems(context: PaletteContext): PaletteItem[] { const decks = context.decks ?? []; const items: PaletteItem[] = []; const active = decks.find(deck => deck.id === context.activeDeck); if (!decks.length) { items.push({ id: 'deck:open', group: 'Deck', label: t('wb.palette.openPower'), hint: 'Ctrl+Shift+D', keywords: 'deck tweetdeck columns power user', action: { type: 'deck' } }); return items; } for (const deck of decks) { if (deck.id === active?.id) continue; items.push({ id: `deck:${deck.id}`, group: 'Deck', label: t('wb.palette.switchTo', { name: deck.name }), hint: t('wb.deck.columnCount', { count: deck.columns.length }), keywords: 'deck tweetdeck columns power user switch', action: { type: 'deck', deckId: deck.id } }); } const target = active ?? decks[0]!; items.push({ id: `deck-add:${target.id}`, group: 'Deck', label: t('wb.palette.addColumn'), hint: target.name, keywords: 'deck column new tweetdeck', action: { type: 'deck-add', deckId: target.id } }); if (active) { active.columns.forEach((column, index) => { items.push({ id: `deck-focus:${column.id}`, group: 'Deck', label: t('wb.palette.focusColumn', { name: column.title }), hint: index < 9 ? `Alt+${index + 1}` : undefined, keywords: 'column deck', action: { type: 'deck-focus', deckId: active.id, columnId: column.id } }); }); } items.push({ id: 'deck-new', group: 'Deck', label: t('wb.palette.newDeck'), keywords: 'deck columns power user create', action: { type: 'deck-new' } }); return items; } export function buildItems(context: PaletteContext, query: string, setup: PaletteSetup): PaletteItem[] { const { site } = setup; const items: PaletteItem[] = []; const text = query.trim(); const search = text ? site.search?.(text) ?? null : null; if (search) items.push(search); for (const tab of context.tabs) { items.push({ id: `tab:${tab.id}`, group: 'Tabs', label: tab.title || tab.url, hint: tab.url, keywords: tab.url, action: { type: 'focus-tab', id: tab.id } }); } if (setup.decks) items.push(...deckItems(context)); if (setup.workspaces !== false) items.push(...workspaceItems(context)); for (const profile of context.profiles) { items.push({ id: `profile:${profile.id}`, group: 'Profiles', label: t('wb.palette.newTabAs', { name: profile.name }), hint: profile.id === context.activeProfile ? t('wb.palette.active') : undefined, keywords: 'account switch', action: { type: 'new-tab', profile: profile.id } }); } for (const section of site.sections) { items.push({ id: `panel:${section.id}`, group: setup.product, label: section.label(), keywords: section.keywords, icon: section.icon, action: { type: 'panel', section: section.id } }); } if (setup.media) items.push({ id: 'media', group: setup.product, label: t('wb.palette.media'), hint: 'Ctrl+Shift+G', keywords: 'photos images screenshots attach paste media', icon: 'image', action: { type: 'media' } }); // Find in the page of the tab in front (the shell's find bar; nothing is sent anywhere). if (setup.find !== false) items.push({ id: 'find', group: setup.product, label: menuText('find'), hint: 'Ctrl+F', keywords: 'find search page text match', icon: 'search', action: { type: 'find' } }); // The app menu (the titlebar's "Settings and more"): language, theme, mode, zoom and the rest. items.push({ id: 'app-menu', group: setup.product, label: menuText('label'), hint: 'Alt+F', keywords: 'menu more settings language theme mode zoom options preferences', icon: 'settings', action: { type: 'menu' } }); // Plugin commands, grouped under each plugin's name; they run in the focused site tab. if (context.commands) { const { tab } = context.commands; // One heading per plugin: stable sort keeps each plugin's own order. const sorted = context.commands.items.slice(0, MAX_PLUGIN_COMMANDS).sort((a, b) => a.plugin.localeCompare(b.plugin)); for (const command of sorted) { items.push({ id: `cmd:${command.id}`, group: command.plugin, label: command.title, keywords: `${command.plugin} plugin ${command.keywords}`, icon: 'bolt', action: { type: 'plugin-command', tab, command: command.id } }); } } items.push(...site.items?.() ?? []); if (!text) return items; const completions = site.completions?.(query.replace(/^\s+/, '')) ?? []; const scored = rankItems(items, text, item => item === search); // The literal search stays available as the last resort. return search ? [...completions, ...scored, search] : [...completions, ...scored]; }