@portabletext/editor 2.12.2 → 2.13.0

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.
Files changed (33) hide show
  1. package/lib/_chunks-cjs/selector.is-selecting-entire-blocks.cjs +64 -5
  2. package/lib/_chunks-cjs/selector.is-selecting-entire-blocks.cjs.map +1 -1
  3. package/lib/_chunks-cjs/util.slice-blocks.cjs +1 -0
  4. package/lib/_chunks-cjs/util.slice-blocks.cjs.map +1 -1
  5. package/lib/_chunks-dts/behavior.types.action.d.cts +64 -72
  6. package/lib/_chunks-dts/behavior.types.action.d.ts +9 -17
  7. package/lib/_chunks-es/selector.is-selecting-entire-blocks.js +65 -6
  8. package/lib/_chunks-es/selector.is-selecting-entire-blocks.js.map +1 -1
  9. package/lib/_chunks-es/util.slice-blocks.js +1 -0
  10. package/lib/_chunks-es/util.slice-blocks.js.map +1 -1
  11. package/lib/index.cjs +69 -60
  12. package/lib/index.cjs.map +1 -1
  13. package/lib/index.js +69 -60
  14. package/lib/index.js.map +1 -1
  15. package/lib/plugins/index.cjs +1 -1
  16. package/lib/plugins/index.d.ts +3 -3
  17. package/lib/plugins/index.js +1 -1
  18. package/lib/selectors/index.cjs +1 -0
  19. package/lib/selectors/index.cjs.map +1 -1
  20. package/lib/selectors/index.d.cts +15 -1
  21. package/lib/selectors/index.d.ts +15 -1
  22. package/lib/selectors/index.js +2 -1
  23. package/package.json +16 -16
  24. package/src/behaviors/behavior.abstract.delete.ts +1 -0
  25. package/src/behaviors/behavior.abstract.keyboard.ts +27 -0
  26. package/src/editor/Editable.tsx +1 -90
  27. package/src/internal-utils/asserters.ts +1 -1
  28. package/src/keyboard-shortcuts/default-keyboard-shortcuts.ts +22 -0
  29. package/src/operations/behavior.operation.delete.ts +6 -24
  30. package/src/selectors/index.ts +1 -0
  31. package/src/selectors/selector.get-mark-state.ts +75 -6
  32. package/src/test/vitest/step-definitions.tsx +1 -8
  33. package/src/types/paths.ts +18 -0
