Dictionary.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { useEffect, useState } from "react";
  2. import { Layout, Affix, Col, Row } from "antd";
  3. import DictSearch from "./DictSearch";
  4. import SearchVocabulary from "./SearchVocabulary";
  5. import Compound from "./Compound";
  6. import TermShow from "../term/TermShow";
  7. const { Content } = Layout;
  8. interface IWidget {
  9. word?: string;
  10. compact?: boolean;
  11. onSearch?: Function;
  12. }
  13. const DictionaryWidget = ({ word, compact = false, onSearch }: IWidget) => {
  14. const [split, setSplit] = useState<string>();
  15. const [wordSearch, setWordSearch] = useState<string>();
  16. const [container, setContainer] = useState<HTMLDivElement | null>(null);
  17. const [dictType, setDictType] = useState("dict");
  18. const [wordInput, setWordInput] = useState(word);
  19. const [wordId, setWordId] = useState<string>();
  20. useEffect(() => {
  21. console.debug("param word change", word);
  22. wordInputChange(word);
  23. }, [word]);
  24. const wordChange = (value?: string) => {
  25. console.debug("has ':'", value, value?.includes(":"));
  26. let currWord: string | undefined = "";
  27. if (value?.includes(":")) {
  28. const param = value.split(" ");
  29. param.forEach((value) => {
  30. const kv = value.split(":");
  31. if (kv.length === 2) {
  32. switch (kv[0]) {
  33. case "type":
  34. setDictType(kv[1]);
  35. break;
  36. case "word":
  37. currWord = kv[1];
  38. break;
  39. case "id":
  40. setWordId(kv[1]);
  41. break;
  42. default:
  43. break;
  44. }
  45. }
  46. });
  47. } else {
  48. setDictType("dict");
  49. currWord = value?.toLowerCase();
  50. }
  51. document.getElementById("pcd_dict_top")?.scrollIntoView();
  52. return currWord;
  53. };
  54. const wordInputChange = (value: string | undefined) => {
  55. setWordInput(value);
  56. console.debug("wordInput change", value);
  57. if (value !== wordSearch) {
  58. const currWord = wordChange(value);
  59. setWordSearch(currWord);
  60. }
  61. };
  62. const dictSearch = (value: string, isFactor?: boolean) => {
  63. console.info("onSearch", value);
  64. const currWord = wordChange(value);
  65. if (typeof onSearch !== "undefined" && !isFactor) {
  66. onSearch(currWord);
  67. }
  68. setWordSearch(currWord);
  69. };
  70. return (
  71. <div ref={setContainer}>
  72. <div id="pcd_dict_top"></div>
  73. <Affix
  74. offsetTop={0}
  75. target={compact ? () => container : undefined}
  76. className="dict_search_div"
  77. >
  78. <div
  79. style={{
  80. backgroundColor: "rgba(100,100,100,0.3)",
  81. backdropFilter: "blur(5px)",
  82. }}
  83. >
  84. <Row style={{ paddingTop: "0.5em", paddingBottom: "0.5em" }}>
  85. {compact ? <></> : <Col flex="auto"></Col>}
  86. <Col flex="560px">
  87. <div style={{ display: "flex" }}>
  88. <SearchVocabulary
  89. compact={compact}
  90. value={wordInput?.toLowerCase()}
  91. onSearch={dictSearch}
  92. onSplit={(word: string | undefined) => {
  93. console.log("onSplit", word);
  94. setSplit(word);
  95. }}
  96. />
  97. </div>
  98. </Col>
  99. {compact ? <></> : <Col flex="auto"></Col>}
  100. </Row>
  101. </div>
  102. </Affix>
  103. <Content style={{ minHeight: 700 }}>
  104. <Row>
  105. {compact ? <></> : <Col flex="auto"></Col>}
  106. <Col flex="1260px">
  107. {dictType === "dict" ? (
  108. <div>
  109. <Compound word={word} add={split} onSearch={dictSearch} />
  110. <DictSearch word={wordSearch} compact={compact} />
  111. </div>
  112. ) : (
  113. <TermShow
  114. word={wordSearch}
  115. wordId={wordId}
  116. hideInput
  117. onIdChange={(value: string) => {
  118. const newInput = `type:term id:${value}`;
  119. console.debug("term onIdChange setWordInput", newInput);
  120. if (typeof onSearch !== "undefined") {
  121. onSearch(newInput);
  122. } else {
  123. wordInputChange(newInput);
  124. }
  125. }}
  126. />
  127. )}
  128. </Col>
  129. {compact ? <></> : <Col flex="auto"></Col>}
  130. </Row>
  131. </Content>
  132. </div>
  133. );
  134. };
  135. export default DictionaryWidget;