@hailin-zheng/editor-core 1.0.53 → 1.0.56

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.
package/index.js CHANGED
@@ -2741,11 +2741,39 @@ class DataElementRenderObject extends InlineGroupRenderObject {
2741
2741
  }
2742
2742
  if (this.element.props.underline) {
2743
2743
  const y = position.y + 2 + this.rect.height;
2744
- render.contentContext.strokeLines([{ x: position.x, y }, { x: position.x + this.rect.width, y }], 1, '#595959');
2744
+ render.contentContext.strokeLines([{ x: position.x, y }, {
2745
+ x: position.x + this.rect.width,
2746
+ y
2747
+ }], 1, '#595959');
2745
2748
  }
2746
2749
  e.nextRender();
2750
+ this.drawCaption(e);
2747
2751
  });
2748
2752
  }
2753
+ /**
2754
+ * 绘制数据元标题
2755
+ * @param e
2756
+ * @private
2757
+ */
2758
+ drawCaption(e) {
2759
+ const { render, position, docCtx: { viewOptions } } = e;
2760
+ //获取到焦点时,绘制数据元标题
2761
+ if (render.drawMode === 'view' && this.element.isFocused && this.element.paintRenders.indexOf(this) === 0) {
2762
+ const { caption } = this.element.props;
2763
+ if (!caption) {
2764
+ return;
2765
+ }
2766
+ const textProps = new TextProps();
2767
+ textProps.fontSize = 16;
2768
+ textProps.fontName = viewOptions.defaultFontName;
2769
+ textProps.color = '#fff';
2770
+ const titleWidth = render.contentContext.measureText(caption, textProps).width;
2771
+ const x = position.x;
2772
+ const y = position.y - 20;
2773
+ render.contentContext.fillRect(x, y, titleWidth + 10, 20, 'blue');
2774
+ render.contentContext.drawText(caption, textProps, x + 5, y, titleWidth, 20);
2775
+ }
2776
+ }
2749
2777
  }
