@portabletext/editor 1.47.6 → 1.47.7

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.
@@ -3,32 +3,23 @@ import type {
3
3
  ArraySchemaType,
4
4
  PortableTextBlock,
5
5
  } from '@sanity/types'
6
- import {useActorRef} from '@xstate/react'
7
- import {useMemo} from 'react'
8
- import {
9
- createActor,
10
- type ActorRef,
11
- type EventObject,
12
- type Snapshot,
13
- } from 'xstate'
6
+ import type {ActorRef, EventObject, Snapshot} from 'xstate'
14
7
  import type {Behavior} from '../behaviors/behavior.types.behavior'
15
8
  import type {ExternalBehaviorEvent} from '../behaviors/behavior.types.event'
16
9
  import {createCoreConverters} from '../converters/converters.core'
17
10
  import {compileType} from '../internal-utils/schema'
18
- import type {EditableAPI, PortableTextMemberSchemaTypes} from '../types/editor'
11
+ import type {EditableAPI} from '../types/editor'
19
12
  import {createSlateEditor, type SlateEditor} from './create-slate-editor'
20
- import {
21
- editorMachine,
22
- type EditorActor,
23
- type EditorEmittedEvent,
24
- type ExternalEditorEvent,
13
+ import type {
14
+ EditorActor,
15
+ EditorEmittedEvent,
16
+ ExternalEditorEvent,
25
17
  } from './editor-machine'
26
18
  import {
27
19
  compileSchemaDefinitionToLegacySchema,
28
20
  legacySchemaToEditorSchema,
29
21
  type SchemaDefinition,
30
22
  } from './editor-schema'
31
- import type {EditorSchema} from './editor-schema'
32
23
  import {getEditorSnapshot} from './editor-selector'
33
24
  import type {EditorSnapshot} from './editor-snapshot'
34
25
  import {defaultKeyGenerator} from './key-generator'
@@ -84,11 +75,10 @@ export type InternalEditor = Editor & {
84
75
  editable: EditableAPI
85
76
  editorActor: EditorActor
86
77
  slateEditor: SlateEditor
87
- legacySchema: PortableTextMemberSchemaTypes
88
78
  }
89
79
  }
90
80
 
