fix: mousehover keys can be set blank

This commit is contained in:
Gabe
2025-09-25 00:21:51 +08:00
parent 2a46939aa5
commit 261f29c185
4 changed files with 29 additions and 15 deletions

View File

@@ -1318,9 +1318,9 @@ export const I18N = {
zh_TW: `劃詞翻譯的開啟和關閉請到「規則設定」裡面設定。`, zh_TW: `劃詞翻譯的開啟和關閉請到「規則設定」裡面設定。`,
}, },
mousehover_key_help: { mousehover_key_help: {
zh: `默认为“ControlLeft”`, zh: `当快捷键置空时表示鼠标悬停直接翻译`,
en: `Defaults is "ControlLeft"`, en: `When the shortcut key is empty, it means that the mouse hovers to translate directly`,
zh_TW: `預設為“ControlLeft”`, zh_TW: `當快捷鍵置空時表示滑鼠懸停直接翻譯`,
}, },
autoscan_alt: { autoscan_alt: {
zh: `自动扫描`, zh: `自动扫描`,

View File

@@ -48,7 +48,7 @@ export const shortcutRegister = (targetKeys = [], fn, target = document) => {
const targetKeySet = new Set(targetKeys); const targetKeySet = new Set(targetKeys);
const onKeyDown = (pressedKeys, event) => { const onKeyDown = (pressedKeys, event) => {
if (targetKeySet.size > 0 && isSameSet(targetKeySet, pressedKeys)) { if (isSameSet(targetKeySet, pressedKeys)) {
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
fn(); fn();

View File

@@ -7,7 +7,7 @@ import {
OPT_STYLE_FUZZY, OPT_STYLE_FUZZY,
GLOBLA_RULE, GLOBLA_RULE,
DEFAULT_SETTING, DEFAULT_SETTING,
DEFAULT_MOUSEHOVER_KEY, // DEFAULT_MOUSEHOVER_KEY,
OPT_STYLE_NONE, OPT_STYLE_NONE,
DEFAULT_API_SETTING, DEFAULT_API_SETTING,
} from "../config"; } from "../config";
@@ -511,7 +511,15 @@ export class Translator {
targetNode = targetNode.parentElement; targetNode = targetNode.parentElement;
} }
this.#hoveredNode = foundNode || startNode; 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的回调 // 创建shadowroot的回调
@@ -535,7 +543,11 @@ export class Translator {
let targetNode = this.#hoveredNode; let targetNode = this.#hoveredNode;
if (!targetNode || !this.#observedNodes.has(targetNode)) return; if (!targetNode || !this.#observedNodes.has(targetNode)) return;
// 切换该节点翻译状态 this.#toggleTargetNode(targetNode);
}
// 切换节点翻译状态
#toggleTargetNode(targetNode) {
if (this.#processedNodes.has(targetNode)) { if (this.#processedNodes.has(targetNode)) {
this.#cleanupDirectTranslations(targetNode); this.#cleanupDirectTranslations(targetNode);
} else { } else {
@@ -598,7 +610,7 @@ export class Translator {
this.#rescanQueue.forEach((t) => this.#rescanContainer(t)); this.#rescanQueue.forEach((t) => this.#rescanContainer(t));
this.#rescanQueue.clear(); this.#rescanQueue.clear();
this.#isQueueProcessing = false; this.#isQueueProcessing = false;
}, 200); }, 100);
} }
} }
@@ -700,8 +712,8 @@ export class Translator {
// 处理一个待翻译的节点 // 处理一个待翻译的节点
async #processNode(node) { async #processNode(node) {
if ( if (
!Translator.isElementOrFragment(node) || this.#processedNodes.has(node) ||
this.#processedNodes.has(node) !Translator.isElementOrFragment(node)
) { ) {
return; return;
} }
@@ -1202,9 +1214,10 @@ export class Translator {
this.#setting.mouseHoverSetting.useMouseHover = true; this.#setting.mouseHoverSetting.useMouseHover = true;
document.addEventListener("mousemove", this.#boundMouseMoveHandler); document.addEventListener("mousemove", this.#boundMouseMoveHandler);
let { mouseHoverKey } = this.#setting.mouseHoverSetting; const { mouseHoverKey } = this.#setting.mouseHoverSetting;
if (mouseHoverKey.length === 0) { if (mouseHoverKey.length === 0) {
mouseHoverKey = DEFAULT_MOUSEHOVER_KEY; // mouseHoverKey = DEFAULT_MOUSEHOVER_KEY;
return;
} }
this.#removeKeydownHandler = shortcutRegister( this.#removeKeydownHandler = shortcutRegister(
mouseHoverKey, mouseHoverKey,

View File

@@ -19,9 +19,10 @@ export default function ShortcutInput({
const i18n = useI18n(); const i18n = useI18n();
const commitChanges = () => { const commitChanges = () => {
if (editingKeys.length > 0) { // if (editingKeys.length > 0) {
onChange(editingKeys); // onChange(editingKeys);
} // }
onChange(editingKeys);
setIsEditing(false); setIsEditing(false);
}; };