slate-angular 20.2.0-next.12 → 20.2.0-next.14

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.
@@ -1801,6 +1801,7 @@ class BaseElementFlavour extends BaseFlavour {
1801
1801
  if (ELEMENT_TO_COMPONENT.get(this.element) === this) {
1802
1802
  ELEMENT_TO_COMPONENT.delete(this.element);
1803
1803
  }
1804
+ this.listRender.destroy();
1804
1805
  this.nativeElement?.remove();
1805
1806
  }
1806
1807
  onContextChange() {
@@ -2197,6 +2198,9 @@ class LeavesRender {
2197
2198
  });
2198
2199
  return { decoratedLeaves, contexts };
2199
2200
  }
2201
+ destroy() {
2202
+ this.views.forEach(view => view.destroy());
2203
+ }
2200
2204
  }
2201
2205
  function getContext$1(index, leafContexts) {
2202
2206
  return leafContexts[index];
@@ -2239,6 +2243,7 @@ class BaseTextFlavour extends BaseFlavour {
2239
2243
  NODE_TO_ELEMENT.delete(this.text);
2240
2244
  }
2241
2245
  ELEMENT_TO_NODE.delete(this.nativeElement);
2246
+ this.leavesRender.destroy();
2242
2247
  this.nativeElement?.remove();
2243
2248
  }
