Files
kiss-translator/src/views/Content/index.js

108 lines
2.7 KiB
JavaScript
Raw Normal View History

2023-07-20 13:45:41 +08:00
import { useMemo, useState } from "react";
import LoadingIcon from "./LoadingIcon";
2023-07-21 11:55:30 +08:00
import {
OPT_STYLE_LINE,
OPT_STYLE_DOTLINE,
OPT_STYLE_DASHLINE,
OPT_STYLE_WAVYLINE,
OPT_STYLE_FUZZY,
OPT_STYLE_HIGHTLIGHT,
2023-08-03 12:49:03 +08:00
OPT_TRANS_OPENAI,
2023-07-21 11:55:30 +08:00
} from "../../config";
2023-07-20 13:45:41 +08:00
import { useTranslate } from "../../hooks/Translate";
export default function Content({ q, rule }) {
const [hover, setHover] = useState(false);
const { text, sameLang, loading, textStyle } = useTranslate(q, rule);
const handleMouseEnter = () => {
setHover(true);
};
const handleMouseLeave = () => {
setHover(false);
};
const style = useMemo(() => {
switch (textStyle) {
2023-07-21 11:55:30 +08:00
case OPT_STYLE_LINE: // 下划线
return {
opacity: hover ? 1 : 0.6,
textDecoration: "underline 2px",
textUnderlineOffset: "0.3em",
};
case OPT_STYLE_DOTLINE: // 点状线
return {
opacity: hover ? 1 : 0.6,
textDecoration: "dotted underline 2px",
textUnderlineOffset: "0.3em",
};
2023-07-21 12:31:21 +08:00
case OPT_STYLE_DASHLINE: // 虚线
2023-07-20 13:45:41 +08:00
return {
opacity: hover ? 1 : 0.6,
textDecoration: "dashed underline 2px",
textUnderlineOffset: "0.3em",
};
2023-07-21 11:55:30 +08:00
case OPT_STYLE_WAVYLINE: // 波浪线
return {
opacity: hover ? 1 : 0.6,
textDecoration: "wavy underline 2px",
textUnderlineOffset: "0.3em",
};
case OPT_STYLE_FUZZY: // 模糊
2023-07-20 13:45:41 +08:00
return {
filter: hover ? "none" : "blur(5px)",
2023-07-21 11:55:30 +08:00
transition: "filter 0.2s ease-in-out",
};
case OPT_STYLE_HIGHTLIGHT: // 高亮
return {
color: "#FFF",
backgroundColor: "#209CEE",
2023-07-20 13:45:41 +08:00
};
default:
return {};
}
}, [textStyle, hover]);
if (loading) {
return (
<>
{q.length > 40 ? <br /> : " "}
<LoadingIcon />
</>
);
}
if (text && !sameLang) {
return (
<>
{q.length > 40 ? <br /> : " "}
2023-08-03 12:49:03 +08:00
{rule.translator === OPT_TRANS_OPENAI ? (
<span
style={style}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
>
{text}
</span>
) : (
<span
style={style}
onMouseEnter={handleMouseEnter}
onMouseLeave={handleMouseLeave}
dangerouslySetInnerHTML={{
__html: text.replace(
/<code>(.*?)<\/code>/gi,
(_match, p1) =>
`<code>${p1
.replace(/</g, "&lt;")
.replace(/>/g, "&gt;")}</code>`
),
}}
/>
)}
2023-07-20 13:45:41 +08:00
</>
);
}
}