CaseFormula.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { MoreOutlined } from "@ant-design/icons";
  2. import { Button, Dropdown, type MenuProps } from "antd"
  3. import { useEffect, useState } from "react";
  4. import { useAppSelector } from "../../../hooks";
  5. import type { IWbw } from "./WbwWord"
  6. import { inlineDict as _inlineDict } from "../../../reducers/inline-dict";
  7. interface IWidget {
  8. data?: IWbw;
  9. onChange?: Function;
  10. onCaseChange?: Function;
  11. }
  12. const CaseFormulaWidget = ({ data, onChange, _____onCaseChange }: IWidget) => {
  13. const [formula, setFormula] = useState<MenuProps["items"]>([]);
  14. const inlineDict = useAppSelector(_inlineDict);
  15. useEffect(() => {
  16. if (
  17. typeof data === "undefined" ||
  18. typeof data.case === "undefined" ||
  19. typeof data.case.value !== "string"
  20. ) {
  21. setFormula([]);
  22. return;
  23. }
  24. const _case = data.case?.value?.split("#");
  25. if (_case?.length !== 2) {
  26. setFormula([]);
  27. return;
  28. }
  29. let grammar = _case[1];
  30. if (typeof grammar !== "string" || grammar.length === 0) {
  31. setFormula([]);
  32. return;
  33. }
  34. let result = inlineDict.wordList.filter(
  35. (word) => word.word === "_formula_" && word.grammar === grammar
  36. );
  37. if (result.length === 0) {
  38. //没找到,再次查找
  39. grammar = "*" + grammar.split("$").slice(1).join("$");
  40. result = inlineDict.wordList.filter(
  41. (word) => word.word === "_formula_" && word.grammar === grammar
  42. );
  43. }
  44. let strFormula: string;
  45. if (result.length > 0 && result[0].mean) {
  46. strFormula = result[0].mean;
  47. } else {
  48. strFormula = "{无}";
  49. }
  50. const menu1 = strFormula.split("/").map((item) => item.split("$"));
  51. const items = menu1[0].map((item1) => {
  52. const children = menu1[1]
  53. ? menu1[1].map((item2) => {
  54. let key: string;
  55. let label: string;
  56. if (item1.includes("@")) {
  57. key = item1.replace("@", item2);
  58. label = key;
  59. } else if (item2.includes("@")) {
  60. key = item2.replace("@", item1);
  61. label = key;
  62. } else {
  63. key = item1 + item2;
  64. label = item2;
  65. }
  66. return {
  67. key: key,
  68. label: label.replaceAll("{", "").replaceAll("}", ""),
  69. };
  70. })
  71. : undefined;
  72. return {
  73. key: item1,
  74. label: item1.replace("@", "~").replaceAll("{", "").replaceAll("}", ""),
  75. children: children,
  76. };
  77. });
  78. setFormula(items);
  79. }, [data?.case, inlineDict.wordList]);
  80. return (
  81. <Dropdown
  82. menu={{
  83. items: formula,
  84. onClick: (e) => {
  85. console.log("click ", e.key);
  86. if (typeof onChange !== "undefined") {
  87. onChange(e.key);
  88. }
  89. },
  90. }}
  91. placement="bottomRight"
  92. >
  93. <Button
  94. type="text"
  95. size="small"
  96. icon={<MoreOutlined />}
  97. onClick={(e) => e.preventDefault()}
  98. />
  99. </Dropdown>
  100. );
  101. };
  102. export default CaseFormulaWidget;