slate-angular 20.2.0-next.17 → 20.2.0-next.19

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.
@@ -954,6 +954,51 @@ const fallbackCopyText = async (text) => {
954
954
  });
955
955
  };
956
956
 
957
+ const ELEMENT_KEY_TO_HEIGHTS = new WeakMap();
958
+ const getRealHeightByElement = (editor, element, defaultHeight = VIRTUAL_SCROLL_DEFAULT_BLOCK_HEIGHT) => {
959
+ const isVisible = editor.isVisible(element);
960
+ if (!isVisible) {
961
+ return 0;
962
+ }
963
+ if (!element) {
964
+ return defaultHeight;
965
+ }
966
+ const heights = ELEMENT_KEY_TO_HEIGHTS.get(editor);
967
+ const key = AngularEditor.findKey(editor, element);
968
+ const height = heights?.get(key.id);
969
+ if (typeof height === 'number') {
970
+ return height;
971
+ }
972
+ if (heights?.has(key.id)) {
973
+ console.error('getBlockHeight: invalid height value', key.id, height);
974
+ }
975
+ return defaultHeight;
976
+ };
977
+ const buildHeightsAndAccumulatedHeights = (editor) => {
978
+ const children = (editor.children || []);
979
+ const heights = new Array(children.length);
980
+ const accumulatedHeights = new Array(children.length + 1);
981
+ accumulatedHeights[0] = 0;
982
+ for (let i = 0; i < children.length; i++) {
983
+ const height = getRealHeightByElement(editor, children[i]);
984
+ heights[i] = height;
985
+ accumulatedHeights[i + 1] = accumulatedHeights[i] + height;
986
+ }
987
+ return { heights, accumulatedHeights };
988
+ };
989
+ const scrollToElement = (editor, element, scrollTo) => {
990
+ const children = editor.children;
991
+ if (!children.length) {
992
+ return;
993
+ }
994
+ const anchorIndex = children.findIndex(item => item === element);
995
+ if (anchorIndex < 0) {
996
+ return;
997
+ }
998
+ const { accumulatedHeights } = buildHeightsAndAccumulatedHeights(editor);
999
+ scrollTo(accumulatedHeights[anchorIndex] ?? 0);
1000
+ };
1001
+
957
1002
  const withAngular = (editor, clipboardFormatKey = 'x-slate-fragment') => {
958
1003
  let e = editor;
959
1004
  let { apply } = e;
@@ -3085,8 +3130,6 @@ class VirtualScrollDebugOverlay {
3085
3130
  }
3086
3131
  }
3087
3132
 
3088
- const JUST_NOW_UPDATED_VIRTUAL_VIEW = new WeakMap();
3089
- const ELEMENT_KEY_TO_HEIGHTS = new WeakMap();
3090
3133
  // not correctly clipboardData on beforeinput
3091
3134
  const forceOnDOMPaste = IS_SAFARI;
3092
3135
  const isDebug = localStorage.getItem(SLATE_DEBUG_KEY) === 'true';
@@ -3218,7 +3261,7 @@ class SlateEditable {
3218
3261
  else {
3219
3262
  this.listRender.update(childrenForRender, this.editor, this.context);
3220
3263
  }
3221
- this.scheduleMeasureVisibleHeights();
3264
+ this.tryMeasureInViewportChildrenHeights();
3222
3265
  }
