@portabletext/editor 0.0.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 (97) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +3 -0
  3. package/lib/index.d.mts +911 -0
  4. package/lib/index.d.ts +911 -0
  5. package/lib/index.esm.js +4896 -0
  6. package/lib/index.esm.js.map +1 -0
  7. package/lib/index.js +4874 -0
  8. package/lib/index.js.map +1 -0
  9. package/lib/index.mjs +4896 -0
  10. package/lib/index.mjs.map +1 -0
  11. package/package.json +119 -0
  12. package/src/editor/Editable.tsx +683 -0
  13. package/src/editor/PortableTextEditor.tsx +308 -0
  14. package/src/editor/__tests__/PortableTextEditor.test.tsx +386 -0
  15. package/src/editor/__tests__/PortableTextEditorTester.tsx +116 -0
  16. package/src/editor/__tests__/RangeDecorations.test.tsx +115 -0
  17. package/src/editor/__tests__/handleClick.test.tsx +218 -0
  18. package/src/editor/__tests__/pteWarningsSelfSolving.test.tsx +389 -0
  19. package/src/editor/__tests__/utils.ts +39 -0
  20. package/src/editor/components/DraggableBlock.tsx +287 -0
  21. package/src/editor/components/Element.tsx +279 -0
  22. package/src/editor/components/Leaf.tsx +288 -0
  23. package/src/editor/components/SlateContainer.tsx +81 -0
  24. package/src/editor/components/Synchronizer.tsx +190 -0
  25. package/src/editor/hooks/usePortableTextEditor.ts +23 -0
  26. package/src/editor/hooks/usePortableTextEditorKeyGenerator.ts +24 -0
  27. package/src/editor/hooks/usePortableTextEditorSelection.ts +22 -0
  28. package/src/editor/hooks/usePortableTextEditorValue.ts +16 -0
  29. package/src/editor/hooks/usePortableTextReadOnly.ts +20 -0
  30. package/src/editor/hooks/useSyncValue.test.tsx +125 -0
  31. package/src/editor/hooks/useSyncValue.ts +372 -0
  32. package/src/editor/nodes/DefaultAnnotation.tsx +16 -0
  33. package/src/editor/nodes/DefaultObject.tsx +15 -0
  34. package/src/editor/nodes/index.ts +189 -0
  35. package/src/editor/plugins/__tests__/withEditableAPIDelete.test.tsx +244 -0
  36. package/src/editor/plugins/__tests__/withEditableAPIGetFragment.test.tsx +142 -0
  37. package/src/editor/plugins/__tests__/withEditableAPIInsert.test.tsx +346 -0
  38. package/src/editor/plugins/__tests__/withEditableAPISelectionsOverlapping.test.tsx +162 -0
  39. package/src/editor/plugins/__tests__/withHotkeys.test.tsx +212 -0
  40. package/src/editor/plugins/__tests__/withInsertBreak.test.tsx +204 -0
  41. package/src/editor/plugins/__tests__/withPlaceholderBlock.test.tsx +133 -0
  42. package/src/editor/plugins/__tests__/withPortableTextLists.test.tsx +65 -0
  43. package/src/editor/plugins/__tests__/withPortableTextMarkModel.test.tsx +1377 -0
  44. package/src/editor/plugins/__tests__/withPortableTextSelections.test.tsx +91 -0
  45. package/src/editor/plugins/__tests__/withUndoRedo.test.tsx +115 -0
  46. package/src/editor/plugins/createWithEditableAPI.ts +573 -0
  47. package/src/editor/plugins/createWithHotKeys.ts +304 -0
  48. package/src/editor/plugins/createWithInsertBreak.ts +45 -0
  49. package/src/editor/plugins/createWithInsertData.ts +359 -0
  50. package/src/editor/plugins/createWithMaxBlocks.ts +24 -0
  51. package/src/editor/plugins/createWithObjectKeys.ts +63 -0
  52. package/src/editor/plugins/createWithPatches.ts +274 -0
  53. package/src/editor/plugins/createWithPlaceholderBlock.ts +36 -0
  54. package/src/editor/plugins/createWithPortableTextBlockStyle.ts +91 -0
  55. package/src/editor/plugins/createWithPortableTextLists.ts +160 -0
  56. package/src/editor/plugins/createWithPortableTextMarkModel.ts +441 -0
  57. package/src/editor/plugins/createWithPortableTextSelections.ts +65 -0
  58. package/src/editor/plugins/createWithSchemaTypes.ts +76 -0
  59. package/src/editor/plugins/createWithUndoRedo.ts +494 -0
  60. package/src/editor/plugins/createWithUtils.ts +81 -0
  61. package/src/editor/plugins/index.ts +155 -0
  62. package/src/index.ts +11 -0
  63. package/src/patch/PatchEvent.ts +33 -0
  64. package/src/patch/applyPatch.ts +29 -0
  65. package/src/patch/array.ts +89 -0
  66. package/src/patch/arrayInsert.ts +27 -0
  67. package/src/patch/object.ts +39 -0
  68. package/src/patch/patches.ts +53 -0
  69. package/src/patch/primitive.ts +43 -0
  70. package/src/patch/string.ts +51 -0
  71. package/src/types/editor.ts +576 -0
  72. package/src/types/options.ts +17 -0
  73. package/src/types/patch.ts +65 -0
  74. package/src/types/slate.ts +25 -0
  75. package/src/utils/__tests__/dmpToOperations.test.ts +181 -0
  76. package/src/utils/__tests__/operationToPatches.test.ts +421 -0
  77. package/src/utils/__tests__/patchToOperations.test.ts +293 -0
  78. package/src/utils/__tests__/ranges.test.ts +18 -0
  79. package/src/utils/__tests__/valueNormalization.test.tsx +62 -0
  80. package/src/utils/__tests__/values.test.ts +253 -0
  81. package/src/utils/applyPatch.ts +407 -0
  82. package/src/utils/bufferUntil.ts +15 -0
  83. package/src/utils/debug.ts +12 -0
  84. package/src/utils/getPortableTextMemberSchemaTypes.ts +100 -0
  85. package/src/utils/operationToPatches.ts +357 -0
  86. package/src/utils/patches.ts +36 -0
  87. package/src/utils/paths.ts +60 -0
  88. package/src/utils/ranges.ts +77 -0
  89. package/src/utils/schema.ts +8 -0
  90. package/src/utils/selection.ts +65 -0
  91. package/src/utils/ucs2Indices.ts +67 -0
  92. package/src/utils/validateValue.ts +394 -0
  93. package/src/utils/values.ts +208 -0
  94. package/src/utils/weakMaps.ts +24 -0
  95. package/src/utils/withChanges.ts +25 -0
  96. package/src/utils/withPreserveKeys.ts +14 -0
  97. package/src/utils/withoutPatching.ts +14 -0
