@portabletext/editor 1.57.3 → 1.57.5

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 +143 -137
  13. package/lib/index.cjs.map +1 -1
  14. package/lib/index.js +108 -102
  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 +13 -13
  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 +25 -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);
@@ -3334,7 +3320,16 @@ const addAnnotationOperationImplementation = ({
3334
3320
  context,
3335
3321
  operation
3336
3322
  }) => {
3337
- const anchorBlockKey = getBlockKeyFromSelectionPoint(operation.at.anchor), focusBlockKey = getBlockKeyFromSelectionPoint(operation.at.focus), anchorBlockPath = anchorBlockKey !== void 0 ? getBlockPath({
3323
+ const anchorBlockKey = getBlockKeyFromSelectionPoint(operation.at.anchor), focusBlockKey = getBlockKeyFromSelectionPoint(operation.at.focus), endBlockKey = operation.at.backward ? anchorBlockKey : focusBlockKey, endOffset = operation.at.backward ? operation.at.focus.offset : operation.at.anchor.offset;
3324
+ if (!endBlockKey)
3325
+ throw new Error("Failed to get end block key");
3326
+ const endBlockIndex = operation.editor.blockIndexMap.get(endBlockKey);
3327
+ if (endBlockIndex === void 0)
3328
+ throw new Error("Failed to get end block index");
3329
+ const endBlock = operation.editor.value.at(endBlockIndex);
3330
+ if (!endBlock)
3331
+ throw new Error("Failed to get end block");
3332
+ const anchorBlockPath = anchorBlockKey !== void 0 ? getBlockPath({
3338
3333
  editor: operation.editor,
3339
3334
  _key: anchorBlockKey
3340
3335
  }) : void 0, focusBlockPath = focusBlockKey !== void 0 ? getBlockPath({
@@ -3357,10 +3352,12 @@ const addAnnotationOperationImplementation = ({
3357
3352
  });
3358
3353
  if (!range)
3359
3354
  throw new Error(`Failed to get Slate Range for selection ${JSON.stringify(operation.at)}`);
3355
+ const hanging = isTextBlock(context, endBlock) && endOffset === 0;
3360
3356
  operation.editor.delete({
3361
3357
  at: range,
3362
3358
  reverse: operation.direction === "backward",
3363
- unit: operation.unit
3359
+ unit: operation.unit,
3360
+ hanging
3364
3361
  });
3365
3362
  }, insertInlineObjectOperationImplementation = ({
3366
3363
  context,
@@ -3633,25 +3630,27 @@ const moveBackwardOperationImplementation = ({
3633
3630
  reverse: !0
3634
3631
  });
3635
3632
  }, moveBlockOperationImplementation = ({
3636
- context,
3637
3633
  operation
3638
3634
  }) => {
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]];
3635
+ const originKey = getBlockKeyFromSelectionPoint({
3636
+ path: operation.at
3637
+ });
3638
+ if (!originKey)
3639
+ throw new Error("Failed to get block key from selection point");
3640
+ const originBlockIndex = operation.editor.blockIndexMap.get(originKey);
3641
+ if (originBlockIndex === void 0)
3642
+ throw new Error("Failed to get block index from block key");
3643
+ const destinationKey = getBlockKeyFromSelectionPoint({
3644
+ path: operation.to
3645
+ });
3646
+ if (!destinationKey)
3647
+ throw new Error("Failed to get block key from selection point");
3648
+ const destinationBlockIndex = operation.editor.blockIndexMap.get(destinationKey);
3649
+ if (destinationBlockIndex === void 0)
3650
+ throw new Error("Failed to get block index from block key");
3652
3651
  Transforms.moveNodes(operation.editor, {
3653
- at,
3654
- to,
3652
+ at: [originBlockIndex],
3653
+ to: [destinationBlockIndex],
3655
3654
  mode: "highest"
3656
3655
  });
3657
3656
  }, moveForwardOperationImplementation = ({
@@ -10897,6 +10896,13 @@ function createWithHotkeys(editorActor, portableTextEditor, hotkeysFromOptions)
10897
10896
  }, editor;
10898
10897
  };
10899
10898
  }
10899
+ function moveRangeByOperation(range, operation) {
10900
+ const anchor = Point.transform(range.anchor, operation), focus = Point.transform(range.focus, operation);
10901
+ return anchor === null || focus === null ? null : Point.equals(anchor, range.anchor) && Point.equals(focus, range.focus) ? range : {
10902
+ anchor,
10903
+ focus
10904
+ };
10905
+ }
10900
10906
  const slateOperationCallback = ({
10901
10907
  input,
10902
10908
  sendBack