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

145 lines
3.5 KiB
JavaScript
Raw Normal View History

2023-09-01 15:57:05 +08:00
import { 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,
2023-09-01 15:57:05 +08:00
OPT_STYLE_HIGHLIGHT,
OPT_STYLE_DIY,
2023-08-08 16:41:47 +08:00
DEFAULT_COLOR,
EVENT_KISS,
MSG_TRANS_CURRULE,
2023-09-01 11:21:06 +08:00
TRANS_NEWLINE_LENGTH,
2023-07-21 11:55:30 +08:00
} from "../../config";
2023-07-20 13:45:41 +08:00
import { useTranslate } from "../../hooks/Translate";
2023-09-01 15:57:05 +08:00
import styled from "styled-components";
const LineSpan = styled.span`
opacity: 0.6;
text-decoration-line: underline;
text-decoration-style: ${(props) => props.$lineStyle};
text-decoration-color: ${(props) => props.$lineColor};
text-decoration-thickness: 2px;
text-underline-offset: 0.3em;
-webkit-text-decoration-line: underline;
-webkit-text-decoration-style: ${(props) => props.$lineStyle};
-webkit-text-decoration-color: ${(props) => props.$lineColor};
-webkit-text-decoration-thickness: 2px;
-webkit-text-underline-offset: 0.3em;
&:hover {
opacity: 1;
}
`;
const FuzzySpan = styled.span`
filter: blur(5px);
transition: filter 0.2s ease-in-out;
&hover: {
filter: none;
}
`;
const HighlightSpan = styled.span`
2023-09-01 17:07:21 +08:00
color: #fff;
2023-09-01 15:57:05 +08:00
background-color: ${(props) => props.$bgColor};
&hover: {
filter: none;
}
`;
const DiySpan = styled.span`
${(props) => props.$diyStyle}
`;
function StyledSpan({ textStyle, textDiyStyle, bgColor, children }) {
switch (textStyle) {
case OPT_STYLE_LINE: // 下划线
return (
<LineSpan $lineStyle="solid" $lineColor={bgColor}>
{children}
</LineSpan>
);
case OPT_STYLE_DOTLINE: // 点状线
return (
<LineSpan $lineStyle="dotted" $lineColor={bgColor}>
{children}
</LineSpan>
);
case OPT_STYLE_DASHLINE: // 虚线
return (
<LineSpan $lineStyle="dashed" $lineColor={bgColor}>
{children}
</LineSpan>
);
case OPT_STYLE_WAVYLINE: // 波浪线
return (
<LineSpan $lineStyle="wavy" $lineColor={bgColor}>
{children}
</LineSpan>
);
case OPT_STYLE_FUZZY: // 模糊
return <FuzzySpan>{children}</FuzzySpan>;
case OPT_STYLE_HIGHLIGHT: // 高亮
return (
<HighlightSpan $bgColor={bgColor || DEFAULT_COLOR}>
{children}
</HighlightSpan>
);
case OPT_STYLE_DIY: // 自定义
return <DiySpan $diyStyle={textDiyStyle}>{children}</DiySpan>;
default:
return <span>{children}</span>;
}
}
2023-07-20 13:45:41 +08:00
export default function Content({ q, translator }) {
const [rule, setRule] = useState(translator.rule);
2023-08-30 18:05:37 +08:00
const { text, sameLang, loading } = useTranslate(q, rule, translator.setting);
2023-09-01 15:57:05 +08:00
const { textStyle, bgColor = "", textDiyStyle = "" } = rule;
2023-07-20 13:45:41 +08:00
2023-09-01 11:21:06 +08:00
const { newlineLength = TRANS_NEWLINE_LENGTH } = translator.setting;
const handleKissEvent = (e) => {
const { action, args } = e.detail;
switch (action) {
case MSG_TRANS_CURRULE:
setRule(args);
break;
default:
}
};
useEffect(() => {
window.addEventListener(EVENT_KISS, handleKissEvent);
return () => {
window.removeEventListener(EVENT_KISS, handleKissEvent);
};
}, []);
2023-07-20 13:45:41 +08:00
if (loading) {
return (
<>
2023-09-01 11:21:06 +08:00
{q.length > newlineLength ? <br /> : " "}
2023-07-20 13:45:41 +08:00
<LoadingIcon />
</>
);
}
if (text && !sameLang) {
return (
<>
2023-09-01 11:21:06 +08:00
{q.length > newlineLength ? <br /> : " "}
2023-09-01 15:57:05 +08:00
<StyledSpan
textStyle={textStyle}
textDiyStyle={textDiyStyle}
bgColor={bgColor}
2023-08-03 15:29:54 +08:00
>
{text}
2023-09-01 15:57:05 +08:00
</StyledSpan>
2023-07-20 13:45:41 +08:00
</>
);
}
}