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

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,13 +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
- await this.updateView();
3250
3247
  } else if (!parentNode.isText()) {
3251
3248
  const index = Array.from(parentElement.childNodes).findIndex((item) => item === element2);
3252
3249
  const node = this.domParseNode(element2);
@@ -3256,10 +3253,9 @@ const onComposition = async function(e) {
3256
3253
  if (this.selection.focused()) {
3257
3254
  this.setSelectionAfter(node, "all");
3258
3255
  }
3259
- await this.updateView();
3260
3256
  }
3261
3257
  this.isComposition = false;
3262
- setPlaceholder.apply(this);
3258
+ await this.updateView();
3263
3259
  }
3264
3260
  };
3265
3261
  const onKeyboard = function(e) {
@@ -3311,126 +3307,6 @@ const onCut = function(e) {
3311
3307
  event2.preventDefault();
3312
3308
  }
3313
3309
  };
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
- const removeDomObserve = (editor) => {
3339
- if (editor.domObserver) {
3340
- editor.domObserver.disconnect();
3341
- editor.domObserver = null;
3342
- }
3343
- };
3344
- const setDomObserve = (editor) => {
3345
- if (!window.MutationObserver) {
3346
- console.warn("The current browser does not support MutationObserver");
3347
- return;
3348
- }
3349
- removeDomObserve(editor);
3350
- editor.domObserver = new MutationObserver((mutationList) => {
3351
- if (editor.isComposition) {
3352
- return;
3353
- }
3354
- let hasUpdate = false;
3355
- const illegalDoms = [];
3356
- for (let i = 0; i < mutationList.length; i++) {
3357
- const mutationRecord = mutationList[i];
3358
- 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;
3381
- }
3382
- } 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;
3386
- 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
- } 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
- });
3415
- }
3416
- }
3417
- }
3418
- }
3419
- if (hasUpdate) {
3420
- illegalDoms.forEach((item) => {
3421
- item.remove();
3422
- });
3423
- editor.updateView();
3424
- }
3425
- });
3426
- editor.domObserver.observe(editor.$el, {
3427
- attributes: false,
3428
- characterData: true,
3429
- characterDataOldValue: true,
3430
- childList: true,
3431
- subtree: true
3432
- });
3433
- };
3434
3310
  class Extension {
3435
3311
  constructor(name) {
3436
3312
  /**
@@ -6207,6 +6083,7 @@ const imageResizable = (editor) => {
6207
6083
  event$1.interaction.stop();
6208
6084
  return;
6209
6085
  }
6086
+ editor.removeDomObserve();
6210
6087
  event.on(event$1.target, "dragstart", (e) => e.preventDefault());
6211
6088
  const node = editor.findNode(event$1.target);
6212
6089
  data.set(event$1.target, "node", node);
@@ -6375,6 +6252,7 @@ const videoResizable = (editor) => {
6375
6252
  event$1.interaction.stop();
6376
6253
  return;
6377
6254
  }
6255
+ editor.removeDomObserve();
6378
6256
  event.on(event$1.target, "dragstart", (e) => e.preventDefault());
6379
6257
  const node = editor.findNode(event$1.target);
6380
6258
  data.set(event$1.target, "node", node);
@@ -35940,6 +35818,7 @@ const tableResizable = (editor) => {
35940
35818
  event$1.interaction.stop();
35941
35819
  return;
35942
35820
  }
35821
+ editor.removeDomObserve();
35943
35822
  event.on(event$1.target, "dragstart", (e) => e.preventDefault());
35944
35823
  const node = editor.findNode(event$1.target);
35945
35824
  const index = node.parent.children.findIndex((item) => item.isEqual(node));
@@ -38208,10 +38087,10 @@ class Editor {
38208
38087
  checkNodes.apply(this);
38209
38088
  setPlaceholder.apply(this);
38210
38089
  const oldHtml = this.$el.innerHTML;
38211
- removeDomObserve(this);
38090
+ this.removeDomObserve();
38212
38091
  const useDefault = typeof this.onUpdateView == "function" ? await this.onUpdateView.apply(this, [false]) : true;
38213
38092
  useDefault && defaultUpdateView.apply(this, [false]);
38214
- setDomObserve(this);
38093
+ this.setDomObserve();
38215
38094
  const newHtml = this.$el.innerHTML;
38216
38095
  if (oldHtml != newHtml) {
38217
38096
  if (typeof this.onChange == "function") {
@@ -38275,10 +38154,10 @@ class Editor {
38275
38154
  });
38276
38155
  checkNodes.apply(this);
38277
38156
  setPlaceholder.apply(this);
38278
- removeDomObserve(this);
38157
+ this.removeDomObserve();
38279
38158
  const useDefault = typeof this.onUpdateView == "function" ? await this.onUpdateView.apply(this, [true]) : true;
38280
38159
  if (useDefault) defaultUpdateView.apply(this, [true]);
38281
- setDomObserve(this);
38160
+ this.setDomObserve();
38282
38161
  this.history.setState(this.stackNodes, this.selection);
38283
38162
  this.oldStackNodes = this.stackNodes.map((item) => item.fullClone());
38284
38163
  if (typeof this.afterUpdateView == "function") this.afterUpdateView.apply(this);
@@ -38365,6 +38244,135 @@ class Editor {
38365
38244
  }
38366
38245
  return true;
38367
38246
  }
38247
+ /**
38248
+ * 移除对编辑器的dom监听
38249
+ */
38250
+ removeDomObserve() {
38251
+ if (this.domObserver) {
38252
+ this.domObserver.disconnect();
38253
+ this.domObserver = null;
38254
+ }
38255
+ }
38256
+ /**
38257
+ * 设置对编辑器的dom监听,主要解决非法dom插入问题
38258
+ */
38259
+ setDomObserve() {
38260
+ if (!window.MutationObserver) {
38261
+ console.warn("The current browser does not support MutationObserver");
38262
+ return;
38263
+ }
38264
+ this.removeDomObserve();
38265
+ this.domObserver = new MutationObserver((mutationList) => {
38266
+ if (this.isComposition) {
38267
+ return;
38268
+ }
38269
+ const updateRecords = [];
38270
+ for (let i = 0; i < mutationList.length; i++) {
38271
+ const mutationRecord = mutationList[i];
38272
+ if (mutationRecord.type == "characterData") {
38273
+ if (!updateRecords.find((item) => item.type === "update" && item.elm === mutationRecord.target)) {
38274
+ updateRecords.push({
38275
+ type: "update",
38276
+ elm: mutationRecord.target
38277
+ });
38278
+ }
38279
+ } else if (mutationRecord.type == "attributes") {
38280
+ if (mutationRecord.target != this.$el && !updateRecords.find((item) => item.type === "update" && item.elm === mutationRecord.target)) {
38281
+ updateRecords.push({
38282
+ type: "update",
38283
+ elm: mutationRecord.target
38284
+ });
38285
+ }
38286
+ } else if (mutationRecord.type == "childList") {
38287
+ if (mutationRecord.addedNodes.length > 0) {
38288
+ mutationRecord.addedNodes.forEach((addNode) => {
38289
+ if (!updateRecords.find((item) => item.type === "add" && item.elm === addNode)) {
38290
+ updateRecords.push({
38291
+ type: "add",
38292
+ elm: addNode
38293
+ });
38294
+ }
38295
+ });
38296
+ }
38297
+ if (mutationRecord.removedNodes.length > 0) {
38298
+ mutationRecord.removedNodes.forEach((removedNode) => {
38299
+ const recordIndex = updateRecords.findIndex((item) => item.type === "add" && item.elm === removedNode);
38300
+ if (recordIndex > -1) {
38301
+ updateRecords.splice(recordIndex, 1);
38302
+ } else if (!updateRecords.find((item) => item.type === "remove" && item.elm === removedNode)) {
38303
+ updateRecords.push({
38304
+ type: "remove",
38305
+ elm: removedNode
38306
+ });
38307
+ }
38308
+ });
38309
+ }
38310
+ }
38311
+ }
38312
+ if (updateRecords.length > 0) {
38313
+ updateRecords.forEach((record) => {
38314
+ if (record.type === "update") {
38315
+ const elm = record.elm.nodeType === 3 ? record.elm.parentNode : record.elm;
38316
+ const nodeKey = elm.getAttribute(NODE_MARK);
38317
+ const node = nodeKey ? KNode.searchByKey(nodeKey, this.stackNodes) : null;
38318
+ if (node) {
38319
+ const newNode2 = this.domParseNode(elm);
38320
+ this.addNodeAfter(newNode2, node);
38321
+ if (this.isSelectionInTargetNode(node, "start")) {
38322
+ this.setSelectionAfter(newNode2, "start");
38323
+ }
38324
+ if (this.isSelectionInTargetNode(node, "end")) {
38325
+ this.setSelectionAfter(newNode2, "end");
38326
+ }
38327
+ const index = (node.parent ? node.parent.children : this.stackNodes).findIndex((item) => item.isEqual(node));
38328
+ (node.parent ? node.parent.children : this.stackNodes).splice(index, 1);
38329
+ }
38330
+ } else if (record.type === "add") {
38331
+ if (updateRecords.some((item) => item.type === "update" && (item.elm.nodeType === 3 ? item.elm.parentNode : item.elm).contains(record.elm))) {
38332
+ return;
38333
+ }
38334
+ const parentElement = record.elm.parentNode;
38335
+ const index = Array.from(parentElement.childNodes).findIndex((item) => item === record.elm);
38336
+ const node = this.domParseNode(record.elm);
38337
+ if (parentElement === this.$el) {
38338
+ this.stackNodes.splice(index, 0, node);
38339
+ } else {
38340
+ const nodeKey = parentElement.getAttribute(NODE_MARK);
38341
+ const parentNode = nodeKey ? KNode.searchByKey(nodeKey, this.stackNodes) : null;
38342
+ if (parentNode) this.addNode(node, parentNode, index);
38343
+ }
38344
+ parentElement.removeChild(record.elm);
38345
+ } else if (record.type === "remove") {
38346
+ if (updateRecords.some((item) => item.type === "update" && (item.elm.nodeType === 3 ? item.elm.parentNode : item.elm).contains(record.elm))) {
38347
+ return;
38348
+ }
38349
+ const elm = record.elm.nodeType === 3 ? record.elm.parentNode : record.elm;
38350
+ const nodeKey = elm.getAttribute(NODE_MARK);
38351
+ const node = nodeKey ? KNode.searchByKey(nodeKey, this.stackNodes) : null;
38352
+ if (node) {
38353
+ const startInNode = this.isSelectionInTargetNode(node, "start");
38354
+ const endInNode = this.isSelectionInTargetNode(node, "end");
38355
+ node.toEmpty();
38356
+ if (startInNode) {
38357
+ this.updateSelectionRecently("start");
38358
+ }
38359
+ if (endInNode) {
38360
+ this.updateSelectionRecently("end");
38361
+ }
38362
+ }
38363
+ }
38364
+ });
38365
+ this.updateView();
38366
+ }
38367
+ });
38368
+ this.domObserver.observe(this.$el, {
38369
+ attributes: true,
38370
+ characterData: true,
38371
+ characterDataOldValue: true,
38372
+ childList: true,
38373
+ subtree: true
38374
+ });
38375
+ }
38368
38376
  /**
38369
38377
  * 配置编辑器,返回创建的编辑器
38370
38378
  */