slate-angular 20.2.11 → 20.2.13

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.
@@ -5,7 +5,7 @@ import * as i0 from '@angular/core';
5
5
  import { TemplateRef, ComponentRef, IterableDiffers, inject, ViewContainerRef, forwardRef, HostBinding, Input, ChangeDetectionStrategy, Component, NgModule, ElementRef, ChangeDetectorRef, Directive, ViewChild } from '@angular/core';
6
6
  import { direction } from 'direction';
7
7
  import scrollIntoView from 'scroll-into-view-if-needed';
8
- import { Subject, debounceTime } from 'rxjs';
8
+ import { Subject, tap, debounceTime, filter } from 'rxjs';
9
9
  import { NG_VALUE_ACCESSOR } from '@angular/forms';
10
10
  import { HistoryEditor } from 'slate-history';
11
11
  import { CommonModule } from '@angular/common';
@@ -1566,7 +1566,10 @@ const measureHeightByIndics = (editor, indics, force = false) => {
1566
1566
  return;
1567
1567
  }
1568
1568
  hasChanged = true;
1569
- calcHeightByElement(editor, element);
1569
+ const currentHeight = calcHeightByElement(editor, element);
1570
+ if (isDebug) {
1571
+ debugLog('log', 'measureHeightByIndics: height not equal, index: ', index, 'preHeight: ', preHeight, 'height: ', currentHeight);
1572
+ }
1570
1573
  });
1571
1574
  return hasChanged;
1572
1575
  };
@@ -3383,7 +3386,7 @@ class SlateEditable {
3383
3386
  this.inViewportChildren = [];
3384
3387
  this.inViewportIndics = [];
3385
3388
  this.keyHeightMap = new Map();
3386
- this.viewportRefresh$ = new Subject();
3389
+ this.indicsOfNeedBeMeasured$ = new Subject();
3387
3390
  this.virtualScrollInitialized = false;
3388
3391
  }
