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

178 lines
5.1 KiB
JavaScript
Raw Normal View History

2023-10-23 18:02:42 +08:00
import { SettingProvider } from "../../hooks/Setting";
import ThemeProvider from "../../hooks/Theme";
import DraggableResizable from "./DraggableResizable";
import Stack from "@mui/material/Stack";
import Box from "@mui/material/Box";
import Divider from "@mui/material/Divider";
2023-10-26 12:24:24 +08:00
import IconButton from "@mui/material/IconButton";
2024-04-16 00:54:37 +08:00
import DragIndicatorIcon from "@mui/icons-material/DragIndicator";
import UnfoldLessIcon from "@mui/icons-material/UnfoldLess";
import UnfoldMoreIcon from "@mui/icons-material/UnfoldMore";
import PushPinIcon from "@mui/icons-material/PushPin";
import PushPinOutlinedIcon from "@mui/icons-material/PushPinOutlined";
2024-04-20 18:07:16 +08:00
import LockIcon from "@mui/icons-material/Lock";
import LockOpenIcon from "@mui/icons-material/LockOpen";
2024-04-16 00:54:37 +08:00
import CloseIcon from "@mui/icons-material/Close";
2025-09-26 21:13:04 +08:00
import Typography from "@mui/material/Typography";
2023-10-23 18:02:42 +08:00
import { useI18n } from "../../hooks/I18n";
2025-10-02 21:59:31 +08:00
import { useState } from "react";
import { isMobile } from "../../libs/mobile";
2025-10-02 21:59:31 +08:00
import TranForm from "./TranForm.js";
2023-10-24 17:58:37 +08:00
2024-04-16 00:54:37 +08:00
function Header({
setShowPopup,
simpleStyle,
setSimpleStyle,
hideClickAway,
setHideClickAway,
2024-04-20 18:07:16 +08:00
followSelection,
setFollowSelection,
mouseHover,
2024-04-16 00:54:37 +08:00
}) {
2025-10-01 12:08:10 +08:00
const i18n = useI18n();
if (!isMobile && simpleStyle && !mouseHover) {
return;
}
2024-04-16 00:54:37 +08:00
return (
<Box
onMouseUp={(e) => e.stopPropagation()}
onTouchEnd={(e) => e.stopPropagation()}
>
2024-04-16 15:22:27 +08:00
<Stack direction="row" justifyContent="space-between" alignItems="center">
2025-09-26 21:13:04 +08:00
<Stack direction="row" alignItems="center">
<DragIndicatorIcon fontSize="small" />
<Typography
variant="body2"
sx={{
userSelect: "none",
WebkitUserSelect: "none",
fontWeight: "bold",
}}
>
{`${process.env.REACT_APP_NAME} v${process.env.REACT_APP_VERSION}`}
</Typography>
</Stack>
2024-04-16 15:22:27 +08:00
<Stack direction="row" alignItems="center">
<IconButton
size="small"
2025-10-01 12:08:10 +08:00
title={i18n("btn_tip_click_away")}
2024-04-16 15:22:27 +08:00
onClick={() => {
setHideClickAway((pre) => !pre);
}}
>
{hideClickAway ? (
2024-04-20 18:07:16 +08:00
<LockOpenIcon fontSize="small" />
) : (
<LockIcon fontSize="small" />
)}
</IconButton>
<IconButton
size="small"
2025-10-01 12:08:10 +08:00
title={i18n("btn_tip_follow_selection")}
2024-04-20 18:07:16 +08:00
onClick={() => {
setFollowSelection((pre) => !pre);
}}
>
{followSelection ? (
2024-04-16 15:22:27 +08:00
<PushPinOutlinedIcon fontSize="small" />
) : (
<PushPinIcon fontSize="small" />
)}
</IconButton>
<IconButton
size="small"
2025-10-01 12:08:10 +08:00
title={i18n("btn_tip_simple_style")}
2024-04-16 15:22:27 +08:00
onClick={() => {
setSimpleStyle((pre) => !pre);
}}
>
{simpleStyle ? (
<UnfoldMoreIcon fontSize="small" />
) : (
<UnfoldLessIcon fontSize="small" />
)}
</IconButton>
<IconButton
size="small"
onClick={() => {
setShowPopup(false);
}}
>
<CloseIcon fontSize="small" />
</IconButton>
</Stack>
2024-04-16 00:54:37 +08:00
</Stack>
2024-04-16 15:22:27 +08:00
<Divider />
</Box>
2024-04-16 00:54:37 +08:00
);
}
2023-10-24 17:58:37 +08:00
export default function TranBox({
text,
setText,
setShowBox,
2025-10-03 18:28:50 +08:00
tranboxSetting: { enDict, enSug, apiSlugs, fromLang, toLang, toLang2 },
2023-10-24 17:58:37 +08:00
transApis,
boxSize,
setBoxSize,
boxPosition,
setBoxPosition,
2024-04-16 00:54:37 +08:00
simpleStyle,
setSimpleStyle,
hideClickAway,
setHideClickAway,
2024-04-20 18:07:16 +08:00
followSelection,
setFollowSelection,
2025-10-02 21:59:31 +08:00
extStyles = "",
2024-05-22 23:33:30 +08:00
langDetector,
2023-10-24 17:58:37 +08:00
}) {
const [mouseHover, setMouseHover] = useState(false);
// todo: 这里的 SettingProvider 不应和 background 的共用
2023-10-23 18:02:42 +08:00
return (
<SettingProvider>
2024-04-17 15:35:44 +08:00
<ThemeProvider styles={extStyles}>
2023-10-23 18:02:42 +08:00
<DraggableResizable
2024-04-20 18:07:16 +08:00
position={boxPosition}
size={boxSize}
setSize={setBoxSize}
setPosition={setBoxPosition}
2024-04-16 00:54:37 +08:00
header={
<Header
setShowPopup={setShowBox}
simpleStyle={simpleStyle}
setSimpleStyle={setSimpleStyle}
hideClickAway={hideClickAway}
setHideClickAway={setHideClickAway}
2024-04-20 18:07:16 +08:00
followSelection={followSelection}
setFollowSelection={setFollowSelection}
mouseHover={mouseHover}
2024-04-16 00:54:37 +08:00
/>
}
2024-04-01 12:25:59 +08:00
onClick={(e) => e.stopPropagation()}
onMouseEnter={() => setMouseHover(true)}
onMouseLeave={() => setMouseHover(false)}
2023-10-23 18:02:42 +08:00
>
2025-10-02 21:59:31 +08:00
<Box sx={{ p: simpleStyle ? 1 : 2 }}>
<TranForm
text={text}
setText={setText}
apiSlugs={apiSlugs}
fromLang={fromLang}
toLang={toLang}
toLang2={toLang2}
transApis={transApis}
simpleStyle={simpleStyle}
langDetector={langDetector}
enDict={enDict}
2025-10-03 18:28:50 +08:00
enSug={enSug}
2025-10-02 21:59:31 +08:00
/>
</Box>
2023-10-23 18:02:42 +08:00
</DraggableResizable>
</ThemeProvider>
</SettingProvider>
);
}