SentRead.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { useEffect, useRef, useState } from "react";
  2. import { Typography } from "antd";
  3. import { useAppSelector } from "../../hooks";
  4. import {
  5. onChangeKey,
  6. onChangeValue,
  7. settingInfo,
  8. } from "../../reducers/setting";
  9. import { GetUserSetting } from "../auth/setting/default";
  10. import { TCodeConvertor } from "./utilities";
  11. import { ISentence } from "./SentEdit";
  12. import MdView from "./MdView";
  13. import store from "../../store";
  14. import { push } from "../../reducers/sentence";
  15. const { Text } = Typography;
  16. interface IWidgetSentReadFrame {
  17. origin?: ISentence[];
  18. translation?: ISentence[];
  19. layout?: "row" | "column";
  20. book?: number;
  21. para?: number;
  22. wordStart?: number;
  23. wordEnd?: number;
  24. sentId?: string;
  25. error?: string;
  26. }
  27. const SentReadFrame = ({
  28. origin,
  29. translation,
  30. layout = "column",
  31. book,
  32. para,
  33. wordStart,
  34. wordEnd,
  35. sentId,
  36. error,
  37. }: IWidgetSentReadFrame) => {
  38. const [paliCode1, setPaliCode1] = useState<TCodeConvertor>("roman");
  39. const key = useAppSelector(onChangeKey);
  40. const value = useAppSelector(onChangeValue);
  41. const settings = useAppSelector(settingInfo);
  42. const boxOrg = useRef<HTMLDivElement>(null);
  43. const boxSent = useRef<HTMLDivElement>(null);
  44. useEffect(() => {
  45. store.dispatch(
  46. push({
  47. id: `${book}-${para}-${wordStart}-${wordEnd}`,
  48. origin: origin?.map((item) => item.html),
  49. translation: translation?.map((item) => item.html),
  50. })
  51. );
  52. }, []);
  53. useEffect(() => {
  54. const displayOriginal = GetUserSetting(
  55. "setting.display.original",
  56. settings
  57. );
  58. if (typeof displayOriginal === "boolean") {
  59. if (boxOrg.current) {
  60. if (displayOriginal === true) {
  61. boxOrg.current.style.display = "block";
  62. } else {
  63. boxOrg.current.style.display = "none";
  64. }
  65. }
  66. }
  67. const layoutDirection = GetUserSetting(
  68. "setting.layout.direction",
  69. settings
  70. );
  71. if (typeof layoutDirection === "string") {
  72. if (boxSent.current) {
  73. boxSent.current.style.flexDirection = layoutDirection;
  74. }
  75. }
  76. const _paliCode1 = GetUserSetting("setting.pali.script.primary", settings);
  77. if (typeof _paliCode1 !== "undefined") {
  78. setPaliCode1(_paliCode1.toString() as TCodeConvertor);
  79. }
  80. }, [key, value, settings]);
  81. return (
  82. <div
  83. style={{ display: "flex", flexDirection: layout, marginBottom: 10 }}
  84. ref={boxSent}
  85. >
  86. <Text type="danger" mark>
  87. {error}
  88. </Text>
  89. <div
  90. dangerouslySetInnerHTML={{
  91. __html: `<div class="pcd_sent" id="sent_${book}-${para}-${wordStart}-${wordEnd}"></div>`,
  92. }}
  93. />
  94. <div style={{ flex: "5", color: "#9f3a01" }} ref={boxOrg}>
  95. {origin?.map((item, id) => {
  96. return (
  97. <MdView
  98. key={id}
  99. style={{ color: "brown" }}
  100. html={item.html}
  101. wordWidget={true}
  102. convertor={paliCode1}
  103. />
  104. );
  105. })}
  106. </div>
  107. <div style={{ flex: "5" }}>
  108. {translation?.map((item, id) => {
  109. if (item.html.indexOf("<hr>") >= 0) console.log(item.html);
  110. return (
  111. <Text key={id}>
  112. <MdView html={item.html} />
  113. </Text>
  114. );
  115. })}
  116. </div>
  117. </div>
  118. );
  119. };
  120. interface IWidgetTerm {
  121. props: string;
  122. }
  123. const Widget = ({ props }: IWidgetTerm) => {
  124. const prop = JSON.parse(atob(props)) as IWidgetSentReadFrame;
  125. return (
  126. <>
  127. <SentReadFrame {...prop} />
  128. </>
  129. );
  130. };
  131. export default Widget;