slate-angular 20.2.0-next.1 → 20.2.0-next.2

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.
@@ -2556,6 +2556,7 @@ function executeAfterViewInit(editor) {
2556
2556
  const JUST_NOW_UPDATED_VIRTUAL_VIEW = new WeakMap();
2557
2557
  // not correctly clipboardData on beforeinput
2558
2558
  const forceOnDOMPaste = IS_SAFARI;
2559
+ const isDebug = localStorage.getItem(SLATE_DEBUG_KEY) === 'true';
2559
2560
  class SlateEditable {
2560
2561
  set virtualScroll(config) {
2561
2562
  this.virtualConfig = config;
@@ -2563,12 +2564,26 @@ class SlateEditable {
2563
2564
  this.refreshVirtualViewAnimId = requestAnimationFrame(() => {
2564
2565
  const virtualView = this.refreshVirtualView();
2565
2566
  const diff = this.diffVirtualView(virtualView);
2566
- if (diff) {
2567
- this.applyVirtualView(virtualView);
2568
- if (this.listRender.initialized) {
2569
- this.listRender.update(virtualView.renderedChildren, this.editor, this.context);
2567
+ if (diff.isDiff) {
2568
+ if (diff.isMissingTop || diff.isMissingBottom) {
2569
+ this.measureHeightByIndexes([...diff.diffTopRenderedIndexes, ...diff.diffBottomRenderedIndexes], true).then(result => {
2570
+ if (isDebug) {
2571
+ console.log('async measureHeightByIndexes:', result);
2572
+ }
2573
+ this.applyVirtualView(result || virtualView);
2574
+ if (this.listRender.initialized) {
2575
+ this.listRender.update(this.renderedChildren, this.editor, this.context);
2576
+ }
2577
+ this.scheduleMeasureVisibleHeights();
2578
+ });
2579
+ }
2580
+ else {
2581
+ this.applyVirtualView(virtualView);
2582
+ if (this.listRender.initialized) {
2583
+ this.listRender.update(virtualView.renderedChildren, this.editor, this.context);
2584
+ }
2585
+ this.scheduleMeasureVisibleHeights();
2570
2586
  }
2571
- this.scheduleMeasureVisibleHeights();
2572
2587
  }
2573
2588
  });
2574
2589
  }
@@ -3010,65 +3025,72 @@ class SlateEditable {
3010
3025
  }
3011
3026
  diffVirtualView(virtualView) {
3012
3027
  if (!this.renderedChildren.length) {
3013
- return true;
3028
+ return {
3029
+ isDiff: true,
3030
+ diffTopRenderedIndexes: [],
3031
+ diffBottomRenderedIndexes: []
3032
+ };
3014
3033
  }
3015
3034
  const oldVisibleIndexes = [...this.virtualVisibleIndexes];
3016
3035
  const newVisibleIndexes = [...virtualView.visibleIndexes];
3017
- if (newVisibleIndexes[0] !== oldVisibleIndexes[0] ||
3018
- newVisibleIndexes[newVisibleIndexes.length - 1] !== oldVisibleIndexes[oldVisibleIndexes.length - 1]) {
3019
- if (localStorage.getItem(SLATE_DEBUG_KEY) === 'true') {
3020
- const diffTopRenderedIndexes = [];
3021
- const diffBottomRenderedIndexes = [];
3022
- let direction = '';
3023
- if (newVisibleIndexes[0] > oldVisibleIndexes[0]) {
3024
- // 向下
3025
- direction = 'down';
3026
- for (let index = 0; index < oldVisibleIndexes.length; index++) {
3027
- const element = oldVisibleIndexes[index];
3028
- if (!newVisibleIndexes.includes(element)) {
3029
- diffTopRenderedIndexes.push(element);
3030
- }
3031
- else {
3032
- break;
3033
- }
3036
+ const firstNewIndex = newVisibleIndexes[0];
3037
+ const lastNewIndex = newVisibleIndexes[newVisibleIndexes.length - 1];
3038
+ const firstOldIndex = oldVisibleIndexes[0];
3039
+ const lastOldIndex = oldVisibleIndexes[oldVisibleIndexes.length - 1];
3040
+ if (firstNewIndex !== firstOldIndex || lastNewIndex !== lastOldIndex) {
3041
+ const diffTopRenderedIndexes = [];
3042
+ const diffBottomRenderedIndexes = [];
3043
+ const isMissingTop = firstNewIndex !== firstOldIndex && firstNewIndex > firstOldIndex;
3044
+ const isAddedTop = firstNewIndex !== firstOldIndex && firstNewIndex < firstOldIndex;
3045
+ const isMissingBottom = lastNewIndex !== lastOldIndex && lastOldIndex > lastNewIndex;
3046
+ const isAddedBottom = lastNewIndex !== lastOldIndex && lastOldIndex < lastNewIndex;
3047
+ if (isMissingTop || isAddedBottom) {
3048
+ // 向下
3049
+ for (let index = 0; index < oldVisibleIndexes.length; index++) {
3050
+ const element = oldVisibleIndexes[index];
3051
+ if (!newVisibleIndexes.includes(element)) {
3052
+ diffTopRenderedIndexes.push(element);
3034
3053
  }
3035
- for (let index = newVisibleIndexes.length - 1; index >= 0; index--) {
3036
- const element = newVisibleIndexes[index];
3037
- if (!oldVisibleIndexes.includes(element)) {
3038
- diffBottomRenderedIndexes.push(element);
3039
- }
3040
- else {
3041
- break;
3042
- }
3054
+ else {
3055
+ break;
3043
3056
  }
3044
3057
  }
3045
- else {
3046
- // 向上
3047
- direction = 'up';
3048
- for (let index = 0; index < newVisibleIndexes.length; index++) {
3049
- const element = newVisibleIndexes[index];
3050
- if (!oldVisibleIndexes.includes(element)) {
3051
- diffTopRenderedIndexes.push(element);
3052
- }
3053
- else {
3054
- break;
3055
- }
3058
+ for (let index = newVisibleIndexes.length - 1; index >= 0; index--) {
3059
+ const element = newVisibleIndexes[index];
3060
+ if (!oldVisibleIndexes.includes(element)) {
3061
+ diffBottomRenderedIndexes.push(element);
3056
3062
  }
3057
- for (let index = oldVisibleIndexes.length - 1; index >= 0; index--) {
3058
- const element = oldVisibleIndexes[index];
3059
- if (!newVisibleIndexes.includes(element)) {
3060
- diffBottomRenderedIndexes.push(element);
3061
- }
3062
- else {
3063
- break;
3064
- }
3063
+ else {
3064
+ break;
3065
+ }
3066
+ }
3067
+ }
3068
+ else if (isAddedTop || isMissingBottom) {
3069
+ // 向上
3070
+ for (let index = 0; index < newVisibleIndexes.length; index++) {
3071
+ const element = newVisibleIndexes[index];
3072
+ if (!oldVisibleIndexes.includes(element)) {
3073
+ diffTopRenderedIndexes.push(element);
3074
+ }
3075
+ else {
3076
+ break;
3065
3077
  }
3066
3078
  }
3079
+ for (let index = oldVisibleIndexes.length - 1; index >= 0; index--) {
3080
+ const element = oldVisibleIndexes[index];
3081
+ if (!newVisibleIndexes.includes(element)) {
3082
+ diffBottomRenderedIndexes.push(element);
3083
+ }
3084
+ else {
3085
+ break;
3086
+ }
3087
+ }
3088
+ }
3089
+ if (isDebug) {
3067
3090
  console.log('oldVisibleIndexes:', oldVisibleIndexes);
3068
3091
  console.log('newVisibleIndexes:', newVisibleIndexes);
3069
- const directionStr = direction === 'down' ? '+' : '-';
3070
- console.log('diffTopRenderedIndexes:', directionStr, diffTopRenderedIndexes, diffTopRenderedIndexes.map(index => virtualView.heights[index]));
3071
- console.log('diffBottomRenderedIndexes:', directionStr, diffBottomRenderedIndexes, diffBottomRenderedIndexes.map(index => virtualView.heights[index]));
3092
+ console.log('diffTopRenderedIndexes:', isMissingTop ? '-' : isAddedTop ? '+' : '-', diffTopRenderedIndexes, diffTopRenderedIndexes.map(index => this.getBlockHeight(index, 0)));
3093
+ console.log('diffBottomRenderedIndexes:', isAddedBottom ? '+' : isMissingBottom ? '-' : '+', diffBottomRenderedIndexes, diffBottomRenderedIndexes.map(index => this.getBlockHeight(index, 0)));
3072
3094
  const needTop = virtualView.heights.slice(0, newVisibleIndexes[0]).reduce((acc, height) => acc + height, 0);
3073
3095
  const needBottom = virtualView.heights
3074
3096
  .slice(newVisibleIndexes[newVisibleIndexes.length - 1] + 1)
@@ -3077,17 +3099,29 @@ class SlateEditable {
3077
3099
  console.log('newBottomHeight:', needBottom, 'prevBottomHeight:', parseFloat(this.virtualBottomHeightElement.style.height));
3078
3100
  console.warn('=========== Dividing line ===========');
3079
3101
  }
3080
- return true;
3102
+ return {
3103
+ isDiff: true,
3104
+ isMissingTop,
3105
+ isAddedTop,
3106
+ isMissingBottom,
3107
+ isAddedBottom,
3108
+ diffTopRenderedIndexes,
3109
+ diffBottomRenderedIndexes
3110
+ };
3081
3111
  }
3082
- return false;
3112
+ return {
3113
+ isDiff: false,
3114
+ diffTopRenderedIndexes: [],
3115
+ diffBottomRenderedIndexes: []
3116
+ };
3083
3117
  }
3084
- getBlockHeight(index) {
3118
+ getBlockHeight(index, defaultHeight = VIRTUAL_SCROLL_DEFAULT_BLOCK_HEIGHT) {
3085
3119
  const node = this.editor.children[index];
3086
3120
  if (!node) {
3087
- return VIRTUAL_SCROLL_DEFAULT_BLOCK_HEIGHT;
3121
+ return defaultHeight;
3088
3122
  }
3089
3123
  const key = AngularEditor.findKey(this.editor, node);
3090
- return this.measuredHeights.get(key.id) ?? VIRTUAL_SCROLL_DEFAULT_BLOCK_HEIGHT;
3124
+ return this.measuredHeights.get(key.id) ?? defaultHeight;
3091
3125
  }
3092
3126
  buildAccumulatedHeight(heights) {
3093
3127
  const accumulatedHeights = new Array(heights.length + 1).fill(0);
@@ -3146,6 +3180,42 @@ class SlateEditable {
3146
3180
  });
3147
3181
  });
3148
3182
  }
3183
+ async measureHeightByIndexes(indexes, isRefresh = false) {
3184
+ const children = (this.editor.children || []);
3185
+ let isHeightChanged = false;
3186
+ const promises = [];
3187
+ indexes.forEach(index => {
3188
+ const node = children[index];
3189
+ if (!node) {
3190
+ return;
3191
+ }
3192
+ const key = AngularEditor.findKey(this.editor, node);
3193
+ const view = ELEMENT_TO_COMPONENT.get(node);
3194
+ if (!view) {
3195
+ return;
3196
+ }
3197
+ const promise = view.getRealHeight()?.then(height => {
3198
+ const prevHeight = this.measuredHeights.get(key.id);
3199
+ if (isDebug) {
3200
+ console.log('measureHeightByIndexes: get index:', index, 'prevHeight:', prevHeight, 'newHeight:', height);
3201
+ }
3202
+ if (prevHeight && height !== prevHeight) {
3203
+ this.measuredHeights.set(key.id, height);
3204
+ isHeightChanged = true;
3205
+ }
3206
+ });
3207
+ if (promise) {
3208
+ promises.push(promise);
3209
+ }
3210
+ });
3211
+ if (promises.length > 0) {
3212
+ await Promise.all(promises);
3213
+ if (isHeightChanged && isRefresh) {
3214
+ return this.refreshVirtualView();
3215
+ }
3216
+ }
3217
+ return null;
3218
+ }
3149
3219
  //#region event proxy
3150
3220
  addEventListener(eventName, listener, target = this.elementRef.nativeElement) {
3151
3221
  this.manualListeners.push(this.renderer2.listen(target, eventName, (event) => {