@portabletext/editor 1.44.16 → 1.45.0

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 (63) hide show
  1. package/lib/_chunks-cjs/behavior.core.cjs +5 -5
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
  3. package/lib/_chunks-cjs/behavior.markdown.cjs +28 -22
  4. package/lib/_chunks-cjs/behavior.markdown.cjs.map +1 -1
  5. package/lib/_chunks-cjs/editor-provider.cjs +62 -38
  6. package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
  7. package/lib/_chunks-cjs/parse-blocks.cjs +74 -22
  8. package/lib/_chunks-cjs/parse-blocks.cjs.map +1 -1
  9. package/lib/_chunks-es/behavior.core.js +5 -5
  10. package/lib/_chunks-es/behavior.core.js.map +1 -1
  11. package/lib/_chunks-es/behavior.markdown.js +28 -22
  12. package/lib/_chunks-es/behavior.markdown.js.map +1 -1
  13. package/lib/_chunks-es/editor-provider.js +63 -39
  14. package/lib/_chunks-es/editor-provider.js.map +1 -1
  15. package/lib/_chunks-es/parse-blocks.js +75 -23
  16. package/lib/_chunks-es/parse-blocks.js.map +1 -1
  17. package/lib/behaviors/index.cjs +18 -14
  18. package/lib/behaviors/index.cjs.map +1 -1
  19. package/lib/behaviors/index.d.cts +53 -41
  20. package/lib/behaviors/index.d.ts +53 -41
  21. package/lib/behaviors/index.js +18 -14
  22. package/lib/behaviors/index.js.map +1 -1
  23. package/lib/index.cjs +1 -1
  24. package/lib/index.cjs.map +1 -1
  25. package/lib/index.d.cts +28 -9
  26. package/lib/index.d.ts +28 -9
  27. package/lib/index.js +1 -1
  28. package/lib/index.js.map +1 -1
  29. package/lib/plugins/index.cjs +7 -7
  30. package/lib/plugins/index.cjs.map +1 -1
  31. package/lib/plugins/index.d.cts +8 -6
  32. package/lib/plugins/index.d.ts +8 -6
  33. package/lib/plugins/index.js +7 -7
  34. package/lib/plugins/index.js.map +1 -1
  35. package/lib/selectors/index.d.cts +8 -6
  36. package/lib/selectors/index.d.ts +8 -6
  37. package/lib/utils/index.d.cts +8 -6
  38. package/lib/utils/index.d.ts +8 -6
  39. package/package.json +3 -3
  40. package/src/behavior-actions/behavior.action.annotation.add.ts +26 -5
  41. package/src/behavior-actions/behavior.action.decorator.add.ts +4 -4
  42. package/src/behavior-actions/behavior.action.delete.text.ts +1 -4
  43. package/src/behavior-actions/behavior.action.delete.ts +2 -2
  44. package/src/behavior-actions/behavior.action.insert-inline-object.ts +14 -13
  45. package/src/behavior-actions/behavior.action.select.ts +1 -1
  46. package/src/behaviors/behavior.abstract.decorator.ts +2 -2
  47. package/src/behaviors/behavior.abstract.select.ts +2 -2
  48. package/src/behaviors/behavior.core.annotations.ts +1 -1
  49. package/src/behaviors/behavior.core.block-objects.ts +4 -4
  50. package/src/behaviors/behavior.decorator-pair.ts +3 -3
  51. package/src/behaviors/behavior.default.ts +4 -4
  52. package/src/behaviors/behavior.emoji-picker.ts +18 -14
  53. package/src/behaviors/behavior.markdown.ts +28 -22
  54. package/src/behaviors/behavior.types.event.ts +11 -6
  55. package/src/converters/converter.portable-text.deserialize.test.ts +12 -3
  56. package/src/converters/converter.text-html.deserialize.test.ts +3 -1
  57. package/src/editor/Editable.tsx +1 -1
  58. package/src/editor/define-schema.ts +29 -5
  59. package/src/editor/plugins/__tests__/withEditableAPIInsert.test.tsx +2 -2
  60. package/src/editor/plugins/create-with-event-listeners.ts +1 -1
  61. package/src/internal-utils/parse-blocks.ts +109 -39
  62. package/src/plugins/plugin.decorator-shortcut.ts +3 -3
  63. package/src/plugins/plugin.one-line.tsx +1 -1
