@portabletext/editor 1.46.0 → 1.47.1
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/behavior.core.cjs +7 -7
- package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
- package/lib/_chunks-cjs/behavior.markdown.cjs.map +1 -1
- package/lib/_chunks-cjs/editor-provider.cjs +411 -327
- package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
- package/lib/_chunks-cjs/parse-blocks.cjs +4 -4
- package/lib/_chunks-cjs/parse-blocks.cjs.map +1 -1
- package/lib/_chunks-cjs/selector.is-overlapping-selection.cjs.map +1 -1
- package/lib/_chunks-es/behavior.core.js +7 -7
- package/lib/_chunks-es/behavior.core.js.map +1 -1
- package/lib/_chunks-es/behavior.markdown.js.map +1 -1
- package/lib/_chunks-es/editor-provider.js +412 -328
- package/lib/_chunks-es/editor-provider.js.map +1 -1
- package/lib/_chunks-es/parse-blocks.js +4 -4
- package/lib/_chunks-es/parse-blocks.js.map +1 -1
- package/lib/_chunks-es/selector.is-overlapping-selection.js.map +1 -1
- package/lib/behaviors/index.cjs.map +1 -1
- package/lib/behaviors/index.d.cts +590 -28
- package/lib/behaviors/index.d.ts +590 -28
- package/lib/behaviors/index.js.map +1 -1
- package/lib/index.cjs +4 -4
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +71 -3
- package/lib/index.d.ts +71 -3
- package/lib/index.js +4 -4
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.cjs.map +1 -1
- package/lib/plugins/index.d.cts +78 -23
- package/lib/plugins/index.d.ts +78 -23
- package/lib/plugins/index.js.map +1 -1
- package/lib/selectors/index.d.cts +79 -27
- package/lib/selectors/index.d.ts +79 -27
- package/lib/utils/index.d.cts +84 -29
- package/lib/utils/index.d.ts +84 -29
- package/package.json +6 -6
- package/src/behavior-actions/behavior.action.insert.block.ts +1 -1
- package/src/behavior-actions/behavior.action.move.backward.ts +12 -0
- package/src/behavior-actions/behavior.action.move.forward.ts +11 -0
- package/src/behavior-actions/behavior.action.split.block.ts +1 -1
- package/src/behavior-actions/behavior.actions.ts +18 -0
- package/src/behavior-actions/behavior.guards.ts +1 -1
- package/src/behaviors/behavior.core.decorators.ts +4 -4
- package/src/behaviors/behavior.core.insert-break.ts +4 -4
- package/src/behaviors/behavior.decorator-pair.ts +1 -1
- package/src/behaviors/behavior.links.ts +1 -1
- package/src/behaviors/behavior.markdown.ts +1 -1
- package/src/behaviors/behavior.perform-event.ts +1 -1
- package/src/behaviors/behavior.types.event.ts +10 -0
- package/src/converters/converter.portable-text.deserialize.test.ts +2 -3
- package/src/converters/converter.text-html.deserialize.test.ts +62 -5
- package/src/converters/converter.text-html.serialize.test.ts +9 -5
- package/src/converters/converter.text-html.ts +66 -64
- package/src/converters/converter.text-plain.test.ts +9 -4
- package/src/converters/converter.text-plain.ts +91 -87
- package/src/converters/converters.core.ts +13 -8
- package/src/editor/Editable.tsx +10 -5
- package/src/editor/PortableTextEditor.tsx +5 -5
- package/src/editor/__tests__/PortableTextEditorTester.tsx +4 -3
- package/src/editor/__tests__/insert-block.test.tsx +10 -10
- package/src/editor/__tests__/self-solving.test.tsx +2 -2
- package/src/editor/components/Synchronizer.tsx +1 -1
- package/src/editor/create-editor.ts +51 -18
- package/src/editor/editor-machine.ts +1 -1
- package/src/editor/{define-schema.ts → editor-schema.ts} +114 -5
- package/src/editor/editor-snapshot.ts +1 -1
- package/src/editor/get-active-decorators.ts +2 -2
- package/src/editor/{create-editor-schema.ts → legacy-schema.ts} +3 -3
- package/src/editor/mutation-machine.ts +1 -1
- package/src/editor/plugins/createWithObjectKeys.ts +6 -9
- package/src/editor/plugins/createWithPatches.ts +12 -11
- package/src/editor/plugins/createWithPortableTextBlockStyle.ts +2 -6
- package/src/editor/plugins/createWithPortableTextMarkModel.ts +15 -12
- package/src/editor/plugins/createWithSchemaTypes.ts +24 -16
- package/src/editor/plugins/createWithUndoRedo.ts +3 -4
- package/src/editor/plugins/createWithUtils.ts +12 -10
- package/src/editor/plugins/with-plugins.ts +5 -15
- package/src/editor/range-decorations-machine.ts +1 -1
- package/src/editor/sync-machine.ts +1 -1
- package/src/index.ts +2 -2
- package/src/internal-utils/__tests__/operationToPatches.test.ts +12 -10
- package/src/internal-utils/__tests__/patchToOperations.test.ts +3 -2
- package/src/internal-utils/__tests__/values.test.ts +3 -2
- package/src/internal-utils/applyPatch.ts +7 -9
- package/src/internal-utils/create-test-snapshot.ts +1 -1
- package/src/internal-utils/drag-selection.test.ts +1 -1
- package/src/internal-utils/operationToPatches.ts +4 -6
- package/src/internal-utils/parse-blocks.test.ts +1 -1
- package/src/internal-utils/parse-blocks.ts +8 -7
- package/src/internal-utils/slate-children-to-blocks.ts +1 -1
- package/src/internal-utils/slate-utils.ts +1 -1
- package/src/internal-utils/validateValue.ts +4 -6
- package/src/internal-utils/values.ts +5 -5
- package/src/plugins/plugin.decorator-shortcut.ts +1 -1
- package/src/plugins/plugin.markdown.test.tsx +1 -1
- package/src/plugins/plugin.markdown.tsx +1 -1
- package/src/selectors/selector.get-selection-text.test.ts +1 -1
- package/src/selectors/selector.get-trimmed-selection.test.ts +1 -1
|
@@ -29,12 +29,13 @@ import type {
|
|
|
29
29
|
} from '../types/editor'
|
|
30
30
|
import {Synchronizer} from './components/Synchronizer'
|
|
31
31
|
import {createInternalEditor, type InternalEditor} from './create-editor'
|
|
32
|
-
import {createEditorSchema} from './create-editor-schema'
|
|
33
32
|
import {EditorActorContext} from './editor-actor-context'
|
|
34
33
|
import type {EditorActor} from './editor-machine'
|
|
34
|
+
import {legacySchemaToEditorSchema} from './editor-schema'
|
|
35
35
|
import {PortableTextEditorContext} from './hooks/usePortableTextEditor'
|
|
36
36
|
import {PortableTextEditorSelectionProvider} from './hooks/usePortableTextEditorSelection'
|
|
37
37
|
import {defaultKeyGenerator} from './key-generator'
|
|
38
|
+
import {createLegacySchema} from './legacy-schema'
|
|
38
39
|
|
|
39
40
|
const debug = debugWithName('component:PortableTextEditor')
|
|
40
41
|
|
|
@@ -148,8 +149,7 @@ export class PortableTextEditor extends Component<
|
|
|
148
149
|
})
|
|
149
150
|
}
|
|
150
151
|
|
|
151
|
-
this.schemaTypes =
|
|
152
|
-
this.editor._internal.editorActor.getSnapshot().context.schema
|
|
152
|
+
this.schemaTypes = this.editor._internal.legacySchema
|
|
153
153
|
this.editable = this.editor._internal.editable
|
|
154
154
|
}
|
|
155
155
|
|
|
@@ -160,7 +160,7 @@ export class PortableTextEditor extends Component<
|
|
|
160
160
|
!prevProps.editor &&
|
|
161
161
|
this.props.schemaType !== prevProps.schemaType
|
|
162
162
|
) {
|
|
163
|
-
this.schemaTypes =
|
|
163
|
+
this.schemaTypes = createLegacySchema(
|
|
164
164
|
this.props.schemaType.hasOwnProperty('jsonType')
|
|
165
165
|
? this.props.schemaType
|
|
166
166
|
: compileType(this.props.schemaType),
|
|
@@ -168,7 +168,7 @@ export class PortableTextEditor extends Component<
|
|
|
168
168
|
|
|
169
169
|
this.editor._internal.editorActor.send({
|
|
170
170
|
type: 'update schema',
|
|
171
|
-
schema: this.schemaTypes,
|
|
171
|
+
schema: legacySchemaToEditorSchema(this.schemaTypes),
|
|
172
172
|
})
|
|
173
173
|
}
|
|
174
174
|
|
|
@@ -10,8 +10,9 @@ import {
|
|
|
10
10
|
} from '../../index'
|
|
11
11
|
|
|
12
12
|
const imageType = defineField({
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
name: 'custom image',
|
|
14
|
+
type: 'object',
|
|
15
|
+
fields: [],
|
|
15
16
|
})
|
|
16
17
|
|
|
17
18
|
const someObject = defineField({
|
|
@@ -68,7 +69,7 @@ const colorAndLink = defineArrayMember({
|
|
|
68
69
|
|
|
69
70
|
const schema = Schema.compile({
|
|
70
71
|
name: 'test',
|
|
71
|
-
types: [portableTextType, colorAndLink],
|
|
72
|
+
types: [portableTextType, colorAndLink, imageType],
|
|
72
73
|
})
|
|
73
74
|
|
|
74
75
|
export const PortableTextEditorTester = forwardRef(
|
|
@@ -12,9 +12,9 @@ const schema = Schema.compile({
|
|
|
12
12
|
{
|
|
13
13
|
name: 'portable-text',
|
|
14
14
|
type: 'array',
|
|
15
|
-
of: [{type: 'block'}, {type: 'image'}],
|
|
15
|
+
of: [{type: 'block'}, {type: 'custom image'}],
|
|
16
16
|
},
|
|
17
|
-
{name: 'image', type: 'object'},
|
|
17
|
+
{name: 'custom image', type: 'object'},
|
|
18
18
|
],
|
|
19
19
|
}).get('portable-text')
|
|
20
20
|
|
|
@@ -71,7 +71,7 @@ describe(PortableTextEditor.insertBlock.name, () => {
|
|
|
71
71
|
await waitFor(() => {
|
|
72
72
|
if (editorRef.current) {
|
|
73
73
|
const imageBlockType = editorRef.current.schemaTypes.blockObjects.find(
|
|
74
|
-
(object) => object.name === 'image',
|
|
74
|
+
(object) => object.name === 'custom image',
|
|
75
75
|
)!
|
|
76
76
|
PortableTextEditor.insertBlock(editorRef.current, imageBlockType)
|
|
77
77
|
}
|
|
@@ -81,7 +81,7 @@ describe(PortableTextEditor.insertBlock.name, () => {
|
|
|
81
81
|
await waitFor(() => {
|
|
82
82
|
if (editorRef.current) {
|
|
83
83
|
expect(PortableTextEditor.getValue(editorRef.current)).toEqual([
|
|
84
|
-
{_key: 'bb', _type: 'image'},
|
|
84
|
+
{_key: 'bb', _type: 'custom image'},
|
|
85
85
|
])
|
|
86
86
|
}
|
|
87
87
|
})
|
|
@@ -140,7 +140,7 @@ describe(PortableTextEditor.insertBlock.name, () => {
|
|
|
140
140
|
await waitFor(() => {
|
|
141
141
|
if (editorRef.current) {
|
|
142
142
|
const imageBlockType = editorRef.current.schemaTypes.blockObjects.find(
|
|
143
|
-
(object) => object.name === 'image',
|
|
143
|
+
(object) => object.name === 'custom image',
|
|
144
144
|
)!
|
|
145
145
|
PortableTextEditor.insertBlock(editorRef.current, imageBlockType)
|
|
146
146
|
}
|
|
@@ -151,7 +151,7 @@ describe(PortableTextEditor.insertBlock.name, () => {
|
|
|
151
151
|
if (editorRef.current) {
|
|
152
152
|
expect(PortableTextEditor.getValue(editorRef.current)).toEqual([
|
|
153
153
|
nonEmptyTextBlock,
|
|
154
|
-
{_key: 'bb', _type: 'image'},
|
|
154
|
+
{_key: 'bb', _type: 'custom image'},
|
|
155
155
|
])
|
|
156
156
|
}
|
|
157
157
|
})
|
|
@@ -174,7 +174,7 @@ describe(PortableTextEditor.insertBlock.name, () => {
|
|
|
174
174
|
}
|
|
175
175
|
const imageBlock: PortableTextBlock = {
|
|
176
176
|
_key: 'bb',
|
|
177
|
-
_type: 'image',
|
|
177
|
+
_type: 'custom image',
|
|
178
178
|
}
|
|
179
179
|
const initialValue: Array<PortableTextBlock> = [emptyTextBlock, imageBlock]
|
|
180
180
|
const onChange: (change: EditorChange) => void = vi.fn()
|
|
@@ -226,7 +226,7 @@ describe(PortableTextEditor.insertBlock.name, () => {
|
|
|
226
226
|
await waitFor(() => {
|
|
227
227
|
if (editorRef.current) {
|
|
228
228
|
const imageBlockType = editorRef.current.schemaTypes.blockObjects.find(
|
|
229
|
-
(object) => object.name === 'image',
|
|
229
|
+
(object) => object.name === 'custom image',
|
|
230
230
|
)!
|
|
231
231
|
PortableTextEditor.insertBlock(editorRef.current, imageBlockType)
|
|
232
232
|
}
|
|
@@ -236,8 +236,8 @@ describe(PortableTextEditor.insertBlock.name, () => {
|
|
|
236
236
|
await waitFor(() => {
|
|
237
237
|
if (editorRef.current) {
|
|
238
238
|
expect(PortableTextEditor.getValue(editorRef.current)).toEqual([
|
|
239
|
-
{_key: 'bc', _type: 'image'},
|
|
240
|
-
{_key: 'bb', _type: 'image'},
|
|
239
|
+
{_key: 'bc', _type: 'custom image'},
|
|
240
|
+
{_key: 'bb', _type: 'custom image'},
|
|
241
241
|
])
|
|
242
242
|
}
|
|
243
243
|
})
|
|
@@ -13,9 +13,9 @@ const schema = Schema.compile({
|
|
|
13
13
|
{
|
|
14
14
|
name: 'portable-text',
|
|
15
15
|
type: 'array',
|
|
16
|
-
of: [{type: 'block'}, {type: 'image'}],
|
|
16
|
+
of: [{type: 'block'}, {type: 'custom image'}],
|
|
17
17
|
},
|
|
18
|
-
{name: 'image', type: 'object'},
|
|
18
|
+
{name: 'custom image', type: 'object'},
|
|
19
19
|
],
|
|
20
20
|
}).get('portable-text')
|
|
21
21
|
type OnChange = ComponentProps<typeof PortableTextEditor>['onChange']
|
|
@@ -44,7 +44,7 @@ export function Synchronizer(props: SynchronizerProps) {
|
|
|
44
44
|
})
|
|
45
45
|
const mutationActorRef = useActorRef(mutationMachine, {
|
|
46
46
|
input: {
|
|
47
|
-
schema: editorActor.getSnapshot().context.schema,
|
|
47
|
+
schema: props.editorActor.getSnapshot().context.schema,
|
|
48
48
|
slateEditor,
|
|
49
49
|
},
|
|
50
50
|
})
|
|
@@ -13,21 +13,26 @@ import {
|
|
|
13
13
|
} from 'xstate'
|
|
14
14
|
import type {Behavior} from '../behaviors/behavior.types.behavior'
|
|
15
15
|
import type {ExternalBehaviorEvent} from '../behaviors/behavior.types.event'
|
|
16
|
-
import {
|
|
16
|
+
import {createCoreConverters} from '../converters/converters.core'
|
|
17
17
|
import {compileType} from '../internal-utils/schema'
|
|
18
|
-
import type {EditableAPI} from '../types/editor'
|
|
19
|
-
import {createEditorSchema} from './create-editor-schema'
|
|
18
|
+
import type {EditableAPI, PortableTextMemberSchemaTypes} from '../types/editor'
|
|
20
19
|
import {createSlateEditor, type SlateEditor} from './create-slate-editor'
|
|
21
|
-
import {compileSchemaDefinition, type SchemaDefinition} from './define-schema'
|
|
22
20
|
import {
|
|
23
21
|
editorMachine,
|
|
24
22
|
type EditorActor,
|
|
25
23
|
type EditorEmittedEvent,
|
|
26
24
|
type ExternalEditorEvent,
|
|
27
25
|
} from './editor-machine'
|
|
26
|
+
import {
|
|
27
|
+
compileSchemaDefinitionToLegacySchema,
|
|
28
|
+
legacySchemaToEditorSchema,
|
|
29
|
+
type SchemaDefinition,
|
|
30
|
+
} from './editor-schema'
|
|
31
|
+
import type {EditorSchema} from './editor-schema'
|
|
28
32
|
import {getEditorSnapshot} from './editor-selector'
|
|
29
33
|
import type {EditorSnapshot} from './editor-snapshot'
|
|
30
34
|
import {defaultKeyGenerator} from './key-generator'
|
|
35
|
+
import {createLegacySchema} from './legacy-schema'
|
|
31
36
|
import {createEditableAPI} from './plugins/createWithEditableAPI'
|
|
32
37
|
|
|
33
38
|
/**
|
|
@@ -79,49 +84,76 @@ export type InternalEditor = Editor & {
|
|
|
79
84
|
editable: EditableAPI
|
|
80
85
|
editorActor: EditorActor
|
|
81
86
|
slateEditor: SlateEditor
|
|
87
|
+
legacySchema: PortableTextMemberSchemaTypes
|
|
82
88
|
}
|
|
83
89
|
}
|
|
84
90
|
|
|
85
91
|
export function createInternalEditor(config: EditorConfig): InternalEditor {
|
|
92
|
+
const legacySchema = config.schemaDefinition
|
|
93
|
+
? compileSchemaDefinitionToLegacySchema(config.schemaDefinition)
|
|
94
|
+
: createLegacySchema(
|
|
95
|
+
config.schema.hasOwnProperty('jsonType')
|
|
96
|
+
? config.schema
|
|
97
|
+
: compileType(config.schema),
|
|
98
|
+
)
|
|
99
|
+
const editorSchema = legacySchemaToEditorSchema(legacySchema)
|
|
100
|
+
|
|
86
101
|
const editorActor = createActor(editorMachine, {
|
|
87
|
-
input: editorConfigToMachineInput(
|
|
102
|
+
input: editorConfigToMachineInput({
|
|
103
|
+
...config,
|
|
104
|
+
schema: editorSchema,
|
|
105
|
+
legacySchema,
|
|
106
|
+
}),
|
|
88
107
|
})
|
|
89
108
|
editorActor.start()
|
|
90
109
|
|
|
91
|
-
return createInternalEditorFromActor(editorActor)
|
|
110
|
+
return createInternalEditorFromActor(editorActor, legacySchema)
|
|
92
111
|
}
|
|
93
112
|
|
|
94
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
|
+
|
|
95
123
|
const editorActor = useActorRef(editorMachine, {
|
|
96
|
-
input: editorConfigToMachineInput(
|
|
124
|
+
input: editorConfigToMachineInput({
|
|
125
|
+
...config,
|
|
126
|
+
schema: editorSchema,
|
|
127
|
+
legacySchema,
|
|
128
|
+
}),
|
|
97
129
|
})
|
|
98
130
|
|
|
99
131
|
return useMemo(
|
|
100
|
-
() => createInternalEditorFromActor(editorActor),
|
|
101
|
-
[editorActor],
|
|
132
|
+
() => createInternalEditorFromActor(editorActor, legacySchema),
|
|
133
|
+
[editorActor, legacySchema],
|
|
102
134
|
)
|
|
103
135
|
}
|
|
104
136
|
|
|
105
|
-
function editorConfigToMachineInput(
|
|
137
|
+
function editorConfigToMachineInput(
|
|
138
|
+
config: Omit<EditorConfig, 'schema'> & {
|
|
139
|
+
schema: EditorSchema
|
|
140
|
+
legacySchema: PortableTextMemberSchemaTypes
|
|
141
|
+
},
|
|
142
|
+
) {
|
|
106
143
|
return {
|
|
107
144
|
behaviors: config.behaviors,
|
|
108
|
-
converters:
|
|
145
|
+
converters: createCoreConverters(config.legacySchema),
|
|
109
146
|
keyGenerator: config.keyGenerator ?? defaultKeyGenerator,
|
|
110
147
|
maxBlocks: config.maxBlocks,
|
|
111
148
|
readOnly: config.readOnly,
|
|
112
|
-
schema: config.
|
|
113
|
-
? compileSchemaDefinition(config.schemaDefinition)
|
|
114
|
-
: createEditorSchema(
|
|
115
|
-
config.schema.hasOwnProperty('jsonType')
|
|
116
|
-
? config.schema
|
|
117
|
-
: compileType(config.schema),
|
|
118
|
-
),
|
|
149
|
+
schema: config.schema,
|
|
119
150
|
initialValue: config.initialValue,
|
|
120
151
|
} as const
|
|
121
152
|
}
|
|
122
153
|
|
|
123
154
|
function createInternalEditorFromActor(
|
|
124
155
|
editorActor: EditorActor,
|
|
156
|
+
legacySchema: PortableTextMemberSchemaTypes,
|
|
125
157
|
): InternalEditor {
|
|
126
158
|
const slateEditor = createSlateEditor({editorActor})
|
|
127
159
|
const editable = createEditableAPI(slateEditor.instance, editorActor)
|
|
@@ -208,6 +240,7 @@ function createInternalEditorFromActor(
|
|
|
208
240
|
_internal: {
|
|
209
241
|
editable,
|
|
210
242
|
editorActor,
|
|
243
|
+
legacySchema,
|
|
211
244
|
slateEditor,
|
|
212
245
|
},
|
|
213
246
|
}
|
|
@@ -21,7 +21,7 @@ import type {
|
|
|
21
21
|
InvalidValueResolution,
|
|
22
22
|
PortableTextSlateEditor,
|
|
23
23
|
} from '../types/editor'
|
|
24
|
-
import type {EditorSchema} from './
|
|
24
|
+
import type {EditorSchema} from './editor-schema'
|
|
25
25
|
import {createEditorSnapshot} from './editor-snapshot'
|
|
26
26
|
|
|
27
27
|
export * from 'xstate/guards'
|
|
@@ -2,8 +2,8 @@ import {Schema as SanitySchema} from '@sanity/schema'
|
|
|
2
2
|
import {defineField, defineType, type ObjectSchemaType} from '@sanity/types'
|
|
3
3
|
import startCase from 'lodash.startcase'
|
|
4
4
|
import type {PortableTextMemberSchemaTypes} from '../types/editor'
|
|
5
|
-
import {createEditorSchema} from './create-editor-schema'
|
|
6
5
|
import {defaultKeyGenerator} from './key-generator'
|
|
6
|
+
import {createLegacySchema} from './legacy-schema'
|
|
7
7
|
|
|
8
8
|
/**
|
|
9
9
|
* @public
|
|
@@ -88,11 +88,120 @@ const defaultObjectTitles: Record<string, string> = {
|
|
|
88
88
|
/**
|
|
89
89
|
* @public
|
|
90
90
|
*/
|
|
91
|
-
export type EditorSchema =
|
|
91
|
+
export type EditorSchema = {
|
|
92
|
+
annotations: ReadonlyArray<
|
|
93
|
+
BaseDefinition & {
|
|
94
|
+
fields: ReadonlyArray<{name: string; type: string}>
|
|
95
|
+
}
|
|
96
|
+
>
|
|
97
|
+
block: {
|
|
98
|
+
name: string
|
|
99
|
+
}
|
|
100
|
+
blockObjects: ReadonlyArray<
|
|
101
|
+
BaseDefinition & {
|
|
102
|
+
fields: ReadonlyArray<{name: string; type: string}>
|
|
103
|
+
}
|
|
104
|
+
>
|
|
105
|
+
decorators: ReadonlyArray<
|
|
106
|
+
BaseDefinition & {
|
|
107
|
+
/**
|
|
108
|
+
* @deprecated
|
|
109
|
+
* Use `name` instead
|
|
110
|
+
*/
|
|
111
|
+
value: string
|
|
112
|
+
}
|
|
113
|
+
>
|
|
114
|
+
inlineObjects: ReadonlyArray<
|
|
115
|
+
BaseDefinition & {
|
|
116
|
+
fields: ReadonlyArray<{name: string; type: string}>
|
|
117
|
+
}
|
|
118
|
+
>
|
|
119
|
+
span: {
|
|
120
|
+
name: string
|
|
121
|
+
}
|
|
122
|
+
styles: ReadonlyArray<
|
|
123
|
+
BaseDefinition & {
|
|
124
|
+
/**
|
|
125
|
+
* @deprecated
|
|
126
|
+
* Use `name` instead
|
|
127
|
+
*/
|
|
128
|
+
value: string
|
|
129
|
+
}
|
|
130
|
+
>
|
|
131
|
+
lists: ReadonlyArray<
|
|
132
|
+
BaseDefinition & {
|
|
133
|
+
/**
|
|
134
|
+
* @deprecated
|
|
135
|
+
* Use `name` instead
|
|
136
|
+
*/
|
|
137
|
+
value: string
|
|
138
|
+
}
|
|
139
|
+
>
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
export function legacySchemaToEditorSchema(
|
|
143
|
+
schema: PortableTextMemberSchemaTypes,
|
|
144
|
+
): EditorSchema {
|
|
145
|
+
return {
|
|
146
|
+
annotations: schema.annotations.map((annotation) => ({
|
|
147
|
+
name: annotation.name,
|
|
148
|
+
fields: annotation.fields.map((field) => ({
|
|
149
|
+
name: field.name,
|
|
150
|
+
type: field.type.jsonType,
|
|
151
|
+
})),
|
|
152
|
+
title: annotation.title,
|
|
153
|
+
})),
|
|
154
|
+
block: {
|
|
155
|
+
name: schema.block.name,
|
|
156
|
+
},
|
|
157
|
+
blockObjects: schema.blockObjects.map((blockObject) => ({
|
|
158
|
+
name: blockObject.name,
|
|
159
|
+
fields: blockObject.fields.map((field) => ({
|
|
160
|
+
name: field.name,
|
|
161
|
+
type: field.type.jsonType,
|
|
162
|
+
})),
|
|
163
|
+
title: blockObject.title,
|
|
164
|
+
})),
|
|
165
|
+
decorators: schema.decorators.map((decorator) => ({
|
|
166
|
+
name: decorator.value,
|
|
167
|
+
title: decorator.title,
|
|
168
|
+
value: decorator.value,
|
|
169
|
+
})),
|
|
170
|
+
inlineObjects: schema.inlineObjects.map((inlineObject) => ({
|
|
171
|
+
name: inlineObject.name,
|
|
172
|
+
fields: inlineObject.fields.map((field) => ({
|
|
173
|
+
name: field.name,
|
|
174
|
+
type: field.type.jsonType,
|
|
175
|
+
})),
|
|
176
|
+
title: inlineObject.title,
|
|
177
|
+
})),
|
|
178
|
+
span: {
|
|
179
|
+
name: schema.span.name,
|
|
180
|
+
},
|
|
181
|
+
styles: schema.styles.map((style) => ({
|
|
182
|
+
name: style.value,
|
|
183
|
+
title: style.title,
|
|
184
|
+
value: style.value,
|
|
185
|
+
})),
|
|
186
|
+
lists: schema.lists.map((list) => ({
|
|
187
|
+
name: list.value,
|
|
188
|
+
title: list.title,
|
|
189
|
+
value: list.value,
|
|
190
|
+
})),
|
|
191
|
+
}
|
|
192
|
+
}
|
|
92
193
|
|
|
93
194
|
export function compileSchemaDefinition<
|
|
94
195
|
TSchemaDefinition extends SchemaDefinition,
|
|
95
|
-
>(definition
|
|
196
|
+
>(definition: TSchemaDefinition): EditorSchema {
|
|
197
|
+
return legacySchemaToEditorSchema(
|
|
198
|
+
compileSchemaDefinitionToLegacySchema(definition),
|
|
199
|
+
)
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export function compileSchemaDefinitionToLegacySchema<
|
|
203
|
+
TSchemaDefinition extends SchemaDefinition,
|
|
204
|
+
>(definition?: TSchemaDefinition): PortableTextMemberSchemaTypes {
|
|
96
205
|
const blockObjects =
|
|
97
206
|
definition?.blockObjects?.map((blockObject) =>
|
|
98
207
|
defineType({
|
|
@@ -179,7 +288,7 @@ export function compileSchemaDefinition<
|
|
|
179
288
|
types: [portableTextSchema, ...blockObjects, ...inlineObjects],
|
|
180
289
|
}).get('portable-text')
|
|
181
290
|
|
|
182
|
-
const pteSchema =
|
|
291
|
+
const pteSchema = createLegacySchema(schema)
|
|
183
292
|
|
|
184
293
|
return {
|
|
185
294
|
...pteSchema,
|
|
@@ -203,5 +312,5 @@ export function compileSchemaDefinition<
|
|
|
203
312
|
} as ObjectSchemaType)
|
|
204
313
|
: inlineObject,
|
|
205
314
|
),
|
|
206
|
-
} satisfies
|
|
315
|
+
} satisfies PortableTextMemberSchemaTypes
|
|
207
316
|
}
|
|
@@ -4,8 +4,8 @@ import type {EventPosition} from '../internal-utils/event-position'
|
|
|
4
4
|
import {slateChildrenToBlocks} from '../internal-utils/slate-children-to-blocks'
|
|
5
5
|
import {slateRangeToSelection} from '../internal-utils/slate-utils'
|
|
6
6
|
import type {EditorSelection, PortableTextSlateEditor} from '../types/editor'
|
|
7
|
-
import type {EditorSchema} from './define-schema'
|
|
8
7
|
import type {HasTag} from './editor-machine'
|
|
8
|
+
import type {EditorSchema} from './editor-schema'
|
|
9
9
|
import {getActiveDecorators} from './get-active-decorators'
|
|
10
10
|
|
|
11
11
|
/**
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {Editor} from 'slate'
|
|
2
2
|
import type {PortableTextSlateEditor} from '../types/editor'
|
|
3
|
-
import type {EditorSchema} from './
|
|
3
|
+
import type {EditorSchema} from './editor-schema'
|
|
4
4
|
|
|
5
5
|
export function getActiveDecorators({
|
|
6
6
|
schema,
|
|
@@ -9,7 +9,7 @@ export function getActiveDecorators({
|
|
|
9
9
|
schema: EditorSchema
|
|
10
10
|
slateEditorInstance: PortableTextSlateEditor
|
|
11
11
|
}) {
|
|
12
|
-
const decorators = schema.decorators.map((decorator) => decorator.
|
|
12
|
+
const decorators = schema.decorators.map((decorator) => decorator.name)
|
|
13
13
|
|
|
14
14
|
const marks =
|
|
15
15
|
{
|
|
@@ -6,11 +6,11 @@ import type {
|
|
|
6
6
|
SchemaType,
|
|
7
7
|
SpanSchemaType,
|
|
8
8
|
} from '@sanity/types'
|
|
9
|
-
import type {
|
|
9
|
+
import type {PortableTextMemberSchemaTypes} from '../types/editor'
|
|
10
10
|
|
|
11
|
-
export function
|
|
11
|
+
export function createLegacySchema(
|
|
12
12
|
portableTextType: ArraySchemaType<PortableTextBlock>,
|
|
13
|
-
):
|
|
13
|
+
): PortableTextMemberSchemaTypes {
|
|
14
14
|
if (!portableTextType) {
|
|
15
15
|
throw new Error("Parameter 'portabletextType' missing (required)")
|
|
16
16
|
}
|
|
@@ -14,7 +14,7 @@ import {
|
|
|
14
14
|
type AnyEventObject,
|
|
15
15
|
} from 'xstate'
|
|
16
16
|
import type {PortableTextSlateEditor} from '../types/editor'
|
|
17
|
-
import type {EditorSchema} from './
|
|
17
|
+
import type {EditorSchema} from './editor-schema'
|
|
18
18
|
|
|
19
19
|
/**
|
|
20
20
|
* Makes sure editor mutation events are debounced
|
|
@@ -1,20 +1,14 @@
|
|
|
1
1
|
import {Editor, Element, Node, Transforms} from 'slate'
|
|
2
2
|
import {isChangingRemotely} from '../../internal-utils/withChanges'
|
|
3
3
|
import {isRedoing, isUndoing} from '../../internal-utils/withUndoRedo'
|
|
4
|
-
import type {
|
|
5
|
-
PortableTextMemberSchemaTypes,
|
|
6
|
-
PortableTextSlateEditor,
|
|
7
|
-
} from '../../types/editor'
|
|
4
|
+
import type {PortableTextSlateEditor} from '../../types/editor'
|
|
8
5
|
import type {EditorActor} from '../editor-machine'
|
|
9
6
|
|
|
10
7
|
/**
|
|
11
8
|
* This plugin makes sure that every new node in the editor get a new _key prop when created
|
|
12
9
|
*
|
|
13
10
|
*/
|
|
14
|
-
export function createWithObjectKeys(
|
|
15
|
-
editorActor: EditorActor,
|
|
16
|
-
schemaTypes: PortableTextMemberSchemaTypes,
|
|
17
|
-
) {
|
|
11
|
+
export function createWithObjectKeys(editorActor: EditorActor) {
|
|
18
12
|
return function withKeys(
|
|
19
13
|
editor: PortableTextSlateEditor,
|
|
20
14
|
): PortableTextSlateEditor {
|
|
@@ -89,7 +83,10 @@ export function createWithObjectKeys(
|
|
|
89
83
|
|
|
90
84
|
editor.normalizeNode = (entry) => {
|
|
91
85
|
const [node, path] = entry
|
|
92
|
-
if (
|
|
86
|
+
if (
|
|
87
|
+
Element.isElement(node) &&
|
|
88
|
+
node._type === editorActor.getSnapshot().context.schema.block.name
|
|
89
|
+
) {
|
|
93
90
|
// Set key on block itself
|
|
94
91
|
if (!node._key) {
|
|
95
92
|
editorActor.send({type: 'normalizing'})
|
|
@@ -25,10 +25,7 @@ import {
|
|
|
25
25
|
PATCHING,
|
|
26
26
|
withoutPatching,
|
|
27
27
|
} from '../../internal-utils/withoutPatching'
|
|
28
|
-
import type {
|
|
29
|
-
PortableTextMemberSchemaTypes,
|
|
30
|
-
PortableTextSlateEditor,
|
|
31
|
-
} from '../../types/editor'
|
|
28
|
+
import type {PortableTextSlateEditor} from '../../types/editor'
|
|
32
29
|
import type {EditorActor} from '../editor-machine'
|
|
33
30
|
import {getCurrentActionId} from '../with-applying-behavior-actions'
|
|
34
31
|
import {withoutSaving} from './createWithUndoRedo'
|
|
@@ -82,21 +79,19 @@ export interface PatchFunctions {
|
|
|
82
79
|
interface Options {
|
|
83
80
|
editorActor: EditorActor
|
|
84
81
|
patchFunctions: PatchFunctions
|
|
85
|
-
schemaTypes: PortableTextMemberSchemaTypes
|
|
86
82
|
subscriptions: Array<() => () => void>
|
|
87
83
|
}
|
|
88
84
|
|
|
89
85
|
export function createWithPatches({
|
|
90
86
|
editorActor,
|
|
91
87
|
patchFunctions,
|
|
92
|
-
schemaTypes,
|
|
93
88
|
subscriptions,
|
|
94
89
|
}: Options): (editor: PortableTextSlateEditor) => PortableTextSlateEditor {
|
|
95
90
|
// The previous editor children are needed to figure out the _key of deleted nodes
|
|
96
91
|
// The editor.children would no longer contain that information if the node is already deleted.
|
|
97
92
|
let previousChildren: Descendant[]
|
|
98
93
|
|
|
99
|
-
const applyPatch = createApplyPatch(
|
|
94
|
+
const applyPatch = createApplyPatch(editorActor.getSnapshot().context.schema)
|
|
100
95
|
|
|
101
96
|
return function withPatches(editor: PortableTextSlateEditor) {
|
|
102
97
|
IS_PROCESSING_REMOTE_CHANGES.set(editor, false)
|
|
@@ -156,12 +151,18 @@ export function createWithPatches({
|
|
|
156
151
|
// Update previous children here before we apply
|
|
157
152
|
previousChildren = editor.children
|
|
158
153
|
|
|
159
|
-
const editorWasEmpty = isEqualToEmptyEditor(
|
|
154
|
+
const editorWasEmpty = isEqualToEmptyEditor(
|
|
155
|
+
previousChildren,
|
|
156
|
+
editorActor.getSnapshot().context.schema,
|
|
157
|
+
)
|
|
160
158
|
|
|
161
159
|
// Apply the operation
|
|
162
160
|
apply(operation)
|
|
163
161
|
|
|
164
|
-
const editorIsEmpty = isEqualToEmptyEditor(
|
|
162
|
+
const editorIsEmpty = isEqualToEmptyEditor(
|
|
163
|
+
editor.children,
|
|
164
|
+
editorActor.getSnapshot().context.schema,
|
|
165
|
+
)
|
|
165
166
|
|
|
166
167
|
if (!isPatching(editor)) {
|
|
167
168
|
if (debugVerbose && debug.enabled)
|
|
@@ -275,7 +276,7 @@ export function createWithPatches({
|
|
|
275
276
|
type: 'notify.unset',
|
|
276
277
|
previousValue: fromSlateValue(
|
|
277
278
|
previousChildren,
|
|
278
|
-
|
|
279
|
+
editorActor.getSnapshot().context.schema.block.name,
|
|
279
280
|
KEY_TO_VALUE_ELEMENT.get(editor),
|
|
280
281
|
),
|
|
281
282
|
})
|
|
@@ -295,7 +296,7 @@ export function createWithPatches({
|
|
|
295
296
|
actionId: getCurrentActionId(editor),
|
|
296
297
|
value: fromSlateValue(
|
|
297
298
|
editor.children,
|
|
298
|
-
|
|
299
|
+
editorActor.getSnapshot().context.schema.block.name,
|
|
299
300
|
KEY_TO_VALUE_ELEMENT.get(editor),
|
|
300
301
|
),
|
|
301
302
|
})
|
|
@@ -1,18 +1,14 @@
|
|
|
1
1
|
import {Editor, Path, Text as SlateText, Transforms} from 'slate'
|
|
2
2
|
import {debugWithName} from '../../internal-utils/debug'
|
|
3
|
-
import type {
|
|
4
|
-
PortableTextMemberSchemaTypes,
|
|
5
|
-
PortableTextSlateEditor,
|
|
6
|
-
} from '../../types/editor'
|
|
3
|
+
import type {PortableTextSlateEditor} from '../../types/editor'
|
|
7
4
|
import type {EditorActor} from '../editor-machine'
|
|
8
5
|
|
|
9
6
|
const debug = debugWithName('plugin:withPortableTextBlockStyle')
|
|
10
7
|
|
|
11
8
|
export function createWithPortableTextBlockStyle(
|
|
12
9
|
editorActor: EditorActor,
|
|
13
|
-
types: PortableTextMemberSchemaTypes,
|
|
14
10
|
): (editor: PortableTextSlateEditor) => PortableTextSlateEditor {
|
|
15
|
-
const defaultStyle =
|
|
11
|
+
const defaultStyle = editorActor.getSnapshot().context.schema.styles[0].name
|
|
16
12
|
return function withPortableTextBlockStyle(
|
|
17
13
|
editor: PortableTextSlateEditor,
|
|
18
14
|
): PortableTextSlateEditor {
|