| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506 |
- import { useEffect, useState } from "react";
- import { useIntl } from "react-intl";
- import { message as AntdMessage, Modal, Collapse } from "antd";
- import { ExclamationCircleOutlined, LoadingOutlined } from "@ant-design/icons";
- import type { ISentence } from "../SentEdit";
- import SentEditMenu from "./SentEditMenu";
- import SentCellEditable from "./SentCellEditable";
- import MdView from "../MdView";
- import EditInfo, { Details } from "./EditInfo";
- import SuggestionToolbar from "./SuggestionToolbar";
- import { useAppSelector } from "../../../hooks";
- import { accept, doneSent, done, sentence } from "../../../reducers/accept-pr";
- import type { IWbw } from "../Wbw/WbwWord";
- import { my_to_roman } from "../../code/my";
- import SentWbwEdit, { sentSave } from "./SentWbwEdit";
- import { getEnding } from "../../../reducers/nissaya-ending-vocabulary";
- import { nissayaBase } from "../Nissaya/NissayaMeaning";
- import { anchor, message } from "../../../reducers/discussion";
- import TextDiff from "../../general/TextDiff";
- import { sentSave as _sentSave } from "./SentCellEditable";
- import type { IDeleteResponse } from "../../../api/Article";
- import { delete_, get } from "../../../request";
- import "./style.css";
- import StudioName from "../../auth/Studio";
- import CopyToModal from "../../channel/CopyToModal";
- import store from "../../../store";
- import { randomString } from "../../../utils";
- import User from "../../auth/User";
- import type { ISentenceListResponse } from "../../../api/Corpus";
- import { toISentence } from "./SentCanRead";
- import SentAttachment from "./SentAttachment";
- import NissayaSent from "../Nissaya/NissayaSent";
- interface ISnowFlakeResponse {
- ok: boolean;
- message?: string;
- data: {
- rows: string;
- count: number;
- };
- }
- interface IWidget {
- initValue?: ISentence;
- value?: ISentence;
- wordWidget?: boolean;
- isPr?: boolean;
- editMode?: boolean;
- compact?: boolean;
- showDiff?: boolean;
- diffText?: string | null;
- onChange?: (data: ISentence) => void;
- onDelete?: Function;
- }
- const SentCellWidget = ({
- initValue,
- value,
- wordWidget = false,
- isPr = false,
- editMode = false,
- compact = false,
- showDiff = false,
- diffText,
- onChange,
- onDelete,
- }: IWidget) => {
- console.debug("SentCell render", value);
- const intl = useIntl();
- const [isEditMode, setIsEditMode] = useState(editMode);
- const [sentData, setSentData] = useState<ISentence | undefined>(initValue);
- const [bgColor, setBgColor] = useState<string>();
- const [loading, setLoading] = useState(false);
- const [uuid] = useState(randomString());
- const endings = useAppSelector(getEnding);
- const acceptPr = useAppSelector(sentence);
- const changedSent = useAppSelector(doneSent);
- const [prOpen, setPrOpen] = useState(false);
- const discussionMessage = useAppSelector(message);
- const anchorInfo = useAppSelector(anchor);
- const [copyOpen, setCopyOpen] = useState<boolean>(false);
- const sentId = `${sentData?.book}-${sentData?.para}-${sentData?.wordStart}-${sentData?.wordEnd}`;
- const sid = `${sentData?.book}_${sentData?.para}_${sentData?.wordStart}_${sentData?.wordEnd}_${sentData?.channel?.id}`;
- useEffect(() => {
- if (
- discussionMessage &&
- discussionMessage.resId &&
- discussionMessage.resId === initValue?.id
- ) {
- setBgColor("#1890ff33");
- } else {
- setBgColor(undefined);
- }
- }, [discussionMessage, initValue?.id]);
- useEffect(() => {
- if (anchorInfo && anchorInfo?.resId === initValue?.id) {
- const ele = document.getElementById(sid);
- if (ele !== null) {
- ele.scrollIntoView({
- behavior: "smooth",
- block: "center",
- inline: "nearest",
- });
- }
- }
- }, [anchorInfo, initValue?.id, sid]);
- useEffect(() => {
- if (value) {
- setSentData(value);
- }
- }, [value]);
- useEffect(() => {
- console.debug("sent cell acceptPr", acceptPr, uuid);
- if (isPr) {
- console.debug("sent cell is pr");
- return;
- }
- if (typeof acceptPr === "undefined" || acceptPr.length === 0) {
- console.debug("sent cell acceptPr is empty");
- return;
- }
- if (!sentData) {
- console.debug("sent cell sentData is empty");
- return;
- }
- if (changedSent?.includes(uuid)) {
- console.debug("sent cell already apply", uuid);
- return;
- }
- const found = acceptPr
- .filter((value) => typeof value !== "undefined")
- .find((value) => {
- const vId = `${value.book}_${value.para}_${value.wordStart}_${value.wordEnd}_${value.channel.id}`;
- return vId === sid;
- });
- if (typeof found !== "undefined") {
- console.debug("sent cell sentence apply", uuid, found, found);
- setSentData(found);
- store.dispatch(done(uuid));
- }
- }, [acceptPr, sentData, isPr, uuid, changedSent, sid]);
- const deletePr = (id: string) => {
- delete_<IDeleteResponse>(`/v2/sentpr/${id}`)
- .then((json) => {
- if (json.ok) {
- AntdMessage.success("删除成功");
- if (typeof onDelete !== "undefined") {
- onDelete();
- }
- } else {
- AntdMessage.error(json.message);
- }
- })
- .catch((e) => console.log("Oops errors!", e));
- };
- const refresh = () => {
- if (typeof sentData === "undefined") {
- return;
- }
- let url = `/v2/sentence?view=channel&sentence=${sentId}&html=true`;
- url += `&channel=${sentData.channel.id}`;
- console.debug("api request", url);
- setLoading(true);
- get<ISentenceListResponse>(url)
- .then((json) => {
- console.debug("api response", json);
- if (json.ok && json.data.count > 0) {
- const newData: ISentence[] = json.data.rows.map((item) => {
- return toISentence(item, [sentData.channel.id]);
- });
- setSentData(newData[0]);
- }
- })
- .finally(() => setLoading(false));
- };
- return (
- <div style={{ marginBottom: "8px", backgroundColor: bgColor }}>
- {loading ? <LoadingOutlined /> : <></>}
- {isPr ? undefined : (
- <div
- dangerouslySetInnerHTML={{
- __html: `<div class="tran_sent" id="${sid}" ></div>`,
- }}
- />
- )}
- <SentEditMenu
- isPr={isPr}
- data={sentData}
- onModeChange={(mode: string) => {
- if (mode === "edit") {
- setIsEditMode(true);
- }
- }}
- onMenuClick={(key: string) => {
- switch (key) {
- case "refresh":
- refresh();
- break;
- case "copy-to":
- setCopyOpen(true);
- break;
- case "suggestion":
- setPrOpen(true);
- break;
- case "paste":
- navigator.clipboard.readText().then((value: string) => {
- if (sentData && value !== "") {
- sentData.content = value;
- _sentSave(
- sentData,
- (res: ISentence) => {
- //setSentData(res);
- //发布句子的改变,让同样的句子更新
- store.dispatch(accept([res]));
- if (typeof onChange !== "undefined") {
- onChange(res);
- }
- },
- () => {}
- );
- }
- });
- break;
- case "delete":
- Modal.confirm({
- icon: <ExclamationCircleOutlined />,
- title: intl.formatMessage({
- id: "message.delete.confirm",
- }),
- content: "",
- okText: intl.formatMessage({
- id: "buttons.delete",
- }),
- okType: "danger",
- cancelText: intl.formatMessage({
- id: "buttons.no",
- }),
- onOk() {
- if (isPr && sentData && sentData.id) {
- deletePr(sentData.id);
- }
- },
- });
- break;
- default:
- break;
- }
- }}
- onConvert={async (format: string) => {
- switch (format) {
- case "json":
- const wbw: IWbw[] = sentData?.content
- ? sentData.content
- .split("\n")
- .filter((value) => value.trim().length > 0)
- .map((item, id) => {
- const parts = item.split("=");
- const word = my_to_roman(parts[0]);
- const meaning: string =
- parts.length > 1
- ? parts[1]
- .trim()
- .replaceAll("။", "")
- .replaceAll("(", " ( ")
- .replaceAll(")", " ) ")
- : "";
- const translation: string =
- parts.length > 2 ? parts[2].trim() : "";
- let parent: string = "";
- let factors: string = "";
- const factor1 = meaning
- .split(" ")
- .filter((value) => value !== "");
- factors = factor1
- .map((item) => {
- if (endings) {
- const base = nissayaBase(item, endings);
- if (factor1.length === 1) {
- parent = base.base;
- }
- const end = base.ending ? base.ending : [];
- return [base.base, ...end]
- .filter((value) => value !== "")
- .join("-");
- } else {
- return item;
- }
- })
- .join("+");
- return {
- uid: "0",
- book: sentData.book,
- para: sentData.para,
- sn: [id],
- word: { value: word ? word : parts[0], status: 0 },
- real: { value: meaning, status: 0 },
- meaning: { value: translation, status: 0 },
- parent: { value: parent, status: 0 },
- factors: {
- value: factors,
- status: 0,
- },
- confidence: 0.5,
- };
- })
- : [];
- if (wbw.length > 0) {
- const snowflake = await get<ISnowFlakeResponse>(
- `/v2/snowflake?count=${wbw.length}`
- );
- wbw.forEach((_value: IWbw, index: number, array: IWbw[]) => {
- array[index].uid = snowflake.data.rows[index];
- });
- }
- if (sentData) {
- const newData = JSON.parse(JSON.stringify(sentData));
- newData.contentType = "json";
- newData.content = JSON.stringify(wbw);
- setSentData(newData);
- sentSave(newData, intl);
- }
- setIsEditMode(true);
- break;
- case "markdown":
- Modal.confirm({
- title: "格式转换",
- content:
- "转换为markdown格式后,拆分意思数据会丢失。确定要转换吗?",
- onOk() {
- if (sentData) {
- const newData = JSON.parse(JSON.stringify(sentData));
- const wbwData: IWbw[] = newData.content
- ? JSON.parse(newData.content)
- : [];
- const newContent = wbwData
- .filter((value) => value.sn.length === 1)
- .map((item) => {
- return [
- item.word.value,
- item.real.value,
- item.meaning?.value,
- ].join("=");
- })
- .join("\n");
- newData.content = newContent;
- newData["contentType"] = "markdown";
- sentSave(newData, intl);
- setSentData(newData);
- }
- setIsEditMode(true);
- },
- });
- break;
- }
- }}
- >
- {sentData ? (
- <div style={{ display: "flex" }}>
- <div style={{ marginRight: 8 }}>
- {isPr ? (
- <User {...sentData.editor} showName={false} />
- ) : (
- <StudioName
- data={sentData.studio}
- hideName
- popOver={
- compact ? (
- <Details data={sentData} isPr={isPr} />
- ) : undefined
- }
- />
- )}
- </div>
- <div
- style={{
- display: "flex",
- flexDirection: compact ? "row" : "column",
- alignItems: "flex-start",
- width: "100%",
- }}
- >
- {isEditMode ? (
- sentData?.contentType === "json" ? (
- <SentWbwEdit
- data={sentData}
- onClose={() => {
- setIsEditMode(false);
- }}
- onSave={(data: ISentence) => {
- console.debug("sent cell onSave", data);
- setSentData(data);
- }}
- />
- ) : (
- <SentCellEditable
- data={sentData}
- isPr={isPr}
- onClose={() => {
- setIsEditMode(false);
- }}
- onSave={(data: ISentence) => {
- console.debug("sent cell onSave", data);
- //setSentData(data);
- store.dispatch(accept([data]));
- setIsEditMode(false);
- if (typeof onChange !== "undefined") {
- onChange(data);
- }
- }}
- />
- )
- ) : showDiff ? (
- <TextDiff
- showToolTip={false}
- content={sentData.content}
- oldContent={diffText}
- />
- ) : sentData.channel.type === "nissaya" ? (
- <NissayaSent data={JSON.parse(sentData.content ?? "[])")} />
- ) : (
- <MdView
- className="sentence"
- style={{
- width: "100%",
- marginBottom: 0,
- }}
- placeholder={intl.formatMessage({
- id: "labels.input",
- })}
- html={sentData.html ? sentData.html : sentData.content}
- wordWidget={wordWidget}
- />
- )}
- <div
- style={{
- display: "flex",
- justifyContent: "space-between",
- width: compact ? undefined : "100%",
- paddingRight: 20,
- flexWrap: "wrap",
- }}
- >
- <EditInfo data={sentData} isPr={isPr} compact={compact} />
- <SuggestionToolbar
- style={{
- marginBottom: 0,
- justifyContent: "flex-end",
- marginLeft: "auto",
- }}
- compact={compact}
- data={sentData}
- isPr={isPr}
- prOpen={prOpen}
- onPrClose={() => setPrOpen(false)}
- onDelete={() => {
- if (isPr && sentData.id) {
- deletePr(sentData.id);
- }
- }}
- />
- </div>
- </div>
- </div>
- ) : undefined}
- </SentEditMenu>
- <CopyToModal
- important
- sentencesId={[sentId]}
- channel={sentData?.channel}
- open={copyOpen}
- onClose={() => setCopyOpen(false)}
- />
- <Collapse
- bordered={false}
- style={{ display: "none", backgroundColor: "unset" }}
- >
- <Collapse.Panel
- header={"attachment"}
- key="parent2"
- style={{ backgroundColor: "unset" }}
- >
- <SentAttachment sentenceId={sentData?.id} />
- </Collapse.Panel>
- </Collapse>
- </div>
- );
- };
- export default SentCellWidget;
|