CaseFormula.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { MoreOutlined } from "@ant-design/icons";
  2. import { Button, Dropdown, type MenuProps } from "antd";
  3. import { useMemo } from "react";
  4. import { useAppSelector } from "../../hooks";
  5. import type { IWbw } from "../../types/wbw";
  6. import { inlineDict as _inlineDict } from "../../reducers/inline-dict";
  7. interface IWidget {
  8. data?: IWbw;
  9. onChange?: (key: string) => void;
  10. }
  11. const CaseFormulaWidget = ({ data, onChange }: IWidget) => {
  12. const inlineDict = useAppSelector(_inlineDict);
  13. const formula = useMemo<MenuProps["items"]>(() => {
  14. if (!data?.case?.value) return [];
  15. const _case = data.case.value.split("#");
  16. if (_case.length !== 2) return [];
  17. let grammar = _case[1];
  18. if (!grammar) return [];
  19. let result = inlineDict.wordList.filter(
  20. (word) => word.word === "_formula_" && word.grammar === grammar
  21. );
  22. if (result.length === 0) {
  23. grammar = "*" + grammar.split("$").slice(1).join("$");
  24. result = inlineDict.wordList.filter(
  25. (word) => word.word === "_formula_" && word.grammar === grammar
  26. );
  27. }
  28. const strFormula =
  29. result.length > 0 && result[0].mean ? result[0].mean : "{无}";
  30. const menu1 = strFormula.split("/").map((item) => item.split("$"));
  31. return menu1[0].map((item1) => {
  32. const children = menu1[1]
  33. ? menu1[1].map((item2) => {
  34. let key: string;
  35. let label: string;
  36. if (item1.includes("@")) {
  37. key = item1.replace("@", item2);
  38. label = key;
  39. } else if (item2.includes("@")) {
  40. key = item2.replace("@", item1);
  41. label = key;
  42. } else {
  43. key = item1 + item2;
  44. label = item2;
  45. }
  46. return {
  47. key,
  48. label: label.replaceAll("{", "").replaceAll("}", ""),
  49. };
  50. })
  51. : undefined;
  52. return {
  53. key: item1,
  54. label: item1.replace("@", "~").replaceAll("{", "").replaceAll("}", ""),
  55. children,
  56. };
  57. });
  58. }, [data, inlineDict.wordList]);
  59. return (
  60. <Dropdown
  61. menu={{
  62. items: formula,
  63. onClick: (e) => {
  64. onChange?.(e.key);
  65. },
  66. }}
  67. placement="bottomRight"
  68. >
  69. <Button
  70. type="text"
  71. size="small"
  72. icon={<MoreOutlined />}
  73. onClick={(e) => e.preventDefault()}
  74. />
  75. </Dropdown>
  76. );
  77. };
  78. export default CaseFormulaWidget;