@portabletext/editor 1.23.0 → 1.24.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 (53) hide show
  1. package/lib/_chunks-cjs/behavior.core.cjs +65 -2
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
  3. package/lib/_chunks-cjs/util.slice-blocks.cjs +23 -9
  4. package/lib/_chunks-cjs/util.slice-blocks.cjs.map +1 -1
  5. package/lib/_chunks-es/behavior.core.js +65 -2
  6. package/lib/_chunks-es/behavior.core.js.map +1 -1
  7. package/lib/_chunks-es/util.slice-blocks.js +23 -9
  8. package/lib/_chunks-es/util.slice-blocks.js.map +1 -1
  9. package/lib/behaviors/index.d.cts +1111 -44
  10. package/lib/behaviors/index.d.ts +1111 -44
  11. package/lib/index.cjs +535 -333
  12. package/lib/index.cjs.map +1 -1
  13. package/lib/index.d.cts +158 -1
  14. package/lib/index.d.ts +158 -1
  15. package/lib/index.js +539 -335
  16. package/lib/index.js.map +1 -1
  17. package/lib/selectors/index.d.cts +73 -0
  18. package/lib/selectors/index.d.ts +73 -0
  19. package/package.json +11 -10
  20. package/src/behavior-actions/behavior.action.data-transfer-set.ts +7 -0
  21. package/src/behavior-actions/behavior.action.insert-blocks.ts +61 -0
  22. package/src/behavior-actions/behavior.actions.ts +75 -0
  23. package/src/behaviors/behavior.core.deserialize.ts +46 -0
  24. package/src/behaviors/behavior.core.serialize.ts +44 -0
  25. package/src/behaviors/behavior.core.ts +7 -0
  26. package/src/behaviors/behavior.types.ts +39 -2
  27. package/src/converters/converter.json.ts +53 -0
  28. package/src/converters/converter.portable-text.deserialize.test.ts +686 -0
  29. package/src/converters/converter.portable-text.ts +59 -0
  30. package/src/converters/converter.text-html.deserialize.test.ts +349 -0
  31. package/src/converters/converter.text-html.serialize.test.ts +233 -0
  32. package/src/converters/converter.text-html.ts +61 -0
  33. package/src/converters/converter.text-plain.test.ts +241 -0
  34. package/src/converters/converter.text-plain.ts +91 -0
  35. package/src/converters/converter.ts +65 -0
  36. package/src/converters/converters.ts +11 -0
  37. package/src/editor/Editable.tsx +3 -13
  38. package/src/editor/create-editor.ts +2 -0
  39. package/src/editor/editor-machine.ts +18 -1
  40. package/src/editor/editor-selector.ts +1 -0
  41. package/src/editor/editor-snapshot.ts +5 -0
  42. package/src/editor/plugins/create-with-event-listeners.ts +44 -0
  43. package/src/internal-utils/asserters.ts +9 -0
  44. package/src/internal-utils/mime-type.ts +1 -0
  45. package/src/internal-utils/parse-blocks.ts +136 -0
  46. package/src/internal-utils/test-key-generator.ts +9 -0
  47. package/src/selectors/selector.get-selected-spans.test.ts +1 -0
  48. package/src/selectors/selector.get-selection-text.test.ts +1 -0
  49. package/src/selectors/selector.is-active-decorator.test.ts +1 -0
  50. package/src/utils/util.slice-blocks.test.ts +87 -0
  51. package/src/utils/util.slice-blocks.ts +27 -10
  52. package/src/editor/plugins/__tests__/createWithInsertData.test.tsx +0 -181
  53. package/src/editor/plugins/createWithInsertData.ts +0 -425
