@portabletext/editor 1.44.1 → 1.44.3

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 (60) hide show
  1. package/lib/_chunks-cjs/behavior.core.cjs +3 -3
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
  3. package/lib/_chunks-cjs/editor-provider.cjs +315 -368
  4. package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
  5. package/lib/_chunks-es/behavior.core.js +3 -3
  6. package/lib/_chunks-es/behavior.core.js.map +1 -1
  7. package/lib/_chunks-es/editor-provider.js +317 -370
  8. package/lib/_chunks-es/editor-provider.js.map +1 -1
  9. package/lib/behaviors/index.cjs +2 -2
  10. package/lib/behaviors/index.cjs.map +1 -1
  11. package/lib/behaviors/index.d.cts +7951 -15846
  12. package/lib/behaviors/index.d.ts +7951 -15846
  13. package/lib/behaviors/index.js +4 -4
  14. package/lib/behaviors/index.js.map +1 -1
  15. package/lib/index.cjs +19 -7
  16. package/lib/index.cjs.map +1 -1
  17. package/lib/index.d.cts +1742 -15478
  18. package/lib/index.d.ts +1742 -15478
  19. package/lib/index.js +20 -8
  20. package/lib/index.js.map +1 -1
  21. package/lib/plugins/index.d.cts +1742 -15479
  22. package/lib/plugins/index.d.ts +1742 -15479
  23. package/lib/selectors/index.d.cts +1739 -15475
  24. package/lib/selectors/index.d.ts +1739 -15475
  25. package/lib/utils/index.d.cts +1739 -15475
  26. package/lib/utils/index.d.ts +1739 -15475
  27. package/package.json +9 -9
  28. package/src/behavior-actions/behavior.action.decorator.add.ts +7 -2
  29. package/src/behavior-actions/behavior.actions.ts +0 -18
  30. package/src/behaviors/{behavior.internal.annotation.ts → behavior.abstract.annotation.ts} +1 -1
  31. package/src/behaviors/{behavior.internal.decorator.ts → behavior.abstract.decorator.ts} +1 -1
  32. package/src/behaviors/{behavior.internal.insert.ts → behavior.abstract.insert.ts} +1 -1
  33. package/src/behaviors/{behavior.internal.list-item.ts → behavior.abstract.list-item.ts} +1 -1
  34. package/src/behaviors/behavior.abstract.move.ts +78 -0
  35. package/src/behaviors/{behavior.internal.select.ts → behavior.abstract.select.ts} +1 -1
  36. package/src/behaviors/{behavior.internal.style.ts → behavior.abstract.style.ts} +1 -1
  37. package/src/behaviors/behavior.code-editor.ts +13 -8
  38. package/src/behaviors/behavior.default.ts +14 -12
  39. package/src/behaviors/behavior.perform-event.ts +4 -4
  40. package/src/behaviors/behavior.types.action.ts +3 -3
  41. package/src/behaviors/behavior.types.behavior.ts +4 -3
  42. package/src/behaviors/behavior.types.event.ts +153 -153
  43. package/src/editor/Editable.tsx +17 -17
  44. package/src/editor/components/Leaf.tsx +2 -2
  45. package/src/editor/create-editor.ts +37 -11
  46. package/src/editor/editor-machine.ts +4 -76
  47. package/src/editor/editor-selector.ts +5 -2
  48. package/src/editor/editor-snapshot.ts +10 -9
  49. package/src/editor/plugins/create-with-event-listeners.ts +6 -75
  50. package/src/editor/plugins/createWithEditableAPI.ts +32 -44
  51. package/src/editor/plugins/createWithPortableTextSelections.ts +8 -13
  52. package/src/editor/plugins/with-plugins.ts +3 -8
  53. package/src/internal-utils/event-position.ts +10 -7
  54. package/src/internal-utils/paths.ts +1 -37
  55. package/src/internal-utils/ranges.ts +3 -37
  56. package/src/internal-utils/slate-children-to-blocks.ts +49 -0
  57. package/src/internal-utils/slate-utils.ts +108 -9
  58. package/src/type-utils.ts +2 -0
  59. package/src/behavior-actions/behavior.action.move.block-down.ts +0 -48
  60. package/src/behavior-actions/behavior.action.move.block-up.ts +0 -53
@@ -124,87 +124,6 @@ function debugWithName(name) {
124
124
  const namespace = `${rootName}${name}`;
125
125
  return debug__default.default && debug__default.default.enabled(namespace) ? debug__default.default(namespace) : debug__default.default(rootName);
126
126
  }