@@ -3151,6 +3151,18 @@ const addAnnotationActionImplementation = ({
3151
3151
  context,
3152
3152
  action
3153
3153
  }) => {
3154
+ const parsedAnnotation = parseBlocks.parseAnnotation({
3155
+ annotation: {
3156
+ _type: action.annotation.name,
3157
+ ...action.annotation.value
3158
+ },
3159
+ context,
3160
+ options: {
3161
+ refreshKeys: !1
3162
+ }
3163
+ });
3164
+ if (!parsedAnnotation)
3165
+ throw new Error(`Failed to parse annotation ${JSON.stringify(action.annotation)}`);
3154
3166
  const editor = action.editor;
3155
3167
  if (!editor.selection || slate.Range.isCollapsed(editor.selection))
3156
3168
  return;
@@ -3160,15 +3172,15 @@ const addAnnotationActionImplementation = ({
3160
3172
  match: (node) => editor.isTextBlock(node),
3161
3173
  reverse: slate.Range.isBackward(editor.selection)
3162
3174
  });
3175
+ let blockIndex = 0;
3163
3176
  for (const [block, blockPath] of selectedBlocks) {
3164
3177
  if (block.children.length === 0 || block.children.length === 1 && block.children[0].text === "")
3165
3178
  continue;
3166
- const annotationKey = context.keyGenerator(), markDefs = block.markDefs ?? [];
3167
- markDefs.find((markDef) => markDef._type === action.annotation.name && markDef._key === annotationKey) === void 0 && (slate.Transforms.setNodes(editor, {
3179
+ const annotationKey = blockIndex === 0 ? parsedAnnotation._key : context.keyGenerator(), markDefs = block.markDefs ?? [];
3180
+ markDefs.find((markDef) => markDef._type === parsedAnnotation._type && markDef._key === annotationKey) === void 0 && (slate.Transforms.setNodes(editor, {
3168
3181
  markDefs: [...markDefs, {
3169
- _type: action.annotation.name,
3170
- _key: annotationKey,
3171
- ...action.annotation.value
3182
+ ...parsedAnnotation,
3183
+ _key: annotationKey
3172
3184
  }]
3173
3185
  }, {
3174
3186
  at: blockPath
@@ -3184,7 +3196,7 @@ const addAnnotationActionImplementation = ({
3184
3196
  for (const [span, path] of children) {
3185
3197
  if (!editor.isTextSpan(span) || !slate.Range.includes(editor.selection, path))
3186
3198
  continue;
3187
- const marks = span.marks ?? [], existingSameTypeAnnotations = marks.filter((mark) => markDefs.some((markDef) => markDef._key === mark && markDef._type === action.annotation.name));
3199
+ const marks = span.marks ?? [], existingSameTypeAnnotations = marks.filter((mark) => markDefs.some((markDef) => markDef._key === mark && markDef._type === parsedAnnotation._type));
3188
3200
  slate.Transforms.setNodes(editor, {
3189
3201
  marks: [...marks.filter((mark) => !existingSameTypeAnnotations.includes(mark)), annotationKey]
3190
3202
  }, {
@@ -3195,6 +3207,7 @@ const addAnnotationActionImplementation = ({
3195
3207
  _key: span._key
3196
3208
  }];
3197
3209
  }
3210
+ blockIndex++;
3198
3211
  }
3199
3212
  return markDefPath && spanPath && (paths = {
3200
3213
  markDefPath,
@@ -3607,13 +3620,13 @@ const decoratorAddActionImplementation = ({
3607
3620
  context,
3608
3621
  action
3609
3622
  }) => {
3610
- const editor = action.editor, mark = action.decorator, value = fromSlateValue(editor.children, context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), manualAnchor = action.offsets?.anchor ? util_sliceBlocks.blockOffsetToSpanSelectionPoint({
3623
+ const editor = action.editor, mark = action.decorator, value = fromSlateValue(editor.children, context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(editor)), manualAnchor = action.at?.anchor ? util_sliceBlocks.blockOffsetToSpanSelectionPoint({
3611
3624
  value,
3612
- blockOffset: action.offsets.anchor,
3625
+ blockOffset: action.at.anchor,
3613
3626
  direction: "backward"
3614
- }) : void 0, manualFocus = action.offsets?.focus ? util_sliceBlocks.blockOffsetToSpanSelectionPoint({
3627
+ }) : void 0, manualFocus = action.at?.focus ? util_sliceBlocks.blockOffsetToSpanSelectionPoint({
3615
3628
  value,
3616
- blockOffset: action.offsets.focus,
3629
+ blockOffset: action.at.focus,
3617
3630
  direction: "forward"
3618
3631
  }) : void 0, manualSelection = manualAnchor && manualFocus ? {
3619
3632
  anchor: manualAnchor,
@@ -3715,9 +3728,9 @@ const decoratorAddActionImplementation = ({
3715
3728
  }, deleteActionImplementation = ({
3716
3729
  action
3717
3730
  }) => {
3718
- const range = toSlateRange(action.selection, action.editor);
3731
+ const range = toSlateRange(action.at, action.editor);
3719
3732
  if (!range)
3720
- throw new Error(`Failed to get Slate Range for selection ${JSON.stringify(action.selection)}`);
3733
+ throw new Error(`Failed to get Slate Range for selection ${JSON.stringify(action.at)}`);
3721
3734
  action.editor.delete({
3722
3735
  at: range
3723
3736
  });
@@ -3755,10 +3768,7 @@ const decoratorAddActionImplementation = ({
3755
3768
  }) => {
3756
3769
  const value = fromSlateValue(action.editor.children, context.schema.block.name, KEY_TO_VALUE_ELEMENT.get(action.editor)), selection = util_selectionPointToBlockOffset.blockOffsetsToSelection({
3757
3770
  value,
3758
- offsets: {
3759
- anchor: action.anchor,
3760
- focus: action.focus
3761
- }
3771
+ offsets: action.at
3762
3772
  });
3763
3773
  if (!selection)
3764
3774
  throw new Error("Unable to find selection from block offsets");
@@ -3793,10 +3803,18 @@ const decoratorAddActionImplementation = ({
3793
3803
  context,
3794
3804
  action
3795
3805
  }) => {
3796
- if (!context.schema.inlineObjects.some((inlineObject) => inlineObject.name === action.inlineObject.name)) {
3797
- console.error("Unable to insert unknown inline object");
3798
- return;
3799
- }
3806
+ const parsedInlineObject = parseBlocks.parseInlineObject({
3807
+ context,
3808
+ inlineObject: {
3809
+ _type: action.inlineObject.name,
3810
+ ...action.inlineObject.value ?? {}
3811
+ },
3812
+ options: {
3813
+ refreshKeys: !1
3814
+ }
3815
+ });
3816
+ if (!parsedInlineObject)
3817
+ throw new Error(`Failed to parse inline object ${JSON.stringify(action.inlineObject)}`);
3800
3818
  if (!action.editor.selection) {
3801
3819
  console.error("Unable to insert inline object without selection");
3802
3820
  return;
@@ -3812,11 +3830,7 @@ const decoratorAddActionImplementation = ({
3812
3830
  const child = toSlateValue([{
3813
3831
  _type: context.schema.block.name,
3814
3832
  _key: context.keyGenerator(),
3815
- children: [{
3816
- _type: action.inlineObject.name,
3817
- _key: context.keyGenerator(),
3818
- ...action.inlineObject.value ?? {}
3819
- }]
3833
+ children: [parsedInlineObject]
3820
3834
  }], {
3821
3835
  schemaTypes: context.schema
3822
3836
  }).at(0)?.children.at(0);
@@ -4012,7 +4026,7 @@ const moveBlockActionImplementation = ({
4012
4026
  }, selectActionImplementation = ({
4013
4027
  action
4014
4028
  }) => {
4015
- const newSelection = toSlateRange(action.selection, action.editor);
4029
+ const newSelection = toSlateRange(action.at, action.editor);
4016
4030
  newSelection ? slate.Transforms.select(action.editor, newSelection) : slate.Transforms.deselect(action.editor);
4017
4031
  }, splitBlockActionImplementation = ({
4018
4032
  context,
@@ -4404,7 +4418,7 @@ function createWithEventListeners(editorActor) {
4404
4418
  type: "behavior event",
4405
4419
  behaviorEvent: {
4406
4420
  type: "select",
4407
- selection: slateRangeToSelection({
4421
+ at: slateRangeToSelection({
4408
4422
  schema: editorActor.getSnapshot().context.schema,
4409
4423
  editor,
4410
4424
  range
@@ -5137,7 +5151,10 @@ function compileSchemaDefinition(definition) {
5137
5151
  // This avoids the default title which is a title case of the object name
5138
5152
  defaultObjectTitles[blockObject.name]
5139
5153
  ) : blockObject.title,
5140
- fields: []
5154
+ fields: blockObject.fields?.map((field) => ({
5155
+ name: field.name,
5156
+ type: field.type
5157
+ })) ?? []
5141
5158
  })) ?? [], inlineObjects = definition?.inlineObjects?.map((inlineObject) => types.defineType({
5142
5159
  type: "object",
5143
5160
  // Very naive way to work around `SanitySchema.compile` adding default
@@ -5147,7 +5164,10 @@ function compileSchemaDefinition(definition) {
5147
5164
  // This avoids the default title which is a title case of the object name
5148
5165
  defaultObjectTitles[inlineObject.name]
5149
5166
  ) : inlineObject.title,
5150
- fields: []
5167
+ fields: inlineObject.fields?.map((field) => ({
5168
+ name: field.name,
5169
+ type: field.type
5170
+ })) ?? []
5151
5171
  })) ?? [], portableTextSchema = types.defineField({
5152
5172
  type: "array",
5153
5173
  name: "portable-text",
@@ -5167,7 +5187,11 @@ function compileSchemaDefinition(definition) {
5167
5187
  annotations: definition?.annotations?.map((annotation) => ({
5168
5188
  name: annotation.name,
5169
5189
  type: "object",
5170
- title: annotation.title
5190
+ title: annotation.title,
5191
+ fields: annotation.fields?.map((field) => ({
5192
+ name: field.name,
5193
+ type: field.type
5194
+ })) ?? []
5171
5195
  })) ?? []
5172
5196
  },
5173
5197
  lists: definition?.lists?.map((list) => ({
@@ -5240,9 +5264,9 @@ const abstractAnnotationBehaviors = [behavior_core.defineBehavior({
5240
5264
  snapshot,
5241
5265
  event
5242
5266
  }) => {
5243
- const manualSelection = event.offsets ? util_selectionPointToBlockOffset.blockOffsetsToSelection({
5267
+ const manualSelection = event.at ? util_selectionPointToBlockOffset.blockOffsetsToSelection({
5244
5268
  value: snapshot.context.value,
5245
- offsets: event.offsets
5269
+ offsets: event.at
5246
5270
  }) : null;
5247
5271
  return manualSelection ? !selector_isSelectingEntireBlocks.isActiveDecorator(event.decorator)({
5248
5272
  ...snapshot,
@@ -5492,7 +5516,7 @@ const abstractAnnotationBehaviors = [behavior_core.defineBehavior({
5492
5516
  selection
5493
5517
  }) => [behavior_core.raise({
5494
5518
  type: "select",
5495
- selection
5519
+ at: selection
5496
5520
  })]]
5497
5521
  }), behavior_core.defineBehavior({
5498
5522
  on: "select.next block",
@@ -5515,7 +5539,7 @@ const abstractAnnotationBehaviors = [behavior_core.defineBehavior({
5515
5539
  selection
5516
5540
  }) => [behavior_core.raise({
5517
5541
  type: "select",
5518
- selection
5542
+ at: selection
5519
5543
  })]]
5520
5544
  })], abstractStyleBehaviors = [behavior_core.defineBehavior({
5521
5545
  on: "style.add",
@@ -5693,7 +5717,7 @@ const abstractAnnotationBehaviors = [behavior_core.defineBehavior({
5693
5717
  originEvent: event
5694
5718
  }), behavior_core.raise({
5695
5719
  type: "delete",
5696
- selection
5720
+ at: selection
5697
5721
  })]]
5698
5722
  }),
5699
5723
  behavior_core.defineBehavior({
@@ -5752,7 +5776,7 @@ const abstractAnnotationBehaviors = [behavior_core.defineBehavior({
5752
5776
  event
5753
5777
  }) => [behavior_core.raise({
5754
5778
  type: "select",
5755
- selection: event.position.selection
5779
+ at: event.position.selection
5756
5780
  }), behavior_core.raise({
5757
5781
  type: "deserialize",
5758
5782
  originEvent: event
@@ -5802,7 +5826,7 @@ const abstractAnnotationBehaviors = [behavior_core.defineBehavior({
5802
5826
  at: block.path
5803
5827
  })) : [behavior_core.raise({
5804
5828
  type: "delete",
5805
- selection: dragOrigin.selection
5829
+ at: dragOrigin.selection
5806
5830
  })], behavior_core.raise({
5807
5831
  type: "insert.blocks",
5808
5832
  blocks: event.data,
@@ -5896,7 +5920,7 @@ const abstractAnnotationBehaviors = [behavior_core.defineBehavior({
5896
5920
  selection
5897
5921
  }) => [behavior_core.raise({
5898
5922
  type: "delete",
5899
- selection
5923
+ at: selection
5900
5924
  }), behavior_core.raise({
5901
5925
  type: "deserialize",
5902
5926
  originEvent: event