md-editor-v3 5.4.0 → 5.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1267,22 +1267,57 @@ const contentProps = {
1267
1267
  type: String
1268
1268
  }
1269
1269
  };
1270
- const splitHtml = (html) => {
1270
+ const splitNodes = (html) => {
1271
1271
  const parser = new DOMParser();
1272
1272
  const doc = parser.parseFromString(html, "text/html");
1273
- return Array.from(doc.body.children);
1273
+ return Array.from(doc.body.childNodes);
1274
1274
  };
1275
1275
  const compareHtml = (newHtml, currentHtml) => {
1276
- const newNodes = splitHtml(newHtml);
1277
- const currentNodes = splitHtml(currentHtml);
1278
- return newNodes.map((node, index2) => {
1279
- var _a;
1280
- return {
1281
- index: index2,
1282
- newNode: node.outerHTML,
1283
- isUpdated: ((_a = currentNodes[index2]) == null ? void 0 : _a.outerHTML) !== node.outerHTML
1284
- };
1285
- }).filter((update) => update.isUpdated);
1276
+ const newNodes = splitNodes(newHtml);
1277
+ const currentNodes = splitNodes(currentHtml);
1278
+ const updates = [];
1279
+ const deletes = [];
1280
+ newNodes.forEach((newNode, index2) => {
1281
+ const currentNode = currentNodes[index2];
1282
+ if (!currentNode) {
1283
+ updates.push({
1284
+ index: index2,
1285
+ newNode: newNode.nodeType === 3 ? newNode.textContent || "" : newNode.outerHTML,
1286
+ isText: newNode.nodeType === 3
1287
+ });
1288
+ } else if (newNode.nodeType === 3 && currentNode.nodeType === 3) {
1289
+ if (newNode.textContent !== currentNode.textContent) {
1290
+ updates.push({
1291
+ index: index2,
1292
+ newNode: newNode.textContent || "",
1293
+ isText: true
1294
+ });
1295
+ }
1296
+ } else if (newNode.nodeType !== 3 && currentNode.nodeType !== 3) {
1297
+ if (currentNode.outerHTML !== newNode.outerHTML) {
1298
+ updates.push({
1299
+ index: index2,
1300
+ newNode: newNode.outerHTML,
1301
+ isText: false
1302
+ });
1303
+ }
1304
+ } else {
1305
+ updates.push({
1306
+ index: index2,
1307
+ newNode: newNode.nodeType === 3 ? newNode.textContent || "" : newNode.outerHTML,
1308
+ isText: newNode.nodeType === 3
1309
+ });
1310
+ }
1311
+ });
1312
+ if (currentNodes.length > newNodes.length) {
1313
+ for (let i = newNodes.length; i < currentNodes.length; i++) {
1314
+ deletes.push(i);
1315
+ }
1316
+ }
1317
+ return {
1318
+ updates,
1319
+ deletes
1320
+ };
1286
1321
  };
1287
1322
  const UpdateOnDemand = /* @__PURE__ */ vue.defineComponent({
1288
1323
  name: "UpdateOnDemand",
@@ -1298,23 +1333,43 @@ const UpdateOnDemand = /* @__PURE__ */ vue.defineComponent({
1298
1333
  const showCodeRowNumber = vue.inject("showCodeRowNumber");
1299
1334
  const htmlContainer = vue.ref();
1300
1335
  const firstHtml = vue.ref(props.html);
1301
- const updateHtmlContent = (updates) => {
1336
+ const updateHtmlContent = (updates, deletes) => {
1337
+ if (!htmlContainer.value) return;
1338
+ deletes.reverse().forEach((index2) => {
1339
+ var _a, _b;
1340
+ (_b = (_a = htmlContainer.value) == null ? void 0 : _a.childNodes[index2]) == null ? void 0 : _b.remove();
1341
+ });
1302
1342
  updates.forEach(({
1303
1343
  index: index2,
1304
- newNode
1344
+ newNode,
1345
+ isText
1305
1346
  }) => {
1306
- var _a, _b;
1307
- const targetNode = (_a = htmlContainer.value) == null ? void 0 : _a.children[index2];
1347
+ var _a;
1348
+ const targetNode = (_a = htmlContainer.value) == null ? void 0 : _a.childNodes[index2];
1308
1349
  if (targetNode) {
1309
- targetNode.outerHTML = newNode;
1350
+ if (isText) {
1351
+ targetNode.textContent = newNode;
1352
+ } else {
1353
+ targetNode.outerHTML = newNode;
1354
+ }
1310
1355
  } else {
1311
- (_b = htmlContainer.value) == null ? void 0 : _b.insertAdjacentHTML("beforeend", newNode);
1356
+ if (htmlContainer.value) {
1357
+ if (isText) {
1358
+ const textNode = document.createTextNode(newNode);
1359
+ htmlContainer.value.appendChild(textNode);
1360
+ } else {
1361
+ htmlContainer.value.insertAdjacentHTML("beforeend", newNode);
1362
+ }
1363
+ }
1312
1364
  }
1313
1365
  });
1314
1366
  };
1315
1367
  vue.watch(() => props.html, (newHtml, oldHtml) => {
1316
- const updates = compareHtml(newHtml, oldHtml || "");
1317
- updateHtmlContent(updates);
1368
+ const {
1369
+ updates,
1370
+ deletes
1371
+ } = compareHtml(newHtml, oldHtml || "");
1372
+ updateHtmlContent(updates, deletes);
1318
1373
  });
1319
1374
  return () => vue.createVNode("div", {
1320
1375
  "id": `${editorId}-preview`,
@@ -1266,22 +1266,57 @@ const contentProps = {
1266
1266
  type: String
1267
1267
  }
1268
1268
  };
1269
- const splitHtml = (html) => {
1269
+ const splitNodes = (html) => {
1270
1270
  const parser = new DOMParser();
1271
1271
  const doc = parser.parseFromString(html, "text/html");
1272
- return Array.from(doc.body.children);
1272
+ return Array.from(doc.body.childNodes);
1273
1273
  };
1274
1274
  const compareHtml = (newHtml, currentHtml) => {
1275
- const newNodes = splitHtml(newHtml);
1276
- const currentNodes = splitHtml(currentHtml);
1277
- return newNodes.map((node, index) => {
1278
- var _a;
1279
- return {
1280
- index,
1281
- newNode: node.outerHTML,
1282
- isUpdated: ((_a = currentNodes[index]) == null ? void 0 : _a.outerHTML) !== node.outerHTML
1283
- };
1284
- }).filter((update) => update.isUpdated);
1275
+ const newNodes = splitNodes(newHtml);
1276
+ const currentNodes = splitNodes(currentHtml);
1277
+ const updates = [];
1278
+ const deletes = [];
1279
+ newNodes.forEach((newNode, index) => {
1280
+ const currentNode = currentNodes[index];
1281
+ if (!currentNode) {
1282
+ updates.push({
1283
+ index,
1284
+ newNode: newNode.nodeType === 3 ? newNode.textContent || "" : newNode.outerHTML,
1285
+ isText: newNode.nodeType === 3
1286
+ });
1287
+ } else if (newNode.nodeType === 3 && currentNode.nodeType === 3) {
1288
+ if (newNode.textContent !== currentNode.textContent) {
1289
+ updates.push({
1290
+ index,
1291
+ newNode: newNode.textContent || "",
1292
+ isText: true
1293
+ });
1294
+ }
1295
+ } else if (newNode.nodeType !== 3 && currentNode.nodeType !== 3) {
1296
+ if (currentNode.outerHTML !== newNode.outerHTML) {
1297
+ updates.push({
1298
+ index,
1299
+ newNode: newNode.outerHTML,
1300
+ isText: false
1301
+ });
1302
+ }
1303
+ } else {
1304
+ updates.push({
1305
+ index,
1306
+ newNode: newNode.nodeType === 3 ? newNode.textContent || "" : newNode.outerHTML,
1307
+ isText: newNode.nodeType === 3
1308
+ });
1309
+ }
1310
+ });
1311
+ if (currentNodes.length > newNodes.length) {
1312
+ for (let i = newNodes.length; i < currentNodes.length; i++) {
1313
+ deletes.push(i);
1314
+ }
1315
+ }
1316
+ return {
1317
+ updates,
1318
+ deletes
1319
+ };
1285
1320
  };
1286
1321
  const UpdateOnDemand = /* @__PURE__ */ defineComponent({
1287
1322
  name: "UpdateOnDemand",
@@ -1297,23 +1332,43 @@ const UpdateOnDemand = /* @__PURE__ */ defineComponent({
1297
1332
  const showCodeRowNumber = inject("showCodeRowNumber");
1298
1333
  const htmlContainer = ref();
1299
1334
  const firstHtml = ref(props.html);
1300
- const updateHtmlContent = (updates) => {
1335
+ const updateHtmlContent = (updates, deletes) => {
1336
+ if (!htmlContainer.value) return;
1337
+ deletes.reverse().forEach((index) => {
1338
+ var _a, _b;
1339
+ (_b = (_a = htmlContainer.value) == null ? void 0 : _a.childNodes[index]) == null ? void 0 : _b.remove();
1340
+ });
1301
1341
  updates.forEach(({
1302
1342
  index,
1303
- newNode
1343
+ newNode,
1344
+ isText
1304
1345
  }) => {
1305
- var _a, _b;
1306
- const targetNode = (_a = htmlContainer.value) == null ? void 0 : _a.children[index];
1346
+ var _a;
1347
+ const targetNode = (_a = htmlContainer.value) == null ? void 0 : _a.childNodes[index];
1307
1348
  if (targetNode) {
1308
- targetNode.outerHTML = newNode;
1349
+ if (isText) {
1350
+ targetNode.textContent = newNode;
1351
+ } else {
1352
+ targetNode.outerHTML = newNode;
1353
+ }
1309
1354
  } else {
1310
- (_b = htmlContainer.value) == null ? void 0 : _b.insertAdjacentHTML("beforeend", newNode);
1355
+ if (htmlContainer.value) {
1356
+ if (isText) {
1357
+ const textNode = document.createTextNode(newNode);
1358
+ htmlContainer.value.appendChild(textNode);
1359
+ } else {
1360
+ htmlContainer.value.insertAdjacentHTML("beforeend", newNode);
1361
+ }
1362
+ }
1311
1363
  }
1312
1364
  });
1313
1365
  };
1314
1366
  watch(() => props.html, (newHtml, oldHtml) => {
1315
- const updates = compareHtml(newHtml, oldHtml || "");
1316
- updateHtmlContent(updates);
1367
+ const {
1368
+ updates,
1369
+ deletes
1370
+ } = compareHtml(newHtml, oldHtml || "");
1371
+ updateHtmlContent(updates, deletes);
1317
1372
  });
1318
1373
  return () => createVNode("div", {
1319
1374
  "id": `${editorId}-preview`,