package/lib/index.js CHANGED
@@ -157,17 +157,6 @@ function fromSlateValue(value, textBlockType, keyMap = {}) {
157
157
  function isEqualToEmptyEditor(children, schemaTypes) {
158
158
  return children === void 0 || children && Array.isArray(children) && children.length === 0 || children && Array.isArray(children) && children.length === 1 && Element$1.isElement(children[0]) && children[0]._type === schemaTypes.block.name && "style" in children[0] && children[0].style === schemaTypes.styles[0].name && !("listItem" in children[0]) && Array.isArray(children[0].children) && children[0].children.length === 1 && Text.isText(children[0].children[0]) && children[0].children[0]._type === "span" && !children[0].children[0].marks?.join("") && children[0].children[0].text === "";
159
159
  }
160
- function getBlockPath({
161
- editor,
162
- _key
163
- }) {
164
- const [, blockPath] = Array.from(Editor.nodes(editor, {
165
- at: [],
166
- match: (n) => n._key === _key
167
- })).at(0) ?? [void 0, void 0], blockIndex = blockPath?.at(0);
168
- if (blockIndex !== void 0)
169
- return [blockIndex];
170
- }
171
160
  function getFocusBlock({
172
161
  editor
173
162
  }) {
@@ -1734,7 +1723,7 @@ function createDecorate(schema, slateEditor) {
1734
1723
  };
1735
1724
  }
1736
1725
  const RelayActorContext = createContext({}), debug$d = debugWithName("component:Editable"), PortableTextEditable = forwardRef(function(props, forwardedRef) {
1737
- const $ = c(172);
1726
+ const $ = c(171);
1738
1727
  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;
1739
1728
  $[0] !== props ? ({
1740
1729
  hotkeys,
@@ -2271,50 +2260,16 @@ const RelayActorContext = createContext({}), debug$d = debugWithName("component:
2271
2260
  }, $[140] = editorActor, $[141] = onDragLeave, $[142] = slateEditor, $[143] = t34) : t34 = $[143];
2272
2261
  const handleDragLeave = t34;
2273
2262
  let t35;
2274
- $[144] !== forwardedRef || $[145] !== slateEditor ? (t35 = (node) => {
2275
- if (typeof forwardedRef == "function" ? forwardedRef(node) : forwardedRef && (forwardedRef.current = node), node) {
2276
- const mutationObserver = new MutationObserver(() => {
2277
- validateSelection(slateEditor, node);
2278
- });
2279
- return mutationObserver.observe(node, {
2280
- attributeOldValue: !1,
2281
- attributes: !1,
2282
- characterData: !1,
2283
- childList: !0,
2284
- subtree: !0
2285
- }), () => {
2286
- mutationObserver.disconnect();
2287
- };
2288
- }
2289
- }, $[144] = forwardedRef, $[145] = slateEditor, $[146] = t35) : t35 = $[146];
2263
+ $[144] !== forwardedRef ? (t35 = (node) => {
2264
+ typeof forwardedRef == "function" ? forwardedRef(node) : forwardedRef && (forwardedRef.current = node);
2265
+ }, $[144] = forwardedRef, $[145] = t35) : t35 = $[145];
2290
2266
  const callbackRef = t35;
2291
2267
  if (!portableTextEditor)
2292
2268
  return null;
2293
2269
  let t36;
2294
- 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;
2270
+ 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;
2295
2271
  });
2296
2272
  PortableTextEditable.displayName = "ForwardRef(PortableTextEditable)";
2297
- function validateSelection(slateEditor, activeElement) {
2298
- if (!slateEditor.selection)
2299
- return;
2300
- let root;
2301
- try {
2302
- root = ReactEditor.findDocumentOrShadowRoot(slateEditor);
2303
- } catch {
2304
- }
2305
- if (!root || activeElement !== root.activeElement)
2306
- return;
2307
- const domSelection = ReactEditor.getWindow(slateEditor).getSelection();
2308
- if (!domSelection || domSelection.rangeCount === 0)
2309
- return;
2310
- const existingDOMRange = domSelection.getRangeAt(0);
2311
- try {
2312
- const newDOMRange = ReactEditor.toDOMRange(slateEditor, slateEditor.selection);
2313
- (newDOMRange.startOffset !== existingDOMRange.startOffset || newDOMRange.endOffset !== existingDOMRange.endOffset) && (debug$d("DOM range out of sync, validating selection"), domSelection?.removeAllRanges(), domSelection.addRange(newDOMRange));
2314
- } catch {
2315
- debug$d("Could not resolve selection, selecting top document"), Transforms.deselect(slateEditor), slateEditor.children.length > 0 && Transforms.select(slateEditor, [0, 0]), slateEditor.onChange();
2316
- }
2317
- }
2318
2273
  function _temp(s) {
2319
2274
  return s.matches({
2320
2275
  "edit mode": "read only"
@@ -4467,16 +4422,19 @@ const addAnnotationOperationImplementation = ({
4467
4422
  const endBlock = operation.editor.value.at(endBlockIndex);
4468
4423
  if (!endBlock)
4469
4424
  throw new Error("Failed to get end block");
4470
- const anchorBlockPath = anchorBlockKey !== void 0 ? getBlockPath({
4471
- editor: operation.editor,
4472
- _key: anchorBlockKey
4473
- }) : void 0, focusBlockPath = focusBlockKey !== void 0 ? getBlockPath({
4474
- editor: operation.editor,
4475
- _key: focusBlockKey
4476
- }) : void 0;
4477
- if (operation.at.anchor.path.length === 1 && operation.at.focus.path.length === 1 && anchorBlockPath && focusBlockPath && anchorBlockPath[0] === focusBlockPath[0]) {
4425
+ if (operation.unit === "block") {
4478
4426
  Transforms.removeNodes(operation.editor, {
4479
- at: [anchorBlockPath[0]]
4427
+ at: {
4428
+ anchor: {
4429
+ path: [startBlockIndex],
4430
+ offset: 0
4431
+ },
4432
+ focus: {
4433
+ path: [endBlockIndex],
4434
+ offset: 0
4435
+ }
4436
+ },
4437
+ mode: "highest"
4480
4438
  }), operation.editor.children.length === 0 && Transforms.insertNodes(operation.editor, createPlaceholderBlock(context));
4481
4439
  return;
4482
4440
  }
@@ -6664,6 +6622,15 @@ const addAnnotationOnCollapsedSelection = defineBehavior({
6664
6622
  shift: !1
6665
6623
  }]
6666
6624
  }),
6625
+ backspace: createKeyboardShortcut({
6626
+ default: [{
6627
+ key: "Backspace",
6628
+ alt: !1,
6629
+ ctrl: !1,
6630
+ meta: !1,
6631
+ shift: !1
6632
+ }]
6633
+ }),
6667
6634
  break: createKeyboardShortcut({
6668
6635
  default: [{
6669
6636
  key: "Enter",
@@ -6682,6 +6649,15 @@ const addAnnotationOnCollapsedSelection = defineBehavior({
6682
6649
  underline,
6683
6650
  code
6684
6651
  },
6652
+ delete: createKeyboardShortcut({
6653
+ default: [{
6654
+ key: "Delete",
6655
+ alt: !1,
6656
+ ctrl: !1,
6657
+ meta: !1,
6658
+ shift: !1
6659
+ }]
6660
+ }),
6685
6661
  history: {
6686
6662
  undo,
6687
6663
  redo
@@ -8077,7 +8053,8 @@ const abstractAnnotationBehaviors = [defineBehavior({
8077
8053
  path: event.at,
8078
8054
  offset: 0
8079
8055
  }
8080
- }
8056
+ },
8057
+ unit: "block"
8081
8058
  })]]
8082
8059
  }), defineBehavior({
8083
8060
  on: "delete.child",
@@ -8725,6 +8702,38 @@ const abstractAnnotationBehaviors = [defineBehavior({
8725
8702
  alt: !1
8726
8703
  }]
8727
8704
  }), abstractKeyboardBehaviors = [
8705
+ /**
8706
+ * When Backspace is pressed on an inline object, Slate will raise a
8707
+ * `delete.backward` event with `unit: 'block'`. This is wrong and this
8708
+ * Behavior adjusts that.
8709
+ */
8710
+ defineBehavior({
8711
+ on: "keyboard.keydown",
8712
+ guard: ({
8713
+ snapshot,
8714
+ event
8715
+ }) => defaultKeyboardShortcuts.backspace.guard(event.originEvent) && isSelectionCollapsed$1(snapshot) && getFocusInlineObject(snapshot),
8716
+ actions: [() => [raise({
8717
+ type: "delete.backward",
8718
+ unit: "character"
8719
+ })]]
8720
+ }),
8721
+ /**
8722
+ * When Delete is pressed on an inline object, Slate will raise a
8723
+ * `delete.forward` event with `unit: 'block'`. This is wrong and this
8724
+ * Behavior adjusts that.
8725
+ */
8726
+ defineBehavior({
8727
+ on: "keyboard.keydown",
8728
+ guard: ({
8729
+ snapshot,
8730
+ event
8731
+ }) => defaultKeyboardShortcuts.delete.guard(event.originEvent) && isSelectionCollapsed$1(snapshot) && getFocusInlineObject(snapshot),
8732
+ actions: [() => [raise({
8733
+ type: "delete.forward",
8734
+ unit: "character"
8735
+ })]]
8736
+ }),
8728
8737
  /**
8729
8738
  * Allow raising an `insert.break` event when pressing Enter on an inline
8730
8739
  * object.