SentContent.tsx 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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, useState } from "react";
  7. import { GetUserSetting } from "../../auth/setting/default";
  8. import { mode } from "../../../reducers/article-mode";
  9. import { IWbw } from "../Wbw/WbwWord";
  10. interface ILayoutFlex {
  11. left: number;
  12. right: number;
  13. }
  14. type TDirection = "row" | "column";
  15. interface IWidgetSentContent {
  16. sid?: string;
  17. book: number;
  18. para: number;
  19. wordStart: number;
  20. wordEnd: number;
  21. origin?: ISentence[];
  22. translation?: ISentence[];
  23. layout?: TDirection;
  24. onWbwChange?: Function;
  25. }
  26. const Widget = ({
  27. sid,
  28. book,
  29. para,
  30. wordStart,
  31. wordEnd,
  32. origin,
  33. translation,
  34. layout = "column",
  35. onWbwChange,
  36. }: IWidgetSentContent) => {
  37. const [layoutDirection, setLayoutDirection] = useState<TDirection>(layout);
  38. const [layoutFlex, setLayoutFlex] = useState<ILayoutFlex>({
  39. left: 5,
  40. right: 5,
  41. });
  42. const settings = useAppSelector(settingInfo);
  43. useEffect(() => {
  44. const layoutDirection = GetUserSetting(
  45. "setting.layout.direction",
  46. settings
  47. );
  48. if (typeof layoutDirection === "string") {
  49. setLayoutDirection(layoutDirection as TDirection);
  50. }
  51. }, [settings]);
  52. const newMode = useAppSelector(mode);
  53. useEffect(() => {
  54. switch (newMode) {
  55. case "edit":
  56. setLayoutFlex({
  57. left: 5,
  58. right: 5,
  59. });
  60. break;
  61. case "wbw":
  62. setLayoutFlex({
  63. left: 7,
  64. right: 3,
  65. });
  66. break;
  67. }
  68. }, [newMode]);
  69. return (
  70. <div
  71. style={{
  72. display: "flex",
  73. flexDirection: layoutDirection,
  74. marginBottom: 10,
  75. }}
  76. >
  77. <div
  78. dangerouslySetInnerHTML={{
  79. __html: `<div class="pcd_sent" id="sent_${sid}"></div>`,
  80. }}
  81. />
  82. <div style={{ flex: layoutFlex.left, color: "#9f3a01" }}>
  83. {origin?.map((item, id) => {
  84. if (item.channel.type === "wbw") {
  85. return (
  86. <WbwSentCtl
  87. key={id}
  88. book={book}
  89. para={para}
  90. channelId={item.channel.id}
  91. data={JSON.parse(item.content)}
  92. onChange={(data: IWbw[]) => {
  93. if (typeof onWbwChange !== "undefined") {
  94. onWbwChange(data);
  95. }
  96. }}
  97. />
  98. );
  99. } else {
  100. return <SentCell key={id} data={item} wordWidget={true} />;
  101. }
  102. })}
  103. </div>
  104. <div style={{ flex: layoutFlex.right }}>
  105. {translation?.map((item, id) => {
  106. return <SentCell key={id} data={item} />;
  107. })}
  108. </div>
  109. </div>
  110. );
  111. };
  112. export default Widget;