slate-angular 20.2.16 → 20.2.18

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.
@@ -1492,10 +1492,17 @@ const getBlockCardByNativeElement = (nativeElement) => {
1492
1492
  return null;
1493
1493
  };
1494
1494
 
1495
+ const roundTo = (value, precision = 2) => {
1496
+ const factor = 10 ** precision;
1497
+ const n = Math.round(value * factor) / factor;
1498
+ return Object.is(n, -0) ? 0 : n;
1499
+ };
1500
+
1495
1501
  const isDebug = localStorage.getItem(SLATE_DEBUG_KEY) === 'true';
1496
1502
  const isDebugScrollTop = localStorage.getItem(SLATE_DEBUG_KEY_SCROLL_TOP) === 'true';
1497
1503
  const ELEMENT_KEY_TO_HEIGHTS = new WeakMap();
1498
1504
  const EDITOR_TO_BUSINESS_TOP = new WeakMap();
1505
+ const EDITOR_TO_VIEWPORT_HEIGHT = new WeakMap();
1499
1506
  const EDITOR_TO_ROOT_NODE_WIDTH = new WeakMap();
1500
1507
  const EDITOR_TO_IS_FROM_SCROLL_TO = new WeakMap();
1501
1508
  const isValidNumber = (value) => {
@@ -1573,7 +1580,7 @@ const measureHeightByIndics = (editor, indics, force = false) => {
1573
1580
  hasChanged = true;
1574
1581
  }
1575
1582
  if (isDebug && isValidNumber(currentHeight)) {
1576
- debugLog('log', 'measureHeightByIndics: height not equal, index: ', index, 'preHeight: ', preHeight, 'height: ', currentHeight);
1583
+ debugLog('log', 'measureHeightByIndics: index: ', index, 'preHeight: ', preHeight, 'height: ', currentHeight);
1577
1584
  }
1578
1585
  });
1579
1586
  return hasChanged;
