WbwWord.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. import { useState } from "react";
  2. import WbwCase from "./WbwCase";
  3. import { bookMarkColor } from "./WbwDetailBookMark";
  4. import WbwFactorMeaning from "./WbwFactorMeaning";
  5. import WbwFactors from "./WbwFactors";
  6. import WbwMeaning from "./WbwMeaning";
  7. import WbwPali from "./WbwPali";
  8. import WbwWord from "./WbwWord";
  9. import "./wbw.css";
  10. export type TFieldName =
  11. | "word"
  12. | "real"
  13. | "meaning"
  14. | "type"
  15. | "grammar"
  16. | "case"
  17. | "parent"
  18. | "factors"
  19. | "factorMeaning"
  20. | "relation"
  21. | "note"
  22. | "bookMarkColor"
  23. | "bookMarkText"
  24. | "locked"
  25. | "confidence";
  26. export interface IWbwField {
  27. field: TFieldName;
  28. value: string;
  29. }
  30. enum WbwStatus {
  31. initiate = 0,
  32. auto = 3,
  33. manual = 5,
  34. }
  35. interface WbwElement {
  36. value: string;
  37. status: WbwStatus;
  38. }
  39. interface WbwElement2 {
  40. value: string[];
  41. status: WbwStatus;
  42. }
  43. interface WbwElement3 {
  44. value: number;
  45. status: WbwStatus;
  46. }
  47. export interface IWbw {
  48. word: WbwElement;
  49. real?: WbwElement;
  50. meaning?: WbwElement2;
  51. type?: WbwElement;
  52. grammar?: WbwElement;
  53. case?: WbwElement2;
  54. parent?: WbwElement;
  55. factors?: WbwElement;
  56. factorMeaning?: WbwElement;
  57. relation?: WbwElement;
  58. note?: WbwElement;
  59. bookMarkColor?: WbwElement3;
  60. bookMarkText?: WbwElement;
  61. locked?: boolean;
  62. confidence: number;
  63. }
  64. export interface IWbwFields {
  65. meaning?: boolean;
  66. factors?: boolean;
  67. factorMeaning?: boolean;
  68. case?: boolean;
  69. }
  70. interface IWidget {
  71. data: IWbw;
  72. display?: "block" | "inline";
  73. fields?: IWbwFields;
  74. onChange?: Function;
  75. onSplit?: Function;
  76. }
  77. const Widget = ({
  78. data,
  79. display = "block",
  80. fields = { meaning: true, factors: true, factorMeaning: true, case: true },
  81. onChange,
  82. onSplit,
  83. }: IWidget) => {
  84. const [wordData, setWordData] = useState(data);
  85. const styleWbw: React.CSSProperties = {
  86. display: display === "block" ? "block" : "flex",
  87. };
  88. const color = wordData.bookMarkColor
  89. ? bookMarkColor[wordData.bookMarkColor.value]
  90. : "unset";
  91. return (
  92. <div className={`wbw_word ${display}`} style={styleWbw}>
  93. <WbwPali
  94. data={wordData}
  95. onSave={(e: IWbw) => {
  96. console.log("save", e);
  97. const newData: IWbw = JSON.parse(JSON.stringify(e));
  98. setWordData(newData);
  99. if (typeof onChange !== "undefined") {
  100. onChange(e);
  101. }
  102. }}
  103. />
  104. <div
  105. className="wbw_body"
  106. style={{
  107. background: `linear-gradient(90deg, rgba(255, 255, 255, 0), ${color})`,
  108. }}
  109. >
  110. {fields?.meaning ? (
  111. <WbwMeaning
  112. data={wordData}
  113. onChange={(e: string) => {
  114. console.log("meaning change", e);
  115. const newData: IWbw = JSON.parse(JSON.stringify(wordData));
  116. newData.meaning = { value: [e], status: 5 };
  117. setWordData(newData);
  118. }}
  119. />
  120. ) : undefined}
  121. {fields?.factors ? <WbwFactors data={wordData} /> : undefined}
  122. {fields?.factorMeaning ? (
  123. <WbwFactorMeaning data={wordData} />
  124. ) : undefined}
  125. {fields?.case ? (
  126. <WbwCase
  127. data={wordData}
  128. onSplit={() => {
  129. console.log("onSplit", wordData.factors?.value);
  130. if (typeof onSplit !== "undefined") {
  131. onSplit();
  132. }
  133. }}
  134. />
  135. ) : undefined}
  136. </div>
  137. </div>
  138. );
  139. };
  140. export default Widget;