toJSON.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import { flattenDeep } from "lodash-es";
  2. let res = [];
  3. /**
  4. *
  5. * @param {HTMLDivElement} editor
  6. */
  7. export function toJSON(editor) {
  8. // console.log(editor.innerHTML);
  9. res = [];
  10. for (const e of [...editor.childNodes]) {
  11. toSection(e);
  12. }
  13. const newRes = [];
  14. for (let section of res) {
  15. section = flattenElement(section);
  16. let newSection = [];
  17. for (const b of section) {
  18. const toB = toBlock(b);
  19. if (toB) newSection.push(toB);
  20. }
  21. if (newSection.length === 0) {
  22. // 空行特殊处理
  23. newSection = [{ type: "text", value: " ", param: null }];
  24. }
  25. newRes.push({ blocks: newSection });
  26. }
  27. res = { sections: newRes };
  28. // console.log(res);
  29. // console.log(JSON.stringify(res));
  30. // console.log(JSON.stringify(res, null, 2));
  31. return JSON.stringify(res, null, 2);
  32. }
  33. /**
  34. *
  35. * @param {Node} e
  36. */
  37. function toSection(e) {
  38. if (e.nodeType == Node.ELEMENT_NODE && e.nodeName === "DIV") {
  39. res.push(e.childNodes);
  40. } else if (e.nodeType == Node.TEXT_NODE) {
  41. res.push([e]);
  42. } else {
  43. console.log("toSection: 非div, 非TEXT", e);
  44. }
  45. }
  46. /**
  47. *
  48. * @param {Node[]} section
  49. */
  50. function flattenElement(section) {
  51. section = [...section];
  52. // console.log(section);
  53. for (let i = 0; i < section.length; i++) {
  54. if (section[i].hasChildNodes()) {
  55. section[i] = [...section[i].childNodes];
  56. for (let j = 0; j < section[i].length; j++) {
  57. if (section[i][j].hasChildNodes()) {
  58. section[i][j] = [...section[i][j].childNodes];
  59. for (let k = 0; k < section[i][j].length; k++) {
  60. if (section[i][j][k].hasChildNodes()) {
  61. section[i][j][k] = [...section[i][j][k].childNodes];
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. section = flattenDeep(section);
  69. return section;
  70. }
  71. /**
  72. *
  73. * @param {Node} e
  74. * @param {String} tag
  75. */
  76. function checkAncestorElementTag(e, tag) {
  77. for (let i = 0; i < 3; i++) {
  78. if (e.parentElement.nodeName === tag) {
  79. return true;
  80. } else {
  81. e = e.parentElement;
  82. }
  83. }
  84. return false;
  85. }
  86. /**
  87. *
  88. * @param {Node} e
  89. */
  90. function toBlock(e) {
  91. let block = {};
  92. if (e.nodeType === Node.TEXT_NODE) {
  93. block.type = "text";
  94. block.value = e.textContent;
  95. block.param = {};
  96. // block.param.italic =
  97. // window.getComputedStyle(e.parentElement).fontStyle === "italic";
  98. // block.param.bold =
  99. // window.getComputedStyle(e.parentElement).fontWeight > 400;
  100. // block.param.underline =
  101. // window.getComputedStyle(e.parentElement).textDecorationLine ===
  102. // "underline";
  103. block.param.italic = checkAncestorElementTag(e, "I");
  104. block.param.bold = checkAncestorElementTag(e, "B");
  105. block.param.underline = checkAncestorElementTag(e, "U");
  106. // console.log(block.param);
  107. const allFalse = Object.values(block.param).every((v) => v === false);
  108. // console.log(allFalse);
  109. if (allFalse) {
  110. block.param = null;
  111. }
  112. // } else if (e.nodeType == Node.ELEMENT_NODE && e.nodeName === "SPAN") {
  113. // block.type = "text";
  114. // block.value = e.textContent;
  115. // block.param = {};
  116. } else if (e.nodeType == Node.ELEMENT_NODE && e.nodeName === "U") {
  117. block.type = "text";
  118. block.value = e.textContent;
  119. block.param = { underline: true };
  120. } else if (e.nodeType == Node.ELEMENT_NODE && e.nodeName === "B") {
  121. block.type = "text";
  122. block.value = e.textContent;
  123. block.param = { bold: true };
  124. } else if (e.nodeType == Node.ELEMENT_NODE && e.nodeName === "I") {
  125. block.type = "text";
  126. block.value = e.textContent;
  127. block.param = { italic: true };
  128. } else if (e.nodeType == Node.ELEMENT_NODE && e.nodeName === "IMG") {
  129. block.type = "image";
  130. block.value = e.src;
  131. block.param = { width: e.width, height: e.height };
  132. } else if (e.nodeType == Node.ELEMENT_NODE && e.nodeName === "BR") {
  133. block.type = "text";
  134. block.value = "";
  135. block.param = null;
  136. } else {
  137. console.log("toBlock: 非法", e);
  138. }
  139. if (block.type === "text" && block.value === "") {
  140. block = null; // 返回null的block,从json中剔除
  141. }
  142. return block;
  143. }