@portabletext/editor 1.50.3 → 1.50.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/behaviors/index.d.cts +1 -0
- package/lib/behaviors/index.d.ts +1 -0
- package/lib/index.cjs +322 -55
- package/lib/index.cjs.map +1 -1
- package/lib/index.d.cts +2 -0
- package/lib/index.d.ts +2 -0
- package/lib/index.js +323 -55
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.d.cts +1 -0
- package/lib/plugins/index.d.ts +1 -0
- package/lib/selectors/index.d.cts +1 -0
- package/lib/selectors/index.d.ts +1 -0
- package/lib/utils/index.d.cts +1 -0
- package/lib/utils/index.d.ts +1 -0
- package/package.json +2 -1
- package/src/editor/PortableTextEditor.tsx +22 -22
- package/src/editor/create-slate-editor.tsx +9 -1
- package/src/editor/editor-selector.ts +1 -5
- package/src/editor/editor-snapshot.ts +1 -3
- package/src/editor/plugins/slate-plugin.update-value.ts +30 -0
- package/src/editor/plugins/with-plugins.ts +8 -1
- package/src/internal-utils/apply-operation-to-portable-text.test.ts +175 -0
- package/src/internal-utils/apply-operation-to-portable-text.ts +435 -0
- package/src/internal-utils/create-placeholder-block.ts +20 -0
- package/src/internal-utils/portable-text-node.ts +209 -0
- package/src/types/editor.ts +1 -0
- package/src/internal-utils/__tests__/patchToOperations.test.ts +0 -312
- package/src/internal-utils/slate-children-to-blocks.ts +0 -49
|
@@ -1,312 +0,0 @@
|
|
|
1
|
-
import type {Patch} from '@portabletext/patches'
|
|
2
|
-
import {createEditor, type Descendant} from 'slate'
|
|
3
|
-
import {beforeEach, describe, expect, it} from 'vitest'
|
|
4
|
-
import {createActor} from 'xstate'
|
|
5
|
-
import {schemaType} from '../../editor/__tests__/PortableTextEditorTester'
|
|
6
|
-
import {editorMachine} from '../../editor/editor-machine'
|
|
7
|
-
import {legacySchemaToEditorSchema} from '../../editor/editor-schema'
|
|
8
|
-
import {defaultKeyGenerator} from '../../editor/key-generator'
|
|
9
|
-
import {createLegacySchema} from '../../editor/legacy-schema'
|
|
10
|
-
import {withPlugins} from '../../editor/plugins/with-plugins'
|
|
11
|
-
import {relayMachine} from '../../editor/relay-machine'
|
|
12
|
-
import {createApplyPatch} from '../applyPatch'
|
|
13
|
-
import {VOID_CHILD_KEY} from '../values'
|
|
14
|
-
|
|
15
|
-
const legacySchema = createLegacySchema(schemaType)
|
|
16
|
-
const schemaTypes = legacySchemaToEditorSchema(legacySchema)
|
|
17
|
-
|
|
18
|
-
const patchToOperations = createApplyPatch(schemaTypes)
|
|
19
|
-
|
|
20
|
-
const editor = withPlugins(createEditor(), {
|
|
21
|
-
editorActor: createActor(editorMachine, {
|
|
22
|
-
input: {
|
|
23
|
-
schema: schemaTypes,
|
|
24
|
-
keyGenerator: defaultKeyGenerator,
|
|
25
|
-
getLegacySchema: () => legacySchema,
|
|
26
|
-
},
|
|
27
|
-
}),
|
|
28
|
-
relayActor: createActor(relayMachine),
|
|
29
|
-
subscriptions: [],
|
|
30
|
-
})
|
|
31
|
-
|
|
32
|
-
const createDefaultValue = (): Descendant[] => [
|
|
33
|
-
{
|
|
34
|
-
_type: 'image',
|
|
35
|
-
_key: 'c01739b0d03b',
|
|
36
|
-
children: [
|
|
37
|
-
{
|
|
38
|
-
_key: VOID_CHILD_KEY,
|
|
39
|
-
_type: 'span',
|
|
40
|
-
text: '',
|
|
41
|
-
marks: [],
|
|
42
|
-
},
|
|
43
|
-
],
|
|
44
|
-
__inline: false,
|
|
45
|
-
value: {
|
|
46
|
-
asset: {
|
|
47
|
-
_ref: 'image-f52f71bc1df46e080dabe43a8effe8ccfb5f21de-4032x3024-png',
|
|
48
|
-
_type: 'reference',
|
|
49
|
-
},
|
|
50
|
-
},
|
|
51
|
-
},
|
|
52
|
-
]
|
|
53
|
-
|
|
54
|
-
describe('operationToPatches', () => {
|
|
55
|
-
beforeEach(() => {
|
|
56
|
-
editor.onChange()
|
|
57
|
-
})
|
|
58
|
-
|
|
59
|
-
it('makes the correct operations for block objects', () => {
|
|
60
|
-
editor.children = createDefaultValue()
|
|
61
|
-
const patches = [
|
|
62
|
-
{
|
|
63
|
-
type: 'unset',
|
|
64
|
-
path: [{_key: 'c01739b0d03b'}, 'hotspot'],
|
|
65
|
-
origin: 'remote',
|
|
66
|
-
},
|
|
67
|
-
{type: 'unset', path: [{_key: 'c01739b0d03b'}, 'crop'], origin: 'remote'},
|
|
68
|
-
{
|
|
69
|
-
type: 'set',
|
|
70
|
-
path: [{_key: 'c01739b0d03b'}, 'asset'],
|
|
71
|
-
value: {
|
|
72
|
-
_ref: 'image-b5681d9d0b2b6c922238e7c694500dd7c1349b19-256x256-jpg',
|
|
73
|
-
_type: 'reference',
|
|
74
|
-
},
|
|
75
|
-
origin: 'remote',
|
|
76
|
-
},
|
|
77
|
-
] as Patch[]
|
|
78
|
-
patches.forEach((p) => {
|
|
79
|
-
patchToOperations(editor, p)
|
|
80
|
-
})
|
|
81
|
-
expect(editor.children).toEqual([
|
|
82
|
-
{
|
|
83
|
-
__inline: false,
|
|
84
|
-
_key: 'c01739b0d03b',
|
|
85
|
-
_type: 'image',
|
|
86
|
-
children: [
|
|
87
|
-
{
|
|
88
|
-
_key: VOID_CHILD_KEY,
|
|
89
|
-
_type: 'span',
|
|
90
|
-
marks: [],
|
|
91
|
-
text: '',
|
|
92
|
-
},
|
|
93
|
-
],
|
|
94
|
-
value: {
|
|
95
|
-
asset: {
|
|
96
|
-
_ref: 'image-b5681d9d0b2b6c922238e7c694500dd7c1349b19-256x256-jpg',
|
|
97
|
-
_type: 'reference',
|
|
98
|
-
},
|
|
99
|
-
},
|
|
100
|
-
},
|
|
101
|
-
])
|
|
102
|
-
})
|
|
103
|
-
it('will not create operations for insertion inside blocks', () => {
|
|
104
|
-
editor.children = [
|
|
105
|
-
{
|
|
106
|
-
_type: 'someType',
|
|
107
|
-
_key: 'c01739b0d03b',
|
|
108
|
-
children: [
|
|
109
|
-
{
|
|
110
|
-
_key: VOID_CHILD_KEY,
|
|
111
|
-
_type: 'span',
|
|
112
|
-
text: '',
|
|
113
|
-
marks: [],
|
|
114
|
-
},
|
|
115
|
-
],
|
|
116
|
-
__inline: false,
|
|
117
|
-
value: {
|
|
118
|
-
asset: {
|
|
119
|
-
_ref: 'image-f52f71bc1df46e080dabe43a8effe8ccfb5f21de-4032x3024-png',
|
|
120
|
-
_type: 'reference',
|
|
121
|
-
},
|
|
122
|
-
nestedArray: [],
|
|
123
|
-
},
|
|
124
|
-
},
|
|
125
|
-
]
|
|
126
|
-
const patches = [
|
|
127
|
-
{
|
|
128
|
-
type: 'insert',
|
|
129
|
-
path: [{_key: 'c01739b0d03b'}, 'nestedArray'],
|
|
130
|
-
origin: 'remote',
|
|
131
|
-
},
|
|
132
|
-
] as Patch[]
|
|
133
|
-
patches.forEach((p) => {
|
|
134
|
-
patchToOperations(editor, p)
|
|
135
|
-
})
|
|
136
|
-
expect(editor.children).toMatchInlineSnapshot(`
|
|
137
|
-
[
|
|
138
|
-
{
|
|
139
|
-
"__inline": false,
|
|
140
|
-
"_key": "c01739b0d03b",
|
|
141
|
-
"_type": "someType",
|
|
142
|
-
"children": [
|
|
143
|
-
{
|
|
144
|
-
"_key": "${VOID_CHILD_KEY}",
|
|
145
|
-
"_type": "span",
|
|
146
|
-
"marks": [],
|
|
147
|
-
"text": "",
|
|
148
|
-
},
|
|
149
|
-
],
|
|
150
|
-
"value": {
|
|
151
|
-
"asset": {
|
|
152
|
-
"_ref": "image-f52f71bc1df46e080dabe43a8effe8ccfb5f21de-4032x3024-png",
|
|
153
|
-
"_type": "reference",
|
|
154
|
-
},
|
|
155
|
-
"nestedArray": [],
|
|
156
|
-
},
|
|
157
|
-
},
|
|
158
|
-
]
|
|
159
|
-
`)
|
|
160
|
-
})
|
|
161
|
-
it('will not create operations for removal inside blocks', () => {
|
|
162
|
-
editor.children = [
|
|
163
|
-
{
|
|
164
|
-
_type: 'someType',
|
|
165
|
-
_key: 'c01739b0d03b',
|
|
166
|
-
children: [
|
|
167
|
-
{
|
|
168
|
-
_key: VOID_CHILD_KEY,
|
|
169
|
-
_type: 'span',
|
|
170
|
-
text: '',
|
|
171
|
-
marks: [],
|
|
172
|
-
},
|
|
173
|
-
],
|
|
174
|
-
__inline: false,
|
|
175
|
-
value: {
|
|
176
|
-
asset: {
|
|
177
|
-
_ref: 'image-f52f71bc1df46e080dabe43a8effe8ccfb5f21de-4032x3024-png',
|
|
178
|
-
_type: 'reference',
|
|
179
|
-
},
|
|
180
|
-
nestedArray: [
|
|
181
|
-
{
|
|
182
|
-
_key: 'foo',
|
|
183
|
-
_type: 'nestedValue',
|
|
184
|
-
},
|
|
185
|
-
],
|
|
186
|
-
},
|
|
187
|
-
},
|
|
188
|
-
]
|
|
189
|
-
const patches = [
|
|
190
|
-
{
|
|
191
|
-
type: 'unset',
|
|
192
|
-
path: [{_key: 'c01739b0d03b'}, 'nestedArray', 0],
|
|
193
|
-
origin: 'remote',
|
|
194
|
-
},
|
|
195
|
-
] as Patch[]
|
|
196
|
-
patches.forEach((p) => {
|
|
197
|
-
patchToOperations(editor, p)
|
|
198
|
-
})
|
|
199
|
-
expect(editor.children).toMatchInlineSnapshot(`
|
|
200
|
-
[
|
|
201
|
-
{
|
|
202
|
-
"__inline": false,
|
|
203
|
-
"_key": "c01739b0d03b",
|
|
204
|
-
"_type": "someType",
|
|
205
|
-
"children": [
|
|
206
|
-
{
|
|
207
|
-
"_key": "${VOID_CHILD_KEY}",
|
|
208
|
-
"_type": "span",
|
|
209
|
-
"marks": [],
|
|
210
|
-
"text": "",
|
|
211
|
-
},
|
|
212
|
-
],
|
|
213
|
-
"value": {
|
|
214
|
-
"asset": {
|
|
215
|
-
"_ref": "image-f52f71bc1df46e080dabe43a8effe8ccfb5f21de-4032x3024-png",
|
|
216
|
-
"_type": "reference",
|
|
217
|
-
},
|
|
218
|
-
"nestedArray": [
|
|
219
|
-
{
|
|
220
|
-
"_key": "foo",
|
|
221
|
-
"_type": "nestedValue",
|
|
222
|
-
},
|
|
223
|
-
],
|
|
224
|
-
},
|
|
225
|
-
},
|
|
226
|
-
]
|
|
227
|
-
`)
|
|
228
|
-
})
|
|
229
|
-
it('will not create operations for setting data inside blocks', () => {
|
|
230
|
-
editor.children = [
|
|
231
|
-
{
|
|
232
|
-
_key: '1335959d4d03',
|
|
233
|
-
_type: 'block',
|
|
234
|
-
children: [
|
|
235
|
-
{
|
|
236
|
-
_key: '9bd868adcd6b',
|
|
237
|
-
_type: 'span',
|
|
238
|
-
marks: [],
|
|
239
|
-
text: '1 ',
|
|
240
|
-
},
|
|
241
|
-
{
|
|
242
|
-
_key: '6f75d593f3fc',
|
|
243
|
-
_type: 'span',
|
|
244
|
-
marks: ['11de7fcea659'],
|
|
245
|
-
text: '2',
|
|
246
|
-
},
|
|
247
|
-
{
|
|
248
|
-
_key: '033618a7f081',
|
|
249
|
-
_type: 'span',
|
|
250
|
-
marks: [],
|
|
251
|
-
text: ' 3',
|
|
252
|
-
},
|
|
253
|
-
],
|
|
254
|
-
markDefs: [
|
|
255
|
-
{
|
|
256
|
-
_key: '11de7fcea659',
|
|
257
|
-
_type: 'link',
|
|
258
|
-
},
|
|
259
|
-
],
|
|
260
|
-
style: 'normal',
|
|
261
|
-
},
|
|
262
|
-
]
|
|
263
|
-
const patches = [
|
|
264
|
-
{
|
|
265
|
-
type: 'set',
|
|
266
|
-
path: [{_key: '1335959d4d03'}, 'markDefs', {_key: '11de7fcea659'}],
|
|
267
|
-
origin: 'remote',
|
|
268
|
-
value: {href: 'http://www.test.com'},
|
|
269
|
-
},
|
|
270
|
-
] as Patch[]
|
|
271
|
-
patches.forEach((p) => {
|
|
272
|
-
patchToOperations(editor, p)
|
|
273
|
-
})
|
|
274
|
-
expect(editor.children).toMatchInlineSnapshot(`
|
|
275
|
-
[
|
|
276
|
-
{
|
|
277
|
-
"_key": "1335959d4d03",
|
|
278
|
-
"_type": "block",
|
|
279
|
-
"children": [
|
|
280
|
-
{
|
|
281
|
-
"_key": "9bd868adcd6b",
|
|
282
|
-
"_type": "span",
|
|
283
|
-
"marks": [],
|
|
284
|
-
"text": "1 ",
|
|
285
|
-
},
|
|
286
|
-
{
|
|
287
|
-
"_key": "6f75d593f3fc",
|
|
288
|
-
"_type": "span",
|
|
289
|
-
"marks": [
|
|
290
|
-
"11de7fcea659",
|
|
291
|
-
],
|
|
292
|
-
"text": "2",
|
|
293
|
-
},
|
|
294
|
-
{
|
|
295
|
-
"_key": "033618a7f081",
|
|
296
|
-
"_type": "span",
|
|
297
|
-
"marks": [],
|
|
298
|
-
"text": " 3",
|
|
299
|
-
},
|
|
300
|
-
],
|
|
301
|
-
"markDefs": [
|
|
302
|
-
{
|
|
303
|
-
"_key": "11de7fcea659",
|
|
304
|
-
"_type": "link",
|
|
305
|
-
},
|
|
306
|
-
],
|
|
307
|
-
"style": "normal",
|
|
308
|
-
},
|
|
309
|
-
]
|
|
310
|
-
`)
|
|
311
|
-
})
|
|
312
|
-
})
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import type {PortableTextBlock} from '@sanity/types'
|
|
2
|
-
import type {Descendant} from 'slate'
|
|
3
|
-
import type {EditorSchema} from '../editor/editor-schema'
|
|
4
|
-
|
|
5
|
-
export function slateChildrenToBlocks(
|
|
6
|
-
schema: EditorSchema,
|
|
7
|
-
value: Array<Descendant>,
|
|
8
|
-
): Array<PortableTextBlock> {
|
|
9
|
-
const blocks: Array<PortableTextBlock> = new Array(value.length)
|
|
10
|
-
|
|
11
|
-
for (let blockIndex = 0; blockIndex < value.length; blockIndex++) {
|
|
12
|
-
const descendant = value[blockIndex]
|
|
13
|
-
|
|
14
|
-
if (descendant._type !== schema.block.name) {
|
|
15
|
-
blocks[blockIndex] = {
|
|
16
|
-
_key: descendant._key,
|
|
17
|
-
_type: descendant._type,
|
|
18
|
-
...('value' in descendant && typeof descendant.value === 'object'
|
|
19
|
-
? descendant.value
|
|
20
|
-
: {}),
|
|
21
|
-
}
|
|
22
|
-
continue
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
const children = 'children' in descendant ? descendant.children : []
|
|
26
|
-
const processedChildren = new Array(children.length)
|
|
27
|
-
|
|
28
|
-
for (let childIndex = 0; childIndex < children.length; childIndex++) {
|
|
29
|
-
const child = children[childIndex]
|
|
30
|
-
processedChildren[childIndex] =
|
|
31
|
-
child._type === schema.span.name
|
|
32
|
-
? child
|
|
33
|
-
: {
|
|
34
|
-
_key: child._key,
|
|
35
|
-
_type: child._type,
|
|
36
|
-
...('value' in child && typeof child.value === 'object'
|
|
37
|
-
? child.value
|
|
38
|
-
: {}),
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
blocks[blockIndex] = {
|
|
43
|
-
...descendant,
|
|
44
|
-
children: processedChildren,
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
return blocks
|
|
49
|
-
}
|