feat: support builtin AI

This commit is contained in:
Gabe
2025-10-04 21:25:54 +08:00
parent c353c88db8
commit 7b2b48f0d1
23 changed files with 558 additions and 243 deletions

View File

@@ -7,7 +7,7 @@ import Divider from "@mui/material/Divider";
import Alert from "@mui/material/Alert";
import CopyBtn from "./CopyBtn";
import { useAsyncNow } from "../../hooks/Fetch";
import { DICT_MAP } from "./DictMap";
import { dictHandlers } from "./DictHandler";
function DictBody({ text, setCopyText, dict }) {
const { loading, error, data } = useAsyncNow(dict.apiFn, text);
@@ -17,7 +17,7 @@ function DictBody({ text, setCopyText, dict }) {
return;
}
const copyText = [text, dict.toText(data)].join("\n");
const copyText = [text, dict.toText(data).join("\n")].join("\n");
setCopyText(copyText);
}, [data, text, dict, setCopyText]);
@@ -46,7 +46,7 @@ function DictBody({ text, setCopyText, dict }) {
export default function DictCont({ text, enDict }) {
const [copyText, setCopyText] = useState(text);
const dict = DICT_MAP[enDict];
const dict = dictHandlers[enDict];
return (
<Stack spacing={1}>

View File

@@ -3,13 +3,11 @@ import AudioBtn from "./AudioBtn";
import { OPT_DICT_BING, OPT_DICT_YOUDAO } from "../../config";
import { apiMicrosoftDict, apiYoudaoDict } from "../../apis";
export const DICT_MAP = {
export const dictHandlers = {
[OPT_DICT_BING]: {
apiFn: apiMicrosoftDict,
toText: (data) =>
data.trs
?.map(({ pos, def }) => `${pos ? `[${pos}] ` : ""}${def}`)
.join("\n"),
data.trs?.map(({ pos, def }) => `${pos ? `[${pos}] ` : ""}${def}`) || [],
uiAudio: (data) => (
<Typography component="div">
{data?.aus.map(({ key, audio, phonetic }) => (
@@ -38,9 +36,9 @@ export const DICT_MAP = {
[OPT_DICT_YOUDAO]: {
apiFn: apiYoudaoDict,
toText: (data) =>
data?.ec?.word?.trs
?.map(({ pos, tran }) => `${pos ? `[${pos}] ` : ""}${tran}`)
.join("\n"),
data?.ec?.word?.trs?.map(
({ pos, tran }) => `${pos ? `[${pos}] ` : ""}${tran}`
) || [],
uiAudio: () => null,
uiTrans: (data) => (
<Typography component="ul">