2244
2249
  onContextChange() {
@@ -3074,8 +3079,10 @@ const forceOnDOMPaste = IS_SAFARI;
3074
3079
  const isDebug = localStorage.getItem(SLATE_DEBUG_KEY) === 'true';
3075
3080
  class SlateEditable {
3076
3081
  set virtualScroll(config) {
3077
- this.virtualConfig = config;
3078
- this.doVirtualScroll();
3082
+ this.virtualScrollConfig = config;
3083
+ if (this.isEnabledVirtualScroll()) {
3084
+ this.tryUpdateVirtualViewport();
3085
+ }
3079
3086
  }
3080
3087
  get hasBeforeInputSupport() {
3081
3088
  return HAS_BEFORE_INPUT_SUPPORT;
@@ -3120,14 +3127,14 @@ class SlateEditable {
3120
3127
  return null;
3121
3128
  }
3122
3129
  };
3123
- this.virtualConfig = {
3130
+ this.virtualScrollConfig = {
3124
3131
  enabled: false,
3125
3132
  scrollTop: 0,
3126
3133
  viewportHeight: 0
3127
3134
  };
3128
- this.renderedChildren = [];
3129
- this.virtualVisibleIndexes = new Set();
3130
- this.measuredHeights = new Map();
3135
+ this.inViewportChildren = [];
3136
+ this.inViewportIndics = new Set();
3137
+ this.keyHeightMap = new Map();
3131
3138
  // the height from scroll container top to editor top height element
3132
3139
  this.businessHeight = 0;
3133
3140
  this.virtualScrollInitialized = false;
@@ -3141,7 +3148,7 @@ class SlateEditable {
3141
3148
  NODE_TO_ELEMENT.set(this.editor, this.elementRef.nativeElement);
3142
3149
  ELEMENT_TO_NODE.set(this.elementRef.nativeElement, this.editor);
3143
3150
  IS_READ_ONLY.set(this.editor, this.readonly);
3144
- ELEMENT_KEY_TO_HEIGHTS.set(this.editor, this.measuredHeights);
3151
+ ELEMENT_KEY_TO_HEIGHTS.set(this.editor, this.keyHeightMap);
3145
3152
  EDITOR_TO_ON_CHANGE.set(this.editor, () => {
3146
3153
  this.ngZone.run(() => {
3147
3154
  this.onChange();
@@ -3155,7 +3162,7 @@ class SlateEditable {
3155
3162
  // add browser class
3156
3163
  let browserClass = IS_FIREFOX ? 'firefox' : IS_SAFARI ? 'safari' : '';
3157
3164
  browserClass && this.elementRef.nativeElement.classList.add(browserClass);
3158
- this.initializeVirtualScrolling();
3165
+ this.initializeVirtualScroll();
3159
3166
  this.listRender = new ListRender(this.viewContext, this.viewContainerRef, this.getOutletParent, this.getOutletElement);
3160
3167
  }
3161
3168
  ngOnChanges(simpleChanges) {
@@ -3187,16 +3194,26 @@ class SlateEditable {
3187
3194
  if (value && value.length) {
3188
3195
  this.editor.children = value;
3189
3196
  this.initializeContext();
3190
- const virtualView = this.refreshVirtualView();
3191
- this.applyVirtualView(virtualView);
3192
- const childrenForRender = virtualView.renderedChildren;
3193
- if (!this.listRender.initialized) {
3194
- this.listRender.initialize(childrenForRender, this.editor, this.context);
3197
+ if (this.isEnabledVirtualScroll()) {
3198
+ const virtualView = this.calculateVirtualViewport();
3199
+ this.applyVirtualView(virtualView);
3200
+ const childrenForRender = virtualView.inViewportChildren;
3201
+ if (!this.listRender.initialized) {
3202
+ this.listRender.initialize(childrenForRender, this.editor, this.context);
3203
+ }
3204
+ else {
3205
+ this.listRender.update(childrenForRender, this.editor, this.context);
3206
+ }
3207
+ this.scheduleMeasureVisibleHeights();
3195
3208
  }
3196
3209
  else {
3197
- this.listRender.update(childrenForRender, this.editor, this.context);
3210
+ if (!this.listRender.initialized) {
3211
+ this.listRender.initialize(this.editor.children, this.editor, this.context);
3212
+ }
3213
+ else {
3214
+ this.listRender.update(this.editor.children, this.editor, this.context);
3215
+ }
3198
3216
  }
3199
- this.scheduleMeasureVisibleHeights();
3200
3217
  this.cdr.markForCheck();
3201
3218
  }
3202
3219
  }
@@ -3230,8 +3247,8 @@ class SlateEditable {
3230
3247
  toNativeSelection() {
3231
3248
  try {
3232
3249
  let { selection } = this.editor;
3233
- if (this.virtualConfig?.enabled && selection) {
3234
- const indics = Array.from(this.virtualVisibleIndexes.values());
3250
+ if (this.isEnabledVirtualScroll() && selection) {
3251
+ const indics = Array.from(this.inViewportIndics.values());
3235
3252
  if (indics.length > 0) {
3236
3253
  const currentVisibleRange = {
3237
3254
  anchor: Editor.start(this.editor, [indics[0]]),
@@ -3335,11 +3352,16 @@ class SlateEditable {
3335
3352
  ngDoCheck() { }
3336
3353
  forceRender() {
3337
3354
  this.updateContext();
3338
- const virtualView = this.refreshVirtualView();
3339
- this.applyVirtualView(virtualView);
3340
- const visibleIndexes = Array.from(this.virtualVisibleIndexes);
3341
- this.listRender.update(this.renderedChildren, this.editor, this.context);
3342
- this.remeasureHeightByIndics(visibleIndexes);
3355
+ if (this.isEnabledVirtualScroll()) {
3356
+ const virtualView = this.calculateVirtualViewport();
3357
+ this.applyVirtualView(virtualView);
3358
+ this.listRender.update(this.inViewportChildren, this.editor, this.context);
3359
+ const visibleIndexes = Array.from(this.inViewportIndics);
3360
+ this.remeasureHeightByIndics(visibleIndexes);
3361
+ }
3362
+ else {
3363
+ this.listRender.update(this.editor.children, this.editor, this.context);
3364
+ }
3343
3365
  // repair collaborative editing when Chinese input is interrupted by other users' cursors
3344
3366
  // when the DOMElement where the selection is located is removed
3345
3367
  // the compositionupdate and compositionend events will no longer be fired
@@ -3378,10 +3400,15 @@ class SlateEditable {
3378
3400
  render() {
3379
3401
  const changed = this.updateContext();
3380
3402
  if (changed) {
3381
- const virtualView = this.refreshVirtualView();
3382
- this.applyVirtualView(virtualView);
3383
- this.listRender.update(virtualView.renderedChildren, this.editor, this.context);
3384
- this.scheduleMeasureVisibleHeights();
3403
+ if (this.isEnabledVirtualScroll()) {
3404
+ const virtualView = this.calculateVirtualViewport();
3405
+ this.applyVirtualView(virtualView);
3406
+ this.listRender.update(virtualView.inViewportChildren, this.editor, this.context);
3407
+ this.scheduleMeasureVisibleHeights();
3408
+ }
3409
+ else {
3410
+ this.listRender.update(this.editor.children, this.editor, this.context);
3411
+ }
3385
3412
  }
3386
3413
  }
3387
3414
  updateContext() {
@@ -3444,14 +3471,14 @@ class SlateEditable {
3444
3471
  decorations.push(...placeholderDecorations);
3445
3472
  return decorations;
3446
3473
  }
3447
- shouldUseVirtual() {
3448
- return !!(this.virtualConfig && this.virtualConfig.enabled);
3474
+ isEnabledVirtualScroll() {
3475
+ return !!(this.virtualScrollConfig && this.virtualScrollConfig.enabled);
3449
3476
  }
3450
- initializeVirtualScrolling() {
3477
+ initializeVirtualScroll() {
3451
3478
  if (this.virtualScrollInitialized) {
3452
3479
  return;
3453
3480
  }
3454
- if (this.virtualConfig && this.virtualConfig.enabled) {
3481
+ if (this.isEnabledVirtualScroll()) {
3455
3482
  this.virtualScrollInitialized = true;
3456
3483
  this.virtualTopHeightElement = document.createElement('div');
3457
3484
  this.virtualTopHeightElement.classList.add('virtual-top-height');
@@ -3469,7 +3496,7 @@ class SlateEditable {
3469
3496
  this.editorResizeObserver = new ResizeObserver(entries => {
3470
3497
  if (entries.length > 0 && entries[0].contentRect.width !== editorResizeObserverRectWidth) {
3471
3498
  editorResizeObserverRectWidth = entries[0].contentRect.width;
3472
- this.remeasureHeightByIndics(Array.from(this.virtualVisibleIndexes));
3499
+ this.remeasureHeightByIndics(Array.from(this.inViewportIndics));
3473
3500
  }
3474
3501
  });
3475
3502
  this.editorResizeObserver.observe(this.elementRef.nativeElement);
@@ -3479,7 +3506,7 @@ class SlateEditable {
3479
3506
  }
3480
3507
  }
3481
3508
  }
3482
- changeVirtualHeight(topHeight, bottomHeight) {
3509
+ setVirtualSpaceHeight(topHeight, bottomHeight) {
3483
3510
  if (!this.virtualScrollInitialized) {
3484
3511
  return;
3485
3512
  }
@@ -3490,19 +3517,19 @@ class SlateEditable {
3490
3517
  const doc = this.elementRef?.nativeElement?.ownerDocument ?? document;
3491
3518
  VirtualScrollDebugOverlay.log(doc, type, ...args);
3492
3519
  }
3493
- doVirtualScroll() {
3520
+ tryUpdateVirtualViewport() {
3494
3521
  this.refreshVirtualViewAnimId && cancelAnimationFrame(this.refreshVirtualViewAnimId);
3495
3522
  this.refreshVirtualViewAnimId = requestAnimationFrame(() => {
3496
- let virtualView = this.refreshVirtualView();
3497
- let diff = this.diffVirtualView(virtualView);
3523
+ let virtualView = this.calculateVirtualViewport();
3524
+ let diff = this.diffVirtualViewport(virtualView);
3498
3525
  if (!diff.isDiff) {
3499
3526
  return;
3500
3527
  }
3501
3528
  if (diff.isMissingTop) {
3502
3529
  const result = this.remeasureHeightByIndics(diff.diffTopRenderedIndexes);
3503
3530
  if (result) {
3504
- virtualView = this.refreshVirtualView();
3505
- diff = this.diffVirtualView(virtualView, 'second');
3531
+ virtualView = this.calculateVirtualViewport();
3532
+ diff = this.diffVirtualViewport(virtualView, 'second');
3506
3533
  if (!diff.isDiff) {
3507
3534
  return;
3508
3535
  }
@@ -3510,7 +3537,7 @@ class SlateEditable {
3510
3537
  }
3511
3538
  this.applyVirtualView(virtualView);
3512
3539
  if (this.listRender.initialized) {
3513
- this.listRender.update(virtualView.renderedChildren, this.editor, this.context);
3540
+ this.listRender.update(virtualView.inViewportChildren, this.editor, this.context);
3514
3541
  if (!AngularEditor.isReadOnly(this.editor) && this.editor.selection) {
3515
3542
  this.toNativeSelection();
3516
3543
  }
@@ -3518,26 +3545,26 @@ class SlateEditable {
3518
3545
  this.scheduleMeasureVisibleHeights();
3519
3546
  });
3520
3547
  }
3521
- refreshVirtualView() {
3548
+ calculateVirtualViewport() {
3522
3549
  const children = (this.editor.children || []);
3523
- if (!children.length || !this.shouldUseVirtual()) {
3550
+ if (!children.length || !this.isEnabledVirtualScroll()) {
3524
3551
  return {
3525
- renderedChildren: children,
3552
+ inViewportChildren: children,
3526
3553
  visibleIndexes: new Set(),
3527
3554
  top: 0,
3528
3555
  bottom: 0,
3529
3556
  heights: []
3530
3557
  };
3531
3558
  }
3532
- const scrollTop = this.virtualConfig.scrollTop;
3559
+ const scrollTop = this.virtualScrollConfig.scrollTop;
3533
3560
  if (isDebug) {
3534
3561
  const doc = this.elementRef?.nativeElement?.ownerDocument ?? document;
3535
3562
  VirtualScrollDebugOverlay.syncScrollTop(doc, Number.isFinite(scrollTop) ? scrollTop : 0);
3536
3563
  }
3537
- const viewportHeight = this.virtualConfig.viewportHeight ?? 0;
3564
+ const viewportHeight = this.virtualScrollConfig.viewportHeight ?? 0;
3538
3565
  if (!viewportHeight) {
3539
3566
  return {
3540
- renderedChildren: [],
3567
+ inViewportChildren: [],
3541
3568
  visibleIndexes: new Set(),
3542
3569
  top: 0,
3543
3570
  bottom: 0,
@@ -3577,7 +3604,7 @@ class SlateEditable {
3577
3604
  const top = visibleStartIndex === -1 ? 0 : accumulatedHeights[visibleStartIndex];
3578
3605
  const bottom = totalHeight - accumulatedHeights[visibleEndIndex + 1];
3579
3606
  return {
3580
- renderedChildren: visible.length ? visible : children,
3607
+ inViewportChildren: visible.length ? visible : children,
3581
3608
  visibleIndexes: new Set(visibleIndexes),
3582
3609
  top,
3583
3610
  bottom,
@@ -3585,19 +3612,19 @@ class SlateEditable {
3585
3612
  };
3586
3613
  }
3587
3614
  applyVirtualView(virtualView) {
3588
- this.renderedChildren = virtualView.renderedChildren;
3589
- this.changeVirtualHeight(virtualView.top, virtualView.bottom);
3590
- this.virtualVisibleIndexes = virtualView.visibleIndexes;
3615
+ this.inViewportChildren = virtualView.inViewportChildren;
3616
+ this.setVirtualSpaceHeight(virtualView.top, virtualView.bottom);
3617
+ this.inViewportIndics = virtualView.visibleIndexes;
3591
3618
  }
3592
- diffVirtualView(virtualView, stage = 'first') {
3593
- if (!this.renderedChildren.length) {
3619
+ diffVirtualViewport(virtualView, stage = 'first') {
3620
+ if (!this.inViewportChildren.length) {
3594
3621
  return {
3595
3622
  isDiff: true,
3596
3623
  diffTopRenderedIndexes: [],
3597
3624
  diffBottomRenderedIndexes: []
3598
3625
  };
3599
3626
  }
3600
- const oldVisibleIndexes = [...this.virtualVisibleIndexes];
3627
+ const oldVisibleIndexes = [...this.inViewportIndics];
3601
3628
  const newVisibleIndexes = [...virtualView.visibleIndexes];
3602
3629
  const firstNewIndex = newVisibleIndexes[0];
3603
3630
  const lastNewIndex = newVisibleIndexes[newVisibleIndexes.length - 1];
@@ -3653,7 +3680,7 @@ class SlateEditable {
3653
3680
  }
3654
3681
  }
3655
3682
  if (isDebug) {
3656
- this.debugLog('log', `====== diffVirtualView stage: ${stage} ======`);
3683
+ this.debugLog('log', `====== diffVirtualViewport stage: ${stage} ======`);
3657
3684
  this.debugLog('log', 'oldVisibleIndexes:', oldVisibleIndexes);
3658
3685
  this.debugLog('log', 'newVisibleIndexes:', newVisibleIndexes);
3659
3686
  this.debugLog('log', 'diffTopRenderedIndexes:', isMissingTop ? '-' : isAddedTop ? '+' : '-', diffTopRenderedIndexes, diffTopRenderedIndexes.map(index => this.getBlockHeight(index, 0)));
@@ -3692,11 +3719,11 @@ class SlateEditable {
3692
3719
  return defaultHeight;
3693
3720
  }
3694
3721
  const key = AngularEditor.findKey(this.editor, node);
3695
- const height = this.measuredHeights.get(key.id);
3722
+ const height = this.keyHeightMap.get(key.id);
3696
3723
  if (typeof height === 'number') {
3697
3724
  return height;
3698
3725
  }
3699
- if (this.measuredHeights.has(key.id)) {
3726
+ if (this.keyHeightMap.has(key.id)) {
3700
3727
  console.error('getBlockHeight: invalid height value', key.id, height);
3701
3728
  }
3702
3729
  return defaultHeight;
@@ -3710,7 +3737,7 @@ class SlateEditable {
3710
3737
  return accumulatedHeights;
3711
3738
  }
3712
3739
  scheduleMeasureVisibleHeights() {
3713
- if (!this.shouldUseVirtual()) {
3740
+ if (!this.isEnabledVirtualScroll()) {
3714
3741
  return;
3715
3742
  }
3716
3743
  this.measureVisibleHeightsAnimId && cancelAnimationFrame(this.measureVisibleHeightsAnimId);
@@ -3720,14 +3747,14 @@ class SlateEditable {
3720
3747
  }
3721
3748
  measureVisibleHeights() {
3722
3749
  const children = (this.editor.children || []);
3723
- this.virtualVisibleIndexes.forEach(index => {
3750
+ this.inViewportIndics.forEach(index => {
3724
3751
  const node = children[index];
3725
3752
  if (!node) {
3726
3753
  return;
3727
3754
  }
3728
3755
  const key = AngularEditor.findKey(this.editor, node);
3729
3756
  // 跳过已测过的块,除非强制测量
3730
- if (this.measuredHeights.has(key.id)) {
3757
+ if (this.keyHeightMap.has(key.id)) {
3731
3758
  return;
3732
3759
  }
3733
3760
  const view = ELEMENT_TO_COMPONENT.get(node);
@@ -3737,11 +3764,11 @@ class SlateEditable {
3737
3764
  const ret = view.getRealHeight();
3738
3765
  if (ret instanceof Promise) {
3739
3766
  ret.then(height => {
3740
- this.measuredHeights.set(key.id, height);
3767
+ this.keyHeightMap.set(key.id, height);
3741
3768
  });
3742
3769
  }
3743
3770
  else {
3744
- this.measuredHeights.set(key.id, ret);
3771
+ this.keyHeightMap.set(key.id, ret);
3745
3772
  }
3746
3773
  });
3747
3774
  }
@@ -3758,12 +3785,12 @@ class SlateEditable {
3758
3785
  if (!view) {
3759
3786
  return;
3760
3787
  }
3761
- const prevHeight = this.measuredHeights.get(key.id);
3788
+ const prevHeight = this.keyHeightMap.get(key.id);
3762
3789
  const ret = view.getRealHeight();
3763
3790
  if (ret instanceof Promise) {
3764
3791
  ret.then(height => {
3765
3792
  if (height !== prevHeight) {
3766
- this.measuredHeights.set(key.id, height);
3793
+ this.keyHeightMap.set(key.id, height);
3767
3794
  isHeightChanged = true;
3768
3795
  if (isDebug) {
3769
3796
  this.debugLog('log', `remeasureHeightByIndics, index: ${index} prevHeight: ${prevHeight} newHeight: ${height}`);
@@ -3773,7 +3800,7 @@ class SlateEditable {
3773
3800
  }
3774
3801
  else {
3775
3802
  if (ret !== prevHeight) {
3776
- this.measuredHeights.set(key.id, ret);
3803
+ this.keyHeightMap.set(key.id, ret);
3777
3804
  isHeightChanged = true;
3778
3805
  if (isDebug) {
3779
3806
  this.debugLog('log', `remeasureHeightByIndics, index: ${index} prevHeight: ${prevHeight} newHeight: ${ret}`);
@@ -4793,6 +4820,7 @@ class BaseElementComponent extends BaseComponent {
4793
4820
  if (ELEMENT_TO_COMPONENT.get(this.element) === this) {
4794
4821
  ELEMENT_TO_COMPONENT.delete(this.element);
4795
4822
  }
4823
+ this.listRender.destroy();
4796
4824
  }
4797
4825
  onContextChange() {
4798
4826
  this.childrenContext = this.getChildrenContext();
@@ -4880,6 +4908,7 @@ class BaseTextComponent extends BaseComponent {
4880
4908
  NODE_TO_ELEMENT.delete(this.text);
4881
4909
  }
4882
4910
  ELEMENT_TO_NODE.delete(this.nativeElement);
4911
+ this.leavesRender.destroy();
4883
4912
  }
4884
4913
  onContextChange() {
4885
4914
  this.updateWeakMap();