Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

设置划词翻译默认选中所有文本 并解决过程中发现的bug #768

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/window/Translate/components/SourceArea/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export default function SourceArea(props) {
const { t } = useTranslation();
const textAreaRef = useRef();
const speak = useVoice();
// 是否是首次打开页面 首次打开页面时全选内容 这样可以将'划词翻译'和'输入翻译'合并为一个功能
const [first, setFirst] = useState(false);

const handleNewText = async (text) => {
text = text.trim();
Expand Down Expand Up @@ -141,7 +143,8 @@ export default function SourceArea(props) {
}
if (incrementalTranslate) {
setSourceText((old) => {
return old + ' ' + newText;
//增量翻译的时候 如果原来没有内容 则直接设置内容 避免文章开头出现空格
return old == '' ? newText : old + ' ' + newText;
});
} else {
setSourceText(newText);
Expand All @@ -150,6 +153,7 @@ export default function SourceArea(props) {
syncSourceText();
});
}
setFirst(true);
};

const keyDown = (event) => {
Expand Down Expand Up @@ -238,6 +242,9 @@ export default function SourceArea(props) {
textAreaRef.current.style.height = '50px';
textAreaRef.current.style.height = textAreaRef.current.scrollHeight + 'px';
}, [sourceText]);
useEffect(() => {
textAreaRef.current.select();
}, [first]);

const detect_language = async (text) => {
setDetectLanguage(await detect(text));
Expand Down