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

309 lines
9.3 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 TextField from "@mui/material/TextField";
import MenuItem from "@mui/material/MenuItem";
import Grid from "@mui/material/Grid";
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";
import DoneIcon from "@mui/icons-material/Done";
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";
2023-10-23 18:02:42 +08:00
import { useI18n } from "../../hooks/I18n";
2023-10-24 23:37:53 +08:00
import { OPT_TRANS_ALL, OPT_LANGS_FROM, OPT_LANGS_TO } from "../../config";
import { useState, useRef } from "react";
import TranCont from "./TranCont";
2024-04-15 18:04:35 +08:00
import DictCont from "./DictCont";
import SugCont from "./SugCont";
2023-10-26 12:24:24 +08:00
import CopyBtn from "./CopyBtn";
2024-04-16 00:54:37 +08:00
import { isValidWord } from "../../libs/utils";
import { isMobile } from "../../libs/mobile";
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
}) {
if (!isMobile && simpleStyle && !mouseHover) {
return;
}
2024-04-16 00:54:37 +08:00
return (
<Box
className="KT-transbox-header"
onMouseUp={(e) => e.stopPropagation()}
onTouchEnd={(e) => e.stopPropagation()}
>
2024-04-16 15:22:27 +08:00
<Stack direction="row" justifyContent="space-between" alignItems="center">
<DragIndicatorIcon fontSize="small" />
<Stack direction="row" alignItems="center">
<IconButton
size="small"
onClick={() => {
setHideClickAway((pre) => !pre);
}}
>
{hideClickAway ? (
2024-04-20 18:07:16 +08:00
<LockOpenIcon fontSize="small" />
) : (
<LockIcon fontSize="small" />
)}
</IconButton>
<IconButton
size="small"
onClick={() => {
setFollowSelection((pre) => !pre);
}}
>
{followSelection ? (
2024-04-16 15:22:27 +08:00
<PushPinOutlinedIcon fontSize="small" />
) : (
<PushPinIcon fontSize="small" />
)}
</IconButton>
<IconButton
size="small"
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
);
}
function TranForm({ text, setText, tranboxSetting, transApis, simpleStyle }) {
2023-10-23 18:02:42 +08:00
const i18n = useI18n();
2023-10-24 17:58:37 +08:00
const [editMode, setEditMode] = useState(false);
const [editText, setEditText] = useState("");
const [translator, setTranslator] = useState(tranboxSetting.translator);
const [fromLang, setFromLang] = useState(tranboxSetting.fromLang);
const [toLang, setToLang] = useState(tranboxSetting.toLang);
const inputRef = useRef(null);
2023-10-23 18:02:42 +08:00
return (
2024-04-17 15:35:44 +08:00
<Stack
className="KT-transbox-container"
sx={{ p: simpleStyle ? 1 : 2 }}
spacing={simpleStyle ? 1 : 2}
>
2024-04-16 00:54:37 +08:00
{!simpleStyle && (
<>
2024-04-17 15:35:44 +08:00
<Box className="KT-transbox-select">
2024-04-16 00:54:37 +08:00
<Grid container spacing={simpleStyle ? 1 : 2} columns={12}>
<Grid item xs={4} sm={4} md={4} lg={4}>
<TextField
select
SelectProps={{ MenuProps: { disablePortal: true } }}
fullWidth
size="small"
name="fromLang"
value={fromLang}
label={i18n("from_lang")}
onChange={(e) => {
setFromLang(e.target.value);
}}
>
{OPT_LANGS_FROM.map(([lang, name]) => (
<MenuItem key={lang} value={lang}>
{name}
</MenuItem>
))}
</TextField>
</Grid>
<Grid item xs={4} sm={4} md={4} lg={4}>
<TextField
select
SelectProps={{ MenuProps: { disablePortal: true } }}
fullWidth
size="small"
name="toLang"
value={toLang}
label={i18n("to_lang")}
onChange={(e) => {
setToLang(e.target.value);
}}
>
{OPT_LANGS_TO.map(([lang, name]) => (
<MenuItem key={lang} value={lang}>
{name}
</MenuItem>
))}
</TextField>
</Grid>
<Grid item xs={4} sm={4} md={4} lg={4}>
<TextField
select
SelectProps={{ MenuProps: { disablePortal: true } }}
fullWidth
size="small"
value={translator}
name="translator"
label={i18n("translate_service")}
onChange={(e) => {
setTranslator(e.target.value);
}}
>
{OPT_TRANS_ALL.map((item) => (
<MenuItem key={item} value={item}>
{item}
</MenuItem>
))}
</TextField>
</Grid>
</Grid>
</Box>
2024-04-17 15:35:44 +08:00
<Box className="KT-transbox-origin">
2023-10-23 18:02:42 +08:00
<TextField
size="small"
2024-04-16 00:54:37 +08:00
label={i18n("original_text")}
inputRef={inputRef}
2023-10-23 18:02:42 +08:00
fullWidth
2024-04-16 00:54:37 +08:00
multiline
value={editMode ? editText : text}
2023-10-24 17:58:37 +08:00
onChange={(e) => {
2024-04-16 00:54:37 +08:00
setEditText(e.target.value);
2023-10-24 17:58:37 +08:00
}}
2024-04-16 00:54:37 +08:00
onFocus={() => {
setEditMode(true);
setEditText(text);
2023-10-24 17:58:37 +08:00
}}
2024-04-16 00:54:37 +08:00
onBlur={() => {
setEditMode(false);
setText(editText.trim());
}}
InputProps={{
endAdornment: (
<Stack
direction="row"
sx={{
position: "absolute",
right: 0,
top: 0,
2023-10-26 12:24:24 +08:00
}}
>
2024-04-16 00:54:37 +08:00
{editMode ? (
<IconButton
size="small"
onClick={(e) => {
e.stopPropagation();
}}
>
<DoneIcon fontSize="inherit" />
</IconButton>
) : (
<CopyBtn text={text} />
)}
</Stack>
),
}}
/>
</Box>
</>
)}
2024-04-17 10:31:37 +08:00
{(!simpleStyle || !isValidWord(text) || !toLang.startsWith("zh")) && (
2024-04-16 00:54:37 +08:00
<TranCont
text={text}
translator={translator}
fromLang={fromLang}
toLang={toLang}
toLang2={tranboxSetting.toLang2}
transApis={transApis}
simpleStyle={simpleStyle}
2023-10-24 17:58:37 +08:00
/>
2024-04-16 00:54:37 +08:00
)}
2023-10-24 17:58:37 +08:00
2024-04-15 18:04:35 +08:00
<DictCont text={text} />
<SugCont text={text} />
2023-10-23 18:02:42 +08:00
</Stack>
);
}
2023-10-24 17:58:37 +08:00
export default function TranBox({
text,
setText,
setShowBox,
tranboxSetting,
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,
2024-04-17 15:35:44 +08:00
extStyles,
2023-10-24 17:58:37 +08:00
}) {
const [mouseHover, setMouseHover] = useState(false);
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
>
2023-10-24 17:58:37 +08:00
<TranForm
text={text}
setText={setText}
tranboxSetting={tranboxSetting}
transApis={transApis}
2024-04-16 00:54:37 +08:00
simpleStyle={simpleStyle}
2023-10-24 17:58:37 +08:00
/>
2023-10-23 18:02:42 +08:00
</DraggableResizable>
</ThemeProvider>
</SettingProvider>
);
}