WbwCase.tsx 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import { useEffect, useState } from "react";
  2. import { type IntlShape, useIntl } from "react-intl";
  3. import { Typography, Button, Space } from "antd";
  4. import { SwapOutlined } from "@ant-design/icons";
  5. import type { MenuProps } from "antd";
  6. import { Dropdown } from "antd";
  7. import { LoadingOutlined, FileExclamationOutlined } from "@ant-design/icons";
  8. import type { IWbw, TWbwDisplayMode } from "./WbwWord";
  9. import "./wbw.css";
  10. import { useAppSelector } from "../../../hooks";
  11. import { inlineDict as _inlineDict } from "../../../reducers/inline-dict";
  12. import WbwParent2 from "./WbwParent2";
  13. import type { IApiResponseDictData } from "../../../api/Dict";
  14. import { errorClass } from "./WbwMeaning";
  15. import WbwParentIcon from "./WbwParentIcon";
  16. export interface ValueType {
  17. key: string;
  18. label: string;
  19. }
  20. const { Text } = Typography;
  21. export const caseInDict = (
  22. wordIn: string,
  23. wordIndex: string[],
  24. wordList: IApiResponseDictData[],
  25. intl: IntlShape
  26. ): MenuProps["items"] => {
  27. if (!wordIn) {
  28. return [];
  29. }
  30. if (wordIndex.includes(wordIn)) {
  31. const result = wordList.filter((word) => word.word === wordIn);
  32. //查重
  33. //TODO 加入信心指数并排序
  34. const myMap = new Map<string, number>();
  35. const factors: string[] = [];
  36. for (const iterator of result) {
  37. myMap.set(iterator.type + "#" + iterator.grammar, 1);
  38. }
  39. myMap.forEach((_value, key, _map) => {
  40. factors.push(key);
  41. });
  42. const menu = factors.map((item) => {
  43. const arrItem: string[] = item
  44. .replaceAll(".", "")
  45. .replaceAll("#", "$")
  46. .split("$");
  47. const noNull = arrItem.filter((item) => item !== "");
  48. noNull.forEach((item, index, arr) => {
  49. arr[index] = intl.formatMessage({
  50. id: `dict.fields.type.${item}.short.label`,
  51. defaultMessage: item,
  52. });
  53. });
  54. return { key: item, label: noNull.join(" ") };
  55. });
  56. if (menu.length > 0) {
  57. return menu;
  58. } else {
  59. return [
  60. {
  61. key: "",
  62. disabled: true,
  63. label: (
  64. <>
  65. <FileExclamationOutlined />{" "}
  66. {intl.formatMessage({
  67. id: "labels.empty",
  68. })}
  69. </>
  70. ),
  71. },
  72. ];
  73. }
  74. } else {
  75. return [
  76. {
  77. key: "",
  78. disabled: true,
  79. label: (
  80. <>
  81. <LoadingOutlined />{" "}
  82. {intl.formatMessage({
  83. id: "labels.loading",
  84. })}
  85. </>
  86. ),
  87. },
  88. ];
  89. }
  90. };
  91. interface IWidget {
  92. data: IWbw;
  93. answer?: IWbw;
  94. display?: TWbwDisplayMode;
  95. onSplit?: Function;
  96. onChange?: Function;
  97. }
  98. const WbwCaseWidget = ({
  99. data,
  100. answer,
  101. display,
  102. onSplit,
  103. onChange,
  104. }: IWidget) => {
  105. const intl = useIntl();
  106. const defaultMenu: MenuProps["items"] = [
  107. {
  108. key: "loading",
  109. label: (
  110. <Space>
  111. <LoadingOutlined />
  112. {"Loading"}
  113. </Space>
  114. ),
  115. },
  116. ];
  117. const [items, setItems] = useState<MenuProps["items"]>(defaultMenu);
  118. const inlineDict = useAppSelector(_inlineDict);
  119. useEffect(() => {
  120. if (!data.real.value) {
  121. return;
  122. }
  123. setItems(
  124. caseInDict(
  125. data.real.value,
  126. inlineDict.wordIndex,
  127. inlineDict.wordList,
  128. intl
  129. )
  130. );
  131. }, [data.real.value, inlineDict, intl]);
  132. const onClick: MenuProps["onClick"] = (e) => {
  133. console.log("click ", e);
  134. if (typeof onChange !== "undefined") {
  135. onChange(e.key);
  136. }
  137. };
  138. const showSplit: boolean = data.factors?.value?.includes("+") ? true : false;
  139. let caseElement: JSX.Element | JSX.Element[] | undefined;
  140. if (display === "block") {
  141. if (
  142. typeof data.case?.value === "string" &&
  143. data.case.value?.trim().length > 0
  144. ) {
  145. caseElement = data.case.value
  146. .replace("#", "$")
  147. .split("$")
  148. .map((item, id) => {
  149. if (item !== "") {
  150. const strCase = item.replaceAll(".", "");
  151. return (
  152. <span key={id} className="case">
  153. {intl.formatMessage({
  154. id: `dict.fields.type.${strCase}.short.label`,
  155. defaultMessage: strCase,
  156. })}
  157. </span>
  158. );
  159. } else {
  160. return <span key={id}>-</span>;
  161. }
  162. });
  163. } else {
  164. //空白的语法信息在逐词解析模式显示占位字符串
  165. caseElement = (
  166. <span>{intl.formatMessage({ id: "forms.fields.case.label" })}</span>
  167. );
  168. }
  169. }
  170. if (
  171. typeof data.real?.value === "string" &&
  172. data.real.value.trim().length > 0
  173. ) {
  174. //非标点符号
  175. const checkClass = answer
  176. ? errorClass("case", data.case?.value, answer?.case?.value)
  177. : "";
  178. return (
  179. <div className={"wbw_word_item"} style={{ display: "flex" }}>
  180. <Text type="secondary">
  181. <div>
  182. <span className={checkClass}>
  183. <Dropdown
  184. key="dropdown"
  185. menu={{ items, onClick }}
  186. placement="bottomLeft"
  187. >
  188. <span>{caseElement}</span>
  189. </Dropdown>
  190. </span>
  191. <WbwParentIcon data={data} answer={answer} />
  192. <WbwParent2 data={data} />
  193. {showSplit ? (
  194. <Button
  195. key="button"
  196. className="wbw_split"
  197. size="small"
  198. shape="circle"
  199. icon={<SwapOutlined />}
  200. onClick={() => {
  201. if (typeof onSplit !== "undefined") {
  202. onSplit(true);
  203. }
  204. }}
  205. />
  206. ) : undefined}
  207. </div>
  208. </Text>
  209. </div>
  210. );
  211. } else {
  212. //标点符号
  213. return <></>;
  214. }
  215. };
  216. export default WbwCaseWidget;