package/lib/index.d.ts ADDED
@@ -0,0 +1,911 @@
1
+ /// <reference types="react" />
2
+
3
+ import {ArrayDefinition} from '@sanity/types'
4
+ import {ArraySchemaType} from '@sanity/types'
5
+ import {BaseSyntheticEvent} from 'react'
6
+ import {BlockDecoratorDefinition} from '@sanity/types'
7
+ import {BlockListDefinition} from '@sanity/types'
8
+ import {BlockSchemaType} from '@sanity/types'
9
+ import {BlockStyleDefinition} from '@sanity/types'
10
+ import {ClipboardEvent as ClipboardEvent_2} from 'react'
11
+ import {Component} from 'react'
12
+ import {Descendant} from 'slate'
13
+ import {DOMNode} from 'slate-react/dist/utils/dom'
14
+ import {FocusEvent as FocusEvent_2} from 'react'
15
+ import {ForwardRefExoticComponent} from 'react'
16
+ import {HTMLProps} from 'react'
17
+ import {JSX as JSX_2} from 'react'
18
+ import {KeyboardEvent as KeyboardEvent_2} from 'react'
19
+ import {MutableRefObject} from 'react'
20
+ import {Node as Node_2} from 'slate'
21
+ import {ObjectSchemaType} from '@sanity/types'
22
+ import {Observable} from 'rxjs'
23
+ import {Operation} from 'slate'
24
+ import {Path} from '@sanity/types'
25
+ import {PortableTextBlock} from '@sanity/types'
26
+ import {PortableTextChild} from '@sanity/types'
27
+ import {PortableTextListBlock} from '@sanity/types'
28
+ import {PortableTextObject} from '@sanity/types'
29
+ import {PortableTextSpan} from '@sanity/types'
30
+ import {PortableTextTextBlock} from '@sanity/types'
31
+ import {PropsWithChildren} from 'react'
32
+ import {ReactEditor} from 'slate-react'
33
+ import {ReactElement} from 'react'
34
+ import {ReactNode} from 'react'
35
+ import {RefAttributes} from 'react'
36
+ import {RefObject} from 'react'
37
+ import {SpanSchemaType} from '@sanity/types'
38
+ import {Subject} from 'rxjs'
39
+ import {TextareaHTMLAttributes} from 'react'
40
+ import {TypedObject} from '@sanity/types'
41
+
42
+ /** @beta */
43
+ export declare interface BlockAnnotationRenderProps {
44
+ block: PortableTextBlock
45
+ children: ReactElement
46
+ editorElementRef: RefObject<HTMLElement>
47
+ focused: boolean
48
+ path: Path
49
+ schemaType: ObjectSchemaType
50
+ selected: boolean
51
+ /** @deprecated Use `schemaType` instead */
52
+ type: ObjectSchemaType
53
+ value: PortableTextObject
54
+ }
55
+
56
+ /** @beta */
57
+ export declare interface BlockChildRenderProps {
58
+ annotations: PortableTextObject[]
59
+ children: ReactElement
60
+ editorElementRef: RefObject<HTMLElement>
61
+ focused: boolean
62
+ path: Path
63
+ selected: boolean
64
+ schemaType: ObjectSchemaType
65
+ /** @deprecated Use `schemaType` instead */
66
+ type: ObjectSchemaType
67
+ value: PortableTextChild
68
+ }
69
+
70
+ /** @beta */
71
+ export declare interface BlockDecoratorRenderProps {
72
+ children: ReactElement
73
+ editorElementRef: RefObject<HTMLElement>
74
+ focused: boolean
75
+ path: Path
76
+ schemaType: BlockDecoratorDefinition
77
+ selected: boolean
78
+ /** @deprecated Use `schemaType` instead */
79
+ type: BlockDecoratorDefinition
80
+ value: string
81
+ }
82
+
83
+ /** @beta */
84
+ export declare interface BlockListItemRenderProps {
85
+ block: PortableTextTextBlock
86
+ children: ReactElement
87
+ editorElementRef: RefObject<HTMLElement>
88
+ focused: boolean
89
+ level: number
90
+ path: Path
91
+ schemaType: BlockListDefinition
92
+ selected: boolean
93
+ value: string
94
+ }
95
+
96
+ /** @beta */
97
+ export declare interface BlockRenderProps {
98
+ children: ReactElement
99
+ editorElementRef: RefObject<HTMLElement>
100
+ focused: boolean
101
+ level?: number
102
+ listItem?: string
103
+ path: Path
104
+ selected: boolean
105
+ style?: string
106
+ schemaType: ObjectSchemaType
107
+ /** @deprecated Use `schemaType` instead */
108
+ type: ObjectSchemaType
109
+ value: PortableTextBlock
110
+ }
111
+
112
+ /** @beta */
113
+ export declare interface BlockStyleRenderProps {
114
+ block: PortableTextTextBlock
115
+ children: ReactElement
116
+ editorElementRef: RefObject<HTMLElement>
117
+ focused: boolean
118
+ path: Path
119
+ selected: boolean
120
+ schemaType: BlockStyleDefinition
121
+ value: string
122
+ }
123
+
124
+ /**
125
+ * The editor blurred
126
+ * @beta */
127
+ export declare type BlurChange = {
128
+ type: 'blur'
129
+ event: FocusEvent_2<HTMLDivElement, Element>
130
+ }
131
+
132
+ /**
133
+ * Try to compact a set of patches
134
+ *
135
+ */
136
+ export declare function compactPatches(patches: Patch[]): Patch[]
137
+
138
+ /**
139
+ * The editor was either connected or disconnected to the network
140
+ * To show out of sync warnings etc when in collaborative mode.
141
+ * @beta */
142
+ export declare type ConnectionChange = {
143
+ type: 'connection'
144
+ value: 'online' | 'offline'
145
+ }
146
+
147
+ export declare type createEditorOptions = {
148
+ keyGenerator: () => string
149
+ patches$?: PatchObservable
150
+ portableTextEditor: PortableTextEditor
151
+ readOnly: boolean
152
+ maxBlocks?: number
153
+ }
154
+
155
+ export declare type DecPatch = {
156
+ path: Path
157
+ origin?: Origin
158
+ type: 'dec'
159
+ value: JSONValue
160
+ }
161
+
162
+ export declare type DiffMatchPatch = {
163
+ path: Path
164
+ type: 'diffMatchPatch'
165
+ origin?: Origin
166
+ value: string
167
+ }
168
+
169
+ /** @beta */
170
+ export declare interface EditableAPI {
171
+ activeAnnotations: () => PortableTextObject[]
172
+ isAnnotationActive: (annotationType: PortableTextObject['_type']) => boolean
173
+ addAnnotation: (
174
+ type: ObjectSchemaType,
175
+ value?: {
176
+ [prop: string]: unknown
177
+ },
178
+ ) =>
179
+ | {
180
+ spanPath: Path
181
+ markDefPath: Path
182
+ }
183
+ | undefined
184
+ blur: () => void
185
+ delete: (selection: EditorSelection, options?: EditableAPIDeleteOptions) => void
186
+ findByPath: (path: Path) => [PortableTextBlock | PortableTextChild | undefined, Path | undefined]
187
+ findDOMNode: (element: PortableTextBlock | PortableTextChild) => DOMNode | undefined
188
+ focus: () => void
189
+ focusBlock: () => PortableTextBlock | undefined
190
+ focusChild: () => PortableTextChild | undefined
191
+ getSelection: () => EditorSelection
192
+ getFragment: () => PortableTextBlock[] | undefined
193
+ getValue: () => PortableTextBlock[] | undefined
194
+ hasBlockStyle: (style: string) => boolean
195
+ hasListStyle: (listStyle: string) => boolean
196
+ insertBlock: (
197
+ type: BlockSchemaType | ObjectSchemaType,
198
+ value?: {
199
+ [prop: string]: unknown
200
+ },
201
+ ) => Path
202
+ insertChild: (
203
+ type: SpanSchemaType | ObjectSchemaType,
204
+ value?: {
205
+ [prop: string]: unknown
206
+ },
207
+ ) => Path
208
+ insertBreak: () => void
209
+ isCollapsedSelection: () => boolean
210
+ isExpandedSelection: () => boolean
211
+ isMarkActive: (mark: string) => boolean
212
+ isSelectionsOverlapping: (selectionA: EditorSelection, selectionB: EditorSelection) => boolean
213
+ isVoid: (element: PortableTextBlock | PortableTextChild) => boolean
214
+ marks: () => string[]
215
+ redo: () => void
216
+ removeAnnotation: (type: ObjectSchemaType) => void
217
+ select: (selection: EditorSelection) => void
218
+ toggleBlockStyle: (blockStyle: string) => void
219
+ toggleList: (listStyle: string) => void
220
+ toggleMark: (mark: string) => void
221
+ undo: () => void
222
+ }
223
+
224
+ /** @beta */
225
+ export declare interface EditableAPIDeleteOptions {
226
+ mode?: 'blocks' | 'children' | 'selected'
227
+ }
228
+
229
+ /**
230
+ * When the editor changes, it will emit a change item describing the change
231
+ * @beta */
232
+ export declare type EditorChange =
233
+ | BlurChange
234
+ | ConnectionChange
235
+ | ErrorChange
236
+ | FocusChange
237
+ | InvalidValue
238
+ | LoadingChange
239
+ | MutationChange
240
+ | PatchChange
241
+ | ReadyChange
242
+ | RedoChange
243
+ | SelectionChange
244
+ | UndoChange
245
+ | UnsetChange
246
+ | ValueChange
247
+
248
+ export declare type EditorChanges = Subject<EditorChange>
249
+
250
+ /** @internal */
251
+ export declare type EditorNode = Node_2 & {
252
+ _key: string
253
+ _type: string
254
+ }
255
+
256
+ /** @beta */
257
+ export declare type EditorSelection = {
258
+ anchor: EditorSelectionPoint
259
+ focus: EditorSelectionPoint
260
+ backward?: boolean
261
+ } | null
262
+
263
+ /** @beta */
264
+ export declare type EditorSelectionPoint = {
265
+ path: Path
266
+ offset: number
267
+ }
268
+
269
+ /**
270
+ * The editor produced an error
271
+ * @beta */
272
+ export declare type ErrorChange = {
273
+ type: 'error'
274
+ name: string
275
+ level: 'warning' | 'error'
276
+ description: string
277
+ data?: unknown
278
+ }
279
+
280
+ /**
281
+ * The editor received focus
282
+ * @beta */
283
+ export declare type FocusChange = {
284
+ type: 'focus'
285
+ event: FocusEvent_2<HTMLDivElement, Element>
286
+ }
287
+
288
+ /** @internal */
289
+ declare interface History_2 {
290
+ redos: HistoryItem[]
291
+ undos: HistoryItem[]
292
+ }
293
+ export {History_2 as History}
294
+
295
+ /** @internal */
296
+ export declare type HistoryItem = {
297
+ operations: Operation[]
298
+ timestamp: Date
299
+ }
300
+
301
+ export declare type HotkeyOptions = {
302
+ marks?: Record<string, string>
303
+ custom?: Record<string, (event: BaseSyntheticEvent, editor: PortableTextEditor) => void>
304
+ }
305
+
306
+ export declare type IncPatch = {
307
+ path: Path
308
+ origin?: Origin
309
+ type: 'inc'
310
+ value: JSONValue
311
+ }
312
+
313
+ export declare type InsertPatch = {
314
+ path: Path
315
+ origin?: Origin
316
+ type: 'insert'
317
+ position: InsertPosition_2
318
+ items: JSONValue[]
319
+ }
320
+
321
+ declare type InsertPosition_2 = 'before' | 'after' | 'replace'
322
+ export {InsertPosition_2 as InsertPosition}
323
+
324
+ /**
325
+ * The editor has an invalid value
326
+ * @beta */
327
+ export declare type InvalidValue = {
328
+ type: 'invalidValue'
329
+ resolution: InvalidValueResolution | null
330
+ value: PortableTextBlock[] | undefined
331
+ }
332
+
333
+ /**
334
+ * The editor has invalid data in the value that can be resolved by the user
335
+ * @beta */
336
+ export declare type InvalidValueResolution = {
337
+ autoResolve?: boolean
338
+ patches: Patch[]
339
+ description: string
340
+ action: string
341
+ item: PortableTextBlock[] | PortableTextBlock | PortableTextChild | undefined
342
+ /**
343
+ * i18n keys for the description and action
344
+ *
345
+ * These are in addition to the description and action properties, to decouple the editor from
346
+ * the i18n system, and allow usage without it. The i18n keys take precedence over the
347
+ * description and action properties, if i18n framework is available.
348
+ */
349
+ i18n: {
350
+ description: `inputs.portable-text.invalid-value.${Lowercase<string>}.description`
351
+ action: `inputs.portable-text.invalid-value.${Lowercase<string>}.action`
352
+ values?: Record<string, string | number | string[]>
353
+ }
354
+ }
355
+
356
+ export declare type JSONValue =
357
+ | number
358
+ | string
359
+ | boolean
360
+ | {
361
+ [key: string]: JSONValue
362
+ }
363
+ | JSONValue[]
364
+
365
+ export declare const keyGenerator: () => string
366
+
367
+ /**
368
+ * The editor is currently loading something
369
+ * Could be used to show a spinner etc.
370
+ * @beta */
371
+ export declare type LoadingChange = {
372
+ type: 'loading'
373
+ isLoading: boolean
374
+ }
375
+
376
+ /**
377
+ * The editor has mutated it's content.
378
+ * @beta */
379
+ export declare type MutationChange = {
380
+ type: 'mutation'
381
+ patches: Patch[]
382
+ snapshot: PortableTextBlock[] | undefined
383
+ }
384
+
385
+ /** @beta */
386
+ export declare type OnBeforeInputFn = (event: InputEvent) => void
387
+
388
+ /** @beta */
389
+ export declare type OnCopyFn = (
390
+ event: ClipboardEvent_2<HTMLDivElement | HTMLSpanElement>,
391
+ ) => undefined | unknown
392
+
393
+ /** @beta */
394
+ export declare type OnPasteFn = (data: PasteData) => OnPasteResultOrPromise
395
+
396
+ /** @beta */
397
+ export declare type OnPasteResult =
398
+ | {
399
+ insert?: TypedObject[]
400
+ path?: Path
401
+ }
402
+ | undefined
403
+
404
+ export declare type OnPasteResultOrPromise = OnPasteResult | Promise<OnPasteResult>
405
+
406
+ export declare type Origin = 'remote' | 'local' | 'internal'
407
+
408
+ /** @beta */
409
+ export declare interface PasteData {
410
+ event: ClipboardEvent_2
411
+ path: Path
412
+ schemaTypes: PortableTextMemberSchemaTypes
413
+ value: PortableTextBlock[] | undefined
414
+ }
415
+
416
+ export declare type Patch =
417
+ | SetPatch
418
+ | SetIfMissingPatch
419
+ | UnsetPatch
420
+ | InsertPatch
421
+ | DiffMatchPatch
422
+ | IncPatch
423
+ | DecPatch
424
+
425
+ /**
426
+ * The editor has produced a patch
427
+ * @beta */
428
+ export declare type PatchChange = {
429
+ type: 'patch'
430
+ patch: Patch
431
+ }
432
+
433
+ /** @beta */
434
+ export declare type PatchObservable = Observable<{
435
+ patches: Patch[]
436
+ snapshot: PortableTextBlock[] | undefined
437
+ }>
438
+
439
+ /**
440
+ * @public
441
+ */
442
+ export declare const PortableTextEditable: ForwardRefExoticComponent<
443
+ Omit<
444
+ Omit<TextareaHTMLAttributes<HTMLDivElement>, 'onPaste' | 'onCopy' | 'onBeforeInput'> & {
445
+ hotkeys?: HotkeyOptions | undefined
446
+ onBeforeInput?: ((event: InputEvent) => void) | undefined
447
+ onPaste?: OnPasteFn | undefined
448
+ onCopy?: OnCopyFn | undefined
449
+ ref: MutableRefObject<HTMLDivElement | null>
450
+ rangeDecorations?: RangeDecoration[] | undefined
451
+ renderAnnotation?: RenderAnnotationFunction | undefined
452
+ renderBlock?: RenderBlockFunction | undefined
453
+ renderChild?: RenderChildFunction | undefined
454
+ renderDecorator?: RenderDecoratorFunction | undefined
455
+ renderListItem?: RenderListItemFunction | undefined
456
+ renderPlaceholder?: (() => ReactNode) | undefined
457
+ renderStyle?: RenderStyleFunction | undefined
458
+ scrollSelectionIntoView?: ScrollSelectionIntoViewFunction | undefined
459
+ selection?: EditorSelection | undefined
460
+ spellCheck?: boolean | undefined
461
+ } & Omit<HTMLProps<HTMLDivElement>, 'onPaste' | 'onBeforeInput' | 'as'>,
462
+ 'ref'
463
+ > &
464
+ RefAttributes<HTMLDivElement>
465
+ >
466
+
467
+ /**
468
+ * @public
469
+ */
470
+ export declare type PortableTextEditableProps = Omit<
471
+ TextareaHTMLAttributes<HTMLDivElement>,
472
+ 'onPaste' | 'onCopy' | 'onBeforeInput'
473
+ > & {
474
+ hotkeys?: HotkeyOptions
475
+ onBeforeInput?: (event: InputEvent) => void
476
+ onPaste?: OnPasteFn
477
+ onCopy?: OnCopyFn
478
+ ref: MutableRefObject<HTMLDivElement | null>
479
+ rangeDecorations?: RangeDecoration[]
480
+ renderAnnotation?: RenderAnnotationFunction
481
+ renderBlock?: RenderBlockFunction
482
+ renderChild?: RenderChildFunction
483
+ renderDecorator?: RenderDecoratorFunction
484
+ renderListItem?: RenderListItemFunction
485
+ renderPlaceholder?: () => ReactNode
486
+ renderStyle?: RenderStyleFunction
487
+ scrollSelectionIntoView?: ScrollSelectionIntoViewFunction
488
+ selection?: EditorSelection
489
+ spellCheck?: boolean
490
+ }
491
+
492
+ /**
493
+ * The main Portable Text Editor component.
494
+ * @public
495
+ */
496
+ export declare class PortableTextEditor extends Component<PortableTextEditorProps> {
497
+ /**
498
+ * An observable of all the editor changes.
499
+ */
500
+ change$: EditorChanges
501
+ /**
502
+ * A lookup table for all the relevant schema types for this portable text type.
503
+ */
504
+ schemaTypes: PortableTextMemberSchemaTypes
505
+ /**
506
+ * The editor API (currently implemented with Slate).
507
+ */
508
+ private editable?
509
+ constructor(props: PortableTextEditorProps)
510
+ componentDidUpdate(prevProps: PortableTextEditorProps): void
511
+ setEditable: (editable: EditableAPI) => void
512
+ render(): JSX_2.Element
513
+ static activeAnnotations: (editor: PortableTextEditor) => PortableTextObject[]
514
+ static isAnnotationActive: (
515
+ editor: PortableTextEditor,
516
+ annotationType: PortableTextObject['_type'],
517
+ ) => boolean
518
+ static addAnnotation: (
519
+ editor: PortableTextEditor,
520
+ type: ObjectSchemaType,
521
+ value?: {
522
+ [prop: string]: unknown
523
+ },
524
+ ) =>
525
+ | {
526
+ spanPath: Path
527
+ markDefPath: Path
528
+ }
529
+ | undefined
530
+ static blur: (editor: PortableTextEditor) => void
531
+ static delete: (
532
+ editor: PortableTextEditor,
533
+ selection: EditorSelection,
534
+ options?: EditableAPIDeleteOptions,
535
+ ) => void | undefined
536
+ static findDOMNode: (
537
+ editor: PortableTextEditor,
538
+ element: PortableTextBlock | PortableTextChild,
539
+ ) => Node | undefined
540
+ static findByPath: (
541
+ editor: PortableTextEditor,
542
+ path: Path,
543
+ ) =>
544
+ | []
545
+ | [
546
+ (
547
+ | PortableTextTextBlock<PortableTextObject | PortableTextSpan>
548
+ | PortableTextObject
549
+ | PortableTextSpan
550
+ | undefined
551
+ ),
552
+ Path | undefined,
553
+ ]
554
+ static focus: (editor: PortableTextEditor) => void
555
+ static focusBlock: (editor: PortableTextEditor) => PortableTextBlock | undefined
556
+ static focusChild: (editor: PortableTextEditor) => PortableTextChild | undefined
557
+ static getSelection: (editor: PortableTextEditor) => EditorSelection
558
+ static getValue: (editor: PortableTextEditor) => PortableTextBlock[] | undefined
559
+ static hasBlockStyle: (editor: PortableTextEditor, blockStyle: string) => boolean | undefined
560
+ static hasListStyle: (editor: PortableTextEditor, listStyle: string) => boolean | undefined
561
+ static isCollapsedSelection: (editor: PortableTextEditor) => boolean | undefined
562
+ static isExpandedSelection: (editor: PortableTextEditor) => boolean | undefined
563
+ static isMarkActive: (editor: PortableTextEditor, mark: string) => boolean | undefined
564
+ static insertChild: (
565
+ editor: PortableTextEditor,
566
+ type: SpanSchemaType | ObjectSchemaType,
567
+ value?: {
568
+ [prop: string]: unknown
569
+ },
570
+ ) => Path | undefined
571
+ static insertBlock: (
572
+ editor: PortableTextEditor,
573
+ type: BlockSchemaType | ObjectSchemaType,
574
+ value?: {
575
+ [prop: string]: unknown
576
+ },
577
+ ) => Path | undefined
578
+ static insertBreak: (editor: PortableTextEditor) => void
579
+ static isVoid: (
580
+ editor: PortableTextEditor,
581
+ element: PortableTextBlock | PortableTextChild,
582
+ ) => boolean | undefined
583
+ static isObjectPath: (editor: PortableTextEditor, path: Path) => boolean
584
+ static marks: (editor: PortableTextEditor) => string[] | undefined
585
+ static select: (editor: PortableTextEditor, selection: EditorSelection | null) => void
586
+ static removeAnnotation: (editor: PortableTextEditor, type: ObjectSchemaType) => void | undefined
587
+ static toggleBlockStyle: (editor: PortableTextEditor, blockStyle: string) => void | undefined
588
+ static toggleList: (editor: PortableTextEditor, listStyle: string) => void
589
+ static toggleMark: (editor: PortableTextEditor, mark: string) => void
590
+ static getFragment: (editor: PortableTextEditor) => PortableTextBlock[] | undefined
591
+ static undo: (editor: PortableTextEditor) => void
592
+ static redo: (editor: PortableTextEditor) => void
593
+ static isSelectionsOverlapping: (
594
+ editor: PortableTextEditor,
595
+ selectionA: EditorSelection,
596
+ selectionB: EditorSelection,
597
+ ) => boolean | undefined
598
+ }
599
+
600
+ /**
601
+ * Props for the PortableTextEditor component
602
+ *
603
+ * @public
604
+ */
605
+ /**
606
+ * Props for the PortableTextEditor component
607
+ *
608
+ * @public
609
+ */
610
+ export declare type PortableTextEditorProps = PropsWithChildren<{
611
+ /**
612
+ * Function that gets called when the editor changes the value
613
+ */
614
+ onChange: (change: EditorChange) => void
615
+ /**
616
+ * Schema type for the portable text field
617
+ */
618
+ schemaType: ArraySchemaType<PortableTextBlock> | ArrayDefinition
619
+ /**
620
+ * Maximum number of blocks to allow within the editor
621
+ */
622
+ maxBlocks?: number | string
623
+ /**
624
+ * Whether or not the editor should be in read-only mode
625
+ */
626
+ readOnly?: boolean
627
+ /**
628
+ * The current value of the portable text field
629
+ */
630
+ value?: PortableTextBlock[]
631
+ /**
632
+ * Function used to generate keys for array items (`_key`)
633
+ */
634
+ keyGenerator?: () => string
635
+ /**
636
+ * Observable of local and remote patches for the edited value.
637
+ */
638
+ patches$?: PatchObservable
639
+ /**
640
+ * Backward compatibility (renamed to patches$).
641
+ */
642
+ incomingPatches$?: PatchObservable
643
+ /**
644
+ * A ref to the editor instance
645
+ */
646
+ editorRef?: MutableRefObject<PortableTextEditor | null>
647
+ }>
648
+
649
+ /** @internal */
650
+ export declare type PortableTextMemberSchemaTypes = {
651
+ annotations: (ObjectSchemaType & {
652
+ i18nTitleKey?: string
653
+ })[]
654
+ block: ObjectSchemaType
655
+ blockObjects: ObjectSchemaType[]
656
+ decorators: BlockDecoratorDefinition[]
657
+ inlineObjects: ObjectSchemaType[]
658
+ portableText: ArraySchemaType<PortableTextBlock>
659
+ span: ObjectSchemaType
660
+ styles: BlockStyleDefinition[]
661
+ lists: BlockListDefinition[]
662
+ }
663
+
664
+ /** @internal */
665
+ export declare interface PortableTextSlateEditor extends ReactEditor {
666
+ _key: 'editor'
667
+ _type: 'editor'
668
+ destroy: () => void
669
+ createPlaceholderBlock: () => Descendant
670
+ editable: EditableAPI
671
+ history: History_2
672
+ insertPortableTextData: (data: DataTransfer) => boolean
673
+ insertTextOrHTMLData: (data: DataTransfer) => boolean
674
+ isTextBlock: (value: unknown) => value is PortableTextTextBlock
675
+ isTextSpan: (value: unknown) => value is PortableTextSpan
676
+ isListBlock: (value: unknown) => value is PortableTextListBlock
677
+ subscriptions: (() => () => void)[]
678
+ /**
679
+ * Increments selected list items levels, or decrements them if `reverse` is true.
680
+ *
681
+ * @param reverse - if true, decrement instead of incrementing
682
+ * @returns True if anything was incremented in the selection
683
+ */
684
+ pteIncrementBlockLevels: (reverse?: boolean) => boolean
685
+ /**
686
+ * Toggle selected blocks as listItem
687
+ *
688
+ * @param listStyle - Style of list item to toggle on/off
689
+ */
690
+ pteToggleListItem: (listStyle: string) => void
691
+ /**
692
+ * Set selected block as listItem
693
+ *
694
+ * @param listStyle - Style of list item to set
695
+ */
696
+ pteSetListItem: (listStyle: string) => void
697
+ /**
698
+ * Unset selected block as listItem
699
+ *
700
+ * @param listStyle - Style of list item to unset
701
+ */
702
+ pteUnsetListItem: (listStyle: string) => void
703
+ /**
704
+ * Ends a list
705
+ *
706
+ * @returns True if a list was ended in the selection
707
+ */
708
+ pteEndList: () => boolean
709
+ /**
710
+ * Toggle marks in the selection
711
+ *
712
+ * @param mark - Mark to toggle on/off
713
+ */
714
+ pteToggleMark: (mark: string) => void
715
+ /**
716
+ * Test if a mark is active in the current selection
717
+ *
718
+ * @param mark - Mark to check whether or not is active
719
+ */
720
+ pteIsMarkActive: (mark: string) => boolean
721
+ /**
722
+ * Toggle the selected block style
723
+ *
724
+ * @param style - The style name
725
+ *
726
+ */
727
+ pteToggleBlockStyle: (style: string) => void
728
+ /**
729
+ * Test if the current selection has a certain block style
730
+ *
731
+ * @param style - The style name
732
+ *
733
+ */
734
+ pteHasBlockStyle: (style: string) => boolean
735
+ /**
736
+ * Test if the current selection has a certain list style
737
+ *
738
+ * @param listStyle - Style name to check whether or not the selection has
739
+ *
740
+ */
741
+ pteHasListStyle: (style: string) => boolean
742
+ /**
743
+ * Try to expand the current selection to a word
744
+ */
745
+ pteExpandToWord: () => void
746
+ /**
747
+ * Use hotkeys
748
+ */
749
+ pteWithHotKeys: (event: KeyboardEvent_2<HTMLDivElement>) => void
750
+ /**
751
+ * Helper function that creates an empty text block
752
+ */
753
+ pteCreateEmptyBlock: () => Descendant
754
+ /**
755
+ * Undo
756
+ */
757
+ undo: () => void
758
+ /**
759
+ * Redo
760
+ */
761
+ redo: () => void
762
+ }
763
+
764
+ /**
765
+ * A range decoration is a UI affordance that wraps a given selection range in the editor
766
+ * with a custom component. This can be used to highlight search results,
767
+ * mark validation errors on specific words, draw user presence and similar.
768
+ * @alpha */
769
+ export declare interface RangeDecoration {
770
+ /**
771
+ * A component for rendering the range decoration.
772
+ * The component will receive the children (text) of the range decoration as its children.
773
+ *
774
+ * @example
775
+ * ```ts
776
+ * (rangeComponentProps: PropsWithChildren) => (
777
+ * <SearchResultHighlight>
778
+ * {rangeComponentProps.children}
779
+ * </SearchResultHighlight>
780
+ * )
781
+ * ```
782
+ */
783
+ component: (props: PropsWithChildren) => ReactElement
784
+ /**
785
+ * The editor content selection range
786
+ */
787
+ selection: EditorSelection
788
+ /**
789
+ * A optional callback that will be called when the range decoration potentially moves according to user edits.
790
+ */
791
+ onMoved?: (details: RangeDecorationOnMovedDetails) => void
792
+ /**
793
+ * A custom payload that can be set on the range decoration
794
+ */
795
+ payload?: Record<string, unknown>
796
+ }
797
+
798
+ /**
799
+ * Parameters for the callback that will be called for a RangeDecoration's onMoved.
800
+ * @alpha */
801
+ export declare interface RangeDecorationOnMovedDetails {
802
+ rangeDecoration: RangeDecoration
803
+ newSelection: EditorSelection
804
+ origin: 'remote' | 'local'
805
+ }
806
+
807
+ /**
808
+ * The editor content is ready to be edited by the user
809
+ * @beta */
810
+ export declare type ReadyChange = {
811
+ type: 'ready'
812
+ }
813
+
814
+ /**
815
+ * The editor performed redo history step
816
+ * @beta */
817
+ export declare type RedoChange = {
818
+ type: 'redo'
819
+ patches: Patch[]
820
+ timestamp: Date
821
+ }
822
+
823
+ /** @beta */
824
+ export declare type RenderAnnotationFunction = (props: BlockAnnotationRenderProps) => JSX.Element
825
+
826
+ /** @beta */
827
+ export declare type RenderBlockFunction = (props: BlockRenderProps) => JSX.Element
828
+
829
+ /** @beta */
830
+ export declare type RenderChildFunction = (props: BlockChildRenderProps) => JSX.Element
831
+
832
+ /** @beta */
833
+ export declare type RenderDecoratorFunction = (props: BlockDecoratorRenderProps) => JSX.Element
834
+
835
+ /** @beta */
836
+ export declare type RenderEditableFunction = (props: PortableTextEditableProps) => JSX.Element
837
+
838
+ /** @beta */
839
+ export declare type RenderListItemFunction = (props: BlockListItemRenderProps) => JSX.Element
840
+
841
+ /** @beta */
842
+ export declare type RenderStyleFunction = (props: BlockStyleRenderProps) => JSX.Element
843
+
844
+ /** @beta */
845
+ export declare type ScrollSelectionIntoViewFunction = (
846
+ editor: PortableTextEditor,
847
+ domRange: globalThis.Range,
848
+ ) => void
849
+
850
+ /**
851
+ * The editor has a new selection
852
+ * @beta */
853
+ export declare type SelectionChange = {
854
+ type: 'selection'
855
+ selection: EditorSelection
856
+ }
857
+
858
+ export declare type SetIfMissingPatch = {
859
+ path: Path
860
+ origin?: Origin
861
+ type: 'setIfMissing'
862
+ value: JSONValue
863
+ }
864
+
865
+ export declare type SetPatch = {
866
+ path: Path
867
+ type: 'set'
868
+ origin?: Origin
869
+ value: JSONValue
870
+ }
871
+
872
+ /**
873
+ * The editor performed a undo history step
874
+ * @beta */
875
+ export declare type UndoChange = {
876
+ type: 'undo'
877
+ patches: Patch[]
878
+ timestamp: Date
879
+ }
880
+
881
+ /** @beta */
882
+ export declare type UnsetChange = {
883
+ type: 'unset'
884
+ previousValue: PortableTextBlock[]
885
+ }
886
+
887
+ export declare type UnsetPatch = {
888
+ path: Path
889
+ origin?: Origin
890
+ type: 'unset'
891
+ }
892
+
893
+ /**
894
+ * Get the current editor object from the React context.
895
+ */
896
+ export declare const usePortableTextEditor: () => PortableTextEditor
897
+
898
+ /**
899
+ * Get the current editor selection from the React context.
900
+ */
901
+ export declare const usePortableTextEditorSelection: () => EditorSelection
902
+
903
+ /**
904
+ * The editor has received a new (props) value
905
+ * @beta */
906
+ export declare type ValueChange = {
907
+ type: 'value'
908
+ value: PortableTextBlock[] | undefined
909
+ }
910
+
911
+ export {}