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

@@ -1,6 +1,6 @@
import { defineContentScript } from 'wxt/utils/define-content-script';
import { isExtensionValid, safeSendMessage } from '@lib/messaging';
import { WRITING_STYLES } from '@lib/actions';
import { MIN_SELECTION_LENGTH, WRITING_STYLES } from '@lib/actions';
export default defineContentScript({
matches: ['<all_urls>'],
@@ -48,7 +48,7 @@ export default defineContentScript({
const end = el.selectionEnd ?? -1;
if (end - start >= 2) {
const text = el.value.substring(start, end).trim();
if (text.length > 10) {
if (text.length >= MIN_SELECTION_LENGTH) {
selectedText = text;
storedStart = start;
storedEnd = end;
@@ -64,7 +64,7 @@ export default defineContentScript({
const sel = window.getSelection();
if (sel && sel.rangeCount > 0) {
const text = sel.toString().trim();
if (text.length > 10) {
if (text.length >= MIN_SELECTION_LENGTH) {
selectedText = text;
storedRange = sel.getRangeAt(0).cloneRange(); // CLONE — selection will be lost later
storedElement = null;