127
- function createKeyedPath(point, value, types2) {
128
- const blockPath = [point.path[0]];
129
- if (!value)
130
- return null;
131
- const block = value[blockPath[0]];
132
- if (!block)
133
- return null;
134
- const keyedBlockPath = [{
135
- _key: block._key
136
- }];
137
- if (block._type !== types2.block.name)
138
- return keyedBlockPath;
139
- let keyedChildPath;
140
- const childPath = point.path.slice(0, 2), child = Array.isArray(block.children) && block.children[childPath[1]];
141
- return child && (keyedChildPath = ["children", {
142
- _key: child._key
143
- }]), keyedChildPath ? [...keyedBlockPath, ...keyedChildPath] : keyedBlockPath;
144
- }
145
- function toSlatePath(path, editor) {
146
- if (!editor)
147
- return [];
148
- const [block, blockPath] = Array.from(slate.Editor.nodes(editor, {
149
- at: [],
150
- match: (n) => types.isKeySegment(path[0]) && n._key === path[0]._key
151
- }))[0] || [void 0, void 0];
152
- if (!block || !slate.Element.isElement(block))
153
- return [];
154
- if (editor.isVoid(block))
155
- return [blockPath[0], 0];
156
- const childPath = [path[2]], childIndex = block.children.findIndex((child) => isEqual__default.default([{
157
- _key: child._key
158
- }], childPath));
159
- if (childIndex >= 0 && block.children[childIndex]) {
160
- const child = block.children[childIndex];
161
- return slate.Element.isElement(child) && editor.isVoid(child) ? blockPath.concat(childIndex).concat(0) : blockPath.concat(childIndex);
162
- }
163
- return [blockPath[0], 0];
164
- }
165
- function toPortableTextRange(value, range, types2) {
166
- if (!range)
167
- return null;
168
- let anchor = null, focus = null;
169
- const anchorPath = range.anchor && createKeyedPath(range.anchor, value, types2);
170
- anchorPath && range.anchor && (anchor = {
171
- path: anchorPath,
172
- offset: range.anchor.offset
173
- });
174
- const focusPath = range.focus && createKeyedPath(range.focus, value, types2);
175
- focusPath && range.focus && (focus = {
176
- path: focusPath,
177
- offset: range.focus.offset
178
- });
179
- const backward = !!(slate.Range.isRange(range) && slate.Range.isBackward(range));
180
- return anchor && focus ? {
181
- anchor,
182
- focus,
183
- backward
184
- } : null;
185
- }
186
- function toSlateRange(selection, editor) {
187
- if (!selection || !editor)
188
- return null;
189
- const anchor = {
190
- path: toSlatePath(selection.anchor.path, editor),
191
- offset: selection.anchor.offset
192
- }, focus = {
193
- path: toSlatePath(selection.focus.path, editor),
194
- offset: selection.focus.offset
195
- };
196
- return focus.path.length === 0 || anchor.path.length === 0 ? null : anchor && focus ? {
197
- anchor,
198
- focus
199
- } : null;
200
- }
201
- function moveRangeByOperation(range, operation) {
202
- const anchor = slate.Point.transform(range.anchor, operation), focus = slate.Point.transform(range.focus, operation);
203
- return anchor === null || focus === null ? null : slate.Point.equals(anchor, range.anchor) && slate.Point.equals(focus, range.focus) ? range : {
204
- anchor,
205
- focus
206
- };
207
- }
208
127
  const VOID_CHILD_KEY = "void-child";
