@portabletext/editor 1.49.9 → 1.49.11

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 (40) hide show
  1. package/lib/behaviors/index.d.cts +160 -2885
  2. package/lib/behaviors/index.d.ts +160 -2885
  3. package/lib/index.cjs +356 -311
  4. package/lib/index.cjs.map +1 -1
  5. package/lib/index.d.cts +170 -2874
  6. package/lib/index.d.ts +170 -2874
  7. package/lib/index.js +359 -314
  8. package/lib/index.js.map +1 -1
  9. package/lib/plugins/index.cjs.map +1 -1
  10. package/lib/plugins/index.d.cts +162 -2879
  11. package/lib/plugins/index.d.ts +162 -2879
  12. package/lib/plugins/index.js.map +1 -1
  13. package/lib/selectors/index.d.cts +160 -2885
  14. package/lib/selectors/index.d.ts +160 -2885
  15. package/lib/utils/index.d.cts +160 -2885
  16. package/lib/utils/index.d.ts +160 -2885
  17. package/package.json +7 -7
  18. package/src/editor/Editable.tsx +14 -12
  19. package/src/editor/PortableTextEditor.tsx +21 -26
  20. package/src/editor/__tests__/self-solving.test.tsx +9 -9
  21. package/src/editor/create-editor.ts +91 -53
  22. package/src/editor/create-slate-editor.tsx +3 -0
  23. package/src/editor/editor-machine.ts +15 -127
  24. package/src/editor/editor-provider.tsx +20 -15
  25. package/src/editor/mutation-machine.ts +125 -7
  26. package/src/editor/plugins/createWithPatches.ts +5 -2
  27. package/src/editor/plugins/createWithPortableTextSelections.ts +4 -2
  28. package/src/editor/plugins/with-plugins.ts +8 -3
  29. package/src/editor/relay-actor-context.ts +4 -0
  30. package/src/editor/relay-machine.ts +89 -0
  31. package/src/editor/route-events-to-changes.tsx +4 -10
  32. package/src/editor/sync-machine.ts +2 -2
  33. package/src/editor-event-listener.tsx +1 -1
  34. package/src/editor.ts +2 -4
  35. package/src/index.ts +3 -6
  36. package/src/internal-utils/__tests__/operationToPatches.test.ts +3 -1
  37. package/src/internal-utils/__tests__/patchToOperations.test.ts +2 -0
  38. package/src/internal-utils/applyPatch.ts +6 -2
  39. package/src/plugins/plugin.event-listener.tsx +1 -1
  40. package/src/types/editor.ts +12 -4
@@ -0,0 +1,89 @@
1
+ import type {Patch} from '@portabletext/patches'
2
+ import type {PortableTextBlock} from '@sanity/types'
3
+ import type {FocusEvent} from 'react'
4
+ import {emit, setup, type ActorRefFrom} from 'xstate'
5
+ import type {EditorSelection, InvalidValueResolution} from '../types/editor'
6
+
7
+ /**
8
+ * @public
9
+ */
10
+ export type EditorEmittedEvent =
11
+ | {
12
+ type: 'blurred'
13
+ event: FocusEvent<HTMLDivElement, Element>
14
+ }
15
+ | {
16
+ type: 'done loading'
17
+ }
18
+ | {
19
+ type: 'editable'
20
+ }
21
+ | {
22
+ type: 'focused'
23
+ event: FocusEvent<HTMLDivElement, Element>
24
+ }
25
+ | {
26
+ type: 'invalid value'
27
+ resolution: InvalidValueResolution | null
28
+ value: Array<PortableTextBlock> | undefined
29
+ }
30
+ | {
31
+ type: 'loading'
32
+ }
33
+ | MutationEvent
34
+ | PatchEvent
35
+ | {
36
+ type: 'read only'
37
+ }
38
+ | {
39
+ type: 'ready'
40
+ }
41
+ | {
42
+ type: 'selection'
43
+ selection: EditorSelection
44
+ }
45
+ | {
46
+ type: 'value changed'
47
+ value: Array<PortableTextBlock> | undefined
48
+ }
49
+
50
+ export type InternalEditorEmittedEvent = EditorEmittedEvent | UnsetEvent
51
+
52
+ /**
53
+ * @public
54
+ */
55
+ export type MutationEvent = {
56
+ type: 'mutation'
57
+ patches: Array<Patch>
58
+ /**
59
+ * @deprecated Use `value` instead
60
+ */
61
+ snapshot: Array<PortableTextBlock> | undefined
62
+ value: Array<PortableTextBlock> | undefined
63
+ }
64
+
65
+ export type PatchEvent = {
66
+ type: 'patch'
67
+ patch: Patch
68
+ }
69
+
70
+ export type UnsetEvent = {
71
+ type: 'unset'
72
+ previousValue: Array<PortableTextBlock>
73
+ }
74
+
75
+ export type RelayActor = ActorRefFrom<typeof relayMachine>
76
+
77
+ export const relayMachine = setup({
78
+ types: {
79
+ events: {} as InternalEditorEmittedEvent,
80
+ emitted: {} as InternalEditorEmittedEvent,
81
+ },
82
+ }).createMachine({
83
+ id: 'relay',
84
+ on: {
85
+ '*': {
86
+ actions: emit(({event}) => event),
87
+ },
88
+ },
89
+ })
@@ -1,10 +1,10 @@
1
1
  import {useEffect} from 'react'
