slate-react 0.110.2 → 0.110.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.
package/dist/index.js CHANGED
@@ -258,6 +258,7 @@ typeof globalThis.InputEvent.prototype.getTargetRanges === 'function';
258
258
  * Two weak maps that allow us rebuild a path given a node. They are populated
259
259
  * at render time such that after a render occurs we can always backtrack.
260
260
  */
261
+ var IS_NODE_MAP_DIRTY = new WeakMap();
261
262
  var NODE_TO_INDEX = new WeakMap();
262
263
  var NODE_TO_PARENT = new WeakMap();
263
264
  /**
@@ -1872,6 +1873,9 @@ function createAndroidInputManager(_ref) {
1872
1873
  clearTimeout(flushTimeoutId);
1873
1874
  flushTimeoutId = null;
1874
1875
  }
1876
+ if (IS_NODE_MAP_DIRTY.get(editor)) {
1877
+ return;
1878
+ }
1875
1879
  var type = event.inputType;
1876
1880
  var targetRange = null;
1877
1881
  var data = event.dataTransfer || event.data || undefined;
@@ -2874,6 +2878,7 @@ var useChildren = function useChildren(props) {
2874
2878
  selection = props.selection;
2875
2879
  var decorate = useDecorate();
2876
2880
  var editor = useSlateStatic();
2881
+ IS_NODE_MAP_DIRTY.set(editor, false);
2877
2882
  var path = ReactEditor.findPath(editor, node);
2878
2883
  var children = [];
2879
2884
  var isLeafBlock = slate.Element.isElement(node) && !editor.isInline(node) && slate.Editor.hasInlines(editor, node);
@@ -3377,6 +3382,10 @@ var Editable = /*#__PURE__*/React.forwardRef(function (props, forwardedRef) {
3377
3382
  // while a selection is being dragged.
3378
3383
  var onDOMSelectionChange = React.useMemo(function () {
3379
3384
  return throttle(function () {
3385
+ if (IS_NODE_MAP_DIRTY.get(editor)) {
3386
+ onDOMSelectionChange();
3387
+ return;
3388
+ }
3380
3389
  var el = ReactEditor.toDOMNode(editor, editor);
3381
3390
  var root = el.getRootNode();
3382
3391
  if (!processing.current && IS_WEBKIT && root instanceof ShadowRoot) {
@@ -3631,48 +3640,52 @@ var Editable = /*#__PURE__*/React.forwardRef(function (props, forwardedRef) {
3631
3640
  // When there is an inline element, e.g. a link, and you select
3632
3641
  // right after it (the start of the next node).
3633
3642
  selection.anchor.offset !== 0) {
3634
- var _node$parentElement, _window$getComputedSt;
3635
3643
  _native = true;
3636
3644
  // Skip native if there are marks, as
3637
3645
  // `insertText` will insert a node, not just text.
3638
3646
  if (editor.marks) {
3639
3647
  _native = false;
3640
3648
  }
3641
- // Chrome also has issues correctly editing the end of anchor elements: https://bugs.chromium.org/p/chromium/issues/detail?id=1259100
3642
- // Therefore we don't allow native events to insert text at the end of anchor nodes.
3643
- var anchor = selection.anchor;
3644
- var _ReactEditor$toDOMPoi = ReactEditor.toDOMPoint(editor, anchor),
3645
- _ReactEditor$toDOMPoi2 = _slicedToArray(_ReactEditor$toDOMPoi, 2),
3646
- node = _ReactEditor$toDOMPoi2[0],
3647
- offset = _ReactEditor$toDOMPoi2[1];
3648
- var anchorNode = (_node$parentElement = node.parentElement) === null || _node$parentElement === void 0 ? void 0 : _node$parentElement.closest('a');
3649
- var _window = ReactEditor.getWindow(editor);
3650
- if (_native && anchorNode && ReactEditor.hasDOMNode(editor, anchorNode)) {
3651
- var _lastText$textContent;
3652
- // Find the last text node inside the anchor.
3653
- var lastText = _window === null || _window === void 0 ? void 0 : _window.document.createTreeWalker(anchorNode, NodeFilter.SHOW_TEXT).lastChild();
3654
- if (lastText === node && ((_lastText$textContent = lastText.textContent) === null || _lastText$textContent === void 0 ? void 0 : _lastText$textContent.length) === offset) {
3655
- _native = false;
3649
+ // If the NODE_MAP is dirty, we can't trust the selection anchor (eg ReactEditor.toDOMPoint)
3650
+ if (!IS_NODE_MAP_DIRTY.get(editor)) {
3651
+ var _node$parentElement, _window$getComputedSt;
3652
+ // Chrome also has issues correctly editing the end of anchor elements: https://bugs.chromium.org/p/chromium/issues/detail?id=1259100
3653
+ // Therefore we don't allow native events to insert text at the end of anchor nodes.
3654
+ var anchor = selection.anchor;
3655
+ var _ReactEditor$toDOMPoi = ReactEditor.toDOMPoint(editor, anchor),
3656
+ _ReactEditor$toDOMPoi2 = _slicedToArray(_ReactEditor$toDOMPoi, 2),
3657
+ node = _ReactEditor$toDOMPoi2[0],
3658
+ offset = _ReactEditor$toDOMPoi2[1];
3659
+ var anchorNode = (_node$parentElement = node.parentElement) === null || _node$parentElement === void 0 ? void 0 : _node$parentElement.closest('a');
3660
+ var _window = ReactEditor.getWindow(editor);
3661
+ if (_native && anchorNode && ReactEditor.hasDOMNode(editor, anchorNode)) {
3662
+ var _lastText$textContent;
3663
+ // Find the last text node inside the anchor.
3664
+ var lastText = _window === null || _window === void 0 ? void 0 : _window.document.createTreeWalker(anchorNode, NodeFilter.SHOW_TEXT).lastChild();
3665
+ if (lastText === node && ((_lastText$textContent = lastText.textContent) === null || _lastText$textContent === void 0 ? void 0 : _lastText$textContent.length) === offset) {
3666
+ _native = false;
3667
+ }
3656
3668
  }
3657
- }
3658
- // Chrome has issues with the presence of tab characters inside elements with whiteSpace = 'pre'
3659
- // causing abnormal insert behavior: https://bugs.chromium.org/p/chromium/issues/detail?id=1219139
3660
- if (_native && node.parentElement && (_window === null || _window === void 0 || (_window$getComputedSt = _window.getComputedStyle(node.parentElement)) === null || _window$getComputedSt === void 0 ? void 0 : _window$getComputedSt.whiteSpace) === 'pre') {
3661
- var block = slate.Editor.above(editor, {
3662
- at: anchor.path,
3663
- match: function match(n) {
3664
- return slate.Element.isElement(n) && slate.Editor.isBlock(editor, n);
3669
+ // Chrome has issues with the presence of tab characters inside elements with whiteSpace = 'pre'
3670
+ // causing abnormal insert behavior: https://bugs.chromium.org/p/chromium/issues/detail?id=1219139
3671
+ if (_native && node.parentElement && (_window === null || _window === void 0 || (_window$getComputedSt = _window.getComputedStyle(node.parentElement)) === null || _window$getComputedSt === void 0 ? void 0 : _window$getComputedSt.whiteSpace) === 'pre') {
3672
+ var block = slate.Editor.above(editor, {
3673
+ at: anchor.path,
3674
+ match: function match(n) {
3675
+ return slate.Element.isElement(n) && slate.Editor.isBlock(editor, n);
3676
+ }
3677
+ });
3678
+ if (block && slate.Node.string(block[0]).includes('\t')) {
3679
+ _native = false;
3665
3680
  }
3666
- });
3667
- if (block && slate.Node.string(block[0]).includes('\t')) {
3668
- _native = false;
3669
3681
  }
3670
3682
  }
3671
3683
  }
3672
3684
  // COMPAT: For the deleting forward/backward input types we don't want
3673
3685
  // to change the selection because it is the range that will be deleted,
3674
3686
  // and those commands determine that for themselves.
3675
- if (!type.startsWith('delete') || type.startsWith('deleteBy')) {
3687
+ // If the NODE_MAP is dirty, we can't trust the selection anchor (eg ReactEditor.toDOMPoint via ReactEditor.toSlateRange)
3688
+ if ((!type.startsWith('delete') || type.startsWith('deleteBy')) && !IS_NODE_MAP_DIRTY.get(editor)) {
3676
3689
  var _event$getTargetRange = event.getTargetRanges(),
3677
3690
  _event$getTargetRange2 = _slicedToArray(_event$getTargetRange, 1),
3678
3691
  targetRange = _event$getTargetRange2[0];
@@ -5058,6 +5071,16 @@ var withReact = function withReact(editor) {
5058
5071
  }
5059
5072
  }
5060
5073
  apply(op);
5074
+ switch (op.type) {
5075
+ case 'insert_node':
5076
+ case 'remove_node':
5077
+ case 'merge_node':
5078
+ case 'move_node':
5079
+ case 'split_node':
5080
+ {
5081
+ IS_NODE_MAP_DIRTY.set(e, true);
5082
+ }
5083
+ }
5061
5084
  for (var _i = 0, _matches = matches; _i < _matches.length; _i++) {
5062
5085
  var _matches$_i = _slicedToArray(_matches[_i], 2),
5063
5086
  path = _matches$_i[0],