@portabletext/editor 1.33.4 → 1.33.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.
Files changed (34) hide show
  1. package/lib/_chunks-cjs/behavior.core.cjs +14 -8
  2. package/lib/_chunks-cjs/behavior.core.cjs.map +1 -1
  3. package/lib/_chunks-cjs/behavior.markdown.cjs +20 -12
  4. package/lib/_chunks-cjs/behavior.markdown.cjs.map +1 -1
  5. package/lib/_chunks-cjs/plugin.event-listener.cjs +82 -68
  6. package/lib/_chunks-cjs/plugin.event-listener.cjs.map +1 -1
  7. package/lib/_chunks-cjs/util.block-offsets-to-selection.cjs +103 -46
  8. package/lib/_chunks-cjs/util.block-offsets-to-selection.cjs.map +1 -1
  9. package/lib/_chunks-es/behavior.core.js +14 -8
  10. package/lib/_chunks-es/behavior.core.js.map +1 -1
  11. package/lib/_chunks-es/behavior.markdown.js +20 -12
  12. package/lib/_chunks-es/behavior.markdown.js.map +1 -1
  13. package/lib/_chunks-es/plugin.event-listener.js +87 -72
  14. package/lib/_chunks-es/plugin.event-listener.js.map +1 -1
  15. package/lib/_chunks-es/util.block-offsets-to-selection.js +102 -46
  16. package/lib/_chunks-es/util.block-offsets-to-selection.js.map +1 -1
  17. package/lib/behaviors/index.d.cts +1 -33
  18. package/lib/behaviors/index.d.ts +1 -33
  19. package/lib/index.d.cts +162 -1496
  20. package/lib/index.d.ts +162 -1496
  21. package/lib/plugins/index.d.cts +162 -1496
  22. package/lib/plugins/index.d.ts +162 -1496
  23. package/package.json +9 -9
  24. package/src/behavior-actions/behavior.action.block.set.ts +48 -5
  25. package/src/behavior-actions/behavior.action.block.unset.ts +77 -3
  26. package/src/behavior-actions/behavior.actions.ts +1 -18
  27. package/src/behaviors/behavior.core.lists.ts +18 -14
  28. package/src/behaviors/behavior.markdown.ts +14 -12
  29. package/src/behaviors/behavior.types.ts +1 -13
  30. package/src/converters/converter.portable-text.deserialize.test.ts +3 -2
  31. package/src/internal-utils/parse-blocks.test.ts +455 -0
  32. package/src/internal-utils/parse-blocks.ts +202 -87
  33. package/src/behavior-actions/behavior.action.text-block.set.ts +0 -25
  34. package/src/behavior-actions/behavior.action.text-block.unset.ts +0 -17
