fix: youdao dict

This commit is contained in:
Gabe
2025-10-04 22:29:03 +08:00
parent 7b2b48f0d1
commit e562f0b851
6 changed files with 50 additions and 22 deletions

View File

@@ -9,7 +9,7 @@ import CopyBtn from "./CopyBtn";
import { useAsyncNow } from "../../hooks/Fetch";
import { dictHandlers } from "./DictHandler";
function DictBody({ text, setCopyText, dict }) {
function DictBody({ text, setCopyText, setRealWord, dict }) {
const { loading, error, data } = useAsyncNow(dict.apiFn, text);
useEffect(() => {
@@ -17,9 +17,11 @@ function DictBody({ text, setCopyText, dict }) {
return;
}
const copyText = [text, dict.toText(data).join("\n")].join("\n");
const realWord = dict.reWord(data) || text;
const copyText = [realWord, dict.toText(data).join("\n")].join("\n");
setRealWord(realWord);
setCopyText(copyText);
}, [data, text, dict, setCopyText]);
}, [data, text, dict, setCopyText, setRealWord]);
const uiAudio = useMemo(() => dict.uiAudio(data), [data, dict]);
const uiTrans = useMemo(() => dict.uiTrans(data), [data, dict]);
@@ -46,6 +48,7 @@ function DictBody({ text, setCopyText, dict }) {
export default function DictCont({ text, enDict }) {
const [copyText, setCopyText] = useState(text);
const [realWord, setRealWord] = useState(text);
const dict = dictHandlers[enDict];
return (
@@ -53,18 +56,25 @@ export default function DictCont({ text, enDict }) {
{text && (
<Stack direction="row" justifyContent="space-between">
<Typography variant="subtitle1" style={{ fontWeight: "bold" }}>
{text}
{realWord}
</Typography>
<Stack direction="row" justifyContent="space-between">
<CopyBtn text={copyText} />
<FavBtn word={text} />
<FavBtn word={realWord} />
</Stack>
</Stack>
)}
<Divider />
{dict && <DictBody text={text} setCopyText={setCopyText} dict={dict} />}
{dict && (
<DictBody
text={text}
setCopyText={setCopyText}
setRealWord={setRealWord}
dict={dict}
/>
)}
</Stack>
);
}

View File

@@ -6,15 +6,16 @@ import { apiMicrosoftDict, apiYoudaoDict } from "../../apis";
export const dictHandlers = {
[OPT_DICT_BING]: {
apiFn: apiMicrosoftDict,
reWord: (data) => data?.word,
toText: (data) =>
data.trs?.map(({ pos, def }) => `${pos ? `[${pos}] ` : ""}${def}`) || [],
data?.trs?.map(({ pos, def }) => `${pos ? `[${pos}] ` : ""}${def}`) || [],
uiAudio: (data) => (
<Typography component="div">
{data?.aus.map(({ key, audio, phonetic }) => (
{data?.aus?.map(({ key, audio, phonetic }) => (
<Typography
component="div"
key={key}
style={{ display: "inline-block" }}
style={{ display: "inline-block", paddingRight: "1em" }}
>
<Typography component="span">{phonetic}</Typography>
<AudioBtn src={audio} />
@@ -35,11 +36,27 @@ export const dictHandlers = {
},
[OPT_DICT_YOUDAO]: {
apiFn: apiYoudaoDict,
reWord: (data) => data?.ec?.word?.["return-phrase"],
toText: (data) =>
data?.ec?.word?.trs?.map(
({ pos, tran }) => `${pos ? `[${pos}] ` : ""}${tran}`
) || [],
uiAudio: () => null,
uiAudio: (data) => (
<Typography component="div">
<Typography
component="div"
style={{ display: "inline-block", paddingRight: "1em" }}
>
<Typography component="span">{`UK [${data?.ec?.word?.ukphone}]`}</Typography>
</Typography>
<Typography
component="div"
style={{ display: "inline-block", paddingRight: "1em" }}
>
<Typography component="span">{`US [${data?.ec?.word?.usphone}]`}</Typography>
</Typography>
</Typography>
),
uiTrans: (data) => (
<Typography component="ul">
{data?.ec?.word?.trs?.map(({ pos, tran }, idx) => (