2750
2778
  const validateDataEle = (ele) => {
2751
2779
  return ele instanceof DataElementLeaf || ele instanceof DataElementInlineGroup;
@@ -6590,8 +6618,8 @@ class DocumentSelection {
6590
6618
  this.selectionState.addRange(startRange);
6591
6619
  }
6592
6620
  /**
6593
- * 获取选区共同的祖先控件
6594
- */
6621
+ * 获取选区共同的祖先控件
6622
+ */
6595
6623
  static getAncestorCommonControl(startControl, endControl) {
6596
6624
  const startNestParents = this.getNestParents(startControl.parent);
6597
6625
  const ancestorCommonControl = this.getCommonParenet(endControl.parent, startNestParents);
@@ -6620,26 +6648,22 @@ class DocumentSelection {
6620
6648
  }
6621
6649
  return this.getCommonParenet(control?.parent, array);
6622
6650
  }
6651
+ /**
6652
+ * 更新选区对象
6653
+ * return:当前选区是否改变
6654
+ * @private
6655
+ */
6623
6656
  updateSelectionState() {
6624
- if (this.compareSelectionEquals()) {
6657
+ if (!this.selectionState.rangeDirty) {
6625
6658
  return false;
6626
6659
  }
6627
6660
  const range = this.selectionState.range;
6661
+ //转换选区
6628
6662
  this.transformRange();
6663
+ this.selectionState.clear();
6629
6664
  if (!range) {
6630
6665
  return false;
6631
6666
  }
6632
- const startControlIndex = ElementUtil.getControlIndex(range.startControl);
6633
- const endControlIndex = ElementUtil.getControlIndex(range.endControl);
6634
- if (endControlIndex < startControlIndex) {
6635
- const clone = range.clone();
6636
- clone.startControl = range.endControl;
6637
- clone.startOffset = range.endOffset;
6638
- clone.endControl = range.startControl;
6639
- clone.endOffset = range.startOffset;
6640
- clone.editable = range.editable;
6641
- this.selectionState.addRange(clone);
6642
- }
6643
6667
  this.selectionState.editable = range.editable;
6644
6668
  this.selectionState.startControl = range.startControl;
6645
6669
  this.selectionState.startOffset = range.startOffset;
@@ -6649,6 +6673,7 @@ class DocumentSelection {
6649
6673
  this.selectionState.endControlIndex = ElementUtil.getControlIndex(range.endControl);
6650
6674
  this.selectionState.ancestorCommonControl = DocumentSelection.getAncestorCommonControl(this.selectionState.startControl, this.selectionState.endControl);
6651
6675
  this.selectionState.enableTrackChanges = this.getEnableTrackChanges(range.startControl);
6676
+ this.selectionState.rangeDirty = false;
6652
6677
  return true;
6653
6678
  }
6654
6679
  /**
@@ -6659,17 +6684,6 @@ class DocumentSelection {
6659
6684
  const body = ElementUtil.getParentByType(ele, DocumentBodyElement);
6660
6685
  return body?.trackChanges || false;
6661
6686
  }
6662
- compareSelectionEquals() {
6663
- const range = this.selectionState.range;
6664
- const snap = this.snapshotSelectionState;
6665
- if (snap && range) {
6666
- return range.startControl === snap.startControl
6667
- && range.startOffset === snap.startOffset
6668
- && range.endControl === snap.endControl
6669
- && range.endOffset === snap.endOffset;
6670
- }
6671
- return false;
6672
- }
6673
6687
  /**
6674
6688
  * 转换选区内容
6675
6689
  * 处理结束选区在开始选区之前
@@ -6763,9 +6777,14 @@ class SelectionState {
6763
6777
  this.selectedRange = null;
6764
6778
  this.cursorPos = null;
6765
6779
  }
6780
+ /**
6781
+ * 当前选区是否发生改变
6782
+ */
6783
+ rangeDirty = false;
6766
6784
  addRange(range) {
6767
6785
  this.clear();
6768
6786
  this.range = range;
6787
+ this.rangeDirty = true;
6769
6788
  this.onChangedEvent.next();
6770
6789
  }
6771
6790
  resetRange(startControl, startOffset) {
@@ -12238,6 +12257,7 @@ class DocumentEvent {
12238
12257
  const newParents = ElementUtil.getParentElements(focusElement);
12239
12258
  const leaveParents = oldParents.filter(item => newParents.every(oldItem => item !== oldItem));
12240
12259
  const enterParents = newParents.filter(item => oldParents.every(oldItem => item !== oldItem));
12260
+ let cancelEvent = false;
12241
12261
  const loopInvokeEvent = (eventName, array) => {
12242
12262
  for (const element of array) {
12243
12263
  const mouseEvent = new LostCursorEvent(this.docCtx);
@@ -12245,11 +12265,17 @@ class DocumentEvent {
12245
12265
  mouseEvent.currentElement = element;
12246
12266
  mouseEvent.ctx = this.docCtx;
12247
12267
  element.invokeEvent(eventName, mouseEvent);
12268
+ if (mouseEvent.isCancel) {
12269
+ cancelEvent = true;
12270
+ break;
12271
+ }
12248
12272
  }
12249
12273
  };
12250
12274
  loopInvokeEvent('LostCursor', leaveParents);
12251
12275
  loopInvokeEvent('GotCursor', enterParents);
12252
- this.prevCursorItems = newParents;
12276
+ if (!cancelEvent) {
12277
+ this.prevCursorItems = newParents;
12278
+ }
12253
12279
  //dDocumentEvent.invokeEvent('GotCursor', focusElement, getCursorEvent, 'All', this.docCtx);
12254
12280
  }
12255
12281
  /**
@@ -14292,24 +14318,43 @@ class DocumentChange {
14292
14318
  */
14293
14319
  onPaste(evt) {
14294
14320
  evt.preventDefault();
14295
- let pasteText = evt.clipboardData?.getData('text/plain');
14296
- const pasteData = evt.clipboardData?.getData('doc/plain');
14297
- if (!pasteData) {
14298
- if (pasteText) {
14299
- this.pastePlainText(pasteText);
14321
+ let pasteText = evt.clipboardData?.getData('text/plain') ?? '';
14322
+ const pasteData = evt.clipboardData?.getData('doc/plain') ?? '';
14323
+ const { collapsed } = this.selectionState;
14324
+ if (!collapsed) {
14325
+ //TODO:如果一个容器内的元素被全部删除,则返回 null
14326
+ // const { cursorEle, cursorOffset } = this.onRangeDelete();
14327
+ // startControl = cursorEle;
14328
+ // startOffset = cursorOffset;
14329
+ this.onRangeDelete();
14330
+ const cb = () => {
14331
+ this.handlePasteContent({ text: pasteText, doc: pasteData }, this.selectionState);
14332
+ };
14333
+ this.docCtx.setPaintedCallback(cb);
14334
+ return;
14335
+ }
14336
+ this.handlePasteContent({ text: pasteText, doc: pasteData }, this.selectionState);
14337
+ }
14338
+ /**
14339
+ * 处理粘贴的内容
14340
+ * @private
14341
+ */
14342
+ handlePasteContent(pasteData, ss) {
14343
+ const { startOffset, startControl, editable } = ss;
14344
+ if (!startControl || !editable) {
14345
+ return;
14346
+ }
14347
+ if (!pasteData.doc) {
14348
+ if (pasteData.text) {
14349
+ this.pastePlainText(pasteData.text);
14300
14350
  }
14301
14351
  return;
14302
14352
  }
14303
- const pasteElement = this.eleReader.readElement(JSON.parse(pasteData));
14353
+ const pasteElement = this.eleReader.readElement(JSON.parse(pasteData.doc));
14304
14354
  if (!pasteElement) {
14305
14355
  console.log('粘贴反序列化数据失败', pasteData);
14306
14356
  return;
14307
14357
  }
14308
- this.selectionState;
14309
- let { startControl, startOffset } = this.selectionState;
14310
- if (!startControl) {
14311
- return;
14312
- }
14313
14358
  //表单模式:如果复制的是单格式的文本,需要序列化为纯文本处理
14314
14359
  if (this.viewOptions.docMode === DocMode.FormEdit) {
14315
14360
  const pasteString = ElementSerialize.serializeString(pasteElement, { all: false });
@@ -14340,7 +14385,6 @@ class DocumentChange {
14340
14385
  this.selectionState.resetRange(lastEle, -1);
14341
14386
  return;
14342
14387
  }
14343
- console.log('粘贴:' + pasteData);
14344
14388
  }
14345
14389
  insertSoftBr() {
14346
14390
  let { startControl, startOffset } = this.selectionState;