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:
@@ -1,6 +1,6 @@
|
||||
import { defineBackground } from 'wxt/utils/define-background';
|
||||
import type { AnalyzePayload, LexAIConfig, LexAIResponse } from '@lib/types';
|
||||
import { ACTIONS, ACTION_LABELS, CONTEXT_MENU_STYLES } from '@lib/actions';
|
||||
import { CONTEXT_MENU_ENTRIES, findContextMenuEntry } from '@lib/actions';
|
||||
import { decryptApiKey } from '@lib/crypto';
|
||||
import { callProvider, getSystemPrompt, listModels } from '@lib/providers';
|
||||
|
||||
@@ -40,35 +40,26 @@ export default defineBackground(() => {
|
||||
// ─── Context menus ───────────────────────────────────────────────────────
|
||||
chrome.runtime.onInstalled.addListener(() => {
|
||||
chrome.contextMenus.removeAll(() => {
|
||||
ACTIONS.forEach(action => {
|
||||
CONTEXT_MENU_ENTRIES.forEach((entry) => {
|
||||
chrome.contextMenus.create({
|
||||
id: `lexai-${action}`,
|
||||
title: `⚡ LexAI: ${ACTION_LABELS[action]}`,
|
||||
id: entry.id,
|
||||
parentId: entry.parentId,
|
||||
title: entry.title,
|
||||
contexts: ['selection'],
|
||||
});
|
||||
CONTEXT_MENU_STYLES.forEach(style => {
|
||||
chrome.contextMenus.create({
|
||||
id: `lexai-${action}-${style.toLowerCase()}`,
|
||||
parentId: `lexai-${action}`,
|
||||
title: style,
|
||||
contexts: ['selection'],
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
chrome.contextMenus.onClicked.addListener((info, tab) => {
|
||||
if (!info.selectionText || !tab?.id) return;
|
||||
// Parse action and style from menuItemId e.g. "lexai-fix-formal"
|
||||
const parts = info.menuItemId.toString().replace('lexai-', '').split('-');
|
||||
const action = parts[0];
|
||||
const style = parts[1] ? parts[1].charAt(0).toUpperCase() + parts[1].slice(1) : 'Default';
|
||||
const entry = findContextMenuEntry(info.menuItemId.toString());
|
||||
if (!entry) return;
|
||||
chrome.tabs.sendMessage(tab.id, {
|
||||
type: 'lexai-context-menu',
|
||||
action,
|
||||
action: entry.action,
|
||||
text: info.selectionText,
|
||||
style,
|
||||
style: entry.style,
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user