@portabletext/editor 1.44.15 → 1.44.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.
@@ -5,7 +5,7 @@ import { ReactEditor, withReact, Slate } from "slate-react";
5
5
  import { useSelector, useActorRef } from "@xstate/react";
6
6
  import debug$g from "debug";
7
7
  import isEqual from "lodash/isEqual.js";
8
- import { Element, Text, Editor, Path, Operation, Transforms, insertText, Node, Range, Point, createEditor } from "slate";
8
+ import { Element, Text, Editor, Path, Operation, Transforms, Node, Range, Point, createEditor } from "slate";
9
9
  import { setup, stateIn, fromCallback, assign, enqueueActions, emit, assertEvent, and, not, createActor } from "xstate";
10
10
  import { unset, set, setIfMissing, insert, diffMatchPatch as diffMatchPatch$1, applyAll } from "@portabletext/patches";
11
11
  import { isPortableTextTextBlock, isKeySegment, isPortableTextSpan as isPortableTextSpan$1, isPortableTextListBlock, defineType, defineField } from "@sanity/types";
@@ -917,8 +917,8 @@ function deisolateChar(diffs, i, dir) {
917
917
  }
918
918
  }
919
919
  if (insertIdx !== null && deleteIdx !== null && hasSharedChar(diffs, insertIdx, deleteIdx, dir)) {
920
- const [insertText2, insertChar] = splitChar(diffs[insertIdx][1], inv), [deleteText] = splitChar(diffs[deleteIdx][1], inv);
921
- diffs[insertIdx][1] = insertText2, diffs[deleteIdx][1] = deleteText, diffs[i][1] = combineChar(diffs[i][1], insertChar, dir);
920
+ const [insertText, insertChar] = splitChar(diffs[insertIdx][1], inv), [deleteText] = splitChar(diffs[deleteIdx][1], inv);
921
+ diffs[insertIdx][1] = insertText, diffs[deleteIdx][1] = deleteText, diffs[i][1] = combineChar(diffs[i][1], insertChar, dir);
922
922
  return;
923
923
  }
924
924
  const [text, char] = splitChar(diffs[i][1], dir);
@@ -2696,74 +2696,10 @@ function createOperationToPatches(types) {
2696
2696
  splitNodePatch
2697
2697
  };
2698
2698
  }
