perf: merge Translate Popup/Selected shortcut

This commit is contained in:
Gabe Yuan
2023-11-28 13:36:40 +08:00
parent 52ca4306fd
commit 7d3972d3a8
8 changed files with 21 additions and 106 deletions

View File

@@ -15,9 +15,6 @@
"message": "Open Options (Alt+O)"
},
"open_tranbox": {
"message": "Open Translate Popup (Alt+B)"
},
"translate_selected": {
"message": "Translate Selected Text (Alt+S)"
"message": "Translate Popup/Selected (Alt+S)"
}
}

View File

@@ -15,9 +15,6 @@
"message": "打开设置 (Alt+O)"
},
"open_tranbox": {
"message": "打开翻译弹窗 (Alt+B)"
},
"translate_selected": {
"message": "翻译选中文字 (Alt+S)"
"message": "翻译弹窗/选中文字 (Alt+S)"
}
}

View File

@@ -7,13 +7,11 @@ import {
MSG_OPEN_OPTIONS,
MSG_SAVE_RULE,
MSG_TRANS_TOGGLE_STYLE,
MSG_TRANSLATE_SELECTED,
MSG_OPEN_TRANBOX,
CMD_TOGGLE_TRANSLATE,
CMD_TOGGLE_STYLE,
CMD_OPEN_OPTIONS,
CMD_OPEN_TRANBOX,
CMD_TRANSLATE_SELECTED,
} from "./config";
import { getSettingWithDefault, tryInitDefaultData } from "./libs/storage";
import { trySyncSettingAndRules } from "./libs/sync";
@@ -32,16 +30,6 @@ browser.runtime.onInstalled.addListener(() => {
tryInitDefaultData();
// 右键菜单
browser.contextMenus.create({
id: CMD_TRANSLATE_SELECTED,
title: browser.i18n.getMessage("translate_selected"),
contexts: ["selection"],
});
browser.contextMenus.create({
id: "selection_separator",
type: "separator",
contexts: ["selection"],
});
browser.contextMenus.create({
id: CMD_TOGGLE_TRANSLATE,
title: browser.i18n.getMessage("toggle_translate"),
@@ -53,13 +41,13 @@ browser.runtime.onInstalled.addListener(() => {
contexts: ["all"],
});
browser.contextMenus.create({
id: "options_separator",
type: "separator",
id: CMD_OPEN_TRANBOX,
title: browser.i18n.getMessage("open_tranbox"),
contexts: ["all"],
});
browser.contextMenus.create({
id: CMD_OPEN_TRANBOX,
title: browser.i18n.getMessage("open_tranbox"),
id: "options_separator",
type: "separator",
contexts: ["all"],
});
browser.contextMenus.create({
@@ -151,9 +139,6 @@ browser.contextMenus.onClicked.addListener(({ menuItemId }) => {
case CMD_TOGGLE_STYLE:
sendTabMsg(MSG_TRANS_TOGGLE_STYLE);
break;
case CMD_TRANSLATE_SELECTED:
sendTabMsg(MSG_TRANSLATE_SELECTED);
break;
case CMD_OPEN_TRANBOX:
sendTabMsg(MSG_OPEN_TRANBOX);
break;

View File

@@ -8,7 +8,6 @@ import {
MSG_TRANS_TOGGLE_STYLE,
MSG_TRANS_GETRULE,
MSG_TRANS_PUTRULE,
MSG_TRANSLATE_SELECTED,
MSG_OPEN_TRANBOX,
APP_LCNAME,
DEFAULT_TRANBOX_SETTING,
@@ -67,9 +66,6 @@ function runtimeListener(translator) {
translator.updateRule(args);
sendIframeMsg(MSG_TRANS_PUTRULE, args);
break;
case MSG_TRANSLATE_SELECTED:
window.dispatchEvent(new CustomEvent(MSG_TRANSLATE_SELECTED));
break;
case MSG_OPEN_TRANBOX:
window.dispatchEvent(new CustomEvent(MSG_OPEN_TRANBOX));
break;

View File

@@ -636,12 +636,8 @@ export const I18N = {
en: `Use Selection Translate`,
},
trigger_tranbox_shortcut: {
zh: `显示翻译框快捷键`,
en: `Toggle Translate Box Shortcut`,
},
trigger_transel_shortcut: {
zh: `翻译选中文字快捷键`,
en: `Translate Selected Shortcut`,
zh: `显示翻译框/翻译选中文字快捷键`,
en: `Open Translate Popup/Translate Selected Shortcut`,
},
tranbtn_offset_x: {
zh: `翻译按钮偏移X0-100`,

View File

@@ -34,7 +34,6 @@ export const CMD_TOGGLE_TRANSLATE = "toggleTranslate";
export const CMD_TOGGLE_STYLE = "toggleStyle";
export const CMD_OPEN_OPTIONS = "openOptions";
export const CMD_OPEN_TRANBOX = "openTranbox";
export const CMD_TRANSLATE_SELECTED = "translateSelected";
export const CLIENT_WEB = "web";
export const CLIENT_CHROME = "chrome";
@@ -60,7 +59,6 @@ export const MSG_OPEN_OPTIONS = "open_options";
export const MSG_SAVE_RULE = "save_rule";
export const MSG_TRANS_TOGGLE = "trans_toggle";
export const MSG_TRANS_TOGGLE_STYLE = "trans_toggle_style";
export const MSG_TRANSLATE_SELECTED = "translate_selected";
export const MSG_OPEN_TRANBOX = "open_tranbox";
export const MSG_TRANS_GETRULE = "trans_getrule";
export const MSG_TRANS_PUTRULE = "trans_putrule";
@@ -340,15 +338,13 @@ export const DEFAULT_INPUT_RULE = {
};
// 划词翻译
export const DEFAULT_TRANBOX_SHORTCUT = ["AltLeft", "KeyB"];
export const DEFAULT_TRANSEL_SHORTCUT = ["AltLeft", "KeyS"];
export const DEFAULT_TRANBOX_SHORTCUT = ["AltLeft", "KeyS"];
export const DEFAULT_TRANBOX_SETTING = {
transOpen: true,
translator: OPT_TRANS_MICROSOFT,
fromLang: "auto",
toLang: "zh-CN",
tranboxShortcut: DEFAULT_TRANBOX_SHORTCUT,
transelShortcut: DEFAULT_TRANSEL_SHORTCUT,
btnOffsetX: 10,
btnOffsetY: 10,
hideTranBtn: false,

View File

@@ -3,12 +3,7 @@ import Stack from "@mui/material/Stack";
import TextField from "@mui/material/TextField";
import MenuItem from "@mui/material/MenuItem";
import { useI18n } from "../../hooks/I18n";
import {
OPT_TRANS_ALL,
OPT_LANGS_FROM,
OPT_LANGS_TO,
DEFAULT_TRANSEL_SHORTCUT,
} from "../../config";
import { OPT_TRANS_ALL, OPT_LANGS_FROM, OPT_LANGS_TO } from "../../config";
import ShortcutInput from "./ShortcutInput";
import FormControlLabel from "@mui/material/FormControlLabel";
import Switch from "@mui/material/Switch";
@@ -44,20 +39,12 @@ export default function Tranbox() {
[updateTranbox]
);
const handleShortcutTransel = useCallback(
(val) => {
updateTranbox({ transelShortcut: val });
},
[updateTranbox]
);
const {
transOpen,
translator,
fromLang,
toLang,
tranboxShortcut,
transelShortcut = DEFAULT_TRANSEL_SHORTCUT,
btnOffsetX,
btnOffsetY,
hideTranBtn = false,
@@ -160,12 +147,6 @@ export default function Tranbox() {
onChange={handleShortcutInput}
label={i18n("trigger_tranbox_shortcut")}
/>
<ShortcutInput
value={transelShortcut}
onChange={handleShortcutTransel}
label={i18n("trigger_transel_shortcut")}
/>
</Stack>
</Box>
);

View File

@@ -4,11 +4,7 @@ import TranBox from "./TranBox";
import { shortcutRegister } from "../../libs/shortcut";
import { sleep } from "../../libs/utils";
import { isGm } from "../../libs/client";
import {
MSG_TRANSLATE_SELECTED,
MSG_OPEN_TRANBOX,
DEFAULT_TRANSEL_SHORTCUT,
} from "../../config";
import { MSG_OPEN_TRANBOX, DEFAULT_TRANBOX_SHORTCUT } from "../../config";
export default function Slection({ tranboxSetting, transApis }) {
const [showBox, setShowBox] = useState(false);
@@ -30,11 +26,12 @@ export default function Slection({ tranboxSetting, transApis }) {
setShowBox(true);
};
const handleTranSelected = useCallback(() => {
const handleTranbox = useCallback(() => {
setShowBtn(false);
const selectedText = window.getSelection()?.toString()?.trim() || "";
if (!selectedText) {
setShowBox((pre) => !pre);
return;
}
@@ -66,44 +63,21 @@ export default function Slection({ tranboxSetting, transApis }) {
useEffect(() => {
const clearShortcut = shortcutRegister(
tranboxSetting.tranboxShortcut,
() => {
setShowBox((pre) => !pre);
}
tranboxSetting.tranboxShortcut || DEFAULT_TRANBOX_SHORTCUT,
handleTranbox
);
return () => {
clearShortcut();
};
}, [tranboxSetting.tranboxShortcut]);
}, [tranboxSetting.tranboxShortcut, handleTranbox]);
useEffect(() => {
const clearShortcut = shortcutRegister(
tranboxSetting.transelShortcut || DEFAULT_TRANSEL_SHORTCUT,
handleTranSelected
);
window.addEventListener(MSG_OPEN_TRANBOX, handleTranbox);
return () => {
clearShortcut();
window.removeEventListener(MSG_OPEN_TRANBOX, handleTranbox);
};
}, [tranboxSetting.transelShortcut, handleTranSelected]);
useEffect(() => {
window.addEventListener(MSG_TRANSLATE_SELECTED, handleTranSelected);
return () => {
window.removeEventListener(MSG_TRANSLATE_SELECTED, handleTranSelected);
};
}, [handleTranSelected]);
useEffect(() => {
const handleOpenTranbox = () => {
setShowBox((pre) => !pre);
};
window.addEventListener(MSG_OPEN_TRANBOX, handleOpenTranbox);
return () => {
window.removeEventListener(MSG_OPEN_TRANBOX, handleOpenTranbox);
};
}, []);
}, [handleTranbox]);
useEffect(() => {
if (!isGm) {
@@ -117,16 +91,9 @@ export default function Slection({ tranboxSetting, transApis }) {
GM.registerMenuCommand(
"Translate Selected Text (Alt+S)",
(event) => {
handleTranSelected();
handleTranbox();
},
"S"
),
GM.registerMenuCommand(
"Open Translate Popup (Alt+B)",
(event) => {
setShowBox((pre) => !pre);
},
"B"
)
);
@@ -138,7 +105,7 @@ export default function Slection({ tranboxSetting, transApis }) {
} catch (err) {
console.log("[registerMenuCommand]", err);
}
}, [handleTranSelected]);
}, [handleTranbox]);
return (
<>