SentRead.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import { useEffect, useRef, useState } from "react";
  2. import { Button, Dropdown, MenuProps, Typography } from "antd";
  3. import { LoadingOutlined, CloseOutlined } from "@ant-design/icons";
  4. import { useAppSelector } from "../../hooks";
  5. import {
  6. onChangeKey,
  7. onChangeValue,
  8. settingInfo,
  9. } from "../../reducers/setting";
  10. import { GetUserSetting } from "../auth/setting/default";
  11. import { TCodeConvertor } from "./utilities";
  12. import { ISentence, IWidgetSentEditInner, SentEditInner } from "./SentEdit";
  13. import MdView from "./MdView";
  14. import store from "../../store";
  15. import { push } from "../../reducers/sentence";
  16. import "./style.css";
  17. import InteractiveButton from "./SentEdit/InteractiveButton";
  18. import { prOpen } from "./SentEdit/SuggestionButton";
  19. import { openDiscussion } from "../discussion/DiscussionButton";
  20. import { IEditableSentence } from "../api/Corpus";
  21. import { get } from "../../request";
  22. const { Text } = Typography;
  23. const items: MenuProps["items"] = [
  24. {
  25. label: "编辑",
  26. key: "edit",
  27. },
  28. {
  29. label: "讨论",
  30. key: "discussion",
  31. },
  32. {
  33. label: "修改建议",
  34. key: "pr",
  35. },
  36. {
  37. label: "标签",
  38. key: "tag",
  39. },
  40. ];
  41. interface IWidgetSentReadFrame {
  42. origin?: ISentence[];
  43. translation?: ISentence[];
  44. layout?: "row" | "column";
  45. book?: number;
  46. para?: number;
  47. wordStart?: number;
  48. wordEnd?: number;
  49. sentId?: string;
  50. error?: string;
  51. }
  52. const SentReadFrame = ({
  53. origin,
  54. translation,
  55. layout = "column",
  56. book,
  57. para,
  58. wordStart,
  59. wordEnd,
  60. sentId,
  61. error,
  62. }: IWidgetSentReadFrame) => {
  63. const [paliCode1, setPaliCode1] = useState<TCodeConvertor>("roman");
  64. const [active, setActive] = useState(false);
  65. const [loading, setLoading] = useState(false);
  66. const [sentData, setSentData] = useState<IWidgetSentEditInner>();
  67. const [showEdit, SetShowEdit] = useState(false);
  68. const [translationData, setTranslationData] = useState<
  69. ISentence[] | undefined
  70. >(translation);
  71. const key = useAppSelector(onChangeKey);
  72. const value = useAppSelector(onChangeValue);
  73. const settings = useAppSelector(settingInfo);
  74. const boxOrg = useRef<HTMLDivElement>(null);
  75. const boxSent = useRef<HTMLDivElement>(null);
  76. useEffect(() => {
  77. store.dispatch(
  78. push({
  79. id: `${book}-${para}-${wordStart}-${wordEnd}`,
  80. origin: origin?.map((item) => item.html),
  81. translation: translation?.map((item) => item.html),
  82. })
  83. );
  84. }, [book, origin, para, translation, wordEnd, wordStart]);
  85. useEffect(() => {
  86. const displayOriginal = GetUserSetting(
  87. "setting.display.original",
  88. settings
  89. );
  90. if (typeof displayOriginal === "boolean") {
  91. if (boxOrg.current) {
  92. if (
  93. displayOriginal === false &&
  94. translation &&
  95. translation.length > 0
  96. ) {
  97. boxOrg.current.style.display = "none";
  98. } else {
  99. boxOrg.current.style.display = "block";
  100. }
  101. }
  102. }
  103. const layoutDirection = GetUserSetting(
  104. "setting.layout.direction",
  105. settings
  106. );
  107. if (typeof layoutDirection === "string") {
  108. if (boxSent.current) {
  109. boxSent.current.style.flexDirection = layoutDirection;
  110. }
  111. }
  112. const _paliCode1 = GetUserSetting("setting.pali.script.primary", settings);
  113. if (typeof _paliCode1 !== "undefined") {
  114. setPaliCode1(_paliCode1.toString() as TCodeConvertor);
  115. }
  116. }, [key, value, settings]);
  117. return (
  118. <span ref={boxSent} className="sent_read_shell">
  119. <Text type="danger" mark>
  120. {error}
  121. </Text>
  122. <span
  123. dangerouslySetInnerHTML={{
  124. __html: `<span class="pcd_sent" id="sent_${book}-${para}-${wordStart}-${wordEnd}"></span>`,
  125. }}
  126. />
  127. <span style={{ flex: "5", color: "#9f3a01" }} ref={boxOrg}>
  128. {origin?.map((item, id) => {
  129. return (
  130. <Text key={id}>
  131. <MdView
  132. style={{ color: "brown" }}
  133. html={item.html}
  134. wordWidget={true}
  135. convertor={paliCode1}
  136. />
  137. </Text>
  138. );
  139. })}
  140. </span>
  141. <span className="sent_read" style={{ flex: "5" }}>
  142. {translationData?.map((item, id) => {
  143. return (
  144. <span key={id}>
  145. {loading ? <LoadingOutlined /> : <></>}
  146. <Dropdown
  147. menu={{
  148. items,
  149. onClick: (e) => {
  150. console.log("click ", e);
  151. switch (e.key) {
  152. case "edit":
  153. const url = `/v2/editable-sentence/${item.id}`;
  154. console.info("api request", url);
  155. setLoading(true);
  156. get<IEditableSentence>(url)
  157. .then((json) => {
  158. console.info("api response", json);
  159. if (json.ok) {
  160. setSentData(json.data);
  161. SetShowEdit(true);
  162. }
  163. })
  164. .finally(() => setLoading(false));
  165. break;
  166. case "discussion":
  167. if (item.id) {
  168. openDiscussion(item.id, false);
  169. }
  170. break;
  171. case "pr":
  172. prOpen(item);
  173. break;
  174. default:
  175. break;
  176. }
  177. },
  178. }}
  179. trigger={["contextMenu"]}
  180. >
  181. <Text
  182. key={id}
  183. className="sent_read_translation"
  184. style={{ display: showEdit ? "none" : "inline" }}
  185. >
  186. <MdView
  187. html={item.html}
  188. style={{ backgroundColor: active ? "beige" : "unset" }}
  189. />
  190. </Text>
  191. </Dropdown>
  192. <div style={{ display: showEdit ? "block" : "none" }}>
  193. <div style={{ textAlign: "right" }}>
  194. <Button
  195. size="small"
  196. icon={<CloseOutlined />}
  197. onClick={() => {
  198. SetShowEdit(false);
  199. }}
  200. >
  201. 返回审阅模式
  202. </Button>
  203. </div>
  204. {sentData ? (
  205. <SentEditInner
  206. mode="edit"
  207. {...sentData}
  208. onTranslationChange={(data: ISentence) => {
  209. console.log("onTranslationChange", data);
  210. if (translationData) {
  211. let newData = [...translationData];
  212. newData.forEach(
  213. (
  214. value: ISentence,
  215. index: number,
  216. array: ISentence[]
  217. ) => {
  218. if (index === id) {
  219. array[index] = data;
  220. }
  221. }
  222. );
  223. setTranslationData(newData);
  224. }
  225. }}
  226. />
  227. ) : (
  228. "无数据"
  229. )}
  230. </div>
  231. <InteractiveButton
  232. data={item}
  233. compact={true}
  234. float={true}
  235. hideCount
  236. hideInZero
  237. onMouseEnter={() => setActive(true)}
  238. onMouseLeave={() => setActive(false)}
  239. />
  240. </span>
  241. );
  242. })}
  243. </span>
  244. </span>
  245. );
  246. };
  247. interface IWidget {
  248. props: string;
  249. }
  250. const Widget = ({ props }: IWidget) => {
  251. const prop = JSON.parse(atob(props)) as IWidgetSentReadFrame;
  252. return (
  253. <>
  254. <SentReadFrame {...prop} />
  255. </>
  256. );
  257. };
  258. export default Widget;