refactor: add TranslatorManager

This commit is contained in:
Gabe
2025-10-21 02:07:33 +08:00
parent ed279cf8a1
commit 53e32d3031
17 changed files with 566 additions and 405 deletions

View File

@@ -0,0 +1,73 @@
import Fab from "@mui/material/Fab";
import TranslateIcon from "@mui/icons-material/Translate";
import ThemeProvider from "../../hooks/Theme";
import Draggable from "./Draggable";
import { useState, useMemo, useCallback } from "react";
import { SettingProvider } from "../../hooks/Setting";
import { MSG_TRANS_TOGGLE } from "../../config";
import { sendIframeMsg } from "../../libs/iframe";
import useWindowSize from "../../hooks/WindowSize";
export default function ContentFab({
translator,
fabConfig: { x: fabX, y: fabY, fabClickAction = 0 } = {},
popupManager,
}) {
const fabWidth = 40;
const windowSize = useWindowSize();
const [moved, setMoved] = useState(false);
const handleStart = useCallback(() => {
setMoved(false);
}, []);
const handleMove = useCallback(() => {
setMoved(true);
}, []);
const handleClick = useCallback(() => {
if (!moved) {
if (fabClickAction === 1) {
translator.toggle();
sendIframeMsg(MSG_TRANS_TOGGLE);
} else {
popupManager.toggle();
}
}
}, [moved, translator, popupManager, fabClickAction]);
const fabProps = useMemo(
() => ({
windowSize,
width: fabWidth,
height: fabWidth,
left: fabX ?? -fabWidth,
top: fabY ?? windowSize.h / 2,
}),
[windowSize, fabWidth, fabX, fabY]
);
return (
<SettingProvider>
<ThemeProvider>
<Draggable
key="fab"
snapEdge
{...fabProps}
onStart={handleStart}
onMove={handleMove}
handler={
<Fab size="small" color="primary" onClick={handleClick}>
<TranslateIcon
sx={{
width: 24,
height: 24,
}}
/>
</Fab>
}
/>
</ThemeProvider>
</SettingProvider>
);
}