@portabletext/editor 1.4.0 → 1.5.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.
package/lib/index.d.mts CHANGED
@@ -2,21 +2,22 @@ import {Patch} from '@portabletext/patches'
2
2
  import type {
3
3
  ArrayDefinition,
4
4
  ArraySchemaType,
5
- BlockDecoratorDefinition,
6
5
  BlockListDefinition,
7
- BlockSchemaType,
8
6
  BlockStyleDefinition,
9
7
  KeyedSegment,
10
8
  ObjectSchemaType,
11
9
  Path,
12
- PortableTextBlock,
13
10
  PortableTextChild,
14
11
  PortableTextListBlock,
15
12
  PortableTextObject,
16
- SpanSchemaType,
17
13
  TypedObject,
18
14
  } from '@sanity/types'
19
- import {PortableTextSpan, PortableTextTextBlock} from '@sanity/types'
15
+ import {
16
+ BlockDecoratorDefinition,
17
+ PortableTextBlock,
18
+ PortableTextSpan,
19
+ PortableTextTextBlock,
20
+ } from '@sanity/types'
20
21
  import type {
21
22
  BaseSyntheticEvent,
22
23
  ClipboardEvent as ClipboardEvent_2,
@@ -36,8 +37,8 @@ import {
36
37
  } from 'react'
37
38
  import type {Observable, Subject} from 'rxjs'
38
39
  import type {Descendant, Node as Node_2, Operation, TextUnit} from 'slate'
40
+ import type {DOMNode} from 'slate-dom'
39
41
  import type {ReactEditor} from 'slate-react'
40
- import type {DOMNode} from 'slate-react/dist/utils/dom'
41
42
  import type {TextInsertTextOptions} from 'slate/dist/interfaces/transforms/text'
42
43
  import {
43
44
  ActionArgs,
@@ -64,6 +65,15 @@ import {
64
65
  Values,
65
66
  } from 'xstate'
66
67
 
68
+ /**
69
+ * @alpha
70
+ */
71
+ export declare type BaseDefinition = {
72
+ name: string
73
+ title?: string
74
+ icon?: BlockDecoratorDefinition['icon']
75
+ }
76
+
67
77
  /**
68
78
  * @alpha
69
79
  */
@@ -303,10 +313,10 @@ export declare type createEditorOptions = {
303
313
  export declare function createMarkdownBehaviors(
304
314
  config: MarkdownBehaviorsConfig,
305
315
  ): Behavior<
306
- | 'insert break'
307
- | 'insert soft break'
308
316
  | 'delete backward'
309
317
  | 'delete forward'
318
+ | 'insert soft break'
319
+ | 'insert break'
310
320
  | 'insert text',
311
321
  true
312
322
  >[]
@@ -319,12 +329,23 @@ export declare function defineBehavior<
319
329
  TGuardResponse = true,
320
330
  >(behavior: Behavior<TBehaviorEventType, TGuardResponse>): Behavior
321
331
 
332
+ /**
333
+ * @alpha
334
+ */
335
+ export declare function defineSchema<
336
+ const TSchemaDefinition extends SchemaDefinition,
337
+ >(definition: TSchemaDefinition): TSchemaDefinition
338
+
322
339
  /** @beta */
323
340
  export declare interface EditableAPI {
324
341
  activeAnnotations: () => PortableTextObject[]
325
342
  isAnnotationActive: (annotationType: PortableTextObject['_type']) => boolean
326
- addAnnotation: (
327
- type: ObjectSchemaType,
343
+ addAnnotation: <
344
+ TSchemaType extends {
345
+ name: string
346
+ },
347
+ >(
348
+ type: TSchemaType,
328
349
  value?: {
329
350
  [prop: string]: unknown
330
351
  },
@@ -354,14 +375,22 @@ export declare interface EditableAPI {
354
375
  getValue: () => PortableTextBlock[] | undefined
355
376
  hasBlockStyle: (style: string) => boolean
356
377
  hasListStyle: (listStyle: string) => boolean
357
- insertBlock: (
358
- type: BlockSchemaType | ObjectSchemaType,
378
+ insertBlock: <
379
+ TSchemaType extends {
380
+ name: string
381
+ },
382
+ >(
383
+ type: TSchemaType,
359
384
  value?: {
360
385
  [prop: string]: unknown
361
386
  },
362
387
  ) => Path
363
- insertChild: (
364
- type: SpanSchemaType | ObjectSchemaType,
388
+ insertChild: <
389
+ TSchemaType extends {
390
+ name: string
391
+ },
392
+ >(
393
+ type: TSchemaType,
365
394
  value?: {
366
395
  [prop: string]: unknown
367
396
  },
@@ -377,7 +406,13 @@ export declare interface EditableAPI {
377
406
  isVoid: (element: PortableTextBlock | PortableTextChild) => boolean
378
407
  marks: () => string[]
379
408
  redo: () => void
380
- removeAnnotation: (type: ObjectSchemaType) => void
409
+ removeAnnotation: <
410
+ TSchemaType extends {
411
+ name: string
412
+ },
413
+ >(
414
+ type: TSchemaType,
415
+ ) => void
381
416
  select: (selection: EditorSelection) => void
382
417
  toggleBlockStyle: (blockStyle: string) => void
383
418
  toggleList: (listStyle: string) => void
@@ -430,8 +465,16 @@ export declare type EditorChanges = Subject<EditorChange>
430
465
  export declare type EditorConfig = {
431
466
  behaviors?: Array<Behavior>
432
467
  keyGenerator?: () => string
433
- schema: ArraySchemaType<PortableTextBlock> | ArrayDefinition
434
- }
468
+ } & (
469
+ | {
470
+ schemaDefinition: SchemaDefinition
471
+ schema?: undefined
472
+ }
473
+ | {
474
+ schemaDefinition?: undefined
475
+ schema: ArraySchemaType<PortableTextBlock> | ArrayDefinition
476
+ }
477
+ )
435
478
 
436
479
  /**
437
480
  * @internal
@@ -867,10 +910,10 @@ export declare const editorMachine: StateMachine<
867
910
  >
868
911
  }) => {
869
912
  behaviors: Behavior<
870
- | 'insert break'
871
- | 'insert soft break'
872
913
  | 'delete backward'
873
914
  | 'delete forward'
915
+ | 'insert soft break'
916
+ | 'insert break'
874
917
  | 'insert text',
875
918
  true
876
919
  >[]
@@ -3092,6 +3135,8 @@ export declare type PickFromUnion<
3092
3135
  TPickedTags extends TUnion[TTagKey],
3093
3136
  > = TUnion extends Record<TTagKey, TPickedTags> ? TUnion : never
3094
3137
 
3138
+ export {PortableTextBlock}
3139
+
3095
3140
  /**
3096
3141
  * @public
3097
3142
  */
@@ -3156,9 +3201,13 @@ export declare class PortableTextEditor extends Component<
3156
3201
  editor: PortableTextEditor,
3157
3202
  annotationType: PortableTextObject['_type'],
3158
3203
  ) => boolean
3159
- static addAnnotation: (
3204
+ static addAnnotation: <
3205
+ TSchemaType extends {
3206
+ name: string
3207
+ },
3208
+ >(
3160
3209
  editor: PortableTextEditor,
3161
- type: ObjectSchemaType,
3210
+ type: TSchemaType,
3162
3211
  value?: {
3163
3212
  [prop: string]: unknown
3164
3213
  },
@@ -3231,16 +3280,24 @@ export declare class PortableTextEditor extends Component<
3231
3280
  editor: PortableTextEditor,
3232
3281
  mark: string,
3233
3282
  ) => boolean | undefined
3234
- static insertChild: (
3283
+ static insertChild: <
3284
+ TSchemaType extends {
3285
+ name: string
3286
+ },
3287
+ >(
3235
3288
  editor: PortableTextEditor,
3236
- type: SpanSchemaType | ObjectSchemaType,
3289
+ type: TSchemaType,
3237
3290
  value?: {
3238
3291
  [prop: string]: unknown
3239
3292
  },
3240
3293
  ) => Path | undefined
3241
- static insertBlock: (
3294
+ static insertBlock: <
3295
+ TSchemaType extends {
3296
+ name: string
3297
+ },
3298
+ >(
3242
3299
  editor: PortableTextEditor,
3243
- type: BlockSchemaType | ObjectSchemaType,
3300
+ type: TSchemaType,
3244
3301
  value?: {
3245
3302
  [prop: string]: unknown
3246
3303
  },
@@ -3256,9 +3313,13 @@ export declare class PortableTextEditor extends Component<
3256
3313
  editor: PortableTextEditor,
3257
3314
  selection: EditorSelection | null,
3258
3315
  ) => void
3259
- static removeAnnotation: (
3316
+ static removeAnnotation: <
3317
+ TSchemaType extends {
3318
+ name: string
3319
+ },
3320
+ >(
3260
3321
  editor: PortableTextEditor,
3261
- type: ObjectSchemaType,
3322
+ type: TSchemaType,
3262
3323
  ) => void | undefined
3263
3324
  static toggleBlockStyle: (
3264
3325
  editor: PortableTextEditor,
@@ -3551,6 +3612,20 @@ export declare type RenderStyleFunction = (
3551
3612
  props: BlockStyleRenderProps,
3552
3613
  ) => JSX.Element
3553
3614
 
3615
+ /**
3616
+ * @alpha
3617
+ */
3618
+ export declare type SchemaDefinition<
3619
+ TBaseDefinition extends BaseDefinition = BaseDefinition,
3620
+ > = {
3621
+ decorators?: ReadonlyArray<TBaseDefinition>
3622
+ blockObjects?: ReadonlyArray<TBaseDefinition>
3623
+ inlineObjects?: ReadonlyArray<TBaseDefinition>
3624
+ annotations?: ReadonlyArray<TBaseDefinition>
3625
+ lists?: ReadonlyArray<TBaseDefinition>
3626
+ styles?: ReadonlyArray<TBaseDefinition>
3627
+ }
3628
+
3554
3629
  /** @beta */
3555
3630
  export declare type ScrollSelectionIntoViewFunction = (
3556
3631
  editor: PortableTextEditor,
@@ -4015,10 +4090,10 @@ export declare function useEditor(config: EditorConfig): Actor<
4015
4090
  >
4016
4091
  }) => {
4017
4092
  behaviors: Behavior<
4018
- | 'insert break'
4019
- | 'insert soft break'
4020
4093
  | 'delete backward'
4021
4094
  | 'delete forward'
4095
+ | 'insert soft break'
4096
+ | 'insert break'
4022
4097
  | 'insert text',
4023
4098
  true
4024
4099
  >[]
package/lib/index.d.ts CHANGED
@@ -2,21 +2,22 @@ import {Patch} from '@portabletext/patches'
2
2
  import type {
3
3
  ArrayDefinition,
4
4
  ArraySchemaType,
5
- BlockDecoratorDefinition,
6
5
  BlockListDefinition,
7
- BlockSchemaType,
8
6
  BlockStyleDefinition,
9
7
  KeyedSegment,
10
8
  ObjectSchemaType,
11
9
  Path,
12
- PortableTextBlock,
13
10
  PortableTextChild,
14
11
  PortableTextListBlock,
15
12
  PortableTextObject,
16
- SpanSchemaType,
17
13
  TypedObject,
18
14
  } from '@sanity/types'
19
- import {PortableTextSpan, PortableTextTextBlock} from '@sanity/types'
15
+ import {
16
+ BlockDecoratorDefinition,
17
+ PortableTextBlock,
18
+ PortableTextSpan,
19
+ PortableTextTextBlock,
20
+ } from '@sanity/types'
20
21
  import type {
21
22
  BaseSyntheticEvent,
22
23
  ClipboardEvent as ClipboardEvent_2,
@@ -36,8 +37,8 @@ import {
36
37
  } from 'react'
37
38
  import type {Observable, Subject} from 'rxjs'
38
39
  import type {Descendant, Node as Node_2, Operation, TextUnit} from 'slate'
40
+ import type {DOMNode} from 'slate-dom'
39
41
  import type {ReactEditor} from 'slate-react'
40
- import type {DOMNode} from 'slate-react/dist/utils/dom'
41
42
  import type {TextInsertTextOptions} from 'slate/dist/interfaces/transforms/text'
42
43
  import {
43
44
  ActionArgs,
@@ -64,6 +65,15 @@ import {
64
65
  Values,
65
66
  } from 'xstate'
66
67
 
68
+ /**
69
+ * @alpha
70
+ */
71
+ export declare type BaseDefinition = {
72
+ name: string
73
+ title?: string
74
+ icon?: BlockDecoratorDefinition['icon']
75
+ }
76
+
67
77
  /**
68
78
  * @alpha
69
79
  */
@@ -303,10 +313,10 @@ export declare type createEditorOptions = {
303
313
  export declare function createMarkdownBehaviors(
304
314
  config: MarkdownBehaviorsConfig,
305
315
  ): Behavior<
306
- | 'insert break'
307
- | 'insert soft break'
308
316
  | 'delete backward'
309
317
  | 'delete forward'
318
+ | 'insert soft break'
319
+ | 'insert break'
310
320
  | 'insert text',
311
321
  true
312
322
  >[]
@@ -319,12 +329,23 @@ export declare function defineBehavior<
319
329
  TGuardResponse = true,
320
330
  >(behavior: Behavior<TBehaviorEventType, TGuardResponse>): Behavior
321
331
 
332
+ /**
333
+ * @alpha
334
+ */
335
+ export declare function defineSchema<
336
+ const TSchemaDefinition extends SchemaDefinition,
337
+ >(definition: TSchemaDefinition): TSchemaDefinition
338
+
322
339
  /** @beta */
323
340
  export declare interface EditableAPI {
324
341
  activeAnnotations: () => PortableTextObject[]
325
342
  isAnnotationActive: (annotationType: PortableTextObject['_type']) => boolean
326
- addAnnotation: (
327
- type: ObjectSchemaType,
343
+ addAnnotation: <
344
+ TSchemaType extends {
345
+ name: string
346
+ },
347
+ >(
348
+ type: TSchemaType,
328
349
  value?: {
329
350
  [prop: string]: unknown
330
351
  },
@@ -354,14 +375,22 @@ export declare interface EditableAPI {
354
375
  getValue: () => PortableTextBlock[] | undefined
355
376
  hasBlockStyle: (style: string) => boolean
356
377
  hasListStyle: (listStyle: string) => boolean
357
- insertBlock: (
358
- type: BlockSchemaType | ObjectSchemaType,
378
+ insertBlock: <
379
+ TSchemaType extends {
380
+ name: string
381
+ },
382
+ >(
383
+ type: TSchemaType,
359
384
  value?: {
360
385
  [prop: string]: unknown
361
386
  },
362
387
  ) => Path
363
- insertChild: (
364
- type: SpanSchemaType | ObjectSchemaType,
388
+ insertChild: <
389
+ TSchemaType extends {
390
+ name: string
391
+ },
392
+ >(
393
+ type: TSchemaType,
365
394
  value?: {
366
395
  [prop: string]: unknown
367
396
  },
@@ -377,7 +406,13 @@ export declare interface EditableAPI {
377
406
  isVoid: (element: PortableTextBlock | PortableTextChild) => boolean
378
407
  marks: () => string[]
379
408
  redo: () => void
380
- removeAnnotation: (type: ObjectSchemaType) => void
409
+ removeAnnotation: <
410
+ TSchemaType extends {
411
+ name: string
412
+ },
413
+ >(
414
+ type: TSchemaType,
415
+ ) => void
381
416
  select: (selection: EditorSelection) => void
382
417
  toggleBlockStyle: (blockStyle: string) => void
383
418
  toggleList: (listStyle: string) => void
@@ -430,8 +465,16 @@ export declare type EditorChanges = Subject<EditorChange>
430
465
  export declare type EditorConfig = {
431
466
  behaviors?: Array<Behavior>
432
467
  keyGenerator?: () => string
433
- schema: ArraySchemaType<PortableTextBlock> | ArrayDefinition
434
- }
468
+ } & (
469
+ | {
470
+ schemaDefinition: SchemaDefinition
471
+ schema?: undefined
472
+ }
473
+ | {
474
+ schemaDefinition?: undefined
475
+ schema: ArraySchemaType<PortableTextBlock> | ArrayDefinition
476
+ }
477
+ )
435
478
 
436
479
  /**
437
480
  * @internal
@@ -867,10 +910,10 @@ export declare const editorMachine: StateMachine<
867
910
  >
868
911
  }) => {
869
912
  behaviors: Behavior<
870
- | 'insert break'
871
- | 'insert soft break'
872
913
  | 'delete backward'
873
914
  | 'delete forward'
915
+ | 'insert soft break'
916
+ | 'insert break'
874
917
  | 'insert text',
875
918
  true
876
919
  >[]
@@ -3092,6 +3135,8 @@ export declare type PickFromUnion<
3092
3135
  TPickedTags extends TUnion[TTagKey],
3093
3136
  > = TUnion extends Record<TTagKey, TPickedTags> ? TUnion : never
3094
3137
 
3138
+ export {PortableTextBlock}
3139
+
3095
3140
  /**
3096
3141
  * @public
3097
3142
  */
@@ -3156,9 +3201,13 @@ export declare class PortableTextEditor extends Component<
3156
3201
  editor: PortableTextEditor,
3157
3202
  annotationType: PortableTextObject['_type'],
3158
3203
  ) => boolean
3159
- static addAnnotation: (
3204
+ static addAnnotation: <
3205
+ TSchemaType extends {
3206
+ name: string
3207
+ },
3208
+ >(
3160
3209
  editor: PortableTextEditor,
3161
- type: ObjectSchemaType,
3210
+ type: TSchemaType,
3162
3211
  value?: {
3163
3212
  [prop: string]: unknown
3164
3213
  },
@@ -3231,16 +3280,24 @@ export declare class PortableTextEditor extends Component<
3231
3280
  editor: PortableTextEditor,
3232
3281
  mark: string,
3233
3282
  ) => boolean | undefined
3234
- static insertChild: (
3283
+ static insertChild: <
3284
+ TSchemaType extends {
3285
+ name: string
3286
+ },
3287
+ >(
3235
3288
  editor: PortableTextEditor,
3236
- type: SpanSchemaType | ObjectSchemaType,
3289
+ type: TSchemaType,
3237
3290
  value?: {
3238
3291
  [prop: string]: unknown
3239
3292
  },
3240
3293
  ) => Path | undefined
3241
- static insertBlock: (
3294
+ static insertBlock: <
3295
+ TSchemaType extends {
3296
+ name: string
3297
+ },
3298
+ >(
3242
3299
  editor: PortableTextEditor,
3243
- type: BlockSchemaType | ObjectSchemaType,
3300
+ type: TSchemaType,
3244
3301
  value?: {
3245
3302
  [prop: string]: unknown
3246
3303
  },
@@ -3256,9 +3313,13 @@ export declare class PortableTextEditor extends Component<
3256
3313
  editor: PortableTextEditor,
3257
3314
  selection: EditorSelection | null,
3258
3315
  ) => void
3259
- static removeAnnotation: (
3316
+ static removeAnnotation: <
3317
+ TSchemaType extends {
3318
+ name: string
3319
+ },
3320
+ >(
3260
3321
  editor: PortableTextEditor,
3261
- type: ObjectSchemaType,
3322
+ type: TSchemaType,
3262
3323
  ) => void | undefined
3263
3324
  static toggleBlockStyle: (
3264
3325
  editor: PortableTextEditor,
@@ -3551,6 +3612,20 @@ export declare type RenderStyleFunction = (
3551
3612
  props: BlockStyleRenderProps,
3552
3613
  ) => JSX.Element
3553
3614
 
3615
+ /**
3616
+ * @alpha
3617
+ */
3618
+ export declare type SchemaDefinition<
3619
+ TBaseDefinition extends BaseDefinition = BaseDefinition,
3620
+ > = {
3621
+ decorators?: ReadonlyArray<TBaseDefinition>
3622
+ blockObjects?: ReadonlyArray<TBaseDefinition>
3623
+ inlineObjects?: ReadonlyArray<TBaseDefinition>
3624
+ annotations?: ReadonlyArray<TBaseDefinition>
3625
+ lists?: ReadonlyArray<TBaseDefinition>
3626
+ styles?: ReadonlyArray<TBaseDefinition>
3627
+ }
3628
+
3554
3629
  /** @beta */
3555
3630
  export declare type ScrollSelectionIntoViewFunction = (
3556
3631
  editor: PortableTextEditor,
@@ -4015,10 +4090,10 @@ export declare function useEditor(config: EditorConfig): Actor<
4015
4090
  >
4016
4091
  }) => {
4017
4092
  behaviors: Behavior<
4018
- | 'insert break'
4019
- | 'insert soft break'
4020
4093
  | 'delete backward'
4021
4094
  | 'delete forward'
4095
+ | 'insert soft break'
4096
+ | 'insert break'
4022
4097
  | 'insert text',
4023
4098
  true
4024
4099
  >[]