@portabletext/editor 2.12.3 → 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 (32) 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 +73 -81
  6. package/lib/_chunks-dts/behavior.types.action.d.ts +0 -8
  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 +64 -21
  12. package/lib/index.cjs.map +1 -1
  13. package/lib/index.js +64 -21
  14. package/lib/index.js.map +1 -1
  15. package/lib/plugins/index.cjs +1 -1
  16. package/lib/plugins/index.d.cts +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/lib/utils/index.d.ts +2 -2
  24. package/package.json +14 -14
  25. package/src/behaviors/behavior.abstract.delete.ts +1 -0
  26. package/src/behaviors/behavior.abstract.keyboard.ts +27 -0
  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/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
  }) {
@@ -4433,16 +4422,19 @@ const addAnnotationOperationImplementation = ({
4433
4422
  const endBlock = operation.editor.value.at(endBlockIndex);
4434
4423
  if (!endBlock)
4435
4424
  throw new Error("Failed to get end block");
4436
- const anchorBlockPath = anchorBlockKey !== void 0 ? getBlockPath({
4437
- editor: operation.editor,
4438
- _key: anchorBlockKey
4439
- }) : void 0, focusBlockPath = focusBlockKey !== void 0 ? getBlockPath({
4440
- editor: operation.editor,
4441
- _key: focusBlockKey
4442
- }) : void 0;
4443
- if (operation.at.anchor.path.length === 1 && operation.at.focus.path.length === 1 && anchorBlockPath && focusBlockPath && anchorBlockPath[0] === focusBlockPath[0]) {
4425
+ if (operation.unit === "block") {
4444
4426
  Transforms.removeNodes(operation.editor, {
4445
- 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"
4446
4438
  }), operation.editor.children.length === 0 && Transforms.insertNodes(operation.editor, createPlaceholderBlock(context));
4447
4439
  return;
4448
4440
  }
@@ -6630,6 +6622,15 @@ const addAnnotationOnCollapsedSelection = defineBehavior({
6630
6622
  shift: !1
6631
6623
  }]
6632
6624
  }),
6625
+ backspace: createKeyboardShortcut({
6626
+ default: [{
6627
+ key: "Backspace",
6628
+ alt: !1,
6629
+ ctrl: !1,
6630
+ meta: !1,
6631
+ shift: !1
6632
+ }]
6633
+ }),
6633
6634
  break: createKeyboardShortcut({
6634
6635
  default: [{
6635
6636
  key: "Enter",
@@ -6648,6 +6649,15 @@ const addAnnotationOnCollapsedSelection = defineBehavior({
6648
6649
  underline,
6649
6650
  code
6650
6651
  },
6652
+ delete: createKeyboardShortcut({
6653
+ default: [{
6654
+ key: "Delete",
6655
+ alt: !1,
6656
+ ctrl: !1,
6657
+ meta: !1,
6658
+ shift: !1
6659
+ }]
6660
+ }),
6651
6661
  history: {
6652
6662
  undo,
6653
6663
  redo
@@ -8043,7 +8053,8 @@ const abstractAnnotationBehaviors = [defineBehavior({
8043
8053
  path: event.at,
8044
8054
  offset: 0
8045
8055
  }
8046
- }
8056
+ },
8057
+ unit: "block"
8047
8058
  })]]
8048
8059
  }), defineBehavior({
8049
8060
  on: "delete.child",
@@ -8691,6 +8702,38 @@ const abstractAnnotationBehaviors = [defineBehavior({
8691
8702
  alt: !1
8692
8703
  }]
8693
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
+ }),
8694
8737
  /**
8695
8738
  * Allow raising an `insert.break` event when pressing Enter on an inline
8696
8739
  * object.