@portabletext/editor 2.13.1 → 2.13.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.
package/lib/index.js CHANGED
@@ -1913,7 +1913,7 @@ function createDecorate(schema, slateEditor) {
1913
1913
  };
1914
1914
  }
1915
1915
  const RelayActorContext = createContext({}), debug$d = debugWithName("component:Editable"), PortableTextEditable = forwardRef(function(props, forwardedRef) {
1916
- const $ = c(171);
1916
+ const $ = c(172);
1917
1917
  let hotkeys, onBeforeInput, onBlur, onClick, onCopy, onCut, onDrag, onDragEnd, onDragEnter, onDragLeave, onDragOver, onDragStart, onDrop, onFocus, onPaste, propsSelection, rangeDecorations, renderAnnotation, renderBlock, renderChild, renderDecorator, renderListItem, renderPlaceholder, renderStyle, restProps, scrollSelectionIntoView, spellCheck;
1918
1918
  $[0] !== props ? ({
1919
1919
  hotkeys,
@@ -2450,16 +2450,50 @@ const RelayActorContext = createContext({}), debug$d = debugWithName("component:
2450
2450
  }, $[140] = editorActor, $[141] = onDragLeave, $[142] = slateEditor, $[143] = t34) : t34 = $[143];
2451
2451
  const handleDragLeave = t34;
2452
2452
  let t35;
2453
- $[144] !== forwardedRef ? (t35 = (node) => {
2454
- typeof forwardedRef == "function" ? forwardedRef(node) : forwardedRef && (forwardedRef.current = node);
2455
- }, $[144] = forwardedRef, $[145] = t35) : t35 = $[145];
2453
+ $[144] !== forwardedRef || $[145] !== slateEditor ? (t35 = (node) => {
2454
+ if (typeof forwardedRef == "function" ? forwardedRef(node) : forwardedRef && (forwardedRef.current = node), node) {
2455
+ const mutationObserver = new MutationObserver(() => {
2456
+ validateSelection(slateEditor, node);
2457
+ });
2458
+ return mutationObserver.observe(node, {
2459
+ attributeOldValue: !1,
2460
+ attributes: !1,
2461
+ characterData: !1,
2462
+ childList: !0,
2463
+ subtree: !0
2464
+ }), () => {
2465
+ mutationObserver.disconnect();
2466
+ };
2467
+ }
2468
+ }, $[144] = forwardedRef, $[145] = slateEditor, $[146] = t35) : t35 = $[146];
2456
2469
  const callbackRef = t35;
2457
2470
  if (!portableTextEditor)
2458
2471
  return null;
2459
2472
  let t36;
2460
- return $[146] !== callbackRef || $[147] !== decorate || $[148] !== handleClick || $[149] !== handleCopy || $[150] !== handleCut || $[151] !== handleDrag || $[152] !== handleDragEnd || $[153] !== handleDragEnter || $[154] !== handleDragLeave || $[155] !== handleDragOver || $[156] !== handleDragStart || $[157] !== handleDrop || $[158] !== handleKeyDown || $[159] !== handleKeyUp || $[160] !== handleOnBeforeInput || $[161] !== handleOnBlur || $[162] !== handleOnFocus || $[163] !== handlePaste || $[164] !== hasInvalidValue || $[165] !== readOnly || $[166] !== renderElement || $[167] !== renderLeaf || $[168] !== restProps || $[169] !== scrollSelectionIntoViewToSlate ? (t36 = hasInvalidValue ? null : /* @__PURE__ */ jsx(Editable, { ...restProps, ref: callbackRef, "data-read-only": readOnly, autoFocus: !1, className: restProps.className || "pt-editable", decorate, onBlur: handleOnBlur, onCopy: handleCopy, onCut: handleCut, onClick: handleClick, onDOMBeforeInput: handleOnBeforeInput, onDragStart: handleDragStart, onDrag: handleDrag, onDragEnd: handleDragEnd, onDragEnter: handleDragEnter, onDragOver: handleDragOver, onDrop: handleDrop, onDragLeave: handleDragLeave, onFocus: handleOnFocus, onKeyDown: handleKeyDown, onKeyUp: handleKeyUp, onPaste: handlePaste, readOnly, renderPlaceholder: void 0, renderElement, renderLeaf, renderText, scrollSelectionIntoView: scrollSelectionIntoViewToSlate }), $[146] = callbackRef, $[147] = decorate, $[148] = handleClick, $[149] = handleCopy, $[150] = handleCut, $[151] = handleDrag, $[152] = handleDragEnd, $[153] = handleDragEnter, $[154] = handleDragLeave, $[155] = handleDragOver, $[156] = handleDragStart, $[157] = handleDrop, $[158] = handleKeyDown, $[159] = handleKeyUp, $[160] = handleOnBeforeInput, $[161] = handleOnBlur, $[162] = handleOnFocus, $[163] = handlePaste, $[164] = hasInvalidValue, $[165] = readOnly, $[166] = renderElement, $[167] = renderLeaf, $[168] = restProps, $[169] = scrollSelectionIntoViewToSlate, $[170] = t36) : t36 = $[170], t36;
2473
+ return $[147] !== callbackRef || $[148] !== decorate || $[149] !== handleClick || $[150] !== handleCopy || $[151] !== handleCut || $[152] !== handleDrag || $[153] !== handleDragEnd || $[154] !== handleDragEnter || $[155] !== handleDragLeave || $[156] !== handleDragOver || $[157] !== handleDragStart || $[158] !== handleDrop || $[159] !== handleKeyDown || $[160] !== handleKeyUp || $[161] !== handleOnBeforeInput || $[162] !== handleOnBlur || $[163] !== handleOnFocus || $[164] !== handlePaste || $[165] !== hasInvalidValue || $[166] !== readOnly || $[167] !== renderElement || $[168] !== renderLeaf || $[169] !== restProps || $[170] !== scrollSelectionIntoViewToSlate ? (t36 = hasInvalidValue ? null : /* @__PURE__ */ jsx(Editable, { ...restProps, ref: callbackRef, "data-read-only": readOnly, autoFocus: !1, className: restProps.className || "pt-editable", decorate, onBlur: handleOnBlur, onCopy: handleCopy, onCut: handleCut, onClick: handleClick, onDOMBeforeInput: handleOnBeforeInput, onDragStart: handleDragStart, onDrag: handleDrag, onDragEnd: handleDragEnd, onDragEnter: handleDragEnter, onDragOver: handleDragOver, onDrop: handleDrop, onDragLeave: handleDragLeave, onFocus: handleOnFocus, onKeyDown: handleKeyDown, onKeyUp: handleKeyUp, onPaste: handlePaste, readOnly, renderPlaceholder: void 0, renderElement, renderLeaf, renderText, scrollSelectionIntoView: scrollSelectionIntoViewToSlate }), $[147] = callbackRef, $[148] = decorate, $[149] = handleClick, $[150] = handleCopy, $[151] = handleCut, $[152] = handleDrag, $[153] = handleDragEnd, $[154] = handleDragEnter, $[155] = handleDragLeave, $[156] = handleDragOver, $[157] = handleDragStart, $[158] = handleDrop, $[159] = handleKeyDown, $[160] = handleKeyUp, $[161] = handleOnBeforeInput, $[162] = handleOnBlur, $[163] = handleOnFocus, $[164] = handlePaste, $[165] = hasInvalidValue, $[166] = readOnly, $[167] = renderElement, $[168] = renderLeaf, $[169] = restProps, $[170] = scrollSelectionIntoViewToSlate, $[171] = t36) : t36 = $[171], t36;
2461
2474
  });
2462
2475
  PortableTextEditable.displayName = "ForwardRef(PortableTextEditable)";
2476
+ function validateSelection(slateEditor, activeElement) {
2477
+ if (!slateEditor.selection)
2478
+ return;
2479
+ let root;
2480
+ try {
2481
+ root = ReactEditor.findDocumentOrShadowRoot(slateEditor);
2482
+ } catch {
2483
+ }
2484
+ if (!root || activeElement !== root.activeElement)
2485
+ return;
2486
+ const domSelection = ReactEditor.getWindow(slateEditor).getSelection();
2487
+ if (!domSelection || domSelection.rangeCount === 0)
2488
+ return;
2489
+ const existingDOMRange = domSelection.getRangeAt(0);
2490
+ try {
2491
+ const newDOMRange = ReactEditor.toDOMRange(slateEditor, slateEditor.selection);
2492
+ (newDOMRange.startOffset !== existingDOMRange.startOffset || newDOMRange.endOffset !== existingDOMRange.endOffset) && (debug$d("DOM range out of sync, validating selection"), domSelection?.removeAllRanges(), domSelection.addRange(newDOMRange));
2493
+ } catch {
2494
+ debug$d("Could not resolve selection, selecting top document"), Transforms.deselect(slateEditor), slateEditor.children.length > 0 && Transforms.select(slateEditor, [0, 0]), slateEditor.onChange();
2495
+ }
2496
+ }
2463
2497
  function _temp(s) {
2464
2498
  return s.matches({
2465
2499
  "edit mode": "read only"