@portabletext/editor 1.55.14 → 1.55.16

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.cjs CHANGED
@@ -519,47 +519,78 @@ function getSlateRangeFromEvent(editor, event) {
519
519
  }
520
520
  return range;
521
521
  }
522
- function toSlatePath(path, editor) {
522
+ function toSlatePath(snapshot, path) {
523
523
  const blockKey = util_sliceBlocks.getBlockKeyFromSelectionPoint({
524
524
  path
525
525
  });
526
526
  if (!blockKey)
527
- return [];
528
- const blockIndex = editor.blockIndexMap.get(blockKey);
527
+ return {
528
+ block: void 0,
529
+ child: void 0,
530
+ path: []
531
+ };
532
+ const blockIndex = snapshot.blockIndexMap.get(blockKey);
529
533
  if (blockIndex === void 0)
530
- return [];
531
- const block = editor.children.at(blockIndex);
532
- if (!block || !slate.Element.isElement(block))
533
- return [];
534
- if (editor.isVoid(block))
535
- return [blockIndex, 0];
534
+ return {
535
+ block: void 0,
536
+ child: void 0,
537
+ path: []
538
+ };
539
+ const block = snapshot.context.value.at(blockIndex);
540
+ if (!block)
541
+ return {
542
+ block: void 0,
543
+ child: void 0,
544
+ path: []
545
+ };
546
+ if (!util_sliceBlocks.isTextBlock(snapshot.context, block))
547
+ return {
548
+ block,
549
+ child: void 0,
550
+ path: [blockIndex, 0]
551
+ };
536
552
  const childKey = util_sliceBlocks.getChildKeyFromSelectionPoint({
537
553
  path
538
554
  });
539
555
  if (!childKey)
540
- return [blockIndex, 0];
541
- let childPath = [], childIndex = -1;
556
+ return {
557
+ block,
558
+ child: void 0,
559
+ path: [blockIndex, 0]
560
+ };
561
+ let childPath = [], childIndex = -1, pathChild;
542
562
  for (const child of block.children)
543
563
  if (childIndex++, child._key === childKey) {
544
- slate.Element.isElement(child) && editor.isVoid(child) ? childPath = [childIndex, 0] : childPath = [childIndex];
564
+ pathChild = child, util_sliceBlocks.isSpan(snapshot.context, child) ? childPath = [childIndex] : childPath = [childIndex, 0];
545
565
  break;
546
566
  }
547
- return [blockIndex].concat(childPath);
567
+ return childPath.length === 0 ? {
568
+ block,
569
+ child: void 0,
570
+ path: [blockIndex, 0]
571
+ } : {
572
+ block,
573
+ child: pathChild,
574
+ path: [blockIndex].concat(childPath)
575
+ };
548
576
  }
549
- function toSlateRange(selection, editor) {
550
- if (!selection || !editor)
577
+ function toSlateRange(snapshot) {
578
+ if (!snapshot.context.selection)
579
+ return null;
580
+ const anchorPath = toSlatePath(snapshot, snapshot.context.selection.anchor.path), focusPath = toSlatePath(snapshot, snapshot.context.selection.focus.path);
581
+ if (anchorPath.path.length === 0 || focusPath.path.length === 0)
551
582
  return null;
552
- const anchor = {
553
- path: toSlatePath(selection.anchor.path, editor),
554
- offset: selection.anchor.offset
555
- }, focus = {
556
- path: toSlatePath(selection.focus.path, editor),
557
- offset: selection.focus.offset
583
+ const anchorOffset = anchorPath.child && util_sliceBlocks.isSpan(snapshot.context, anchorPath.child) ? Math.min(anchorPath.child.text.length, snapshot.context.selection.anchor.offset) : 0, focusOffset = focusPath.child && util_sliceBlocks.isSpan(snapshot.context, focusPath.child) ? Math.min(focusPath.child.text.length, snapshot.context.selection.focus.offset) : 0;
584
+ return {
585
+ anchor: {
586
+ path: anchorPath.path,
587
+ offset: anchorOffset
588
+ },
589
+ focus: {
590
+ path: focusPath.path,
591
+ offset: focusOffset
592
+ }
558
593
  };
559
- return focus.path.length === 0 || anchor.path.length === 0 ? null : anchor && focus ? {
560
- anchor,
561
- focus
562
- } : null;
563
594
  }
564
595
  function moveRangeByOperation(range, operation) {
565
596
  const anchor = slate.Point.transform(range.anchor, operation), focus = slate.Point.transform(range.focus, operation);
@@ -2875,15 +2906,22 @@ const addAnnotationOperationImplementation = ({
2875
2906
  operation
2876
2907
  }) => {
2877
2908
  const location = toSlateRange({
2878
- anchor: {
2879
- path: operation.at,
2880
- offset: 0
2909
+ context: {
2910
+ schema: context.schema,
2911
+ value: operation.editor.value,
2912
+ selection: {
2913
+ anchor: {
2914
+ path: operation.at,
2915
+ offset: 0
2916
+ },
2917
+ focus: {
2918
+ path: operation.at,
2919
+ offset: 0
2920
+ }
2921
+ }
2881
2922
  },
2882
- focus: {
2883
- path: operation.at,
2884
- offset: 0
2885
- }
2886
- }, operation.editor);
2923
+ blockIndexMap: operation.editor.blockIndexMap
2924
+ });
2887
2925
  if (!location)
2888
2926
  throw new Error(`Unable to convert ${JSON.stringify(operation.at)} into a Slate Range`);
2889
2927
  const block = slate.Editor.node(operation.editor, location, {
@@ -2923,15 +2961,22 @@ const addAnnotationOperationImplementation = ({
2923
2961
  operation
2924
2962
  }) => {
2925
2963
  const location = toSlateRange({
2926
- anchor: {
2927
- path: operation.at,
2928
- offset: 0
2964
+ context: {
2965
+ schema: context.schema,
2966
+ value: operation.editor.value,
2967
+ selection: {
2968
+ anchor: {
2969
+ path: operation.at,
2970
+ offset: 0
2971
+ },
2972
+ focus: {
2973
+ path: operation.at,
2974
+ offset: 0
2975
+ }
2976
+ }
2929
2977
  },
2930
- focus: {
2931
- path: operation.at,
2932
- offset: 0
2933
- }
2934
- }, operation.editor);
2978
+ blockIndexMap: operation.editor.blockIndexMap
2979
+ });
2935
2980
  if (!location)
2936
2981
  throw new Error(`Unable to convert ${JSON.stringify(operation.at)} into a Slate Range`);
2937
2982
  const block = slate.Editor.node(operation.editor, location, {
@@ -2988,15 +3033,22 @@ const addAnnotationOperationImplementation = ({
2988
3033
  operation
2989
3034
  }) => {
2990
3035
  const location = toSlateRange({
2991
- anchor: {
2992
- path: operation.at,
2993
- offset: 0
3036
+ context: {
3037
+ schema: context.schema,
3038
+ value: operation.editor.value,
3039
+ selection: {
3040
+ anchor: {
3041
+ path: operation.at,
3042
+ offset: 0
3043
+ },
3044
+ focus: {
3045
+ path: operation.at,
3046
+ offset: 0
3047
+ }
3048
+ }
2994
3049
  },
2995
- focus: {
2996
- path: operation.at,
2997
- offset: 0
2998
- }
2999
- }, operation.editor);
3050
+ blockIndexMap: operation.editor.blockIndexMap
3051
+ });
3000
3052
  if (!location)
3001
3053
  throw new Error(`Unable to convert ${JSON.stringify(operation.at)} into a Slate Range`);
3002
3054
  const childEntry = slate.Editor.node(operation.editor, location, {
@@ -3057,15 +3109,22 @@ const addAnnotationOperationImplementation = ({
3057
3109
  operation
3058
3110
  }) => {
3059
3111
  const location = toSlateRange({
3060
- anchor: {
3061
- path: operation.at,
3062
- offset: 0
3112
+ context: {
3113
+ schema: context.schema,
3114
+ value: operation.editor.value,
3115
+ selection: {
3116
+ anchor: {
3117
+ path: operation.at,
3118
+ offset: 0
3119
+ },
3120
+ focus: {
3121
+ path: operation.at,
3122
+ offset: 0
3123
+ }
3124
+ }
3063
3125
  },
3064
- focus: {
3065
- path: operation.at,
3066
- offset: 0
3067
- }
3068
- }, operation.editor);
3126
+ blockIndexMap: operation.editor.blockIndexMap
3127
+ });
3069
3128
  if (!location)
3070
3129
  throw new Error(`Unable to convert ${JSON.stringify(operation.at)} into a Slate Range`);
3071
3130
  const childEntry = slate.Editor.node(operation.editor, location, {
@@ -3130,7 +3189,14 @@ const addAnnotationOperationImplementation = ({
3130
3189
  }) : void 0, manualSelection = manualAnchor && manualFocus ? {
3131
3190
  anchor: manualAnchor,
3132
3191
  focus: manualFocus
3133
- } : void 0, selection = manualSelection ? toSlateRange(manualSelection, operation.editor) ?? editor.selection : editor.selection;
3192
+ } : void 0, selection = manualSelection ? toSlateRange({
3193
+ context: {
3194
+ schema: context.schema,
3195
+ value: operation.editor.value,
3196
+ selection: manualSelection
3197
+ },
3198
+ blockIndexMap: operation.editor.blockIndexMap
3199
+ }) ?? editor.selection : editor.selection;
3134
3200
  if (!selection)
3135
3201
  return;
3136
3202
  const editorSelection = slateRangeToSelection({
@@ -3183,7 +3249,14 @@ const addAnnotationOperationImplementation = ({
3183
3249
  });
3184
3250
  if (!trimmedSelection)
3185
3251
  throw new Error("Unable to find trimmed selection");
3186
- const newRange = toSlateRange(trimmedSelection, editor);
3252
+ const newRange = toSlateRange({
3253
+ context: {
3254
+ schema: context.schema,
3255
+ value: operation.editor.value,
3256
+ selection: trimmedSelection
3257
+ },
3258
+ blockIndexMap: operation.editor.blockIndexMap
3259
+ });
3187
3260
  if (!newRange)
3188
3261
  throw new Error("Unable to find new selection");
3189
3262
  const splitTextNodes = slate.Range.isRange(newRange) ? [...slate.Editor.nodes(editor, {
@@ -3244,7 +3317,14 @@ const addAnnotationOperationImplementation = ({
3244
3317
  }), operation.editor.children.length === 0 && slate.Transforms.insertNodes(operation.editor, createPlaceholderBlock(context));
3245
3318
  return;
3246
3319
  }
3247
- const range = toSlateRange(operation.at, operation.editor);
3320
+ const range = toSlateRange({
3321
+ context: {
3322
+ schema: context.schema,
3323
+ value: operation.editor.value,
3324
+ selection: operation.at
3325
+ },
3326
+ blockIndexMap: operation.editor.blockIndexMap
3327
+ });
3248
3328
  if (!range)
3249
3329
  throw new Error(`Failed to get Slate Range for selection ${JSON.stringify(operation.at)}`);
3250
3330
  operation.editor.delete({
@@ -3517,9 +3597,22 @@ const moveBackwardOperationImplementation = ({
3517
3597
  reverse: !0
3518
3598
  });
3519
3599
  }, moveBlockOperationImplementation = ({
3600
+ context,
3520
3601
  operation
3521
3602
  }) => {
3522
- const at = [toSlatePath(operation.at, operation.editor)[0]], to = [toSlatePath(operation.to, operation.editor)[0]];
3603
+ const at = [toSlatePath({
3604
+ context: {
3605
+ schema: context.schema,
3606
+ value: operation.editor.value
3607
+ },
3608
+ blockIndexMap: operation.editor.blockIndexMap
3609
+ }, operation.at).path[0]], to = [toSlatePath({
3610
+ context: {
3611
+ schema: context.schema,
3612
+ value: operation.editor.value
3613
+ },
3614
+ blockIndexMap: operation.editor.blockIndexMap
3615
+ }, operation.to).path[0]];
3523
3616
  slate.Transforms.moveNodes(operation.editor, {
3524
3617
  at,
3525
3618
  to,
@@ -3533,9 +3626,17 @@ const moveBackwardOperationImplementation = ({
3533
3626
  distance: operation.distance
3534
3627
  });
3535
3628
  }, selectOperationImplementation = ({
3629
+ context,
3536
3630
  operation
3537
3631
  }) => {
3538
- const newSelection = toSlateRange(operation.at, operation.editor);
3632
+ const newSelection = toSlateRange({
3633
+ context: {
3634
+ schema: context.schema,
3635
+ value: operation.editor.value,
3636
+ selection: operation.at
3637
+ },
3638
+ blockIndexMap: operation.editor.blockIndexMap
3639
+ });
3539
3640
  newSelection ? slate.Transforms.select(operation.editor, newSelection) : slate.Transforms.deselect(operation.editor);
3540
3641
  }, behaviorOperationImplementations = {
3541
3642
  "annotation.add": addAnnotationOperationImplementation,
@@ -5171,7 +5272,7 @@ function createEditorDom(sendBack, slateEditor) {
5171
5272
  function getBlockNodes(slateEditor, snapshot) {
5172
5273
  if (!snapshot.context.selection)
5173
5274
  return [];
5174
- const range = toSlateRange(snapshot.context.selection, slateEditor);
5275
+ const range = toSlateRange(snapshot);
5175
5276
  if (!range)
5176
5277
  return [];
5177
5278
  try {
@@ -5187,7 +5288,7 @@ function getBlockNodes(slateEditor, snapshot) {
5187
5288
  function getChildNodes(slateEditor, snapshot) {
5188
5289
  if (!snapshot.context.selection)
5189
5290
  return [];
5190
- const range = toSlateRange(snapshot.context.selection, slateEditor);
5291
+ const range = toSlateRange(snapshot);
5191
5292
  if (!range)
5192
5293
  return [];
5193
5294
  try {
@@ -5931,16 +6032,16 @@ const coreDndBehaviors = [
5931
6032
  dragOrigin,
5932
6033
  dropPosition,
5933
6034
  originEvent
5934
- }) => [...draggingEntireBlocks ? draggedBlocks.map((block) => behaviors_index.raise({
6035
+ }) => [behaviors_index.raise({
6036
+ type: "select",
6037
+ at: dropPosition
6038
+ }), ...draggingEntireBlocks ? draggedBlocks.map((block) => behaviors_index.raise({
5935
6039
  type: "delete.block",
5936
6040
  at: block.path
5937
6041
  })) : [behaviors_index.raise({
5938
6042
  type: "delete",
5939
6043
  at: dragOrigin.selection
5940
6044
  })], behaviors_index.raise({
5941
- type: "select",
5942
- at: dropPosition
5943
- }), behaviors_index.raise({
5944
6045
  type: "insert.blocks",
5945
6046
  blocks: event.data,
5946
6047
  placement: draggingEntireBlocks ? originEvent.position.block === "start" ? "before" : originEvent.position.block === "end" ? "after" : "auto" : "auto"
@@ -8502,7 +8603,14 @@ function createEditableAPI(editor, editorActor) {
8502
8603
  });
8503
8604
  },
8504
8605
  select: (selection) => {
8505
- const slateSelection = toSlateRange(selection, editor);
8606
+ const slateSelection = toSlateRange({
8607
+ context: {
8608
+ schema: editorActor.getSnapshot().context.schema,
8609
+ value: editor.value,
8610
+ selection
8611
+ },
8612
+ blockIndexMap: editor.blockIndexMap
8613
+ });
8506
8614
  slateSelection ? slate.Transforms.select(editor, slateSelection) : slate.Transforms.deselect(editor), editor.onChange();
8507
8615
  },
8508
8616
  focusBlock: () => {
@@ -8608,15 +8716,22 @@ function createEditableAPI(editor, editorActor) {
8608
8716
  isVoid: (element) => ![types2.block.name, types2.span.name].includes(element._type),
8609
8717
  findByPath: (path) => {
8610
8718
  const slatePath = toSlateRange({
8611
- focus: {
8612
- path,
8613
- offset: 0
8719
+ context: {
8720
+ schema: editorActor.getSnapshot().context.schema,
8721
+ value: editor.value,
8722
+ selection: {
8723
+ focus: {
8724
+ path,
8725
+ offset: 0
8726
+ },
8727
+ anchor: {
8728
+ path,
8729
+ offset: 0
8730
+ }
8731
+ }
8614
8732
  },
8615
- anchor: {
8616
- path,
8617
- offset: 0
8618
- }
8619
- }, editor);
8733
+ blockIndexMap: editor.blockIndexMap
8734
+ });
8620
8735
  if (slatePath) {
8621
8736
  const [block, blockPath] = slate.Editor.node(editor, slatePath.focus.path.slice(0, 1));
8622
8737
  if (block && blockPath && typeof block._key == "string") {
@@ -8698,7 +8813,14 @@ function createEditableAPI(editor, editorActor) {
8698
8813
  },
8699
8814
  delete: (selection, options) => {
8700
8815
  if (selection) {
8701
- const range = toSlateRange(selection, editor);
8816
+ const range = toSlateRange({
8817
+ context: {
8818
+ schema: editorActor.getSnapshot().context.schema,
8819
+ value: editor.value,
8820
+ selection
8821
+ },
8822
+ blockIndexMap: editor.blockIndexMap
8823
+ });
8702
8824
  if (!(range && range.anchor.path.length > 0 && range.focus.path.length > 0))
8703
8825
  throw new Error("Invalid range");
8704
8826
  if (range) {
@@ -8710,7 +8832,7 @@ function createEditableAPI(editor, editorActor) {
8710
8832
  }), editor.onChange();
8711
8833
  return;
8712
8834
  }
8713
- options?.mode === "blocks" && (debug$5("Deleting blocks touched by selection"), slate.Transforms.removeNodes(editor, {
8835
+ if (options?.mode === "blocks" && (debug$5("Deleting blocks touched by selection"), slate.Transforms.removeNodes(editor, {
8714
8836
  at: range,
8715
8837
  voids: !0,
8716
8838
  match: (node) => editor.isTextBlock(node) || !editor.isTextBlock(node) && slate.Element.isElement(node)
@@ -8719,9 +8841,17 @@ function createEditableAPI(editor, editorActor) {
8719
8841
  voids: !0,
8720
8842
  match: (node) => node._type === types2.span.name || // Text children
8721
8843
  !editor.isTextBlock(node) && slate.Element.isElement(node)
8722
- })), editor.children.length === 0 && (editor.children = [editor.pteCreateTextBlock({
8723
- decorators: []
8724
- })]), editor.onChange();
8844
+ })), editor.children.length === 0) {
8845
+ const placeholderBlock = createPlaceholderBlock(editorActor.getSnapshot().context);
8846
+ editor.children = [placeholderBlock], editor.value = [placeholderBlock], buildIndexMaps({
8847
+ schema: editorActor.getSnapshot().context.schema,
8848
+ value: editor.value
8849
+ }, {
8850
+ blockIndexMap: editor.blockIndexMap,
8851
+ listIndexMap: editor.listIndexMap
8852
+ });
8853
+ }
8854
+ editor.onChange();
8725
8855
  }
8726
8856
  }
8727
8857
  },
@@ -8759,7 +8889,21 @@ function createEditableAPI(editor, editorActor) {
8759
8889
  },
8760
8890
  getFragment: () => fromSlateValue(editor.getFragment(), types2.block.name),
8761
8891
  isSelectionsOverlapping: (selectionA, selectionB) => {
8762
- const rangeA = toSlateRange(selectionA, editor), rangeB = toSlateRange(selectionB, editor);
8892
+ const rangeA = toSlateRange({
8893
+ context: {
8894
+ schema: editorActor.getSnapshot().context.schema,
8895
+ value: editor.value,
8896
+ selection: selectionA
8897
+ },
8898
+ blockIndexMap: editor.blockIndexMap
8899
+ }), rangeB = toSlateRange({
8900
+ context: {
8901
+ schema: editorActor.getSnapshot().context.schema,
8902
+ value: editor.value,
8903
+ selection: selectionB
8904
+ },
8905
+ blockIndexMap: editor.blockIndexMap
8906
+ });
8763
8907
  return slate.Range.isRange(rangeA) && slate.Range.isRange(rangeB) && slate.Range.includes(rangeA, rangeB);
8764
8908
  }
8765
8909
  };
@@ -10727,7 +10871,14 @@ const slateOperationCallback = ({
10727
10871
  }) => {
10728
10872
  const rangeDecorationState = [];
10729
10873
  for (const rangeDecoration of context.pendingRangeDecorations) {
10730
- const slateRange = toSlateRange(rangeDecoration.selection, context.slateEditor);
10874
+ const slateRange = toSlateRange({
10875
+ context: {
10876
+ schema: context.schema,
10877
+ value: context.slateEditor.value,
10878
+ selection: rangeDecoration.selection
10879
+ },
10880
+ blockIndexMap: context.slateEditor.blockIndexMap
10881
+ });
10731
10882
  if (!slate.Range.isRange(slateRange)) {
10732
10883
  rangeDecoration.onMoved?.({
10733
10884
  newSelection: null,
@@ -10751,7 +10902,14 @@ const slateOperationCallback = ({
10751
10902
  return;
10752
10903
  const rangeDecorationState = [];
10753
10904
  for (const rangeDecoration of event.rangeDecorations) {
10754
- const slateRange = toSlateRange(rangeDecoration.selection, context.slateEditor);
10905
+ const slateRange = toSlateRange({
10906
+ context: {
10907
+ schema: context.schema,
10908
+ value: context.slateEditor.value,
10909
+ selection: rangeDecoration.selection
10910
+ },
10911
+ blockIndexMap: context.slateEditor.blockIndexMap
10912
+ });
10755
10913
  if (!slate.Range.isRange(slateRange)) {
10756
10914
  rangeDecoration.onMoved?.({
10757
10915
  newSelection: null,
@@ -10775,7 +10933,14 @@ const slateOperationCallback = ({
10775
10933
  return;
10776
10934
  const rangeDecorationState = [];
10777
10935
  for (const decoratedRange of context.slateEditor.decoratedRanges) {
10778
- const slateRange = toSlateRange(decoratedRange.rangeDecoration.selection, context.slateEditor);
10936
+ const slateRange = toSlateRange({
10937
+ context: {
10938
+ schema: context.schema,
10939
+ value: context.slateEditor.value,
10940
+ selection: decoratedRange.rangeDecoration.selection
10941
+ },
10942
+ blockIndexMap: context.slateEditor.blockIndexMap
10943
+ });
10779
10944
  if (!slate.Range.isRange(slateRange)) {
10780
10945
  decoratedRange.rangeDecoration.onMoved?.({
10781
10946
  newSelection: null,
@@ -11029,7 +11194,14 @@ const debug = debugWithName("component:Editable"), PortableTextEditable = React.
11029
11194
  const normalizedSelection = normalizeSelection(propsSelection, fromSlateValue(slateEditor.children, editorActor.getSnapshot().context.schema.block.name));
11030
11195
  if (normalizedSelection !== null) {
11031
11196
  debug(`Normalized selection from props ${JSON.stringify(normalizedSelection)}`);
11032
- const slateRange = toSlateRange(normalizedSelection, slateEditor);
11197
+ const slateRange = toSlateRange({
11198
+ context: {
11199
+ schema: editorActor.getSnapshot().context.schema,
11200
+ value: slateEditor.value,
11201
+ selection: normalizedSelection
11202
+ },
11203
+ blockIndexMap: slateEditor.blockIndexMap
11204
+ });
11033
11205
  slateRange && (slate.Transforms.select(slateEditor, slateRange), slateEditor.operations.some((o) => o.type === "set_selection") || editorActor.send({
11034
11206
  type: "update selection",
11035
11207
  selection: normalizedSelection