@portabletext/editor 1.57.3 → 1.57.4

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 (43) hide show
  1. package/lib/_chunks-cjs/selector.get-text-before.cjs +3 -3
  2. package/lib/_chunks-cjs/selector.get-text-before.cjs.map +1 -1
  3. package/lib/_chunks-cjs/selector.is-selecting-entire-blocks.cjs +114 -114
  4. package/lib/_chunks-cjs/selector.is-selecting-entire-blocks.cjs.map +1 -1
  5. package/lib/_chunks-cjs/{selector.get-focus-span.cjs → selector.is-selection-expanded.cjs} +25 -25
  6. package/lib/_chunks-cjs/selector.is-selection-expanded.cjs.map +1 -0
  7. package/lib/_chunks-es/selector.get-text-before.js +1 -1
  8. package/lib/_chunks-es/selector.is-selecting-entire-blocks.js +93 -93
  9. package/lib/_chunks-es/selector.is-selecting-entire-blocks.js.map +1 -1
  10. package/lib/_chunks-es/{selector.get-focus-span.js → selector.is-selection-expanded.js} +26 -26
  11. package/lib/_chunks-es/selector.is-selection-expanded.js.map +1 -0
  12. package/lib/index.cjs +130 -135
  13. package/lib/index.cjs.map +1 -1
  14. package/lib/index.js +95 -100
  15. package/lib/index.js.map +1 -1
  16. package/lib/plugins/index.cjs +16 -16
  17. package/lib/plugins/index.cjs.map +1 -1
  18. package/lib/plugins/index.js +1 -1
  19. package/lib/selectors/index.cjs +14 -14
  20. package/lib/selectors/index.cjs.map +1 -1
  21. package/lib/selectors/index.js +2 -2
  22. package/package.json +8 -8
  23. package/src/editor/Editable.tsx +1 -1
  24. package/src/editor/editor-dom.ts +1 -1
  25. package/src/editor/plugins/createWithEditableAPI.ts +1 -1
  26. package/src/editor/range-decorations-machine.ts +2 -1
  27. package/src/internal-utils/__tests__/ranges.test.ts +1 -1
  28. package/src/internal-utils/move-range-by-operation.ts +19 -0
  29. package/src/internal-utils/{ranges.test.ts → to-slate-range.test.ts} +101 -1
  30. package/src/internal-utils/to-slate-range.ts +171 -0
  31. package/src/operations/behavior.operation.block.set.ts +1 -1
  32. package/src/operations/behavior.operation.block.unset.ts +1 -1
  33. package/src/operations/behavior.operation.child.set.ts +1 -1
  34. package/src/operations/behavior.operation.child.unset.ts +1 -1
  35. package/src/operations/behavior.operation.decorator.add.ts +1 -1
  36. package/src/operations/behavior.operation.delete.ts +1 -1
  37. package/src/operations/behavior.operation.move.block.ts +34 -28
  38. package/src/operations/behavior.operation.select.ts +1 -1
  39. package/src/selectors/selector.get-mark-state.ts +3 -1
  40. package/lib/_chunks-cjs/selector.get-focus-span.cjs.map +0 -1
  41. package/lib/_chunks-es/selector.get-focus-span.js.map +0 -1
  42. package/src/internal-utils/paths.ts +0 -110
  43. package/src/internal-utils/ranges.ts +0 -70
package/lib/index.js CHANGED
@@ -5,15 +5,15 @@ import { useEditor, EditorContext } from "./_chunks-es/use-editor.js";
5
5
  import { jsx, jsxs, Fragment } from "react/jsx-runtime";
6
6
  import { useSelector, useActorRef } from "@xstate/react";
7
7
  import noop from "lodash/noop.js";
8
- import { Element as Element$1, Text, Range, Editor, Node, Point, Transforms, Path, Operation, createEditor } from "slate";
8
+ import { Element as Element$1, Text, Range, Editor, Node, Transforms, Path, Operation, Point, createEditor } from "slate";
9
9
  import { useSelected, useSlateSelector, useSlateStatic, withReact, ReactEditor, Slate, useSlate, Editable } from "slate-react";
10
10
  import debug$f from "debug";
11
11
  import { DOMEditor, isDOMNode } from "slate-dom";