@@ -0,0 +1,455 @@
1
+ import {describe, expect, test} from 'vitest'
2
+ import {compileSchemaDefinition, defineSchema} from '../editor/define-schema'
3
+ import {parseBlock, parseSpan} from './parse-blocks'
4
+ import {createTestKeyGenerator} from './test-key-generator'
5
+
6
+ describe(parseBlock.name, () => {
7
+ test('null', () => {
8
+ expect(
9
+ parseBlock({
10
+ block: null,
11
+ context: {
12
+ keyGenerator: createTestKeyGenerator(),
13
+ schema: compileSchemaDefinition(defineSchema({})),
14
+ },
15
+ options: {refreshKeys: false},
16
+ }),
17
+ ).toBe(undefined)
18
+ })
19
+
20
+ test('undefined', () => {
21
+ expect(
22
+ parseBlock({
23
+ block: undefined,
24
+ context: {
25
+ keyGenerator: createTestKeyGenerator(),
26
+ schema: compileSchemaDefinition(defineSchema({})),
27
+ },
28
+ options: {refreshKeys: false},
29
+ }),
30
+ ).toBe(undefined)
31
+ })
32
+
33
+ describe('block object', () => {
34
+ test('empty', () => {
35
+ expect(
36
+ parseBlock({
37
+ block: {},
38
+ context: {
39
+ keyGenerator: createTestKeyGenerator(),
40
+ schema: compileSchemaDefinition(defineSchema({})),
41
+ },
42
+ options: {refreshKeys: false},
43
+ }),
44
+ ).toBe(undefined)
45
+ })
46
+
47
+ test('missing _type', () => {
48
+ expect(
49
+ parseBlock({
50
+ block: {_key: 'k0'},
51
+ context: {
52
+ keyGenerator: createTestKeyGenerator(),
53
+ schema: compileSchemaDefinition(
54
+ defineSchema({blockObjects: [{name: 'image'}]}),
55
+ ),
56
+ },
57
+ options: {refreshKeys: false},
58
+ }),
59
+ ).toBe(undefined)
60
+ })
61
+
62
+ test('missing _key', () => {
63
+ expect(
64
+ parseBlock({
65
+ block: {_type: 'image'},
66
+ context: {
67
+ keyGenerator: createTestKeyGenerator(),
68
+ schema: compileSchemaDefinition(
69
+ defineSchema({blockObjects: [{name: 'image'}]}),
70
+ ),
71
+ },
72
+ options: {refreshKeys: false},
73
+ }),
74
+ ).toEqual({
75
+ _key: 'k0',
76
+ _type: 'image',
77
+ })
78
+ })
79
+ })
80
+
81
+ describe('text block', () => {
82
+ test('only _type', () => {
83
+ expect(
84
+ parseBlock({
85
+ block: {_type: 'block'},
86
+ context: {
87
+ keyGenerator: createTestKeyGenerator(),
88
+ schema: compileSchemaDefinition(defineSchema({})),
89
+ },
90
+ options: {refreshKeys: false},
91
+ }),
92
+ ).toEqual({
93
+ _key: 'k0',
94
+ _type: 'block',
95
+ children: [
96
+ {
97
+ _key: 'k1',
98
+ _type: 'span',
99
+ text: '',
100
+ marks: [],
101
+ },
102
+ ],
103
+ markDefs: [],
104
+ style: 'normal',
105
+ })
106
+ })
107
+
108
+ test('custom _type', () => {
109
+ const schema = compileSchemaDefinition(defineSchema({}))
110
+ expect(
111
+ parseBlock({
112
+ block: {_type: 'text'},
113
+ context: {
114
+ keyGenerator: createTestKeyGenerator(),
115
+ schema: {...schema, block: {...schema.block, name: 'text'}},
116
+ },
117
+ options: {refreshKeys: false},
118
+ }),
119
+ ).toEqual({
120
+ _key: 'k0',
121
+ _type: 'text',
122
+ children: [
123
+ {
124
+ _key: 'k1',
125
+ _type: 'span',
126
+ text: '',
127
+ marks: [],
128
+ },
129
+ ],
130
+ markDefs: [],
131
+ style: 'normal',
132
+ })
133
+ })
134
+
135
+ test('only children', () => {
136
+ expect(
137
+ parseBlock({
138
+ block: {
139
+ children: [
140
+ {
141
+ _key: 'k1',
142
+ _type: 'span',
143
+ text: 'foo',
144
+ marks: [],
145
+ },
146
+ ],
147
+ },
148
+ context: {
149
+ keyGenerator: createTestKeyGenerator(),
150
+ schema: compileSchemaDefinition(defineSchema({})),
151
+ },
152
+ options: {refreshKeys: false},
153
+ }),
154
+ ).toBe(undefined)
155
+ })
156
+
157
+ test('invalid children', () => {
158
+ expect(
159
+ parseBlock({
160
+ block: {
161
+ _type: 'block',
162
+ children: [
163
+ undefined,
164
+ null,
165
+ 'foo',
166
+ 42,
167
+ {foo: 'bar'},
168
+ {
169
+ _key: 'k1',
170
+ text: 'foo',
171
+ marks: [],
172
+ },
173
+ {
174
+ _type: 'stock-ticker',
175
+ },
176
+ {_type: 'span'},
177
+ {_type: 'span', text: 'foo'},
178
+ {_type: 'span', marks: ['strong']},
179
+ {_type: 'span', marks: ['em']},
180
+ ],
181
+ },
182
+ context: {
183
+ keyGenerator: createTestKeyGenerator(),
184
+ schema: compileSchemaDefinition(
185
+ defineSchema({
186
+ inlineObjects: [{name: 'stock-ticker'}],
187
+ decorators: [{name: 'em'}],
188
+ }),
189
+ ),
190
+ },
191
+ options: {refreshKeys: false},
192
+ }),
193
+ ).toEqual({
194
+ _key: 'k0',
195
+ _type: 'block',
196
+ children: [
197
+ {
198
+ _key: 'k1',
199
+ _type: 'stock-ticker',
200
+ },
201
+ {
202
+ _key: 'k2',
203
+ _type: 'span',
204
+ text: '',
205
+ marks: [],
206
+ },
207
+ {
208
+ _key: 'k3',
209
+ _type: 'span',
210
+ text: 'foo',
211
+ marks: [],
212
+ },
213
+ {
214
+ _key: 'k4',
215
+ _type: 'span',
216
+ text: '',
217
+ marks: [],
218
+ },
219
+ {
220
+ _key: 'k5',
221
+ _type: 'span',
222
+ text: '',
223
+ marks: ['em'],
224
+ },
225
+ ],
226
+ markDefs: [],
227
+ style: 'normal',
228
+ })
229
+ })
230
+
231
+ test('only (known) listItem', () => {
232
+ expect(
233
+ parseBlock({
234
+ block: {_type: 'block', listItem: 'bullet'},
235
+ context: {
236
+ keyGenerator: createTestKeyGenerator(),
237
+ schema: compileSchemaDefinition(
238
+ defineSchema({lists: [{name: 'bullet'}]}),
239
+ ),
240
+ },
241
+ options: {refreshKeys: false},
242
+ }),
243
+ ).toEqual({
244
+ _key: 'k0',
245
+ _type: 'block',
246
+ children: [
247
+ {
248
+ _key: 'k1',
249
+ _type: 'span',
250
+ text: '',
251
+ marks: [],
252
+ },
253
+ ],
254
+ markDefs: [],
255
+ listItem: 'bullet',
256
+ style: 'normal',
257
+ })
258
+ })
259
+
260
+ test('only (unknown) listItem', () => {
261
+ expect(
262
+ parseBlock({
263
+ block: {_type: 'block', listItem: 'number'},
264
+ context: {
265
+ keyGenerator: createTestKeyGenerator(),
266
+ schema: compileSchemaDefinition(
267
+ defineSchema({lists: [{name: 'bullet'}]}),
268
+ ),
269
+ },
270
+ options: {refreshKeys: false},
271
+ }),
272
+ ).toEqual({
273
+ _key: 'k0',
274
+ _type: 'block',
275
+ children: [
276
+ {
277
+ _key: 'k1',
278
+ _type: 'span',
279
+ text: '',
280
+ marks: [],
281
+ },
282
+ ],
283
+ markDefs: [],
284
+ style: 'normal',
285
+ })
286
+ })
287
+ })
288
+ })
289
+
290
+ describe(parseSpan.name, () => {
291
+ test('undefined', () => {
292
+ expect(
293
+ parseSpan({
294
+ span: undefined,
295
+ context: {
296
+ keyGenerator: createTestKeyGenerator(),
297
+ schema: compileSchemaDefinition(defineSchema({})),
298
+ },
299
+ markDefKeyMap: new Map(),
300
+ options: {refreshKeys: false},
301
+ }),
302
+ ).toBe(undefined)
303
+ })
304
+
305
+ test('null', () => {
306
+ expect(
307
+ parseSpan({
308
+ span: null,
309
+ context: {
310
+ keyGenerator: createTestKeyGenerator(),
311
+ schema: compileSchemaDefinition(defineSchema({})),
312
+ },
313
+ markDefKeyMap: new Map(),
314
+ options: {refreshKeys: false},
315
+ }),
316
+ ).toBe(undefined)
317
+ })
318
+
319
+ test('empty object', () => {
320
+ expect(
321
+ parseSpan({
322
+ span: {},
323
+ context: {
324
+ keyGenerator: createTestKeyGenerator(),
325
+ schema: compileSchemaDefinition(defineSchema({})),
326
+ },
327
+ markDefKeyMap: new Map(),
328
+ options: {refreshKeys: false},
329
+ }),
330
+ ).toBe(undefined)
331
+ })
332
+
333
+ test('invalid _type', () => {
334
+ expect(
335
+ parseSpan({
336
+ span: {_type: 'stock-ticker'},
337
+ context: {
338
+ keyGenerator: createTestKeyGenerator(),
339
+ schema: compileSchemaDefinition(defineSchema({})),
340
+ },
341
+ markDefKeyMap: new Map(),
342
+ options: {refreshKeys: false},
343
+ }),
344
+ ).toBe(undefined)
345
+ })
346
+
347
+ test('only _type', () => {
348
+ expect(
349
+ parseSpan({
350
+ span: {_type: 'span'},
351
+ context: {
352
+ keyGenerator: createTestKeyGenerator(),
353
+ schema: compileSchemaDefinition(defineSchema({})),
354
+ },
355
+ markDefKeyMap: new Map(),
356
+ options: {refreshKeys: false},
357
+ }),
358
+ ).toEqual({
359
+ _key: 'k0',
360
+ _type: 'span',
361
+ text: '',
362
+ marks: [],
363
+ })
364
+ })
365
+
366
+ test('custom props', () => {
367
+ expect(
368
+ parseSpan({
369
+ span: {_type: 'span', foo: 'bar'},
370
+ context: {
371
+ keyGenerator: createTestKeyGenerator(),
372
+ schema: compileSchemaDefinition(defineSchema({})),
373
+ },
374
+ markDefKeyMap: new Map(),
375
+ options: {refreshKeys: false},
376
+ }),
377
+ ).toEqual({
378
+ _key: 'k0',
379
+ _type: 'span',
380
+ text: '',
381
+ marks: [],
382
+ foo: 'bar',
383
+ })
384
+ })
385
+
386
+ test('invalid marks array', () => {
387
+ expect(
388
+ parseSpan({
389
+ span: {
390
+ _type: 'span',
391
+ marks: 42,
392
+ },
393
+ context: {
394
+ keyGenerator: createTestKeyGenerator(),
395
+ schema: compileSchemaDefinition(defineSchema({})),
396
+ },
397
+ markDefKeyMap: new Map(),
398
+ options: {refreshKeys: false},
399
+ }),
400
+ ).toEqual({
401
+ _key: 'k0',
402
+ _type: 'span',
403
+ text: '',
404
+ marks: [],
405
+ })
406
+ })
407
+
408
+ test('invalid marks', () => {
409
+ expect(
410
+ parseSpan({
411
+ span: {
412
+ _type: 'span',
413
+ marks: [null, undefined, 'foo', 42, {foo: 'bar'}, 'strong'],
414
+ },
415
+ context: {
416
+ keyGenerator: createTestKeyGenerator(),
417
+ schema: compileSchemaDefinition(
418
+ defineSchema({decorators: [{name: 'strong'}]}),
419
+ ),
420
+ },
421
+ markDefKeyMap: new Map(),
422
+ options: {refreshKeys: false},
423
+ }),
424
+ ).toEqual({
425
+ _key: 'k0',
426
+ _type: 'span',
427
+ text: '',
428
+ marks: ['strong'],
429
+ })
430
+ })
431
+
432
+ test('unknown decorator', () => {
433
+ expect(
434
+ parseSpan({
435
+ span: {
436
+ _type: 'span',
437
+ marks: ['strong', 'em'],
438
+ },
439
+ context: {
440
+ keyGenerator: createTestKeyGenerator(),
441
+ schema: compileSchemaDefinition(
442
+ defineSchema({decorators: [{name: 'strong'}]}),
443
+ ),
444
+ },
445
+ markDefKeyMap: new Map(),
446
+ options: {refreshKeys: false},
447
+ }),
448
+ ).toEqual({
449
+ _key: 'k0',
450
+ _type: 'span',
451
+ text: '',
452
+ marks: ['strong'],
453
+ })
454
+ })
455
+ })