@kaitify/core 0.0.1-beta.26 → 0.0.1-beta.27

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.
@@ -3240,12 +3240,10 @@ const onComposition = async function(e) {
3240
3240
  const parentElement = element2.parentElement;
3241
3241
  const parentNode = this.findNode(parentElement);
3242
3242
  if (parentNode.isText() && parentNode.textContent != element2.textContent) {
3243
- const textContent = parentNode.textContent || "";
3244
3243
  parentNode.textContent = element2.textContent || "";
3245
3244
  if (this.isSelectionInTargetNode(parentNode)) {
3246
3245
  updateSelection$1.apply(this);
3247
3246
  }
3248
- element2.textContent = textContent;
3249
3247
  await this.updateView();
3250
3248
  } else if (!parentNode.isText()) {
3251
3249
  const index = Array.from(parentElement.childNodes).findIndex((item) => item === element2);
@@ -3311,30 +3309,6 @@ const onCut = function(e) {
3311
3309
  event2.preventDefault();
3312
3310
  }
3313
3311
  };
3314
- const isLegalDom = (editor, dom) => {
3315
- let legal = true;
3316
- if (dom.nodeType == 3) {
3317
- if (dom.parentNode && dom.parentNode.childNodes.length == 1) {
3318
- try {
3319
- const node = editor.findNode(dom.parentNode);
3320
- if (!node.isText()) {
3321
- legal = false;
3322
- }
3323
- } catch (error2) {
3324
- legal = false;
3325
- }
3326
- } else {
3327
- legal = false;
3328
- }
3329
- } else if (dom.nodeType == 1) {
3330
- try {
3331
- editor.findNode(dom);
3332
- } catch (error2) {
3333
- legal = false;
3334
- }
3335
- }
3336
- return legal;
3337
- };
3338
3312
  const removeDomObserve = (editor) => {
3339
3313
  if (editor.domObserver) {
3340
3314
  editor.domObserver.disconnect();
@@ -3351,80 +3325,107 @@ const setDomObserve = (editor) => {
3351
3325
  if (editor.isComposition) {
3352
3326
  return;
3353
3327
  }
3354
- let hasUpdate = false;
3355
- const illegalDoms = [];
3328
+ const updateRecords = [];
3356
3329
  for (let i = 0; i < mutationList.length; i++) {
3357
3330
  const mutationRecord = mutationList[i];
3358
3331
  if (mutationRecord.type == "characterData") {
3359
- const parentElement = mutationRecord.target.parentNode;
3360
- const parentNode = editor.findNode(parentElement);
3361
- if (parentNode.isText() && parentNode.textContent != mutationRecord.target.textContent) {
3362
- const textContent = parentNode.textContent || "";
3363
- parentNode.textContent = mutationRecord.target.textContent || "";
3364
- if (editor.isSelectionInTargetNode(parentNode)) {
3365
- updateSelection$1.apply(editor);
3366
- }
3367
- removeDomObserve(editor);
3368
- mutationRecord.target.textContent = textContent;
3369
- setDomObserve(editor);
3370
- hasUpdate = true;
3371
- } else if (!parentNode.isText()) {
3372
- const index = Array.from(parentElement.childNodes).findIndex((item) => item === mutationRecord.target);
3373
- const node = editor.domParseNode(mutationRecord.target);
3374
- parentNode.children.splice(index, 0, node);
3375
- node.parent = parentNode;
3376
- illegalDoms.push(mutationRecord.target);
3377
- if (editor.selection.focused()) {
3378
- editor.setSelectionAfter(node, "all");
3379
- }
3380
- hasUpdate = true;
3332
+ if (!updateRecords.find((item) => item.type === "update" && item.elm === mutationRecord.target)) {
3333
+ updateRecords.push({
3334
+ type: "update",
3335
+ elm: mutationRecord.target
3336
+ });
3337
+ }
3338
+ } else if (mutationRecord.type == "attributes") {
3339
+ if (mutationRecord.target != editor.$el && !updateRecords.find((item) => item.type === "update" && item.elm === mutationRecord.target)) {
3340
+ updateRecords.push({
3341
+ type: "update",
3342
+ elm: mutationRecord.target
3343
+ });
3381
3344
  }
3382
3345
  } else if (mutationRecord.type == "childList") {
3383
- const elements = Array.from(mutationRecord.addedNodes).filter((item) => !isLegalDom(editor, item));
3384
- if (elements.length > 0) {
3385
- const parentElement = mutationRecord.target;
3346
+ if (mutationRecord.addedNodes.length > 0) {
3347
+ mutationRecord.addedNodes.forEach((addNode) => {
3348
+ if (!updateRecords.find((item) => item.type === "add" && item.elm === addNode)) {
3349
+ updateRecords.push({
3350
+ type: "add",
3351
+ elm: addNode
3352
+ });
3353
+ }
3354
+ });
3355
+ }
3356
+ if (mutationRecord.removedNodes.length > 0) {
3357
+ mutationRecord.removedNodes.forEach((removedNode) => {
3358
+ const recordIndex = updateRecords.findIndex((item) => item.type === "add" && item.elm === removedNode);
3359
+ if (recordIndex > -1) {
3360
+ updateRecords.splice(recordIndex, 1);
3361
+ } else if (!updateRecords.find((item) => item.type === "remove" && item.elm === removedNode)) {
3362
+ updateRecords.push({
3363
+ type: "remove",
3364
+ elm: removedNode
3365
+ });
3366
+ }
3367
+ });
3368
+ }
3369
+ }
3370
+ }
3371
+ if (updateRecords.length > 0) {
3372
+ updateRecords.forEach((record) => {
3373
+ if (record.type === "update") {
3374
+ const elm = record.elm.nodeType === 3 ? record.elm.parentNode : record.elm;
3375
+ const nodeKey = elm.getAttribute(NODE_MARK);
3376
+ const node = nodeKey ? KNode.searchByKey(nodeKey, editor.stackNodes) : null;
3377
+ if (node) {
3378
+ const newNode2 = editor.domParseNode(elm);
3379
+ editor.addNodeAfter(newNode2, node);
3380
+ if (editor.isSelectionInTargetNode(node, "start")) {
3381
+ editor.setSelectionAfter(newNode2, "start");
3382
+ }
3383
+ if (editor.isSelectionInTargetNode(node, "end")) {
3384
+ editor.setSelectionAfter(newNode2, "end");
3385
+ }
3386
+ const index = (node.parent ? node.parent.children : editor.stackNodes).findIndex((item) => item.isEqual(node));
3387
+ (node.parent ? node.parent.children : editor.stackNodes).splice(index, 1);
3388
+ }
3389
+ } else if (record.type === "add") {
3390
+ if (updateRecords.some((item) => item.type === "update" && (item.elm.nodeType === 3 ? item.elm.parentNode : item.elm).contains(record.elm))) {
3391
+ return;
3392
+ }
3393
+ const parentElement = record.elm.parentNode;
3394
+ const index = Array.from(parentElement.childNodes).findIndex((item) => item === record.elm);
3395
+ const node = editor.domParseNode(record.elm);
3386
3396
  if (parentElement === editor.$el) {
3387
- elements.forEach((el) => {
3388
- const index = Array.from(parentElement.childNodes).findIndex((item) => item === el);
3389
- const node = editor.domParseNode(el);
3390
- editor.stackNodes.splice(index, 0, node);
3391
- illegalDoms.push(el);
3392
- if (editor.selection.focused()) {
3393
- editor.setSelectionAfter(node, "all");
3394
- }
3395
- hasUpdate = true;
3396
- });
3397
+ editor.stackNodes.splice(index, 0, node);
3397
3398
  } else {
3398
- const parentNode = editor.findNode(parentElement);
3399
- elements.forEach((el) => {
3400
- const index = Array.from(parentElement.childNodes).findIndex((item) => item === el);
3401
- const node = editor.domParseNode(el);
3402
- if (parentNode.hasChildren()) {
3403
- parentNode.children.splice(index, 0, node);
3404
- node.parent = parentNode;
3405
- } else {
3406
- parentNode.parent.children.splice(index, 0, node);
3407
- node.parent = parentNode.parent;
3408
- }
3409
- illegalDoms.push(el);
3410
- if (editor.selection.focused()) {
3411
- editor.setSelectionAfter(node, "all");
3412
- }
3413
- hasUpdate = true;
3414
- });
3399
+ const nodeKey = parentElement.getAttribute(NODE_MARK);
3400
+ const parentNode = nodeKey ? KNode.searchByKey(nodeKey, editor.stackNodes) : null;
3401
+ if (parentNode) editor.addNode(node, parentNode, index);
3402
+ }
3403
+ parentElement.removeChild(record.elm);
3404
+ } else if (record.type === "remove") {
3405
+ if (updateRecords.some((item) => item.type === "update" && (item.elm.nodeType === 3 ? item.elm.parentNode : item.elm).contains(record.elm))) {
3406
+ return;
3407
+ }
3408
+ const elm = record.elm.nodeType === 3 ? record.elm.parentNode : record.elm;
3409
+ const nodeKey = elm.getAttribute(NODE_MARK);
3410
+ const node = nodeKey ? KNode.searchByKey(nodeKey, editor.stackNodes) : null;
3411
+ if (node) {
3412
+ const startInNode = editor.isSelectionInTargetNode(node, "start");
3413
+ const endInNode = editor.isSelectionInTargetNode(node, "end");
3414
+ node.toEmpty();
3415
+ if (startInNode) {
3416
+ editor.updateSelectionRecently("start");
3417
+ }
3418
+ if (endInNode) {
3419
+ editor.updateSelectionRecently("end");
3420
+ }
3415
3421
  }
3416
3422
  }
3417
- }
3418
- }
3419
- if (hasUpdate) {
3420
- illegalDoms.forEach((item) => {
3421
- item.remove();
3422
3423
  });
3423
3424
  editor.updateView();
3424
3425
  }
3425
3426
  });
3426
3427
  editor.domObserver.observe(editor.$el, {
3427
- attributes: false,
3428
+ attributes: true,
3428
3429
  characterData: true,
3429
3430
  characterDataOldValue: true,
3430
3431
  childList: true,