@portabletext/editor 1.40.2 → 1.40.4

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 (74) hide show
  1. package/lib/_chunks-cjs/behavior.core.cjs +37 -16
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
  3. package/lib/_chunks-cjs/behavior.markdown.cjs +22 -10
  4. package/lib/_chunks-cjs/behavior.markdown.cjs.map +1 -1
  5. package/lib/_chunks-cjs/editor-provider.cjs +93 -100
  6. package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
  7. package/lib/_chunks-cjs/util.is-selection-collapsed.cjs +6 -0
  8. package/lib/_chunks-cjs/util.is-selection-collapsed.cjs.map +1 -0
  9. package/lib/_chunks-cjs/util.merge-text-blocks.cjs +26 -0
  10. package/lib/_chunks-cjs/util.merge-text-blocks.cjs.map +1 -0
  11. package/lib/_chunks-cjs/util.selection-point-to-block-offset.cjs +1 -0
  12. package/lib/_chunks-cjs/util.selection-point-to-block-offset.cjs.map +1 -1
  13. package/lib/_chunks-cjs/util.slice-blocks.cjs.map +1 -1
  14. package/lib/_chunks-es/behavior.core.js +37 -16
  15. package/lib/_chunks-es/behavior.core.js.map +1 -1
  16. package/lib/_chunks-es/behavior.markdown.js +22 -10
  17. package/lib/_chunks-es/behavior.markdown.js.map +1 -1
  18. package/lib/_chunks-es/editor-provider.js +94 -101
  19. package/lib/_chunks-es/editor-provider.js.map +1 -1
  20. package/lib/_chunks-es/util.is-selection-collapsed.js +7 -0
  21. package/lib/_chunks-es/util.is-selection-collapsed.js.map +1 -0
  22. package/lib/_chunks-es/util.merge-text-blocks.js +27 -0
  23. package/lib/_chunks-es/util.merge-text-blocks.js.map +1 -0
  24. package/lib/_chunks-es/util.selection-point-to-block-offset.js +1 -0
  25. package/lib/_chunks-es/util.slice-blocks.js.map +1 -1
  26. package/lib/behaviors/index.d.cts +3881 -5053
  27. package/lib/behaviors/index.d.ts +3881 -5053
  28. package/lib/index.cjs +47 -13
  29. package/lib/index.cjs.map +1 -1
  30. package/lib/index.d.cts +3402 -4440
  31. package/lib/index.d.ts +3402 -4440
  32. package/lib/index.js +49 -14
  33. package/lib/index.js.map +1 -1
  34. package/lib/plugins/index.cjs +9 -127
  35. package/lib/plugins/index.cjs.map +1 -1
  36. package/lib/plugins/index.d.cts +3404 -4441
  37. package/lib/plugins/index.d.ts +3404 -4441
  38. package/lib/plugins/index.js +11 -129
  39. package/lib/plugins/index.js.map +1 -1
  40. package/lib/selectors/index.d.cts +3401 -4440
  41. package/lib/selectors/index.d.ts +3401 -4440
  42. package/lib/utils/index.cjs +45 -7
  43. package/lib/utils/index.cjs.map +1 -1
  44. package/lib/utils/index.d.cts +3403 -4443
  45. package/lib/utils/index.d.ts +3403 -4443
  46. package/lib/utils/index.js +45 -5
  47. package/lib/utils/index.js.map +1 -1
  48. package/package.json +6 -6
  49. package/src/behavior-actions/behavior.action.delete.block.ts +2 -2
  50. package/src/behavior-actions/behavior.action.insert-blocks.ts +5 -1
  51. package/src/behavior-actions/behavior.actions.ts +0 -18
  52. package/src/behaviors/behavior.core.block-objects.ts +57 -7
  53. package/src/behaviors/behavior.core.insert-break.ts +0 -4
  54. package/src/behaviors/behavior.default.ts +1 -1
  55. package/src/behaviors/behavior.markdown.ts +22 -10
  56. package/src/behaviors/behavior.types.ts +171 -138
  57. package/src/editor/create-editor.ts +2 -0
  58. package/src/editor/editor-machine.ts +28 -28
  59. package/src/editor/plugins/create-with-event-listeners.ts +15 -0
  60. package/src/editor/plugins/createWithEditableAPI.ts +4 -4
  61. package/src/internal-utils/drag-selection.test.ts +74 -1
  62. package/src/internal-utils/drag-selection.ts +20 -4
  63. package/src/internal-utils/event-position.ts +38 -7
  64. package/src/internal-utils/slate-utils.ts +60 -1
  65. package/src/plugins/plugin.one-line.tsx +10 -128
  66. package/src/types/block-with-optional-key.ts +13 -0
  67. package/src/utils/util.is-keyed-segment.ts +2 -2
  68. package/src/utils/util.is-text-block.ts +4 -3
  69. package/lib/_chunks-cjs/util.split-text-block.cjs +0 -68
  70. package/lib/_chunks-cjs/util.split-text-block.cjs.map +0 -1
  71. package/lib/_chunks-es/util.split-text-block.js +0 -70
  72. package/lib/_chunks-es/util.split-text-block.js.map +0 -1
  73. package/src/behavior-actions/behavior.action.insert.block-object.ts +0 -20
  74. package/src/behavior-actions/behavior.action.insert.text-block.ts +0 -33
