@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
@@ -312,6 +312,65 @@ function fromSlateValue(value, textBlockType, keyMap = {}) {
312
312
  function isEqualToEmptyEditor(children, schemaTypes) {
313
313
  return children === void 0 || children && Array.isArray(children) && children.length === 0 || children && Array.isArray(children) && children.length === 1 && slate.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 && slate.Text.isText(children[0].children[0]) && children[0].children[0]._type === "span" && !children[0].children[0].marks?.join("") && children[0].children[0].text === "";
314
314
  }
315
+ function getFocusBlock({
316
+ editor
317
+ }) {
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];
322
+ }
323
+ function getFocusChild({
324
+ editor
325
+ }) {
326
+ const [focusBlock, focusBlockPath] = getFocusBlock({
327
+ editor
328
+ }), childIndex = editor.selection?.focus.path.at(1);
329
+ if (!focusBlock || !focusBlockPath || childIndex === void 0)
330
+ return [void 0, void 0];
331
+ const focusChild = slate.Node.child(focusBlock, childIndex);
332
+ return focusChild ? [focusChild, [...focusBlockPath, childIndex]] : [void 0, void 0];
333
+ }
334
+ function getLastBlock({
335
+ editor
336
+ }) {
337
+ return Array.from(slate.Editor.nodes(editor, {
338
+ match: (n) => !slate.Editor.isEditor(n),
339
+ at: [],
340
+ reverse: !0
341
+ })).at(0) ?? [void 0, void 0];
342
+ }
343
+ function getNodeBlock({
344
+ editor,
345
+ schema: schema2,
346
+ node
347
+ }) {
348
+ if (slate.Editor.isEditor(node))
349
+ return;
350
+ if (isBlockElement(schema2, node))
351
+ return elementToBlock({
352
+ schema: schema2,
353
+ element: node
354
+ });
355
+ const parent = Array.from(slate.Editor.nodes(editor, {
356
+ mode: "highest",
357
+ at: [],
358
+ match: (n) => isBlockElement(schema2, n) && n.children.some((child) => child._key === node._key)
359
+ })).at(0)?.at(0);
360
+ return slate.Element.isElement(parent) ? elementToBlock({
361
+ schema: schema2,
362
+ element: parent
363
+ }) : void 0;
364
+ }
365
+ function elementToBlock({
366
+ schema: schema2,
367
+ element
368
+ }) {
369
+ return fromSlateValue([element], schema2.block.name)?.at(0);
370
+ }
371
+ function isBlockElement(schema2, node) {
372
+ return slate.Element.isElement(node) && (schema2.block.name === node._type || schema2.blockObjects.some((blockObject) => blockObject.name === node._type));
373
+ }
315
374
  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 = () => {
316
375
  const editor = React.useContext(PortableTextEditorContext);
317
376
  if (!editor)
@@ -3731,10 +3790,10 @@ function createEditableAPI(editor, editorActor) {
3731
3790
  insertBlock: (type, value) => (editorActor.send({
3732
3791
  type: "behavior event",
3733
3792
  behaviorEvent: {
3734
- type: "insert.block object",
3735
- blockObject: {
3736
- name: type.name,
3737
- value
3793
+ type: "insert.block",
3794
+ block: {
3795
+ _type: type.name,
3796
+ ...value || {}
3738
3797
  },
3739
3798
  placement: "auto"
3740
3799
  },
@@ -4216,11 +4275,11 @@ const addAnnotationActionImplementation = ({
4216
4275
  }) => {
4217
4276
  const range = toSlateRange({
4218
4277
  anchor: {
4219
- path: action.blockPath,
4278
+ path: action.at,
4220
4279
  offset: 0
4221
4280
  },
4222
4281
  focus: {
4223
- path: action.blockPath,
4282
+ path: action.at,
4224
4283
  offset: 0
4225
4284
  }
4226
4285
  }, action.editor);
@@ -4271,36 +4330,7 @@ const addAnnotationActionImplementation = ({
4271
4330
  action
4272
4331
  }) => {
4273
4332
  console.warn(`Deserialization of ${action.mimeType} failed with reason "${action.reason}"`);
4274
- };
4275
- function getFocusBlock({
4276
- editor
4277
- }) {
4278
- return editor.selection ? Array.from(slate.Editor.nodes(editor, {
4279
- at: editor.selection.focus.path.slice(0, 1),
4280
- match: (n) => !slate.Editor.isEditor(n)
4281
- })).at(0) ?? [void 0, void 0] : [void 0, void 0];
4282
- }
4283
- function getFocusChild({
4284
- editor
4285
- }) {
4286
- const [focusBlock, focusBlockPath] = getFocusBlock({
4287
- editor
4288
- }), childIndex = editor.selection?.focus.path.at(1);
4289
- if (!focusBlock || !focusBlockPath || childIndex === void 0)
4290
- return [void 0, void 0];
4291
- const focusChild = slate.Node.child(focusBlock, childIndex);
4292
- return focusChild ? [focusChild, [...focusBlockPath, childIndex]] : [void 0, void 0];
4293
- }
4294
- function getLastBlock({
4295
- editor
4296
- }) {
4297
- return Array.from(slate.Editor.nodes(editor, {
4298
- match: (n) => !slate.Editor.isEditor(n),
4299
- at: [],
4300
- reverse: !0
4301
- })).at(0) ?? [void 0, void 0];
4302
- }
4303
- const insertBlockActionImplementation = ({
4333
+ }, insertBlockActionImplementation = ({
4304
4334
  context,
4305
4335
  action
4306
4336
  }) => {
@@ -4588,15 +4618,17 @@ const selectActionImplementation = ({
4588
4618
  editor: action.editor,
4589
4619
  schema: context.schema
4590
4620
  }), index++;
4591
- } else
4621
+ } else {
4622
+ let index = 0;
4592
4623
  for (const block of fragment)
4593
4624
  insertBlock({
4594
4625
  block,
4595
- placement: "auto",
4626
+ placement: index === 0 ? "auto" : "after",
4596
4627
  select: "end",
4597
4628
  editor: action.editor,
4598
4629
  schema: context.schema
4599
- });
4630
+ }), index++;
4631
+ }
4600
4632
  }, deserializationSuccessActionImplementation = ({
4601
4633
  context,
4602
4634
  action
@@ -4682,53 +4714,10 @@ const selectActionImplementation = ({
4682
4714
  text: action.text,
4683
4715
  marks: [...annotations?.map((annotation) => annotation._key) ?? [], ...action.decorators ?? []]
4684
4716
  });
4685
- }, insertBlockObjectActionImplementation = ({
4686
- context,
4687
- action
4688
- }) => {
4689
- insertBlockActionImplementation({
4690
- context,
4691
- action: {
4692
- block: {
4693
- _key: context.keyGenerator(),
4694
- _type: action.blockObject.name,
4695
- ...action.blockObject.value ? action.blockObject.value : {}
4696
- },
4697
- editor: action.editor,
4698
- placement: action.placement
4699
- }
4700
- });
4701
4717
  }, insertTextActionImplementation = ({
4702
4718
  action
4703
4719
  }) => {
4704
4720
  action.editor.insertText(action.text);
4705
- }, insertTextBlockActionImplementation = ({
4706
- context,
4707
- action
4708
- }) => {
4709
- insertBlockActionImplementation({
4710
- context,
4711
- action: {
4712
- block: {
4713
- _key: context.keyGenerator(),
4714
- _type: context.schema.block.name,
4715
- children: action.textBlock?.children?.map((child) => ({
4716
- ...child,
4717
- _key: context.keyGenerator()
4718
- })) ?? [{
4719
- _type: context.schema.span.name,
4720
- _key: context.keyGenerator(),
4721
- text: "",
4722
- marks: []
4723
- }],
4724
- markDefs: [],
4725
- style: context.schema.styles[0].value ?? "normal"
4726
- },
4727
- editor: action.editor,
4728
- placement: action.placement,
4729
- select: "start"
4730
- }
4731
- });
4732
4721
  }, moveBlockActionImplementation = ({
4733
4722
  action
4734
4723
  }) => {
@@ -4834,13 +4823,11 @@ const selectActionImplementation = ({
4834
4823
  "history.undo": historyUndoActionImplementation,
4835
4824
  "insert.block": insertBlockActionImplementation,
4836
4825
  "insert.blocks": insertBlocksActionImplementation,
4837
- "insert.block object": insertBlockObjectActionImplementation,
4838
4826
  "insert.break": insertBreakActionImplementation,
4839
4827
  "insert.inline object": insertInlineObjectActionImplementation,
4840
4828
  "insert.soft break": insertSoftBreakActionImplementation,
4841
4829
  "insert.span": insertSpanActionImplementation,
4842
4830
  "insert.text": insertTextActionImplementation,
4843
- "insert.text block": insertTextBlockActionImplementation,
4844
4831
  effect: effectActionImplementation,
4845
4832
  "list item.add": addListItemActionImplementation,
4846
4833
  "list item.remove": removeListItemActionImplementation,
@@ -5024,13 +5011,6 @@ function performAction({
5024
5011
  });
5025
5012
  break;
5026
5013
  }
5027
- case "insert.block object": {
5028
- behaviorActionImplementations["insert.block object"]({
5029
- context,
5030
- action
5031
- });
5032
- break;
5033
- }
5034
5014
  case "insert.inline object": {
5035
5015
  behaviorActionImplementations["insert.inline object"]({
5036
5016
  context,
@@ -5066,13 +5046,6 @@ function performAction({
5066
5046
  });
5067
5047
  break;
5068
5048
  }
5069
- case "insert.text block": {
5070
- behaviorActionImplementations["insert.text block"]({
5071
- context,
5072
- action
5073
- });
5074
- break;
5075
- }
5076
5049
  case "list item.add": {
5077
5050
  behaviorActionImplementations["list item.add"]({
5078
5051
  context,
@@ -5207,6 +5180,20 @@ function createWithEventListeners(editorActor, subscriptions) {
5207
5180
  editor
5208
5181
  });
5209
5182
  break;
5183
+ case "insert.block object":
5184
+ editorActor.send({
5185
+ type: "behavior event",
5186
+ behaviorEvent: {
5187
+ type: "insert.block",
5188
+ block: {
5189
+ _type: event.blockObject.name,
5190
+ ...event.blockObject.value ?? {}
5191
+ },
5192
+ placement: event.placement
5193
+ },
5194
+ editor
5195
+ });
5196
+ break;
5210
5197
  default:
5211
5198
  editorActor.send({
5212
5199
  type: "behavior event",
@@ -6355,7 +6342,7 @@ const keyIs = {
6355
6342
  originEvent
6356
6343
  }) => [...draggingEntireBlocks ? draggedBlocks.map((block) => behavior_core.raise({
6357
6344
  type: "delete.block",
6358
- blockPath: block.path
6345
+ at: block.path
6359
6346
  })) : [behavior_core.raise({
6360
6347
  type: "delete",
6361
6348
  selection: dragOrigin.selection
@@ -6527,7 +6514,12 @@ const editorMachine = xstate.setup({
6527
6514
  const defaultAction = event.type === "custom behavior event" || behavior_core.isClipboardBehaviorEvent(event.behaviorEvent) || behavior_core.isDragBehaviorEvent(event.behaviorEvent) || behavior_core.isInputBehaviorEvent(event.behaviorEvent) || behavior_core.isKeyboardBehaviorEvent(event.behaviorEvent) || behavior_core.isMouseBehaviorEvent(event.behaviorEvent) || event.behaviorEvent.type === "deserialize" || event.behaviorEvent.type === "serialize" ? void 0 : {
6528
6515
  ...event.behaviorEvent,
6529
6516
  editor: event.editor
6530
- }, defaultActionCallback = event.type === "behavior event" ? event.defaultActionCallback : void 0, eventBehaviors = [...context.behaviors.values(), ...defaultBehaviors].filter((behavior) => behavior.on === "*" ? !0 : behavior_core.isClipboardBehaviorEvent(event.behaviorEvent) ? behavior.on === "clipboard.*" || behavior.on === event.behaviorEvent.type : behavior_core.isDragBehaviorEvent(event.behaviorEvent) ? behavior.on === "drag.*" || behavior.on === event.behaviorEvent.type : behavior_core.isInputBehaviorEvent(event.behaviorEvent) ? behavior.on === "input.*" : behavior_core.isKeyboardBehaviorEvent(event.behaviorEvent) ? behavior.on === "keyboard.*" || behavior.on === event.behaviorEvent.type : behavior_core.isMouseBehaviorEvent(event.behaviorEvent) && behavior.on === "mouse.*" || behavior.on === event.behaviorEvent.type);
6517
+ }, defaultActionCallback = event.type === "behavior event" ? event.defaultActionCallback : void 0, eventBehaviors = [...context.behaviors.values(), ...defaultBehaviors].filter((behavior) => {
6518
+ if (behavior.on === "*")
6519
+ return !0;
6520
+ const [listenedNamespace] = behavior.on.includes("*") && behavior.on.includes(".") ? behavior.on.split(".") : [void 0], [eventNamespace] = event.behaviorEvent.type.includes(".") ? event.behaviorEvent.type.split(".") : [void 0];
6521
+ 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;
6522
+ });
6531
6523
  if (eventBehaviors.length === 0) {
6532
6524
  if (defaultActionCallback) {
6533
6525
  withApplyingBehaviorActions(event.editor, () => {
@@ -7712,6 +7704,7 @@ exports.defaultKeyGenerator = defaultKeyGenerator;
7712
7704
  exports.defineSchema = defineSchema;
7713
7705
  exports.fromSlateValue = fromSlateValue;
7714
7706
  exports.getEditorSnapshot = getEditorSnapshot;
7707
+ exports.getNodeBlock = getNodeBlock;
7715
7708
  exports.isEqualToEmptyEditor = isEqualToEmptyEditor;
7716
7709
  exports.moveRangeByOperation = moveRangeByOperation;
7717
7710
  exports.toPortableTextRange = toPortableTextRange;