slate-angular 20.2.9 → 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,14 @@ 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
- let height = getCachedHeightByElement(editor, children[i]);
1594
+ const isVisible = visibleStates[i];
1595
+ let height = isVisible ? getCachedHeightByElement(editor, children[i]) : 0;
1595
1596
  if (height === null) {
1596
1597
  try {
1597
1598
  height = editor.getRoughHeight(children[i]);
@@ -1605,8 +1606,8 @@ const buildHeightsAndAccumulatedHeights = (editor) => {
1605
1606
  }
1606
1607
  return { heights, accumulatedHeights };
1607
1608
  };
1608
- const calculateVirtualTopHeight = (editor, startIndex) => {
1609
- const { accumulatedHeights } = buildHeightsAndAccumulatedHeights(editor);
1609
+ const calculateVirtualTopHeight = (editor, startIndex, visibleStates) => {
1610
+ const { accumulatedHeights } = buildHeightsAndAccumulatedHeights(editor, visibleStates);
1610
1611
  return accumulatedHeights[startIndex] ?? 0;
1611
1612
  };
1612
1613
  const scrollToElement = (editor, element, scrollTo) => {
@@ -1618,7 +1619,8 @@ const scrollToElement = (editor, element, scrollTo) => {
1618
1619
  if (anchorIndex < 0) {
1619
1620
  return;
1620
1621
  }
1621
- const { accumulatedHeights } = buildHeightsAndAccumulatedHeights(editor);
1622
+ const visibleStates = editor.getAllVisibleStates();
1623
+ const { accumulatedHeights } = buildHeightsAndAccumulatedHeights(editor, visibleStates);
1622
1624
  scrollTo((accumulatedHeights[anchorIndex] ?? 0) + getBusinessTop(editor));
1623
1625
  EDITOR_TO_IS_FROM_SCROLL_TO.set(editor, true);
1624
1626
  setTimeout(() => {
@@ -1825,6 +1827,9 @@ const withAngular = (editor, clipboardFormatKey = 'x-slate-fragment') => {
1825
1827
  e.isVisible = element => {
1826
1828
  return true;
1827
1829
  };
1830
+ e.getAllVisibleStates = () => {
1831
+ return new Array(e.children.length).fill(true);
1832
+ };
1828
1833
  e.getRoughHeight = (element, defaultHeight) => {
1829
1834
  return defaultHeight === undefined ? VIRTUAL_SCROLL_DEFAULT_BLOCK_HEIGHT : defaultHeight;
1830
1835
  };
@@ -2921,11 +2926,13 @@ const createText = (text) => {
2921
2926
  return { nativeElement };
2922
2927
  };
2923
2928
 
2929
+ const PRE_RENDERING_ELEMENT_ON_TOP_CLASS = 'pre-rendering-element-on-top';
2924
2930
  const setPreRenderingElementStyle = (editor, rootNode, isClear = false) => {
2925
2931
  if (isClear) {
2926
2932
  rootNode.style.top = '';
2927
2933
  rootNode.style.width = '';
2928
2934
  rootNode.style.position = '';
2935
+ rootNode.classList.remove(PRE_RENDERING_ELEMENT_ON_TOP_CLASS);
2929
2936
  return;
2930
2937
  }
2931
2938
  const preRenderingWidth = EDITOR_TO_ROOT_NODE_WIDTH.get(editor) ?? 0;
@@ -2937,6 +2944,20 @@ const setPreRenderingElementStyle = (editor, rootNode, isClear = false) => {
2937
2944
  rootNode.style.width = '100%';
2938
2945
  }
2939
2946
  rootNode.style.position = 'absolute';
2947
+ rootNode.classList.add(PRE_RENDERING_ELEMENT_ON_TOP_CLASS);
2948
+ };
2949
+ const updatePreRenderingElementWidth = (editor) => {
2950
+ const editorDom = AngularEditor.toDOMNode(editor, editor);
2951
+ const rootNodes = editorDom.querySelectorAll(`.${PRE_RENDERING_ELEMENT_ON_TOP_CLASS}`);
2952
+ const preRenderingWidth = EDITOR_TO_ROOT_NODE_WIDTH.get(editor) ?? 0;
2953
+ rootNodes.forEach(rootNode => {
2954
+ if (preRenderingWidth) {
2955
+ rootNode.style.width = `${preRenderingWidth}px`;
2956
+ }
2957
+ else {
2958
+ rootNode.style.width = '100%';
2959
+ }
2960
+ });
2940
2961
  };
2941
2962
  class ListRender {
2942
2963
  constructor(viewContext, viewContainerRef, getOutletParent, getOutletElement) {
@@ -3421,7 +3442,8 @@ class SlateEditable {
3421
3442
  this.editor.children = value;
3422
3443
  this.initializeContext();
3423
3444
  if (this.isEnabledVirtualScroll()) {
3424
- const virtualView = this.calculateVirtualViewport();
3445
+ const visibleStates = this.editor.getAllVisibleStates();
3446
+ const virtualView = this.calculateVirtualViewport(visibleStates);
3425
3447
  this.applyVirtualView(virtualView);
3426
3448
  const childrenForRender = virtualView.inViewportChildren;
3427
3449
  if (isDebug) {
@@ -3431,7 +3453,7 @@ class SlateEditable {
3431
3453
  this.listRender.initialize(childrenForRender, this.editor, this.context, 0, virtualView.inViewportIndics);
3432
3454
  }
3433
3455
  else {
3434
- const { preRenderingCount, childrenWithPreRendering, childrenWithPreRenderingIndics } = this.handlePreRendering();
3456
+ const { preRenderingCount, childrenWithPreRendering, childrenWithPreRenderingIndics } = this.handlePreRendering(visibleStates);
3435
3457
  this.listRender.update(childrenWithPreRendering, this.editor, this.context, preRenderingCount, childrenWithPreRenderingIndics);
3436
3458
  }
3437
3459
  this.viewportRefresh$.next();
@@ -3678,19 +3700,20 @@ class SlateEditable {
3678
3700
  }
3679
3701
  }
3680
3702
  updateListRenderAndRemeasureHeights() {
3681
- let virtualView = this.calculateVirtualViewport();
3703
+ const visibleStates = this.editor.getAllVisibleStates();
3704
+ let virtualView = this.calculateVirtualViewport(visibleStates);
3682
3705
  let diff = this.diffVirtualViewport(virtualView, 'onChange');
3683
3706
  if (diff.isDifferent && diff.needRemoveOnTop) {
3684
3707
  const remeasureIndics = diff.changedIndexesOfTop;
3685
3708
  const changed = measureHeightByIndics(this.editor, remeasureIndics);
3686
3709
  if (changed) {
3687
- virtualView = this.calculateVirtualViewport();
3710
+ virtualView = this.calculateVirtualViewport(visibleStates);
3688
3711
  diff = this.diffVirtualViewport(virtualView, 'second');
3689
3712
  }
3690
3713
  }
3691
3714
  // const oldInViewportChildren = this.inViewportChildren;
3692
3715
  this.applyVirtualView(virtualView);
3693
- const { preRenderingCount, childrenWithPreRendering, childrenWithPreRenderingIndics } = this.handlePreRendering();
3716
+ const { preRenderingCount, childrenWithPreRendering, childrenWithPreRenderingIndics } = this.handlePreRendering(visibleStates);
3694
3717
  this.listRender.update(childrenWithPreRendering, this.editor, this.context, preRenderingCount, childrenWithPreRenderingIndics);
3695
3718
  // 新增或者修改的才需要重算,计算出这个结果
3696
3719
  // const remeasureIndics = [];
@@ -3782,12 +3805,16 @@ class SlateEditable {
3782
3805
  this.elementRef.nativeElement.appendChild(this.virtualCenterOutlet);
3783
3806
  this.elementRef.nativeElement.appendChild(this.virtualBottomHeightElement);
3784
3807
  let editorResizeObserverRectWidth = this.elementRef.nativeElement.getBoundingClientRect().width;
3785
- EDITOR_TO_ROOT_NODE_WIDTH.set(this.editor, this.virtualTopHeightElement.getBoundingClientRect().width);
3808
+ EDITOR_TO_ROOT_NODE_WIDTH.set(this.editor, this.virtualTopHeightElement.offsetWidth);
3786
3809
  this.editorResizeObserver = new ResizeObserver(entries => {
3787
3810
  if (entries.length > 0 && entries[0].contentRect.width !== editorResizeObserverRectWidth) {
3788
3811
  editorResizeObserverRectWidth = entries[0].contentRect.width;
3789
3812
  this.keyHeightMap.clear();
3790
- EDITOR_TO_ROOT_NODE_WIDTH.set(this.editor, this.virtualTopHeightElement.getBoundingClientRect().width);
3813
+ const firstElement = this.inViewportChildren[0];
3814
+ const firstDomElement = AngularEditor.toDOMNode(this.editor, firstElement);
3815
+ const target = firstDomElement || this.virtualTopHeightElement;
3816
+ EDITOR_TO_ROOT_NODE_WIDTH.set(this.editor, target.offsetWidth);
3817
+ updatePreRenderingElementWidth(this.editor);
3791
3818
  this.viewportRefresh$.next();
3792
3819
  if (isDebug) {
3793
3820
  debugLog('log', 'editorResizeObserverRectWidth: ', editorResizeObserverRectWidth, 'EDITOR_TO_ROOT_NODE_WIDTH: ', EDITOR_TO_ROOT_NODE_WIDTH.get(this.editor));
@@ -3825,14 +3852,14 @@ class SlateEditable {
3825
3852
  }
3826
3853
  return parseFloat(this.virtualTopHeightElement.style.height.replace('px', ''));
3827
3854
  }
3828
- handlePreRendering() {
3855
+ handlePreRendering(visibleStates) {
3829
3856
  let preRenderingCount = 0;
3830
3857
  const childrenWithPreRendering = [...this.inViewportChildren];
3831
3858
  const childrenWithPreRenderingIndics = [...this.inViewportIndics];
3832
3859
  const firstIndex = this.inViewportIndics[0];
3833
3860
  for (let index = firstIndex - 1; index >= 0; index--) {
3834
3861
  const element = this.editor.children[index];
3835
- if (this.editor.isVisible(element)) {
3862
+ if (visibleStates[index]) {
3836
3863
  childrenWithPreRendering.unshift(element);
3837
3864
  childrenWithPreRenderingIndics.unshift(index);
3838
3865
  preRenderingCount = 1;
@@ -3842,7 +3869,7 @@ class SlateEditable {
3842
3869
  const lastIndex = this.inViewportIndics[this.inViewportIndics.length - 1];
3843
3870
  for (let index = lastIndex + 1; index < this.editor.children.length; index++) {
3844
3871
  const element = this.editor.children[index];
3845
- if (this.editor.isVisible(element)) {
3872
+ if (visibleStates[index]) {
3846
3873
  childrenWithPreRendering.push(element);
3847
3874
  childrenWithPreRenderingIndics.push(index);
3848
3875
  break;
@@ -3857,7 +3884,8 @@ class SlateEditable {
3857
3884
  const isFromScrollTo = EDITOR_TO_IS_FROM_SCROLL_TO.get(this.editor);
3858
3885
  if (this.inViewportIndics.length > 0 && !isFromScrollTo) {
3859
3886
  const topHeight = this.getActualVirtualTopHeight();
3860
- const refreshVirtualTopHeight = calculateVirtualTopHeight(this.editor, this.inViewportIndics[0]);
3887
+ const visibleStates = this.editor.getAllVisibleStates();
3888
+ const refreshVirtualTopHeight = calculateVirtualTopHeight(this.editor, this.inViewportIndics[0], visibleStates);
3861
3889
  if (topHeight !== refreshVirtualTopHeight) {
3862
3890
  if (isDebug) {
3863
3891
  debugLog('log', 'update top height since dirty state(正数减去高度,负数代表增加高度): ', topHeight - refreshVirtualTopHeight);
@@ -3871,20 +3899,21 @@ class SlateEditable {
3871
3899
  if (isDebug) {
3872
3900
  debugLog('log', 'tryUpdateVirtualViewport Anim start');
3873
3901
  }
3874
- let virtualView = this.calculateVirtualViewport();
3902
+ const visibleStates = this.editor.getAllVisibleStates();
3903
+ let virtualView = this.calculateVirtualViewport(visibleStates);
3875
3904
  let diff = this.diffVirtualViewport(virtualView);
3876
3905
  if (diff.isDifferent && diff.needRemoveOnTop && !isFromScrollTo) {
3877
3906
  const remeasureIndics = diff.changedIndexesOfTop;
3878
3907
  const changed = measureHeightByIndics(this.editor, remeasureIndics);
3879
3908
  if (changed) {
3880
- virtualView = this.calculateVirtualViewport();
3909
+ virtualView = this.calculateVirtualViewport(visibleStates);
3881
3910
  diff = this.diffVirtualViewport(virtualView, 'second');
3882
3911
  }
3883
3912
  }
3884
3913
  if (diff.isDifferent) {
3885
3914
  this.applyVirtualView(virtualView);
3886
3915
  if (this.listRender.initialized) {
3887
- const { preRenderingCount, childrenWithPreRendering, childrenWithPreRenderingIndics } = this.handlePreRendering();
3916
+ const { preRenderingCount, childrenWithPreRendering, childrenWithPreRenderingIndics } = this.handlePreRendering(visibleStates);
3888
3917
  this.listRender.update(childrenWithPreRendering, this.editor, this.context, preRenderingCount, childrenWithPreRenderingIndics);
3889
3918
  if (diff.needAddOnTop && !isFromScrollTo) {
3890
3919
  const remeasureAddedIndics = diff.changedIndexesOfTop;
@@ -3895,7 +3924,7 @@ class SlateEditable {
3895
3924
  const topHeightBeforeAdd = virtualView.accumulatedHeights[startIndexBeforeAdd];
3896
3925
  const changed = measureHeightByIndics(this.editor, remeasureAddedIndics);
3897
3926
  if (changed) {
3898
- const newHeights = buildHeightsAndAccumulatedHeights(this.editor);
3927
+ const newHeights = buildHeightsAndAccumulatedHeights(this.editor, visibleStates);
3899
3928
  const actualTopHeightAfterAdd = newHeights.accumulatedHeights[startIndexBeforeAdd];
3900
3929
  const newTopHeight = virtualView.top - (actualTopHeightAfterAdd - topHeightBeforeAdd);
3901
3930
  this.setVirtualSpaceHeight(newTopHeight);
@@ -3915,7 +3944,7 @@ class SlateEditable {
3915
3944
  }
3916
3945
  });
3917
3946
  }
3918
- calculateVirtualViewport() {
3947
+ calculateVirtualViewport(visibleStates) {
3919
3948
  const children = (this.editor.children || []);
3920
3949
  if (!children.length || !this.isEnabledVirtualScroll()) {
3921
3950
  return {
@@ -3952,7 +3981,7 @@ class SlateEditable {
3952
3981
  }, 100);
3953
3982
  }
3954
3983
  const adjustedScrollTop = Math.max(0, scrollTop - getBusinessTop(this.editor));
3955
- const { heights, accumulatedHeights } = buildHeightsAndAccumulatedHeights(this.editor);
3984
+ const { heights, accumulatedHeights } = buildHeightsAndAccumulatedHeights(this.editor, visibleStates);
3956
3985
  const totalHeight = accumulatedHeights[elementLength];
3957
3986
  const maxScrollTop = Math.max(0, totalHeight - viewportHeight);
3958
3987
  const limitedScrollTop = Math.min(adjustedScrollTop, maxScrollTop);
@@ -3964,7 +3993,7 @@ class SlateEditable {
3964
3993
  for (let i = 0; i < elementLength && accumulatedOffset < viewBottom; i++) {
3965
3994
  const currentHeight = heights[i];
3966
3995
  const nextOffset = accumulatedOffset + currentHeight;
3967
- const isVisible = this.editor.isVisible(children[i]);
3996
+ const isVisible = visibleStates[i];
3968
3997
  if (!isVisible) {
3969
3998
  accumulatedOffset = nextOffset;
3970
3999
  continue;