@@ -5,7 +5,7 @@ import { ReactEditor, withReact, Slate } from "slate-react";
5
5
  import { useSelector, useActorRef } from "@xstate/react";
6
6
  import debug$e from "debug";
7
7
  import isEqual from "lodash/isEqual.js";
8
- import { Editor, Element, Range, Point, Text, Operation, Transforms, Path, insertText, Node, createEditor } from "slate";
8
+ import { Editor, Element, Range, Point, Text, Node, Operation, Transforms, Path, 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";
@@ -337,6 +337,65 @@ function fromSlateValue(value, textBlockType, keyMap = {}) {
337
337
  function isEqualToEmptyEditor(children, schemaTypes) {
338
338
  return children === void 0 || children && Array.isArray(children) && children.length === 0 || children && Array.isArray(children) && children.length === 1 && Element.isElement(children[0]) && children[0]._type === schemaTypes.block.name && "style" in children[0] && children[0].style === schemaTypes.styles[0].value && !("listItem" in children[0]) && Array.isArray(children[0].children) && children[0].children.length === 1 && Text.isText(children[0].children[0]) && children[0].children[0]._type === "span" && !children[0].children[0].marks?.join("") && children[0].children[0].text === "";
339
339
  }
340
+ function getFocusBlock({
341
+ editor
342
+ }) {
343
+ return editor.selection ? Array.from(Editor.nodes(editor, {
344
+ at: editor.selection.focus.path.slice(0, 1),
345
+ match: (n) => !Editor.isEditor(n)
346
+ })).at(0) ?? [void 0, void 0] : [void 0, void 0];
347
+ }
348
+ function getFocusChild({
349
+ editor
350
+ }) {
351
+ const [focusBlock, focusBlockPath] = getFocusBlock({
352
+ editor
353
+ }), childIndex = editor.selection?.focus.path.at(1);
354
+ if (!focusBlock || !focusBlockPath || childIndex === void 0)
355
+ return [void 0, void 0];
356
+ const focusChild = Node.child(focusBlock, childIndex);
357
+ return focusChild ? [focusChild, [...focusBlockPath, childIndex]] : [void 0, void 0];
358
+ }
359
+ function getLastBlock({
360
+ editor
361
+ }) {
362
+ return Array.from(Editor.nodes(editor, {
363
+ match: (n) => !Editor.isEditor(n),
364
+ at: [],
365
+ reverse: !0
366
+ })).at(0) ?? [void 0, void 0];
367
+ }
368
+ function getNodeBlock({
369
+ editor,
370
+ schema,
371
+ node
372
+ }) {
373
+ if (Editor.isEditor(node))
374
+ return;
375
+ if (isBlockElement(schema, node))
376
+ return elementToBlock({
377
+ schema,
378
+ element: node
379
+ });
380
+ const parent = Array.from(Editor.nodes(editor, {
381
+ mode: "highest",
382
+ at: [],
383
+ match: (n) => isBlockElement(schema, n) && n.children.some((child) => child._key === node._key)
384
+ })).at(0)?.at(0);
385
+ return Element.isElement(parent) ? elementToBlock({
386
+ schema,
387
+ element: parent
388
+ }) : void 0;
389
+ }
390
+ function elementToBlock({
391
+ schema,
392
+ element
393
+ }) {
394
+ return fromSlateValue([element], schema.block.name)?.at(0);
395
+ }
396
+ function isBlockElement(schema, node) {
397
+ return Element.isElement(node) && (schema.block.name === node._type || schema.blockObjects.some((blockObject) => blockObject.name === node._type));
398
+ }
340
399
  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 = () => {
341
400
  const editor = useContext(PortableTextEditorContext);
342
401
  if (!editor)
@@ -3756,10 +3815,10 @@ function createEditableAPI(editor, editorActor) {
3756
3815
  insertBlock: (type, value) => (editorActor.send({
3757
3816
  type: "behavior event",
3758
3817
  behaviorEvent: {
3759
- type: "insert.block object",
3760
- blockObject: {
3761
- name: type.name,
3762
- value
3818
+ type: "insert.block",
3819
+ block: {
3820
+ _type: type.name,
3821
+ ...value || {}
3763
3822
  },
3764
3823
  placement: "auto"
3765
3824
  },
@@ -4241,11 +4300,11 @@ const addAnnotationActionImplementation = ({
4241
4300
  }) => {
4242
4301
  const range = toSlateRange({
4243
4302
  anchor: {
4244
- path: action.blockPath,
4303
+ path: action.at,
4245
4304
  offset: 0
4246
4305
  },
4247
4306
  focus: {
4248
- path: action.blockPath,
4307
+ path: action.at,
4249
4308
  offset: 0
4250
4309
  }
4251
4310
  }, action.editor);
@@ -4296,36 +4355,7 @@ const addAnnotationActionImplementation = ({
4296
4355
  action
4297
4356
  }) => {
4298
4357
  console.warn(`Deserialization of ${action.mimeType} failed with reason "${action.reason}"`);
4299
- };
4300
- function getFocusBlock({
4301
- editor
4302
- }) {
4303
- return editor.selection ? Array.from(Editor.nodes(editor, {
4304
- at: editor.selection.focus.path.slice(0, 1),
4305
- match: (n) => !Editor.isEditor(n)
4306
- })).at(0) ?? [void 0, void 0] : [void 0, void 0];
4307
- }
4308
- function getFocusChild({
4309
- editor
4310
- }) {
4311
- const [focusBlock, focusBlockPath] = getFocusBlock({
4312
- editor
4313
- }), childIndex = editor.selection?.focus.path.at(1);
4314
- if (!focusBlock || !focusBlockPath || childIndex === void 0)
4315
- return [void 0, void 0];
4316
- const focusChild = Node.child(focusBlock, childIndex);
4317
- return focusChild ? [focusChild, [...focusBlockPath, childIndex]] : [void 0, void 0];
4318
- }
4319
- function getLastBlock({
4320
- editor
4321
- }) {
4322
- return Array.from(Editor.nodes(editor, {
4323
- match: (n) => !Editor.isEditor(n),
4324
- at: [],
4325
- reverse: !0
4326
- })).at(0) ?? [void 0, void 0];
4327
- }
4328
- const insertBlockActionImplementation = ({
4358
+ }, insertBlockActionImplementation = ({
4329
4359
  context,
4330
4360
  action
4331
4361
  }) => {
@@ -4613,15 +4643,17 @@ const selectActionImplementation = ({
4613
4643
  editor: action.editor,
4614
4644
  schema: context.schema
4615
4645
  }), index++;
4616
- } else
4646
+ } else {
4647
+ let index = 0;
4617
4648
  for (const block of fragment)
4618
4649
  insertBlock({
4619
4650
  block,
4620
- placement: "auto",
4651
+ placement: index === 0 ? "auto" : "after",
4621
4652
  select: "end",
4622
4653
  editor: action.editor,
4623
4654
  schema: context.schema
4624
- });
4655
+ }), index++;
4656
+ }
4625
4657
  }, deserializationSuccessActionImplementation = ({
4626
4658
  context,
4627
4659
  action
@@ -4707,53 +4739,10 @@ const selectActionImplementation = ({
4707
4739
  text: action.text,
4708
4740
  marks: [...annotations?.map((annotation) => annotation._key) ?? [], ...action.decorators ?? []]
4709
4741
  });
4710
- }, insertBlockObjectActionImplementation = ({
4711
- context,
4712
- action
4713
- }) => {
4714
- insertBlockActionImplementation({
4715
- context,
4716
- action: {
4717
- block: {
4718
- _key: context.keyGenerator(),
4719
- _type: action.blockObject.name,
4720
- ...action.blockObject.value ? action.blockObject.value : {}
4721
- },
4722
- editor: action.editor,
4723
- placement: action.placement
4724
- }
4725
- });
4726
4742
  }, insertTextActionImplementation = ({
4727
4743
  action
4728
4744
  }) => {
4729
4745
  action.editor.insertText(action.text);
4730
- }, insertTextBlockActionImplementation = ({
4731
- context,
4732
- action
4733
- }) => {
4734
- insertBlockActionImplementation({
4735
- context,
4736
- action: {
4737
- block: {
4738
- _key: context.keyGenerator(),
4739
- _type: context.schema.block.name,
4740
- children: action.textBlock?.children?.map((child) => ({
4741
- ...child,
4742
- _key: context.keyGenerator()
4743
- })) ?? [{
4744
- _type: context.schema.span.name,
4745
- _key: context.keyGenerator(),
4746
- text: "",
4747
- marks: []
4748
- }],
4749
- markDefs: [],
4750
- style: context.schema.styles[0].value ?? "normal"
4751
- },
4752
- editor: action.editor,
4753
- placement: action.placement,
4754
- select: "start"
4755
- }
4756
- });
4757
4746
  }, moveBlockActionImplementation = ({
4758
4747
  action
4759
4748
  }) => {
@@ -4859,13 +4848,11 @@ const selectActionImplementation = ({
4859
4848
  "history.undo": historyUndoActionImplementation,
4860
4849
  "insert.block": insertBlockActionImplementation,
4861
4850
  "insert.blocks": insertBlocksActionImplementation,
4862
- "insert.block object": insertBlockObjectActionImplementation,
4863
4851
  "insert.break": insertBreakActionImplementation,
4864
4852
  "insert.inline object": insertInlineObjectActionImplementation,
4865
4853
  "insert.soft break": insertSoftBreakActionImplementation,
4866
4854
  "insert.span": insertSpanActionImplementation,
4867
4855
  "insert.text": insertTextActionImplementation,
4868
- "insert.text block": insertTextBlockActionImplementation,
4869
4856
  effect: effectActionImplementation,
4870
4857
  "list item.add": addListItemActionImplementation,
4871
4858
  "list item.remove": removeListItemActionImplementation,
@@ -5049,13 +5036,6 @@ function performAction({
5049
5036
  });
5050
5037
  break;
5051
5038
  }
5052
- case "insert.block object": {
5053
- behaviorActionImplementations["insert.block object"]({
5054
- context,
5055
- action
5056
- });
5057
- break;
5058
- }
5059
5039
  case "insert.inline object": {
5060
5040
  behaviorActionImplementations["insert.inline object"]({
5061
5041
  context,
@@ -5091,13 +5071,6 @@ function performAction({
5091
5071
  });
5092
5072
  break;
5093
5073
  }
5094
- case "insert.text block": {
5095
- behaviorActionImplementations["insert.text block"]({
5096
- context,
5097
- action
5098
- });
5099
- break;
5100
- }
5101
5074
  case "list item.add": {
5102
5075
  behaviorActionImplementations["list item.add"]({
5103
5076
  context,
@@ -5232,6 +5205,20 @@ function createWithEventListeners(editorActor, subscriptions) {
5232
5205
  editor
5233
5206
  });
5234
5207
  break;
5208
+ case "insert.block object":
5209
+ editorActor.send({
5210
+ type: "behavior event",
5211
+ behaviorEvent: {
5212
+ type: "insert.block",
5213
+ block: {
5214
+ _type: event.blockObject.name,
5215
+ ...event.blockObject.value ?? {}
5216
+ },
5217
+ placement: event.placement
5218
+ },
5219
+ editor
5220
+ });
5221
+ break;
5235
5222
  default:
5236
5223
  editorActor.send({
5237
5224
  type: "behavior event",
@@ -6380,7 +6367,7 @@ const keyIs = {
6380
6367
  originEvent
6381
6368
  }) => [...draggingEntireBlocks ? draggedBlocks.map((block) => raise({
6382
6369
  type: "delete.block",
6383
- blockPath: block.path
6370
+ at: block.path
6384
6371
  })) : [raise({
6385
6372
  type: "delete",
6386
6373
  selection: dragOrigin.selection
@@ -6552,7 +6539,12 @@ const editorMachine = setup({
6552
6539
  const defaultAction = event.type === "custom behavior event" || isClipboardBehaviorEvent(event.behaviorEvent) || isDragBehaviorEvent(event.behaviorEvent) || isInputBehaviorEvent(event.behaviorEvent) || isKeyboardBehaviorEvent(event.behaviorEvent) || isMouseBehaviorEvent(event.behaviorEvent) || event.behaviorEvent.type === "deserialize" || event.behaviorEvent.type === "serialize" ? void 0 : {
6553
6540
  ...event.behaviorEvent,
6554
6541
  editor: event.editor
6555
- }, defaultActionCallback = event.type === "behavior event" ? event.defaultActionCallback : void 0, eventBehaviors = [...context.behaviors.values(), ...defaultBehaviors].filter((behavior) => behavior.on === "*" ? !0 : isClipboardBehaviorEvent(event.behaviorEvent) ? behavior.on === "clipboard.*" || behavior.on === event.behaviorEvent.type : isDragBehaviorEvent(event.behaviorEvent) ? behavior.on === "drag.*" || behavior.on === event.behaviorEvent.type : isInputBehaviorEvent(event.behaviorEvent) ? behavior.on === "input.*" : isKeyboardBehaviorEvent(event.behaviorEvent) ? behavior.on === "keyboard.*" || behavior.on === event.behaviorEvent.type : isMouseBehaviorEvent(event.behaviorEvent) && behavior.on === "mouse.*" || behavior.on === event.behaviorEvent.type);
6542
+ }, defaultActionCallback = event.type === "behavior event" ? event.defaultActionCallback : void 0, eventBehaviors = [...context.behaviors.values(), ...defaultBehaviors].filter((behavior) => {
6543
+ if (behavior.on === "*")
6544
+ return !0;
6545
+ const [listenedNamespace] = behavior.on.includes("*") && behavior.on.includes(".") ? behavior.on.split(".") : [void 0], [eventNamespace] = event.behaviorEvent.type.includes(".") ? event.behaviorEvent.type.split(".") : [void 0];
6546
+ return listenedNamespace !== void 0 && eventNamespace !== void 0 && listenedNamespace === eventNamespace || listenedNamespace !== void 0 && eventNamespace === void 0 && listenedNamespace === event.behaviorEvent.type ? !0 : behavior.on === event.behaviorEvent.type;
6547
+ });
6556
6548
  if (eventBehaviors.length === 0) {
6557
6549
  if (defaultActionCallback) {
6558
6550
  withApplyingBehaviorActions(event.editor, () => {
@@ -7738,6 +7730,7 @@ export {
7738
7730
  defineSchema,
7739
7731
  fromSlateValue,
7740
7732
  getEditorSnapshot,
7733
+ getNodeBlock,
7741
7734
  isEqualToEmptyEditor,
7742
7735
  moveRangeByOperation,
7743
7736
  toPortableTextRange,