3223
3266
  else {
3224
3267
  if (!this.listRender.initialized) {
@@ -3376,11 +3419,7 @@ class SlateEditable {
3376
3419
  forceRender() {
3377
3420
  this.updateContext();
3378
3421
  if (this.isEnabledVirtualScroll()) {
3379
- const virtualView = this.calculateVirtualViewport();
3380
- this.applyVirtualView(virtualView);
3381
- this.listRender.update(this.inViewportChildren, this.editor, this.context);
3382
- const visibleIndexes = Array.from(this.inViewportIndics);
3383
- this.remeasureHeightByIndics(visibleIndexes);
3422
+ this.updateListRenderAndRemeasureHeights();
3384
3423
  }
3385
3424
  else {
3386
3425
  this.listRender.update(this.editor.children, this.editor, this.context);
@@ -3424,16 +3463,31 @@ class SlateEditable {
3424
3463
  const changed = this.updateContext();
3425
3464
  if (changed) {
3426
3465
  if (this.isEnabledVirtualScroll()) {
3427
- const virtualView = this.calculateVirtualViewport();
3428
- this.applyVirtualView(virtualView);
3429
- this.listRender.update(virtualView.inViewportChildren, this.editor, this.context);
3430
- this.scheduleMeasureVisibleHeights();
3466
+ this.updateListRenderAndRemeasureHeights();
3431
3467
  }
3432
3468
  else {
3433
3469
  this.listRender.update(this.editor.children, this.editor, this.context);
3434
3470
  }
3435
3471
  }
3436
3472
  }
3473
+ updateListRenderAndRemeasureHeights() {
3474
+ const virtualView = this.calculateVirtualViewport();
3475
+ const oldInViewportChildren = this.inViewportChildren;
3476
+ this.applyVirtualView(virtualView);
3477
+ this.listRender.update(this.inViewportChildren, this.editor, this.context);
3478
+ // 新增或者修改的才需要重算,计算出这个结果
3479
+ const remeasureIndics = [];
3480
+ const newInViewportIndics = Array.from(this.inViewportIndics);
3481
+ this.inViewportChildren.forEach((child, index) => {
3482
+ if (oldInViewportChildren.indexOf(child) === -1) {
3483
+ remeasureIndics.push(newInViewportIndics[index]);
3484
+ }
3485
+ });
3486
+ if (isDebug && remeasureIndics.length > 0) {
3487
+ console.log('remeasure height by indics: ', remeasureIndics);
3488
+ }
3489
+ this.remeasureHeightByIndics(remeasureIndics);
3490
+ }
3437
3491
  updateContext() {
3438
3492
  const decorations = this.generateDecorations();
3439
3493
  if (this.context.selection !== this.editor.selection ||
@@ -3519,7 +3573,8 @@ class SlateEditable {
3519
3573
  this.editorResizeObserver = new ResizeObserver(entries => {
3520
3574
  if (entries.length > 0 && entries[0].contentRect.width !== editorResizeObserverRectWidth) {
3521
3575
  editorResizeObserverRectWidth = entries[0].contentRect.width;
3522
- this.remeasureHeightByIndics(Array.from(this.inViewportIndics));
3576
+ const remeasureIndics = Array.from(this.inViewportIndics);
3577
+ this.remeasureHeightByIndics(remeasureIndics);
3523
3578
  }
3524
3579
  });
3525
3580
  this.editorResizeObserver.observe(this.elementRef.nativeElement);
@@ -3541,15 +3596,16 @@ class SlateEditable {
3541
3596
  VirtualScrollDebugOverlay.log(doc, type, ...args);
3542
3597
  }
3543
3598
  tryUpdateVirtualViewport() {
3544
- this.refreshVirtualViewAnimId && cancelAnimationFrame(this.refreshVirtualViewAnimId);
3545
- this.refreshVirtualViewAnimId = requestAnimationFrame(() => {
3599
+ this.tryUpdateVirtualViewportAnimId && cancelAnimationFrame(this.tryUpdateVirtualViewportAnimId);
3600
+ this.tryUpdateVirtualViewportAnimId = requestAnimationFrame(() => {
3546
3601
  let virtualView = this.calculateVirtualViewport();
3547
3602
  let diff = this.diffVirtualViewport(virtualView);
3548
3603
  if (!diff.isDiff) {
3549
3604
  return;
3550
3605
  }
3551
3606
  if (diff.isMissingTop) {
3552
- const result = this.remeasureHeightByIndics(diff.diffTopRenderedIndexes);
3607
+ const remeasureIndics = diff.diffTopRenderedIndexes;
3608
+ const result = this.remeasureHeightByIndics(remeasureIndics);
3553
3609
  if (result) {
3554
3610
  virtualView = this.calculateVirtualViewport();
3555
3611
  diff = this.diffVirtualViewport(virtualView, 'second');
@@ -3565,7 +3621,7 @@ class SlateEditable {
3565
3621
  this.toNativeSelection();
3566
3622
  }
3567
3623
  }
3568
- this.scheduleMeasureVisibleHeights();
3624
+ this.tryMeasureInViewportChildrenHeights();
3569
3625
  });
3570
3626
  }
3571
3627
  calculateVirtualViewport() {
@@ -3596,8 +3652,7 @@ class SlateEditable {
3596
3652
  }
3597
3653
  const elementLength = children.length;
3598
3654
  const adjustedScrollTop = Math.max(0, scrollTop - this.businessHeight);
3599
- const heights = children.map((_, idx) => this.getBlockHeight(idx));
3600
- const accumulatedHeights = this.buildAccumulatedHeight(heights);
3655
+ const { heights, accumulatedHeights } = buildHeightsAndAccumulatedHeights(this.editor);
3601
3656
  const totalHeight = accumulatedHeights[elementLength];
3602
3657
  const maxScrollTop = Math.max(0, totalHeight - viewportHeight);
3603
3658
  const limitedScrollTop = Math.min(adjustedScrollTop, maxScrollTop);
@@ -3706,8 +3761,8 @@ class SlateEditable {
3706
3761
  this.debugLog('log', `====== diffVirtualViewport stage: ${stage} ======`);
3707
3762
  this.debugLog('log', 'oldVisibleIndexes:', oldVisibleIndexes);
3708
3763
  this.debugLog('log', 'newVisibleIndexes:', newVisibleIndexes);
3709
- this.debugLog('log', 'diffTopRenderedIndexes:', isMissingTop ? '-' : isAddedTop ? '+' : '-', diffTopRenderedIndexes, diffTopRenderedIndexes.map(index => this.getBlockHeight(index, 0)));
3710
- this.debugLog('log', 'diffBottomRenderedIndexes:', isAddedBottom ? '+' : isMissingBottom ? '-' : '+', diffBottomRenderedIndexes, diffBottomRenderedIndexes.map(index => this.getBlockHeight(index, 0)));
3764
+ this.debugLog('log', 'diffTopRenderedIndexes:', isMissingTop ? '-' : isAddedTop ? '+' : '-', diffTopRenderedIndexes, diffTopRenderedIndexes.map(index => getRealHeightByElement(this.editor, this.editor.children[index], 0)));
3765
+ this.debugLog('log', 'diffBottomRenderedIndexes:', isAddedBottom ? '+' : isMissingBottom ? '-' : '+', diffBottomRenderedIndexes, diffBottomRenderedIndexes.map(index => getRealHeightByElement(this.editor, this.editor.children[index], 0)));
3711
3766
  const needTop = virtualView.heights.slice(0, newVisibleIndexes[0]).reduce((acc, height) => acc + height, 0);
3712
3767
  const needBottom = virtualView.heights
3713
3768
  .slice(newVisibleIndexes[newVisibleIndexes.length - 1] + 1)
@@ -3732,39 +3787,12 @@ class SlateEditable {
3732
3787
  diffBottomRenderedIndexes: []
3733
3788
  };
3734
3789
  }
3735
- getBlockHeight(index, defaultHeight = VIRTUAL_SCROLL_DEFAULT_BLOCK_HEIGHT) {
3736
- const node = this.editor.children[index];
3737
- const isVisible = this.editor.isVisible(node);
3738
- if (!isVisible) {
3739
- return 0;
3740
- }
3741
- if (!node) {
3742
- return defaultHeight;
3743
- }
3744
- const key = AngularEditor.findKey(this.editor, node);
3745
- const height = this.keyHeightMap.get(key.id);
3746
- if (typeof height === 'number') {
3747
- return height;
3748
- }
3749
- if (this.keyHeightMap.has(key.id)) {
3750
- console.error('getBlockHeight: invalid height value', key.id, height);
3751
- }
3752
- return defaultHeight;
3753
- }
3754
- buildAccumulatedHeight(heights) {
3755
- const accumulatedHeights = new Array(heights.length + 1).fill(0);
3756
- for (let i = 0; i < heights.length; i++) {
3757
- // 存储前 i 个的累计高度
3758
- accumulatedHeights[i + 1] = accumulatedHeights[i] + heights[i];
3759
- }
3760
- return accumulatedHeights;
3761
- }
3762
- scheduleMeasureVisibleHeights() {
3790
+ tryMeasureInViewportChildrenHeights() {
3763
3791
  if (!this.isEnabledVirtualScroll()) {
3764
3792
  return;
3765
3793
  }
3766
- this.measureVisibleHeightsAnimId && cancelAnimationFrame(this.measureVisibleHeightsAnimId);
3767
- this.measureVisibleHeightsAnimId = requestAnimationFrame(() => {
3794
+ this.tryMeasureInViewportChildrenHeightsAnimId && cancelAnimationFrame(this.tryMeasureInViewportChildrenHeightsAnimId);
3795
+ this.tryMeasureInViewportChildrenHeightsAnimId = requestAnimationFrame(() => {
3768
3796
  this.measureVisibleHeights();
3769
3797
  });
3770
3798
  }
@@ -3798,7 +3826,7 @@ class SlateEditable {
3798
3826
  remeasureHeightByIndics(indics) {
3799
3827
  const children = (this.editor.children || []);
3800
3828
  let isHeightChanged = false;
3801
- indics.forEach(index => {
3829
+ indics.forEach((index, i) => {
3802
3830
  const node = children[index];
3803
3831
  if (!node) {
3804
3832
  return;
@@ -3812,21 +3840,21 @@ class SlateEditable {
3812
3840
  const ret = view.getRealHeight();
3813
3841
  if (ret instanceof Promise) {
3814
3842
  ret.then(height => {
3843
+ this.keyHeightMap.set(key.id, height);
3815
3844
  if (height !== prevHeight) {
3816
- this.keyHeightMap.set(key.id, height);
3817
3845
  isHeightChanged = true;
3818
3846
  if (isDebug) {
3819
- this.debugLog('log', `remeasureHeightByIndics, index: ${index} prevHeight: ${prevHeight} newHeight: ${height}`);
3847
+ this.debugLog('log', `remeasure element height, index: ${index} prevHeight: ${prevHeight} newHeight: ${height}`);
3820
3848
  }
3821
3849
  }
3822
3850
  });
3823
3851
  }
3824
3852
  else {
3853
+ this.keyHeightMap.set(key.id, ret);
3825
3854
  if (ret !== prevHeight) {
3826
- this.keyHeightMap.set(key.id, ret);
3827
3855
  isHeightChanged = true;
3828
3856
  if (isDebug) {
3829
- this.debugLog('log', `remeasureHeightByIndics, index: ${index} prevHeight: ${prevHeight} newHeight: ${ret}`);
3857
+ this.debugLog('log', `remeasure element height, index: ${index} prevHeight: ${prevHeight} newHeight: ${ret}`);
3830
3858
  }
3831
3859
  }
3832
3860
  }
@@ -5046,5 +5074,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.12", ngImpo
5046
5074
  * Generated bundle index. Do not edit.
5047
5075
  */
5048
5076
 
5049
- export { AngularEditor, BaseComponent, BaseElementComponent, BaseElementFlavour, BaseFlavour, BaseLeafComponent, BaseLeafFlavour, BaseTextComponent, BaseTextFlavour, BlockCardRef, DEFAULT_ELEMENT_HEIGHT, DefaultTextFlavour, EDITOR_TO_AFTER_VIEW_INIT_QUEUE, EDITOR_TO_VIRTUAL_SCROLL_SELECTION, ELEMENT_KEY_TO_HEIGHTS, ELEMENT_TO_COMPONENT, FAKE_LEFT_BLOCK_CARD_OFFSET, FAKE_RIGHT_BLOCK_CARD_OFFSET, FlavourRef, HAS_BEFORE_INPUT_SUPPORT, IS_ANDROID, IS_APPLE, IS_CHROME, IS_CHROME_LEGACY, IS_EDGE_LEGACY, IS_ENABLED_VIRTUAL_SCROLL, IS_FIREFOX, IS_FIREFOX_LEGACY, IS_IOS, IS_QQBROWSER, IS_SAFARI, IS_UC_MOBILE, IS_WECHATBROWSER, JUST_NOW_UPDATED_VIRTUAL_VIEW, PLACEHOLDER_SYMBOL, SLATE_BLOCK_CARD_CLASS_NAME, SLATE_DEBUG_KEY, SlateBlockCard, SlateChildrenOutlet, SlateEditable, SlateErrorCode, SlateFragmentAttributeKey, SlateModule, VIRTUAL_SCROLL_DEFAULT_BLOCK_HEIGHT, VoidTextFlavour, blobAsString, buildHTMLText, check, completeTable, createClipboardData, createText, createThrottleRAF, defaultScrollSelectionIntoView, fallbackCopyText, getBlockCardByNativeElement, getCardTargetAttribute, getClipboardData, getClipboardFromHTMLText, getContentHeight, getDataTransferClipboard, getDataTransferClipboardText, getNavigatorClipboard, getPlainText, getSelection, getSlateFragmentAttribute, getZeroTextNode, hasAfterContextChange, hasBeforeContextChange, hasBlockCard, hasBlockCardWithNode, hotkeys, isCardCenterByTargetAttr, isCardLeft, isCardLeftByTargetAttr, isCardRightByTargetAttr, isClipboardFile, isClipboardReadSupported, isClipboardWriteSupported, isClipboardWriteTextSupported, isComponentType, isDOMText, isDecoratorRangeListEqual, isFlavourType, isInvalidTable, isTemplateRef, isValid, normalize, setClipboardData, setDataTransferClipboard, setDataTransferClipboardText, setNavigatorClipboard, shallowCompare, stripHtml, withAngular };
5077
+ export { AngularEditor, BaseComponent, BaseElementComponent, BaseElementFlavour, BaseFlavour, BaseLeafComponent, BaseLeafFlavour, BaseTextComponent, BaseTextFlavour, BlockCardRef, DEFAULT_ELEMENT_HEIGHT, DefaultTextFlavour, EDITOR_TO_AFTER_VIEW_INIT_QUEUE, EDITOR_TO_VIRTUAL_SCROLL_SELECTION, ELEMENT_KEY_TO_HEIGHTS, ELEMENT_TO_COMPONENT, FAKE_LEFT_BLOCK_CARD_OFFSET, FAKE_RIGHT_BLOCK_CARD_OFFSET, FlavourRef, HAS_BEFORE_INPUT_SUPPORT, IS_ANDROID, IS_APPLE, IS_CHROME, IS_CHROME_LEGACY, IS_EDGE_LEGACY, IS_ENABLED_VIRTUAL_SCROLL, IS_FIREFOX, IS_FIREFOX_LEGACY, IS_IOS, IS_QQBROWSER, IS_SAFARI, IS_UC_MOBILE, IS_WECHATBROWSER, PLACEHOLDER_SYMBOL, SLATE_BLOCK_CARD_CLASS_NAME, SLATE_DEBUG_KEY, SlateBlockCard, SlateChildrenOutlet, SlateEditable, SlateErrorCode, SlateFragmentAttributeKey, SlateModule, VIRTUAL_SCROLL_DEFAULT_BLOCK_HEIGHT, VoidTextFlavour, blobAsString, buildHTMLText, buildHeightsAndAccumulatedHeights, check, completeTable, createClipboardData, createText, createThrottleRAF, defaultScrollSelectionIntoView, fallbackCopyText, getBlockCardByNativeElement, getCardTargetAttribute, getClipboardData, getClipboardFromHTMLText, getContentHeight, getDataTransferClipboard, getDataTransferClipboardText, getNavigatorClipboard, getPlainText, getRealHeightByElement, getSelection, getSlateFragmentAttribute, getZeroTextNode, hasAfterContextChange, hasBeforeContextChange, hasBlockCard, hasBlockCardWithNode, hotkeys, isCardCenterByTargetAttr, isCardLeft, isCardLeftByTargetAttr, isCardRightByTargetAttr, isClipboardFile, isClipboardReadSupported, isClipboardWriteSupported, isClipboardWriteTextSupported, isComponentType, isDOMText, isDecoratorRangeListEqual, isFlavourType, isInvalidTable, isTemplateRef, isValid, normalize, scrollToElement, setClipboardData, setDataTransferClipboard, setDataTransferClipboardText, setNavigatorClipboard, shallowCompare, stripHtml, withAngular };
5050
5078
  //# sourceMappingURL=slate-angular.mjs.map