slate-angular 20.2.2 → 20.2.3

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.
@@ -5,7 +5,7 @@ import * as i0 from '@angular/core';
5
5
  import { TemplateRef, ComponentRef, IterableDiffers, inject, ViewContainerRef, forwardRef, HostBinding, Input, ChangeDetectionStrategy, Component, NgModule, ElementRef, ChangeDetectorRef, Directive, ViewChild } from '@angular/core';
6
6
  import { direction } from 'direction';
7
7
  import scrollIntoView from 'scroll-into-view-if-needed';
8
- import { Subject } from 'rxjs';
8
+ import { Subject, debounceTime } from 'rxjs';
9
9
  import { NG_VALUE_ACCESSOR } from '@angular/forms';
10
10
  import { HistoryEditor } from 'slate-history';
11
11
  import { CommonModule } from '@angular/common';
@@ -1491,11 +1491,7 @@ 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, isVisible) => {
1495
- const visible = isVisible ?? editor.isVisible(element);
1496
- if (!visible) {
1497
- return 0;
1498
- }
1494
+ const getRealHeightByElement = (editor, element, defaultHeight) => {
1499
1495
  const heights = ELEMENT_KEY_TO_HEIGHTS.get(editor);
1500
1496
  const key = AngularEditor.findKey(editor, element);
1501
1497
  const height = heights?.get(key.id);
@@ -1505,7 +1501,7 @@ const getRealHeightByElement = (editor, element, defaultHeight = VIRTUAL_SCROLL_
1505
1501
  if (heights?.has(key.id)) {
1506
1502
  console.error('getBlockHeight: invalid height value', key.id, height);
1507
1503
  }
1508
- return defaultHeight;
1504
+ return editor.getRoughHeight(element, defaultHeight);
1509
1505
  };
1510
1506
  const buildHeightsAndAccumulatedHeights = (editor) => {
1511
1507
  const children = (editor.children || []);
@@ -1514,9 +1510,7 @@ const buildHeightsAndAccumulatedHeights = (editor) => {
1514
1510
  const accumulatedHeights = new Array(children.length + 1);
1515
1511
  accumulatedHeights[0] = 0;
1516
1512
  for (let i = 0; i < children.length; i++) {
1517
- const isVisible = editor.isVisible(children[i]);
1518
- visibleStates[i] = isVisible;
1519
- const height = getRealHeightByElement(editor, children[i], VIRTUAL_SCROLL_DEFAULT_BLOCK_HEIGHT, isVisible);
1513
+ const height = getRealHeightByElement(editor, children[i], VIRTUAL_SCROLL_DEFAULT_BLOCK_HEIGHT);
1520
1514
  heights[i] = height;
1521
1515
  accumulatedHeights[i + 1] = accumulatedHeights[i] + height;
1522
1516
  }
@@ -1737,6 +1731,9 @@ const withAngular = (editor, clipboardFormatKey = 'x-slate-fragment') => {
1737
1731
  e.isVisible = element => {
1738
1732
  return true;
1739
1733
  };
1734
+ e.getRoughHeight = (element, defaultHeight) => {
1735
+ return defaultHeight === undefined ? VIRTUAL_SCROLL_DEFAULT_BLOCK_HEIGHT : defaultHeight;
1736
+ };
1740
1737
  return e;
1741
1738
  };
1742
1739
 
@@ -2341,7 +2338,6 @@ class BaseElementFlavour extends BaseFlavour {
2341
2338
  this.getOutletElement = () => {
2342
2339
  return this.nativeElement.querySelector('.children-outlet');
2343
2340
  };
2344
- this.stableHeight = null;
2345
2341
  }
2346
2342
  get element() {
2347
2343
  return this._context && this._context.element;
@@ -2428,20 +2424,11 @@ class BaseElementFlavour extends BaseFlavour {
2428
2424
  readonly: this._context.readonly
2429
2425
  };
2430
2426
  }
2431
- isStableHeight() {
2432
- return false;
2433
- }
2434
2427
  getRealHeight() {
2435
- if (this.isStableHeight() && this.stableHeight !== null) {
2436
- return this.stableHeight;
2437
- }
2438
2428
  const blockCard = getBlockCardByNativeElement(this.nativeElement);
2439
2429
  const target = blockCard || this.nativeElement;
2440
2430
  const computedStyle = getComputedStyle(target);
2441
2431
  const height = Math.ceil(target.getBoundingClientRect().height) + parseFloat(computedStyle.marginTop) + parseFloat(computedStyle.marginBottom);
2442
- if (this.isStableHeight()) {
2443
- this.stableHeight = height;
2444
- }
2445
2432
  return height;
2446
2433
  }
2447
2434
  }
@@ -3323,6 +3310,7 @@ class SlateEditable {
3323
3310
  this.inViewportChildren = [];
3324
3311
  this.inViewportIndics = [];
3325
3312
  this.keyHeightMap = new Map();
3313
+ this.viewportRefresh$ = new Subject();
3326
3314
  this.virtualScrollInitialized = false;
3327
3315
  }
3328
3316
  ngOnInit() {
@@ -3394,6 +3382,7 @@ class SlateEditable {
3394
3382
  const { preRenderingCount, childrenWithPreRendering, childrenWithPreRenderingIndics } = this.handlePreRendering();
3395
3383
  this.listRender.update(childrenWithPreRendering, this.editor, this.context, preRenderingCount, childrenWithPreRenderingIndics);
3396
3384
  }
3385
+ this.viewportRefresh$.next();
3397
3386
  }
3398
3387
  else {
3399
3388
  if (!this.listRender.initialized) {
@@ -3647,17 +3636,18 @@ class SlateEditable {
3647
3636
  diff = this.diffVirtualViewport(virtualView, 'second');
3648
3637
  }
3649
3638
  }
3650
- const oldInViewportChildren = this.inViewportChildren;
3639
+ // const oldInViewportChildren = this.inViewportChildren;
3651
3640
  this.applyVirtualView(virtualView);
3652
3641
  const { preRenderingCount, childrenWithPreRendering, childrenWithPreRenderingIndics } = this.handlePreRendering();
3653
3642
  this.listRender.update(childrenWithPreRendering, this.editor, this.context, preRenderingCount, childrenWithPreRenderingIndics);
3654
3643
  // 新增或者修改的才需要重算,计算出这个结果
3655
- const remeasureIndics = [];
3656
- this.inViewportChildren.forEach((child, index) => {
3657
- if (oldInViewportChildren.indexOf(child) === -1) {
3658
- remeasureIndics.push(this.inViewportIndics[index]);
3659
- }
3660
- });
3644
+ // const remeasureIndics = [];
3645
+ // this.inViewportChildren.forEach((child, index) => {
3646
+ // if (oldInViewportChildren.indexOf(child) === -1) {
3647
+ // remeasureIndics.push(this.inViewportIndics[index]);
3648
+ // }
3649
+ // });
3650
+ this.viewportRefresh$.next();
3661
3651
  }
3662
3652
  updateContext() {
3663
3653
  const decorations = this.generateDecorations();
@@ -3745,15 +3735,27 @@ class SlateEditable {
3745
3735
  if (entries.length > 0 && entries[0].contentRect.width !== editorResizeObserverRectWidth) {
3746
3736
  editorResizeObserverRectWidth = entries[0].contentRect.width;
3747
3737
  this.keyHeightMap.clear();
3748
- const remeasureIndics = this.inViewportIndics;
3749
- measureHeightByIndics(this.editor, remeasureIndics, true);
3750
3738
  EDITOR_TO_ROOT_NODE_WIDTH.set(this.editor, this.virtualTopHeightElement.getBoundingClientRect().width);
3739
+ this.viewportRefresh$.next();
3751
3740
  if (isDebug) {
3752
3741
  debugLog('log', 'editorResizeObserverRectWidth: ', editorResizeObserverRectWidth, 'EDITOR_TO_ROOT_NODE_WIDTH: ', EDITOR_TO_ROOT_NODE_WIDTH.get(this.editor));
3753
3742
  }
3754
3743
  }
3755
3744
  });
3756
3745
  this.editorResizeObserver.observe(this.elementRef.nativeElement);
3746
+ this.viewportRefresh$.pipe(debounceTime(1000)).subscribe(() => {
3747
+ // const res = measureHeightByIndics(this.editor, this.inViewportIndics);
3748
+ // if (isDebug) {
3749
+ // debugLog(
3750
+ // 'log',
3751
+ // 'viewportRefresh$ debounceTime 1000ms',
3752
+ // 'inViewportIndics: ',
3753
+ // this.inViewportIndics,
3754
+ // 'measureHeightByIndics height changed: ',
3755
+ // res
3756
+ // );
3757
+ // }
3758
+ });
3757
3759
  }
3758
3760
  }
3759
3761
  setVirtualSpaceHeight(topHeight, bottomHeight) {
@@ -3852,6 +3854,7 @@ class SlateEditable {
3852
3854
  if (!AngularEditor.isReadOnly(this.editor) && this.editor.selection) {
3853
3855
  this.toNativeSelection(false);
3854
3856
  }
3857
+ this.viewportRefresh$.next();
3855
3858
  }
3856
3859
  }
3857
3860
  if (isDebug) {
@@ -5023,7 +5026,6 @@ class BaseElementComponent extends BaseComponent {
5023
5026
  }
5024
5027
  return null;
5025
5028
  };
5026
- this.stableHeight = null;
5027
5029
  }
5028
5030
  get element() {
5029
5031
  return this._context && this._context.element;
@@ -5106,20 +5108,11 @@ class BaseElementComponent extends BaseComponent {
5106
5108
  readonly: this._context.readonly
5107
5109
  };
5108
5110
  }
5109
- isStableHeight() {
5110
- return false;
5111
- }
5112
5111
  getRealHeight() {
5113
- if (this.isStableHeight() && this.stableHeight !== null) {
5114
- return this.stableHeight;
5115
- }
5116
5112
  const blockCard = getBlockCardByNativeElement(this.nativeElement);
5117
5113
  const target = blockCard || this.nativeElement;
5118
5114
  const computedStyle = getComputedStyle(target);
5119
5115
  const height = Math.ceil(target.getBoundingClientRect().height) + parseFloat(computedStyle.marginTop) + parseFloat(computedStyle.marginBottom);
5120
- if (this.isStableHeight()) {
5121
- this.stableHeight = height;
5122
- }
5123
5116
  return height;
5124
5117
  }
5125
5118
  static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.3.12", ngImport: i0, type: BaseElementComponent, deps: null, target: i0.ɵɵFactoryTarget.Directive }); }