From 56d4733e2a095361011ca4102f53cb174fd28af4 Mon Sep 17 00:00:00 2001 From: Gabe Date: Thu, 16 Oct 2025 20:16:03 +0800 Subject: [PATCH] feat: terms style --- src/config/i18n.js | 5 +++++ src/config/rules.js | 5 +++-- src/libs/rules.js | 3 +++ src/libs/translator.js | 11 +++++++---- src/views/Options/Rules.js | 14 +++++++++++--- 5 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/config/i18n.js b/src/config/i18n.js index 5827fe4..9602053 100644 --- a/src/config/i18n.js +++ b/src/config/i18n.js @@ -718,6 +718,11 @@ export const I18N = { en: `Selector Style`, zh_TW: `選擇器節點樣式`, }, + terms_style: { + zh: `专业术语样式`, + en: `Terms Style`, + zh_TW: `專業術語樣式`, + }, selector_style_helper: { zh: `开启翻译时注入。`, en: `It is injected when translation is turned on.`, diff --git a/src/config/rules.js b/src/config/rules.js index a37d37e..ce47ca3 100644 --- a/src/config/rules.js +++ b/src/config/rules.js @@ -78,8 +78,7 @@ background: linear-gradient( export const DEFAULT_SELECTOR = "h1, h2, h3, h4, h5, h6, li, p, dd, blockquote, figcaption, label, legend"; -export const DEFAULT_IGNORE_SELECTOR = - "aside, button, footer, pre, mark, nav"; +export const DEFAULT_IGNORE_SELECTOR = "aside, button, footer, pre, mark, nav"; export const DEFAULT_KEEP_SELECTOR = `a:has(code)`; export const DEFAULT_RULE = { pattern: "", // 匹配网址 @@ -94,6 +93,7 @@ export const DEFAULT_RULE = { transOpen: GLOBAL_KEY, // 开启翻译 bgColor: "", // 译文颜色 textDiyStyle: "", // 自定义译文样式 + termsStyle: "", // 专业术语样式 selectStyle: "", // 选择器节点样式 parentStyle: "", // 选择器父节点样式 grandStyle: "", // 选择器父节点样式 @@ -132,6 +132,7 @@ export const GLOBLA_RULE = { transOpen: "false", // 开启翻译 bgColor: "", // 译文颜色 textDiyStyle: DEFAULT_DIY_STYLE, // 自定义译文样式 + termsStyle: "font-weight: bold;", // 专业术语样式 selectStyle: DEFAULT_SELECT_STYLE, // 选择器节点样式 parentStyle: DEFAULT_SELECT_STYLE, // 选择器父节点样式 grandStyle: DEFAULT_SELECT_STYLE, // 选择器祖节点样式 diff --git a/src/libs/rules.js b/src/libs/rules.js index 44b57fb..c085d90 100644 --- a/src/libs/rules.js +++ b/src/libs/rules.js @@ -52,6 +52,7 @@ export const matchRule = async (href, { injectRules, subrulesList }) => { "ignoreSelector", "terms", "aiTerms", + "termsStyle", "selectStyle", "parentStyle", "grandStyle", @@ -136,6 +137,7 @@ export const checkRules = (rules) => { ignoreSelector, terms, aiTerms, + termsStyle, selectStyle, parentStyle, grandStyle, @@ -170,6 +172,7 @@ export const checkRules = (rules) => { ignoreSelector: type(ignoreSelector) === "string" ? ignoreSelector : "", terms: type(terms) === "string" ? terms : "", aiTerms: type(aiTerms) === "string" ? aiTerms : "", + termsStyle: type(termsStyle) === "string" ? termsStyle : "", selectStyle: type(selectStyle) === "string" ? selectStyle : "", parentStyle: type(parentStyle) === "string" ? parentStyle : "", grandStyle: type(grandStyle) === "string" ? grandStyle : "", diff --git a/src/libs/translator.js b/src/libs/translator.js index 291858f..55a1a69 100644 --- a/src/libs/translator.js +++ b/src/libs/translator.js @@ -959,6 +959,7 @@ export class Translator { transStartHook, transEndHook, transOnly, + termsStyle, selectStyle, parentStyle, grandStyle, @@ -988,8 +989,10 @@ export class Translator { } try { - const [processedString, placeholderMap] = - this.#serializeForTranslation(nodes); + const [processedString, placeholderMap] = this.#serializeForTranslation( + nodes, + termsStyle + ); // console.log("processedString", processedString); if (this.#isInvalidText(processedString)) return; @@ -1078,7 +1081,7 @@ export class Translator { } // 处理节点转为翻译字符串 - #serializeForTranslation(nodes) { + #serializeForTranslation(nodes, termsStyle) { let replaceCounter = 0; // {{n}} let wrapCounter = 0; // const placeholderMap = new Map(); @@ -1118,7 +1121,7 @@ export class Translator { const termValue = this.#termValues[matchedIndex]; return pushReplace( - `${termValue || fullMatch}` + `${termValue || fullMatch}` ); }); } diff --git a/src/views/Options/Rules.js b/src/views/Options/Rules.js index cc7809e..a13406b 100644 --- a/src/views/Options/Rules.js +++ b/src/views/Options/Rules.js @@ -97,6 +97,7 @@ function RuleFields({ rule, rules, setShow, setKeyword }) { ignoreSelector = "", terms = "", aiTerms = "", + termsStyle = "", selectStyle = "", parentStyle = "", grandStyle = "", @@ -547,10 +548,19 @@ function RuleFields({ rule, rules, setShow, setKeyword }) { maxRows={10} /> +