12
- import { getBlockStartPoint, getBlockKeyFromSelectionPoint, isTextBlock, getChildKeyFromSelectionPoint, isSpan, parseBlock, parseAnnotation, blockOffsetToSpanSelectionPoint, parseInlineObject, isKeyedSegment, isListBlock, isTypedObject, getSelectionStartPoint, getSelectionEndPoint, getTextBlockText, parseBlocks } from "./_chunks-es/util.slice-blocks.js";
13
- import { getBlockEndPoint, isSelectionCollapsed, isEmptyTextBlock, isEqualSelectionPoints } from "./_chunks-es/util.is-selection-collapsed.js";
12
+ import { getBlockStartPoint, getBlockKeyFromSelectionPoint, isTextBlock, getChildKeyFromSelectionPoint, blockOffsetToSpanSelectionPoint, isSpan, parseBlock, parseAnnotation, parseInlineObject, isKeyedSegment, isListBlock, isTypedObject, getSelectionStartPoint, getSelectionEndPoint, getTextBlockText, parseBlocks } from "./_chunks-es/util.slice-blocks.js";
13
+ import { getBlockEndPoint, isSelectionCollapsed, isEqualSelectionPoints, isEmptyTextBlock } from "./_chunks-es/util.is-selection-collapsed.js";
14
14
  import isEqual from "lodash/isEqual.js";
15
+ import { isSelectionCollapsed as isSelectionCollapsed$1, getFocusTextBlock, getFocusSpan as getFocusSpan$1, isSelectionExpanded, getFocusBlock as getFocusBlock$1, getSelectedValue, getFocusChild as getFocusChild$1 } from "./_chunks-es/selector.is-selection-expanded.js";
15
16
  import { getFocusInlineObject, getSelectedBlocks, getSelectionStartBlock as getSelectionStartBlock$1, getSelectionEndBlock as getSelectionEndBlock$1, isOverlappingSelection, isSelectingEntireBlocks, getMarkState, getActiveDecorators, getActiveAnnotationsMarks, getTrimmedSelection, getCaretWordSelection, getFocusBlockObject, getPreviousBlock, getNextBlock, isAtTheEndOfBlock, isAtTheStartOfBlock, getFirstBlock as getFirstBlock$1, getLastBlock as getLastBlock$1, getFocusListBlock, isActiveAnnotation, isActiveDecorator, getSelectedTextBlocks, isActiveListItem, isActiveStyle, getActiveAnnotations } from "./_chunks-es/selector.is-selecting-entire-blocks.js";
16
- import { isSelectionCollapsed as isSelectionCollapsed$1, getFocusTextBlock, getFocusSpan as getFocusSpan$1, isSelectionExpanded, getFocusBlock as getFocusBlock$1, getSelectedValue, getFocusChild as getFocusChild$1 } from "./_chunks-es/selector.get-focus-span.js";
17
17
  import getRandomValues from "get-random-values-esm";
18
18
  import { defineBehavior, forward, raise, effect } from "./behaviors/index.js";
19
19
  import uniq from "lodash/uniq.js";
@@ -549,86 +549,6 @@ function getSlateRangeFromEvent(editor, event) {
549
549
  }
550
550
  return range;
551
551
  }