@@ -1581,6 +1588,9 @@ const measureHeightByIndics = (editor, indics, force = false) => {
1581
1588
  const getBusinessTop = (editor) => {
1582
1589
  return EDITOR_TO_BUSINESS_TOP.get(editor) ?? 0;
1583
1590
  };
1591
+ const getViewportHeight = (editor) => {
1592
+ return EDITOR_TO_VIEWPORT_HEIGHT.get(editor) ?? window.innerHeight;
1593
+ };
1584
1594
  const getCachedHeightByElement = (editor, element) => {
1585
1595
  const heights = ELEMENT_KEY_TO_HEIGHTS.get(editor);
1586
1596
  const key = AngularEditor.findKey(editor, element);
@@ -1616,7 +1626,8 @@ const buildHeightsAndAccumulatedHeights = (editor, visibleStates) => {
1616
1626
  };
1617
1627
  const calculateVirtualTopHeight = (editor, startIndex, visibleStates) => {
1618
1628
  const { accumulatedHeights } = buildHeightsAndAccumulatedHeights(editor, visibleStates);
1619
- return accumulatedHeights[startIndex] ?? 0;
1629
+ const virtualTopHeight = roundTo(accumulatedHeights[startIndex] ?? 0, 1);
1630
+ return virtualTopHeight;
1620
1631
  };
1621
1632
  const scrollToElement = (editor, element, scrollTo) => {
1622
1633
  const children = editor.children;
@@ -3384,8 +3395,6 @@ class SlateEditable {
3384
3395
  this.virtualScrollConfig = {
3385
3396
  enabled: false,
3386
3397
  scrollTop: 0,
3387
- viewportHeight: 0,
3388
- viewportBoundingTop: 0,
3389
3398
  scrollContainer: null
3390
3399
  };
3391
3400
  this.inViewportChildren = [];
@@ -3548,6 +3557,17 @@ class SlateEditable {
3548
3557
  }
3549
3558
  });
3550
3559
  this.editorResizeObserver.observe(this.elementRef.nativeElement);
3560
+ if (this.virtualScrollConfig.scrollContainer) {
3561
+ this.editorScrollContainerResizeObserver = new ResizeObserver(entries => {
3562
+ const height = this.virtualScrollConfig.scrollContainer.getBoundingClientRect().height;
3563
+ EDITOR_TO_VIEWPORT_HEIGHT.set(this.editor, height);
3564
+ if (isDebug) {
3565
+ debugLog('log', 'editorScrollContainerResizeObserver calc viewport height: ', height);
3566
+ this.virtualTopHeightElement.setAttribute('viewport-height', height.toString());
3567
+ }
3568
+ });
3569
+ this.editorScrollContainerResizeObserver.observe(this.virtualScrollConfig.scrollContainer);
3570
+ }
3551
3571
  let pendingRemeasureIndics = [];
3552
3572
  this.indicsOfNeedBeMeasured$
3553
3573
  .pipe(tap((previousValue) => {
@@ -3569,6 +3589,17 @@ class SlateEditable {
3569
3589
  });
3570
3590
  }
3571
3591
  }
3592
+ calcBusinessTop() {
3593
+ const virtualTopBoundingTop = this.virtualTopHeightElement.getBoundingClientRect()?.top ?? 0;
3594
+ const viewportBoundingTop = this.virtualScrollConfig.scrollContainer?.getBoundingClientRect()?.top ?? 0;
3595
+ const businessTop = Math.ceil(virtualTopBoundingTop) + Math.ceil(this.virtualScrollConfig.scrollTop) - Math.floor(viewportBoundingTop);
3596
+ EDITOR_TO_BUSINESS_TOP.set(this.editor, businessTop);
3597
+ if (isDebug) {
3598
+ debugLog('log', 'calcBusinessTop: ', businessTop);
3599
+ this.virtualTopHeightElement.setAttribute('data-business-top', businessTop.toString());
3600
+ }
3601
+ return businessTop;
3602
+ }
3572
3603
  getChangedIndics(previousValue) {
3573
3604
  const remeasureIndics = [];
3574
3605
  this.inViewportChildren.forEach((child, index) => {
@@ -3582,9 +3613,9 @@ class SlateEditable {
3582
3613
  if (!this.virtualScrollInitialized) {
3583
3614
  return;
3584
3615
  }
3585
- this.virtualTopHeightElement.style.height = `${topHeight}px`;
3616
+ this.virtualTopHeightElement.style.height = `${roundTo(topHeight, 1)}px`;
3586
3617
  if (bottomHeight !== undefined) {
3587
- this.virtualBottomHeightElement.style.height = `${bottomHeight}px`;
3618
+ this.virtualBottomHeightElement.style.height = `${roundTo(bottomHeight, 1)}px`;
3588
3619
  }
3589
3620
  }
3590
3621
  getActualVirtualTopHeight() {
@@ -3629,7 +3660,7 @@ class SlateEditable {
3629
3660
  const refreshVirtualTopHeight = calculateVirtualTopHeight(this.editor, this.inViewportIndics[0], visibleStates);
3630
3661
  if (topHeight !== refreshVirtualTopHeight) {
3631
3662
  if (isDebug) {
3632
- debugLog('log', 'update top height since dirty state(正数减去高度,负数代表增加高度): ', topHeight - refreshVirtualTopHeight);
3663
+ debugLog('log', 'update top height since dirty state,增加高度: ', refreshVirtualTopHeight - topHeight);
3633
3664
  }
3634
3665
  this.setVirtualSpaceHeight(refreshVirtualTopHeight);
3635
3666
  return;
@@ -3668,9 +3699,11 @@ class SlateEditable {
3668
3699
  const newHeights = buildHeightsAndAccumulatedHeights(this.editor, visibleStates);
3669
3700
  const actualTopHeightAfterAdd = newHeights.accumulatedHeights[startIndexBeforeAdd];
3670
3701
  const newTopHeight = virtualView.top - (actualTopHeightAfterAdd - topHeightBeforeAdd);
3671
- this.setVirtualSpaceHeight(newTopHeight);
3672
- if (isDebug) {
3673
- debugLog('log', `update top height since will add element in top(正数减去高度,负数代表增加高度): ${actualTopHeightAfterAdd - topHeightBeforeAdd}`);
3702
+ if (topHeightBeforeAdd !== actualTopHeightAfterAdd) {
3703
+ this.setVirtualSpaceHeight(newTopHeight);
3704
+ if (isDebug) {
3705
+ debugLog('log', `update top height since will add element in top,减去高度: ${topHeightBeforeAdd - actualTopHeightAfterAdd}`);
3706
+ }
3674
3707
  }
3675
3708
  }
3676
3709
  }
@@ -3696,32 +3729,12 @@ class SlateEditable {
3696
3729
  };
3697
3730
  }
3698
3731
  const scrollTop = this.virtualScrollConfig.scrollTop;
3699
- let viewportHeight = this.virtualScrollConfig.viewportHeight ?? 0;
3700
- if (!viewportHeight) {
3701
- return {
3702
- inViewportChildren: [],
3703
- inViewportIndics: [],
3704
- top: 0,
3705
- bottom: 0,
3706
- heights: []
3707
- };
3708
- }
3732
+ let viewportHeight = getViewportHeight(this.editor);
3709
3733
  const elementLength = children.length;
3710
- if (!EDITOR_TO_BUSINESS_TOP.has(this.editor)) {
3711
- EDITOR_TO_BUSINESS_TOP.set(this.editor, 0);
3712
- setTimeout(() => {
3713
- const virtualTopBoundingTop = this.virtualTopHeightElement.getBoundingClientRect()?.top ?? 0;
3714
- const businessTop = Math.ceil(virtualTopBoundingTop) +
3715
- Math.ceil(this.virtualScrollConfig.scrollTop) -
3716
- Math.floor(this.virtualScrollConfig.viewportBoundingTop);
3717
- EDITOR_TO_BUSINESS_TOP.set(this.editor, businessTop);
3718
- if (isDebug) {
3719
- debugLog('log', 'businessTop', businessTop);
3720
- this.virtualTopHeightElement.setAttribute('data-business-top', businessTop.toString());
3721
- }
3722
- }, 100);
3734
+ let businessTop = getBusinessTop(this.editor);
3735
+ if (businessTop === 0 && this.virtualScrollConfig.scrollTop > 0) {
3736
+ businessTop = this.calcBusinessTop();
3723
3737
  }
3724
- const businessTop = getBusinessTop(this.editor);
3725
3738
  const { heights, accumulatedHeights } = buildHeightsAndAccumulatedHeights(this.editor, visibleStates);
3726
3739
  const totalHeight = accumulatedHeights[elementLength] + businessTop;
3727
3740
  let startPosition = Math.max(scrollTop - businessTop, 0);
@@ -3952,6 +3965,12 @@ class SlateEditable {
3952
3965
  if (editorElement.contains(domSelection.anchorNode) && editorElement.contains(domSelection.focusNode)) {
3953
3966
  hasDomSelectionInEditor = true;
3954
3967
  }
3968
+ if (!hasDomSelectionInEditor && !AngularEditor.isFocused(this.editor)) {
3969
+ return;
3970
+ }
3971
+ if (AngularEditor.isReadOnly(this.editor) && (!selection || Range.isCollapsed(selection))) {
3972
+ return;
3973
+ }
3955
3974
  // If the DOM selection is in the editor and the editor selection is already correct, we're done.
3956
3975
  if (hasDomSelection && hasDomSelectionInEditor && selection && hasStringTarget(domSelection)) {
3957
3976
  const rangeFromDOMSelection = AngularEditor.toSlateRange(this.editor, domSelection, {
@@ -4897,6 +4916,7 @@ class SlateEditable {
4897
4916
  //#endregion
4898
4917
  ngOnDestroy() {
4899
4918
  this.editorResizeObserver?.disconnect();
4919
+ this.editorScrollContainerResizeObserver?.disconnect();
4900
4920
  NODE_TO_ELEMENT.delete(this.editor);
4901
4921
  this.manualListeners.forEach(manualListener => {
4902
4922
  manualListener();
@@ -5422,5 +5442,5 @@ i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.3.12", ngImpo
5422
5442
  * Generated bundle index. Do not edit.
5423
5443
  */
5424
5444
 
5425
- export { AngularEditor, BaseComponent, BaseElementComponent, BaseElementFlavour, BaseFlavour, BaseLeafComponent, BaseLeafFlavour, BaseTextComponent, BaseTextFlavour, BlockCardRef, DEFAULT_ELEMENT_HEIGHT, DefaultTextFlavour, EDITOR_TO_AFTER_VIEW_INIT_QUEUE, EDITOR_TO_BUSINESS_TOP, EDITOR_TO_IS_FROM_SCROLL_TO, EDITOR_TO_ROOT_NODE_WIDTH, 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, SLATE_DEBUG_KEY_SCROLL_TOP, SlateBlockCard, SlateChildrenOutlet, SlateEditable, SlateErrorCode, SlateFragmentAttributeKey, SlateModule, SlateString, VIRTUAL_SCROLL_DEFAULT_BLOCK_HEIGHT, VoidTextFlavour, blobAsString, buildHTMLText, buildHeightsAndAccumulatedHeights, cacheHeightByElement, calcHeightByElement, calculateVirtualTopHeight, check, clearMinHeightByElement, completeTable, createClipboardData, createText, createThrottleRAF, debugLog, defaultScrollSelectionIntoView, fallbackCopyText, getBlockCardByNativeElement, getBusinessTop, getCachedHeightByElement, 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, isDebug, isDebugScrollTop, isDecoratorRangeListEqual, isFlavourType, isInvalidTable, isTemplateRef, isValid, isValidNumber, measureHeightByIndics, normalize, scrollToElement, setClipboardData, setDataTransferClipboard, setDataTransferClipboardText, setMinHeightByElement, setNavigatorClipboard, shallowCompare, stripHtml, withAngular };
5445
+ export { AngularEditor, BaseComponent, BaseElementComponent, BaseElementFlavour, BaseFlavour, BaseLeafComponent, BaseLeafFlavour, BaseTextComponent, BaseTextFlavour, BlockCardRef, DEFAULT_ELEMENT_HEIGHT, DefaultTextFlavour, EDITOR_TO_AFTER_VIEW_INIT_QUEUE, EDITOR_TO_BUSINESS_TOP, EDITOR_TO_IS_FROM_SCROLL_TO, EDITOR_TO_ROOT_NODE_WIDTH, EDITOR_TO_VIEWPORT_HEIGHT, 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, SLATE_DEBUG_KEY_SCROLL_TOP, SlateBlockCard, SlateChildrenOutlet, SlateEditable, SlateErrorCode, SlateFragmentAttributeKey, SlateModule, SlateString, VIRTUAL_SCROLL_DEFAULT_BLOCK_HEIGHT, VoidTextFlavour, blobAsString, buildHTMLText, buildHeightsAndAccumulatedHeights, cacheHeightByElement, calcHeightByElement, calculateVirtualTopHeight, check, clearMinHeightByElement, completeTable, createClipboardData, createText, createThrottleRAF, debugLog, defaultScrollSelectionIntoView, fallbackCopyText, getBlockCardByNativeElement, getBusinessTop, getCachedHeightByElement, getCardTargetAttribute, getClipboardData, getClipboardFromHTMLText, getContentHeight, getDataTransferClipboard, getDataTransferClipboardText, getNavigatorClipboard, getPlainText, getSelection, getSlateFragmentAttribute, getViewportHeight, getZeroTextNode, hasAfterContextChange, hasBeforeContextChange, hasBlockCard, hasBlockCardWithNode, hotkeys, isCardCenterByTargetAttr, isCardLeft, isCardLeftByTargetAttr, isCardRightByTargetAttr, isClipboardFile, isClipboardReadSupported, isClipboardWriteSupported, isClipboardWriteTextSupported, isComponentType, isDOMText, isDebug, isDebugScrollTop, isDecoratorRangeListEqual, isFlavourType, isInvalidTable, isTemplateRef, isValid, isValidNumber, measureHeightByIndics, normalize, roundTo, scrollToElement, setClipboardData, setDataTransferClipboard, setDataTransferClipboardText, setMinHeightByElement, setNavigatorClipboard, shallowCompare, stripHtml, withAngular };
5426
5446
  //# sourceMappingURL=slate-angular.mjs.map