From 261f29c1858b46d5aeefa64cc28cc8ae20885421 Mon Sep 17 00:00:00 2001 From: Gabe Date: Thu, 25 Sep 2025 00:21:51 +0800 Subject: [PATCH] fix: mousehover keys can be set blank --- src/config/i18n.js | 6 +++--- src/libs/shortcut.js | 2 +- src/libs/translator.js | 29 +++++++++++++++++++++-------- src/views/Options/ShortcutInput.js | 7 ++++--- 4 files changed, 29 insertions(+), 15 deletions(-) diff --git a/src/config/i18n.js b/src/config/i18n.js index 9c4ccba..749f883 100644 --- a/src/config/i18n.js +++ b/src/config/i18n.js @@ -1318,9 +1318,9 @@ export const I18N = { zh_TW: `劃詞翻譯的開啟和關閉請到「規則設定」裡面設定。`, }, mousehover_key_help: { - zh: `默认为“ControlLeft”`, - en: `Defaults is "ControlLeft"`, - zh_TW: `預設為“ControlLeft”`, + zh: `当快捷键置空时表示鼠标悬停直接翻译`, + en: `When the shortcut key is empty, it means that the mouse hovers to translate directly`, + zh_TW: `當快捷鍵置空時表示滑鼠懸停直接翻譯`, }, autoscan_alt: { zh: `自动扫描`, diff --git a/src/libs/shortcut.js b/src/libs/shortcut.js index a0432e4..b396d0f 100644 --- a/src/libs/shortcut.js +++ b/src/libs/shortcut.js @@ -48,7 +48,7 @@ export const shortcutRegister = (targetKeys = [], fn, target = document) => { const targetKeySet = new Set(targetKeys); const onKeyDown = (pressedKeys, event) => { - if (targetKeySet.size > 0 && isSameSet(targetKeySet, pressedKeys)) { + if (isSameSet(targetKeySet, pressedKeys)) { event.preventDefault(); event.stopPropagation(); fn(); diff --git a/src/libs/translator.js b/src/libs/translator.js index f32ee9e..0b44bbf 100644 --- a/src/libs/translator.js +++ b/src/libs/translator.js @@ -7,7 +7,7 @@ import { OPT_STYLE_FUZZY, GLOBLA_RULE, DEFAULT_SETTING, - DEFAULT_MOUSEHOVER_KEY, + // DEFAULT_MOUSEHOVER_KEY, OPT_STYLE_NONE, DEFAULT_API_SETTING, } from "../config"; @@ -511,7 +511,15 @@ export class Translator { targetNode = targetNode.parentElement; } this.#hoveredNode = foundNode || startNode; - }, 200); + + const { mouseHoverKey } = this.#setting.mouseHoverSetting; + if (mouseHoverKey.length === 0 && !this.#isInitialized) { + this.#init(); + } + if (mouseHoverKey.length === 0 && foundNode) { + this.#processNode(foundNode); + } + }, 100); } // 创建shadowroot的回调 @@ -535,7 +543,11 @@ export class Translator { let targetNode = this.#hoveredNode; if (!targetNode || !this.#observedNodes.has(targetNode)) return; - // 切换该节点翻译状态 + this.#toggleTargetNode(targetNode); + } + + // 切换节点翻译状态 + #toggleTargetNode(targetNode) { if (this.#processedNodes.has(targetNode)) { this.#cleanupDirectTranslations(targetNode); } else { @@ -598,7 +610,7 @@ export class Translator { this.#rescanQueue.forEach((t) => this.#rescanContainer(t)); this.#rescanQueue.clear(); this.#isQueueProcessing = false; - }, 200); + }, 100); } } @@ -700,8 +712,8 @@ export class Translator { // 处理一个待翻译的节点 async #processNode(node) { if ( - !Translator.isElementOrFragment(node) || - this.#processedNodes.has(node) + this.#processedNodes.has(node) || + !Translator.isElementOrFragment(node) ) { return; } @@ -1202,9 +1214,10 @@ export class Translator { this.#setting.mouseHoverSetting.useMouseHover = true; document.addEventListener("mousemove", this.#boundMouseMoveHandler); - let { mouseHoverKey } = this.#setting.mouseHoverSetting; + const { mouseHoverKey } = this.#setting.mouseHoverSetting; if (mouseHoverKey.length === 0) { - mouseHoverKey = DEFAULT_MOUSEHOVER_KEY; + // mouseHoverKey = DEFAULT_MOUSEHOVER_KEY; + return; } this.#removeKeydownHandler = shortcutRegister( mouseHoverKey, diff --git a/src/views/Options/ShortcutInput.js b/src/views/Options/ShortcutInput.js index 4d3ac26..56b2753 100644 --- a/src/views/Options/ShortcutInput.js +++ b/src/views/Options/ShortcutInput.js @@ -19,9 +19,10 @@ export default function ShortcutInput({ const i18n = useI18n(); const commitChanges = () => { - if (editingKeys.length > 0) { - onChange(editingKeys); - } + // if (editingKeys.length > 0) { + // onChange(editingKeys); + // } + onChange(editingKeys); setIsEditing(false); };