@portabletext/editor 0.0.0
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/LICENSE +21 -0
- package/README.md +3 -0
- package/lib/index.d.mts +911 -0
- package/lib/index.d.ts +911 -0
- package/lib/index.esm.js +4896 -0
- package/lib/index.esm.js.map +1 -0
- package/lib/index.js +4874 -0
- package/lib/index.js.map +1 -0
- package/lib/index.mjs +4896 -0
- package/lib/index.mjs.map +1 -0
- package/package.json +119 -0
- package/src/editor/Editable.tsx +683 -0
- package/src/editor/PortableTextEditor.tsx +308 -0
- package/src/editor/__tests__/PortableTextEditor.test.tsx +386 -0
- package/src/editor/__tests__/PortableTextEditorTester.tsx +116 -0
- package/src/editor/__tests__/RangeDecorations.test.tsx +115 -0
- package/src/editor/__tests__/handleClick.test.tsx +218 -0
- package/src/editor/__tests__/pteWarningsSelfSolving.test.tsx +389 -0
- package/src/editor/__tests__/utils.ts +39 -0
- package/src/editor/components/DraggableBlock.tsx +287 -0
- package/src/editor/components/Element.tsx +279 -0
- package/src/editor/components/Leaf.tsx +288 -0
- package/src/editor/components/SlateContainer.tsx +81 -0
- package/src/editor/components/Synchronizer.tsx +190 -0
- package/src/editor/hooks/usePortableTextEditor.ts +23 -0
- package/src/editor/hooks/usePortableTextEditorKeyGenerator.ts +24 -0
- package/src/editor/hooks/usePortableTextEditorSelection.ts +22 -0
- package/src/editor/hooks/usePortableTextEditorValue.ts +16 -0
- package/src/editor/hooks/usePortableTextReadOnly.ts +20 -0
- package/src/editor/hooks/useSyncValue.test.tsx +125 -0
- package/src/editor/hooks/useSyncValue.ts +372 -0
- package/src/editor/nodes/DefaultAnnotation.tsx +16 -0
- package/src/editor/nodes/DefaultObject.tsx +15 -0
- package/src/editor/nodes/index.ts +189 -0
- package/src/editor/plugins/__tests__/withEditableAPIDelete.test.tsx +244 -0
- package/src/editor/plugins/__tests__/withEditableAPIGetFragment.test.tsx +142 -0
- package/src/editor/plugins/__tests__/withEditableAPIInsert.test.tsx +346 -0
- package/src/editor/plugins/__tests__/withEditableAPISelectionsOverlapping.test.tsx +162 -0
- package/src/editor/plugins/__tests__/withHotkeys.test.tsx +212 -0
- package/src/editor/plugins/__tests__/withInsertBreak.test.tsx +204 -0
- package/src/editor/plugins/__tests__/withPlaceholderBlock.test.tsx +133 -0
- package/src/editor/plugins/__tests__/withPortableTextLists.test.tsx +65 -0
- package/src/editor/plugins/__tests__/withPortableTextMarkModel.test.tsx +1377 -0
- package/src/editor/plugins/__tests__/withPortableTextSelections.test.tsx +91 -0
- package/src/editor/plugins/__tests__/withUndoRedo.test.tsx +115 -0
- package/src/editor/plugins/createWithEditableAPI.ts +573 -0
- package/src/editor/plugins/createWithHotKeys.ts +304 -0
- package/src/editor/plugins/createWithInsertBreak.ts +45 -0
- package/src/editor/plugins/createWithInsertData.ts +359 -0
- package/src/editor/plugins/createWithMaxBlocks.ts +24 -0
- package/src/editor/plugins/createWithObjectKeys.ts +63 -0
- package/src/editor/plugins/createWithPatches.ts +274 -0
- package/src/editor/plugins/createWithPlaceholderBlock.ts +36 -0
- package/src/editor/plugins/createWithPortableTextBlockStyle.ts +91 -0
- package/src/editor/plugins/createWithPortableTextLists.ts +160 -0
- package/src/editor/plugins/createWithPortableTextMarkModel.ts +441 -0
- package/src/editor/plugins/createWithPortableTextSelections.ts +65 -0
- package/src/editor/plugins/createWithSchemaTypes.ts +76 -0
- package/src/editor/plugins/createWithUndoRedo.ts +494 -0
- package/src/editor/plugins/createWithUtils.ts +81 -0
- package/src/editor/plugins/index.ts +155 -0
- package/src/index.ts +11 -0
- package/src/patch/PatchEvent.ts +33 -0
- package/src/patch/applyPatch.ts +29 -0
- package/src/patch/array.ts +89 -0
- package/src/patch/arrayInsert.ts +27 -0
- package/src/patch/object.ts +39 -0
- package/src/patch/patches.ts +53 -0
- package/src/patch/primitive.ts +43 -0
- package/src/patch/string.ts +51 -0
- package/src/types/editor.ts +576 -0
- package/src/types/options.ts +17 -0
- package/src/types/patch.ts +65 -0
- package/src/types/slate.ts +25 -0
- package/src/utils/__tests__/dmpToOperations.test.ts +181 -0
- package/src/utils/__tests__/operationToPatches.test.ts +421 -0
- package/src/utils/__tests__/patchToOperations.test.ts +293 -0
- package/src/utils/__tests__/ranges.test.ts +18 -0
- package/src/utils/__tests__/valueNormalization.test.tsx +62 -0
- package/src/utils/__tests__/values.test.ts +253 -0
- package/src/utils/applyPatch.ts +407 -0
- package/src/utils/bufferUntil.ts +15 -0
- package/src/utils/debug.ts +12 -0
- package/src/utils/getPortableTextMemberSchemaTypes.ts +100 -0
- package/src/utils/operationToPatches.ts +357 -0
- package/src/utils/patches.ts +36 -0
- package/src/utils/paths.ts +60 -0
- package/src/utils/ranges.ts +77 -0
- package/src/utils/schema.ts +8 -0
- package/src/utils/selection.ts +65 -0
- package/src/utils/ucs2Indices.ts +67 -0
- package/src/utils/validateValue.ts +394 -0
- package/src/utils/values.ts +208 -0
- package/src/utils/weakMaps.ts +24 -0
- package/src/utils/withChanges.ts +25 -0
- package/src/utils/withPreserveKeys.ts +14 -0
- package/src/utils/withoutPatching.ts +14 -0
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
import {beforeEach, describe, expect, it} from '@jest/globals'
|
|
2
|
+
import {noop} from 'lodash'
|
|
3
|
+
import {createEditor, type Descendant} from 'slate'
|
|
4
|
+
|
|
5
|
+
import {keyGenerator, type Patch, PortableTextEditor} from '../..'
|
|
6
|
+
import {schemaType} from '../../editor/__tests__/PortableTextEditorTester'
|
|
7
|
+
import {withPlugins} from '../../editor/plugins'
|
|
8
|
+
import {createApplyPatch} from '../applyPatch'
|
|
9
|
+
import {getPortableTextMemberSchemaTypes} from '../getPortableTextMemberSchemaTypes'
|
|
10
|
+
import {VOID_CHILD_KEY} from '../values'
|
|
11
|
+
|
|
12
|
+
const schemaTypes = getPortableTextMemberSchemaTypes(schemaType)
|
|
13
|
+
|
|
14
|
+
const patchToOperations = createApplyPatch(schemaTypes)
|
|
15
|
+
const portableTextEditor = new PortableTextEditor({schemaType, onChange: noop})
|
|
16
|
+
|
|
17
|
+
const {editor} = withPlugins(createEditor(), {
|
|
18
|
+
portableTextEditor,
|
|
19
|
+
keyGenerator,
|
|
20
|
+
readOnly: false,
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
const createDefaultValue = (): Descendant[] => [
|
|
24
|
+
{
|
|
25
|
+
_type: 'image',
|
|
26
|
+
_key: 'c01739b0d03b',
|
|
27
|
+
children: [
|
|
28
|
+
{
|
|
29
|
+
_key: VOID_CHILD_KEY,
|
|
30
|
+
_type: 'span',
|
|
31
|
+
text: '',
|
|
32
|
+
marks: [],
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
__inline: false,
|
|
36
|
+
value: {
|
|
37
|
+
asset: {
|
|
38
|
+
_ref: 'image-f52f71bc1df46e080dabe43a8effe8ccfb5f21de-4032x3024-png',
|
|
39
|
+
_type: 'reference',
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
describe('operationToPatches', () => {
|
|
46
|
+
beforeEach(() => {
|
|
47
|
+
editor.onChange()
|
|
48
|
+
})
|
|
49
|
+
|
|
50
|
+
it('makes the correct operations for block objects', () => {
|
|
51
|
+
editor.children = createDefaultValue()
|
|
52
|
+
const patches = [
|
|
53
|
+
{type: 'unset', path: [{_key: 'c01739b0d03b'}, 'hotspot'], origin: 'remote'},
|
|
54
|
+
{type: 'unset', path: [{_key: 'c01739b0d03b'}, 'crop'], origin: 'remote'},
|
|
55
|
+
{
|
|
56
|
+
type: 'set',
|
|
57
|
+
path: [{_key: 'c01739b0d03b'}, 'asset'],
|
|
58
|
+
value: {
|
|
59
|
+
_ref: 'image-b5681d9d0b2b6c922238e7c694500dd7c1349b19-256x256-jpg',
|
|
60
|
+
_type: 'reference',
|
|
61
|
+
},
|
|
62
|
+
origin: 'remote',
|
|
63
|
+
},
|
|
64
|
+
] as Patch[]
|
|
65
|
+
patches.forEach((p) => {
|
|
66
|
+
patchToOperations(editor, p)
|
|
67
|
+
})
|
|
68
|
+
expect(editor.children).toMatchInlineSnapshot(`
|
|
69
|
+
Array [
|
|
70
|
+
Object {
|
|
71
|
+
"__inline": false,
|
|
72
|
+
"_key": "c01739b0d03b",
|
|
73
|
+
"_type": "image",
|
|
74
|
+
"children": Array [
|
|
75
|
+
Object {
|
|
76
|
+
"_key": "${VOID_CHILD_KEY}",
|
|
77
|
+
"_type": "span",
|
|
78
|
+
"marks": Array [],
|
|
79
|
+
"text": "",
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
"value": Object {
|
|
83
|
+
"asset": Object {
|
|
84
|
+
"_ref": "image-f52f71bc1df46e080dabe43a8effe8ccfb5f21de-4032x3024-png",
|
|
85
|
+
"_type": "reference",
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
]
|
|
90
|
+
`)
|
|
91
|
+
})
|
|
92
|
+
it('will not create operations for insertion inside object blocks', () => {
|
|
93
|
+
editor.children = [
|
|
94
|
+
{
|
|
95
|
+
_type: 'someType',
|
|
96
|
+
_key: 'c01739b0d03b',
|
|
97
|
+
children: [
|
|
98
|
+
{
|
|
99
|
+
_key: VOID_CHILD_KEY,
|
|
100
|
+
_type: 'span',
|
|
101
|
+
text: '',
|
|
102
|
+
marks: [],
|
|
103
|
+
},
|
|
104
|
+
],
|
|
105
|
+
__inline: false,
|
|
106
|
+
value: {
|
|
107
|
+
asset: {
|
|
108
|
+
_ref: 'image-f52f71bc1df46e080dabe43a8effe8ccfb5f21de-4032x3024-png',
|
|
109
|
+
_type: 'reference',
|
|
110
|
+
},
|
|
111
|
+
nestedArray: [],
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
]
|
|
115
|
+
const patches = [
|
|
116
|
+
{type: 'insert', path: [{_key: 'c01739b0d03b'}, 'nestedArray'], origin: 'remote'},
|
|
117
|
+
] as Patch[]
|
|
118
|
+
patches.forEach((p) => {
|
|
119
|
+
patchToOperations(editor, p)
|
|
120
|
+
})
|
|
121
|
+
expect(editor.children).toMatchInlineSnapshot(`
|
|
122
|
+
Array [
|
|
123
|
+
Object {
|
|
124
|
+
"__inline": false,
|
|
125
|
+
"_key": "c01739b0d03b",
|
|
126
|
+
"_type": "someType",
|
|
127
|
+
"children": Array [
|
|
128
|
+
Object {
|
|
129
|
+
"_key": "${VOID_CHILD_KEY}",
|
|
130
|
+
"_type": "span",
|
|
131
|
+
"marks": Array [],
|
|
132
|
+
"text": "",
|
|
133
|
+
},
|
|
134
|
+
],
|
|
135
|
+
"value": Object {
|
|
136
|
+
"asset": Object {
|
|
137
|
+
"_ref": "image-f52f71bc1df46e080dabe43a8effe8ccfb5f21de-4032x3024-png",
|
|
138
|
+
"_type": "reference",
|
|
139
|
+
},
|
|
140
|
+
"nestedArray": Array [],
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
]
|
|
144
|
+
`)
|
|
145
|
+
})
|
|
146
|
+
it('will not create operations for removal inside object blocks', () => {
|
|
147
|
+
editor.children = [
|
|
148
|
+
{
|
|
149
|
+
_type: 'someType',
|
|
150
|
+
_key: 'c01739b0d03b',
|
|
151
|
+
children: [
|
|
152
|
+
{
|
|
153
|
+
_key: VOID_CHILD_KEY,
|
|
154
|
+
_type: 'span',
|
|
155
|
+
text: '',
|
|
156
|
+
marks: [],
|
|
157
|
+
},
|
|
158
|
+
],
|
|
159
|
+
__inline: false,
|
|
160
|
+
value: {
|
|
161
|
+
asset: {
|
|
162
|
+
_ref: 'image-f52f71bc1df46e080dabe43a8effe8ccfb5f21de-4032x3024-png',
|
|
163
|
+
_type: 'reference',
|
|
164
|
+
},
|
|
165
|
+
nestedArray: [
|
|
166
|
+
{
|
|
167
|
+
_key: 'foo',
|
|
168
|
+
_type: 'nestedValue',
|
|
169
|
+
},
|
|
170
|
+
],
|
|
171
|
+
},
|
|
172
|
+
},
|
|
173
|
+
]
|
|
174
|
+
const patches = [
|
|
175
|
+
{type: 'unset', path: [{_key: 'c01739b0d03b'}, 'nestedArray', 0], origin: 'remote'},
|
|
176
|
+
] as Patch[]
|
|
177
|
+
patches.forEach((p) => {
|
|
178
|
+
patchToOperations(editor, p)
|
|
179
|
+
})
|
|
180
|
+
expect(editor.children).toMatchInlineSnapshot(`
|
|
181
|
+
Array [
|
|
182
|
+
Object {
|
|
183
|
+
"__inline": false,
|
|
184
|
+
"_key": "c01739b0d03b",
|
|
185
|
+
"_type": "someType",
|
|
186
|
+
"children": Array [
|
|
187
|
+
Object {
|
|
188
|
+
"_key": "${VOID_CHILD_KEY}",
|
|
189
|
+
"_type": "span",
|
|
190
|
+
"marks": Array [],
|
|
191
|
+
"text": "",
|
|
192
|
+
},
|
|
193
|
+
],
|
|
194
|
+
"value": Object {
|
|
195
|
+
"asset": Object {
|
|
196
|
+
"_ref": "image-f52f71bc1df46e080dabe43a8effe8ccfb5f21de-4032x3024-png",
|
|
197
|
+
"_type": "reference",
|
|
198
|
+
},
|
|
199
|
+
"nestedArray": Array [
|
|
200
|
+
Object {
|
|
201
|
+
"_key": "foo",
|
|
202
|
+
"_type": "nestedValue",
|
|
203
|
+
},
|
|
204
|
+
],
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
]
|
|
208
|
+
`)
|
|
209
|
+
})
|
|
210
|
+
it('will not create operations for setting data inside object blocks', () => {
|
|
211
|
+
editor.children = [
|
|
212
|
+
{
|
|
213
|
+
_key: '1335959d4d03',
|
|
214
|
+
_type: 'block',
|
|
215
|
+
children: [
|
|
216
|
+
{
|
|
217
|
+
_key: '9bd868adcd6b',
|
|
218
|
+
_type: 'span',
|
|
219
|
+
marks: [],
|
|
220
|
+
text: '1 ',
|
|
221
|
+
},
|
|
222
|
+
{
|
|
223
|
+
_key: '6f75d593f3fc',
|
|
224
|
+
_type: 'span',
|
|
225
|
+
marks: ['11de7fcea659'],
|
|
226
|
+
text: '2',
|
|
227
|
+
},
|
|
228
|
+
{
|
|
229
|
+
_key: '033618a7f081',
|
|
230
|
+
_type: 'span',
|
|
231
|
+
marks: [],
|
|
232
|
+
text: ' 3',
|
|
233
|
+
},
|
|
234
|
+
],
|
|
235
|
+
markDefs: [
|
|
236
|
+
{
|
|
237
|
+
_key: '11de7fcea659',
|
|
238
|
+
_type: 'link',
|
|
239
|
+
},
|
|
240
|
+
],
|
|
241
|
+
style: 'normal',
|
|
242
|
+
},
|
|
243
|
+
]
|
|
244
|
+
const patches = [
|
|
245
|
+
{
|
|
246
|
+
type: 'set',
|
|
247
|
+
path: [{_key: '1335959d4d03'}, 'markDefs', {_key: '11de7fcea659'}],
|
|
248
|
+
origin: 'remote',
|
|
249
|
+
value: {href: 'http://www.test.com'},
|
|
250
|
+
},
|
|
251
|
+
] as Patch[]
|
|
252
|
+
patches.forEach((p) => {
|
|
253
|
+
patchToOperations(editor, p)
|
|
254
|
+
})
|
|
255
|
+
expect(editor.children).toMatchInlineSnapshot(`
|
|
256
|
+
Array [
|
|
257
|
+
Object {
|
|
258
|
+
"_key": "1335959d4d03",
|
|
259
|
+
"_type": "block",
|
|
260
|
+
"children": Array [
|
|
261
|
+
Object {
|
|
262
|
+
"_key": "9bd868adcd6b",
|
|
263
|
+
"_type": "span",
|
|
264
|
+
"marks": Array [],
|
|
265
|
+
"text": "1 ",
|
|
266
|
+
},
|
|
267
|
+
Object {
|
|
268
|
+
"_key": "6f75d593f3fc",
|
|
269
|
+
"_type": "span",
|
|
270
|
+
"marks": Array [
|
|
271
|
+
"11de7fcea659",
|
|
272
|
+
],
|
|
273
|
+
"text": "2",
|
|
274
|
+
},
|
|
275
|
+
Object {
|
|
276
|
+
"_key": "033618a7f081",
|
|
277
|
+
"_type": "span",
|
|
278
|
+
"marks": Array [],
|
|
279
|
+
"text": " 3",
|
|
280
|
+
},
|
|
281
|
+
],
|
|
282
|
+
"markDefs": Array [
|
|
283
|
+
Object {
|
|
284
|
+
"_key": "11de7fcea659",
|
|
285
|
+
"_type": "link",
|
|
286
|
+
},
|
|
287
|
+
],
|
|
288
|
+
"style": "normal",
|
|
289
|
+
},
|
|
290
|
+
]
|
|
291
|
+
`)
|
|
292
|
+
})
|
|
293
|
+
})
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {describe, expect, it} from '@jest/globals'
|
|
2
|
+
import {type InsertTextOperation, type Range} from 'slate'
|
|
3
|
+
|
|
4
|
+
import {moveRangeByOperation} from '../ranges'
|
|
5
|
+
|
|
6
|
+
describe('moveRangeByOperation', () => {
|
|
7
|
+
it('should move range when inserting text in front of it', () => {
|
|
8
|
+
const range: Range = {anchor: {path: [0, 0], offset: 1}, focus: {path: [0, 0], offset: 3}}
|
|
9
|
+
const operation: InsertTextOperation = {
|
|
10
|
+
type: 'insert_text',
|
|
11
|
+
path: [0, 0],
|
|
12
|
+
offset: 0,
|
|
13
|
+
text: 'foo',
|
|
14
|
+
}
|
|
15
|
+
const newRange = moveRangeByOperation(range, operation)
|
|
16
|
+
expect(newRange).toEqual({anchor: {path: [0, 0], offset: 4}, focus: {path: [0, 0], offset: 6}})
|
|
17
|
+
})
|
|
18
|
+
})
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import {describe, expect, it, jest} from '@jest/globals'
|
|
2
|
+
import {render, waitFor} from '@testing-library/react'
|
|
3
|
+
import {createRef, type RefObject} from 'react'
|
|
4
|
+
|
|
5
|
+
import {PortableTextEditorTester, schemaType} from '../../editor/__tests__/PortableTextEditorTester'
|
|
6
|
+
import {PortableTextEditor} from '../../editor/PortableTextEditor'
|
|
7
|
+
|
|
8
|
+
describe('values: normalization', () => {
|
|
9
|
+
it("accepts incoming value with blocks without a style or markDefs prop, but doesn't leave them without them when editing them", async () => {
|
|
10
|
+
const editorRef: RefObject<PortableTextEditor> = createRef()
|
|
11
|
+
const initialValue = [
|
|
12
|
+
{
|
|
13
|
+
_key: '5fc57af23597',
|
|
14
|
+
_type: 'myTestBlockType',
|
|
15
|
+
children: [
|
|
16
|
+
{
|
|
17
|
+
_key: 'be1c67c6971a',
|
|
18
|
+
_type: 'span',
|
|
19
|
+
marks: [],
|
|
20
|
+
text: 'Hello',
|
|
21
|
+
},
|
|
22
|
+
],
|
|
23
|
+
markDefs: [],
|
|
24
|
+
},
|
|
25
|
+
]
|
|
26
|
+
const onChange = jest.fn()
|
|
27
|
+
render(
|
|
28
|
+
<PortableTextEditorTester
|
|
29
|
+
onChange={onChange}
|
|
30
|
+
ref={editorRef}
|
|
31
|
+
schemaType={schemaType}
|
|
32
|
+
value={initialValue}
|
|
33
|
+
/>,
|
|
34
|
+
)
|
|
35
|
+
await waitFor(() => {
|
|
36
|
+
if (editorRef.current) {
|
|
37
|
+
PortableTextEditor.focus(editorRef.current)
|
|
38
|
+
PortableTextEditor.select(editorRef.current, {
|
|
39
|
+
focus: {path: [{_key: '5fc57af23597'}, 'children', {_key: 'be1c67c6971a'}], offset: 0},
|
|
40
|
+
anchor: {path: [{_key: '5fc57af23597'}, 'children', {_key: 'be1c67c6971a'}], offset: 5},
|
|
41
|
+
})
|
|
42
|
+
PortableTextEditor.toggleMark(editorRef.current, 'strong')
|
|
43
|
+
expect(PortableTextEditor.getValue(editorRef.current)).toEqual([
|
|
44
|
+
{
|
|
45
|
+
_key: '5fc57af23597',
|
|
46
|
+
_type: 'myTestBlockType',
|
|
47
|
+
children: [
|
|
48
|
+
{
|
|
49
|
+
_key: 'be1c67c6971a',
|
|
50
|
+
_type: 'span',
|
|
51
|
+
marks: ['strong'],
|
|
52
|
+
text: 'Hello',
|
|
53
|
+
},
|
|
54
|
+
],
|
|
55
|
+
markDefs: [],
|
|
56
|
+
style: 'normal',
|
|
57
|
+
},
|
|
58
|
+
])
|
|
59
|
+
}
|
|
60
|
+
})
|
|
61
|
+
})
|
|
62
|
+
})
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import {describe, expect, it} from '@jest/globals'
|
|
2
|
+
|
|
3
|
+
import {schemaType} from '../../editor/__tests__/PortableTextEditorTester'
|
|
4
|
+
import {getPortableTextMemberSchemaTypes} from '../getPortableTextMemberSchemaTypes'
|
|
5
|
+
import {fromSlateValue, toSlateValue} from '../values'
|
|
6
|
+
|
|
7
|
+
const schemaTypes = getPortableTextMemberSchemaTypes(schemaType)
|
|
8
|
+
|
|
9
|
+
describe('toSlateValue', () => {
|
|
10
|
+
it('checks undefined', () => {
|
|
11
|
+
const result = toSlateValue(undefined, {schemaTypes})
|
|
12
|
+
expect(result).toHaveLength(0)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
it('runs given empty array', () => {
|
|
16
|
+
const result = toSlateValue([], {schemaTypes})
|
|
17
|
+
expect(result).toHaveLength(0)
|
|
18
|
+
})
|
|
19
|
+
|
|
20
|
+
it('given type is custom with no custom properties, should include an empty text property in children and an empty value', () => {
|
|
21
|
+
const result = toSlateValue(
|
|
22
|
+
[
|
|
23
|
+
{
|
|
24
|
+
_type: 'image',
|
|
25
|
+
_key: '123',
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
{schemaTypes},
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
expect(result).toMatchObject([
|
|
32
|
+
{
|
|
33
|
+
_key: '123',
|
|
34
|
+
_type: 'image',
|
|
35
|
+
children: [
|
|
36
|
+
{
|
|
37
|
+
text: '',
|
|
38
|
+
},
|
|
39
|
+
],
|
|
40
|
+
value: {},
|
|
41
|
+
},
|
|
42
|
+
])
|
|
43
|
+
})
|
|
44
|
+
|
|
45
|
+
it('given type is block', () => {
|
|
46
|
+
const result = toSlateValue(
|
|
47
|
+
[
|
|
48
|
+
{
|
|
49
|
+
_type: schemaTypes.block.name,
|
|
50
|
+
_key: '123',
|
|
51
|
+
children: [
|
|
52
|
+
{
|
|
53
|
+
_type: 'span',
|
|
54
|
+
_key: '1231',
|
|
55
|
+
text: '123',
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
},
|
|
59
|
+
],
|
|
60
|
+
{schemaTypes},
|
|
61
|
+
)
|
|
62
|
+
expect(result).toMatchInlineSnapshot(`
|
|
63
|
+
Array [
|
|
64
|
+
Object {
|
|
65
|
+
"_key": "123",
|
|
66
|
+
"_type": "myTestBlockType",
|
|
67
|
+
"children": Array [
|
|
68
|
+
Object {
|
|
69
|
+
"_key": "1231",
|
|
70
|
+
"_type": "span",
|
|
71
|
+
"text": "123",
|
|
72
|
+
},
|
|
73
|
+
],
|
|
74
|
+
"style": "normal",
|
|
75
|
+
},
|
|
76
|
+
]
|
|
77
|
+
`)
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('given type is block and has custom object in children', () => {
|
|
81
|
+
const result = toSlateValue(
|
|
82
|
+
[
|
|
83
|
+
{
|
|
84
|
+
_type: schemaTypes.block.name,
|
|
85
|
+
_key: '123',
|
|
86
|
+
children: [
|
|
87
|
+
{
|
|
88
|
+
_type: 'span',
|
|
89
|
+
_key: '1231',
|
|
90
|
+
text: '123',
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
_type: 'image',
|
|
94
|
+
_key: '1232',
|
|
95
|
+
asset: {
|
|
96
|
+
_ref: 'ref-123',
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
],
|
|
100
|
+
},
|
|
101
|
+
],
|
|
102
|
+
{schemaTypes},
|
|
103
|
+
)
|
|
104
|
+
expect(result).toMatchInlineSnapshot(`
|
|
105
|
+
Array [
|
|
106
|
+
Object {
|
|
107
|
+
"_key": "123",
|
|
108
|
+
"_type": "myTestBlockType",
|
|
109
|
+
"children": Array [
|
|
110
|
+
Object {
|
|
111
|
+
"_key": "1231",
|
|
112
|
+
"_type": "span",
|
|
113
|
+
"text": "123",
|
|
114
|
+
},
|
|
115
|
+
Object {
|
|
116
|
+
"__inline": true,
|
|
117
|
+
"_key": "1232",
|
|
118
|
+
"_type": "image",
|
|
119
|
+
"children": Array [
|
|
120
|
+
Object {
|
|
121
|
+
"_key": "void-child",
|
|
122
|
+
"_type": "span",
|
|
123
|
+
"marks": Array [],
|
|
124
|
+
"text": "",
|
|
125
|
+
},
|
|
126
|
+
],
|
|
127
|
+
"value": Object {
|
|
128
|
+
"asset": Object {
|
|
129
|
+
"_ref": "ref-123",
|
|
130
|
+
},
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
],
|
|
134
|
+
"style": "normal",
|
|
135
|
+
},
|
|
136
|
+
]
|
|
137
|
+
`)
|
|
138
|
+
})
|
|
139
|
+
})
|
|
140
|
+
|
|
141
|
+
describe('fromSlateValue', () => {
|
|
142
|
+
it('runs given empty array', () => {
|
|
143
|
+
const result = fromSlateValue([], 'image')
|
|
144
|
+
expect(result).toHaveLength(0)
|
|
145
|
+
})
|
|
146
|
+
|
|
147
|
+
it('converts a slate value to portable text', () => {
|
|
148
|
+
const ptValue = fromSlateValue(
|
|
149
|
+
[
|
|
150
|
+
{
|
|
151
|
+
_type: 'block',
|
|
152
|
+
_key: 'dr239u3',
|
|
153
|
+
children: [
|
|
154
|
+
{
|
|
155
|
+
_type: 'span',
|
|
156
|
+
_key: '252f4swet',
|
|
157
|
+
marks: [],
|
|
158
|
+
text: 'Hey ',
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
_type: 'image',
|
|
162
|
+
_key: 'e324t4s',
|
|
163
|
+
__inline: true,
|
|
164
|
+
children: [{_key: '1', _type: 'span', text: '', marks: []}],
|
|
165
|
+
value: {
|
|
166
|
+
_type: 'image',
|
|
167
|
+
_key: 'e324t4s',
|
|
168
|
+
asset: {_ref: '32423r32rewr3rwerwer'},
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
],
|
|
172
|
+
markDefs: [],
|
|
173
|
+
style: 'normal',
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
_type: 'image',
|
|
177
|
+
_key: 'wer32434',
|
|
178
|
+
children: [{_key: '1', _type: 'span', text: '', marks: []}],
|
|
179
|
+
value: {
|
|
180
|
+
_type: 'image',
|
|
181
|
+
_key: 'wer32434',
|
|
182
|
+
asset: {_ref: 'werwer452423423'},
|
|
183
|
+
},
|
|
184
|
+
},
|
|
185
|
+
],
|
|
186
|
+
'block',
|
|
187
|
+
)
|
|
188
|
+
expect(ptValue).toEqual([
|
|
189
|
+
{
|
|
190
|
+
_type: 'block',
|
|
191
|
+
_key: 'dr239u3',
|
|
192
|
+
children: [
|
|
193
|
+
{
|
|
194
|
+
_type: 'span',
|
|
195
|
+
_key: '252f4swet',
|
|
196
|
+
marks: [],
|
|
197
|
+
text: 'Hey ',
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
_type: 'image',
|
|
201
|
+
_key: 'e324t4s',
|
|
202
|
+
asset: {_ref: '32423r32rewr3rwerwer'},
|
|
203
|
+
},
|
|
204
|
+
],
|
|
205
|
+
markDefs: [],
|
|
206
|
+
style: 'normal',
|
|
207
|
+
},
|
|
208
|
+
{
|
|
209
|
+
_type: 'image',
|
|
210
|
+
_key: 'wer32434',
|
|
211
|
+
asset: {_ref: 'werwer452423423'},
|
|
212
|
+
},
|
|
213
|
+
])
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
it('has object equality', () => {
|
|
217
|
+
const keyMap = {}
|
|
218
|
+
const value = [
|
|
219
|
+
{
|
|
220
|
+
_type: 'image',
|
|
221
|
+
_key: 'wer32434',
|
|
222
|
+
asset: {_ref: 'werwer452423423'},
|
|
223
|
+
},
|
|
224
|
+
{
|
|
225
|
+
_type: 'block',
|
|
226
|
+
_key: 'dr239u3',
|
|
227
|
+
children: [
|
|
228
|
+
{
|
|
229
|
+
_type: 'span',
|
|
230
|
+
_key: '252f4swet',
|
|
231
|
+
marks: [],
|
|
232
|
+
text: 'Hey ',
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
_type: 'image',
|
|
236
|
+
_key: 'e324t4s',
|
|
237
|
+
asset: {_ref: '32423r32rewr3rwerwer'},
|
|
238
|
+
},
|
|
239
|
+
],
|
|
240
|
+
markDefs: [],
|
|
241
|
+
style: 'normal',
|
|
242
|
+
},
|
|
243
|
+
]
|
|
244
|
+
const toSlate1 = toSlateValue(value, {schemaTypes}, keyMap)
|
|
245
|
+
const toSlate2 = toSlateValue(value, {schemaTypes}, keyMap)
|
|
246
|
+
expect(toSlate1[0]).toBe(toSlate2[0])
|
|
247
|
+
expect(toSlate1[1]).toBe(toSlate2[1])
|
|
248
|
+
const fromSlate1 = fromSlateValue(toSlate1, 'block', keyMap)
|
|
249
|
+
const fromSlate2 = fromSlateValue(toSlate2, 'block', keyMap)
|
|
250
|
+
expect(fromSlate1[0]).toBe(fromSlate2[0])
|
|
251
|
+
expect(fromSlate1[1]).toBe(fromSlate2[1])
|
|
252
|
+
})
|
|
253
|
+
})
|