slate-angular 20.2.10 → 20.2.11

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.
@@ -1585,13 +1585,13 @@ const getCachedHeightByElement = (editor, element) => {
1585
1585
  }
1586
1586
  return null;
1587
1587
  };
1588
- const buildHeightsAndAccumulatedHeights = (editor) => {
1588
+ const buildHeightsAndAccumulatedHeights = (editor, visibleStates) => {
1589
1589
  const children = (editor.children || []);
1590
1590
  const heights = new Array(children.length);
1591
1591
  const accumulatedHeights = new Array(children.length + 1);
1592
1592
  accumulatedHeights[0] = 0;
1593
1593
  for (let i = 0; i < children.length; i++) {
1594
- const isVisible = editor.isVisible(children[i]);
1594
+ const isVisible = visibleStates[i];
1595
1595
  let height = isVisible ? getCachedHeightByElement(editor, children[i]) : 0;
1596
1596
  if (height === null) {
1597
1597
  try {
@@ -1606,8 +1606,8 @@ const buildHeightsAndAccumulatedHeights = (editor) => {
1606
1606
  }
1607
1607
  return { heights, accumulatedHeights };
1608
1608
  };
1609
- const calculateVirtualTopHeight = (editor, startIndex) => {
1610
- const { accumulatedHeights } = buildHeightsAndAccumulatedHeights(editor);
1609
+ const calculateVirtualTopHeight = (editor, startIndex, visibleStates) => {
1610
+ const { accumulatedHeights } = buildHeightsAndAccumulatedHeights(editor, visibleStates);
1611
1611
  return accumulatedHeights[startIndex] ?? 0;
1612
1612
  };
1613
1613
  const scrollToElement = (editor, element, scrollTo) => {
@@ -1619,7 +1619,8 @@ const scrollToElement = (editor, element, scrollTo) => {
1619
1619
  if (anchorIndex < 0) {
1620
1620
  return;
1621
1621
  }
1622
- const { accumulatedHeights } = buildHeightsAndAccumulatedHeights(editor);
1622
+ const visibleStates = editor.getAllVisibleStates();
1623
+ const { accumulatedHeights } = buildHeightsAndAccumulatedHeights(editor, visibleStates);
1623
1624
  scrollTo((accumulatedHeights[anchorIndex] ?? 0) + getBusinessTop(editor));
1624
1625
  EDITOR_TO_IS_FROM_SCROLL_TO.set(editor, true);
1625
1626
  setTimeout(() => {
@@ -1826,6 +1827,9 @@ const withAngular = (editor, clipboardFormatKey = 'x-slate-fragment') => {
1826
1827
  e.isVisible = element => {
1827
1828
  return true;
1828
1829
  };
1830
+ e.getAllVisibleStates = () => {
1831
+ return new Array(e.children.length).fill(true);
1832
+ };
1829
1833
  e.getRoughHeight = (element, defaultHeight) => {
1830
1834
  return defaultHeight === undefined ? VIRTUAL_SCROLL_DEFAULT_BLOCK_HEIGHT : defaultHeight;
1831
1835
  };
@@ -3438,7 +3442,8 @@ class SlateEditable {
3438
3442
  this.editor.children = value;
3439
3443
  this.initializeContext();
3440
3444
  if (this.isEnabledVirtualScroll()) {
3441
- const virtualView = this.calculateVirtualViewport();
3445
+ const visibleStates = this.editor.getAllVisibleStates();
3446
+ const virtualView = this.calculateVirtualViewport(visibleStates);
3442
3447
  this.applyVirtualView(virtualView);
3443
3448
  const childrenForRender = virtualView.inViewportChildren;
3444
3449
  if (isDebug) {
@@ -3448,7 +3453,7 @@ class SlateEditable {
3448
3453
  this.listRender.initialize(childrenForRender, this.editor, this.context, 0, virtualView.inViewportIndics);
3449
3454
  }
3450
3455
  else {
3451
- const { preRenderingCount, childrenWithPreRendering, childrenWithPreRenderingIndics } = this.handlePreRendering();
3456
+ const { preRenderingCount, childrenWithPreRendering, childrenWithPreRenderingIndics } = this.handlePreRendering(visibleStates);
3452
3457
  this.listRender.update(childrenWithPreRendering, this.editor, this.context, preRenderingCount, childrenWithPreRenderingIndics);
3453
3458
  }
3454
3459
  this.viewportRefresh$.next();
@@ -3695,19 +3700,20 @@ class SlateEditable {
3695
3700
  }
3696
3701
  }
3697
3702
  updateListRenderAndRemeasureHeights() {
3698
- let virtualView = this.calculateVirtualViewport();
3703
+ const visibleStates = this.editor.getAllVisibleStates();
3704
+ let virtualView = this.calculateVirtualViewport(visibleStates);
3699
3705
  let diff = this.diffVirtualViewport(virtualView, 'onChange');
3700
3706
  if (diff.isDifferent && diff.needRemoveOnTop) {
3701
3707
  const remeasureIndics = diff.changedIndexesOfTop;
3702
3708
  const changed = measureHeightByIndics(this.editor, remeasureIndics);
3703
3709
  if (changed) {
3704
- virtualView = this.calculateVirtualViewport();
3710
+ virtualView = this.calculateVirtualViewport(visibleStates);
3705
3711
  diff = this.diffVirtualViewport(virtualView, 'second');
3706
3712
  }
3707
3713
  }
3708
3714
  // const oldInViewportChildren = this.inViewportChildren;
3709
3715
  this.applyVirtualView(virtualView);
3710
- const { preRenderingCount, childrenWithPreRendering, childrenWithPreRenderingIndics } = this.handlePreRendering();
3716
+ const { preRenderingCount, childrenWithPreRendering, childrenWithPreRenderingIndics } = this.handlePreRendering(visibleStates);
3711
3717
  this.listRender.update(childrenWithPreRendering, this.editor, this.context, preRenderingCount, childrenWithPreRenderingIndics);
3712
3718
  // 新增或者修改的才需要重算,计算出这个结果
3713
3719
  // const remeasureIndics = [];
@@ -3846,14 +3852,14 @@ class SlateEditable {
3846
3852
  }
3847
3853
  return parseFloat(this.virtualTopHeightElement.style.height.replace('px', ''));
3848
3854
  }
3849
- handlePreRendering() {
3855
+ handlePreRendering(visibleStates) {
3850
3856
  let preRenderingCount = 0;
3851
3857
  const childrenWithPreRendering = [...this.inViewportChildren];
3852
3858
  const childrenWithPreRenderingIndics = [...this.inViewportIndics];
3853
3859
  const firstIndex = this.inViewportIndics[0];
3854
3860
  for (let index = firstIndex - 1; index >= 0; index--) {
3855
3861
  const element = this.editor.children[index];
3856
- if (this.editor.isVisible(element)) {
3862
+ if (visibleStates[index]) {
3857
3863
  childrenWithPreRendering.unshift(element);
3858
3864
  childrenWithPreRenderingIndics.unshift(index);
3859
3865
  preRenderingCount = 1;
@@ -3863,7 +3869,7 @@ class SlateEditable {
3863
3869
  const lastIndex = this.inViewportIndics[this.inViewportIndics.length - 1];
3864
3870
  for (let index = lastIndex + 1; index < this.editor.children.length; index++) {
3865
3871
  const element = this.editor.children[index];
3866
- if (this.editor.isVisible(element)) {
3872
+ if (visibleStates[index]) {
3867
3873
  childrenWithPreRendering.push(element);
3868
3874
  childrenWithPreRenderingIndics.push(index);
3869
3875
  break;
@@ -3878,7 +3884,8 @@ class SlateEditable {
3878
3884
  const isFromScrollTo = EDITOR_TO_IS_FROM_SCROLL_TO.get(this.editor);
3879
3885
  if (this.inViewportIndics.length > 0 && !isFromScrollTo) {
3880
3886
  const topHeight = this.getActualVirtualTopHeight();
3881
- const refreshVirtualTopHeight = calculateVirtualTopHeight(this.editor, this.inViewportIndics[0]);
3887
+ const visibleStates = this.editor.getAllVisibleStates();
3888
+ const refreshVirtualTopHeight = calculateVirtualTopHeight(this.editor, this.inViewportIndics[0], visibleStates);
3882
3889
  if (topHeight !== refreshVirtualTopHeight) {
3883
3890
  if (isDebug) {
3884
3891
  debugLog('log', 'update top height since dirty state(正数减去高度,负数代表增加高度): ', topHeight - refreshVirtualTopHeight);
@@ -3892,20 +3899,21 @@ class SlateEditable {
3892
3899
  if (isDebug) {
3893
3900
  debugLog('log', 'tryUpdateVirtualViewport Anim start');
3894
3901
  }
3895
- let virtualView = this.calculateVirtualViewport();
3902
+ const visibleStates = this.editor.getAllVisibleStates();
3903
+ let virtualView = this.calculateVirtualViewport(visibleStates);
3896
3904
  let diff = this.diffVirtualViewport(virtualView);
3897
3905
  if (diff.isDifferent && diff.needRemoveOnTop && !isFromScrollTo) {
3898
3906
  const remeasureIndics = diff.changedIndexesOfTop;
3899
3907
  const changed = measureHeightByIndics(this.editor, remeasureIndics);
3900
3908
  if (changed) {
3901
- virtualView = this.calculateVirtualViewport();
3909
+ virtualView = this.calculateVirtualViewport(visibleStates);
3902
3910
  diff = this.diffVirtualViewport(virtualView, 'second');
3903
3911
  }
3904
3912
  }
3905
3913
  if (diff.isDifferent) {
3906
3914
  this.applyVirtualView(virtualView);
3907
3915
  if (this.listRender.initialized) {
3908
- const { preRenderingCount, childrenWithPreRendering, childrenWithPreRenderingIndics } = this.handlePreRendering();
3916
+ const { preRenderingCount, childrenWithPreRendering, childrenWithPreRenderingIndics } = this.handlePreRendering(visibleStates);
3909
3917
  this.listRender.update(childrenWithPreRendering, this.editor, this.context, preRenderingCount, childrenWithPreRenderingIndics);
3910
3918
  if (diff.needAddOnTop && !isFromScrollTo) {
3911
3919
  const remeasureAddedIndics = diff.changedIndexesOfTop;
@@ -3916,7 +3924,7 @@ class SlateEditable {
3916
3924
  const topHeightBeforeAdd = virtualView.accumulatedHeights[startIndexBeforeAdd];
3917
3925
  const changed = measureHeightByIndics(this.editor, remeasureAddedIndics);
3918
3926
  if (changed) {
3919
- const newHeights = buildHeightsAndAccumulatedHeights(this.editor);
3927
+ const newHeights = buildHeightsAndAccumulatedHeights(this.editor, visibleStates);
3920
3928
  const actualTopHeightAfterAdd = newHeights.accumulatedHeights[startIndexBeforeAdd];
3921
3929
  const newTopHeight = virtualView.top - (actualTopHeightAfterAdd - topHeightBeforeAdd);
3922
3930
  this.setVirtualSpaceHeight(newTopHeight);
@@ -3936,7 +3944,7 @@ class SlateEditable {
3936
3944
  }
3937
3945
  });
3938
3946
  }
3939
- calculateVirtualViewport() {
3947
+ calculateVirtualViewport(visibleStates) {
3940
3948
  const children = (this.editor.children || []);
3941
3949
  if (!children.length || !this.isEnabledVirtualScroll()) {
3942
3950
  return {
@@ -3973,7 +3981,7 @@ class SlateEditable {
3973
3981
  }, 100);
3974
3982
  }
3975
3983
  const adjustedScrollTop = Math.max(0, scrollTop - getBusinessTop(this.editor));
3976
- const { heights, accumulatedHeights } = buildHeightsAndAccumulatedHeights(this.editor);
3984
+ const { heights, accumulatedHeights } = buildHeightsAndAccumulatedHeights(this.editor, visibleStates);
3977
3985
  const totalHeight = accumulatedHeights[elementLength];
3978
3986
  const maxScrollTop = Math.max(0, totalHeight - viewportHeight);
3979
3987
  const limitedScrollTop = Math.min(adjustedScrollTop, maxScrollTop);
@@ -3985,7 +3993,7 @@ class SlateEditable {
3985
3993
  for (let i = 0; i < elementLength && accumulatedOffset < viewBottom; i++) {
3986
3994
  const currentHeight = heights[i];
3987
3995
  const nextOffset = accumulatedOffset + currentHeight;
3988
- const isVisible = this.editor.isVisible(children[i]);
3996
+ const isVisible = visibleStates[i];
3989
3997
  if (!isVisible) {
3990
3998
  accumulatedOffset = nextOffset;
3991
3999
  continue;