Dictionary.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. setWordInput(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. useEffect(() => {
  55. console.debug("wordInput change", wordInput);
  56. if (wordInput !== wordSearch) {
  57. const currWord = wordChange(wordInput);
  58. setWordSearch(currWord);
  59. }
  60. }, [wordInput]);
  61. const dictSearch = (value: string, isFactor?: boolean) => {
  62. console.info("onSearch", value);
  63. const currWord = wordChange(value);
  64. if (typeof onSearch !== "undefined" && !isFactor) {
  65. onSearch(currWord);
  66. } else {
  67. setWordSearch(currWord);
  68. }
  69. };
  70. return (
  71. <div ref={setContainer}>
  72. <div id="pcd_dict_top"></div>
  73. <Affix offsetTop={0} target={compact ? () => container : undefined}>
  74. <div
  75. style={{
  76. backgroundColor: "rgba(100,100,100,0.3)",
  77. backdropFilter: "blur(5px)",
  78. }}
  79. >
  80. <Row style={{ paddingTop: "0.5em", paddingBottom: "0.5em" }}>
  81. {compact ? <></> : <Col flex="auto"></Col>}
  82. <Col flex="560px">
  83. <div style={{ display: "flex" }}>
  84. <SearchVocabulary
  85. compact={compact}
  86. value={wordInput?.toLowerCase()}
  87. onSearch={dictSearch}
  88. onSplit={(word: string | undefined) => {
  89. console.log("onSplit", word);
  90. setSplit(word);
  91. }}
  92. />
  93. </div>
  94. </Col>
  95. {compact ? <></> : <Col flex="auto"></Col>}
  96. </Row>
  97. </div>
  98. </Affix>
  99. <Content style={{ minHeight: 700 }}>
  100. <Row>
  101. {compact ? <></> : <Col flex="auto"></Col>}
  102. <Col flex="1260px">
  103. {dictType === "dict" ? (
  104. <div>
  105. <Compound word={word} add={split} onSearch={dictSearch} />
  106. <DictSearch word={wordSearch} compact={compact} />
  107. </div>
  108. ) : (
  109. <TermShow
  110. word={wordSearch}
  111. wordId={wordId}
  112. hideInput
  113. onIdChange={(value: string) => {
  114. const newInput = `type:term id:${value}`;
  115. console.debug("term onIdChange setWordInput", newInput);
  116. if (typeof onSearch !== "undefined") {
  117. onSearch(newInput);
  118. } else {
  119. setWordInput(newInput);
  120. }
  121. }}
  122. />
  123. )}
  124. </Col>
  125. {compact ? <></> : <Col flex="auto"></Col>}
  126. </Row>
  127. </Content>
  128. </div>
  129. );
  130. };
  131. export default DictionaryWidget;