Files
kiss-translator/src/views/Selection/index.js

169 lines
4.1 KiB
JavaScript
Raw Normal View History

2023-11-21 11:20:05 +08:00
import { useState, useEffect, useCallback } from "react";
2023-10-26 12:24:24 +08:00
import TranBtn from "./TranBtn";
import TranBox from "./TranBox";
2023-10-24 17:58:37 +08:00
import { shortcutRegister } from "../../libs/shortcut";
2023-11-11 17:22:43 +08:00
import { sleep } from "../../libs/utils";
import { isGm } from "../../libs/client";
2023-11-22 11:02:48 +08:00
import {
MSG_TRANSLATE_SELECTED,
MSG_OPEN_TRANBOX,
DEFAULT_TRANSEL_SHORTCUT,
} from "../../config";
2023-10-23 18:02:42 +08:00
2023-10-24 17:58:37 +08:00
export default function Slection({ tranboxSetting, transApis }) {
2023-10-23 18:02:42 +08:00
const [showBox, setShowBox] = useState(false);
const [showBtn, setShowBtn] = useState(false);
2023-10-24 17:58:37 +08:00
const [selectedText, setSelText] = useState("");
2023-10-23 18:02:42 +08:00
const [text, setText] = useState("");
const [position, setPosition] = useState({ x: 0, y: 0 });
2023-10-25 11:20:05 +08:00
const [boxSize, setBoxSize] = useState({ w: 600, h: 400 });
2023-10-24 17:58:37 +08:00
const [boxPosition, setBoxPosition] = useState({
x: (window.innerWidth - 600) / 2,
2023-10-25 11:20:05 +08:00
y: (window.innerHeight - 400) / 2,
2023-10-24 17:58:37 +08:00
});
2023-10-23 18:02:42 +08:00
2023-11-21 11:20:05 +08:00
const handleClick = (e) => {
e.stopPropagation();
setShowBtn(false);
setText(selectedText);
setShowBox(true);
};
const handleTranSelected = useCallback(() => {
setShowBtn(false);
2023-11-11 17:22:43 +08:00
2023-10-24 17:58:37 +08:00
const selectedText = window.getSelection()?.toString()?.trim() || "";
if (!selectedText) {
return;
}
setSelText(selectedText);
2023-11-21 11:20:05 +08:00
setText(selectedText);
setShowBox(true);
}, []);
2023-10-23 18:02:42 +08:00
2023-11-21 11:20:05 +08:00
useEffect(() => {
async function handleMouseup(e) {
await sleep(10);
2023-10-24 17:58:37 +08:00
2023-11-21 11:20:05 +08:00
const selectedText = window.getSelection()?.toString()?.trim() || "";
setSelText(selectedText);
if (!selectedText) {
setShowBtn(false);
return;
}
!tranboxSetting.hideTranBtn && setShowBtn(true);
setPosition({ x: e.clientX, y: e.clientY });
2023-10-23 18:02:42 +08:00
}
window.addEventListener("mouseup", handleMouseup);
return () => {
window.removeEventListener("mouseup", handleMouseup);
};
2023-11-21 11:20:05 +08:00
}, [tranboxSetting.hideTranBtn]);
2023-10-23 18:02:42 +08:00
2023-10-24 17:58:37 +08:00
useEffect(() => {
const clearShortcut = shortcutRegister(
tranboxSetting.tranboxShortcut,
() => {
setShowBox((pre) => !pre);
}
);
return () => {
clearShortcut();
};
2023-11-21 11:20:05 +08:00
}, [tranboxSetting.tranboxShortcut]);
useEffect(() => {
const clearShortcut = shortcutRegister(
tranboxSetting.transelShortcut || DEFAULT_TRANSEL_SHORTCUT,
handleTranSelected
);
return () => {
clearShortcut();
};
}, [tranboxSetting.transelShortcut, handleTranSelected]);
useEffect(() => {
window.addEventListener(MSG_TRANSLATE_SELECTED, handleTranSelected);
return () => {
window.removeEventListener(MSG_TRANSLATE_SELECTED, handleTranSelected);
};
}, [handleTranSelected]);
2023-10-24 17:58:37 +08:00
2023-11-22 11:02:48 +08:00
useEffect(() => {
const handleOpenTranbox = () => {
setShowBox((pre) => !pre);
};
window.addEventListener(MSG_OPEN_TRANBOX, handleOpenTranbox);
return () => {
window.removeEventListener(MSG_OPEN_TRANBOX, handleOpenTranbox);
};
}, []);
useEffect(() => {
if (!isGm) {
return;
}
// 注册菜单
try {
const menuCommandIds = [];
menuCommandIds.push(
GM.registerMenuCommand(
"Translate Selected Text (Alt+S)",
(event) => {
handleTranSelected();
},
"S"
),
GM.registerMenuCommand(
"Open Translate Popup (Alt+B)",
(event) => {
setShowBox((pre) => !pre);
},
"B"
)
);
return () => {
menuCommandIds.forEach((id) => {
GM.unregisterMenuCommand(id);
});
};
} catch (err) {
console.log("[registerMenuCommand]", err);
}
}, [handleTranSelected]);
2023-10-23 18:02:42 +08:00
return (
<>
{showBox && (
<TranBox
2023-10-24 17:58:37 +08:00
text={text}
setText={setText}
boxSize={boxSize}
setBoxSize={setBoxSize}
boxPosition={boxPosition}
setBoxPosition={setBoxPosition}
2023-10-23 18:02:42 +08:00
tranboxSetting={tranboxSetting}
2023-10-24 17:58:37 +08:00
transApis={transApis}
2023-10-23 18:02:42 +08:00
setShowBox={setShowBox}
/>
)}
{showBtn && (
<TranBtn
position={position}
tranboxSetting={tranboxSetting}
onClick={handleClick}
/>
)}
</>
);
}