package/lib/index.d.ts CHANGED
@@ -98,7 +98,11 @@ export declare type BaseDefinition = {
98
98
  export declare type Behavior<
99
99
  TBehaviorEventType extends BehaviorEvent['type'] = BehaviorEvent['type'],
100
100
  TGuardResponse = true,
101
- TBehaviorEvent extends BehaviorEvent = BehaviorEvent,
101
+ TBehaviorEvent extends BehaviorEvent = PickFromUnion<
102
+ BehaviorEvent,
103
+ 'type',
104
+ TBehaviorEventType
105
+ >,
102
106
  > = {
103
107
  /**
104
108
  * The internal editor event that triggers this behavior.
@@ -364,6 +368,43 @@ export declare type ConnectionChange = {
364
368
  value: 'online' | 'offline'
365
369
  }
366
370
 
371
+ declare type Converter<TMIMEType extends MIMEType = MIMEType> = {
372
+ mimeType: TMIMEType
373
+ serialize: Serializer<TMIMEType>
374
+ deserialize: Deserializer<TMIMEType>
375
+ }
376
+
377
+ declare type ConverterEvent<TMIMEType extends MIMEType = MIMEType> =
378
+ | {
379
+ type: 'serialize'
380
+ originEvent: 'copy' | 'cut' | 'unknown'
381
+ }
382
+ | {
383
+ type: 'serialization.failure'
384
+ mimeType: TMIMEType
385
+ reason: string
386
+ }
387
+ | {
388
+ type: 'serialization.success'
389
+ data: string
390
+ mimeType: TMIMEType
391
+ originEvent: 'copy' | 'cut' | 'unknown'
392
+ }
393
+ | {
394
+ type: 'deserialize'
395
+ data: string
396
+ }
397
+ | {
398
+ type: 'deserialization.failure'
399
+ mimeType: TMIMEType
400
+ reason: string
401
+ }
402
+ | {
403
+ type: 'deserialization.success'
404
+ data: Array<PortableTextBlock>
405
+ mimeType: TMIMEType
406
+ }
407
+
367
408
  /**
368
409
  * @beta
369
410
  */
@@ -402,6 +443,18 @@ export declare function defineSchema<
402
443
  const TSchemaDefinition extends SchemaDefinition,
403
444
  >(definition: TSchemaDefinition): TSchemaDefinition
404
445
 
446
+ declare type Deserializer<TMIMEType extends MIMEType> = ({
447
+ context,
448
+ event,
449
+ }: {
450
+ context: EditorContext
451
+ event: PickFromUnion<ConverterEvent<TMIMEType>, 'type', 'deserialize'>
452
+ }) => PickFromUnion<
453
+ ConverterEvent<TMIMEType>,
454
+ 'type',
455
+ 'deserialization.success' | 'deserialization.failure'
456
+ >
457
+
405
458
  /** @beta */
406
459
  export declare interface EditableAPI {
407
460
  activeAnnotations: () => PortableTextObject[]
@@ -569,6 +622,7 @@ export declare type EditorConfig = {
569
622
  */
570
623
  export declare type EditorContext = {
571
624
  activeDecorators: Array<string>
625
+ converters: Array<Converter>
572
626
  keyGenerator: () => string
573
627
  schema: EditorSchema
574
628
  selection: EditorSelection
@@ -679,6 +733,7 @@ export declare function EditorEventListener(props: {
679
733
  export declare const editorMachine: StateMachine<
680
734
  {
681
735
  behaviors: Set<Behavior>
736
+ converters: Set<Converter>
682
737
  keyGenerator: () => string
683
738
  pendingEvents: Array<PatchEvent | MutationEvent>
684
739
  schema: EditorSchema
@@ -920,6 +975,7 @@ export declare const editorMachine: StateMachine<
920
975
  string,
921
976
  {
922
977
  behaviors?: Array<Behavior>
978
+ converters?: Array<Converter>
923
979
  keyGenerator: () => string
924
980
  maxBlocks?: number
925
981
  readOnly?: boolean
@@ -1066,6 +1122,7 @@ export declare const editorMachine: StateMachine<
1066
1122
  }
1067
1123
  input: {
1068
1124
  behaviors?: Array<Behavior>
1125
+ converters?: Array<Converter>
1069
1126
  keyGenerator: () => string
1070
1127
  maxBlocks?: number
1071
1128
  readOnly?: boolean
@@ -1076,6 +1133,7 @@ export declare const editorMachine: StateMachine<
1076
1133
  MachineSnapshot<
1077
1134
  {
1078
1135
  behaviors: Set<Behavior>
1136
+ converters: Set<Converter>
1079
1137
  keyGenerator: () => string
1080
1138
  pendingEvents: Array<PatchEvent | MutationEvent>
1081
1139
  schema: EditorSchema
@@ -1413,6 +1471,7 @@ export declare const editorMachine: StateMachine<
1413
1471
  >
1414
1472
  }) => {
1415
1473
  behaviors: Set<Behavior>
1474
+ converters: Set<Converter>
1416
1475
  keyGenerator: () => string
1417
1476
  pendingEvents: never[]
1418
1477
  schema: PortableTextMemberSchemaTypes_2
@@ -1432,6 +1491,7 @@ export declare const editorMachine: StateMachine<
1432
1491
  readonly actions: ActionFunction<
1433
1492
  {
1434
1493
  behaviors: Set<Behavior>
1494
+ converters: Set<Converter>
1435
1495
  keyGenerator: () => string
1436
1496
  pendingEvents: Array<PatchEvent | MutationEvent>
1437
1497
  schema: EditorSchema
@@ -1723,6 +1783,7 @@ export declare const editorMachine: StateMachine<
1723
1783
  readonly actions: ActionFunction<
1724
1784
  {
1725
1785
  behaviors: Set<Behavior>
1786
+ converters: Set<Converter>
1726
1787
  keyGenerator: () => string
1727
1788
  pendingEvents: Array<PatchEvent | MutationEvent>
1728
1789
  schema: EditorSchema
@@ -2014,6 +2075,7 @@ export declare const editorMachine: StateMachine<
2014
2075
  readonly actions: ActionFunction<
2015
2076
  {
2016
2077
  behaviors: Set<Behavior>
2078
+ converters: Set<Converter>
2017
2079
  keyGenerator: () => string
2018
2080
  pendingEvents: Array<PatchEvent | MutationEvent>
2019
2081
  schema: EditorSchema
@@ -2306,6 +2368,7 @@ export declare const editorMachine: StateMachine<
2306
2368
  readonly actions: ActionFunction<
2307
2369
  {
2308
2370
  behaviors: Set<Behavior>
2371
+ converters: Set<Converter>
2309
2372
  keyGenerator: () => string
2310
2373
  pendingEvents: Array<PatchEvent | MutationEvent>
2311
2374
  schema: EditorSchema
@@ -2600,6 +2663,7 @@ export declare const editorMachine: StateMachine<
2600
2663
  ActionFunction<
2601
2664
  {
2602
2665
  behaviors: Set<Behavior>
2666
+ converters: Set<Converter>
2603
2667
  keyGenerator: () => string
2604
2668
  pendingEvents: Array<PatchEvent | MutationEvent>
2605
2669
  schema: EditorSchema
@@ -2781,6 +2845,7 @@ export declare const editorMachine: StateMachine<
2781
2845
  ActionFunction<
2782
2846
  {
2783
2847
  behaviors: Set<Behavior>
2848
+ converters: Set<Converter>
2784
2849
  keyGenerator: () => string
2785
2850
  pendingEvents: Array<PatchEvent | MutationEvent>
2786
2851
  schema: EditorSchema
@@ -3073,6 +3138,7 @@ export declare const editorMachine: StateMachine<
3073
3138
  readonly actions: ActionFunction<
3074
3139
  {
3075
3140
  behaviors: Set<Behavior>
3141
+ converters: Set<Converter>
3076
3142
  keyGenerator: () => string
3077
3143
  pendingEvents: Array<PatchEvent | MutationEvent>
3078
3144
  schema: EditorSchema
@@ -3364,6 +3430,7 @@ export declare const editorMachine: StateMachine<
3364
3430
  readonly actions: ActionFunction<
3365
3431
  {
3366
3432
  behaviors: Set<Behavior>
3433
+ converters: Set<Converter>
3367
3434
  keyGenerator: () => string
3368
3435
  pendingEvents: Array<PatchEvent | MutationEvent>
3369
3436
  schema: EditorSchema
@@ -3655,6 +3722,7 @@ export declare const editorMachine: StateMachine<
3655
3722
  readonly actions: ActionFunction<
3656
3723
  {
3657
3724
  behaviors: Set<Behavior>
3725
+ converters: Set<Converter>
3658
3726
  keyGenerator: () => string
3659
3727
  pendingEvents: Array<PatchEvent | MutationEvent>
3660
3728
  schema: EditorSchema
@@ -3945,6 +4013,7 @@ export declare const editorMachine: StateMachine<
3945
4013
  readonly actions: ActionFunction<
3946
4014
  {
3947
4015
  behaviors: Set<Behavior>
4016
+ converters: Set<Converter>
3948
4017
  keyGenerator: () => string
3949
4018
  pendingEvents: Array<PatchEvent | MutationEvent>
3950
4019
  schema: EditorSchema
@@ -4233,6 +4302,7 @@ export declare const editorMachine: StateMachine<
4233
4302
  readonly actions: ActionFunction<
4234
4303
  {
4235
4304
  behaviors: Set<Behavior>
4305
+ converters: Set<Converter>
4236
4306
  keyGenerator: () => string
4237
4307
  pendingEvents: Array<PatchEvent | MutationEvent>
4238
4308
  schema: EditorSchema
@@ -4526,6 +4596,7 @@ export declare const editorMachine: StateMachine<
4526
4596
  readonly actions: ActionFunction<
4527
4597
  {
4528
4598
  behaviors: Set<Behavior>
4599
+ converters: Set<Converter>
4529
4600
  keyGenerator: () => string
4530
4601
  pendingEvents: Array<PatchEvent | MutationEvent>
4531
4602
  schema: EditorSchema
@@ -4712,6 +4783,7 @@ export declare const editorMachine: StateMachine<
4712
4783
  readonly actions: ActionFunction<
4713
4784
  {
4714
4785
  behaviors: Set<Behavior>
4786
+ converters: Set<Converter>
4715
4787
  keyGenerator: () => string
4716
4788
  pendingEvents: Array<PatchEvent | MutationEvent>
4717
4789
  schema: EditorSchema
@@ -4895,6 +4967,7 @@ export declare const editorMachine: StateMachine<
4895
4967
  readonly actions: ActionFunction<
4896
4968
  {
4897
4969
  behaviors: Set<Behavior>
4970
+ converters: Set<Converter>
4898
4971
  keyGenerator: () => string
4899
4972
  pendingEvents: Array<PatchEvent | MutationEvent>
4900
4973
  schema: EditorSchema
@@ -5093,6 +5166,7 @@ export declare const editorMachine: StateMachine<
5093
5166
  }: GuardArgs<
5094
5167
  {
5095
5168
  behaviors: Set<Behavior>
5169
+ converters: Set<Converter>
5096
5170
  keyGenerator: () => string
5097
5171
  pendingEvents: Array<PatchEvent | MutationEvent>
5098
5172
  schema: EditorSchema
@@ -5114,12 +5188,42 @@ export declare const editorMachine: StateMachine<
5114
5188
  }
5115
5189
  readonly 'read only': {
5116
5190
  readonly on: {
5191
+ readonly 'behavior event': {
5192
+ readonly actions: 'handle behavior event'
5193
+ readonly guard: ({
5194
+ event,
5195
+ }: GuardArgs<
5196
+ {
5197
+ behaviors: Set<Behavior>
5198
+ converters: Set<Converter>
5199
+ keyGenerator: () => string
5200
+ pendingEvents: Array<PatchEvent | MutationEvent>
5201
+ schema: EditorSchema
5202
+ initialReadOnly: boolean
5203
+ maxBlocks: number | undefined
5204
+ selection: EditorSelection
5205
+ value: Array<PortableTextBlock> | undefined
5206
+ },
5207
+ {
5208
+ type: 'behavior event'
5209
+ behaviorEvent:
5210
+ | SyntheticBehaviorEvent
5211
+ | NativeBehaviorEvent
5212
+ editor: PortableTextSlateEditor
5213
+ defaultActionCallback?: () => void
5214
+ nativeEvent?: {
5215
+ preventDefault: () => void
5216
+ }
5217
+ }
5218
+ >) => boolean
5219
+ }
5117
5220
  readonly 'update readOnly': {
5118
5221
  readonly guard: ({
5119
5222
  event,
5120
5223
  }: GuardArgs<
5121
5224
  {
5122
5225
  behaviors: Set<Behavior>
5226
+ converters: Set<Converter>
5123
5227
  keyGenerator: () => string
5124
5228
  pendingEvents: Array<PatchEvent | MutationEvent>
5125
5229
  schema: EditorSchema
@@ -5148,6 +5252,7 @@ export declare const editorMachine: StateMachine<
5148
5252
  }: GuardArgs<
5149
5253
  {
5150
5254
  behaviors: Set<Behavior>
5255
+ converters: Set<Converter>
5151
5256
  keyGenerator: () => string
5152
5257
  pendingEvents: Array<PatchEvent | MutationEvent>
5153
5258
  schema: EditorSchema
@@ -5174,6 +5279,7 @@ export declare const editorMachine: StateMachine<
5174
5279
  readonly actions: ActionFunction<
5175
5280
  {
5176
5281
  behaviors: Set<Behavior>
5282
+ converters: Set<Converter>
5177
5283
  keyGenerator: () => string
5178
5284
  pendingEvents: Array<PatchEvent | MutationEvent>
5179
5285
  schema: EditorSchema
@@ -5478,6 +5584,7 @@ export declare const editorMachine: StateMachine<
5478
5584
  readonly actions: ActionFunction<
5479
5585
  {
5480
5586
  behaviors: Set<Behavior>
5587
+ converters: Set<Converter>
5481
5588
  keyGenerator: () => string
5482
5589
  pendingEvents: Array<PatchEvent | MutationEvent>
5483
5590
  schema: EditorSchema
@@ -5770,6 +5877,7 @@ export declare const editorMachine: StateMachine<
5770
5877
  readonly actions: ActionFunction<
5771
5878
  {
5772
5879
  behaviors: Set<Behavior>
5880
+ converters: Set<Converter>
5773
5881
  keyGenerator: () => string
5774
5882
  pendingEvents: Array<PatchEvent | MutationEvent>
5775
5883
  schema: EditorSchema
@@ -6064,6 +6172,7 @@ export declare const editorMachine: StateMachine<
6064
6172
  readonly actions: ActionFunction<
6065
6173
  {
6066
6174
  behaviors: Set<Behavior>
6175
+ converters: Set<Converter>
6067
6176
  keyGenerator: () => string
6068
6177
  pendingEvents: Array<PatchEvent | MutationEvent>
6069
6178
  schema: EditorSchema
@@ -6357,6 +6466,7 @@ export declare const editorMachine: StateMachine<
6357
6466
  readonly actions: ActionFunction<
6358
6467
  {
6359
6468
  behaviors: Set<Behavior>
6469
+ converters: Set<Converter>
6360
6470
  keyGenerator: () => string
6361
6471
  pendingEvents: Array<PatchEvent | MutationEvent>
6362
6472
  schema: EditorSchema
@@ -6649,6 +6759,7 @@ export declare const editorMachine: StateMachine<
6649
6759
  readonly actions: ActionFunction<
6650
6760
  {
6651
6761
  behaviors: Set<Behavior>
6762
+ converters: Set<Converter>
6652
6763
  keyGenerator: () => string
6653
6764
  pendingEvents: Array<PatchEvent | MutationEvent>
6654
6765
  schema: EditorSchema
@@ -6957,6 +7068,7 @@ export declare const editorMachine: StateMachine<
6957
7068
  readonly actions: ActionFunction<
6958
7069
  {
6959
7070
  behaviors: Set<Behavior>
7071
+ converters: Set<Converter>
6960
7072
  keyGenerator: () => string
6961
7073
  pendingEvents: Array<PatchEvent | MutationEvent>
6962
7074
  schema: EditorSchema
@@ -7250,6 +7362,7 @@ export declare const editorMachine: StateMachine<
7250
7362
  readonly actions: ActionFunction<
7251
7363
  {
7252
7364
  behaviors: Set<Behavior>
7365
+ converters: Set<Converter>
7253
7366
  keyGenerator: () => string
7254
7367
  pendingEvents: Array<PatchEvent | MutationEvent>
7255
7368
  schema: EditorSchema
@@ -7543,6 +7656,7 @@ export declare const editorMachine: StateMachine<
7543
7656
  readonly actions: ActionFunction<
7544
7657
  {
7545
7658
  behaviors: Set<Behavior>
7659
+ converters: Set<Converter>
7546
7660
  keyGenerator: () => string
7547
7661
  pendingEvents: Array<PatchEvent | MutationEvent>
7548
7662
  schema: EditorSchema
@@ -8207,6 +8321,8 @@ export declare type LoadingChange = {
8207
8321
  isLoading: boolean
8208
8322
  }
8209
8323
 
8324
+ declare type MIMEType = `${string}/${string}`
8325
+
8210
8326
  /**
8211
8327
  * The editor has mutated it's content.
8212
8328
  * @beta */
@@ -8237,6 +8353,10 @@ export declare type NativeBehaviorEvent =
8237
8353
  type: 'copy'
8238
8354
  data: DataTransfer
8239
8355
  }
8356
+ | {
8357
+ type: 'deserialize'
8358
+ dataTransfer: DataTransfer
8359
+ }
8240
8360
  | {
8241
8361
  type: 'key.down'
8242
8362
  keyboardEvent: Pick<
@@ -8255,6 +8375,11 @@ export declare type NativeBehaviorEvent =
8255
8375
  type: 'paste'
8256
8376
  data: DataTransfer
8257
8377
  }
8378
+ | {
8379
+ type: 'serialize'
8380
+ originEvent: 'copy' | 'cut' | 'unknown'
8381
+ dataTransfer: DataTransfer
8382
+ }
8258
8383
 
8259
8384
  /**
8260
8385
  * @alpha
@@ -8782,6 +8907,18 @@ export declare type SelectionChange = {
8782
8907
  selection: EditorSelection
8783
8908
  }
8784
8909
 
8910
+ declare type Serializer<TMIMEType extends MIMEType> = ({
8911
+ context,
8912
+ event,
8913
+ }: {
8914
+ context: EditorContext
8915
+ event: PickFromUnion<ConverterEvent<TMIMEType>, 'type', 'serialize'>
8916
+ }) => PickFromUnion<
8917
+ ConverterEvent<TMIMEType>,
8918
+ 'type',
8919
+ 'serialization.success' | 'serialization.failure'
8920
+ >
8921
+
8785
8922
  /**
8786
8923
  * @internal
8787
8924
  */
@@ -8812,6 +8949,12 @@ export declare type SyntheticBehaviorEvent =
8812
8949
  | {
8813
8950
  type: 'blur'
8814
8951
  }
8952
+ | {
8953
+ type: 'data transfer.set'
8954
+ data: string
8955
+ dataTransfer: DataTransfer
8956
+ mimeType: MIMEType
8957
+ }
8815
8958
  | {
8816
8959
  type: 'decorator.toggle'
8817
8960
  decorator: string
@@ -8827,6 +8970,10 @@ export declare type SyntheticBehaviorEvent =
8827
8970
  | {
8828
8971
  type: 'focus'
8829
8972
  }
8973
+ | {
8974
+ type: 'insert.blocks'
8975
+ blocks: Array<PortableTextBlock>
8976
+ }
8830
8977
  | {
8831
8978
  type: 'insert.block object'
8832
8979
  placement: 'auto' | 'after' | 'before'
@@ -8869,6 +9016,16 @@ export declare type SyntheticBehaviorEvent =
8869
9016
  type: 'style.toggle'
8870
9017
  style: string
8871
9018
  }
9019
+ | (PickFromUnion<
9020
+ ConverterEvent,
9021
+ 'type',
9022
+ | 'deserialization.failure'
9023
+ | 'deserialization.success'
9024
+ | 'serialization.failure'
9025
+ | 'serialization.success'
9026
+ > & {
9027
+ dataTransfer: DataTransfer
9028
+ })
8872
9029
 
8873
9030
  /**
8874
9031
  * The editor performed a undo history step