@portabletext/editor 2.3.2 → 2.3.4
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-dts/behavior.types.action.d.cts +5 -142
- package/lib/_chunks-dts/behavior.types.action.d.ts +55 -192
- package/lib/index.cjs +43 -279
- package/lib/index.cjs.map +1 -1
- package/lib/index.js +5 -245
- package/lib/index.js.map +1 -1
- package/lib/utils/index.d.cts +2 -2
- package/lib/utils/index.d.ts +2 -2
- package/package.json +7 -5
- package/src/converters/converter.portable-text.deserialize.test.ts +6 -4
- package/src/converters/converter.text-html.deserialize.test.ts +15 -15
- package/src/converters/converter.text-html.serialize.test.ts +8 -8
- package/src/converters/converter.text-plain.test.ts +8 -8
- package/src/editor/create-editor.ts +12 -10
- package/src/editor/editor-schema.ts +2 -312
- package/src/editor/plugins/__tests__/withEditableAPIGetFragment.test.tsx +2 -2
- package/src/editor.ts +1 -1
- package/src/index.ts +3 -3
- package/src/internal-utils/__tests__/values.test.ts +2 -3
- package/src/internal-utils/apply-operation-to-portable-text.test.ts +2 -3
- package/src/internal-utils/build-index-maps.test.ts +2 -3
- package/src/internal-utils/create-test-snapshot.ts +2 -4
- package/src/internal-utils/drag-selection.test.ts +2 -5
- package/src/internal-utils/operation-to-patches.test.ts +5 -7
- package/src/internal-utils/parse-blocks.test.ts +21 -30
- package/src/internal-utils/selection-text.ts +2 -3
- package/src/internal-utils/terse-pt.test.ts +2 -3
- package/src/internal-utils/test-editor.tsx +1 -4
- package/src/internal-utils/to-slate-range.test.ts +2 -3
- package/src/plugins/plugin.internal.auto-close-brackets.test.tsx +1 -1
- package/src/plugins/plugin.markdown.test.tsx +1 -1
- package/src/selectors/selector.get-selection-text.test.ts +2 -3
- package/src/selectors/selector.get-trimmed-selection.test.ts +2 -3
- package/src/utils/util.block-offset.test.ts +2 -3
- package/src/utils/util.slice-blocks.test.ts +2 -3
- package/src/utils/util.slice-text-block.test.ts +2 -3
- package/src/editor/editor-schema-definition.ts +0 -105
- package/src/editor/legacy-schema.ts +0 -115
package/lib/utils/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BlockOffset, BlockPath, ChildPath, EditorContext, EditorSelection, EditorSelectionPoint } from "../_chunks-dts/behavior.types.action.cjs";
|
|
2
|
-
import * as
|
|
2
|
+
import * as _sanity_types9 from "@sanity/types";
|
|
3
3
|
import { KeyedSegment, PortableTextBlock, PortableTextChild, PortableTextSpan, PortableTextTextBlock } from "@sanity/types";
|
|
4
4
|
/**
|
|
5
5
|
* @public
|
|
@@ -150,7 +150,7 @@ declare function mergeTextBlocks({
|
|
|
150
150
|
context: Pick<EditorContext, 'keyGenerator' | 'schema'>;
|
|
151
151
|
targetBlock: PortableTextTextBlock;
|
|
152
152
|
incomingBlock: PortableTextTextBlock;
|
|
153
|
-
}): PortableTextTextBlock<
|
|
153
|
+
}): PortableTextTextBlock<_sanity_types9.PortableTextObject | _sanity_types9.PortableTextSpan>;
|
|
154
154
|
/**
|
|
155
155
|
* @public
|
|
156
156
|
*/
|
package/lib/utils/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { BlockOffset, BlockPath, ChildPath, EditorContext, EditorSelection, EditorSelectionPoint } from "../_chunks-dts/behavior.types.action.js";
|
|
2
|
-
import * as
|
|
2
|
+
import * as _sanity_types9 from "@sanity/types";
|
|
3
3
|
import { KeyedSegment, PortableTextBlock, PortableTextChild, PortableTextSpan, PortableTextTextBlock } from "@sanity/types";
|
|
4
4
|
/**
|
|
5
5
|
* @public
|
|
@@ -150,7 +150,7 @@ declare function mergeTextBlocks({
|
|
|
150
150
|
context: Pick<EditorContext, 'keyGenerator' | 'schema'>;
|
|
151
151
|
targetBlock: PortableTextTextBlock;
|
|
152
152
|
incomingBlock: PortableTextTextBlock;
|
|
153
|
-
}): PortableTextTextBlock<
|
|
153
|
+
}): PortableTextTextBlock<_sanity_types9.PortableTextObject | _sanity_types9.PortableTextSpan>;
|
|
154
154
|
/**
|
|
155
155
|
* @public
|
|
156
156
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@portabletext/editor",
|
|
3
|
-
"version": "2.3.
|
|
3
|
+
"version": "2.3.4",
|
|
4
4
|
"description": "Portable Text Editor made in React",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"sanity",
|
|
@@ -79,9 +79,11 @@
|
|
|
79
79
|
"slate-dom": "^0.117.4",
|
|
80
80
|
"slate-react": "0.117.4",
|
|
81
81
|
"xstate": "^5.20.2",
|
|
82
|
-
"@portabletext/
|
|
82
|
+
"@portabletext/keyboard-shortcuts": "1.1.1",
|
|
83
|
+
"@portabletext/sanity-bridge": "1.0.0",
|
|
83
84
|
"@portabletext/patches": "1.1.6",
|
|
84
|
-
"@portabletext/
|
|
85
|
+
"@portabletext/schema": "1.0.0",
|
|
86
|
+
"@portabletext/block-tools": "3.0.0"
|
|
85
87
|
},
|
|
86
88
|
"devDependencies": {
|
|
87
89
|
"@portabletext/toolkit": "^2.0.17",
|
|
@@ -95,8 +97,8 @@
|
|
|
95
97
|
"@types/lodash.startcase": "^4.4.9",
|
|
96
98
|
"@types/react": "^19.1.9",
|
|
97
99
|
"@types/react-dom": "^19.1.7",
|
|
98
|
-
"@typescript-eslint/eslint-plugin": "^8.39.
|
|
99
|
-
"@typescript-eslint/parser": "^8.39.
|
|
100
|
+
"@typescript-eslint/eslint-plugin": "^8.39.1",
|
|
101
|
+
"@typescript-eslint/parser": "^8.39.1",
|
|
100
102
|
"@vitejs/plugin-react": "^4.7.0",
|
|
101
103
|
"@vitest/browser": "^3.2.4",
|
|
102
104
|
"@vitest/coverage-istanbul": "^3.2.4",
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import {
|
|
2
|
+
compileSchema,
|
|
3
|
+
defineSchema,
|
|
4
|
+
type SchemaDefinition,
|
|
5
|
+
} from '@portabletext/schema'
|
|
1
6
|
import {assert, describe, expect, test} from 'vitest'
|
|
2
|
-
import {compileSchemaDefinition} from '../editor/editor-schema'
|
|
3
|
-
import {defineSchema} from '../editor/editor-schema-definition'
|
|
4
|
-
import type {SchemaDefinition} from '../editor/editor-schema-definition'
|
|
5
7
|
import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
|
|
6
8
|
import {converterPortableText} from './converter.portable-text'
|
|
7
9
|
|
|
@@ -9,7 +11,7 @@ function createSnapshot(schema: SchemaDefinition) {
|
|
|
9
11
|
return createTestSnapshot({
|
|
10
12
|
context: {
|
|
11
13
|
converters: [],
|
|
12
|
-
schema:
|
|
14
|
+
schema: compileSchema(schema),
|
|
13
15
|
},
|
|
14
16
|
})
|
|
15
17
|
}
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {compileSchemaDefinitionToPortableTextMemberSchemaTypes} from '@portabletext/sanity-bridge'
|
|
2
2
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
import
|
|
3
|
+
compileSchema,
|
|
4
|
+
defineSchema,
|
|
5
|
+
type SchemaDefinition,
|
|
6
|
+
} from '@portabletext/schema'
|
|
7
|
+
import {describe, expect, test} from 'vitest'
|
|
8
8
|
import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
|
|
9
9
|
import {createConverterTextHtml} from './converter.text-html'
|
|
10
10
|
|
|
@@ -12,7 +12,7 @@ function createSnapshot(schema: SchemaDefinition) {
|
|
|
12
12
|
return createTestSnapshot({
|
|
13
13
|
context: {
|
|
14
14
|
converters: [],
|
|
15
|
-
schema:
|
|
15
|
+
schema: compileSchema(schema),
|
|
16
16
|
},
|
|
17
17
|
})
|
|
18
18
|
}
|
|
@@ -28,7 +28,7 @@ const nestedList = '<ol><li>foo<ul><li>bar</li></ul></li></ol>'
|
|
|
28
28
|
describe(createConverterTextHtml.name, () => {
|
|
29
29
|
test('paragraph with unknown decorators', () => {
|
|
30
30
|
const converterTextHtml = createConverterTextHtml(
|
|
31
|
-
|
|
31
|
+
compileSchemaDefinitionToPortableTextMemberSchemaTypes(defineSchema({})),
|
|
32
32
|
)
|
|
33
33
|
|
|
34
34
|
expect(
|
|
@@ -61,7 +61,7 @@ describe(createConverterTextHtml.name, () => {
|
|
|
61
61
|
|
|
62
62
|
test('paragraph with known decorators', () => {
|
|
63
63
|
const converterTextHtml = createConverterTextHtml(
|
|
64
|
-
|
|
64
|
+
compileSchemaDefinitionToPortableTextMemberSchemaTypes(
|
|
65
65
|
defineSchema({
|
|
66
66
|
decorators: [{name: 'strong'}, {name: 'em'}, {name: 'code'}],
|
|
67
67
|
}),
|
|
@@ -114,7 +114,7 @@ describe(createConverterTextHtml.name, () => {
|
|
|
114
114
|
|
|
115
115
|
test('image', () => {
|
|
116
116
|
const converterTextHtml = createConverterTextHtml(
|
|
117
|
-
|
|
117
|
+
compileSchemaDefinitionToPortableTextMemberSchemaTypes(
|
|
118
118
|
defineSchema({
|
|
119
119
|
blockObjects: [{name: 'image'}],
|
|
120
120
|
}),
|
|
@@ -139,7 +139,7 @@ describe(createConverterTextHtml.name, () => {
|
|
|
139
139
|
|
|
140
140
|
test('paragraph with unknown link', () => {
|
|
141
141
|
const converterTextHtml = createConverterTextHtml(
|
|
142
|
-
|
|
142
|
+
compileSchemaDefinitionToPortableTextMemberSchemaTypes(defineSchema({})),
|
|
143
143
|
)
|
|
144
144
|
|
|
145
145
|
expect(
|
|
@@ -172,7 +172,7 @@ describe(createConverterTextHtml.name, () => {
|
|
|
172
172
|
|
|
173
173
|
test('paragraph with known link', () => {
|
|
174
174
|
const converterTextHtml = createConverterTextHtml(
|
|
175
|
-
|
|
175
|
+
compileSchemaDefinitionToPortableTextMemberSchemaTypes(
|
|
176
176
|
defineSchema({
|
|
177
177
|
annotations: [
|
|
178
178
|
{name: 'link', fields: [{name: 'href', type: 'string'}]},
|
|
@@ -229,7 +229,7 @@ describe(createConverterTextHtml.name, () => {
|
|
|
229
229
|
|
|
230
230
|
test('unordered list', () => {
|
|
231
231
|
const converterTextHtml = createConverterTextHtml(
|
|
232
|
-
|
|
232
|
+
compileSchemaDefinitionToPortableTextMemberSchemaTypes(
|
|
233
233
|
defineSchema({
|
|
234
234
|
lists: [{name: 'bullet'}],
|
|
235
235
|
}),
|
|
@@ -288,7 +288,7 @@ describe(createConverterTextHtml.name, () => {
|
|
|
288
288
|
|
|
289
289
|
test('ordered list', () => {
|
|
290
290
|
const converterTextHtml = createConverterTextHtml(
|
|
291
|
-
|
|
291
|
+
compileSchemaDefinitionToPortableTextMemberSchemaTypes(
|
|
292
292
|
defineSchema({
|
|
293
293
|
lists: [{name: 'number'}],
|
|
294
294
|
}),
|
|
@@ -347,7 +347,7 @@ describe(createConverterTextHtml.name, () => {
|
|
|
347
347
|
|
|
348
348
|
test('nested list', () => {
|
|
349
349
|
const converterTextHtml = createConverterTextHtml(
|
|
350
|
-
|
|
350
|
+
compileSchemaDefinitionToPortableTextMemberSchemaTypes(
|
|
351
351
|
defineSchema({
|
|
352
352
|
lists: [{name: 'bullet'}, {name: 'number'}],
|
|
353
353
|
}),
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
+
import {compileSchemaDefinitionToPortableTextMemberSchemaTypes} from '@portabletext/sanity-bridge'
|
|
2
|
+
import {
|
|
3
|
+
compileSchema,
|
|
4
|
+
defineSchema,
|
|
5
|
+
type SchemaDefinition,
|
|
6
|
+
} from '@portabletext/schema'
|
|
1
7
|
import type {PortableTextBlock, PortableTextTextBlock} from '@sanity/types'
|
|
2
8
|
import {describe, expect, test} from 'vitest'
|
|
3
9
|
import type {EditorSelection} from '..'
|
|
4
|
-
import {
|
|
5
|
-
compileSchemaDefinition,
|
|
6
|
-
compileSchemaDefinitionToLegacySchema,
|
|
7
|
-
} from '../editor/editor-schema'
|
|
8
|
-
import {defineSchema} from '../editor/editor-schema-definition'
|
|
9
|
-
import type {SchemaDefinition} from '../editor/editor-schema-definition'
|
|
10
10
|
import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
|
|
11
11
|
import {createConverterTextHtml} from './converter.text-html'
|
|
12
12
|
|
|
@@ -79,7 +79,7 @@ function createSnapshot(schema: SchemaDefinition, selection: EditorSelection) {
|
|
|
79
79
|
return createTestSnapshot({
|
|
80
80
|
context: {
|
|
81
81
|
converters: [],
|
|
82
|
-
schema:
|
|
82
|
+
schema: compileSchema(schema),
|
|
83
83
|
selection,
|
|
84
84
|
value: [decoratedParagraph, image, b2, paragraphWithInlineBlock],
|
|
85
85
|
},
|
|
@@ -87,7 +87,7 @@ function createSnapshot(schema: SchemaDefinition, selection: EditorSelection) {
|
|
|
87
87
|
}
|
|
88
88
|
|
|
89
89
|
const converterTextHtml = createConverterTextHtml(
|
|
90
|
-
|
|
90
|
+
compileSchemaDefinitionToPortableTextMemberSchemaTypes(defineSchema({})),
|
|
91
91
|
)
|
|
92
92
|
|
|
93
93
|
describe(converterTextHtml.serialize.name, () => {
|
|
@@ -1,13 +1,13 @@
|
|
|
1
|
+
import {compileSchemaDefinitionToPortableTextMemberSchemaTypes} from '@portabletext/sanity-bridge'
|
|
2
|
+
import {
|
|
3
|
+
compileSchema,
|
|
4
|
+
defineSchema,
|
|
5
|
+
type SchemaDefinition,
|
|
6
|
+
} from '@portabletext/schema'
|
|
1
7
|
import type {PortableTextBlock, PortableTextTextBlock} from '@sanity/types'
|
|
2
8
|
import {expect, test} from 'vitest'
|
|
3
9
|
import type {EditorSelection} from '..'
|
|
4
10
|
import {schemaDefinition} from '../editor/__tests__/PortableTextEditorTester'
|
|
5
|
-
import {
|
|
6
|
-
compileSchemaDefinition,
|
|
7
|
-
compileSchemaDefinitionToLegacySchema,
|
|
8
|
-
} from '../editor/editor-schema'
|
|
9
|
-
import type {SchemaDefinition} from '../editor/editor-schema-definition'
|
|
10
|
-
import {defineSchema} from '../editor/editor-schema-definition'
|
|
11
11
|
import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
|
|
12
12
|
import {createConverterTextPlain} from './converter.text-plain'
|
|
13
13
|
|
|
@@ -76,7 +76,7 @@ function createSnapshot({
|
|
|
76
76
|
return createTestSnapshot({
|
|
77
77
|
context: {
|
|
78
78
|
converters: [],
|
|
79
|
-
schema:
|
|
79
|
+
schema: compileSchema(schema),
|
|
80
80
|
selection,
|
|
81
81
|
value: [b1, b2, b3, b4],
|
|
82
82
|
},
|
|
@@ -84,7 +84,7 @@ function createSnapshot({
|
|
|
84
84
|
}
|
|
85
85
|
|
|
86
86
|
const converterTextPlain = createConverterTextPlain(
|
|
87
|
-
|
|
87
|
+
compileSchemaDefinitionToPortableTextMemberSchemaTypes(schemaDefinition),
|
|
88
88
|
)
|
|
89
89
|
|
|
90
90
|
test(converterTextPlain.serialize.name, () => {
|
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
import {
|
|
2
|
+
compileSchemaDefinitionToPortableTextMemberSchemaTypes,
|
|
3
|
+
createPortableTextMemberSchemaTypes,
|
|
4
|
+
portableTextMemberSchemaTypesToSchema,
|
|
5
|
+
} from '@portabletext/sanity-bridge'
|
|
6
|
+
import {compileSchema} from '@portabletext/schema'
|
|
1
7
|
import {createActor} from 'xstate'
|
|
2
8
|
import {createCoreConverters} from '../converters/converters.core'
|
|
3
9
|
import type {Editor, EditorConfig} from '../editor'
|
|
@@ -12,14 +18,8 @@ import {createSlateEditor, type SlateEditor} from './create-slate-editor'
|
|
|
12
18
|
import {createEditorDom} from './editor-dom'
|
|
13
19
|
import type {EditorActor} from './editor-machine'
|
|
14
20
|
import {editorMachine} from './editor-machine'
|
|
15
|
-
import {
|
|
16
|
-
compileSchemaDefinition,
|
|
17
|
-
compileSchemaDefinitionToLegacySchema,
|
|
18
|
-
legacySchemaToEditorSchema,
|
|
19
|
-
} from './editor-schema'
|
|
20
21
|
import {getEditorSnapshot} from './editor-selector'
|
|
21
22
|
import {defaultKeyGenerator} from './key-generator'
|
|
22
|
-
import {createLegacySchema} from './legacy-schema'
|
|
23
23
|
import {mutationMachine, type MutationActor} from './mutation-machine'
|
|
24
24
|
import {createEditableAPI} from './plugins/createWithEditableAPI'
|
|
25
25
|
import {relayMachine, type RelayActor} from './relay-machine'
|
|
@@ -206,15 +206,17 @@ function editorConfigToMachineInput(config: EditorConfig) {
|
|
|
206
206
|
|
|
207
207
|
function compileSchemasFromEditorConfig(config: EditorConfig) {
|
|
208
208
|
const legacySchema = config.schemaDefinition
|
|
209
|
-
?
|
|
210
|
-
|
|
209
|
+
? compileSchemaDefinitionToPortableTextMemberSchemaTypes(
|
|
210
|
+
config.schemaDefinition,
|
|
211
|
+
)
|
|
212
|
+
: createPortableTextMemberSchemaTypes(
|
|
211
213
|
config.schema.hasOwnProperty('jsonType')
|
|
212
214
|
? config.schema
|
|
213
215
|
: compileType(config.schema),
|
|
214
216
|
)
|
|
215
217
|
const schema = config.schemaDefinition
|
|
216
|
-
?
|
|
217
|
-
:
|
|
218
|
+
? compileSchema(config.schemaDefinition)
|
|
219
|
+
: portableTextMemberSchemaTypesToSchema(legacySchema)
|
|
218
220
|
|
|
219
221
|
return {
|
|
220
222
|
legacySchema,
|
|
@@ -1,316 +1,6 @@
|
|
|
1
|
-
import {Schema
|
|
2
|
-
import {defineField, defineType, type ObjectSchemaType} from '@sanity/types'
|
|
3
|
-
import startCase from 'lodash.startcase'
|
|
4
|
-
import type {PortableTextMemberSchemaTypes} from '../types/editor'
|
|
5
|
-
import type {
|
|
6
|
-
BaseDefinition,
|
|
7
|
-
FieldDefinition,
|
|
8
|
-
SchemaDefinition,
|
|
9
|
-
} from './editor-schema-definition'
|
|
10
|
-
import {defaultKeyGenerator} from './key-generator'
|
|
11
|
-
import {createLegacySchema} from './legacy-schema'
|
|
12
|
-
|
|
13
|
-
const temporaryImageName = `tmp-${defaultKeyGenerator()}-image`
|
|
14
|
-
const temporaryUrlName = `tmp-${defaultKeyGenerator()}-url`
|
|
15
|
-
|
|
16
|
-
const temporaryObjectNames: Record<string, string> = {
|
|
17
|
-
image: temporaryImageName,
|
|
18
|
-
url: temporaryUrlName,
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const objectNames: Record<string, string> = {
|
|
22
|
-
[temporaryImageName]: 'image',
|
|
23
|
-
[temporaryUrlName]: 'url',
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const defaultObjectTitles: Record<string, string> = {
|
|
27
|
-
image: 'Image',
|
|
28
|
-
url: 'URL',
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
/**
|
|
32
|
-
* @public
|
|
33
|
-
*/
|
|
34
|
-
export type EditorSchema = {
|
|
35
|
-
annotations: ReadonlyArray<AnnotationSchemaType>
|
|
36
|
-
block: {
|
|
37
|
-
name: string
|
|
38
|
-
}
|
|
39
|
-
blockObjects: ReadonlyArray<BlockObjectSchemaType>
|
|
40
|
-
decorators: ReadonlyArray<DecoratorSchemaType>
|
|
41
|
-
inlineObjects: ReadonlyArray<InlineObjectSchemaType>
|
|
42
|
-
span: {
|
|
43
|
-
name: string
|
|
44
|
-
}
|
|
45
|
-
styles: ReadonlyArray<StyleSchemaType>
|
|
46
|
-
lists: ReadonlyArray<ListSchemaType>
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
/**
|
|
50
|
-
* @public
|
|
51
|
-
*/
|
|
52
|
-
export type AnnotationSchemaType = BaseDefinition & {
|
|
53
|
-
fields: ReadonlyArray<FieldDefinition>
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
/**
|
|
57
|
-
* @public
|
|
58
|
-
*/
|
|
59
|
-
export type BlockObjectSchemaType = BaseDefinition & {
|
|
60
|
-
fields: ReadonlyArray<FieldDefinition>
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* @public
|
|
65
|
-
*/
|
|
66
|
-
export type DecoratorSchemaType = BaseDefinition & {
|
|
67
|
-
/**
|
|
68
|
-
* @deprecated
|
|
69
|
-
* Use `name` instead
|
|
70
|
-
*/
|
|
71
|
-
value: string
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* @public
|
|
76
|
-
*/
|
|
77
|
-
export type InlineObjectSchemaType = BaseDefinition & {
|
|
78
|
-
fields: ReadonlyArray<FieldDefinition>
|
|
79
|
-
}
|
|
1
|
+
import type {Schema} from '@portabletext/schema'
|
|
80
2
|
|
|
81
3
|
/**
|
|
82
4
|
* @public
|
|
83
5
|
*/
|
|
84
|
-
export type
|
|
85
|
-
/**
|
|
86
|
-
* @deprecated
|
|
87
|
-
* Use `name` instead
|
|
88
|
-
*/
|
|
89
|
-
value: string
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* @public
|
|
94
|
-
*/
|
|
95
|
-
export type StyleSchemaType = BaseDefinition & {
|
|
96
|
-
/**
|
|
97
|
-
* @deprecated
|
|
98
|
-
* Use `name` instead
|
|
99
|
-
*/
|
|
100
|
-
value: string
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
export function legacySchemaToEditorSchema(
|
|
104
|
-
schema: PortableTextMemberSchemaTypes,
|
|
105
|
-
): EditorSchema {
|
|
106
|
-
return {
|
|
107
|
-
annotations: schema.annotations.map((annotation) => ({
|
|
108
|
-
name: annotation.name,
|
|
109
|
-
fields: annotation.fields.map((field) => ({
|
|
110
|
-
name: field.name,
|
|
111
|
-
type: field.type.jsonType,
|
|
112
|
-
title: field.type.title,
|
|
113
|
-
})),
|
|
114
|
-
title: annotation.title,
|
|
115
|
-
})),
|
|
116
|
-
block: {
|
|
117
|
-
name: schema.block.name,
|
|
118
|
-
},
|
|
119
|
-
blockObjects: schema.blockObjects.map((blockObject) => ({
|
|
120
|
-
name: blockObject.name,
|
|
121
|
-
fields: blockObject.fields.map((field) => ({
|
|
122
|
-
name: field.name,
|
|
123
|
-
type: field.type.jsonType,
|
|
124
|
-
title: field.type.title,
|
|
125
|
-
})),
|
|
126
|
-
title: blockObject.title,
|
|
127
|
-
})),
|
|
128
|
-
decorators: schema.decorators.map((decorator) => ({
|
|
129
|
-
name: decorator.value,
|
|
130
|
-
title: decorator.title,
|
|
131
|
-
value: decorator.value,
|
|
132
|
-
})),
|
|
133
|
-
inlineObjects: schema.inlineObjects.map((inlineObject) => ({
|
|
134
|
-
name: inlineObject.name,
|
|
135
|
-
fields: inlineObject.fields.map((field) => ({
|
|
136
|
-
name: field.name,
|
|
137
|
-
type: field.type.jsonType,
|
|
138
|
-
title: field.type.title,
|
|
139
|
-
})),
|
|
140
|
-
title: inlineObject.title,
|
|
141
|
-
})),
|
|
142
|
-
span: {
|
|
143
|
-
name: schema.span.name,
|
|
144
|
-
},
|
|
145
|
-
styles: schema.styles.map((style) => ({
|
|
146
|
-
name: style.value,
|
|
147
|
-
title: style.title,
|
|
148
|
-
value: style.value,
|
|
149
|
-
})),
|
|
150
|
-
lists: schema.lists.map((list) => ({
|
|
151
|
-
name: list.value,
|
|
152
|
-
title: list.title,
|
|
153
|
-
value: list.value,
|
|
154
|
-
})),
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
export function compileSchemaDefinition<
|
|
159
|
-
TSchemaDefinition extends SchemaDefinition,
|
|
160
|
-
>(definition: TSchemaDefinition): EditorSchema {
|
|
161
|
-
const styles = (definition.styles ?? []).map((style) => ({
|
|
162
|
-
...style,
|
|
163
|
-
value: style.name,
|
|
164
|
-
}))
|
|
165
|
-
|
|
166
|
-
return {
|
|
167
|
-
block: {
|
|
168
|
-
name: 'block',
|
|
169
|
-
},
|
|
170
|
-
span: {
|
|
171
|
-
name: 'span',
|
|
172
|
-
},
|
|
173
|
-
styles: !styles.some((style) => style.value === 'normal')
|
|
174
|
-
? [{value: 'normal', name: 'normal', title: 'Normal'}, ...styles]
|
|
175
|
-
: styles,
|
|
176
|
-
lists: (definition.lists ?? []).map((list) => ({
|
|
177
|
-
...list,
|
|
178
|
-
value: list.name,
|
|
179
|
-
})),
|
|
180
|
-
decorators: (definition.decorators ?? []).map((decorator) => ({
|
|
181
|
-
...decorator,
|
|
182
|
-
value: decorator.name,
|
|
183
|
-
})),
|
|
184
|
-
annotations: (definition.annotations ?? []).map((annotation) => ({
|
|
185
|
-
...annotation,
|
|
186
|
-
fields: annotation.fields ?? [],
|
|
187
|
-
})),
|
|
188
|
-
blockObjects: (definition.blockObjects ?? []).map((blockObject) => ({
|
|
189
|
-
...blockObject,
|
|
190
|
-
fields: blockObject.fields ?? [],
|
|
191
|
-
})),
|
|
192
|
-
inlineObjects: (definition.inlineObjects ?? []).map((inlineObject) => ({
|
|
193
|
-
...inlineObject,
|
|
194
|
-
fields: inlineObject.fields ?? [],
|
|
195
|
-
})),
|
|
196
|
-
}
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
export function compileSchemaDefinitionToLegacySchema<
|
|
200
|
-
TSchemaDefinition extends SchemaDefinition,
|
|
201
|
-
>(definition?: TSchemaDefinition): PortableTextMemberSchemaTypes {
|
|
202
|
-
const blockObjects =
|
|
203
|
-
definition?.blockObjects?.map((blockObject) =>
|
|
204
|
-
defineType({
|
|
205
|
-
type: 'object',
|
|
206
|
-
// Very naive way to work around `SanitySchema.compile` adding default
|
|
207
|
-
// fields to objects with certain names.
|
|
208
|
-
name: temporaryObjectNames[blockObject.name] ?? blockObject.name,
|
|
209
|
-
title:
|
|
210
|
-
blockObject.title === undefined
|
|
211
|
-
? // This avoids the default title which is a title case of the object name
|
|
212
|
-
defaultObjectTitles[blockObject.name]
|
|
213
|
-
: blockObject.title,
|
|
214
|
-
fields:
|
|
215
|
-
blockObject.fields?.map((field) => ({
|
|
216
|
-
name: field.name,
|
|
217
|
-
type: field.type,
|
|
218
|
-
title: field.title ?? startCase(field.name),
|
|
219
|
-
})) ?? [],
|
|
220
|
-
}),
|
|
221
|
-
) ?? []
|
|
222
|
-
|
|
223
|
-
const inlineObjects =
|
|
224
|
-
definition?.inlineObjects?.map((inlineObject) =>
|
|
225
|
-
defineType({
|
|
226
|
-
type: 'object',
|
|
227
|
-
// Very naive way to work around `SanitySchema.compile` adding default
|
|
228
|
-
// fields to objects with certain names.
|
|
229
|
-
name: temporaryObjectNames[inlineObject.name] ?? inlineObject.name,
|
|
230
|
-
|
|
231
|
-
title:
|
|
232
|
-
inlineObject.title === undefined
|
|
233
|
-
? // This avoids the default title which is a title case of the object name
|
|
234
|
-
defaultObjectTitles[inlineObject.name]
|
|
235
|
-
: inlineObject.title,
|
|
236
|
-
fields:
|
|
237
|
-
inlineObject.fields?.map((field) => ({
|
|
238
|
-
name: field.name,
|
|
239
|
-
type: field.type,
|
|
240
|
-
title: field.title ?? startCase(field.name),
|
|
241
|
-
})) ?? [],
|
|
242
|
-
}),
|
|
243
|
-
) ?? []
|
|
244
|
-
|
|
245
|
-
const portableTextSchema = defineField({
|
|
246
|
-
type: 'array',
|
|
247
|
-
name: 'portable-text',
|
|
248
|
-
of: [
|
|
249
|
-
...blockObjects.map((blockObject) => ({type: blockObject.name})),
|
|
250
|
-
{
|
|
251
|
-
type: 'block',
|
|
252
|
-
name: 'block',
|
|
253
|
-
of: inlineObjects.map((inlineObject) => ({type: inlineObject.name})),
|
|
254
|
-
marks: {
|
|
255
|
-
decorators:
|
|
256
|
-
definition?.decorators?.map((decorator) => ({
|
|
257
|
-
title: decorator.title ?? startCase(decorator.name),
|
|
258
|
-
value: decorator.name,
|
|
259
|
-
})) ?? [],
|
|
260
|
-
annotations:
|
|
261
|
-
definition?.annotations?.map((annotation) => ({
|
|
262
|
-
name: annotation.name,
|
|
263
|
-
type: 'object',
|
|
264
|
-
title: annotation.title,
|
|
265
|
-
fields:
|
|
266
|
-
annotation.fields?.map((field) => ({
|
|
267
|
-
name: field.name,
|
|
268
|
-
title: field.title ?? startCase(field.name),
|
|
269
|
-
type: field.type,
|
|
270
|
-
})) ?? [],
|
|
271
|
-
})) ?? [],
|
|
272
|
-
},
|
|
273
|
-
lists:
|
|
274
|
-
definition?.lists?.map((list) => ({
|
|
275
|
-
value: list.name,
|
|
276
|
-
title: list.title ?? startCase(list.name),
|
|
277
|
-
})) ?? [],
|
|
278
|
-
styles:
|
|
279
|
-
definition?.styles?.map((style) => ({
|
|
280
|
-
value: style.name,
|
|
281
|
-
title: style.title ?? startCase(style.name),
|
|
282
|
-
})) ?? [],
|
|
283
|
-
},
|
|
284
|
-
],
|
|
285
|
-
})
|
|
286
|
-
|
|
287
|
-
const schema = SanitySchema.compile({
|
|
288
|
-
types: [portableTextSchema, ...blockObjects, ...inlineObjects],
|
|
289
|
-
}).get('portable-text')
|
|
290
|
-
|
|
291
|
-
const pteSchema = createLegacySchema(schema)
|
|
292
|
-
|
|
293
|
-
return {
|
|
294
|
-
...pteSchema,
|
|
295
|
-
blockObjects: pteSchema.blockObjects.map((blockObject) =>
|
|
296
|
-
objectNames[blockObject.name] !== undefined
|
|
297
|
-
? ({
|
|
298
|
-
...blockObject,
|
|
299
|
-
name: objectNames[blockObject.name],
|
|
300
|
-
type: {
|
|
301
|
-
...blockObject.type,
|
|
302
|
-
name: objectNames[blockObject.name],
|
|
303
|
-
},
|
|
304
|
-
} as ObjectSchemaType)
|
|
305
|
-
: blockObject,
|
|
306
|
-
),
|
|
307
|
-
inlineObjects: pteSchema.inlineObjects.map((inlineObject) =>
|
|
308
|
-
objectNames[inlineObject.name] !== undefined
|
|
309
|
-
? ({
|
|
310
|
-
...inlineObject,
|
|
311
|
-
name: objectNames[inlineObject.name],
|
|
312
|
-
} as ObjectSchemaType)
|
|
313
|
-
: inlineObject,
|
|
314
|
-
),
|
|
315
|
-
} satisfies PortableTextMemberSchemaTypes
|
|
316
|
-
}
|
|
6
|
+
export type EditorSchema = Schema
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {compileSchema} from '@portabletext/schema'
|
|
1
2
|
import {render, waitFor} from '@testing-library/react'
|
|
2
3
|
import {createRef, type RefObject} from 'react'
|
|
3
4
|
import {describe, expect, it, vi} from 'vitest'
|
|
@@ -7,7 +8,6 @@ import {
|
|
|
7
8
|
} from '../../__tests__/PortableTextEditorTester'
|
|
8
9
|
import {isTextBlock} from '../../../internal-utils/parse-blocks'
|
|
9
10
|
import {createTestKeyGenerator} from '../../../internal-utils/test-key-generator'
|
|
10
|
-
import {compileSchemaDefinition} from '../../editor-schema'
|
|
11
11
|
import {PortableTextEditor} from '../../PortableTextEditor'
|
|
12
12
|
|
|
13
13
|
const initialValue = [
|
|
@@ -88,7 +88,7 @@ describe('plugin:withEditableAPI: .getFragment()', () => {
|
|
|
88
88
|
fragment &&
|
|
89
89
|
isTextBlock(
|
|
90
90
|
{
|
|
91
|
-
schema:
|
|
91
|
+
schema: compileSchema(schemaDefinition),
|
|
92
92
|
},
|
|
93
93
|
fragment[0],
|
|
94
94
|
) &&
|
package/src/editor.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type {SchemaDefinition} from '@portabletext/schema'
|
|
1
2
|
import type {
|
|
2
3
|
ArrayDefinition,
|
|
3
4
|
ArraySchemaType,
|
|
@@ -8,7 +9,6 @@ import type {Behavior} from './behaviors/behavior.types.behavior'
|
|
|
8
9
|
import type {ExternalBehaviorEvent} from './behaviors/behavior.types.event'
|
|
9
10
|
import type {EditorDom} from './editor/editor-dom'
|
|
10
11
|
import type {ExternalEditorEvent} from './editor/editor-machine'
|
|
11
|
-
import type {SchemaDefinition} from './editor/editor-schema-definition'
|
|
12
12
|
import type {EditorSnapshot} from './editor/editor-snapshot'
|
|
13
13
|
import type {EditorEmittedEvent} from './editor/relay-machine'
|
|
14
14
|
|
package/src/index.ts
CHANGED
|
@@ -19,11 +19,11 @@ export type {
|
|
|
19
19
|
AnnotationSchemaType,
|
|
20
20
|
BlockObjectSchemaType,
|
|
21
21
|
DecoratorSchemaType,
|
|
22
|
-
EditorSchema,
|
|
23
22
|
InlineObjectSchemaType,
|
|
24
23
|
ListSchemaType,
|
|
25
24
|
StyleSchemaType,
|
|
26
|
-
} from '
|
|
25
|
+
} from '@portabletext/schema'
|
|
26
|
+
export type {EditorSchema} from './editor/editor-schema'
|
|
27
27
|
export {
|
|
28
28
|
defineSchema,
|
|
29
29
|
type AnnotationDefinition,
|
|
@@ -35,7 +35,7 @@ export {
|
|
|
35
35
|
type ListDefinition,
|
|
36
36
|
type SchemaDefinition,
|
|
37
37
|
type StyleDefinition,
|
|
38
|
-
} from '
|
|
38
|
+
} from '@portabletext/schema'
|
|
39
39
|
export {useEditorSelector, type EditorSelector} from './editor/editor-selector'
|
|
40
40
|
export type {EditorContext, EditorSnapshot} from './editor/editor-snapshot'
|
|
41
41
|
export {usePortableTextEditor} from './editor/hooks/usePortableTextEditor'
|