209
128
  function keepObjectEquality(object, keyMap) {
210
129
  const value = keyMap[object._key];
@@ -315,10 +234,14 @@ function isEqualToEmptyEditor(children, schemaTypes) {
315
234
  function getFocusBlock({
316
235
  editor
317
236
  }) {
318
- return editor.selection ? Array.from(slate.Editor.nodes(editor, {
319
- at: editor.selection.focus.path.slice(0, 1),
320
- match: (n) => !slate.Editor.isEditor(n)
321
- })).at(0) ?? [void 0, void 0] : [void 0, void 0];
237
+ return editor.selection ? slate.Editor.node(editor, editor.selection.focus.path.slice(0, 1)) ?? [void 0, void 0] : [void 0, void 0];
238
+ }
239
+ function getPointBlock({
240
+ editor,
241
+ point
242
+ }) {
243
+ const [block] = slate.Editor.node(editor, point.path.slice(0, 1)) ?? [void 0, void 0];
244
+ return block ? [block, point.path] : [void 0, void 0];
322
245
  }
323
246
  function getFocusChild({
324
247
  editor
@@ -331,6 +254,23 @@ function getFocusChild({
331
254
  const focusChild = slate.Node.child(focusBlock, childIndex);
332
255
  return focusChild ? [focusChild, [...focusBlockPath, childIndex]] : [void 0, void 0];
333
256
  }
257
+ function getPointChild({
258
+ editor,
259
+ point
260
+ }) {
261
+ const [block, blockPath] = getPointBlock({
262
+ editor,
263
+ point
264
+ }), childIndex = point.path.at(1);
265
+ if (!block || !blockPath || childIndex === void 0)
266
+ return [void 0, void 0];
267
+ try {
268
+ const pointChild = slate.Node.child(block, childIndex);
269
+ return pointChild ? [pointChild, [...blockPath, childIndex]] : [void 0, void 0];
270
+ } catch {
271
+ return [void 0, void 0];
272
+ }
273
+ }
334
274
  function getFirstBlock({
335
275
  editor
336
276
  }) {
@@ -407,6 +347,89 @@ function isStyleActive({
407
347
  })];
408
348
  return selectedBlocks.length > 0 ? selectedBlocks.every(([node]) => node.style === style) : !1;
409
349
  }
350
+ function slateRangeToSelection({
351
+ schema: schema2,
352
+ editor,
353
+ range
354
+ }) {
355
+ const [anchorBlock] = getPointBlock({
356
+ editor,
357
+ point: range.anchor
358
+ }), [focusBlock] = getPointBlock({
359
+ editor,
360
+ point: range.focus
361
+ });
362
+ if (!anchorBlock || !focusBlock)
363
+ return null;
364
+ const [anchorChild] = anchorBlock._type === schema2.block.name ? getPointChild({
365
+ editor,
366
+ point: range.anchor
367
+ }) : [void 0, void 0], [focusChild] = focusBlock._type === schema2.block.name ? getPointChild({
368
+ editor,
369
+ point: range.focus
370
+ }) : [void 0, void 0], selection = {
371
+ anchor: {
372
+ path: [{
373
+ _key: anchorBlock._key
374
+ }],
375
+ offset: range.anchor.offset
376
+ },
377
+ focus: {
378
+ path: [{
379
+ _key: focusBlock._key
380
+ }],
381
+ offset: range.focus.offset
382
+ },
383
+ backward: slate.Range.isBackward(range)
384
+ };
385
+ return anchorChild && (selection.anchor.path.push("children"), selection.anchor.path.push({
386
+ _key: anchorChild._key
387
+ })), focusChild && (selection.focus.path.push("children"), selection.focus.path.push({
388
+ _key: focusChild._key
389
+ })), selection;
390
+ }
391
+ function toSlatePath(path, editor) {
392
+ if (!editor)
393
+ return [];
394
+ const [block, blockPath] = Array.from(slate.Editor.nodes(editor, {
395
+ at: [],
396
+ match: (n) => types.isKeySegment(path[0]) && n._key === path[0]._key
397
+ }))[0] || [void 0, void 0];
398
+ if (!block || !slate.Element.isElement(block))
399
+ return [];
400
+ if (editor.isVoid(block))
401
+ return [blockPath[0], 0];
402
+ const childPath = [path[2]], childIndex = block.children.findIndex((child) => isEqual__default.default([{
403
+ _key: child._key
404
+ }], childPath));
405
+ if (childIndex >= 0 && block.children[childIndex]) {
406
+ const child = block.children[childIndex];
407
+ return slate.Element.isElement(child) && editor.isVoid(child) ? blockPath.concat(childIndex).concat(0) : blockPath.concat(childIndex);
408
+ }
409
+ return [blockPath[0], 0];
410
+ }
411
+ function toSlateRange(selection, editor) {
412
+ if (!selection || !editor)
413
+ return null;
414
+ const anchor = {
415
+ path: toSlatePath(selection.anchor.path, editor),
416
+ offset: selection.anchor.offset
417
+ }, focus = {
418
+ path: toSlatePath(selection.focus.path, editor),
419
+ offset: selection.focus.offset
420
+ };
421
+ return focus.path.length === 0 || anchor.path.length === 0 ? null : anchor && focus ? {
422
+ anchor,
423
+ focus
424
+ } : null;
425
+ }
426
+ function moveRangeByOperation(range, operation) {
427
+ const anchor = slate.Point.transform(range.anchor, operation), focus = slate.Point.transform(range.focus, operation);
428
+ return anchor === null || focus === null ? null : slate.Point.equals(anchor, range.anchor) && slate.Point.equals(focus, range.focus) ? range : {
429
+ anchor,
430
+ focus
431
+ };
432
+ }
410
433
  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 = React.createContext({}), PortableTextEditorContext = React.createContext(null), usePortableTextEditor = () => {
411
434
  const editor = React.useContext(PortableTextEditorContext);
412
435
  if (!editor)
@@ -3549,7 +3572,11 @@ function createEditableAPI(editor, editorActor) {
3549
3572
  }
3550
3573
  },
3551
3574
  editor
3552
- }), toPortableTextRange(fromSlateValue(editor.children, types2.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types2)?.focus.path ?? [];
3575
+ }), editor.selection ? slateRangeToSelection({
3576
+ schema: editorActor.getSnapshot().context.schema,
3577
+ editor,
3578
+ range: editor.selection
3579
+ })?.focus.path ?? [] : [];
3553
3580
  if (!editor.selection)
3554
3581
  throw new Error("The editor has no selection");
3555
3582
  const [focusBlock] = Array.from(slate.Editor.nodes(editor, {
@@ -3577,7 +3604,11 @@ function createEditableAPI(editor, editorActor) {
3577
3604
  })), slate.Transforms.insertNodes(editor, child, {
3578
3605
  select: !0,
3579
3606
  at: editor.selection
3580
- }), editor.onChange(), toPortableTextRange(fromSlateValue(editor.children, types2.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types2)?.focus.path || [];
3607
+ }), editor.onChange(), editor.selection ? slateRangeToSelection({
3608
+ schema: editorActor.getSnapshot().context.schema,
3609
+ editor,
3610
+ range: editor.selection
3611
+ })?.focus.path ?? [] : [];
3581
3612
  },
