@portabletext/editor 1.47.5 → 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.
- package/lib/_chunks-cjs/editor-provider.cjs +2104 -2119
- package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
- package/lib/_chunks-cjs/parse-blocks.cjs +6 -7
- package/lib/_chunks-cjs/parse-blocks.cjs.map +1 -1
- package/lib/_chunks-es/editor-provider.js +2106 -2121
- package/lib/_chunks-es/editor-provider.js.map +1 -1
- package/lib/_chunks-es/parse-blocks.js +6 -7
- package/lib/_chunks-es/parse-blocks.js.map +1 -1
- package/lib/behaviors/index.d.cts +48 -1
- package/lib/behaviors/index.d.ts +48 -1
- package/lib/index.d.cts +28 -3
- package/lib/index.d.ts +28 -3
- package/lib/plugins/index.d.cts +48 -2
- package/lib/plugins/index.d.ts +48 -2
- package/lib/selectors/index.d.cts +51 -1
- package/lib/selectors/index.d.ts +51 -1
- package/lib/utils/index.d.cts +53 -6
- package/lib/utils/index.d.ts +53 -6
- package/package.json +7 -7
- package/src/editor/PortableTextEditor.tsx +29 -11
- package/src/editor/create-editor.ts +18 -64
- package/src/editor/editor-machine.ts +4 -0
- package/src/editor/editor-provider.tsx +14 -7
- package/src/internal-utils/__tests__/operationToPatches.test.ts +3 -1
- package/src/internal-utils/__tests__/patchToOperations.test.ts +3 -1
- package/src/internal-utils/parse-blocks.test.ts +0 -1
- package/src/internal-utils/parse-blocks.ts +11 -21
|
@@ -3,32 +3,23 @@ import type {
|
|
|
3
3
|
ArraySchemaType,
|
|
4
4
|
PortableTextBlock,
|
|
5
5
|
} from '@sanity/types'
|
|
6
|
-
import {
|
|
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
|
|
11
|
+
import type {EditableAPI} from '../types/editor'
|
|
19
12
|
import {createSlateEditor, type SlateEditor} from './create-slate-editor'
|
|
20
|
-
import {
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
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
|
|
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
|
|
91
|
+
return {
|
|
92
|
+
legacySchema,
|
|
93
|
+
schema,
|
|
94
|
+
}
|
|
111
95
|
}
|
|
112
96
|
|
|
113
|
-
export function
|
|
114
|
-
const legacySchema = config
|
|
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(
|
|
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
|
|
107
|
+
schema,
|
|
150
108
|
initialValue: config.initialValue,
|
|
151
109
|
} as const
|
|
152
110
|
}
|
|
153
111
|
|
|
154
|
-
function
|
|
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
|
-
|
|
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
|
|
48
|
-
|
|
49
|
-
|
|
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
|
|
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
|
|
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: [],
|
|
@@ -169,8 +169,7 @@ function parseTextBlock({
|
|
|
169
169
|
.filter((child) => child !== undefined)
|
|
170
170
|
|
|
171
171
|
const parsedBlock: PortableTextTextBlock = {
|
|
172
|
-
|
|
173
|
-
...block,
|
|
172
|
+
_type: context.schema.block.name,
|
|
174
173
|
_key,
|
|
175
174
|
children:
|
|
176
175
|
children.length > 0
|
|
@@ -186,37 +185,30 @@ function parseTextBlock({
|
|
|
186
185
|
markDefs,
|
|
187
186
|
}
|
|
188
187
|
|
|
189
|
-
/**
|
|
190
|
-
* Reset text block .style if it's somehow set to an invalid type
|
|
191
|
-
*/
|
|
192
188
|
if (
|
|
193
|
-
typeof
|
|
194
|
-
|
|
189
|
+
typeof block.style === 'string' &&
|
|
190
|
+
context.schema.styles.find((style) => style.name === block.style)
|
|
195
191
|
) {
|
|
192
|
+
parsedBlock.style = block.style
|
|
193
|
+
} else {
|
|
196
194
|
const defaultStyle = context.schema.styles.at(0)?.name
|
|
197
195
|
|
|
198
196
|
if (defaultStyle !== undefined) {
|
|
199
197
|
parsedBlock.style = defaultStyle
|
|
200
198
|
} else {
|
|
201
|
-
|
|
199
|
+
console.error('Expected default style')
|
|
202
200
|
}
|
|
203
201
|
}
|
|
204
202
|
|
|
205
|
-
/**
|
|
206
|
-
* Reset text block .listItem if it's somehow set to an invalid type
|
|
207
|
-
*/
|
|
208
203
|
if (
|
|
209
|
-
typeof
|
|
210
|
-
|
|
204
|
+
typeof block.listItem === 'string' &&
|
|
205
|
+
context.schema.lists.find((list) => list.name === block.listItem)
|
|
211
206
|
) {
|
|
212
|
-
|
|
207
|
+
parsedBlock.listItem = block.listItem
|
|
213
208
|
}
|
|
214
209
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
*/
|
|
218
|
-
if (typeof parsedBlock.level !== 'number') {
|
|
219
|
-
delete parsedBlock.level
|
|
210
|
+
if (typeof block.level === 'number') {
|
|
211
|
+
parsedBlock.level = block.level
|
|
220
212
|
}
|
|
221
213
|
|
|
222
214
|
return parsedBlock
|
|
@@ -280,8 +272,6 @@ export function parseSpan({
|
|
|
280
272
|
})
|
|
281
273
|
|
|
282
274
|
return {
|
|
283
|
-
// Spread the entire span to allow custom properties on it
|
|
284
|
-
...span,
|
|
285
275
|
_type: 'span',
|
|
286
276
|
_key: options.refreshKeys
|
|
287
277
|
? context.keyGenerator()
|