RelaGraphic.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { Typography } from "antd";
  2. import { useIntl } from "react-intl";
  3. import Mermaid from "../../general/Mermaid";
  4. import { useAppSelector } from "../../../hooks";
  5. import { getGrammar } from "../../../reducers/term-vocabulary";
  6. import type { IWbwRelation } from "./WbwDetailRelation"
  7. import type { IWbw } from "./WbwWord"
  8. import { relationWordId } from "./WbwRelationAdd";
  9. import store from "../../../store";
  10. import { openPanel } from "../../../reducers/right-panel";
  11. import { grammar } from "../../../reducers/command";
  12. import { fullUrl } from "../../../utils";
  13. const { Text } = Typography;
  14. const pureMeaning = (input: string | null | undefined) => {
  15. return input
  16. ? input
  17. ?.replaceAll("[", "")
  18. .replaceAll("]", "")
  19. .replaceAll("{", "")
  20. .replaceAll("}", "")
  21. : "";
  22. };
  23. interface IWidget {
  24. wbwData?: IWbw[];
  25. }
  26. const RelaGraphicWidget = ({ wbwData }: IWidget) => {
  27. const terms = useAppSelector(getGrammar);
  28. const intl = useIntl();
  29. const _____onLoad = () => {
  30. const links = document.getElementsByTagName("a");
  31. alert(links.length + "links");
  32. // 为每个链接添加点击事件监听器
  33. for (let i = 0; i < links.length; i++) {
  34. (function (index) {
  35. links[index].addEventListener("click", function (e) {
  36. // 阻止链接的默认点击行为
  37. e.preventDefault();
  38. // 在这里执行你想在点击链接时执行的代码
  39. console.log("链接被点击: ", this.href);
  40. alert("链接被点击" + this.href);
  41. const iPos = this.href.lastIndexOf("grammar/");
  42. if (iPos >= 0) {
  43. const word = this.href.substring(iPos + 8);
  44. console.debug("relation graphic", word);
  45. store.dispatch(grammar(word));
  46. store.dispatch(openPanel("grammar"));
  47. } else {
  48. window.location.href = this.href;
  49. }
  50. });
  51. })(i);
  52. }
  53. };
  54. const grammarStr = (input?: string | null) => {
  55. if (!input) {
  56. return "";
  57. }
  58. const g = input.split("#");
  59. const mType = g[0].replaceAll(".", "");
  60. const type = intl.formatMessage({
  61. id: `dict.fields.type.${mType}.short.label`,
  62. defaultMessage: mType,
  63. });
  64. let strGrammar: string[] = [];
  65. if (g.length > 1 && g[1].length > 0) {
  66. strGrammar = g[1].split("$").map((item, _id) => {
  67. const mCase = item.replaceAll(".", "");
  68. return intl.formatMessage({
  69. id: `dict.fields.type.${mCase}.short.label`,
  70. defaultMessage: mCase,
  71. });
  72. });
  73. }
  74. let output = type;
  75. if (strGrammar.length > 0) {
  76. output += `|${strGrammar.join("·")}`;
  77. }
  78. return output;
  79. };
  80. //根据relation 绘制关系图
  81. function sent_show_rel_map(data?: IWbw[]): string {
  82. let mermaid: string = "flowchart LR\n";
  83. const relationWords = data
  84. ?.filter((value) => value.relation)
  85. .map((item) => {
  86. if (item.relation && item.relation.value) {
  87. const json: IWbwRelation[] = JSON.parse(item.relation.value);
  88. const graphic = json.map((relation) => {
  89. const localName = terms?.find(
  90. (item) => item.word === relation.relation
  91. )?.meaning;
  92. const fromMeaning = pureMeaning(item.meaning?.value);
  93. const fromGrammar = grammarStr(item.case?.value);
  94. //查找目标意思
  95. const toWord = data.find(
  96. (value: IWbw) => relationWordId(value) === relation.dest_id
  97. );
  98. const toMeaning = pureMeaning(toWord?.meaning?.value);
  99. const url = fullUrl("/term/list/" + relation.relation);
  100. const toGrammar = grammarStr(toWord?.case?.value);
  101. const strFrom = `${relation.sour_id}("${relation.sour_spell}<br />${fromMeaning}<br />${fromGrammar}")`;
  102. const strRelation = `"<a href='${url}' target='_blank'>${relation.relation}</a><br />${localName}"`;
  103. const strTo = `${relation.dest_id}("${relation.dest_spell}<br />${toMeaning}<br />${toGrammar}")`;
  104. return `${strFrom} --${strRelation}--> ${strTo}\n`;
  105. });
  106. return graphic.join("");
  107. } else {
  108. return "";
  109. }
  110. });
  111. mermaid += relationWords?.join("");
  112. console.log("mermaid", mermaid);
  113. return mermaid;
  114. }
  115. const mermaidText = sent_show_rel_map(wbwData);
  116. return (
  117. <div>
  118. <div style={{ display: "flex", justifyContent: "space-between" }}>
  119. <div>
  120. <Text copyable={{ text: mermaidText, tooltips: "复制mermaid代码" }} />
  121. </div>
  122. <div></div>
  123. </div>
  124. <div>
  125. <Mermaid text={mermaidText} />
  126. </div>
  127. </div>
  128. );
  129. };
  130. export default RelaGraphicWidget;