91
- export function createInternalEditor(config: EditorConfig): InternalEditor {
81
+ function compileSchemasFromEditorConfig(config: EditorConfig) {
92
82
  const legacySchema = config.schemaDefinition
93
83
  ? compileSchemaDefinitionToLegacySchema(config.schemaDefinition)
94
84
  : createLegacySchema(
@@ -96,65 +86,30 @@ export function createInternalEditor(config: EditorConfig): InternalEditor {
96
86
  ? config.schema
97
87
  : compileType(config.schema),
98
88
  )
99
- const editorSchema = legacySchemaToEditorSchema(legacySchema)
100
-
101
- const editorActor = createActor(editorMachine, {
102
- input: editorConfigToMachineInput({
103
- ...config,
104
- schema: editorSchema,
105
- legacySchema,
106
- }),
107
- })
108
- editorActor.start()
89
+ const schema = legacySchemaToEditorSchema(legacySchema)
109
90
 
110
- return createInternalEditorFromActor(editorActor, legacySchema)
91
+ return {
92
+ legacySchema,
93
+ schema,
94
+ }
111
95
  }
112
96
 
113
- export function useCreateInternalEditor(config: EditorConfig): InternalEditor {
114
- const legacySchema = config.schemaDefinition
115
- ? compileSchemaDefinitionToLegacySchema(config.schemaDefinition)
116
- : createLegacySchema(
117
- config.schema.hasOwnProperty('jsonType')
118
- ? config.schema
119
- : compileType(config.schema),
120
- )
121
- const editorSchema = legacySchemaToEditorSchema(legacySchema)
122
-
123
- const editorActor = useActorRef(editorMachine, {
124
- input: editorConfigToMachineInput({
125
- ...config,
126
- schema: editorSchema,
127
- legacySchema,
128
- }),
129
- })
130
-
131
- return useMemo(
132
- () => createInternalEditorFromActor(editorActor, legacySchema),
133
- [editorActor, legacySchema],
134
- )
135
- }
97
+ export function editorConfigToMachineInput(config: EditorConfig) {
98
+ const {legacySchema, schema} = compileSchemasFromEditorConfig(config)
136
99
 
137
- function editorConfigToMachineInput(
138
- config: Omit<EditorConfig, 'schema'> & {
139
- schema: EditorSchema
140
- legacySchema: PortableTextMemberSchemaTypes
141
- },
142
- ) {
143
100
  return {
144
101
  behaviors: config.behaviors,
145
- converters: createCoreConverters(config.legacySchema),
102
+ converters: createCoreConverters(legacySchema),
103
+ getLegacySchema: () => legacySchema,
146
104
  keyGenerator: config.keyGenerator ?? defaultKeyGenerator,
147
105
  maxBlocks: config.maxBlocks,
148
106
  readOnly: config.readOnly,
149
- schema: config.schema,
107
+ schema,
150
108
  initialValue: config.initialValue,
151
109
  } as const
152
110
  }
153
111
 
154
- function createInternalEditorFromActor(
155
- editorActor: EditorActor,
156
- legacySchema: PortableTextMemberSchemaTypes,
157
- ): InternalEditor {
112
+ export function createInternalEditor(editorActor: EditorActor): InternalEditor {
158
113
  const slateEditor = createSlateEditor({editorActor})
159
114
  const editable = createEditableAPI(slateEditor.instance, editorActor)
160
115
 
@@ -254,7 +209,6 @@ function createInternalEditorFromActor(
254
209
  _internal: {
255
210
  editable,
256
211
  editorActor,
257
- legacySchema,
258
212
  slateEditor,
259
213
  },
260
214
  }
@@ -21,6 +21,7 @@ import type {NamespaceEvent} from '../type-utils'
21
21
  import type {
22
22
  EditorSelection,
23
23
  InvalidValueResolution,
24
+ PortableTextMemberSchemaTypes,
24
25
  PortableTextSlateEditor,
25
26
  } from '../types/editor'
26
27
  import type {EditorSchema} from './editor-schema'
@@ -215,6 +216,7 @@ export const editorMachine = setup({
215
216
  context: {} as {
216
217
  behaviors: Set<Behavior>
217
218
  converters: Set<Converter>
219
+ getLegacySchema: () => PortableTextMemberSchemaTypes
218
220
  keyGenerator: () => string
219
221
  pendingEvents: Array<InternalPatchEvent | MutationEvent>
220
222
  schema: EditorSchema
@@ -233,6 +235,7 @@ export const editorMachine = setup({
233
235
  input: {} as {
234
236
  behaviors?: Array<Behavior>
235
237
  converters?: Array<Converter>
238
+ getLegacySchema: () => PortableTextMemberSchemaTypes
236
239
  keyGenerator: () => string
237
240
  maxBlocks?: number
238
241
  readOnly?: boolean
@@ -365,6 +368,7 @@ export const editorMachine = setup({
365
368
  context: ({input}) => ({
366
369
  behaviors: new Set([...(input.behaviors ?? coreBehaviors)]),
367
370
  converters: new Set(input.converters ?? []),
371
+ getLegacySchema: input.getLegacySchema,
368
372
  keyGenerator: input.keyGenerator,
369
373
  pendingEvents: [],
370
374
  schema: input.schema,
@@ -1,12 +1,15 @@
1
+ import {useActorRef} from '@xstate/react'
1
2
  import React, {useMemo} from 'react'
2
3
  import {Slate} from 'slate-react'
3
4
  import {Synchronizer} from './components/Synchronizer'
4
5
  import {
5
- useCreateInternalEditor,
6
+ createInternalEditor,
7
+ editorConfigToMachineInput,
6
8
  type Editor,
7
9
  type EditorConfig,
8
10
  } from './create-editor'
9
11
  import {EditorActorContext} from './editor-actor-context'
12
+ import {editorMachine} from './editor-machine'
10
13
  import {PortableTextEditorContext} from './hooks/usePortableTextEditor'
11
14
  import {PortableTextEditorSelectionProvider} from './hooks/usePortableTextEditorSelection'
12
15
  import {
@@ -44,9 +47,13 @@ export type EditorProviderProps = {
44
47
  * @group Components
45
48
  */
46
49
  export function EditorProvider(props: EditorProviderProps) {
47
- const internalEditor = useCreateInternalEditor(props.initialConfig)
48
- const editorActor = internalEditor._internal.editorActor
49
- const slateEditor = internalEditor._internal.slateEditor
50
+ const editorActor = useActorRef(editorMachine, {
51
+ input: editorConfigToMachineInput(props.initialConfig),
52
+ })
53
+ const internalEditor = useMemo(
54
+ () => createInternalEditor(editorActor),
55
+ [editorActor],
56
+ )
50
57
  const portableTextEditor = useMemo(
51
58
  () =>
52
59
  new PortableTextEditor({
@@ -65,12 +72,12 @@ export function EditorProvider(props: EditorProviderProps) {
65
72
  />
66
73
  <Synchronizer
67
74
  editorActor={editorActor}
68
- slateEditor={slateEditor.instance}
75
+ slateEditor={internalEditor._internal.slateEditor.instance}
69
76
  />
70
77
  <EditorActorContext.Provider value={editorActor}>
71
78
  <Slate
72
- editor={slateEditor.instance}
73
- initialValue={slateEditor.initialValue}
79
+ editor={internalEditor._internal.slateEditor.instance}
80
+ initialValue={internalEditor._internal.slateEditor.initialValue}
74
81
  >
75
82
  <PortableTextEditorContext.Provider value={portableTextEditor}>
76
83
  <PortableTextEditorSelectionProvider editorActor={editorActor}>
@@ -11,12 +11,14 @@ import {createLegacySchema} from '../../editor/legacy-schema'
11
11
  import {withPlugins} from '../../editor/plugins/with-plugins'
12
12
  import {createOperationToPatches} from '../operationToPatches'
13
13
 
14
- const schemaTypes = legacySchemaToEditorSchema(createLegacySchema(schemaType))
14
+ const legacySchema = createLegacySchema(schemaType)
15
+ const schemaTypes = legacySchemaToEditorSchema(legacySchema)
15
16
  const editorActor = createActor(editorMachine, {
16
17
  input: {
17
18
  behaviors: coreBehaviors,
18
19
  schema: schemaTypes,
19
20
  keyGenerator: defaultKeyGenerator,
21
+ getLegacySchema: () => legacySchema,
20
22
  },
21
23
  })
22
24
 
@@ -12,7 +12,8 @@ import {withPlugins} from '../../editor/plugins/with-plugins'
12
12
  import {createApplyPatch} from '../applyPatch'
13
13
  import {VOID_CHILD_KEY} from '../values'
14
14
 
15
- const schemaTypes = legacySchemaToEditorSchema(createLegacySchema(schemaType))
15
+ const legacySchema = createLegacySchema(schemaType)
16
+ const schemaTypes = legacySchemaToEditorSchema(legacySchema)
16
17
 
17
18
  const patchToOperations = createApplyPatch(schemaTypes)
18
19
 
@@ -22,6 +23,7 @@ const editor = withPlugins(createEditor(), {
22
23
  behaviors: coreBehaviors,
23
24
  schema: schemaTypes,
24
25
  keyGenerator: defaultKeyGenerator,
26
+ getLegacySchema: () => legacySchema,
25
27
  },
26
28
  }),
27
29
  subscriptions: [],