3389
3392
  ngOnInit() {
@@ -3442,6 +3445,7 @@ class SlateEditable {
3442
3445
  this.editor.children = value;
3443
3446
  this.initializeContext();
3444
3447
  if (this.isEnabledVirtualScroll()) {
3448
+ const previousInViewportChildren = [...this.inViewportChildren];
3445
3449
  const visibleStates = this.editor.getAllVisibleStates();
3446
3450
  const virtualView = this.calculateVirtualViewport(visibleStates);
3447
3451
  this.applyVirtualView(virtualView);
@@ -3456,7 +3460,10 @@ class SlateEditable {
3456
3460
  const { preRenderingCount, childrenWithPreRendering, childrenWithPreRenderingIndics } = this.handlePreRendering(visibleStates);
3457
3461
  this.listRender.update(childrenWithPreRendering, this.editor, this.context, preRenderingCount, childrenWithPreRenderingIndics);
3458
3462
  }
3459
- this.viewportRefresh$.next();
3463
+ const remeasureIndics = this.getChangedIndics(previousInViewportChildren);
3464
+ if (remeasureIndics.length) {
3465
+ this.indicsOfNeedBeMeasured$.next(remeasureIndics);
3466
+ }
3460
3467
  }
3461
3468
  else {
3462
3469
  if (!this.listRender.initialized) {
@@ -3701,6 +3708,7 @@ class SlateEditable {
3701
3708
  }
3702
3709
  updateListRenderAndRemeasureHeights() {
3703
3710
  const visibleStates = this.editor.getAllVisibleStates();
3711
+ const previousInViewportChildren = [...this.inViewportChildren];
3704
3712
  let virtualView = this.calculateVirtualViewport(visibleStates);
3705
3713
  let diff = this.diffVirtualViewport(virtualView, 'onChange');
3706
3714
  if (diff.isDifferent && diff.needRemoveOnTop) {
@@ -3711,18 +3719,13 @@ class SlateEditable {
3711
3719
  diff = this.diffVirtualViewport(virtualView, 'second');
3712
3720
  }
3713
3721
  }
3714
- // const oldInViewportChildren = this.inViewportChildren;
3715
3722
  this.applyVirtualView(virtualView);
3716
3723
  const { preRenderingCount, childrenWithPreRendering, childrenWithPreRenderingIndics } = this.handlePreRendering(visibleStates);
3717
3724
  this.listRender.update(childrenWithPreRendering, this.editor, this.context, preRenderingCount, childrenWithPreRenderingIndics);
3718
- // 新增或者修改的才需要重算,计算出这个结果
3719
- // const remeasureIndics = [];
3720
- // this.inViewportChildren.forEach((child, index) => {
3721
- // if (oldInViewportChildren.indexOf(child) === -1) {
3722
- // remeasureIndics.push(this.inViewportIndics[index]);
3723
- // }
3724
- // });
3725
- this.viewportRefresh$.next();
3725
+ const remeasureIndics = this.getChangedIndics(previousInViewportChildren);
3726
+ if (remeasureIndics.length) {
3727
+ this.indicsOfNeedBeMeasured$.next(remeasureIndics);
3728
+ }
3726
3729
  }
3727
3730
  updateContext() {
3728
3731
  const decorations = this.generateDecorations();
@@ -3810,33 +3813,48 @@ class SlateEditable {
3810
3813
  if (entries.length > 0 && entries[0].contentRect.width !== editorResizeObserverRectWidth) {
3811
3814
  editorResizeObserverRectWidth = entries[0].contentRect.width;
3812
3815
  this.keyHeightMap.clear();
3813
- const firstElement = this.inViewportChildren[0];
3814
- const firstDomElement = AngularEditor.toDOMNode(this.editor, firstElement);
3815
- const target = firstDomElement || this.virtualTopHeightElement;
3816
+ let target = this.virtualTopHeightElement;
3817
+ if (this.inViewportChildren[0]) {
3818
+ const firstElement = this.inViewportChildren[0];
3819
+ const firstDomElement = AngularEditor.toDOMNode(this.editor, firstElement);
3820
+ target = firstDomElement;
3821
+ }
3816
3822
  EDITOR_TO_ROOT_NODE_WIDTH.set(this.editor, target.offsetWidth);
3817
3823
  updatePreRenderingElementWidth(this.editor);
3818
- this.viewportRefresh$.next();
3819
3824
  if (isDebug) {
3820
3825
  debugLog('log', 'editorResizeObserverRectWidth: ', editorResizeObserverRectWidth, 'EDITOR_TO_ROOT_NODE_WIDTH: ', EDITOR_TO_ROOT_NODE_WIDTH.get(this.editor));
3821
3826
  }
3822
3827
  }
3823
3828
  });
3824
3829
  this.editorResizeObserver.observe(this.elementRef.nativeElement);
3825
- this.viewportRefresh$.pipe(debounceTime(1000)).subscribe(() => {
3826
- // const res = measureHeightByIndics(this.editor, this.inViewportIndics);
3827
- // if (isDebug) {
3828
- // debugLog(
3829
- // 'log',
3830
- // 'viewportRefresh$ debounceTime 1000ms',
3831
- // 'inViewportIndics: ',
3832
- // this.inViewportIndics,
3833
- // 'measureHeightByIndics height changed: ',
3834
- // res
3835
- // );
3836
- // }
3830
+ let pendingRemeasureIndics = [];
3831
+ this.indicsOfNeedBeMeasured$
3832
+ .pipe(tap((previousValue) => {
3833
+ previousValue.forEach((index) => {
3834
+ if (!pendingRemeasureIndics.includes(index)) {
3835
+ pendingRemeasureIndics.push(index);
3836
+ }
3837
+ });
3838
+ }), debounceTime(500), filter(() => pendingRemeasureIndics.length > 0))
3839
+ .subscribe(() => {
3840
+ measureHeightByIndics(this.editor, pendingRemeasureIndics, true);
3841
+ pendingRemeasureIndics = [];
3842
+ if (isDebug) {
3843
+ debugLog('log', 'exist pendingRemeasureIndics: ', pendingRemeasureIndics, 'will try to update virtual viewport');
3844
+ }
3845
+ this.tryUpdateVirtualViewport();
3837
3846
  });
3838
3847
  }
3839
3848
  }
3849
+ getChangedIndics(previousValue) {
3850
+ const remeasureIndics = [];
3851
+ this.inViewportChildren.forEach((child, index) => {
3852
+ if (previousValue.indexOf(child) === -1) {
3853
+ remeasureIndics.push(this.inViewportIndics[index]);
3854
+ }
3855
+ });
3856
+ return remeasureIndics;
3857
+ }
3840
3858
  setVirtualSpaceHeight(topHeight, bottomHeight) {
3841
3859
  if (!this.virtualScrollInitialized) {
3842
3860
  return;
@@ -3933,10 +3951,9 @@ class SlateEditable {
3933
3951
  }
3934
3952
  }
3935
3953
  }
3936
- if (!AngularEditor.isReadOnly(this.editor) && this.editor.selection) {
3954
+ if (this.editor.selection) {
3937
3955
  this.toNativeSelection(false);
3938
3956
  }
3939
- this.viewportRefresh$.next();
3940
3957
  }
3941
3958
  }
3942
3959
  if (isDebug) {