2
2
  import {useEffectEvent} from 'use-effect-event'
3
3
  import type {EditorChange} from '../types/editor'
4
- import type {EditorActor, InternalEditorEmittedEvent} from './editor-machine'
4
+ import type {InternalEditorEmittedEvent, RelayActor} from './relay-machine'
5
5
 
6
6
  export function RouteEventsToChanges(props: {
7
- editorActor: EditorActor
7
+ relayActor: RelayActor
8
8
  onChange: (change: EditorChange) => void
9
9
  }) {
10
10
  // We want to ensure that _when_ `props.onChange` is called, it uses the current value.
@@ -16,7 +16,7 @@ export function RouteEventsToChanges(props: {
16
16
  )
17
17
 
18
18
  useEffect(() => {
19
- const sub = props.editorActor.on('*', (event) => {
19
+ const sub = props.relayActor.on('*', (event) => {
20
20
  const change = eventToChange(event)
21
21
 
22
22
  if (change) {
@@ -26,7 +26,7 @@ export function RouteEventsToChanges(props: {
26
26
  return () => {
27
27
  sub.unsubscribe()
28
28
  }
29
- }, [props.editorActor])
29
+ }, [props.relayActor])
30
30
 
31
31
  return null
32
32
  }
@@ -59,12 +59,6 @@ export function eventToChange(
59
59
  value: event.value,
60
60
  }
61
61
  }
62
- case 'error': {
63
- return {
64
- ...event,
65
- level: 'warning',
66
- }
67
- }
68
62
  case 'mutation': {
69
63
  return event
70
64
  }
@@ -114,7 +114,7 @@ export const syncMachine = setup({
114
114
  },
115
115
  events: {} as
116
116
  | {
117
- type: 'has pending patches'
117
+ type: 'has pending mutations'
118
118
  }
119
119
  | {
120
120
  type: 'mutation'
@@ -226,7 +226,7 @@ export const syncMachine = setup({
226
226
  }),
227
227
  ],
228
228
  on: {
229
- 'has pending patches': {
229
+ 'has pending mutations': {
230
230
  actions: assign({
231
231
  isProcessingLocalChanges: true,
232
232
  }),
@@ -1,6 +1,6 @@
1
1
  import {useEffect} from 'react'
2
2
  import {useEffectEvent} from 'use-effect-event'
3
- import type {EditorEmittedEvent} from './editor/editor-machine'
3
+ import type {EditorEmittedEvent} from './editor/relay-machine'
4
4
  import {useEditor} from './editor/use-editor'
5
5
 
6
6
  /**
package/src/editor.ts CHANGED
@@ -6,12 +6,10 @@ import type {
6
6
  import type {ActorRef, EventObject, Snapshot} from 'xstate'
7
7
  import type {Behavior} from './behaviors/behavior.types.behavior'
8
8
  import type {ExternalBehaviorEvent} from './behaviors/behavior.types.event'
9
- import type {
10
- EditorEmittedEvent,
11
- ExternalEditorEvent,
12
- } from './editor/editor-machine'
9
+ import type {ExternalEditorEvent} from './editor/editor-machine'
13
10
  import type {SchemaDefinition} from './editor/editor-schema'
14
11
  import type {EditorSnapshot} from './editor/editor-snapshot'
12
+ import type {EditorEmittedEvent} from './editor/relay-machine'
15
13
 
16
14
  /**
17
15
  * @public
package/src/index.ts CHANGED
@@ -4,15 +4,11 @@ export type {
4
4
  PortableTextChild,
5
5
  PortableTextSpan,
6
6
  } from '@sanity/types'
7
- export {EditorEventListener} from './editor-event-listener'
8
7
  export type {Editor, EditorConfig, EditorEvent} from './editor'
8
+ export {EditorEventListener} from './editor-event-listener'
9
9
  export {PortableTextEditable} from './editor/Editable'
10
10
  export type {PortableTextEditableProps} from './editor/Editable'
11
- export type {
12
- EditorEmittedEvent,
13
- MutationEvent,
14
- PatchesEvent,
15
- } from './editor/editor-machine'
11
+ export type {PatchesEvent} from './editor/editor-machine'
16
12
  export {
17
13
  EditorProvider,
18
14
  type EditorProviderProps,
@@ -30,6 +26,7 @@ export {usePortableTextEditorSelection} from './editor/hooks/usePortableTextEdit
30
26
  export {defaultKeyGenerator as keyGenerator} from './editor/key-generator'
31
27
  export {PortableTextEditor} from './editor/PortableTextEditor'
32
28
  export type {PortableTextEditorProps} from './editor/PortableTextEditor'
29
+ export type {EditorEmittedEvent, MutationEvent} from './editor/relay-machine'
33
30
  export {useEditor} from './editor/use-editor'
34
31
  export type {AddedAnnotationPaths} from './operations/behavior.operation.annotation.add'
35
32
  export type {BlockOffset} from './types/block-offset'
@@ -8,6 +8,7 @@ import {legacySchemaToEditorSchema} from '../../editor/editor-schema'
8
8
  import {defaultKeyGenerator} from '../../editor/key-generator'
9
9
  import {createLegacySchema} from '../../editor/legacy-schema'
10
10
  import {withPlugins} from '../../editor/plugins/with-plugins'
11
+ import {relayMachine} from '../../editor/relay-machine'
11
12
  import {createOperationToPatches} from '../operationToPatches'
12
13
 
13
14
  const legacySchema = createLegacySchema(schemaType)
@@ -19,11 +20,12 @@ const editorActor = createActor(editorMachine, {
19
20
  getLegacySchema: () => legacySchema,
20
21
  },
21
22
  })
22
-
23
+ const relayActor = createActor(relayMachine)
23
24
  const operationToPatches = createOperationToPatches(editorActor)
24
25
 
25
26
  const editor = withPlugins(createEditor(), {
26
27
  editorActor,
28
+ relayActor,
27
29
  subscriptions: [],
28
30
  })
29
31
 
@@ -8,6 +8,7 @@ import {legacySchemaToEditorSchema} from '../../editor/editor-schema'
8
8
  import {defaultKeyGenerator} from '../../editor/key-generator'
9
9
  import {createLegacySchema} from '../../editor/legacy-schema'
10
10
  import {withPlugins} from '../../editor/plugins/with-plugins'
11
+ import {relayMachine} from '../../editor/relay-machine'
11
12
  import {createApplyPatch} from '../applyPatch'
12
13
  import {VOID_CHILD_KEY} from '../values'
13
14
 
@@ -24,6 +25,7 @@ const editor = withPlugins(createEditor(), {
24
25
  getLegacySchema: () => legacySchema,
25
26
  },
26
27
  }),
28
+ relayActor: createActor(relayMachine),
27
29
  subscriptions: [],
28
30
  })
29
31
 
@@ -181,9 +181,13 @@ function insertPatch(
181
181
 
182
182
  Transforms.insertNodes(editor, blocksToInsert, {at: [normalizedIdx]})
183
183
 
184
- if (editorWasEmptyBefore) {
184
+ if (
185
+ editorWasEmptyBefore &&
186
+ typeof patch.path[0] === 'number' &&
187
+ patch.path[0] === 0
188
+ ) {
185
189
  Transforms.removeNodes(editor, {
186
- at: [position === 'after' ? targetBlockIndex + 1 : targetBlockIndex],
190
+ at: [position === 'before' ? targetBlockIndex + 1 : targetBlockIndex],
187
191
  })
188
192
  }
189
193
 
@@ -1,6 +1,6 @@
1
1
  import {useEffect} from 'react'
2
2
  import {useEffectEvent} from 'use-effect-event'
3
- import type {EditorEmittedEvent} from '../editor/editor-machine'
3
+ import type {EditorEmittedEvent} from '../editor/relay-machine'
4
4
  import {useEditor} from '../editor/use-editor'
5
5
 
6
6
  /**
@@ -222,7 +222,9 @@ export type ReadyChange = {
222
222
 
223
223
  /**
224
224
  * The editor produced an error
225
- * @beta */
225
+ * @beta
226
+ * @deprecated The change is no longer emitted
227
+ * */
226
228
  export type ErrorChange = {
227
229
  type: 'error'
228
230
  name: string // short computer readable name
@@ -266,7 +268,9 @@ export type InvalidValue = {
266
268
 
267
269
  /**
268
270
  * The editor performed a undo history step
269
- * @beta */
271
+ * @beta
272
+ * @deprecated The change is no longer emitted
273
+ * */
270
274
  export type UndoChange = {
271
275
  type: 'undo'
272
276
  patches: Patch[]
@@ -275,7 +279,9 @@ export type UndoChange = {
275
279
 
276
280
  /**
277
281
  * The editor performed redo history step
278
- * @beta */
282
+ * @beta
283
+ * @deprecated The change is no longer emitted
284
+ * */
279
285
  export type RedoChange = {
280
286
  type: 'redo'
281
287
  patches: Patch[]
@@ -285,7 +291,9 @@ export type RedoChange = {
285
291
  /**
286
292
  * The editor was either connected or disconnected to the network
287
293
  * To show out of sync warnings etc when in collaborative mode.
288
- * @beta */
294
+ * @beta
295
+ * @deprecated The change is no longer emitted
296
+ * */
289
297
  export type ConnectionChange = {
290
298
  type: 'connection'
291
299
  value: 'online' | 'offline'