ParaShell.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import { useEffect, useState } from "react";
  2. import { useAppSelector } from "../../hooks";
  3. import { currFocus } from "../../reducers/focus";
  4. import { ParaHandleCtl } from "./ParaHandle";
  5. import AiTranslate from "../ai/AiTranslate";
  6. interface IWidgetParaShellCtl {
  7. book: number;
  8. para: number;
  9. mode?: string;
  10. channels?: string[];
  11. sentences: string[];
  12. children?: React.ReactNode | React.ReactNode[];
  13. }
  14. const ParaShellCtl = ({
  15. book,
  16. para,
  17. mode = "read",
  18. channels,
  19. sentences,
  20. children,
  21. }: IWidgetParaShellCtl) => {
  22. const focus = useAppSelector(currFocus);
  23. const [isFocus, setIsFocus] = useState(false);
  24. const [aiTranslateParaId, setAiTranslateParaId] = useState<string>();
  25. useEffect(() => {
  26. if (focus) {
  27. if (focus.focus?.type === "para") {
  28. if (focus.focus.id) {
  29. const arrId = focus.focus.id.split("-");
  30. if (arrId.length > 1) {
  31. const focusBook = parseInt(arrId[0]);
  32. const focusPara = arrId[1].split(",").map((item) => parseInt(item));
  33. if (focusBook === book && focusPara.includes(para)) {
  34. setIsFocus(true);
  35. }
  36. }
  37. } else {
  38. setIsFocus(false);
  39. }
  40. }
  41. } else {
  42. setIsFocus(false);
  43. }
  44. }, [book, focus, para]);
  45. const borderColor = isFocus ? "#e35f00bd " : "rgba(128, 128, 128, 0.3)";
  46. const border = mode === "read" ? "" : "2px solid " + borderColor;
  47. return (
  48. <div
  49. style={{
  50. border: border,
  51. borderRadius: 6,
  52. marginTop: 20,
  53. marginBottom: 28,
  54. padding: 4,
  55. }}
  56. >
  57. <div
  58. style={{
  59. position: "absolute",
  60. marginTop: -31,
  61. marginLeft: -6,
  62. border: border,
  63. borderRadius: "6px",
  64. }}
  65. >
  66. <ParaHandleCtl
  67. book={book}
  68. para={para}
  69. mode={mode}
  70. channels={channels}
  71. sentences={sentences}
  72. onTranslate={() => setAiTranslateParaId(`${book}-${para}`)}
  73. />
  74. </div>
  75. <div>
  76. <AiTranslate autoLoad paragraph={aiTranslateParaId} />
  77. </div>
  78. {children}
  79. </div>
  80. );
  81. };
  82. interface IWidget {
  83. props: string;
  84. children?: React.ReactNode | React.ReactNode[];
  85. }
  86. const Widget = ({ props, children }: IWidget) => {
  87. const prop = JSON.parse(atob(props)) as IWidgetParaShellCtl;
  88. return (
  89. <>
  90. <ParaShellCtl {...prop}>{children}</ParaShellCtl>
  91. </>
  92. );
  93. };
  94. export default Widget;