utilities.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import React from "react";
  2. import MdTpl from "./MdTpl";
  3. import { WdCtl } from "./Wd";
  4. import { Divider } from "antd";
  5. import { roman_to_my, my_to_roman } from "../code/my";
  6. import { roman_to_si } from "../code/si";
  7. import { roman_to_thai } from "../code/thai";
  8. import { roman_to_taitham } from "../code/tai-tham";
  9. export type TCodeConvertor =
  10. | "none"
  11. | "roman"
  12. | "roman_to_my"
  13. | "my_to_roman"
  14. | "roman_to_thai"
  15. | "roman_to_taitham"
  16. | "roman_to_si";
  17. export function XmlToReact(
  18. text: string,
  19. wordWidget: boolean = false,
  20. convertor?: TCodeConvertor
  21. ): React.ReactNode[] {
  22. //console.log("html string:", text);
  23. const parser = new DOMParser();
  24. const xmlDoc = parser.parseFromString(
  25. "<root><root>" + text + "</root></root>",
  26. "text/xml"
  27. );
  28. const x = xmlDoc.documentElement.childNodes;
  29. return convert(x[0], wordWidget, convertor);
  30. function getAttr(node: ChildNode, key: number): Object {
  31. const ele = node as Element;
  32. const attr = ele.attributes;
  33. let output: any = { key: key };
  34. for (let i = 0; i < attr.length; i++) {
  35. if (attr[i].nodeType === 2) {
  36. let key: string = attr[i].nodeName;
  37. output[key] = attr[i].nodeValue;
  38. }
  39. }
  40. return output;
  41. }
  42. function convert(
  43. node: ChildNode,
  44. wordWidget: boolean = false,
  45. convertor?: TCodeConvertor
  46. ): React.ReactNode[] {
  47. let output: React.ReactNode[] = [];
  48. for (let i = 0; i < node.childNodes.length; i++) {
  49. const value = node.childNodes[i];
  50. //console.log(value.nodeName, value.nodeType, value.nodeValue);
  51. switch (value.nodeType) {
  52. case 1: //element node
  53. switch (value.nodeName) {
  54. case "MdTpl":
  55. output.push(
  56. React.createElement(
  57. MdTpl,
  58. getAttr(value, i),
  59. convert(value, wordWidget, convertor)
  60. )
  61. );
  62. break;
  63. case "hr":
  64. output.push(
  65. React.createElement(
  66. Divider,
  67. getAttr(value, i),
  68. convert(value, wordWidget, convertor)
  69. )
  70. );
  71. break;
  72. default:
  73. output.push(
  74. React.createElement(
  75. value.nodeName,
  76. getAttr(value, i),
  77. convert(value, wordWidget, convertor)
  78. )
  79. );
  80. break;
  81. }
  82. break;
  83. case 2: //attribute node
  84. return [];
  85. case 3: //text node
  86. let textValue = value.nodeValue;
  87. //编码转换
  88. if (typeof convertor !== "undefined") {
  89. switch (convertor) {
  90. case "roman_to_my":
  91. textValue = roman_to_my(textValue);
  92. break;
  93. case "my_to_roman":
  94. textValue = my_to_roman(textValue);
  95. break;
  96. case "roman_to_si":
  97. textValue = roman_to_si(textValue);
  98. break;
  99. case "roman_to_thai":
  100. textValue = roman_to_thai(textValue);
  101. break;
  102. case "roman_to_taitham":
  103. textValue = roman_to_taitham(textValue);
  104. break;
  105. }
  106. }
  107. if (wordWidget) {
  108. //将单词按照空格拆开。用组件包裹
  109. const wordList = textValue?.split(" ");
  110. const wordWidget = wordList?.map((word, id) => {
  111. const prop: any = { key: id, text: word };
  112. return React.createElement(WdCtl, prop);
  113. });
  114. output.push(wordWidget);
  115. } else {
  116. output.push(textValue);
  117. }
  118. break;
  119. case 8:
  120. return [];
  121. case 9:
  122. return [];
  123. }
  124. }
  125. return output;
  126. }
  127. }