2699
- const insertBreakActionImplementation = ({
2700
- context,
2701
- action
2702
- }) => {
2703
- const keyGenerator = context.keyGenerator, schema = context.schema, editor = action.editor;
2704
- if (!editor.selection)
2705
- return;
2706
- const anchorBlockPath = editor.selection.anchor.path.slice(0, 1), focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = Node.descendant(editor, focusBlockPath);
2707
- if (editor.isTextBlock(focusBlock) && anchorBlockPath[0] === focusBlockPath[0]) {
2708
- Transforms.splitNodes(editor, {
2709
- at: editor.selection,
2710
- always: !0
2711
- });
2712
- const [nextBlock, nextBlockPath] = Editor.node(editor, Path.next(focusBlockPath), {
2713
- depth: 1
2714
- }), nextChild = Node.child(nextBlock, 0);
2715
- if (!editor.isTextSpan(nextChild) && Transforms.insertNodes(editor, {
2716
- _key: context.keyGenerator(),
2717
- _type: "span",
2718
- text: "",
2719
- marks: []
2720
- }, {
2721
- at: [nextBlockPath[0], 0]
2722
- }), Transforms.setSelection(editor, {
2723
- anchor: {
2724
- path: [...nextBlockPath, 0],
2725
- offset: 0
2726
- },
2727
- focus: {
2728
- path: [...nextBlockPath, 0],
2729
- offset: 0
2730
- }
2731
- }), editor.isTextBlock(nextBlock) && nextBlock.markDefs && nextBlock.markDefs.length > 0) {
2732
- const newMarkDefKeys = /* @__PURE__ */ new Map(), prevNodeSpans = Array.from(Node.children(editor, focusBlockPath)).map((entry) => entry[0]).filter((node) => editor.isTextSpan(node)), children = Node.children(editor, nextBlockPath);
2733
- for (const [child, childPath] of children) {
2734
- if (!editor.isTextSpan(child))
2735
- continue;
2736
- const marks = child.marks ?? [];
2737
- for (const mark of marks)
2738
- schema.decorators.some((decorator) => decorator.value === mark) || prevNodeSpans.some((prevNodeSpan) => prevNodeSpan.marks?.includes(mark)) && !newMarkDefKeys.has(mark) && newMarkDefKeys.set(mark, keyGenerator());
2739
- const newMarks = marks.map((mark) => newMarkDefKeys.get(mark) ?? mark);
2740
- isEqual(marks, newMarks) || Transforms.setNodes(editor, {
2741
- marks: newMarks
2742
- }, {
2743
- at: childPath
2744
- });
2745
- }
2746
- const newMarkDefs = nextBlock.markDefs.map((markDef) => ({
2747
- ...markDef,
2748
- _key: newMarkDefKeys.get(markDef._key) ?? markDef._key
2749
- }));
2750
- isEqual(nextBlock.markDefs, newMarkDefs) || Transforms.setNodes(editor, {
2751
- markDefs: newMarkDefs
2752
- }, {
2753
- at: nextBlockPath,
2754
- match: (node) => editor.isTextBlock(node)
2755
- });
2756
- }
2757
- return;
2758
- }
2759
- Transforms.splitNodes(editor, {
2760
- always: !0
2761
- });
2762
- }, insertSoftBreakActionImplementation = ({
2699
+ const insertTextActionImplementation = ({
2763
2700
  action
2764
2701
  }) => {
2765
- insertText(action.editor, `
2766
- `);
2702
+ action.editor.insertText(action.text);
2767
2703
  };
2768
2704
  function isPortableTextSpan(node) {
2769
2705
  return node._type === "span" && "text" in node && typeof node.text == "string" && (typeof node.marks > "u" || Array.isArray(node.marks) && node.marks.every((mark) => typeof mark == "string"));
@@ -4089,11 +4025,7 @@ function insertBlock({
4089
4025
  }
4090
4026
  }
4091
4027
  }
4092
- const insertTextActionImplementation = ({
4093
- action
4094
- }) => {
4095
- action.editor.insertText(action.text);
4096
- }, moveBlockActionImplementation = ({
4028
+ const moveBlockActionImplementation = ({
4097
4029
  action
4098
4030
  }) => {
4099
4031
  const at = [toSlatePath(action.at, action.editor)[0]], to = [toSlatePath(action.to, action.editor)[0]];
@@ -4108,6 +4040,69 @@ const insertTextActionImplementation = ({
4108
4040
  }) => {
4109
4041
  const newSelection = toSlateRange(action.selection, action.editor);
4110
4042
  newSelection ? Transforms.select(action.editor, newSelection) : Transforms.deselect(action.editor);
4043
+ }, splitBlockActionImplementation = ({
4044
+ context,
4045
+ action
4046
+ }) => {
4047
+ const keyGenerator = context.keyGenerator, schema = context.schema, editor = action.editor;
4048
+ if (!editor.selection)
4049
+ return;
4050
+ const anchorBlockPath = editor.selection.anchor.path.slice(0, 1), focusBlockPath = editor.selection.focus.path.slice(0, 1), focusBlock = Node.descendant(editor, focusBlockPath);
4051
+ if (editor.isTextBlock(focusBlock) && anchorBlockPath[0] === focusBlockPath[0]) {
4052
+ Transforms.splitNodes(editor, {
4053
+ at: editor.selection,
4054
+ always: !0
4055
+ });
4056
+ const [nextBlock, nextBlockPath] = Editor.node(editor, Path.next(focusBlockPath), {
4057
+ depth: 1
4058
+ }), nextChild = Node.child(nextBlock, 0);
4059
+ if (!editor.isTextSpan(nextChild) && Transforms.insertNodes(editor, {
4060
+ _key: context.keyGenerator(),
4061
+ _type: "span",
4062
+ text: "",
4063
+ marks: []
4064
+ }, {
4065
+ at: [nextBlockPath[0], 0]
4066
+ }), Transforms.setSelection(editor, {
4067
+ anchor: {
4068
+ path: [...nextBlockPath, 0],
4069
+ offset: 0
4070
+ },
4071
+ focus: {
4072
+ path: [...nextBlockPath, 0],
4073
+ offset: 0
4074
+ }
4075
+ }), editor.isTextBlock(nextBlock) && nextBlock.markDefs && nextBlock.markDefs.length > 0) {
4076
+ const newMarkDefKeys = /* @__PURE__ */ new Map(), prevNodeSpans = Array.from(Node.children(editor, focusBlockPath)).map((entry) => entry[0]).filter((node) => editor.isTextSpan(node)), children = Node.children(editor, nextBlockPath);
4077
+ for (const [child, childPath] of children) {
4078
+ if (!editor.isTextSpan(child))
4079
+ continue;
4080
+ const marks = child.marks ?? [];
4081
+ for (const mark of marks)
4082
+ schema.decorators.some((decorator) => decorator.value === mark) || prevNodeSpans.some((prevNodeSpan) => prevNodeSpan.marks?.includes(mark)) && !newMarkDefKeys.has(mark) && newMarkDefKeys.set(mark, keyGenerator());
4083
+ const newMarks = marks.map((mark) => newMarkDefKeys.get(mark) ?? mark);
4084
+ isEqual(marks, newMarks) || Transforms.setNodes(editor, {
4085
+ marks: newMarks
4086
+ }, {
4087
+ at: childPath
4088
+ });
4089
+ }
4090
+ const newMarkDefs = nextBlock.markDefs.map((markDef) => ({
4091
+ ...markDef,
4092
+ _key: newMarkDefKeys.get(markDef._key) ?? markDef._key
4093
+ }));
4094
+ isEqual(nextBlock.markDefs, newMarkDefs) || Transforms.setNodes(editor, {
4095
+ markDefs: newMarkDefs
4096
+ }, {
4097
+ at: nextBlockPath,
4098
+ match: (node) => editor.isTextBlock(node)
4099
+ });
4100
+ }
4101
+ return;
4102
+ }
4103
+ Transforms.splitNodes(editor, {
4104
+ always: !0
4105
+ });
4111
4106
  }, debug$a = debugWithName("behaviors:action"), behaviorActionImplementations = {
4112
4107
  "annotation.add": addAnnotationActionImplementation,
4113
4108
  "annotation.remove": removeAnnotationActionImplementation,
@@ -4125,15 +4120,14 @@ const insertTextActionImplementation = ({
4125
4120
  "history.redo": historyRedoActionImplementation,
4126
4121
  "history.undo": historyUndoActionImplementation,
4127
4122
  "insert.block": insertBlockActionImplementation,
4128
- "insert.break": insertBreakActionImplementation,
4129
4123
  "insert.inline object": insertInlineObjectActionImplementation,
4130
- "insert.soft break": insertSoftBreakActionImplementation,
4131
4124
  "insert.span": insertSpanActionImplementation,
4132
4125
  "insert.text": insertTextActionImplementation,
4133
4126
  effect: effectActionImplementation,
4134
4127
  "move.block": moveBlockActionImplementation,
4135
4128
  noop: noopActionImplementation,
4136
- select: selectActionImplementation
4129
+ select: selectActionImplementation,
4130
+ "split.block": splitBlockActionImplementation
4137
4131
  };
4138
4132
  function performAction({
4139
4133
  context,
@@ -4266,20 +4260,6 @@ function performAction({
4266
4260
  });
4267
4261
  break;
4268
4262
  }
4269
- case "insert.break": {
4270
- behaviorActionImplementations["insert.break"]({
4271
- context,
4272
- action
4273
- });
4274
- break;
4275
- }
4276
- case "insert.soft break": {
4277
- behaviorActionImplementations["insert.soft break"]({
4278
- context,
4279
- action
4280
- });
4281
- break;
4282
- }
4283
4263
  case "insert.span": {
4284
4264
  behaviorActionImplementations["insert.span"]({
4285
4265
  context,
@@ -4303,6 +4283,13 @@ function performAction({
4303
4283
  }
4304
4284
  case "noop":
4305
4285
  break;
4286
+ case "split.block": {
4287
+ behaviorActionImplementations["split.block"]({
4288
+ context,
4289
+ action
4290
+ });
4291
+ break;
4292
+ }
4306
4293
  default: {
4307
4294
  behaviorActionImplementations.select({
4308
4295
  context,
@@ -4320,7 +4307,7 @@ function createWithEventListeners(editorActor) {
4320
4307
  deleteBackward,
4321
4308
  deleteForward,
4322
4309
  insertBreak,
4323
- insertText: insertText2,
4310
+ insertText,
4324
4311
  select
4325
4312
  } = editor;
4326
4313
  return editor.deleteBackward = (unit) => {
@@ -4376,12 +4363,14 @@ function createWithEventListeners(editorActor) {
4376
4363
  });
4377
4364
  }, editor.insertSoftBreak = () => {
4378
4365
  if (isApplyingBehaviorActions(editor)) {
4379
- insertSoftBreakActionImplementation({
4366
+ insertTextActionImplementation({
4380
4367
  context: {
4381
4368
  keyGenerator: editorActor.getSnapshot().context.keyGenerator,
4382
4369
  schema: editorActor.getSnapshot().context.schema
4383
4370
  },
4384
4371
  action: {
4372
+ text: `
4373
+ `,
4385
4374
  editor
4386
4375
  }
4387
4376
  });
@@ -4396,7 +4385,7 @@ function createWithEventListeners(editorActor) {
4396
4385
  });
4397
4386
  }, editor.insertText = (text, options) => {
4398
4387
  if (isApplyingBehaviorActions(editor)) {
4399
- insertText2(text, options);
4388
+ insertText(text, options);
4400
4389
  return;
4401
4390
  }
4402
4391
  editorActor.send({
@@ -4407,7 +4396,7 @@ function createWithEventListeners(editorActor) {
4407
4396
  },
4408
4397
  editor,
4409
4398
  defaultActionCallback: () => {
4410
- insertText2(text, options);
4399
+ insertText(text, options);
4411
4400
  }
4412
4401
  });
4413
4402
  }, editor.redo = () => {
@@ -5335,7 +5324,7 @@ const abstractAnnotationBehaviors = [defineBehavior({
5335
5324
  placement: "auto",
5336
5325
  select: "end"
5337
5326
  })] : event.blocks.flatMap((block, index) => index === 0 ? [raise({
5338
- type: "insert.break"
5327
+ type: "split.block"
5339
5328
  }), raise({
5340
5329
  type: "select.previous block",
5341
5330
  select: "end"
@@ -5371,6 +5360,18 @@ const abstractAnnotationBehaviors = [defineBehavior({
5371
5360
  placement: index === 0 ? "auto" : "after",
5372
5361
  select: "end"
5373
5362
  }))]
5363
+ }), defineBehavior({
5364
+ on: "insert.break",
5365
+ actions: [() => [raise({
5366
+ type: "split.block"
5367
+ })]]
5368
+ }), defineBehavior({
5369
+ on: "insert.soft break",
5370
+ actions: [() => [raise({
5371
+ type: "insert.text",
5372
+ text: `
5373
+ `
5374
+ })]]
5374
5375
  })], abstractListItemBehaviors = [defineBehavior({
5375
5376
  on: "list item.add",
5376
5377
  guard: ({
@@ -5955,7 +5956,7 @@ const abstractAnnotationBehaviors = [defineBehavior({
5955
5956
  raiseDeserializationSuccessOrFailure,
5956
5957
  raiseSerializationSuccessOrFailure,
5957
5958
  raiseInsertSoftBreak
5958
- ], abstractBehaviorEventTypes = ["annotation.toggle", "decorator.toggle", "deserialize", "deserialization.success", "deserialization.failure", "insert.blocks", "list item.add", "list item.remove", "list item.toggle", "move.block down", "move.block up", "select.previous block", "select.next block", "serialize", "serialization.success", "serialization.failure", "style.add", "style.remove", "style.toggle"];
5959
+ ], abstractBehaviorEventTypes = ["annotation.toggle", "decorator.toggle", "deserialize", "deserialization.success", "deserialization.failure", "insert.blocks", "insert.break", "insert.soft break", "list item.add", "list item.remove", "list item.toggle", "move.block down", "move.block up", "select.previous block", "select.next block", "serialize", "serialization.success", "serialization.failure", "style.add", "style.remove", "style.toggle"];
5959
5960
  function isAbstractBehaviorEvent(event) {
5960
5961
  return abstractBehaviorEventTypes.includes(event.type);
5961
5962
  }