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

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.
@@ -3244,7 +3244,6 @@ const onComposition = async function(e) {
3244
3244
  if (this.isSelectionInTargetNode(parentNode)) {
3245
3245
  updateSelection$1.apply(this);
3246
3246
  }
3247
- await this.updateView();
3248
3247
  } else if (!parentNode.isText()) {
3249
3248
  const index = Array.from(parentElement.childNodes).findIndex((item) => item === element2);
3250
3249
  const node = this.domParseNode(element2);
@@ -3254,10 +3253,9 @@ const onComposition = async function(e) {
3254
3253
  if (this.selection.focused()) {
3255
3254
  this.setSelectionAfter(node, "all");
3256
3255
  }
3257
- await this.updateView();
3258
3256
  }
3259
3257
  this.isComposition = false;
3260
- setPlaceholder.apply(this);
3258
+ await this.updateView();
3261
3259
  }
3262
3260
  };
3263
3261
  const onKeyboard = function(e) {
@@ -3309,129 +3307,6 @@ const onCut = function(e) {
3309
3307
  event2.preventDefault();
3310
3308
  }
3311
3309
  };
3312
- const removeDomObserve = (editor) => {
3313
- if (editor.domObserver) {
3314
- editor.domObserver.disconnect();
3315
- editor.domObserver = null;
3316
- }
3317
- };
3318
- const setDomObserve = (editor) => {
3319
- if (!window.MutationObserver) {
3320
- console.warn("The current browser does not support MutationObserver");
3321
- return;
3322
- }
3323
- removeDomObserve(editor);
3324
- editor.domObserver = new MutationObserver((mutationList) => {
3325
- if (editor.isComposition) {
3326
- return;
3327
- }
3328
- const updateRecords = [];
3329
- for (let i = 0; i < mutationList.length; i++) {
3330
- const mutationRecord = mutationList[i];
3331
- if (mutationRecord.type == "characterData") {
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
- });
3344
- }
3345
- } else if (mutationRecord.type == "childList") {
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);
3396
- if (parentElement === editor.$el) {
3397
- editor.stackNodes.splice(index, 0, node);
3398
- } else {
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
- }
3421
- }
3422
- }
3423
- });
3424
- editor.updateView();
3425
- }
3426
- });
3427
- editor.domObserver.observe(editor.$el, {
3428
- attributes: true,
3429
- characterData: true,
3430
- characterDataOldValue: true,
3431
- childList: true,
3432
- subtree: true
3433
- });
3434
- };
3435
3310
  class Extension {
3436
3311
  constructor(name) {
3437
3312
  /**
@@ -6208,6 +6083,7 @@ const imageResizable = (editor) => {
6208
6083
  event$1.interaction.stop();
6209
6084
  return;
6210
6085
  }
6086
+ editor.removeDomObserve();
6211
6087
  event.on(event$1.target, "dragstart", (e) => e.preventDefault());
6212
6088
  const node = editor.findNode(event$1.target);
6213
6089
  data.set(event$1.target, "node", node);
@@ -6376,6 +6252,7 @@ const videoResizable = (editor) => {
6376
6252
  event$1.interaction.stop();
6377
6253
  return;
6378
6254
  }
6255
+ editor.removeDomObserve();
6379
6256
  event.on(event$1.target, "dragstart", (e) => e.preventDefault());
6380
6257
  const node = editor.findNode(event$1.target);
6381
6258
  data.set(event$1.target, "node", node);
@@ -35941,6 +35818,7 @@ const tableResizable = (editor) => {
35941
35818
  event$1.interaction.stop();
35942
35819
  return;
35943
35820
  }
35821
+ editor.removeDomObserve();
35944
35822
  event.on(event$1.target, "dragstart", (e) => e.preventDefault());
35945
35823
  const node = editor.findNode(event$1.target);
35946
35824
  const index = node.parent.children.findIndex((item) => item.isEqual(node));
@@ -37078,7 +36956,7 @@ class Editor {
37078
36956
  * 如果编辑器内有滚动条,滚动编辑器到光标可视范围
37079
36957
  */
37080
36958
  scrollViewToSelection() {
37081
- if (this.selection.focused()) {
36959
+ if (this.selection.focused() && this.isEditable()) {
37082
36960
  const focusDom = this.findDom(this.selection.end.node);
37083
36961
  const scrollFunction = async (scrollEl) => {
37084
36962
  const scrollHeight = element.getScrollHeight(scrollEl);
@@ -38209,10 +38087,10 @@ class Editor {
38209
38087
  checkNodes.apply(this);
38210
38088
  setPlaceholder.apply(this);
38211
38089
  const oldHtml = this.$el.innerHTML;
38212
- removeDomObserve(this);
38090
+ this.removeDomObserve();
38213
38091
  const useDefault = typeof this.onUpdateView == "function" ? await this.onUpdateView.apply(this, [false]) : true;
38214
38092
  useDefault && defaultUpdateView.apply(this, [false]);
38215
- setDomObserve(this);
38093
+ this.setDomObserve();
38216
38094
  const newHtml = this.$el.innerHTML;
38217
38095
  if (oldHtml != newHtml) {
38218
38096
  if (typeof this.onChange == "function") {
@@ -38230,6 +38108,9 @@ class Editor {
38230
38108
  * 根据selection更新编辑器真实光标
38231
38109
  */
38232
38110
  async updateRealSelection() {
38111
+ if (!this.isEditable()) {
38112
+ return;
38113
+ }
38233
38114
  const realSelection = window.getSelection();
38234
38115
  if (!realSelection) {
38235
38116
  return;
@@ -38266,9 +38147,11 @@ class Editor {
38266
38147
  }
38267
38148
  }
38268
38149
  /**
38269
- * 重新渲染编辑器视图,不会触发onChange
38150
+ * 重新渲染编辑器视图
38151
+ * 1. 不会触发onChange事件;
38152
+ * 2. 不会渲染真实光标
38270
38153
  */
38271
- async review(value) {
38154
+ async review(value, unPushHistory = false) {
38272
38155
  if (typeof this.beforeUpdateView == "function") this.beforeUpdateView.apply(this);
38273
38156
  this.stackNodes = this.htmlParseNode(value || "");
38274
38157
  this.formatRules.forEach((rule) => {
@@ -38276,11 +38159,13 @@ class Editor {
38276
38159
  });
38277
38160
  checkNodes.apply(this);
38278
38161
  setPlaceholder.apply(this);
38279
- removeDomObserve(this);
38162
+ this.removeDomObserve();
38280
38163
  const useDefault = typeof this.onUpdateView == "function" ? await this.onUpdateView.apply(this, [true]) : true;
38281
38164
  if (useDefault) defaultUpdateView.apply(this, [true]);
38282
- setDomObserve(this);
38283
- this.history.setState(this.stackNodes, this.selection);
38165
+ this.setDomObserve();
38166
+ if (!unPushHistory) {
38167
+ this.history.setState(this.stackNodes, this.selection);
38168
+ }
38284
38169
  this.oldStackNodes = this.stackNodes.map((item) => item.fullClone());
38285
38170
  if (typeof this.afterUpdateView == "function") this.afterUpdateView.apply(this);
38286
38171
  }
@@ -38366,6 +38251,135 @@ class Editor {
38366
38251
  }
38367
38252
  return true;
38368
38253
  }
38254
+ /**
38255
+ * 移除对编辑器的dom监听
38256
+ */
38257
+ removeDomObserve() {
38258
+ if (this.domObserver) {
38259
+ this.domObserver.disconnect();
38260
+ this.domObserver = null;
38261
+ }
38262
+ }
38263
+ /**
38264
+ * 设置对编辑器的dom监听,主要解决非法dom插入问题
38265
+ */
38266
+ setDomObserve() {
38267
+ if (!window.MutationObserver) {
38268
+ console.warn("The current browser does not support MutationObserver");
38269
+ return;
38270
+ }
38271
+ this.removeDomObserve();
38272
+ this.domObserver = new MutationObserver((mutationList) => {
38273
+ if (this.isComposition) {
38274
+ return;
38275
+ }
38276
+ const updateRecords = [];
38277
+ for (let i = 0; i < mutationList.length; i++) {
38278
+ const mutationRecord = mutationList[i];
38279
+ if (mutationRecord.type == "characterData") {
38280
+ if (!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 == "attributes") {
38287
+ if (mutationRecord.target != this.$el && !updateRecords.find((item) => item.type === "update" && item.elm === mutationRecord.target)) {
38288
+ updateRecords.push({
38289
+ type: "update",
38290
+ elm: mutationRecord.target
38291
+ });
38292
+ }
38293
+ } else if (mutationRecord.type == "childList") {
38294
+ if (mutationRecord.addedNodes.length > 0) {
38295
+ mutationRecord.addedNodes.forEach((addNode) => {
38296
+ if (!updateRecords.find((item) => item.type === "add" && item.elm === addNode)) {
38297
+ updateRecords.push({
38298
+ type: "add",
38299
+ elm: addNode
38300
+ });
38301
+ }
38302
+ });
38303
+ }
38304
+ if (mutationRecord.removedNodes.length > 0) {
38305
+ mutationRecord.removedNodes.forEach((removedNode) => {
38306
+ const recordIndex = updateRecords.findIndex((item) => item.type === "add" && item.elm === removedNode);
38307
+ if (recordIndex > -1) {
38308
+ updateRecords.splice(recordIndex, 1);
38309
+ } else if (!updateRecords.find((item) => item.type === "remove" && item.elm === removedNode)) {
38310
+ updateRecords.push({
38311
+ type: "remove",
38312
+ elm: removedNode
38313
+ });
38314
+ }
38315
+ });
38316
+ }
38317
+ }
38318
+ }
38319
+ if (updateRecords.length > 0) {
38320
+ updateRecords.forEach((record) => {
38321
+ if (record.type === "update") {
38322
+ const elm = record.elm.nodeType === 3 ? record.elm.parentNode : record.elm;
38323
+ const nodeKey = elm.getAttribute(NODE_MARK);
38324
+ const node = nodeKey ? KNode.searchByKey(nodeKey, this.stackNodes) : null;
38325
+ if (node) {
38326
+ const newNode2 = this.domParseNode(elm);
38327
+ this.addNodeAfter(newNode2, node);
38328
+ if (this.isSelectionInTargetNode(node, "start")) {
38329
+ this.setSelectionAfter(newNode2, "start");
38330
+ }
38331
+ if (this.isSelectionInTargetNode(node, "end")) {
38332
+ this.setSelectionAfter(newNode2, "end");
38333
+ }
38334
+ const index = (node.parent ? node.parent.children : this.stackNodes).findIndex((item) => item.isEqual(node));
38335
+ (node.parent ? node.parent.children : this.stackNodes).splice(index, 1);
38336
+ }
38337
+ } else if (record.type === "add") {
38338
+ if (updateRecords.some((item) => item.type === "update" && (item.elm.nodeType === 3 ? item.elm.parentNode : item.elm).contains(record.elm))) {
38339
+ return;
38340
+ }
38341
+ const parentElement = record.elm.parentNode;
38342
+ const index = Array.from(parentElement.childNodes).findIndex((item) => item === record.elm);
38343
+ const node = this.domParseNode(record.elm);
38344
+ if (parentElement === this.$el) {
38345
+ this.stackNodes.splice(index, 0, node);
38346
+ } else {
38347
+ const nodeKey = parentElement.getAttribute(NODE_MARK);
38348
+ const parentNode = nodeKey ? KNode.searchByKey(nodeKey, this.stackNodes) : null;
38349
+ if (parentNode) this.addNode(node, parentNode, index);
38350
+ }
38351
+ parentElement.removeChild(record.elm);
38352
+ } else if (record.type === "remove") {
38353
+ if (updateRecords.some((item) => item.type === "update" && (item.elm.nodeType === 3 ? item.elm.parentNode : item.elm).contains(record.elm))) {
38354
+ return;
38355
+ }
38356
+ const elm = record.elm.nodeType === 3 ? record.elm.parentNode : record.elm;
38357
+ const nodeKey = elm.getAttribute(NODE_MARK);
38358
+ const node = nodeKey ? KNode.searchByKey(nodeKey, this.stackNodes) : null;
38359
+ if (node) {
38360
+ const startInNode = this.isSelectionInTargetNode(node, "start");
38361
+ const endInNode = this.isSelectionInTargetNode(node, "end");
38362
+ node.toEmpty();
38363
+ if (startInNode) {
38364
+ this.updateSelectionRecently("start");
38365
+ }
38366
+ if (endInNode) {
38367
+ this.updateSelectionRecently("end");
38368
+ }
38369
+ }
38370
+ }
38371
+ });
38372
+ this.updateView();
38373
+ }
38374
+ });
38375
+ this.domObserver.observe(this.$el, {
38376
+ attributes: true,
38377
+ characterData: true,
38378
+ characterDataOldValue: true,
38379
+ childList: true,
38380
+ subtree: true
38381
+ });
38382
+ }
38369
38383
  /**
38370
38384
  * 配置编辑器,返回创建的编辑器
38371
38385
  */