slate-angular 16.1.0-next.14 → 16.1.0-next.15

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.
@@ -610,13 +610,17 @@ const AngularEditor = {
610
610
  /**
611
611
  * Find a Slate node from a native DOM `element`.
612
612
  */
613
- toSlateNode(editor, domNode) {
613
+ toSlateNode(editor, domNode, options) {
614
+ const { suppressThrow } = options || { suppressThrow: false };
614
615
  let domEl = isDOMElement(domNode) ? domNode : domNode.parentElement;
615
616
  if (domEl && !domEl.hasAttribute('data-slate-node')) {
616
617
  domEl = domEl.closest(`[data-slate-node]`);
617
618
  }
618
619
  const node = domEl ? ELEMENT_TO_NODE.get(domEl) : null;
619
620
  if (!node) {
621
+ if (suppressThrow) {
622
+ return null;
623
+ }
620
624
  throw new Error(`Cannot resolve a Slate node from DOM node: ${domEl}`);
621
625
  }
622
626
  return node;
@@ -632,7 +636,7 @@ const AngularEditor = {
632
636
  if (x == null || y == null) {
633
637
  throw new Error(`Cannot resolve a Slate range from a DOM event: ${event}`);
634
638
  }
635
- const node = AngularEditor.toSlateNode(editor, event.target);
639
+ const node = AngularEditor.toSlateNode(editor, event.target, { suppressThrow: false });
636
640
  const path = AngularEditor.findPath(editor, node);
637
641
  // If the drop target is inside a void node, move it into either the
638
642
  // next or previous node, depending on which side the `x` and `y`
@@ -668,18 +672,25 @@ const AngularEditor = {
668
672
  throw new Error(`Cannot resolve a Slate range from a DOM event: ${event}`);
669
673
  }
670
674
  // Resolve a Slate range from the DOM range.
671
- const range = AngularEditor.toSlateRange(editor, domRange);
675
+ const range = AngularEditor.toSlateRange(editor, domRange, { suppressThrow: false });
672
676
  return range;
673
677
  },
674
- isLeafInEditor(editor, leafNode) {
678
+ isLeafInEditor(editor, leafNode, options) {
679
+ const { suppressThrow } = options;
675
680
  const textNode = leafNode.closest('[data-slate-node="text"]');
676
- const node = AngularEditor.toSlateNode(editor, textNode);
677
- return AngularEditor.isNodeInEditor(editor, node);
681
+ const node = AngularEditor.toSlateNode(editor, textNode, { suppressThrow });
682
+ if (node && AngularEditor.isNodeInEditor(editor, node)) {
683
+ return true;
684
+ }
685
+ else {
686
+ return false;
687
+ }
678
688
  },
679
689
  /**
680
690
  * Find a Slate point from a DOM selection's `domNode` and `domOffset`.
681
691
  */
682
- toSlatePoint(editor, domPoint) {
692
+ toSlatePoint(editor, domPoint, options) {
693
+ const { exactMatch, suppressThrow } = options;
683
694
  const [domNode] = domPoint;
684
695
  const [nearestNode, nearestOffset] = normalizeDOMPoint(domPoint);
685
696
  let parentNode = nearestNode.parentNode;
@@ -726,7 +737,7 @@ const AngularEditor = {
726
737
  let domNode = null;
727
738
  // Calculate how far into the text node the `nearestNode` is, so that we
728
739
  // can determine what the offset relative to the text node is.
729
- if (leafNode && AngularEditor.isLeafInEditor(editor, leafNode)) {
740
+ if (leafNode && AngularEditor.isLeafInEditor(editor, leafNode, { suppressThrow: true })) {
730
741
  textNode = leafNode.closest('[data-slate-node="text"]');
731
742
  const window = AngularEditor.getWindow(editor);
732
743
  const range = window.document.createRange();
@@ -768,19 +779,26 @@ const AngularEditor = {
768
779
  }
769
780
  }
770
781
  if (!textNode) {
782
+ if (suppressThrow) {
783
+ return null;
784
+ }
771
785
  throw new Error(`Cannot resolve a Slate point from DOM point: ${domPoint}`);
772
786
  }
773
787
  // COMPAT: If someone is clicking from one Slate editor into another,
774
788
  // the select event fires twice, once for the old editor's `element`
775
789
  // first, and then afterwards for the correct `element`. (2017/03/03)
776
- const slateNode = AngularEditor.toSlateNode(editor, textNode);
790
+ const slateNode = AngularEditor.toSlateNode(editor, textNode, { suppressThrow });
791
+ if (!slateNode && suppressThrow) {
792
+ return null;
793
+ }
777
794
  const path = AngularEditor.findPath(editor, slateNode);
778
795
  return { path, offset };
779
796
  },
780
797
  /**
781
798
  * Find a Slate range from a DOM range or selection.
782
799
  */
783
- toSlateRange(editor, domRange) {
800
+ toSlateRange(editor, domRange, options) {
801
+ const { exactMatch, suppressThrow } = options || {};
784
802
  const el = isDOMSelection(domRange) ? domRange.anchorNode : domRange.startContainer;
785
803
  let anchorNode;
786
804
  let anchorOffset;
@@ -824,8 +842,14 @@ const AngularEditor = {
824
842
  focusNode = anchorNode;
825
843
  focusOffset = anchorNode.textContent?.length || 0;
826
844
  }
827
- const anchor = AngularEditor.toSlatePoint(editor, [anchorNode, anchorOffset]);
828
- const focus = isCollapsed ? anchor : AngularEditor.toSlatePoint(editor, [focusNode, focusOffset]);
845
+ const anchor = AngularEditor.toSlatePoint(editor, [anchorNode, anchorOffset], { suppressThrow, exactMatch });
846
+ if (!anchor) {
847
+ return null;
848
+ }
849
+ const focus = isCollapsed ? anchor : AngularEditor.toSlatePoint(editor, [focusNode, focusOffset], { suppressThrow, exactMatch });
850
+ if (!focus) {
851
+ return null;
852
+ }
829
853
  let range = { anchor: anchor, focus: focus };
830
854
  // if the selection is a hanging range that ends in a void
831
855
  // and the DOM focus is an Element
@@ -856,7 +880,7 @@ const AngularEditor = {
856
880
  },
857
881
  toSlateCardEntry(editor, node) {
858
882
  const element = node.parentElement.closest('.slate-block-card')?.querySelector('[card-target="card-center"]').firstElementChild;
859
- const slateNode = AngularEditor.toSlateNode(editor, element);
883
+ const slateNode = AngularEditor.toSlateNode(editor, element, { suppressThrow: false });
860
884
  const path = AngularEditor.findPath(editor, slateNode);
861
885
  return [slateNode, path];
862
886
  },
@@ -2461,6 +2485,7 @@ class BaseElementComponent extends BaseComponent {
2461
2485
  if (NODE_TO_ELEMENT.get(this.element) === this.nativeElement) {
2462
2486
  NODE_TO_ELEMENT.delete(this.element);
2463
2487
  }
2488
+ ELEMENT_TO_NODE.delete(this.nativeElement);
2464
2489
  if (ELEMENT_TO_COMPONENT.get(this.element) === this) {
2465
2490
  ELEMENT_TO_COMPONENT.delete(this.element);
2466
2491
  }
@@ -2534,6 +2559,7 @@ class BaseTextComponent extends BaseComponent {
2534
2559
  if (NODE_TO_ELEMENT.get(this.text) === this.nativeElement) {
2535
2560
  NODE_TO_ELEMENT.delete(this.text);
2536
2561
  }
2562
+ ELEMENT_TO_NODE.delete(this.nativeElement);
2537
2563
  }
2538
2564
  onContextChange() {
2539
2565
  this.updateWeakMap();
@@ -3117,12 +3143,11 @@ class SlateEditable {
3117
3143
  hasDomSelectionInEditor = true;
3118
3144
  }
3119
3145
  // If the DOM selection is in the editor and the editor selection is already correct, we're done.
3120
- if (hasDomSelection &&
3121
- hasDomSelectionInEditor &&
3122
- selection &&
3123
- hasStringTarget(domSelection) &&
3124
- Range.equals(AngularEditor.toSlateRange(this.editor, domSelection), selection)) {
3125
- return;
3146
+ if (hasDomSelection && hasDomSelectionInEditor && selection && hasStringTarget(domSelection)) {
3147
+ const rangeFromDOMSelection = AngularEditor.toSlateRange(this.editor, domSelection, { suppressThrow: true });
3148
+ if (rangeFromDOMSelection && Range.equals(rangeFromDOMSelection, selection)) {
3149
+ return;
3150
+ }
3126
3151
  }
3127
3152
  // prevent updating native selection when active element is void element
3128
3153
  if (isTargetInsideVoid(this.editor, activeElement)) {
@@ -3133,7 +3158,7 @@ class SlateEditable {
3133
3158
  // but Slate's value is not being updated through any operation
3134
3159
  // and thus it doesn't transform selection on its own
3135
3160
  if (selection && !AngularEditor.hasRange(this.editor, selection)) {
3136
- this.editor.selection = AngularEditor.toSlateRange(this.editor, domSelection);
3161
+ this.editor.selection = AngularEditor.toSlateRange(this.editor, domSelection, { suppressThrow: false });
3137
3162
  return;
3138
3163
  }
3139
3164
  // Otherwise the DOM selection is out of sync, so update it.