slate-react 0.100.1 → 0.101.0-20231110162556

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.es.js CHANGED
@@ -510,20 +510,47 @@ var ReactEditor = {
510
510
  }
511
511
  throw new Error("Unable to find the path for Slate node: ".concat(Scrubber.stringify(node)));
512
512
  },
513
- focus: editor => {
513
+ focus: function focus(editor) {
514
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
515
+ retries: 5
516
+ };
517
+ // Return if already focused
518
+ if (IS_FOCUSED.get(editor)) {
519
+ return;
520
+ }
521
+ // Retry setting focus if the editor has pending operations.
522
+ // The DOM (selection) is unstable while changes are applied.
523
+ // Retry until retries are exhausted or editor is focused.
524
+ if (options.retries <= 0) {
525
+ throw new Error('Could not set focus, editor seems stuck with pending operations');
526
+ }
527
+ if (editor.operations.length > 0) {
528
+ setTimeout(() => {
529
+ ReactEditor.focus(editor, {
530
+ retries: options.retries - 1
531
+ });
532
+ }, 10);
533
+ return;
534
+ }
514
535
  var el = ReactEditor.toDOMNode(editor, editor);
515
536
  var root = ReactEditor.findDocumentOrShadowRoot(editor);
516
- IS_FOCUSED.set(editor, true);
517
537
  if (root.activeElement !== el) {
538
+ // Ensure that the DOM selection state is set to the editor's selection
518
539
  if (editor.selection && root instanceof Document) {
519
540
  var domSelection = root.getSelection();
520
541
  var domRange = ReactEditor.toDOMRange(editor, editor.selection);
521
542
  domSelection === null || domSelection === void 0 || domSelection.removeAllRanges();
522
543
  domSelection === null || domSelection === void 0 || domSelection.addRange(domRange);
523
544
  }
545
+ // Create a new selection in the top of the document if missing
546
+ if (!editor.selection) {
547
+ Transforms.select(editor, Editor.start(editor, []));
548
+ editor.onChange();
549
+ }
524
550
  el.focus({
525
551
  preventScroll: true
526
552
  });
553
+ IS_FOCUSED.set(editor, true);
527
554
  }
528
555
  },
529
556
  getWindow: editor => {
@@ -815,6 +842,7 @@ var ReactEditor = {
815
842
  };
816
843
  },
817
844
  toSlateRange: (editor, domRange, options) => {
845
+ var _focusNode$textConten;
818
846
  var {
819
847
  exactMatch,
820
848
  suppressThrow
@@ -907,6 +935,12 @@ var ReactEditor = {
907
935
  if (anchorNode == null || focusNode == null || anchorOffset == null || focusOffset == null) {
908
936
  throw new Error("Cannot resolve a Slate range from DOM range: ".concat(domRange));
909
937
  }
938
+ // COMPAT: Firefox sometimes includes an extra \n (rendered by TextString
939
+ // when isTrailing is true) in the focusOffset, resulting in an invalid
940
+ // Slate point. (2023/11/01)
941
+ if (IS_FIREFOX && (_focusNode$textConten = focusNode.textContent) !== null && _focusNode$textConten !== void 0 && _focusNode$textConten.endsWith('\n\n') && focusOffset === focusNode.textContent.length) {
942
+ focusOffset--;
943
+ }
910
944
  // COMPAT: Triple-clicking a word in chrome will sometimes place the focus
911
945
  // inside a `contenteditable="false"` DOM node following the word, which
912
946
  // will cause `toSlatePoint` to throw an error. (2023/03/07)