@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
@@ -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 { Editor, Element, Point, Range, Text, Node, Path, Operation, Transforms, insertText, createEditor } from "slate";
8
+ import { Element, Text, Range, Editor, Node, Point, Path, Operation, Transforms, insertText, 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 { defineType, defineField, isKeySegment, isPortableTextTextBlock, isPortableTextSpan as isPortableTextSpan$1, isPortableTextListBlock } from "@sanity/types";
@@ -14,7 +14,7 @@ import isPlainObject from "lodash/isPlainObject.js";
14
14
  import uniq from "lodash/uniq.js";
15
15
  import getRandomValues from "get-random-values-esm";
16
16
  import { parseBlock, isTextBlock } from "./parse-blocks.js";
17
- import { sliceBlocks, blockOffsetToSpanSelectionPoint, isKeyedSegment as isKeyedSegment$1, getBlockEndPoint, getBlockStartPoint, getTextBlockText } from "./util.slice-blocks.js";
17
+ import { sliceBlocks, blockOffsetToSpanSelectionPoint, getBlockEndPoint, getBlockStartPoint, getTextBlockText } from "./util.slice-blocks.js";
18
18
  import { htmlToBlocks } from "@portabletext/block-tools";
19
19
  import { toHTML } from "@portabletext/to-html";
20
20
  import { Schema } from "@sanity/schema";
@@ -150,87 +150,6 @@ function debugWithName(name) {
150
150
  const namespace = `${rootName}${name}`;
151
151
  return debug$g && debug$g.enabled(namespace) ? debug$g(namespace) : debug$g(rootName);
152
152
  }
153
- function createKeyedPath(point, value, types) {
154
- const blockPath = [point.path[0]];
155
- if (!value)
156
- return null;
157
- const block = value[blockPath[0]];
158
- if (!block)
159
- return null;
160
- const keyedBlockPath = [{
161
- _key: block._key
162
- }];
163
- if (block._type !== types.block.name)
164
- return keyedBlockPath;
165
- let keyedChildPath;
166
- const childPath = point.path.slice(0, 2), child = Array.isArray(block.children) && block.children[childPath[1]];
167
- return child && (keyedChildPath = ["children", {
168
- _key: child._key
169
- }]), keyedChildPath ? [...keyedBlockPath, ...keyedChildPath] : keyedBlockPath;
170
- }
171
- function toSlatePath(path, editor) {
172
- if (!editor)
173
- return [];
174
- const [block, blockPath] = Array.from(Editor.nodes(editor, {
175
- at: [],
176
- match: (n) => isKeySegment(path[0]) && n._key === path[0]._key
177
- }))[0] || [void 0, void 0];
178
- if (!block || !Element.isElement(block))
179
- return [];
180
- if (editor.isVoid(block))
181
- return [blockPath[0], 0];
182
- const childPath = [path[2]], childIndex = block.children.findIndex((child) => isEqual([{
183
- _key: child._key
184
- }], childPath));
185
- if (childIndex >= 0 && block.children[childIndex]) {
186
- const child = block.children[childIndex];
187
- return Element.isElement(child) && editor.isVoid(child) ? blockPath.concat(childIndex).concat(0) : blockPath.concat(childIndex);
188
- }
189
- return [blockPath[0], 0];
190
- }
191
- function toPortableTextRange(value, range, types) {
192
- if (!range)
193
- return null;
194
- let anchor = null, focus = null;
195
- const anchorPath = range.anchor && createKeyedPath(range.anchor, value, types);
196
- anchorPath && range.anchor && (anchor = {
197
- path: anchorPath,
198
- offset: range.anchor.offset
199
- });
200
- const focusPath = range.focus && createKeyedPath(range.focus, value, types);
201
- focusPath && range.focus && (focus = {
202
- path: focusPath,
203
- offset: range.focus.offset
204
- });
205
- const backward = !!(Range.isRange(range) && Range.isBackward(range));
206
- return anchor && focus ? {
207
- anchor,
208
- focus,
209
- backward
210
- } : null;
211
- }
212
- function toSlateRange(selection, editor) {
213
- if (!selection || !editor)
214
- return null;
215
- const anchor = {
216
- path: toSlatePath(selection.anchor.path, editor),
217
- offset: selection.anchor.offset
218
- }, focus = {
219
- path: toSlatePath(selection.focus.path, editor),
220
- offset: selection.focus.offset
221
- };
222
- return focus.path.length === 0 || anchor.path.length === 0 ? null : anchor && focus ? {
223
- anchor,
224
- focus
225
- } : null;
226
- }
227
- function moveRangeByOperation(range, operation) {
228
- const anchor = Point.transform(range.anchor, operation), focus = Point.transform(range.focus, operation);
229
- return anchor === null || focus === null ? null : Point.equals(anchor, range.anchor) && Point.equals(focus, range.focus) ? range : {
230
- anchor,
231
- focus
232
- };
233
- }
234
153
  const VOID_CHILD_KEY = "void-child";
235
154
  function keepObjectEquality(object, keyMap) {
236
155
  const value = keyMap[object._key];
@@ -341,10 +260,14 @@ function isEqualToEmptyEditor(children, schemaTypes) {
341
260
  function getFocusBlock({
342
261
  editor
343
262
  }) {
344
- return editor.selection ? Array.from(Editor.nodes(editor, {
345
- at: editor.selection.focus.path.slice(0, 1),
346
- match: (n) => !Editor.isEditor(n)
347
- })).at(0) ?? [void 0, void 0] : [void 0, void 0];
263
+ return editor.selection ? Editor.node(editor, editor.selection.focus.path.slice(0, 1)) ?? [void 0, void 0] : [void 0, void 0];
264
+ }
265
+ function getPointBlock({
266
+ editor,
267
+ point
268
+ }) {
269
+ const [block] = Editor.node(editor, point.path.slice(0, 1)) ?? [void 0, void 0];
270
+ return block ? [block, point.path] : [void 0, void 0];
348
271
  }
349
272
  function getFocusChild({
350
273
  editor
@@ -357,6 +280,23 @@ function getFocusChild({
357
280
  const focusChild = Node.child(focusBlock, childIndex);
358
281
  return focusChild ? [focusChild, [...focusBlockPath, childIndex]] : [void 0, void 0];
359
282
  }
283
+ function getPointChild({
284
+ editor,
285
+ point
286
+ }) {
287
+ const [block, blockPath] = getPointBlock({
288
+ editor,
289
+ point
290
+ }), childIndex = point.path.at(1);
291
+ if (!block || !blockPath || childIndex === void 0)
292
+ return [void 0, void 0];
293
+ try {
294
+ const pointChild = Node.child(block, childIndex);
295
+ return pointChild ? [pointChild, [...blockPath, childIndex]] : [void 0, void 0];
296
+ } catch {
297
+ return [void 0, void 0];
298
+ }
299
+ }
360
300
  function getFirstBlock({
361
301
  editor
362
302
  }) {
@@ -433,6 +373,89 @@ function isStyleActive({
433
373
  })];
434
374
  return selectedBlocks.length > 0 ? selectedBlocks.every(([node]) => node.style === style) : !1;
435
375
  }
376
+ function slateRangeToSelection({
377
+ schema,
378
+ editor,
379
+ range
380
+ }) {
381
+ const [anchorBlock] = getPointBlock({
382
+ editor,
383
+ point: range.anchor
384
+ }), [focusBlock] = getPointBlock({
385
+ editor,
386
+ point: range.focus
387
+ });
388
+ if (!anchorBlock || !focusBlock)
389
+ return null;
390
+ const [anchorChild] = anchorBlock._type === schema.block.name ? getPointChild({
391
+ editor,
392
+ point: range.anchor
393
+ }) : [void 0, void 0], [focusChild] = focusBlock._type === schema.block.name ? getPointChild({
394
+ editor,
395
+ point: range.focus
396
+ }) : [void 0, void 0], selection = {
397
+ anchor: {
398
+ path: [{
399
+ _key: anchorBlock._key
400
+ }],
401
+ offset: range.anchor.offset
402
+ },
403
+ focus: {
404
+ path: [{
405
+ _key: focusBlock._key
406
+ }],
407
+ offset: range.focus.offset
408
+ },
409
+ backward: Range.isBackward(range)
410
+ };
411
+ return anchorChild && (selection.anchor.path.push("children"), selection.anchor.path.push({
412
+ _key: anchorChild._key
413
+ })), focusChild && (selection.focus.path.push("children"), selection.focus.path.push({
414
+ _key: focusChild._key
415
+ })), selection;
416
+ }
417
+ function toSlatePath(path, editor) {
418
+ if (!editor)
419
+ return [];
420
+ const [block, blockPath] = Array.from(Editor.nodes(editor, {
421
+ at: [],
422
+ match: (n) => isKeySegment(path[0]) && n._key === path[0]._key
423
+ }))[0] || [void 0, void 0];
424
+ if (!block || !Element.isElement(block))
425
+ return [];
426
+ if (editor.isVoid(block))
427
+ return [blockPath[0], 0];
428
+ const childPath = [path[2]], childIndex = block.children.findIndex((child) => isEqual([{
429
+ _key: child._key
430
+ }], childPath));
431
+ if (childIndex >= 0 && block.children[childIndex]) {
432
+ const child = block.children[childIndex];
433
+ return Element.isElement(child) && editor.isVoid(child) ? blockPath.concat(childIndex).concat(0) : blockPath.concat(childIndex);
434
+ }
435
+ return [blockPath[0], 0];
436
+ }
437
+ function toSlateRange(selection, editor) {
438
+ if (!selection || !editor)
439
+ return null;
440
+ const anchor = {
441
+ path: toSlatePath(selection.anchor.path, editor),
442
+ offset: selection.anchor.offset
443
+ }, focus = {
444
+ path: toSlatePath(selection.focus.path, editor),
445
+ offset: selection.focus.offset
446
+ };
447
+ return focus.path.length === 0 || anchor.path.length === 0 ? null : anchor && focus ? {
448
+ anchor,
449
+ focus
450
+ } : null;
451
+ }
452
+ function moveRangeByOperation(range, operation) {
453
+ const anchor = Point.transform(range.anchor, operation), focus = Point.transform(range.focus, operation);
454
+ return anchor === null || focus === null ? null : Point.equals(anchor, range.anchor) && Point.equals(focus, range.focus) ? range : {
455
+ anchor,
456
+ focus
457
+ };
458
+ }
436
459
  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({}), PortableTextEditorContext = createContext(null), usePortableTextEditor = () => {
437
460
  const editor = useContext(PortableTextEditorContext);
438
461
  if (!editor)
@@ -3575,7 +3598,11 @@ function createEditableAPI(editor, editorActor) {
3575
3598
  }
3576
3599
  },
3577
3600
  editor
3578
- }), toPortableTextRange(fromSlateValue(editor.children, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types)?.focus.path ?? [];
3601
+ }), editor.selection ? slateRangeToSelection({
3602
+ schema: editorActor.getSnapshot().context.schema,
3603
+ editor,
3604
+ range: editor.selection
3605
+ })?.focus.path ?? [] : [];
3579
3606
  if (!editor.selection)
3580
3607
  throw new Error("The editor has no selection");
3581
3608
  const [focusBlock] = Array.from(Editor.nodes(editor, {
@@ -3603,7 +3630,11 @@ function createEditableAPI(editor, editorActor) {
3603
3630
  })), Transforms.insertNodes(editor, child, {
3604
3631
  select: !0,
3605
3632
  at: editor.selection
3606
- }), editor.onChange(), toPortableTextRange(fromSlateValue(editor.children, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types)?.focus.path || [];
3633
+ }), editor.onChange(), editor.selection ? slateRangeToSelection({
3634
+ schema: editorActor.getSnapshot().context.schema,
3635
+ editor,
3636
+ range: editor.selection
3637
+ })?.focus.path ?? [] : [];
3607
3638
  },
3608
3639
  insertBlock: (type, value) => (editorActor.send({
3609
3640
  type: "behavior event",
@@ -3616,7 +3647,11 @@ function createEditableAPI(editor, editorActor) {
3616
3647
  placement: "auto"
3617
3648
  },
3618
3649
  editor
3619
- }), toPortableTextRange(fromSlateValue(editor.children, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types)?.focus.path ?? []),
3650
+ }), editor.selection ? slateRangeToSelection({
3651
+ schema: editorActor.getSnapshot().context.schema,
3652
+ editor,
3653
+ range: editor.selection
3654
+ })?.focus.path ?? [] : []),
3620
3655
  hasBlockStyle: (style) => {
3621
3656
  try {
3622
3657
  return isStyleActive({
@@ -3774,7 +3809,11 @@ function createEditableAPI(editor, editorActor) {
3774
3809
  const existing = SLATE_TO_PORTABLE_TEXT_RANGE.get(editor.selection);
3775
3810
  if (existing)
3776
3811
  return existing;
3777
- ptRange = toPortableTextRange(fromSlateValue(editor.children, types.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), editor.selection, types), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange);
3812
+ ptRange = slateRangeToSelection({
3813
+ schema: editorActor.getSnapshot().context.schema,
3814
+ editor,
3815
+ range: editor.selection
3816
+ }), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange);
3778
3817
  }
3779
3818
  return ptRange;
3780
3819
  },
@@ -4065,7 +4104,11 @@ const addAnnotationActionImplementation = ({
4065
4104
  } : void 0, selection = manualSelection ? toSlateRange(manualSelection, action.editor) ?? editor.selection : editor.selection;
4066
4105
  if (!selection)
4067
4106
  return;
4068
- const editorSelection = toPortableTextRange(value, selection, context.schema), anchorOffset = editorSelection ? selectionPointToBlockOffset({
4107
+ const editorSelection = slateRangeToSelection({
4108
+ schema: context.schema,
4109
+ editor,
4110
+ range: selection
4111
+ }), anchorOffset = editorSelection ? selectionPointToBlockOffset({
4069
4112
  value,
4070
4113
  selectionPoint: editorSelection.anchor
4071
4114
  }) : void 0, focusOffset = editorSelection ? selectionPointToBlockOffset({
@@ -4452,63 +4495,6 @@ const insertTextActionImplementation = ({
4452
4495
  to,
4453
4496
  mode: "highest"
4454
4497
  });
4455
- }, moveBlockDownActionImplementation = ({
4456
- context,
4457
- action
4458
- }) => {
4459
- const at = [toSlatePath(action.at, action.editor)[0]], to = [Path.next(at)[0]], selection = toPortableTextRange(action.editor.children, {
4460
- anchor: {
4461
- path: to,
4462
- offset: 0
4463
- },
4464
- focus: {
4465
- path: to,
4466
- offset: 0
4467
- }
4468
- }, context.schema), destinationBlockKey = selection && isKeyedSegment$1(selection.focus.path[0]) ? selection.focus.path[0]._key : void 0;
4469
- if (destinationBlockKey === void 0) {
4470
- console.error("Could not find destination block key");
4471
- return;
4472
- }
4473
- moveBlockActionImplementation({
4474
- action: {
4475
- at: action.at,
4476
- to: [{
4477
- _key: destinationBlockKey
4478
- }],
4479
- editor: action.editor
4480
- }
4481
- });
4482
- }, moveBlockUpActionImplementation = ({
4483
- context,
4484
- action
4485
- }) => {
4486
- const at = [toSlatePath(action.at, action.editor)[0]];
4487
- if (!Path.hasPrevious(at))
4488
- return;
4489
- const to = [Path.previous(at)[0]], selection = toPortableTextRange(action.editor.children, {
4490
- anchor: {
4491
- path: to,
4492
- offset: 0
4493
- },
4494
- focus: {
4495
- path: to,
4496
- offset: 0
4497
- }
4498
- }, context.schema), destinationBlockKey = selection && isKeyedSegment$1(selection.focus.path[0]) ? selection.focus.path[0]._key : void 0;
4499
- if (destinationBlockKey === void 0) {
4500
- console.error("Could not find destination block key");
4501
- return;
4502
- }
4503
- moveBlockActionImplementation({
4504
- action: {
4505
- at: action.at,
4506
- to: [{
4507
- _key: destinationBlockKey
4508
- }],
4509
- editor: action.editor
4510
- }
4511
- });
4512
4498
  }, noopActionImplementation = () => {
4513
4499
  }, selectActionImplementation = ({
4514
4500
  action
@@ -4539,8 +4525,6 @@ const insertTextActionImplementation = ({
4539
4525
  "insert.text": insertTextActionImplementation,
4540
4526
  effect: effectActionImplementation,
4541
4527
  "move.block": moveBlockActionImplementation,
4542
- "move.block down": moveBlockDownActionImplementation,
4543
- "move.block up": moveBlockUpActionImplementation,
4544
4528
  noop: noopActionImplementation,
4545
4529
  select: selectActionImplementation
4546
4530
  };
@@ -4710,20 +4694,6 @@ function performAction({
4710
4694
  });
4711
4695
  break;
4712
4696
  }
4713
- case "move.block down": {
4714
- behaviorActionImplementations["move.block down"]({
4715
- context,
4716
- action
4717
- });
4718
- break;
4719
- }
4720
- case "move.block up": {
4721
- behaviorActionImplementations["move.block up"]({
4722
- context,
4723
- action
4724
- });
4725
- break;
4726
- }
4727
4697
  case "noop":
4728
4698
  break;
4729
4699
  default: {
@@ -4735,65 +4705,10 @@ function performAction({
4735
4705
  }
4736
4706
  }
4737
4707
  }
4738
- function createWithEventListeners(editorActor, subscriptions) {
4708
+ function createWithEventListeners(editorActor) {
4739
4709
  return function(editor) {
4740
4710
  if (editorActor.getSnapshot().context.maxBlocks !== void 0)
4741
4711
  return editor;
4742
- subscriptions.push(() => {
4743
- const subscription = editorActor.on("*", (event) => {
4744
- switch (event.type) {
4745
- // These events are not relevant for Behaviors
4746
- case "blurred":
4747
- case "done loading":
4748
- case "editable":
4749
- case "error":
4750
- case "focused":
4751
- case "invalid value":
4752
- case "loading":
4753
- case "mutation":
4754
- case "patch":
4755
- case "internal.patch":
4756
- case "patches":
4757
- case "read only":
4758
- case "ready":
4759
- case "selection":
4760
- case "value changed":
4761
- case "unset":
4762
- break;
4763
- case "custom.*":
4764
- editorActor.send({
4765
- type: "custom behavior event",
4766
- behaviorEvent: event.event,
4767
- editor
4768
- });
4769
- break;
4770
- case "insert.block object":
4771
- editorActor.send({
4772
- type: "behavior event",
4773
- behaviorEvent: {
4774
- type: "insert.block",
4775
- block: {
4776
- _type: event.blockObject.name,
4777
- ...event.blockObject.value ?? {}
4778
- },
4779
- placement: event.placement
4780
- },
4781
- editor
4782
- });
4783
- break;
4784
- default:
4785
- editorActor.send({
4786
- type: "behavior event",
4787
- behaviorEvent: event,
4788
- editor
4789
- });
4790
- break;
4791
- }
4792
- });
4793
- return () => {
4794
- subscription.unsubscribe();
4795
- };
4796
- });
4797
4712
  const {
4798
4713
  deleteBackward,
4799
4714
  deleteForward,
@@ -4919,7 +4834,11 @@ function createWithEventListeners(editorActor, subscriptions) {
4919
4834
  type: "behavior event",
4920
4835
  behaviorEvent: {
4921
4836
  type: "select",
4922
- selection: toPortableTextRange(fromSlateValue(editor.children, editorActor.getSnapshot().context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), range, editorActor.getSnapshot().context.schema)
4837
+ selection: slateRangeToSelection({
4838
+ schema: editorActor.getSnapshot().context.schema,
4839
+ editor,
4840
+ range
4841
+ })
4923
4842
  },
4924
4843
  editor,
4925
4844
  defaultActionCallback: () => {
@@ -5476,7 +5395,7 @@ function createWithPortableTextBlockStyle(editorActor, types) {
5476
5395
  };
5477
5396
  }
5478
5397
  debugWithName("plugin:withPortableTextSelections");
5479
- function createWithPortableTextSelections(editorActor, types) {
5398
+ function createWithPortableTextSelections(editorActor) {
5480
5399
  let prevSelection = null;
5481
5400
  return function(editor) {
5482
5401
  const emitPortableTextSelection = () => {
@@ -5484,12 +5403,11 @@ function createWithPortableTextSelections(editorActor, types) {
5484
5403
  let ptRange = null;
5485
5404
  if (editor.selection) {
5486
5405
  const existing = SLATE_TO_PORTABLE_TEXT_RANGE.get(editor.selection);
5487
- if (existing)
5488
- ptRange = existing;
5489
- else {
5490
- const value = editor.children;
5491
- ptRange = toPortableTextRange(value, editor.selection, types), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange);
5492
- }
5406
+ existing ? ptRange = existing : (ptRange = slateRangeToSelection({
5407
+ schema: editorActor.getSnapshot().context.schema,
5408
+ editor,
5409
+ range: editor.selection
5410
+ }), SLATE_TO_PORTABLE_TEXT_RANGE.set(editor.selection, ptRange));
5493
5411
  }
5494
5412
  ptRange ? editorActor.send({
5495
5413
  type: "notify.selection",
@@ -5603,8 +5521,8 @@ const withPlugins = (editor, options) => {
5603
5521
  }), withPortableTextMarkModel = createWithPortableTextMarkModel(editorActor, schemaTypes), withPortableTextBlockStyle = createWithPortableTextBlockStyle(editorActor, schemaTypes), withPlaceholderBlock = createWithPlaceholderBlock(editorActor), withUtils = createWithUtils({
5604
5522
  editorActor,
5605
5523
  schemaTypes
5606
- }), withPortableTextSelections = createWithPortableTextSelections(editorActor, schemaTypes);
5607
- return createWithEventListeners(editorActor, options.subscriptions)(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withPortableTextBlockStyle(withPlaceholderBlock(withUtils(withMaxBlocks(withUndoRedo(withPatches(withPortableTextSelections(e)))))))))));
5524
+ }), withPortableTextSelections = createWithPortableTextSelections(editorActor);
5525
+ return createWithEventListeners(editorActor)(withSchemaTypes(withObjectKeys(withPortableTextMarkModel(withPortableTextBlockStyle(withPlaceholderBlock(withUtils(withMaxBlocks(withUndoRedo(withPatches(withPortableTextSelections(e)))))))))));
5608
5526
  }, debug$3 = debugWithName("component:PortableTextEditor:SlateContainer"), slateEditors = /* @__PURE__ */ new WeakMap();
5609
5527
  function createSlateEditor(config) {
5610
5528
  const existingSlateEditor = slateEditors.get(config.editorActor);
@@ -5626,17 +5544,7 @@ function createSlateEditor(config) {
5626
5544
  };
5627
5545
  return slateEditors.set(config.editorActor, slateEditor), slateEditor;
5628
5546
  }
5629
- const keyIs = {
5630
- lineBreak: (event) => event.key === "Enter" && event.shiftKey
5631
- }, raiseInsertSoftBreak = defineBehavior({
5632
- on: "keyboard.keydown",
5633
- guard: ({
5634
- event
5635
- }) => keyIs.lineBreak(event.originEvent),
5636
- actions: [() => [raise({
5637
- type: "insert.soft break"
5638
- })]]
5639
- }), internalAnnotationBehaviors = [defineBehavior({
5547
+ const abstractAnnotationBehaviors = [defineBehavior({
5640
5548
  on: "annotation.toggle",
5641
5549
  guard: ({
5642
5550
  snapshot,
@@ -5660,7 +5568,7 @@ const keyIs = {
5660
5568
  type: "annotation.add",
5661
5569
  annotation: event.annotation
5662
5570
  })]]
5663
- })], internalDecoratorBehaviors = [defineBehavior({
5571
+ })], abstractDecoratorBehaviors = [defineBehavior({
5664
5572
  on: "decorator.toggle",
5665
5573
  guard: ({
5666
5574
  snapshot,
@@ -5696,7 +5604,7 @@ const keyIs = {
5696
5604
  ...event,
5697
5605
  type: "decorator.add"
5698
5606
  })]]
5699
- })], internalInsertBehaviors = [defineBehavior({
5607
+ })], abstractInsertBehaviors = [defineBehavior({
5700
5608
  on: "insert.blocks",
5701
5609
  guard: ({
5702
5610
  event
@@ -5772,7 +5680,7 @@ const keyIs = {
5772
5680
  placement: index === 0 ? "auto" : "after",
5773
5681
  select: "end"
5774
5682
  }))]
5775
- })], internalListItemBehaviors = [defineBehavior({
5683
+ })], abstractListItemBehaviors = [defineBehavior({
5776
5684
  on: "list item.add",
5777
5685
  guard: ({
5778
5686
  snapshot
@@ -5829,7 +5737,75 @@ const keyIs = {
5829
5737
  type: "list item.add",
5830
5738
  listItem: event.listItem
5831
5739
  })]]
5832
- })], internalSelectBehaviors = [defineBehavior({
5740
+ })], abstractMoveBehaviors = [defineBehavior({
5741
+ on: "move.block up",
5742
+ guard: ({
5743
+ snapshot,
5744
+ event
5745
+ }) => {
5746
+ const previousBlock = getPreviousBlock({
5747
+ context: {
5748
+ ...snapshot.context,
5749
+ selection: {
5750
+ anchor: {
5751
+ path: event.at,
5752
+ offset: 0
5753
+ },
5754
+ focus: {
5755
+ path: event.at,
5756
+ offset: 0
5757
+ }
5758
+ }
5759
+ }
5760
+ });
5761
+ return previousBlock ? {
5762
+ previousBlock
5763
+ } : !1;
5764
+ },
5765
+ actions: [({
5766
+ event
5767
+ }, {
5768
+ previousBlock
5769
+ }) => [raise({
5770
+ type: "move.block",
5771
+ at: event.at,
5772
+ to: previousBlock.path
5773
+ })]]
5774
+ }), defineBehavior({
5775
+ on: "move.block down",
5776
+ guard: ({
5777
+ snapshot,
5778
+ event
5779
+ }) => {
5780
+ const nextBlock = getNextBlock({
5781
+ context: {
5782
+ ...snapshot.context,
5783
+ selection: {
5784
+ anchor: {
5785
+ path: event.at,
5786
+ offset: 0
5787
+ },
5788
+ focus: {
5789
+ path: event.at,
5790
+ offset: 0
5791
+ }
5792
+ }
5793
+ }
5794
+ });
5795
+ return nextBlock ? {
5796
+ nextBlock
5797
+ } : !1;
5798
+ },
5799
+ actions: [({
5800
+ event
5801
+ }, {
5802
+ nextBlock
5803
+ }) => [raise({
5804
+ type: "move.block",
5805
+ at: event.at,
5806
+ to: nextBlock.path
5807
+ })]]
5808
+ })], abstractSelectBehaviors = [defineBehavior({
5833
5809
  on: "select.previous block",
5834
5810
  guard: ({
5835
5811
  snapshot,
@@ -5875,7 +5851,7 @@ const keyIs = {
5875
5851
  type: "select",
5876
5852
  selection
5877
5853
  })]]
5878
- })], internalStyleBehaviors = [defineBehavior({
5854
+ })], abstractStyleBehaviors = [defineBehavior({
5879
5855
  on: "style.add",
5880
5856
  guard: ({
5881
5857
  snapshot
@@ -5931,7 +5907,17 @@ const keyIs = {
5931
5907
  type: "style.add",
5932
5908
  style: event.style
5933
5909
  })]]
5934
- })], raiseDeserializationSuccessOrFailure = defineBehavior({
5910
+ })], keyIs = {
5911
+ lineBreak: (event) => event.key === "Enter" && event.shiftKey
5912
+ }, raiseInsertSoftBreak = defineBehavior({
5913
+ on: "keyboard.keydown",
5914
+ guard: ({
5915
+ event
5916
+ }) => keyIs.lineBreak(event.originEvent),
5917
+ actions: [() => [raise({
5918
+ type: "insert.soft break"
5919
+ })]]
5920
+ }), raiseDeserializationSuccessOrFailure = defineBehavior({
5935
5921
  on: "deserialize",
5936
5922
  guard: ({
5937
5923
  snapshot,
@@ -6259,43 +6245,30 @@ const keyIs = {
6259
6245
  originEvent: event
6260
6246
  })]]
6261
6247
  }),
6262
- ...internalAnnotationBehaviors,
6263
- ...internalDecoratorBehaviors,
6264
- ...internalInsertBehaviors,
6265
- ...internalListItemBehaviors,
6266
- ...internalStyleBehaviors,
6267
- ...internalSelectBehaviors,
6248
+ ...abstractAnnotationBehaviors,
6249
+ ...abstractDecoratorBehaviors,
6250
+ ...abstractInsertBehaviors,
6251
+ ...abstractListItemBehaviors,
6252
+ ...abstractMoveBehaviors,
6253
+ ...abstractStyleBehaviors,
6254
+ ...abstractSelectBehaviors,
6268
6255
  raiseDeserializationSuccessOrFailure,
6269
6256
  raiseSerializationSuccessOrFailure,
6270
6257
  raiseInsertSoftBreak
6271
- ];
6272
- function isKeyboardBehaviorEvent(event) {
6273
- return event.type.startsWith("keyboard.");
6274
- }
6275
- function isInternalBehaviorEvent(event) {
6276
- 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.");
6258
+ ], 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"];
6259
+ function isAbstractBehaviorEvent(event) {
6260
+ return abstractBehaviorEventTypes.includes(event.type);
6277
6261
  }
6262
+ 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"];
6278
6263
  function isNativeBehaviorEvent(event) {
6279
- return isClipboardBehaviorEvent(event) || isDragBehaviorEvent(event) || isInputBehaviorEvent(event) || isKeyboardBehaviorEvent(event) || isMouseBehaviorEvent(event);
6280
- }
6281
- function isClipboardBehaviorEvent(event) {
6282
- return event.type.startsWith("clipboard.");
6283
- }
6284
- function isDragBehaviorEvent(event) {
6285
- return event.type.startsWith("drag.");
6286
- }
6287
- function isInputBehaviorEvent(event) {
6288
- return event.type.startsWith("input.");
6289
- }
6290
- function isMouseBehaviorEvent(event) {
6291
- return event.type.startsWith("mouse.");
6264
+ return nativeBehaviorEventTypes.includes(event.type);
6292
6265
  }
6293
6266
  function isCustomBehaviorEvent(event) {
6294
6267
  return event.type.startsWith("custom.");
6295
6268
  }
6296
6269
  const debug$2 = debugWithName("behaviors:event");
6297
6270
  function eventCategory(event) {
6298
- return isNativeBehaviorEvent(event) ? "native" : isInternalBehaviorEvent(event) ? "internal" : isCustomBehaviorEvent(event) ? "custom" : "synthetic";
6271
+ return isNativeBehaviorEvent(event) ? "native" : isAbstractBehaviorEvent(event) ? "abstract" : isCustomBehaviorEvent(event) ? "custom" : "synthetic";
6299
6272
  }
6300
6273
  function performEvent({
6301
6274
  behaviors,
@@ -6308,7 +6281,7 @@ function performEvent({
6308
6281
  defaultActionCallback
6309
6282
  }) {
6310
6283
  debug$2(`(${eventCategory(event)})`, JSON.stringify(event, null, 2));
6311
- const defaultAction = isCustomBehaviorEvent(event) || isNativeBehaviorEvent(event) || isInternalBehaviorEvent(event) ? void 0 : {
6284
+ const defaultAction = isCustomBehaviorEvent(event) || isNativeBehaviorEvent(event) || isAbstractBehaviorEvent(event) ? void 0 : {
6312
6285
  ...event,
6313
6286
  editor
6314
6287
  }, eventBehaviors = behaviors.filter((behavior) => {
@@ -6427,6 +6400,34 @@ function performEvent({
6427
6400
  }), editor.onChange();
6428
6401
  }
6429
6402
  }
6403
+ function slateChildrenToBlocks(schema, value) {
6404
+ const blocks = new Array(value.length);
6405
+ for (let blockIndex = 0; blockIndex < value.length; blockIndex++) {
6406
+ const descendant = value[blockIndex];
6407
+ if (descendant._type !== schema.block.name) {
6408
+ blocks[blockIndex] = {
6409
+ _key: descendant._key,
6410
+ _type: descendant._type,
6411
+ ..."value" in descendant && typeof descendant.value == "object" ? descendant.value : {}
6412
+ };
6413
+ continue;
6414
+ }
6415
+ const children = "children" in descendant ? descendant.children : [], processedChildren = new Array(children.length);
6416
+ for (let childIndex = 0; childIndex < children.length; childIndex++) {
6417
+ const child = children[childIndex];
6418
+ processedChildren[childIndex] = child._type === schema.span.name ? child : {
6419
+ _key: child._key,
6420
+ _type: child._type,
6421
+ ..."value" in child && typeof child.value == "object" ? child.value : {}
6422
+ };
6423
+ }
6424
+ blocks[blockIndex] = {
6425
+ ...descendant,
6426
+ children: processedChildren
6427
+ };
6428
+ }
6429
+ return blocks;
6430
+ }
6430
6431
  function getActiveDecorators({
6431
6432
  schema,
6432
6433
  slateEditorInstance
@@ -6445,7 +6446,11 @@ function createEditorSnapshot({
6445
6446
  hasTag,
6446
6447
  internalDrag
6447
6448
  }) {
6448
- const value = fromSlateValue(editor.children, schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), selection = toPortableTextRange(value, editor.selection, schema);
6449
+ const value = slateChildrenToBlocks(schema, editor.children), selection = editor.selection ? slateRangeToSelection({
6450
+ schema,
6451
+ editor,
6452
+ range: editor.selection
6453
+ }) : null;
6449
6454
  return {
6450
6455
  context: {
6451
6456
  activeDecorators: getActiveDecorators({
@@ -6541,7 +6546,7 @@ const editorMachine = setup({
6541
6546
  event,
6542
6547
  self
6543
6548
  }) => {
6544
- assertEvent(event, ["behavior event", "custom behavior event"]), performEvent({
6549
+ assertEvent(event, ["behavior event"]), performEvent({
6545
6550
  behaviors: [...context.behaviors.values(), ...defaultBehaviors],
6546
6551
  event: event.behaviorEvent,
6547
6552
  editor: event.editor,
@@ -6740,87 +6745,6 @@ const editorMachine = setup({
6740
6745
  },
6741
6746
  "behavior event": {
6742
6747
  actions: "handle behavior event"
6743
- },
6744
- "custom behavior event": {
6745
- actions: "handle behavior event"
6746
- },
6747
- "annotation.*": {
6748
- actions: emit(({
6749
- event
6750
- }) => event)
6751
- },
6752
- "block.*": {
6753
- actions: emit(({
6754
- event
6755
- }) => event)
6756
- },
6757
- blur: {
6758
- actions: emit(({
6759
- event
6760
- }) => event)
6761
- },
6762
- "custom.*": {
6763
- actions: emit(({
6764
- event
6765
- }) => ({
6766
- type: "custom.*",
6767
- event
6768
- }))
6769
- },
6770
- "decorator.*": {
6771
- actions: emit(({
6772
- event
6773
- }) => event)
6774
- },
6775
- "delete.*": {
6776
- actions: emit(({
6777
- event
6778
- }) => event)
6779
- },
6780
- focus: {
6781
- actions: emit(({
6782
- event
6783
- }) => event)
6784
- },
6785
- "history.*": {
6786
- actions: emit(({
6787
- event
6788
- }) => event)
6789
- },
6790
- "insert.*": {
6791
- actions: emit(({
6792
- event
6793
- }) => event)
6794
- },
6795
- "list item.*": {
6796
- actions: emit(({
6797
- event
6798
- }) => event)
6799
- },
6800
- "move.*": {
6801
- actions: emit(({
6802
- event
6803
- }) => event)
6804
- },
6805
- select: {
6806
- actions: emit(({
6807
- event
6808
- }) => event)
6809
- },
6810
- "select.*": {
6811
- actions: emit(({
6812
- event
6813
- }) => event)
6814
- },
6815
- "style.*": {
6816
- actions: emit(({
6817
- event
6818
- }) => event)
6819
- },
6820
- "text block.*": {
6821
- actions: emit(({
6822
- event
6823
- }) => event)
6824
6748
  }
6825
6749
  },
6826
6750
  initial: "idle",
@@ -6932,12 +6856,6 @@ const editorMachine = setup({
6932
6856
  }
6933
6857
  }
6934
6858
  });
6935
- function getValue({
6936
- editorActorSnapshot,
6937
- slateEditorInstance
6938
- }) {
6939
- return fromSlateValue(slateEditorInstance.children, editorActorSnapshot.context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(slateEditorInstance));
6940
- }
6941
6859
  function defaultCompare(a, b) {
6942
6860
  return a === b;
6943
6861
  }
@@ -6969,10 +6887,7 @@ function getEditorSnapshot({
6969
6887
  }),
6970
6888
  schema: editorActorSnapshot.context.schema,
6971
6889
  selection: editorActorSnapshot.context.selection,
6972
- value: getValue({
6973
- editorActorSnapshot,
6974
- slateEditorInstance
6975
- })
6890
+ value: slateChildrenToBlocks(editorActorSnapshot.context.schema, slateEditorInstance.children)
6976
6891
  },
6977
6892
  beta: {
6978
6893
  hasTag: (tag) => editorActorSnapshot.hasTag(tag),
@@ -7028,7 +6943,39 @@ function createInternalEditorFromActor(editorActor) {
7028
6943
  });
7029
6944
  }),
7030
6945
  send: (event) => {
7031
- editorActor.send(event);
6946
+ switch (event.type) {
6947
+ case "add behavior":
6948
+ case "remove behavior":
6949
+ case "update behaviors":
6950
+ case "update key generator":
6951
+ case "update readOnly":
6952
+ case "patches":
6953
+ case "update value":
6954
+ case "update schema":
6955
+ case "update maxBlocks":
6956
+ editorActor.send(event);
6957
+ break;
6958
+ case "insert.block object":
6959
+ editorActor.send({
6960
+ type: "behavior event",
6961
+ behaviorEvent: {
6962
+ type: "insert.block",
6963
+ block: {
6964
+ _type: event.blockObject.name,
6965
+ ...event.blockObject.value ?? {}
6966
+ },
6967
+ placement: event.placement
6968
+ },
6969
+ editor: slateEditor.instance
6970
+ });
6971
+ break;
6972
+ default:
6973
+ editorActor.send({
6974
+ type: "behavior event",
6975
+ behaviorEvent: event,
6976
+ editor: slateEditor.instance
6977
+ });
6978
+ }
7032
6979
  },
7033
6980
  on: (event, listener) => editorActor.on(event, (event2) => {
7034
6981
  switch (event2.type) {
@@ -7638,7 +7585,7 @@ export {
7638
7585
  getNodeBlock,
7639
7586
  isEqualToEmptyEditor,
7640
7587
  moveRangeByOperation,
7641
- toPortableTextRange,
7588
+ slateRangeToSelection,
7642
7589
  toSlateRange,
7643
7590
  useEditor,
7644
7591
  useEditorSelector,