refactor: declarative context-menu registry; named selection threshold

CONTEXT_MENU_ENTRIES maps every menu id to {action, style, parentId,
title}, replacing the split('-') id parsing that only worked because no
name contained a hyphen. Foreign menu ids are now ignored explicitly.
Parity pinned by tests enumerating all 30 previous ids. The 10-char
toolbar trigger threshold is now the named MIN_SELECTION_LENGTH.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
john kevin asprec
2026-07-14 21:44:16 +08:00
parent f0962471b4
commit d0c4cc947d
4 changed files with 100 additions and 21 deletions

View File

@@ -21,3 +21,43 @@ export type WritingStyle = (typeof WRITING_STYLES)[number];
// Context menus omit 'Default' — the parent action item already covers it.
export const CONTEXT_MENU_STYLES = WRITING_STYLES.filter((s) => s !== 'Default');
// Minimum selection length (chars, after trim) before the floating toolbar
// appears. Kept deliberately above 1-2 chars so accidental double-click
// selections don't trigger the UI.
export const MIN_SELECTION_LENGTH = 10;
// ─── Context-menu registry ────────────────────────────────────────────────────
// Declarative id -> {action, style} mapping. Replaces the old split('-')
// parsing of menuItemId, which only worked because no action or style name
// contained a hyphen. Order matters: each parent precedes its children so
// chrome.contextMenus.create never sees an unknown parentId.
export interface ContextMenuEntry {
id: string;
action: ActionId;
style: WritingStyle;
// 'Default' entries are top-level parents; styled entries nest under them.
parentId?: string;
title: string;
}
export const CONTEXT_MENU_ENTRIES: ContextMenuEntry[] = ACTIONS.flatMap((action) => [
{
id: `lexai-${action}`,
action,
style: 'Default' as WritingStyle,
title: `⚡ LexAI: ${ACTION_LABELS[action]}`,
},
...CONTEXT_MENU_STYLES.map((style) => ({
id: `lexai-${action}-${style.toLowerCase()}`,
action,
style,
parentId: `lexai-${action}`,
title: style as string,
})),
]);
export function findContextMenuEntry(menuItemId: string): ContextMenuEntry | undefined {
return CONTEXT_MENU_ENTRIES.find((e) => e.id === menuItemId);
}