552
- function toSlatePath(snapshot, path) {
553
- const blockKey = getBlockKeyFromSelectionPoint({
554
- path
555
- });
556
- if (!blockKey)
557
- return {
558
- block: void 0,
559
- child: void 0,
560
- path: []
561
- };
562
- const blockIndex = snapshot.blockIndexMap.get(blockKey);
563
- if (blockIndex === void 0)
564
- return {
565
- block: void 0,
566
- child: void 0,
567
- path: []
568
- };
569
- const block = snapshot.context.value.at(blockIndex);
570
- if (!block)
571
- return {
572
- block: void 0,
573
- child: void 0,
574
- path: []
575
- };
576
- if (!isTextBlock(snapshot.context, block))
577
- return {
578
- block,
579
- child: void 0,
580
- path: [blockIndex, 0]
581
- };
582
- const childKey = getChildKeyFromSelectionPoint({
583
- path
584
- });
585
- if (!childKey)
586
- return {
587
- block,
588
- child: void 0,
589
- path: [blockIndex, 0]
590
- };
591
- let childPath = [], childIndex = -1, pathChild;
592
- for (const child of block.children)
593
- if (childIndex++, child._key === childKey) {
594
- pathChild = child, isSpan(snapshot.context, child) ? childPath = [childIndex] : childPath = [childIndex, 0];
595
- break;
596
- }
597
- return childPath.length === 0 ? {
598
- block,
599
- child: void 0,
600
- path: [blockIndex, 0]
601
- } : {
602
- block,
603
- child: pathChild,
604
- path: [blockIndex].concat(childPath)
605
- };
606
- }
607
- function toSlateRange(snapshot) {
608
- if (!snapshot.context.selection)
609
- return null;
610
- const anchorPath = toSlatePath(snapshot, snapshot.context.selection.anchor.path), focusPath = toSlatePath(snapshot, snapshot.context.selection.focus.path);
611
- if (anchorPath.path.length === 0 || focusPath.path.length === 0)
612
- return null;
613
- const anchorOffset = anchorPath.child && isSpan(snapshot.context, anchorPath.child) ? Math.min(anchorPath.child.text.length, snapshot.context.selection.anchor.offset) : 0, focusOffset = focusPath.child && isSpan(snapshot.context, focusPath.child) ? Math.min(focusPath.child.text.length, snapshot.context.selection.focus.offset) : 0;
614
- return {
615
- anchor: {
616
- path: anchorPath.path,
617
- offset: anchorOffset
618
- },
619
- focus: {
620
- path: focusPath.path,
621
- offset: focusOffset
622
- }
623
- };
624
- }
625
- function moveRangeByOperation(range, operation) {
626
- const anchor = Point.transform(range.anchor, operation), focus = Point.transform(range.focus, operation);
627
- return anchor === null || focus === null ? null : Point.equals(anchor, range.anchor) && Point.equals(focus, range.focus) ? range : {
628
- anchor,
629
- focus
630
- };
631
- }
632
552
  function normalizePoint(point, value) {
633
553
  if (!point || !value)
634
554
  return null;
@@ -675,6 +595,72 @@ function normalizeSelection(selection, value) {
675
595
  backward: selection.backward
676
596
  } : null;
677
597
  }
598
+ function toSlateRange(snapshot) {
599
+ if (!snapshot.context.selection)
600
+ return null;
601
+ if (isEqualSelectionPoints(snapshot.context.selection.anchor, snapshot.context.selection.focus)) {
602
+ const anchorPoint2 = toSlateSelectionPoint(snapshot, snapshot.context.selection.anchor, snapshot.context.selection.backward ? "forward" : "backward");
603
+ return anchorPoint2 ? {
604
+ anchor: anchorPoint2,
605
+ focus: anchorPoint2
606
+ } : null;
607
+ }
608
+ const anchorPoint = toSlateSelectionPoint(snapshot, snapshot.context.selection.anchor, snapshot.context.selection.backward ? "forward" : "backward"), focusPoint = toSlateSelectionPoint(snapshot, snapshot.context.selection.focus, snapshot.context.selection.backward ? "backward" : "forward");
609
+ return !anchorPoint || !focusPoint ? null : {
610
+ anchor: anchorPoint,
611
+ focus: focusPoint
612
+ };
613
+ }
614
+ function toSlateSelectionPoint(snapshot, selectionPoint, direction) {
615
+ const blockKey = getBlockKeyFromSelectionPoint(selectionPoint);
616
+ if (!blockKey)
617
+ return;
618
+ const blockIndex = snapshot.blockIndexMap.get(blockKey);
619
+ if (blockIndex === void 0)
620
+ return;
621
+ const block = snapshot.context.value.at(blockIndex);
622
+ if (!block)
623
+ return;
624
+ if (!isTextBlock(snapshot.context, block))
625
+ return {
626
+ path: [blockIndex, 0],
627
+ offset: 0
628
+ };
629
+ let childKey = getChildKeyFromSelectionPoint({
630
+ path: selectionPoint.path
631
+ });
632
+ const spanSelectionPoint = childKey ? void 0 : blockOffsetToSpanSelectionPoint({
633
+ context: {
634
+ schema: snapshot.context.schema,
635
+ value: [block]
636
+ },
637
+ blockOffset: {
638
+ path: [{
639
+ _key: blockKey
640
+ }],
641
+ offset: selectionPoint.offset
642
+ },
643
+ direction
644
+ });
645
+ if (childKey = spanSelectionPoint ? getChildKeyFromSelectionPoint(spanSelectionPoint) : childKey, !childKey)
646
+ return {
647
+ path: [blockIndex, 0],
648
+ offset: 0
649
+ };
650
+ let offset = spanSelectionPoint?.offset ?? selectionPoint.offset, childPath = [], childIndex = -1, pathChild;
651
+ for (const child of block.children)
652
+ if (childIndex++, child._key === childKey) {
653
+ pathChild = child, isSpan(snapshot.context, child) ? childPath = [childIndex] : (childPath = [childIndex, 0], offset = 0);
654
+ break;
655
+ }
656
+ return childPath.length === 0 ? {
657
+ path: [blockIndex, 0],
658
+ offset: 0
659
+ } : {
660
+ path: [blockIndex].concat(childPath),
661
+ offset: isSpan(snapshot.context, pathChild) ? Math.min(pathChild.text.length, offset) : offset
662
+ };
663
+ }
678
664
  const IS_PROCESSING_REMOTE_CHANGES = /* @__PURE__ */ new WeakMap(), KEY_TO_SLATE_ELEMENT = /* @__PURE__ */ new WeakMap(), KEY_TO_VALUE_ELEMENT = /* @__PURE__ */ new WeakMap(), SLATE_TO_PORTABLE_TEXT_RANGE = /* @__PURE__ */ new WeakMap(), EditorActorContext = createContext({});