3582
3613
  insertBlock: (type, value) => (editorActor.send({
3583
3614
  type: "behavior event",
@@ -3590,7 +3621,11 @@ function createEditableAPI(editor, editorActor) {
3590
3621
  placement: "auto"
3591
3622
  },
3592
3623
  editor
3593
- }), toPortableTextRange(fromSlateValue(editor.children, types2.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types2)?.focus.path ?? []),
3624
+ }), editor.selection ? slateRangeToSelection({
3625
+ schema: editorActor.getSnapshot().context.schema,
3626
+ editor,
3627
+ range: editor.selection
3628
+ })?.focus.path ?? [] : []),
3594
3629
  hasBlockStyle: (style) => {
3595
3630
  try {
3596
3631
  return isStyleActive({
@@ -3748,7 +3783,11 @@ function createEditableAPI(editor, editorActor) {
3748
3783
  const existing = SLATE_TO_PORTABLE_TEXT_RANGE.get(editor.selection);
3749
3784
  if (existing)
3750
3785
  return existing;
3751
- ptRange = toPortableTextRange(fromSlateValue(editor.children, types2.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types2), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange);
3786
+ ptRange = slateRangeToSelection({
3787
+ schema: editorActor.getSnapshot().context.schema,
3788
+ editor,
3789
+ range: editor.selection
3790
+ }), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange);
3752
3791
  }
3753
3792
  return ptRange;
3754
3793
  },
@@ -4039,7 +4078,11 @@ const addAnnotationActionImplementation = ({
4039
4078
  } : void 0, selection = manualSelection ? toSlateRange(manualSelection, action.editor) ?? editor.selection : editor.selection;
4040
4079
  if (!selection)
4041
4080
  return;
4042
- const editorSelection = toPortableTextRange(value, selection, context.schema), anchorOffset = editorSelection ? util_selectionPointToBlockOffset.selectionPointToBlockOffset({
4081
+ const editorSelection = slateRangeToSelection({
4082
+ schema: context.schema,
4083
+ editor,
4084
+ range: selection
4085
+ }), anchorOffset = editorSelection ? util_selectionPointToBlockOffset.selectionPointToBlockOffset({
4043
4086
  value,
4044
4087
  selectionPoint: editorSelection.anchor
4045
4088
  }) : void 0, focusOffset = editorSelection ? util_selectionPointToBlockOffset.selectionPointToBlockOffset({
@@ -4426,63 +4469,6 @@ const insertTextActionImplementation = ({
4426
4469
  to,
4427
4470
  mode: "highest"
4428
4471
  });
4429
- }, moveBlockDownActionImplementation = ({
4430
- context,
4431
- action
4432
- }) => {
4433
- const at = [toSlatePath(action.at, action.editor)[0]], to = [slate.Path.next(at)[0]], selection = toPortableTextRange(action.editor.children, {
4434
- anchor: {
4435
- path: to,
4436
- offset: 0
4437
- },
4438
- focus: {
4439
- path: to,
4440
- offset: 0
4441
- }
4442
- }, context.schema), destinationBlockKey = selection && util_sliceBlocks.isKeyedSegment(selection.focus.path[0]) ? selection.focus.path[0]._key : void 0;
4443
- if (destinationBlockKey === void 0) {
4444
- console.error("Could not find destination block key");
4445
- return;
4446
- }
4447
- moveBlockActionImplementation({
4448
- action: {
4449
- at: action.at,
4450
- to: [{
4451
- _key: destinationBlockKey
4452
- }],
4453
- editor: action.editor
4454
- }
4455
- });
4456
- }, moveBlockUpActionImplementation = ({
4457
- context,
4458
- action
4459
- }) => {
4460
- const at = [toSlatePath(action.at, action.editor)[0]];
4461
- if (!slate.Path.hasPrevious(at))
4462
- return;
4463
- const to = [slate.Path.previous(at)[0]], selection = toPortableTextRange(action.editor.children, {
4464
- anchor: {
4465
- path: to,
4466
- offset: 0
4467
- },
4468
- focus: {
4469
- path: to,
4470
- offset: 0
4471
- }
4472
- }, context.schema), destinationBlockKey = selection && util_sliceBlocks.isKeyedSegment(selection.focus.path[0]) ? selection.focus.path[0]._key : void 0;
4473
- if (destinationBlockKey === void 0) {
4474
- console.error("Could not find destination block key");
4475
- return;
4476
- }
4477
- moveBlockActionImplementation({
4478
- action: {
4479
- at: action.at,
4480
- to: [{
4481
- _key: destinationBlockKey
4482
- }],
4483
- editor: action.editor
4484
- }
4485
- });
4486
4472
  }, noopActionImplementation = () => {
4487
4473
  }, selectActionImplementation = ({
4488
4474
  action
@@ -4513,8 +4499,6 @@ const insertTextActionImplementation = ({
4513
4499
  "insert.text": insertTextActionImplementation,
4514
4500
  effect: effectActionImplementation,
4515
4501
  "move.block": moveBlockActionImplementation,
4516
- "move.block down": moveBlockDownActionImplementation,
4517
- "move.block up": moveBlockUpActionImplementation,
4518
4502
  noop: noopActionImplementation,
4519
4503
  select: selectActionImplementation
4520
4504
  };
@@ -4684,20 +4668,6 @@ function performAction({
4684
4668
  });
4685
4669
  break;
4686
4670
  }
4687
- case "move.block down": {
4688
- behaviorActionImplementations["move.block down"]({
4689
- context,
4690
- action
4691
- });
4692
- break;
4693
- }
4694
- case "move.block up": {
4695
- behaviorActionImplementations["move.block up"]({
4696
- context,
4697
- action
4698
- });
4699
- break;
4700
- }
4701
4671
  case "noop":
4702
4672
  break;
4703
4673
  default: {
@@ -4709,65 +4679,10 @@ function performAction({
4709
4679
  }
4710
4680
  }
4711
4681
  }
4712
- function createWithEventListeners(editorActor, subscriptions) {
4682
+ function createWithEventListeners(editorActor) {
4713
4683
  return function(editor) {
4714
4684
  if (editorActor.getSnapshot().context.maxBlocks !== void 0)
4715
4685
  return editor;
4716
- subscriptions.push(() => {
4717
- const subscription = editorActor.on("*", (event) => {
4718
- switch (event.type) {
4719
- // These events are not relevant for Behaviors
4720
- case "blurred":
4721
- case "done loading":
4722
- case "editable":
4723
- case "error":
4724
- case "focused":
4725
- case "invalid value":
4726
- case "loading":
4727
- case "mutation":
4728
- case "patch":
4729
- case "internal.patch":
4730
- case "patches":
4731
- case "read only":
4732
- case "ready":
4733
- case "selection":
4734
- case "value changed":
4735
- case "unset":
4736
- break;
4737
- case "custom.*":
4738
- editorActor.send({
4739
- type: "custom behavior event",
4740
- behaviorEvent: event.event,
4741
- editor
4742
- });
4743
- break;
4744
- case "insert.block object":
4745
- editorActor.send({
4746
- type: "behavior event",
4747
- behaviorEvent: {
4748
- type: "insert.block",
4749
- block: {
4750
- _type: event.blockObject.name,
4751
- ...event.blockObject.value ?? {}
4752
- },
4753
- placement: event.placement
4754
- },
4755
- editor
4756
- });
4757
- break;
4758
- default:
4759
- editorActor.send({
4760
- type: "behavior event",
4761
- behaviorEvent: event,
4762
- editor
4763
- });
4764
- break;
4765
- }
4766
- });
4767
- return () => {
4768
- subscription.unsubscribe();
4769
- };
4770
- });
4771
4686
  const {
4772
4687
  deleteBackward,
4773
4688
  deleteForward,
@@ -4893,7 +4808,11 @@ function createWithEventListeners(editorActor, subscriptions) {
4893
4808
  type: "behavior event",
4894
4809
  behaviorEvent: {
4895
4810
  type: "select",
4896
- selection: toPortableTextRange(fromSlateValue(editor.children, editorActor.getSnapshot().context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), range, editorActor.getSnapshot().context.schema)
4811
+ selection: slateRangeToSelection({
4812
+ schema: editorActor.getSnapshot().context.schema,
4813
+ editor,
4814
+ range
4815
+ })
4897
4816
  },
4898
4817
  editor,
4899
4818
  defaultActionCallback: () => {
@@ -5450,7 +5369,7 @@ function createWithPortableTextBlockStyle(editorActor, types2) {
5450
5369
  };
5451
5370
  }
5452
5371
  debugWithName("plugin:withPortableTextSelections");
5453
- function createWithPortableTextSelections(editorActor, types2) {
5372
+ function createWithPortableTextSelections(editorActor) {
5454
5373
  let prevSelection = null;
5455
5374
  return function(editor) {
5456
5375
  const emitPortableTextSelection = () => {
@@ -5458,12 +5377,11 @@ function createWithPortableTextSelections(editorActor, types2) {
5458
5377
  let ptRange = null;
5459
5378
  if (editor.selection) {
5460
5379
  const existing = SLATE_TO_PORTABLE_TEXT_RANGE.get(editor.selection);
5461
- if (existing)
5462
- ptRange = existing;
5463
- else {
5464
- const value = editor.children;
5465
- ptRange = toPortableTextRange(value, editor.selection, types2), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange);
5466
- }
5380
+ existing ? ptRange = existing : (ptRange = slateRangeToSelection({
5381
+ schema: editorActor.getSnapshot().context.schema,
5382
+ editor,
5383
+ range: editor.selection
5384
+ }), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange));
5467
5385
  }
5468
5386
  ptRange ? editorActor.send({
5469
5387
  type: "notify.selection",
@@ -5577,8 +5495,8 @@ const withPlugins = (editor, options) => {
5577
5495
  }), withPortableTextMarkModel = createWithPortableTextMarkModel(editorActor, schemaTypes), withPortableTextBlockStyle = createWithPortableTextBlockStyle(editorActor, schemaTypes), withPlaceholderBlock = createWithPlaceholderBlock(editorActor), withUtils = createWithUtils({
5578
5496
  editorActor,
5579
5497
  schemaTypes
5580
- }), withPortableTextSelections = createWithPortableTextSelections(editorActor, schemaTypes);
5581
- return createWithEventListeners(editorActor, options.subscriptions)(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withPortableTextBlockStyle(withPlaceholderBlock(withUtils(withMaxBlocks(withUndoRedo(withPatches(withPortableTextSelections(e)))))))))));
5498
+ }), withPortableTextSelections = createWithPortableTextSelections(editorActor);
5499
+ return createWithEventListeners(editorActor)(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withPortableTextBlockStyle(withPlaceholderBlock(withUtils(withMaxBlocks(withUndoRedo(withPatches(withPortableTextSelections(e)))))))))));
5582
5500
  }, debug$3 = debugWithName("component:PortableTextEditor:SlateContainer"), slateEditors = /* @__PURE__ */ new WeakMap();
5583
5501
  function createSlateEditor(config) {
5584
5502
  const existingSlateEditor = slateEditors.get(config.editorActor);
@@ -5600,17 +5518,7 @@ function createSlateEditor(config) {
5600
5518
  };
5601
5519
  return slateEditors.set(config.editorActor, slateEditor), slateEditor;
5602
5520
  }
5603
- const keyIs = {
5604
- lineBreak: (event) => event.key === "Enter" && event.shiftKey
5605
- }, raiseInsertSoftBreak = behavior_core.defineBehavior({
5606
- on: "keyboard.keydown",
5607
- guard: ({
5608
- event
5609
- }) => keyIs.lineBreak(event.originEvent),
5610
- actions: [() => [behavior_core.raise({
5611
- type: "insert.soft break"
5612
- })]]
5613
- }), internalAnnotationBehaviors = [behavior_core.defineBehavior({
5521
+ const abstractAnnotationBehaviors = [behavior_core.defineBehavior({
5614
5522
  on: "annotation.toggle",
5615
5523
  guard: ({
5616
5524
  snapshot,
@@ -5634,7 +5542,7 @@ const keyIs = {
5634
5542
  type: "annotation.add",
5635
5543
  annotation: event.annotation
5636
5544
  })]]
5637
- })], internalDecoratorBehaviors = [behavior_core.defineBehavior({
5545
+ })], abstractDecoratorBehaviors = [behavior_core.defineBehavior({
5638
5546
  on: "decorator.toggle",
5639
5547
  guard: ({
5640
5548
  snapshot,
@@ -5670,7 +5578,7 @@ const keyIs = {
5670
5578
  ...event,
5671
5579
  type: "decorator.add"
5672
5580
  })]]
5673
- })], internalInsertBehaviors = [behavior_core.defineBehavior({
5581
+ })], abstractInsertBehaviors = [behavior_core.defineBehavior({
5674
5582
  on: "insert.blocks",
5675
5583
  guard: ({
5676
5584
  event
@@ -5746,7 +5654,7 @@ const keyIs = {
5746
5654
  placement: index === 0 ? "auto" : "after",
5747
5655
  select: "end"
5748
5656
  }))]
5749
- })], internalListItemBehaviors = [behavior_core.defineBehavior({
5657
+ })], abstractListItemBehaviors = [behavior_core.defineBehavior({
5750
5658
  on: "list item.add",
5751
5659
  guard: ({
5752
5660
  snapshot
@@ -5803,7 +5711,75 @@ const keyIs = {
5803
5711
  type: "list item.add",
5804
5712
  listItem: event.listItem
5805
5713
  })]]
5806
- })], internalSelectBehaviors = [behavior_core.defineBehavior({
5714
+ })], abstractMoveBehaviors = [behavior_core.defineBehavior({
5715
+ on: "move.block up",
5716
+ guard: ({
5717
+ snapshot,
5718
+ event
5719
+ }) => {
5720
+ const previousBlock = selector_isOverlappingSelection.getPreviousBlock({
5721
+ context: {
5722
+ ...snapshot.context,
5723
+ selection: {
5724
+ anchor: {
5725
+ path: event.at,
5726
+ offset: 0
5727
+ },
5728
+ focus: {
5729
+ path: event.at,
5730
+ offset: 0
5731
+ }
5732
+ }
5733
+ }
5734
+ });
5735
+ return previousBlock ? {
5736
+ previousBlock
5737
+ } : !1;
5738
+ },
5739
+ actions: [({
5740
+ event
5741
+ }, {
5742
+ previousBlock
5743
+ }) => [behavior_core.raise({
5744
+ type: "move.block",
5745
+ at: event.at,
5746
+ to: previousBlock.path
5747
+ })]]
5748
+ }), behavior_core.defineBehavior({
5749
+ on: "move.block down",
5750
+ guard: ({
5751
+ snapshot,
5752
+ event
5753
+ }) => {
5754
+ const nextBlock = selector_isOverlappingSelection.getNextBlock({
5755
+ context: {
5756
+ ...snapshot.context,
5757
+ selection: {
5758
+ anchor: {
5759
+ path: event.at,
5760
+ offset: 0
5761
+ },
5762
+ focus: {
5763
+ path: event.at,
5764
+ offset: 0
5765
+ }
5766
+ }
5767
+ }
5768
+ });
5769
+ return nextBlock ? {
5770
+ nextBlock
5771
+ } : !1;
5772
+ },
5773
+ actions: [({
5774
+ event
5775
+ }, {
5776
+ nextBlock
5777
+ }) => [behavior_core.raise({
5778
+ type: "move.block",
5779
+ at: event.at,
5780
+ to: nextBlock.path
5781
+ })]]
5782
+ })], abstractSelectBehaviors = [behavior_core.defineBehavior({
5807
5783
  on: "select.previous block",
5808
5784
  guard: ({
5809
5785
  snapshot,
@@ -5849,7 +5825,7 @@ const keyIs = {
5849
5825
  type: "select",
5850
5826
  selection
5851
5827
  })]]
5852
- })], internalStyleBehaviors = [behavior_core.defineBehavior({
5828
+ })], abstractStyleBehaviors = [behavior_core.defineBehavior({
5853
5829
  on: "style.add",
5854
5830
  guard: ({
5855
5831
  snapshot
@@ -5905,7 +5881,17 @@ const keyIs = {
5905
5881
  type: "style.add",
5906
5882
  style: event.style
5907
5883
  })]]
5908
- })], raiseDeserializationSuccessOrFailure = behavior_core.defineBehavior({
5884
+ })], keyIs = {
5885
+ lineBreak: (event) => event.key === "Enter" && event.shiftKey
5886
+ }, raiseInsertSoftBreak = behavior_core.defineBehavior({
5887
+ on: "keyboard.keydown",
5888
+ guard: ({
5889
+ event
5890
+ }) => keyIs.lineBreak(event.originEvent),
5891
+ actions: [() => [behavior_core.raise({
5892
+ type: "insert.soft break"
5893
+ })]]
5894
+ }), raiseDeserializationSuccessOrFailure = behavior_core.defineBehavior({
5909
5895
  on: "deserialize",
5910
5896
  guard: ({
5911
5897
  snapshot,
@@ -6233,43 +6219,30 @@ const keyIs = {
6233
6219
  originEvent: event
6234
6220
  })]]
6235
6221
  }),
6236
- ...internalAnnotationBehaviors,
6237
- ...internalDecoratorBehaviors,
6238
- ...internalInsertBehaviors,
6239
- ...internalListItemBehaviors,
6240
- ...internalStyleBehaviors,
6241
- ...internalSelectBehaviors,
6222
+ ...abstractAnnotationBehaviors,
6223
+ ...abstractDecoratorBehaviors,
6224
+ ...abstractInsertBehaviors,
6225
+ ...abstractListItemBehaviors,
6226
+ ...abstractMoveBehaviors,
6227
+ ...abstractStyleBehaviors,
6228
+ ...abstractSelectBehaviors,
6242
6229
  raiseDeserializationSuccessOrFailure,
6243
6230
  raiseSerializationSuccessOrFailure,
6244
6231
  raiseInsertSoftBreak
6245
- ];
6246
- function isKeyboardBehaviorEvent(event) {
6247
- return event.type.startsWith("keyboard.");
6248
- }
6249
- function isInternalBehaviorEvent(event) {
6250
- return event.type === "deserialize" || event.type.startsWith("deserialization.") || event.type === "insert.blocks" || event.type.startsWith("list item.") || event.type === "serialize" || event.type.startsWith("serialization.") || event.type === "select.next block" || event.type === "select.previous block" || event.type.startsWith("style.");
6232
+ ], 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"];
6233
+ function isAbstractBehaviorEvent(event) {
6234
+ return abstractBehaviorEventTypes.includes(event.type);
6251
6235
  }
6236
+ const nativeBehaviorEventTypes = ["clipboard.copy", "clipboard.cut", "clipboard.paste", "drag.dragstart", "drag.drag", "drag.dragend", "drag.dragenter", "drag.dragover", "drag.dragleave", "drag.drop", "input.*", "keyboard.keydown", "keyboard.keyup", "mouse.click"];
6252
6237
  function isNativeBehaviorEvent(event) {
6253
- return isClipboardBehaviorEvent(event) || isDragBehaviorEvent(event) || isInputBehaviorEvent(event) || isKeyboardBehaviorEvent(event) || isMouseBehaviorEvent(event);
6254
- }
6255
- function isClipboardBehaviorEvent(event) {
6256
- return event.type.startsWith("clipboard.");
6257
- }
6258
- function isDragBehaviorEvent(event) {
6259
- return event.type.startsWith("drag.");
6260
- }
6261
- function isInputBehaviorEvent(event) {
6262
- return event.type.startsWith("input.");
6263
- }
6264
- function isMouseBehaviorEvent(event) {
6265
- return event.type.startsWith("mouse.");
6238
+ return nativeBehaviorEventTypes.includes(event.type);
6266
6239
  }
6267
6240
  function isCustomBehaviorEvent(event) {
6268
6241
  return event.type.startsWith("custom.");
6269
6242
  }
6270
6243
  const debug$2 = debugWithName("behaviors:event");
6271
6244
  function eventCategory(event) {
6272
- return isNativeBehaviorEvent(event) ? "native" : isInternalBehaviorEvent(event) ? "internal" : isCustomBehaviorEvent(event) ? "custom" : "synthetic";
6245
+ return isNativeBehaviorEvent(event) ? "native" : isAbstractBehaviorEvent(event) ? "abstract" : isCustomBehaviorEvent(event) ? "custom" : "synthetic";
6273
6246
  }
6274
6247
  function performEvent({
6275
6248
  behaviors,
@@ -6282,7 +6255,7 @@ function performEvent({
6282
6255
  defaultActionCallback
6283
6256
  }) {
6284
6257
  debug$2(`(${eventCategory(event)})`, JSON.stringify(event, null, 2));
6285
- const defaultAction = isCustomBehaviorEvent(event) || isNativeBehaviorEvent(event) || isInternalBehaviorEvent(event) ? void 0 : {
6258
+ const defaultAction = isCustomBehaviorEvent(event) || isNativeBehaviorEvent(event) || isAbstractBehaviorEvent(event) ? void 0 : {
6286
6259
  ...event,
6287
6260
  editor
6288
6261
  }, eventBehaviors = behaviors.filter((behavior) => {
@@ -6401,6 +6374,34 @@ function performEvent({
6401
6374
  }), editor.onChange();
6402
6375
  }
6403
6376
  }
6377
+ function slateChildrenToBlocks(schema2, value) {
6378
+ const blocks = new Array(value.length);
6379
+ for (let blockIndex = 0; blockIndex < value.length; blockIndex++) {
6380
+ const descendant = value[blockIndex];
6381
+ if (descendant._type !== schema2.block.name) {
6382
+ blocks[blockIndex] = {
6383
+ _key: descendant._key,
6384
+ _type: descendant._type,
6385
+ ..."value" in descendant && typeof descendant.value == "object" ? descendant.value : {}
6386
+ };
6387
+ continue;
6388
+ }
6389
+ const children = "children" in descendant ? descendant.children : [], processedChildren = new Array(children.length);
6390
+ for (let childIndex = 0; childIndex < children.length; childIndex++) {
6391
+ const child = children[childIndex];
6392
+ processedChildren[childIndex] = child._type === schema2.span.name ? child : {
6393
+ _key: child._key,
6394
+ _type: child._type,
6395
+ ..."value" in child && typeof child.value == "object" ? child.value : {}
6396
+ };
6397
+ }
6398
+ blocks[blockIndex] = {
6399
+ ...descendant,
6400
+ children: processedChildren
6401
+ };
6402
+ }
6403
+ return blocks;
6404
+ }
6404
6405
  function getActiveDecorators({
6405
6406
  schema: schema2,
6406
6407
  slateEditorInstance
@@ -6419,7 +6420,11 @@ function createEditorSnapshot({
6419
6420
  hasTag,
6420
6421
  internalDrag
6421
6422
  }) {
6422
- const value = fromSlateValue(editor.children, schema2.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), selection = toPortableTextRange(value, editor.selection, schema2);
6423
+ const value = slateChildrenToBlocks(schema2, editor.children), selection = editor.selection ? slateRangeToSelection({
6424
+ schema: schema2,
6425
+ editor,
6426
+ range: editor.selection
6427
+ }) : null;
6423
6428
  return {
6424
6429
  context: {
6425
6430
  activeDecorators: getActiveDecorators({
@@ -6515,7 +6520,7 @@ const editorMachine = xstate.setup({
6515
6520
  event,
6516
6521
  self
6517
6522
  }) => {
6518
- xstate.assertEvent(event, ["behavior event", "custom behavior event"]), performEvent({
6523
+ xstate.assertEvent(event, ["behavior event"]), performEvent({
6519
6524
  behaviors: [...context.behaviors.values(), ...defaultBehaviors],
6520
6525
  event: event.behaviorEvent,
6521
6526
  editor: event.editor,
@@ -6714,87 +6719,6 @@ const editorMachine = xstate.setup({
6714
6719
  },
6715
6720
  "behavior event": {
6716
6721
  actions: "handle behavior event"
6717
- },
6718
- "custom behavior event": {
6719
- actions: "handle behavior event"
6720
- },
6721
- "annotation.*": {
6722
- actions: xstate.emit(({
6723
- event
6724
- }) => event)
6725
- },
6726
- "block.*": {
6727
- actions: xstate.emit(({
6728
- event
6729
- }) => event)
6730
- },
6731
- blur: {
6732
- actions: xstate.emit(({
6733
- event
6734
- }) => event)
6735
- },
6736
- "custom.*": {
6737
- actions: xstate.emit(({
6738
- event
6739
- }) => ({
6740
- type: "custom.*",
6741
- event
6742
- }))
6743
- },
6744
- "decorator.*": {
6745
- actions: xstate.emit(({
6746
- event
6747
- }) => event)
6748
- },
6749
- "delete.*": {
6750
- actions: xstate.emit(({
6751
- event
6752
- }) => event)
6753
- },
6754
- focus: {
6755
- actions: xstate.emit(({
6756
- event
6757
- }) => event)
6758
- },
6759
- "history.*": {
6760
- actions: xstate.emit(({
6761
- event
6762
- }) => event)
6763
- },
6764
- "insert.*": {
6765
- actions: xstate.emit(({
6766
- event
6767
- }) => event)
6768
- },
6769
- "list item.*": {
6770
- actions: xstate.emit(({
6771
- event
6772
- }) => event)
6773
- },
6774
- "move.*": {
6775
- actions: xstate.emit(({
6776
- event
6777
- }) => event)
6778
- },
6779
- select: {
6780
- actions: xstate.emit(({
6781
- event
6782
- }) => event)
6783
- },
6784
- "select.*": {
6785
- actions: xstate.emit(({
6786
- event
6787
- }) => event)
6788
- },
6789
- "style.*": {
6790
- actions: xstate.emit(({
6791
- event
6792
- }) => event)
6793
- },
6794
- "text block.*": {
6795
- actions: xstate.emit(({
6796
- event
6797
- }) => event)
6798
6722
  }
6799
6723
  },
6800
6724
  initial: "idle",
@@ -6906,12 +6830,6 @@ const editorMachine = xstate.setup({
6906
6830
  }
6907
6831
  }
6908
6832
  });
6909
- function getValue({
6910
- editorActorSnapshot,
6911
- slateEditorInstance
6912
- }) {
6913
- return fromSlateValue(slateEditorInstance.children, editorActorSnapshot.context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(slateEditorInstance));
6914
- }
6915
6833
  function defaultCompare(a, b) {
6916
6834
  return a === b;
6917
6835
  }
@@ -6943,10 +6861,7 @@ function getEditorSnapshot({
6943
6861
  }),
6944
6862
  schema: editorActorSnapshot.context.schema,
6945
6863
  selection: editorActorSnapshot.context.selection,
6946
- value: getValue({
6947
- editorActorSnapshot,
6948
- slateEditorInstance
6949
- })
6864
+ value: slateChildrenToBlocks(editorActorSnapshot.context.schema, slateEditorInstance.children)
6950
6865
  },
6951
6866
  beta: {
6952
6867
  hasTag: (tag) => editorActorSnapshot.hasTag(tag),
@@ -7002,7 +6917,39 @@ function createInternalEditorFromActor(editorActor) {
7002
6917
  });
7003
6918
  }),
7004
6919
  send: (event) => {
7005
- editorActor.send(event);
6920
+ switch (event.type) {
6921
+ case "add behavior":
6922
+ case "remove behavior":
6923
+ case "update behaviors":
6924
+ case "update key generator":
6925
+ case "update readOnly":
6926
+ case "patches":
6927
+ case "update value":
6928
+ case "update schema":
6929
+ case "update maxBlocks":
6930
+ editorActor.send(event);
6931
+ break;
6932
+ case "insert.block object":
6933
+ editorActor.send({
6934
+ type: "behavior event",
6935
+ behaviorEvent: {
6936
+ type: "insert.block",
6937
+ block: {
6938
+ _type: event.blockObject.name,
6939
+ ...event.blockObject.value ?? {}
6940
+ },
6941
+ placement: event.placement
6942
+ },
6943
+ editor: slateEditor.instance
6944
+ });
6945
+ break;
6946
+ default:
6947
+ editorActor.send({
6948
+ type: "behavior event",
6949
+ behaviorEvent: event,
6950
+ editor: slateEditor.instance
6951
+ });
6952
+ }
7006
6953
  },
7007
6954
  on: (event, listener) => editorActor.on(event, (event2) => {
7008
6955
  switch (event2.type) {
@@ -7611,7 +7558,7 @@ exports.getLastBlock = getLastBlock;
7611
7558
  exports.getNodeBlock = getNodeBlock;
7612
7559
  exports.isEqualToEmptyEditor = isEqualToEmptyEditor;
7613
7560
  exports.moveRangeByOperation = moveRangeByOperation;
7614
- exports.toPortableTextRange = toPortableTextRange;
7561
+ exports.slateRangeToSelection = slateRangeToSelection;
7615
7562
  exports.toSlateRange = toSlateRange;
7616
7563
  exports.useEditor = useEditor;
7617
7564
  exports.useEditorSelector = useEditorSelector;