SentContent.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. import { ISentence } from "../SentEdit";
  2. import SentCell from "./SentCell";
  3. import { WbwSentCtl } from "../WbwSent";
  4. import { useAppSelector } from "../../../hooks";
  5. import { settingInfo } from "../../../reducers/setting";
  6. import { useEffect, useLayoutEffect, useRef, useState } from "react";
  7. import { GetUserSetting } from "../../auth/setting/default";
  8. import { mode as _mode } from "../../../reducers/article-mode";
  9. import { IWbw } from "../Wbw/WbwWord";
  10. import { ArticleMode } from "../../article/Article";
  11. import SuggestionFocus from "./SuggestionFocus";
  12. interface ILayoutFlex {
  13. left: number;
  14. right: number;
  15. }
  16. type TDirection = "row" | "column";
  17. interface IWidgetSentContent {
  18. sid?: string;
  19. book: number;
  20. para: number;
  21. wordStart: number;
  22. wordEnd: number;
  23. origin?: ISentence[];
  24. translation?: ISentence[];
  25. layout?: TDirection;
  26. magicDict?: string;
  27. compact?: boolean;
  28. mode?: ArticleMode;
  29. onWbwChange?: Function;
  30. onMagicDictDone?: Function;
  31. }
  32. const SentContentWidget = ({
  33. sid,
  34. book,
  35. para,
  36. wordStart,
  37. wordEnd,
  38. origin,
  39. translation,
  40. layout = "column",
  41. compact = false,
  42. mode,
  43. magicDict,
  44. onWbwChange,
  45. onMagicDictDone,
  46. }: IWidgetSentContent) => {
  47. const [layoutDirection, setLayoutDirection] = useState<TDirection>(layout);
  48. const [layoutFlex, setLayoutFlex] = useState<ILayoutFlex>({
  49. left: 5,
  50. right: 5,
  51. });
  52. const divShell = useRef<HTMLDivElement>(null);
  53. const settings = useAppSelector(settingInfo);
  54. const [divShellWidth, setDivShellWidth] = useState<number>();
  55. useEffect(() => {
  56. const width = divShell.current?.offsetWidth;
  57. if (width && width < 550) {
  58. setLayoutDirection("column");
  59. return;
  60. }
  61. const layoutDirection = GetUserSetting(
  62. "setting.layout.direction",
  63. settings
  64. );
  65. if (typeof layoutDirection === "string") {
  66. setLayoutDirection(layoutDirection as TDirection);
  67. }
  68. }, [settings]);
  69. const newMode = useAppSelector(_mode);
  70. useEffect(() => {
  71. let currMode: ArticleMode;
  72. if (typeof mode !== "undefined") {
  73. currMode = mode;
  74. } else if (typeof newMode !== "undefined") {
  75. currMode = newMode;
  76. } else {
  77. return;
  78. }
  79. switch (currMode) {
  80. case "edit":
  81. setLayoutFlex({
  82. left: 5,
  83. right: 5,
  84. });
  85. break;
  86. case "wbw":
  87. setLayoutFlex({
  88. left: 7,
  89. right: 3,
  90. });
  91. break;
  92. }
  93. }, [mode, newMode]);
  94. useLayoutEffect(() => {
  95. const width = divShell.current?.offsetWidth;
  96. setDivShellWidth(width);
  97. if (width && width < 550) {
  98. setLayoutDirection("column");
  99. return;
  100. }
  101. }, []);
  102. return (
  103. <div
  104. ref={divShell}
  105. style={{
  106. display: "flex",
  107. flexDirection: layoutDirection,
  108. marginBottom: 0,
  109. }}
  110. >
  111. <div
  112. dangerouslySetInnerHTML={{
  113. __html: `<div class="pcd_sent" id="sent_${sid}"></div>`,
  114. }}
  115. />
  116. <div style={{ flex: layoutFlex.left, color: "#9f3a01" }}>
  117. {origin?.map((item, id) => {
  118. if (item.contentType === "json") {
  119. return (
  120. <WbwSentCtl
  121. key={id}
  122. book={book}
  123. para={para}
  124. wordStart={wordStart}
  125. wordEnd={wordEnd}
  126. magicDict={magicDict}
  127. channelId={item.channel.id}
  128. data={JSON.parse(item.content ? item.content : "")}
  129. mode={mode}
  130. onChange={(data: IWbw[]) => {
  131. if (typeof onWbwChange !== "undefined") {
  132. onWbwChange(data);
  133. }
  134. }}
  135. onMagicDictDone={() => {
  136. if (typeof onMagicDictDone !== "undefined") {
  137. onMagicDictDone();
  138. }
  139. }}
  140. />
  141. );
  142. } else {
  143. return <SentCell key={id} initValue={item} wordWidget={true} />;
  144. }
  145. })}
  146. </div>
  147. <div style={{ flex: layoutFlex.right }}>
  148. {translation?.map((item, id) => {
  149. return (
  150. <SuggestionFocus
  151. book={item.book}
  152. para={item.para}
  153. start={item.wordStart}
  154. end={item.wordEnd}
  155. channelId={item.channel.id}
  156. >
  157. <SentCell key={id} initValue={item} compact={compact} />
  158. </SuggestionFocus>
  159. );
  160. })}
  161. </div>
  162. </div>
  163. );
  164. };
  165. export default SentContentWidget;