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

feat: support default collapse #745

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
19 changes: 18 additions & 1 deletion src/window/Config/pages/Service/Translate/ServiceItem/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { RxDragHandleHorizontal } from 'react-icons/rx';
import { Spacer, Button, Switch } from '@nextui-org/react';
import { MdDeleteOutline } from 'react-icons/md';
import { useTranslation } from 'react-i18next';
import { BiSolidEdit } from 'react-icons/bi';
import { BiSolidEdit, BiCollapseVertical, BiExpandVertical } from 'react-icons/bi';
import React from 'react';

import * as builtinServices from '../../../../../../services/translate';
Expand Down Expand Up @@ -59,6 +59,23 @@ export default function ServiceItem(props) {
setServiceConfig({ ...serviceConfig, enable: v });
}}
/>
<Spacer x={2} />
<Button
isIconOnly
size='sm'
variant='light'
onPress={() => {
const collapse = serviceConfig['collapse'] ?? false;
setServiceConfig({ ...serviceConfig, collapse: !collapse });
}}
>
{serviceConfig['collapse'] ? (
<BiExpandVertical className='text-2xl' />
) : (
<BiCollapseVertical className='text-2xl' />
)}
</Button>
<Spacer x={2} />
<Button
isIconOnly
size='sm'
Expand Down
30 changes: 25 additions & 5 deletions src/window/Translate/components/TargetArea/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ export default function TargetArea(props) {
const [clipboardMonitor] = useConfig('clipboard_monitor', false);
const [hideWindow] = useConfig('translate_hide_window', false);
const [historyDisable] = useConfig('history_disable', false);
const { name, index, translateServiceList, pluginList, ...drag } = props;
const { name, index, translateServiceList, pluginList, defaultCollapse, ...drag } = props;
const [translateServiceName, setTranslateServiceName] = useState(name);
const [result, setResult] = useState('');
const [error, setError] = useState('');
const [isLoading, setIsLoading] = useState(false);
const [hide, setHide] = useState(true);
const [collapse, setCollapse] = useState(defaultCollapse);
const [translateTask, setTranslateTask] = useState(null);
const sourceText = useAtomValue(sourceTextAtom);
const sourceLanguage = useAtomValue(sourceLanguageAtom);
const targetLanguage = useAtomValue(targetLanguageAtom);
Expand Down Expand Up @@ -87,7 +89,12 @@ export default function TargetArea(props) {
}
});
}
translate();
if (!collapse) {
translate(sourceText)();
setTranslateTask(null);
} else {
setTranslateTask(() => translate(sourceText));
}
}
}, [sourceText, targetLanguage, sourceLanguage, autoCopy, hideWindow, translateServiceName, clipboardMonitor]);

Expand Down Expand Up @@ -127,7 +134,7 @@ export default function TargetArea(props) {
};
}

const translate = async () => {
const translate = (sourceText) => async () => {
let id = nanoid();
translateID[index] = id;
if (translateServiceName.startsWith('[plugin]')) {
Expand Down Expand Up @@ -417,7 +424,16 @@ export default function TargetArea(props) {
isIconOnly
variant='light'
className='h-[20px] w-[20px]'
onPress={() => setHide(!hide)}
onPress={() =>
setHide((hide) => {
setCollapse(!hide);
if (hide && translateTask !== null) {
translateTask();
setTranslateTask(null);
}
return !hide;
})
}
>
{hide ? (
<BiExpandVertical className='text-[16px]' />
Expand Down Expand Up @@ -711,7 +727,11 @@ export default function TargetArea(props) {
onPress={() => {
setError('');
setResult('');
translate();
if (translateTask !== null) {
translateTask();
} else {
translate(sourceText)();
}
}}
>
<GiCycle className='text-[16px]' />
Expand Down
2 changes: 2 additions & 0 deletions src/window/Translate/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ export default function Translate() {
translateServiceList.map((service, index) => {
const config = serviceConfig[service] ?? {};
const enable = config['enable'] ?? true;
const collapse = config['collapse'];

return enable ? (
<Draggable
Expand All @@ -291,6 +292,7 @@ export default function Translate() {
name={service}
index={index}
translateServiceList={translateServiceList}
defaultCollapse={collapse}
/>
<Spacer y={2} />
</div>
Expand Down