From a13493ebc2b9c642a7525c7c24fbf9803c21bae8 Mon Sep 17 00:00:00 2001 From: Gabe Yuan Date: Tue, 16 Apr 2024 00:54:37 +0800 Subject: [PATCH] feat: simple style tranbox --- src/config/index.js | 3 +- src/views/Selection/TranBox.js | 308 +++++++++++++++++++------------- src/views/Selection/TranBtn.js | 3 +- src/views/Selection/TranCont.js | 10 ++ src/views/Selection/index.js | 26 ++- 5 files changed, 222 insertions(+), 128 deletions(-) diff --git a/src/config/index.js b/src/config/index.js index 5365773..8d67290 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -418,7 +418,8 @@ export const DEFAULT_TRANBOX_SETTING = { btnOffsetX: 10, btnOffsetY: 10, hideTranBtn: false, // 是否隐藏翻译按钮 - hideClickAway: false, // 是否点击外部关闭弹窗 + hideClickAway: false, // 默认是否点击外部关闭弹窗 + simpleStyle: false, // 默认是否简洁界面 }; // 订阅列表 diff --git a/src/views/Selection/TranBox.js b/src/views/Selection/TranBox.js index 93b577d..3b24789 100644 --- a/src/views/Selection/TranBox.js +++ b/src/views/Selection/TranBox.js @@ -1,7 +1,6 @@ import { SettingProvider } from "../../hooks/Setting"; import ThemeProvider from "../../hooks/Theme"; import DraggableResizable from "./DraggableResizable"; -import Header from "../Popup/Header"; import Stack from "@mui/material/Stack"; import TextField from "@mui/material/TextField"; import MenuItem from "@mui/material/MenuItem"; @@ -10,6 +9,12 @@ import Box from "@mui/material/Box"; import Divider from "@mui/material/Divider"; import IconButton from "@mui/material/IconButton"; import DoneIcon from "@mui/icons-material/Done"; +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"; +import CloseIcon from "@mui/icons-material/Close"; import { useI18n } from "../../hooks/I18n"; import { OPT_TRANS_ALL, OPT_LANGS_FROM, OPT_LANGS_TO } from "../../config"; import { useState, useRef } from "react"; @@ -17,8 +22,50 @@ import TranCont from "./TranCont"; import DictCont from "./DictCont"; import SugCont from "./SugCont"; import CopyBtn from "./CopyBtn"; +import { isValidWord } from "../../libs/utils"; -function TranForm({ text, setText, tranboxSetting, transApis }) { +function Header({ + setShowPopup, + simpleStyle, + setSimpleStyle, + hideClickAway, + setHideClickAway, +}) { + return ( + + + + + { + setHideClickAway((pre) => !pre); + }} + > + {hideClickAway ? : } + + { + setSimpleStyle((pre) => !pre); + }} + > + {simpleStyle ? : } + + { + setShowPopup(false); + }} + > + + + + + ); +} + +function TranForm({ text, setText, tranboxSetting, transApis, simpleStyle }) { const i18n = useI18n(); const [editMode, setEditMode] = useState(false); @@ -29,127 +76,135 @@ function TranForm({ text, setText, tranboxSetting, transApis }) { const inputRef = useRef(null); return ( - - - - - { - setFromLang(e.target.value); - }} - > - {OPT_LANGS_FROM.map(([lang, name]) => ( - - {name} - - ))} - - - - { - setToLang(e.target.value); - }} - > - {OPT_LANGS_TO.map(([lang, name]) => ( - - {name} - - ))} - - - - { - setTranslator(e.target.value); - }} - > - {OPT_TRANS_ALL.map((item) => ( - - {item} - - ))} - - - - + + {!simpleStyle && ( + <> + + + + { + setFromLang(e.target.value); + }} + > + {OPT_LANGS_FROM.map(([lang, name]) => ( + + {name} + + ))} + + + + { + setToLang(e.target.value); + }} + > + {OPT_LANGS_TO.map(([lang, name]) => ( + + {name} + + ))} + + + + { + setTranslator(e.target.value); + }} + > + {OPT_TRANS_ALL.map((item) => ( + + {item} + + ))} + + + + - - { - setEditText(e.target.value); - }} - onFocus={() => { - setEditMode(true); - setEditText(text); - }} - onBlur={() => { - setEditMode(false); - setText(editText.trim()); - }} - InputProps={{ - endAdornment: ( - - {editMode ? ( - { - e.stopPropagation(); + + { + setEditText(e.target.value); + }} + onFocus={() => { + setEditMode(true); + setEditText(text); + }} + onBlur={() => { + setEditMode(false); + setText(editText.trim()); + }} + InputProps={{ + endAdornment: ( + - - - ) : ( - - )} - - ), - }} - /> - + {editMode ? ( + { + e.stopPropagation(); + }} + > + + + ) : ( + + )} + + ), + }} + /> + + + )} + + {(!simpleStyle || !isValidWord(text)) && ( + + )} - @@ -166,6 +221,10 @@ export default function TranBox({ setBoxSize, boxPosition, setBoxPosition, + simpleStyle, + setSimpleStyle, + hideClickAway, + setHideClickAway, }) { return ( @@ -173,7 +232,15 @@ export default function TranBox({ } + header={ +
+ } onChangeSize={setBoxSize} onChangePosition={setBoxPosition} onClick={(e) => e.stopPropagation()} @@ -184,6 +251,7 @@ export default function TranBox({ setText={setText} tranboxSetting={tranboxSetting} transApis={transApis} + simpleStyle={simpleStyle} /> diff --git a/src/views/Selection/TranBtn.js b/src/views/Selection/TranBtn.js index bdd7603..418f04c 100644 --- a/src/views/Selection/TranBtn.js +++ b/src/views/Selection/TranBtn.js @@ -25,7 +25,8 @@ export default function TranBtn({ onClick, position, tranboxSetting }) {
+ {trText} + + ); + } + return ( { e.stopPropagation(); @@ -64,10 +74,10 @@ export default function Slection({ return; } - const { pageX, pageY } = isMobile ? e.changedTouches[0] : e; + const { clientX, clientY } = isMobile ? e.changedTouches[0] : e; !tranboxSetting.hideTranBtn && setShowBtn(true); // setPosition({ x: e.clientX, y: e.clientY }); - setPosition({ x: pageX, y: pageY }); + setPosition({ x: clientX, y: clientY }); } // todo: mobile support @@ -133,7 +143,7 @@ export default function Slection({ }, [handleTranbox, contextMenuType]); useEffect(() => { - if (tranboxSetting.hideClickAway) { + if (hideClickAway) { const handleHideBox = () => { setShowBox(false); }; @@ -142,7 +152,7 @@ export default function Slection({ window.removeEventListener("click", handleHideBox); }; } - }, [tranboxSetting.hideClickAway]); + }, [hideClickAway]); return ( <> @@ -157,6 +167,10 @@ export default function Slection({ tranboxSetting={tranboxSetting} transApis={transApis} setShowBox={setShowBox} + simpleStyle={simpleStyle} + setSimpleStyle={setSimpleStyle} + hideClickAway={hideClickAway} + setHideClickAway={setHideClickAway} /> )}