slate-angular 20.2.0 → 20.2.1

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.
@@ -1491,9 +1491,9 @@ const measureHeightByIndics = (editor, indics, force = false) => {
1491
1491
  const getBusinessTop = (editor) => {
1492
1492
  return EDITOR_TO_BUSINESS_TOP.get(editor) ?? 0;
1493
1493
  };
1494
- const getRealHeightByElement = (editor, element, defaultHeight = VIRTUAL_SCROLL_DEFAULT_BLOCK_HEIGHT) => {
1495
- const isVisible = editor.isVisible(element);
1496
- if (!isVisible) {
1494
+ const getRealHeightByElement = (editor, element, defaultHeight = VIRTUAL_SCROLL_DEFAULT_BLOCK_HEIGHT, isVisible) => {
1495
+ const visible = isVisible ?? editor.isVisible(element);
1496
+ if (!visible) {
1497
1497
  return 0;
1498
1498
  }
1499
1499
  const heights = ELEMENT_KEY_TO_HEIGHTS.get(editor);
@@ -1510,14 +1510,17 @@ const getRealHeightByElement = (editor, element, defaultHeight = VIRTUAL_SCROLL_
1510
1510
  const buildHeightsAndAccumulatedHeights = (editor) => {
1511
1511
  const children = (editor.children || []);
1512
1512
  const heights = new Array(children.length);
1513
+ const visibles = new Array(children.length);
1513
1514
  const accumulatedHeights = new Array(children.length + 1);
1514
1515
  accumulatedHeights[0] = 0;
1515
1516
  for (let i = 0; i < children.length; i++) {
1516
- const height = getRealHeightByElement(editor, children[i]);
1517
+ const isVisible = editor.isVisible(children[i]);
1518
+ visibles[i] = isVisible;
1519
+ const height = getRealHeightByElement(editor, children[i], VIRTUAL_SCROLL_DEFAULT_BLOCK_HEIGHT, isVisible);
1517
1520
  heights[i] = height;
1518
1521
  accumulatedHeights[i + 1] = accumulatedHeights[i] + height;
1519
1522
  }
1520
- return { heights, accumulatedHeights };
1523
+ return { heights, accumulatedHeights, visibles };
1521
1524
  };
1522
1525
  const calculateVirtualTopHeight = (editor, startIndex) => {
1523
1526
  const { accumulatedHeights } = buildHeightsAndAccumulatedHeights(editor);
@@ -2903,16 +2906,23 @@ class ListRender {
2903
2906
  this.initialized = false;
2904
2907
  this.preRenderingHTMLElement = [];
2905
2908
  }
2906
- initialize(children, parent, childrenContext, preRenderingCount = 0) {
2909
+ initialize(children, parent, childrenContext, preRenderingCount = 0, childrenIndics) {
2907
2910
  this.initialized = true;
2908
2911
  this.children = children;
2909
2912
  const isRoot = parent === this.viewContext.editor;
2910
2913
  const firstIndex = isRoot ? this.viewContext.editor.children.indexOf(children[0]) : 0;
2911
2914
  const parentPath = AngularEditor.findPath(this.viewContext.editor, parent);
2915
+ const getBlockIndex = (index) => {
2916
+ if (childrenIndics && childrenIndics[index] !== undefined) {
2917
+ return childrenIndics[index];
2918
+ }
2919
+ return isRoot ? firstIndex + index : index;
2920
+ };
2912
2921
  children.forEach((descendant, _index) => {
2913
- NODE_TO_INDEX.set(descendant, firstIndex + _index);
2922
+ const currentIndex = getBlockIndex(_index);
2923
+ NODE_TO_INDEX.set(descendant, currentIndex);
2914
2924
  NODE_TO_PARENT.set(descendant, parent);
2915
- const context = getContext(firstIndex + _index, descendant, parentPath, childrenContext, this.viewContext);
2925
+ const context = getContext(currentIndex, descendant, parentPath, childrenContext, this.viewContext);
2916
2926
  const viewType = getViewType(descendant, parent, this.viewContext);
2917
2927
  const view = createEmbeddedViewOrComponentOrFlavour(viewType, context, this.viewContext, this.viewContainerRef);
2918
2928
  const blockCard = createBlockCard(descendant, view, this.viewContext);
@@ -2929,9 +2939,9 @@ class ListRender {
2929
2939
  executeAfterViewInit(this.viewContext.editor);
2930
2940
  }
2931
2941
  }
2932
- update(children, parent, childrenContext, preRenderingCount = 0) {
2942
+ update(children, parent, childrenContext, preRenderingCount = 0, childrenIndics) {
2933
2943
  if (!this.initialized || this.children.length === 0) {
2934
- this.initialize(children, parent, childrenContext, preRenderingCount);
2944
+ this.initialize(children, parent, childrenContext, preRenderingCount, childrenIndics);
2935
2945
  return;
2936
2946
  }
2937
2947
  if (!this.differ) {
@@ -2961,7 +2971,13 @@ class ListRender {
2961
2971
  const diffResult = this.differ.diff(children);
2962
2972
  const parentPath = AngularEditor.findPath(this.viewContext.editor, parent);
2963
2973
  const isRoot = parent === this.viewContext.editor;
2964
- const firstIndex = isRoot ? this.viewContext.editor.children.indexOf(children[0]) : 0;
2974
+ const firstIndex = isRoot && children.length ? this.viewContext.editor.children.indexOf(children[0]) : 0;
2975
+ const getBlockIndex = (index) => {
2976
+ if (childrenIndics && childrenIndics[index] !== undefined) {
2977
+ return childrenIndics[index];
2978
+ }
2979
+ return isRoot ? firstIndex + index : index;
2980
+ };
2965
2981
  if (diffResult) {
2966
2982
  let firstRootNode = getRootNodes(this.views[0], this.blockCards[0])[0];
2967
2983
  const newContexts = [];
@@ -2969,7 +2985,7 @@ class ListRender {
2969
2985
  const newViews = [];
2970
2986
  const newBlockCards = [];
2971
2987
  diffResult.forEachItem(record => {
2972
- const currentIndex = firstIndex + record.currentIndex;
2988
+ const currentIndex = getBlockIndex(record.currentIndex);
2973
2989
  NODE_TO_INDEX.set(record.item, currentIndex);
2974
2990
  NODE_TO_PARENT.set(record.item, parent);
2975
2991
  let context = getContext(currentIndex, record.item, parentPath, childrenContext, this.viewContext);
@@ -3041,9 +3057,10 @@ class ListRender {
3041
3057
  else {
3042
3058
  const newContexts = [];
3043
3059
  this.children.forEach((child, _index) => {
3044
- NODE_TO_INDEX.set(child, firstIndex + _index);
3060
+ const currentIndex = getBlockIndex(_index);
3061
+ NODE_TO_INDEX.set(child, currentIndex);
3045
3062
  NODE_TO_PARENT.set(child, parent);
3046
- let context = getContext(firstIndex + _index, child, parentPath, childrenContext, this.viewContext);
3063
+ let context = getContext(currentIndex, child, parentPath, childrenContext, this.viewContext);
3047
3064
  const previousContext = this.contexts[_index];
3048
3065
  if (memoizedContext(this.viewContext, child, previousContext, context)) {
3049
3066
  context = previousContext;
@@ -3362,11 +3379,11 @@ class SlateEditable {
3362
3379
  debugLog('log', 'writeValue calculate: ', virtualView.inViewportIndics, 'initialized: ', this.listRender.initialized);
3363
3380
  }
3364
3381
  if (!this.listRender.initialized) {
3365
- this.listRender.initialize(childrenForRender, this.editor, this.context);
3382
+ this.listRender.initialize(childrenForRender, this.editor, this.context, 0, virtualView.inViewportIndics);
3366
3383
  }
3367
3384
  else {
3368
- const { preRenderingCount, childrenWithPreRendering } = this.handlePreRendering();
3369
- this.listRender.update(childrenWithPreRendering, this.editor, this.context, preRenderingCount);
3385
+ const { preRenderingCount, childrenWithPreRendering, childrenWithPreRenderingIndics } = this.handlePreRendering();
3386
+ this.listRender.update(childrenWithPreRendering, this.editor, this.context, preRenderingCount, childrenWithPreRenderingIndics);
3370
3387
  }
3371
3388
  }
3372
3389
  else {
@@ -3441,6 +3458,13 @@ class SlateEditable {
3441
3458
  EDITOR_TO_VIRTUAL_SCROLL_SELECTION.set(this.editor, null);
3442
3459
  return selection;
3443
3460
  }
3461
+ isSelectionInvisible(selection) {
3462
+ const anchorIndex = selection.anchor.path[0];
3463
+ const focusIndex = selection.focus.path[0];
3464
+ const anchorElement = this.editor.children[anchorIndex];
3465
+ const focusElement = this.editor.children[focusIndex];
3466
+ return !anchorElement || !focusElement || !this.editor.isVisible(anchorElement) || !this.editor.isVisible(focusElement);
3467
+ }
3444
3468
  toNativeSelection(autoScroll = true) {
3445
3469
  try {
3446
3470
  let { selection } = this.editor;
@@ -3584,7 +3608,13 @@ class SlateEditable {
3584
3608
  }
3585
3609
  }, 0);
3586
3610
  }
3587
- this.toNativeSelection();
3611
+ if (this.editor.selection && this.isSelectionInvisible(this.editor.selection)) {
3612
+ Transforms.deselect(this.editor);
3613
+ return;
3614
+ }
3615
+ else {
3616
+ this.toNativeSelection();
3617
+ }
3588
3618
  }
3589
3619
  render() {
3590
3620
  const changed = this.updateContext();
@@ -3601,8 +3631,8 @@ class SlateEditable {
3601
3631
  const virtualView = this.calculateVirtualViewport();
3602
3632
  const oldInViewportChildren = this.inViewportChildren;
3603
3633
  this.applyVirtualView(virtualView);
3604
- const { preRenderingCount, childrenWithPreRendering } = this.handlePreRendering();
3605
- this.listRender.update(childrenWithPreRendering, this.editor, this.context, preRenderingCount);
3634
+ const { preRenderingCount, childrenWithPreRendering, childrenWithPreRenderingIndics } = this.handlePreRendering();
3635
+ this.listRender.update(childrenWithPreRendering, this.editor, this.context, preRenderingCount, childrenWithPreRenderingIndics);
3606
3636
  // 新增或者修改的才需要重算,计算出这个结果
3607
3637
  const remeasureIndics = [];
3608
3638
  this.inViewportChildren.forEach((child, index) => {
@@ -3727,19 +3757,29 @@ class SlateEditable {
3727
3757
  return parseFloat(this.virtualTopHeightElement.style.height.replace('px', ''));
3728
3758
  }
3729
3759
  handlePreRendering() {
3730
- let preRenderingCount = 1;
3760
+ let preRenderingCount = 0;
3731
3761
  const childrenWithPreRendering = [...this.inViewportChildren];
3732
- if (this.inViewportIndics[0] !== 0) {
3733
- childrenWithPreRendering.unshift(this.editor.children[this.inViewportIndics[0] - 1]);
3734
- }
3735
- else {
3736
- preRenderingCount = 0;
3762
+ const childrenWithPreRenderingIndics = [...this.inViewportIndics];
3763
+ const firstIndex = this.inViewportIndics[0];
3764
+ for (let index = firstIndex - 1; index >= 0; index--) {
3765
+ const element = this.editor.children[index];
3766
+ if (this.editor.isVisible(element)) {
3767
+ childrenWithPreRendering.unshift(element);
3768
+ childrenWithPreRenderingIndics.unshift(index);
3769
+ preRenderingCount = 1;
3770
+ break;
3771
+ }
3737
3772
  }
3738
3773
  const lastIndex = this.inViewportIndics[this.inViewportIndics.length - 1];
3739
- if (lastIndex !== this.editor.children.length - 1) {
3740
- childrenWithPreRendering.push(this.editor.children[lastIndex + 1]);
3774
+ for (let index = lastIndex + 1; index < this.editor.children.length; index++) {
3775
+ const element = this.editor.children[index];
3776
+ if (this.editor.isVisible(element)) {
3777
+ childrenWithPreRendering.push(element);
3778
+ childrenWithPreRenderingIndics.push(index);
3779
+ break;
3780
+ }
3741
3781
  }
3742
- return { preRenderingCount, childrenWithPreRendering };
3782
+ return { preRenderingCount, childrenWithPreRendering, childrenWithPreRenderingIndics };
3743
3783
  }
3744
3784
  tryUpdateVirtualViewport() {
3745
3785
  if (isDebug) {
@@ -3774,8 +3814,8 @@ class SlateEditable {
3774
3814
  if (diff.isDifferent) {
3775
3815
  this.applyVirtualView(virtualView);
3776
3816
  if (this.listRender.initialized) {
3777
- const { preRenderingCount, childrenWithPreRendering } = this.handlePreRendering();
3778
- this.listRender.update(childrenWithPreRendering, this.editor, this.context, preRenderingCount);
3817
+ const { preRenderingCount, childrenWithPreRendering, childrenWithPreRenderingIndics } = this.handlePreRendering();
3818
+ this.listRender.update(childrenWithPreRendering, this.editor, this.context, preRenderingCount, childrenWithPreRenderingIndics);
3779
3819
  if (diff.needAddOnTop) {
3780
3820
  const remeasureAddedIndics = diff.changedIndexesOfTop;
3781
3821
  if (isDebug) {
@@ -3841,7 +3881,7 @@ class SlateEditable {
3841
3881
  }, 100);
3842
3882
  }
3843
3883
  const adjustedScrollTop = Math.max(0, scrollTop - getBusinessTop(this.editor));
3844
- const { heights, accumulatedHeights } = buildHeightsAndAccumulatedHeights(this.editor);
3884
+ const { heights, accumulatedHeights, visibles } = buildHeightsAndAccumulatedHeights(this.editor);
3845
3885
  const totalHeight = accumulatedHeights[elementLength];
3846
3886
  const maxScrollTop = Math.max(0, totalHeight - viewportHeight);
3847
3887
  const limitedScrollTop = Math.min(adjustedScrollTop, maxScrollTop);
@@ -3853,6 +3893,10 @@ class SlateEditable {
3853
3893
  for (let i = 0; i < elementLength && accumulatedOffset < viewBottom; i++) {
3854
3894
  const currentHeight = heights[i];
3855
3895
  const nextOffset = accumulatedOffset + currentHeight;
3896
+ if (!visibles[i]) {
3897
+ accumulatedOffset = nextOffset;
3898
+ continue;
3899
+ }
3856
3900
  // 可视区域有交集,加入渲染
3857
3901
  if (nextOffset > limitedScrollTop && accumulatedOffset < viewBottom) {
3858
3902
  if (inViewportStartIndex === -1)
@@ -3862,11 +3906,6 @@ class SlateEditable {
3862
3906
  }
3863
3907
  accumulatedOffset = nextOffset;
3864
3908
  }
3865
- if (inViewportStartIndex === -1 && elementLength) {
3866
- inViewportStartIndex = elementLength - 1;
3867
- visible.push(children[inViewportStartIndex]);
3868
- inViewportIndics.push(inViewportStartIndex);
3869
- }
3870
3909
  const inViewportEndIndex = inViewportStartIndex === -1 ? elementLength - 1 : (inViewportIndics[inViewportIndics.length - 1] ?? inViewportStartIndex);
3871
3910
  const top = inViewportStartIndex === -1 ? 0 : accumulatedHeights[inViewportStartIndex];
3872
3911
  const bottom = totalHeight - accumulatedHeights[inViewportEndIndex + 1];
@@ -3901,6 +3940,15 @@ class SlateEditable {
3901
3940
  const lastNewIndex = newIndexesInViewport[newIndexesInViewport.length - 1];
3902
3941
  const firstOldIndex = oldIndexesInViewport[0];
3903
3942
  const lastOldIndex = oldIndexesInViewport[oldIndexesInViewport.length - 1];
3943
+ const isSameViewport = oldIndexesInViewport.length === newIndexesInViewport.length &&
3944
+ oldIndexesInViewport.every((index, i) => index === newIndexesInViewport[i]);
3945
+ if (firstNewIndex === firstOldIndex && lastNewIndex === lastOldIndex) {
3946
+ return {
3947
+ isDifferent: !isSameViewport,
3948
+ changedIndexesOfTop: [],
3949
+ changedIndexesOfBottom: []
3950
+ };
3951
+ }
3904
3952
  if (firstNewIndex !== firstOldIndex || lastNewIndex !== lastOldIndex) {
3905
3953
  const changedIndexesOfTop = [];
3906
3954
  const changedIndexesOfBottom = [];