679
665
  function DropIndicator() {
680
666
  const $ = c(1);
@@ -3633,25 +3619,27 @@ const moveBackwardOperationImplementation = ({
3633
3619
  reverse: !0
3634
3620
  });
3635
3621
  }, moveBlockOperationImplementation = ({
3636
- context,
3637
3622
  operation
3638
3623
  }) => {
3639
- const at = [toSlatePath({
3640
- context: {
3641
- schema: context.schema,
3642
- value: operation.editor.value
3643
- },
3644
- blockIndexMap: operation.editor.blockIndexMap
3645
- }, operation.at).path[0]], to = [toSlatePath({
3646
- context: {
3647
- schema: context.schema,
3648
- value: operation.editor.value
3649
- },
3650
- blockIndexMap: operation.editor.blockIndexMap
3651
- }, operation.to).path[0]];
3624
+ const originKey = getBlockKeyFromSelectionPoint({
3625
+ path: operation.at
3626
+ });
3627
+ if (!originKey)
3628
+ throw new Error("Failed to get block key from selection point");
3629
+ const originBlockIndex = operation.editor.blockIndexMap.get(originKey);
3630
+ if (originBlockIndex === void 0)
3631
+ throw new Error("Failed to get block index from block key");
3632
+ const destinationKey = getBlockKeyFromSelectionPoint({
3633
+ path: operation.to
3634
+ });
3635
+ if (!destinationKey)
3636
+ throw new Error("Failed to get block key from selection point");
3637
+ const destinationBlockIndex = operation.editor.blockIndexMap.get(destinationKey);
3638
+ if (destinationBlockIndex === void 0)
3639
+ throw new Error("Failed to get block index from block key");
3652
3640
  Transforms.moveNodes(operation.editor, {
3653
- at,
3654
- to,
3641
+ at: [originBlockIndex],
3642
+ to: [destinationBlockIndex],
3655
3643
  mode: "highest"
3656
3644
  });
3657
3645
  }, moveForwardOperationImplementation = ({
@@ -10897,6 +10885,13 @@ function createWithHotkeys(editorActor, portableTextEditor, hotkeysFromOptions)
10897
10885
  }, editor;
10898
10886
  };
10899
10887
  }
10888
+ function moveRangeByOperation(range, operation) {
10889
+ const anchor = Point.transform(range.anchor, operation), focus = Point.transform(range.focus, operation);
10890
+ return anchor === null || focus === null ? null : Point.equals(anchor, range.anchor) && Point.equals(focus, range.focus) ? range : {
10891
+ anchor,
10892
+ focus
10893
+ };
10894
+ }
10900
10895
  const slateOperationCallback = ({
10901
10896
  input,
10902
10897
  sendBack