|
@@ -1,3 +1,5 @@
|
|
|
+import { flattenDeep } from "lodash-es";
|
|
|
+
|
|
|
let res = [];
|
|
|
|
|
|
/**
|
|
@@ -11,17 +13,19 @@ export function toJSON(editor) {
|
|
|
toSection(e);
|
|
|
}
|
|
|
const newRes = [];
|
|
|
- for (let s of res) {
|
|
|
- let newS = [];
|
|
|
- for (const b of s) {
|
|
|
+ for (let section of res) {
|
|
|
+ section = flattenElement(section);
|
|
|
+
|
|
|
+ let newSection = [];
|
|
|
+ for (const b of section) {
|
|
|
const toB = toBlock(b);
|
|
|
- if (toB) newS.push(toB);
|
|
|
+ if (toB) newSection.push(toB);
|
|
|
}
|
|
|
- if (newS.length === 0) {
|
|
|
+ if (newSection.length === 0) {
|
|
|
// 空行特殊处理
|
|
|
- newS = [{ type: "text", value: " ", param: null }];
|
|
|
+ newSection = [{ type: "text", value: " ", param: null }];
|
|
|
}
|
|
|
- newRes.push({ blocks: newS });
|
|
|
+ newRes.push({ blocks: newSection });
|
|
|
}
|
|
|
res = { sections: newRes };
|
|
|
// console.log(res);
|
|
@@ -44,6 +48,49 @@ function toSection(e) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @param {Node[]} section
|
|
|
+ */
|
|
|
+function flattenElement(section) {
|
|
|
+ section = [...section];
|
|
|
+ // console.log(section);
|
|
|
+ for (let i = 0; i < section.length; i++) {
|
|
|
+ if (section[i].hasChildNodes()) {
|
|
|
+ section[i] = [...section[i].childNodes];
|
|
|
+
|
|
|
+ for (let j = 0; j < section[i].length; j++) {
|
|
|
+ if (section[i][j].hasChildNodes()) {
|
|
|
+ section[i][j] = [...section[i][j].childNodes];
|
|
|
+ for (let k = 0; k < section[i][j].length; k++) {
|
|
|
+ if (section[i][j][k].hasChildNodes()) {
|
|
|
+ section[i][j][k] = [...section[i][j][k].childNodes];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ section = flattenDeep(section);
|
|
|
+ return section;
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * @param {Node} e
|
|
|
+ * @param {String} tag
|
|
|
+ */
|
|
|
+function checkAncestorElementTag(e, tag) {
|
|
|
+ for (let i = 0; i < 3; i++) {
|
|
|
+ if (e.parentElement.nodeName === tag) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ e = e.parentElement;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+}
|
|
|
+
|
|
|
/**
|
|
|
*
|
|
|
* @param {Node} e
|
|
@@ -53,7 +100,25 @@ function toBlock(e) {
|
|
|
if (e.nodeType === Node.TEXT_NODE) {
|
|
|
block.type = "text";
|
|
|
block.value = e.textContent;
|
|
|
- block.param = null;
|
|
|
+ block.param = {};
|
|
|
+ // block.param.italic =
|
|
|
+ // window.getComputedStyle(e.parentElement).fontStyle === "italic";
|
|
|
+ // block.param.bold =
|
|
|
+ // window.getComputedStyle(e.parentElement).fontWeight > 400;
|
|
|
+ // block.param.underline =
|
|
|
+ // window.getComputedStyle(e.parentElement).textDecorationLine ===
|
|
|
+ // "underline";
|
|
|
+
|
|
|
+ block.param.italic = checkAncestorElementTag(e, "I");
|
|
|
+ block.param.bold = checkAncestorElementTag(e, "B");
|
|
|
+ block.param.underline = checkAncestorElementTag(e, "U");
|
|
|
+
|
|
|
+ // console.log(block.param);
|
|
|
+ const allFalse = Object.values(block.param).every((v) => v === false);
|
|
|
+ // console.log(allFalse);
|
|
|
+ if (allFalse) {
|
|
|
+ block.param = null;
|
|
|
+ }
|
|
|
// } else if (e.nodeType == Node.ELEMENT_NODE && e.nodeName === "SPAN") {
|
|
|
// block.type = "text";
|
|
|
// block.value = e.textContent;
|