2023-08-09 13:22:10 +08:00
|
|
|
import { useMemo, useState, useEffect } from "react";
|
2023-07-20 13:45:41 +08:00
|
|
|
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-08 16:41:47 +08:00
|
|
|
DEFAULT_COLOR,
|
2023-08-09 13:22:10 +08:00
|
|
|
EVENT_KISS,
|
|
|
|
|
MSG_TRANS_CURRULE,
|
2023-07-21 11:55:30 +08:00
|
|
|
} from "../../config";
|
2023-07-20 13:45:41 +08:00
|
|
|
import { useTranslate } from "../../hooks/Translate";
|
|
|
|
|
|
2023-08-09 13:22:10 +08:00
|
|
|
export default function Content({ q, translator }) {
|
|
|
|
|
const [rule, setRule] = useState(translator.rule);
|
2023-07-20 13:45:41 +08:00
|
|
|
const [hover, setHover] = useState(false);
|
2023-08-30 18:05:37 +08:00
|
|
|
const { text, sameLang, loading } = useTranslate(q, rule, translator.setting);
|
2023-08-09 13:22:10 +08:00
|
|
|
const { textStyle, bgColor } = rule;
|
2023-07-20 13:45:41 +08:00
|
|
|
|
|
|
|
|
const handleMouseEnter = () => {
|
|
|
|
|
setHover(true);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleMouseLeave = () => {
|
|
|
|
|
setHover(false);
|
|
|
|
|
};
|
|
|
|
|
|
2023-08-09 13:22:10 +08:00
|
|
|
const handleKissEvent = (e) => {
|
|
|
|
|
const { action, args } = e.detail;
|
|
|
|
|
switch (action) {
|
|
|
|
|
case MSG_TRANS_CURRULE:
|
|
|
|
|
setRule(args);
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
// console.log(`[popup] kissEvent action skip: ${action}`);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
window.addEventListener(EVENT_KISS, handleKissEvent);
|
|
|
|
|
return () => {
|
|
|
|
|
window.removeEventListener(EVENT_KISS, handleKissEvent);
|
|
|
|
|
};
|
|
|
|
|
}, []);
|
|
|
|
|
|
2023-07-20 13:45:41 +08:00
|
|
|
const style = useMemo(() => {
|
2023-08-08 16:41:47 +08:00
|
|
|
const lineColor = bgColor || "";
|
2023-08-29 17:34:37 +08:00
|
|
|
const underlineStyle = (st) => ({
|
|
|
|
|
opacity: hover ? 1 : 0.6,
|
|
|
|
|
textDecorationLine: "underline",
|
|
|
|
|
textDecorationColor: lineColor,
|
|
|
|
|
textDecorationStyle: st,
|
|
|
|
|
textDecorationThickness: "2px",
|
|
|
|
|
textUnderlineOffset: "0.3em",
|
|
|
|
|
WebkittextDecorationLine: "underline",
|
|
|
|
|
WebkittextDecorationColor: lineColor,
|
|
|
|
|
WebkittextDecorationStyle: st,
|
|
|
|
|
WebkittextDecorationThickness: "2px",
|
|
|
|
|
WebkittextTextUnderlineOffset: "0.3em",
|
|
|
|
|
});
|
2023-07-20 13:45:41 +08:00
|
|
|
switch (textStyle) {
|
2023-07-21 11:55:30 +08:00
|
|
|
case OPT_STYLE_LINE: // 下划线
|
2023-08-29 17:34:37 +08:00
|
|
|
return underlineStyle("solid");
|
2023-07-21 11:55:30 +08:00
|
|
|
case OPT_STYLE_DOTLINE: // 点状线
|
2023-08-29 17:34:37 +08:00
|
|
|
return underlineStyle("dotted");
|
2023-07-21 12:31:21 +08:00
|
|
|
case OPT_STYLE_DASHLINE: // 虚线
|
2023-08-29 17:34:37 +08:00
|
|
|
return underlineStyle("dashed");
|
2023-07-21 11:55:30 +08:00
|
|
|
case OPT_STYLE_WAVYLINE: // 波浪线
|
2023-08-29 17:34:37 +08:00
|
|
|
return underlineStyle("wavy");
|
2023-07-21 11:55:30 +08:00
|
|
|
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",
|
2023-08-08 16:41:47 +08:00
|
|
|
backgroundColor: bgColor || DEFAULT_COLOR,
|
2023-07-20 13:45:41 +08:00
|
|
|
};
|
|
|
|
|
default:
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2023-08-08 16:41:47 +08:00
|
|
|
}, [textStyle, hover, bgColor]);
|
2023-07-20 13:45:41 +08:00
|
|
|
|
|
|
|
|
if (loading) {
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{q.length > 40 ? <br /> : " "}
|
|
|
|
|
<LoadingIcon />
|
|
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (text && !sameLang) {
|
|
|
|
|
return (
|
|
|
|
|
<>
|
|
|
|
|
{q.length > 40 ? <br /> : " "}
|
2023-08-03 15:29:54 +08:00
|
|
|
<span
|
|
|
|
|
style={style}
|
|
|
|
|
onMouseEnter={handleMouseEnter}
|
|
|
|
|
onMouseLeave={handleMouseLeave}
|
|
|
|
|
>
|
|
|
|
|
{text}
|
|
|
|
|
</span>
|
2023-07-20 13:45:41 +08:00
|
|
|
</>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|