@portabletext/editor 1.55.4 → 1.55.6
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/util.slice-text-block.cjs +67 -0
- package/lib/_chunks-cjs/util.slice-text-block.cjs.map +1 -0
- package/lib/_chunks-es/util.slice-text-block.js +69 -0
- package/lib/_chunks-es/util.slice-text-block.js.map +1 -0
- package/lib/behaviors/index.d.cts +72 -52
- package/lib/behaviors/index.d.ts +72 -52
- package/lib/index.cjs +15 -15
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +117 -69
- package/lib/index.d.ts +117 -69
- package/lib/index.js +8 -8
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.d.cts +71 -51
- package/lib/plugins/index.d.ts +71 -51
- package/lib/selectors/index.d.cts +68 -51
- package/lib/selectors/index.d.ts +68 -51
- package/lib/utils/index.cjs +12 -13
- package/lib/utils/index.cjs.map +1 -1
- package/lib/utils/index.d.cts +68 -51
- package/lib/utils/index.d.ts +68 -51
- package/lib/utils/index.js +14 -15
- package/lib/utils/index.js.map +1 -1
- package/package.json +2 -2
- package/src/behaviors/behavior.abstract.split.ts +8 -9
- package/src/behaviors/behavior.types.event.ts +3 -0
- package/src/behaviors/index.ts +2 -1
- package/src/converters/converter.portable-text.deserialize.test.ts +3 -5
- package/src/converters/converter.text-html.deserialize.test.ts +2 -2
- package/src/converters/converter.text-html.serialize.test.ts +2 -2
- package/src/converters/converter.text-plain.test.ts +3 -5
- package/src/editor/editor-schema-definition.ts +106 -0
- package/src/editor/editor-schema.ts +65 -107
- package/src/editor.ts +1 -1
- package/src/index.ts +16 -2
- package/src/internal-utils/apply-operation-to-portable-text.test.ts +2 -1
- package/src/internal-utils/build-index-maps.test.ts +2 -1
- package/src/internal-utils/create-test-snapshot.ts +2 -1
- package/src/internal-utils/drag-selection.test.ts +2 -1
- package/src/internal-utils/parse-blocks.test.ts +2 -1
- package/src/internal-utils/selection-text.ts +2 -1
- package/src/internal-utils/terse-pt.test.ts +2 -1
- package/src/internal-utils/test-editor.tsx +4 -1
- package/src/plugins/plugin.markdown.test.tsx +1 -1
- package/src/selectors/selector.get-selection-text.test.ts +2 -1
- package/src/selectors/selector.get-trimmed-selection.test.ts +2 -1
- package/src/utils/util.block-offset.test.ts +2 -1
- package/src/utils/util.slice-blocks.test.ts +2 -1
- package/src/utils/util.slice-text-block.test.ts +163 -0
- package/src/utils/util.slice-text-block.ts +89 -0
- package/src/utils/util.split-text-block.ts +7 -16
- package/lib/_chunks-cjs/util.selection-point-to-block-offset.cjs +0 -23
- package/lib/_chunks-cjs/util.selection-point-to-block-offset.cjs.map +0 -1
- package/lib/_chunks-es/util.selection-point-to-block-offset.js +0 -25
- package/lib/_chunks-es/util.selection-point-to-block-offset.js.map +0 -1
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @public
|
|
3
|
+
*/
|
|
4
|
+
export type BaseDefinition = {
|
|
5
|
+
name: string
|
|
6
|
+
title?: string
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* @public
|
|
11
|
+
*/
|
|
12
|
+
export type FieldDefinition = {
|
|
13
|
+
name: string
|
|
14
|
+
type: 'string' | 'number' | 'boolean' | 'array' | 'object'
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
export type DecoratorDefinition<
|
|
21
|
+
TBaseDefinition extends BaseDefinition = BaseDefinition,
|
|
22
|
+
> = TBaseDefinition
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* @public
|
|
26
|
+
*/
|
|
27
|
+
export type AnnotationDefinition<
|
|
28
|
+
TBaseDefinition extends BaseDefinition = BaseDefinition,
|
|
29
|
+
> = TBaseDefinition & {
|
|
30
|
+
fields?: ReadonlyArray<FieldDefinition>
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
36
|
+
export type BlockObjectDefinition<
|
|
37
|
+
TBaseDefinition extends BaseDefinition = BaseDefinition,
|
|
38
|
+
> = TBaseDefinition & {
|
|
39
|
+
fields?: ReadonlyArray<FieldDefinition>
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
export type InlineObjectDefinition<
|
|
46
|
+
TBaseDefinition extends BaseDefinition = BaseDefinition,
|
|
47
|
+
> = TBaseDefinition & {
|
|
48
|
+
fields?: ReadonlyArray<FieldDefinition>
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* @public
|
|
53
|
+
*/
|
|
54
|
+
export type ListDefinition<
|
|
55
|
+
TBaseDefinition extends BaseDefinition = BaseDefinition,
|
|
56
|
+
> = TBaseDefinition
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* @public
|
|
60
|
+
*/
|
|
61
|
+
export type StyleDefinition<
|
|
62
|
+
TBaseDefinition extends BaseDefinition = BaseDefinition,
|
|
63
|
+
> = TBaseDefinition
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* @public
|
|
67
|
+
*/
|
|
68
|
+
export type SchemaDefinition<
|
|
69
|
+
TBaseDefinition extends BaseDefinition = BaseDefinition,
|
|
70
|
+
> = {
|
|
71
|
+
decorators?: ReadonlyArray<DecoratorDefinition<TBaseDefinition>>
|
|
72
|
+
blockObjects?: ReadonlyArray<BlockObjectDefinition<TBaseDefinition>>
|
|
73
|
+
inlineObjects?: ReadonlyArray<InlineObjectDefinition<TBaseDefinition>>
|
|
74
|
+
annotations?: ReadonlyArray<AnnotationDefinition<TBaseDefinition>>
|
|
75
|
+
lists?: ReadonlyArray<ListDefinition<TBaseDefinition>>
|
|
76
|
+
styles?: ReadonlyArray<StyleDefinition<TBaseDefinition>>
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @public
|
|
81
|
+
* A helper wrapper that adds editor support, such as autocomplete and type checking, for a schema definition.
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* import { defineSchema } from '@portabletext/editor'
|
|
85
|
+
*
|
|
86
|
+
* const schemaDefinition = defineSchema({
|
|
87
|
+
* decorators: [{name: 'strong'}, {name: 'em'}, {name: 'underline'}],
|
|
88
|
+
* annotations: [{name: 'link'}],
|
|
89
|
+
* styles: [
|
|
90
|
+
* {name: 'normal'},
|
|
91
|
+
* {name: 'h1'},
|
|
92
|
+
* {name: 'h2'},
|
|
93
|
+
* {name: 'h3'},
|
|
94
|
+
* {name: 'blockquote'},
|
|
95
|
+
* ],
|
|
96
|
+
* lists: [],
|
|
97
|
+
* inlineObjects: [],
|
|
98
|
+
* blockObjects: [],
|
|
99
|
+
* }
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
export function defineSchema<const TSchemaDefinition extends SchemaDefinition>(
|
|
103
|
+
definition: TSchemaDefinition,
|
|
104
|
+
): TSchemaDefinition {
|
|
105
|
+
return definition
|
|
106
|
+
}
|
|
@@ -2,74 +2,14 @@ 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 type {
|
|
6
|
+
BaseDefinition,
|
|
7
|
+
FieldDefinition,
|
|
8
|
+
SchemaDefinition,
|
|
9
|
+
} from './editor-schema-definition'
|
|
5
10
|
import {defaultKeyGenerator} from './key-generator'
|
|
6
11
|
import {createLegacySchema} from './legacy-schema'
|
|
7
12
|
|
|
8
|
-
/**
|
|
9
|
-
* @public
|
|
10
|
-
*/
|
|
11
|
-
export type BaseDefinition = {
|
|
12
|
-
name: string
|
|
13
|
-
title?: string
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @public
|
|
18
|
-
*/
|
|
19
|
-
export type FieldDefinition = {
|
|
20
|
-
name: string
|
|
21
|
-
type: 'string' | 'number' | 'boolean' | 'array' | 'object'
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* @public
|
|
26
|
-
*/
|
|
27
|
-
export type SchemaDefinition<
|
|
28
|
-
TBaseDefinition extends BaseDefinition = BaseDefinition,
|
|
29
|
-
> = {
|
|
30
|
-
decorators?: ReadonlyArray<TBaseDefinition>
|
|
31
|
-
blockObjects?: ReadonlyArray<
|
|
32
|
-
TBaseDefinition & {fields?: ReadonlyArray<FieldDefinition>}
|
|
33
|
-
>
|
|
34
|
-
inlineObjects?: ReadonlyArray<
|
|
35
|
-
TBaseDefinition & {fields?: ReadonlyArray<FieldDefinition>}
|
|
36
|
-
>
|
|
37
|
-
annotations?: ReadonlyArray<
|
|
38
|
-
TBaseDefinition & {fields?: ReadonlyArray<FieldDefinition>}
|
|
39
|
-
>
|
|
40
|
-
lists?: ReadonlyArray<TBaseDefinition>
|
|
41
|
-
styles?: ReadonlyArray<TBaseDefinition>
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* @public
|
|
46
|
-
* A helper wrapper that adds editor support, such as autocomplete and type checking, for a schema definition.
|
|
47
|
-
* @example
|
|
48
|
-
* ```ts
|
|
49
|
-
* import { defineSchema } from '@portabletext/editor'
|
|
50
|
-
*
|
|
51
|
-
* const schemaDefinition = defineSchema({
|
|
52
|
-
* decorators: [{name: 'strong'}, {name: 'em'}, {name: 'underline'}],
|
|
53
|
-
* annotations: [{name: 'link'}],
|
|
54
|
-
* styles: [
|
|
55
|
-
* {name: 'normal'},
|
|
56
|
-
* {name: 'h1'},
|
|
57
|
-
* {name: 'h2'},
|
|
58
|
-
* {name: 'h3'},
|
|
59
|
-
* {name: 'blockquote'},
|
|
60
|
-
* ],
|
|
61
|
-
* lists: [],
|
|
62
|
-
* inlineObjects: [],
|
|
63
|
-
* blockObjects: [],
|
|
64
|
-
* }
|
|
65
|
-
* ```
|
|
66
|
-
*/
|
|
67
|
-
export function defineSchema<const TSchemaDefinition extends SchemaDefinition>(
|
|
68
|
-
definition: TSchemaDefinition,
|
|
69
|
-
): TSchemaDefinition {
|
|
70
|
-
return definition
|
|
71
|
-
}
|
|
72
|
-
|
|
73
13
|
const temporaryImageName = `tmp-${defaultKeyGenerator()}-image`
|
|
74
14
|
const temporaryUrlName = `tmp-${defaultKeyGenerator()}-url`
|
|
75
15
|
|
|
@@ -92,54 +32,72 @@ const defaultObjectTitles: Record<string, string> = {
|
|
|
92
32
|
* @public
|
|
93
33
|
*/
|
|
94
34
|
export type EditorSchema = {
|
|
95
|
-
annotations: ReadonlyArray<
|
|
96
|
-
BaseDefinition & {
|
|
97
|
-
fields: ReadonlyArray<{name: string; type: string}>
|
|
98
|
-
}
|
|
99
|
-
>
|
|
35
|
+
annotations: ReadonlyArray<AnnotationSchemaType>
|
|
100
36
|
block: {
|
|
101
37
|
name: string
|
|
102
38
|
}
|
|
103
|
-
blockObjects: ReadonlyArray<
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
107
|
-
>
|
|
108
|
-
decorators: ReadonlyArray<
|
|
109
|
-
BaseDefinition & {
|
|
110
|
-
/**
|
|
111
|
-
* @deprecated
|
|
112
|
-
* Use `name` instead
|
|
113
|
-
*/
|
|
114
|
-
value: string
|
|
115
|
-
}
|
|
116
|
-
>
|
|
117
|
-
inlineObjects: ReadonlyArray<
|
|
118
|
-
BaseDefinition & {
|
|
119
|
-
fields: ReadonlyArray<{name: string; type: string}>
|
|
120
|
-
}
|
|
121
|
-
>
|
|
39
|
+
blockObjects: ReadonlyArray<BlockObjectSchemaType>
|
|
40
|
+
decorators: ReadonlyArray<DecoratorSchemaType>
|
|
41
|
+
inlineObjects: ReadonlyArray<InlineObjectSchemaType>
|
|
122
42
|
span: {
|
|
123
43
|
name: string
|
|
124
44
|
}
|
|
125
|
-
styles: ReadonlyArray<
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
>
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* @public
|
|
83
|
+
*/
|
|
84
|
+
export type ListSchemaType = BaseDefinition & {
|
|
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
|
|
143
101
|
}
|
|
144
102
|
|
|
145
103
|
export function legacySchemaToEditorSchema(
|
package/src/editor.ts
CHANGED
|
@@ -8,7 +8,7 @@ import type {Behavior} from './behaviors/behavior.types.behavior'
|
|
|
8
8
|
import type {ExternalBehaviorEvent} from './behaviors/behavior.types.event'
|
|
9
9
|
import type {EditorDom} from './editor/editor-dom'
|
|
10
10
|
import type {ExternalEditorEvent} from './editor/editor-machine'
|
|
11
|
-
import type {SchemaDefinition} from './editor/editor-schema'
|
|
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
|
@@ -14,13 +14,27 @@ export {
|
|
|
14
14
|
EditorProvider,
|
|
15
15
|
type EditorProviderProps,
|
|
16
16
|
} from './editor/editor-provider'
|
|
17
|
+
export type {
|
|
18
|
+
AnnotationSchemaType,
|
|
19
|
+
BlockObjectSchemaType,
|
|
20
|
+
DecoratorSchemaType,
|
|
21
|
+
EditorSchema,
|
|
22
|
+
InlineObjectSchemaType,
|
|
23
|
+
ListSchemaType,
|
|
24
|
+
StyleSchemaType,
|
|
25
|
+
} from './editor/editor-schema'
|
|
17
26
|
export {
|
|
18
27
|
defineSchema,
|
|
28
|
+
type AnnotationDefinition,
|
|
19
29
|
type BaseDefinition,
|
|
30
|
+
type BlockObjectDefinition,
|
|
31
|
+
type DecoratorDefinition,
|
|
20
32
|
type FieldDefinition,
|
|
33
|
+
type InlineObjectDefinition,
|
|
34
|
+
type ListDefinition,
|
|
21
35
|
type SchemaDefinition,
|
|
22
|
-
|
|
23
|
-
|
|
36
|
+
type StyleDefinition,
|
|
37
|
+
} from './editor/editor-schema-definition'
|
|
24
38
|
export {useEditorSelector, type EditorSelector} from './editor/editor-selector'
|
|
25
39
|
export type {EditorContext, EditorSnapshot} from './editor/editor-snapshot'
|
|
26
40
|
export {usePortableTextEditor} from './editor/hooks/usePortableTextEditor'
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {describe, expect, test} from 'vitest'
|
|
2
|
-
import {compileSchemaDefinition
|
|
2
|
+
import {compileSchemaDefinition} from '../editor/editor-schema'
|
|
3
|
+
import {defineSchema} from '../editor/editor-schema-definition'
|
|
3
4
|
import {applyOperationToPortableText} from './apply-operation-to-portable-text'
|
|
4
5
|
import {createTestKeyGenerator} from './test-key-generator'
|
|
5
6
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type {PortableTextBlock} from '@sanity/types'
|
|
2
2
|
import {describe, expect, test} from 'vitest'
|
|
3
|
-
import {compileSchemaDefinition
|
|
3
|
+
import {compileSchemaDefinition} from '../editor/editor-schema'
|
|
4
|
+
import {defineSchema} from '../editor/editor-schema-definition'
|
|
4
5
|
import {defaultKeyGenerator} from '../editor/key-generator'
|
|
5
6
|
import {buildIndexMaps} from './build-index-maps'
|
|
6
7
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type {EditorSnapshot} from '..'
|
|
2
|
-
import {compileSchemaDefinition
|
|
2
|
+
import {compileSchemaDefinition} from '../editor/editor-schema'
|
|
3
|
+
import {defineSchema} from '../editor/editor-schema-definition'
|
|
3
4
|
import {createTestKeyGenerator} from './test-key-generator'
|
|
4
5
|
|
|
5
6
|
export function createTestSnapshot(snapshot: {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {describe, expect, test} from 'vitest'
|
|
2
|
-
import {compileSchemaDefinition
|
|
2
|
+
import {compileSchemaDefinition} from '../editor/editor-schema'
|
|
3
|
+
import {defineSchema} from '../editor/editor-schema-definition'
|
|
3
4
|
import type {EditorSelection} from '../types/editor'
|
|
4
5
|
import {createTestSnapshot} from './create-test-snapshot'
|
|
5
6
|
import {getDragSelection} from './drag-selection'
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {describe, expect, test} from 'vitest'
|
|
2
|
-
import {compileSchemaDefinition
|
|
2
|
+
import {compileSchemaDefinition} from '../editor/editor-schema'
|
|
3
|
+
import {defineSchema} from '../editor/editor-schema-definition'
|
|
3
4
|
import {parseBlock, parseSpan} from './parse-blocks'
|
|
4
5
|
import {createTestKeyGenerator} from './test-key-generator'
|
|
5
6
|
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type {PortableTextBlock} from '@sanity/types'
|
|
2
|
-
import {compileSchemaDefinition
|
|
2
|
+
import {compileSchemaDefinition} from '../editor/editor-schema'
|
|
3
|
+
import {defineSchema} from '../editor/editor-schema-definition'
|
|
3
4
|
import type {EditorSelection} from '../types/editor'
|
|
4
5
|
import {sliceBlocks} from '../utils/util.slice-blocks'
|
|
5
6
|
import {getTersePt} from './terse-pt'
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import {expect, test} from 'vitest'
|
|
2
|
-
import {compileSchemaDefinition
|
|
2
|
+
import {compileSchemaDefinition} from '../editor/editor-schema'
|
|
3
|
+
import {defineSchema} from '../editor/editor-schema-definition'
|
|
3
4
|
import {createTestKeyGenerator} from '../internal-utils/test-key-generator'
|
|
4
5
|
import {getTersePt, parseTersePt, parseTersePtString} from './terse-pt'
|
|
5
6
|
|
|
@@ -6,7 +6,10 @@ import {render} from 'vitest-browser-react'
|
|
|
6
6
|
import type {Editor} from '../editor'
|
|
7
7
|
import {PortableTextEditable} from '../editor/Editable'
|
|
8
8
|
import {EditorProvider} from '../editor/editor-provider'
|
|
9
|
-
import {
|
|
9
|
+
import {
|
|
10
|
+
defineSchema,
|
|
11
|
+
type SchemaDefinition,
|
|
12
|
+
} from '../editor/editor-schema-definition'
|
|
10
13
|
import type {EditorEmittedEvent} from '../editor/relay-machine'
|
|
11
14
|
import {EditorRefPlugin} from '../plugins/plugin.editor-ref'
|
|
12
15
|
import {EventListenerPlugin} from '../plugins/plugin.event-listener'
|
|
@@ -4,7 +4,7 @@ import {describe, expect, test, vi} from 'vitest'
|
|
|
4
4
|
import {render} from 'vitest-browser-react'
|
|
5
5
|
import {PortableTextEditable, type Editor} from '..'
|
|
6
6
|
import {EditorProvider} from '../editor/editor-provider'
|
|
7
|
-
import {defineSchema} from '../editor/editor-schema'
|
|
7
|
+
import {defineSchema} from '../editor/editor-schema-definition'
|
|
8
8
|
import {getTersePt} from '../internal-utils/terse-pt'
|
|
9
9
|
import {createTestKeyGenerator} from '../internal-utils/test-key-generator'
|
|
10
10
|
import {getTextMarks} from '../internal-utils/text-marks'
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import type {PortableTextBlock} from '@sanity/types'
|
|
2
2
|
import {expect, test} from 'vitest'
|
|
3
3
|
import type {EditorSelection} from '..'
|
|
4
|
-
import {compileSchemaDefinition
|
|
4
|
+
import {compileSchemaDefinition} from '../editor/editor-schema'
|
|
5
|
+
import {defineSchema} from '../editor/editor-schema-definition'
|
|
5
6
|
import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
|
|
6
7
|
import {getSelectionText} from './selector.get-selection-text'
|
|
7
8
|
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type {PortableTextBlock} from '@sanity/types'
|
|
2
2
|
import {describe, expect, test} from 'vitest'
|
|
3
|
-
import {compileSchemaDefinition
|
|
3
|
+
import {compileSchemaDefinition} from '../editor/editor-schema'
|
|
4
|
+
import {defineSchema} from '../editor/editor-schema-definition'
|
|
4
5
|
import {createTestSnapshot} from '../internal-utils/create-test-snapshot'
|
|
5
6
|
import {parseBlock} from '../internal-utils/parse-blocks'
|
|
6
7
|
import {createTestKeyGenerator} from '../internal-utils/test-key-generator'
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type {PortableTextBlock} from '@sanity/types'
|
|
2
2
|
import {expect, test} from 'vitest'
|
|
3
|
-
import {compileSchemaDefinition
|
|
3
|
+
import {compileSchemaDefinition} from '../editor/editor-schema'
|
|
4
|
+
import {defineSchema} from '../editor/editor-schema-definition'
|
|
4
5
|
import {blockOffsetToSpanSelectionPoint} from './util.block-offset'
|
|
5
6
|
|
|
6
7
|
const schema = compileSchemaDefinition(defineSchema({}))
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type {PortableTextBlock, PortableTextTextBlock} from '@sanity/types'
|
|
2
2
|
import {describe, expect, test} from 'vitest'
|
|
3
|
-
import {compileSchemaDefinition
|
|
3
|
+
import {compileSchemaDefinition} from '../editor/editor-schema'
|
|
4
|
+
import {defineSchema} from '../editor/editor-schema-definition'
|
|
4
5
|
import {sliceBlocks} from './util.slice-blocks'
|
|
5
6
|
|
|
6
7
|
const b1: PortableTextTextBlock = {
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
PortableTextObject,
|
|
3
|
+
PortableTextSpan,
|
|
4
|
+
PortableTextTextBlock,
|
|
5
|
+
} from '@sanity/types'
|
|
6
|
+
import {describe, expect, test} from 'vitest'
|
|
7
|
+
import {keyGenerator} from '..'
|
|
8
|
+
import {compileSchemaDefinition} from '../editor/editor-schema'
|
|
9
|
+
import {defineSchema} from '../editor/editor-schema-definition'
|
|
10
|
+
import {sliceTextBlock} from './util.slice-text-block'
|
|
11
|
+
|
|
12
|
+
const schema = compileSchemaDefinition(
|
|
13
|
+
defineSchema({
|
|
14
|
+
inlineObjects: [{name: 'stock-ticker'}],
|
|
15
|
+
}),
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
function createSpan(span: Partial<PortableTextSpan>) {
|
|
19
|
+
return {
|
|
20
|
+
_type: 'span',
|
|
21
|
+
_key: span._key ?? keyGenerator(),
|
|
22
|
+
text: span.text ?? '',
|
|
23
|
+
marks: span.marks ?? [],
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function createInlineObject(inlineObject: Partial<PortableTextObject>) {
|
|
28
|
+
return {
|
|
29
|
+
_type: 'stock-ticker',
|
|
30
|
+
_key: inlineObject._key ?? keyGenerator(),
|
|
31
|
+
...inlineObject,
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function createBlock(
|
|
36
|
+
block: Partial<PortableTextTextBlock>,
|
|
37
|
+
): PortableTextTextBlock {
|
|
38
|
+
return {
|
|
39
|
+
_type: 'block',
|
|
40
|
+
_key: block._key ?? keyGenerator(),
|
|
41
|
+
children: block.children ?? [createSpan({})],
|
|
42
|
+
markDefs: block.markDefs ?? [],
|
|
43
|
+
style: block.style ?? 'normal',
|
|
44
|
+
...block,
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
describe(sliceTextBlock.name, () => {
|
|
49
|
+
test('empty block', () => {
|
|
50
|
+
const span = createSpan({})
|
|
51
|
+
const block = createBlock({
|
|
52
|
+
children: [span],
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
expect(
|
|
56
|
+
sliceTextBlock({
|
|
57
|
+
context: {
|
|
58
|
+
schema,
|
|
59
|
+
selection: {
|
|
60
|
+
anchor: {
|
|
61
|
+
path: [{_key: block._key}, 'children', {_key: span._key}],
|
|
62
|
+
offset: 0,
|
|
63
|
+
},
|
|
64
|
+
focus: {
|
|
65
|
+
path: [{_key: block._key}, 'children', {_key: span._key}],
|
|
66
|
+
offset: 0,
|
|
67
|
+
},
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
block,
|
|
71
|
+
}),
|
|
72
|
+
).toEqual(block)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
test('middle', () => {
|
|
76
|
+
const span = createSpan({
|
|
77
|
+
text: 'foo bar baz',
|
|
78
|
+
})
|
|
79
|
+
const block = createBlock({
|
|
80
|
+
children: [span],
|
|
81
|
+
})
|
|
82
|
+
|
|
83
|
+
expect(
|
|
84
|
+
sliceTextBlock({
|
|
85
|
+
context: {
|
|
86
|
+
schema,
|
|
87
|
+
selection: {
|
|
88
|
+
anchor: {
|
|
89
|
+
path: [{_key: block._key}, 'children', {_key: span._key}],
|
|
90
|
+
offset: 4,
|
|
91
|
+
},
|
|
92
|
+
focus: {
|
|
93
|
+
path: [{_key: block._key}, 'children', {_key: span._key}],
|
|
94
|
+
offset: 7,
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
block,
|
|
99
|
+
}),
|
|
100
|
+
).toEqual({
|
|
101
|
+
...block,
|
|
102
|
+
children: [
|
|
103
|
+
{
|
|
104
|
+
...span,
|
|
105
|
+
text: 'bar',
|
|
106
|
+
},
|
|
107
|
+
],
|
|
108
|
+
})
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
test('multiple children', () => {
|
|
112
|
+
const fooSpan = createSpan({
|
|
113
|
+
text: 'foo',
|
|
114
|
+
marks: ['strong'],
|
|
115
|
+
})
|
|
116
|
+
const barSpan = createSpan({
|
|
117
|
+
text: 'bar',
|
|
118
|
+
marks: ['em'],
|
|
119
|
+
})
|
|
120
|
+
const bazSpan = createSpan({
|
|
121
|
+
text: 'baz',
|
|
122
|
+
marks: ['underline'],
|
|
123
|
+
})
|
|
124
|
+
const stockTicker = createInlineObject({
|
|
125
|
+
symbol: 'AAPL',
|
|
126
|
+
})
|
|
127
|
+
const block = createBlock({
|
|
128
|
+
children: [fooSpan, barSpan, stockTicker, bazSpan],
|
|
129
|
+
})
|
|
130
|
+
|
|
131
|
+
expect(
|
|
132
|
+
sliceTextBlock({
|
|
133
|
+
context: {
|
|
134
|
+
schema,
|
|
135
|
+
selection: {
|
|
136
|
+
anchor: {
|
|
137
|
+
path: [{_key: block._key}, 'children', {_key: barSpan._key}],
|
|
138
|
+
offset: 1,
|
|
139
|
+
},
|
|
140
|
+
focus: {
|
|
141
|
+
path: [{_key: block._key}, 'children', {_key: bazSpan._key}],
|
|
142
|
+
offset: 1,
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
block,
|
|
147
|
+
}),
|
|
148
|
+
).toEqual({
|
|
149
|
+
...block,
|
|
150
|
+
children: [
|
|
151
|
+
{
|
|
152
|
+
...barSpan,
|
|
153
|
+
text: 'ar',
|
|
154
|
+
},
|
|
155
|
+
stockTicker,
|
|
156
|
+
{
|
|
157
|
+
...bazSpan,
|
|
158
|
+
text: 'b',
|
|
159
|
+
},
|
|
160
|
+
],
|
|
161
|
+
})
|
|
162
|
+
})
|
|
163
|
+
})
|