@portabletext/editor 2.7.2 → 2.8.1

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 (72) hide show
  1. package/lib/_chunks-cjs/selector.is-selecting-entire-blocks.cjs +3 -1
  2. package/lib/_chunks-cjs/selector.is-selecting-entire-blocks.cjs.map +1 -1
  3. package/lib/_chunks-cjs/selector.is-selection-expanded.cjs +10 -4
  4. package/lib/_chunks-cjs/selector.is-selection-expanded.cjs.map +1 -1
  5. package/lib/_chunks-cjs/util.merge-text-blocks.cjs +0 -1
  6. package/lib/_chunks-cjs/util.merge-text-blocks.cjs.map +1 -1
  7. package/lib/_chunks-cjs/util.slice-blocks.cjs +60 -9
  8. package/lib/_chunks-cjs/util.slice-blocks.cjs.map +1 -1
  9. package/lib/_chunks-dts/behavior.types.action.d.cts +149 -149
  10. package/lib/_chunks-dts/behavior.types.action.d.ts +86 -86
  11. package/lib/_chunks-es/selector.is-selecting-entire-blocks.js +4 -2
  12. package/lib/_chunks-es/selector.is-selecting-entire-blocks.js.map +1 -1
  13. package/lib/_chunks-es/selector.is-selection-expanded.js +10 -4
  14. package/lib/_chunks-es/selector.is-selection-expanded.js.map +1 -1
  15. package/lib/_chunks-es/util.merge-text-blocks.js +0 -1
  16. package/lib/_chunks-es/util.merge-text-blocks.js.map +1 -1
  17. package/lib/_chunks-es/util.slice-blocks.js +56 -8
  18. package/lib/_chunks-es/util.slice-blocks.js.map +1 -1
  19. package/lib/index.cjs +94 -131
  20. package/lib/index.cjs.map +1 -1
  21. package/lib/index.js +90 -128
  22. package/lib/index.js.map +1 -1
  23. package/lib/plugins/index.d.cts +3 -3
  24. package/lib/selectors/index.d.cts +13 -3
  25. package/lib/selectors/index.d.ts +13 -3
  26. package/package.json +13 -14
  27. package/src/behaviors/behavior.abstract.insert.ts +58 -1
  28. package/src/behaviors/behavior.abstract.split.ts +0 -1
  29. package/src/behaviors/behavior.core.annotations.ts +24 -2
  30. package/src/behaviors/behavior.core.ts +1 -1
  31. package/src/behaviors/behavior.types.event.ts +18 -18
  32. package/src/converters/converter.portable-text.ts +0 -1
  33. package/src/converters/converter.text-html.serialize.test.ts +27 -17
  34. package/src/converters/converter.text-html.ts +0 -1
  35. package/src/converters/converter.text-plain.test.ts +1 -1
  36. package/src/converters/converter.text-plain.ts +0 -1
  37. package/src/editor/Editable.tsx +0 -1
  38. package/src/editor/plugins/createWithEditableAPI.ts +16 -0
  39. package/src/internal-utils/parse-blocks.test.ts +23 -23
  40. package/src/internal-utils/parse-blocks.ts +13 -24
  41. package/src/internal-utils/test-editor.tsx +15 -21
  42. package/src/operations/behavior.operation.annotation.add.ts +2 -13
  43. package/src/operations/behavior.operation.block.set.ts +1 -1
  44. package/src/operations/behavior.operation.block.unset.ts +2 -2
  45. package/src/operations/behavior.operation.insert.block.ts +1 -1
  46. package/src/operations/behavior.operations.ts +0 -18
  47. package/src/plugins/plugin.internal.auto-close-brackets.test.tsx +25 -71
  48. package/src/plugins/plugin.markdown.test.tsx +12 -30
  49. package/src/selectors/selector.get-selected-value.test.ts +748 -0
  50. package/src/selectors/selector.get-selected-value.ts +28 -7
  51. package/src/selectors/selector.get-trimmed-selection.test.ts +0 -1
  52. package/src/selectors/selector.is-active-annotation.test.ts +320 -0
  53. package/src/selectors/selector.is-active-annotation.ts +24 -0
  54. package/src/utils/util.merge-text-blocks.ts +1 -1
  55. package/src/utils/util.slice-blocks.ts +36 -3
  56. package/src/editor/__tests__/PortableTextEditor.test.tsx +0 -430
  57. package/src/editor/__tests__/PortableTextEditorTester.tsx +0 -58
  58. package/src/editor/__tests__/RangeDecorations.test.tsx +0 -213
  59. package/src/editor/__tests__/insert-block.test.tsx +0 -224
  60. package/src/editor/__tests__/self-solving.test.tsx +0 -183
  61. package/src/editor/plugins/__tests__/withEditableAPIDelete.test.tsx +0 -298
  62. package/src/editor/plugins/__tests__/withEditableAPIGetFragment.test.tsx +0 -177
  63. package/src/editor/plugins/__tests__/withEditableAPIInsert.test.tsx +0 -538
  64. package/src/editor/plugins/__tests__/withEditableAPISelectionsOverlapping.test.tsx +0 -162
  65. package/src/editor/plugins/__tests__/withPortableTextLists.test.tsx +0 -65
  66. package/src/editor/plugins/__tests__/withPortableTextMarkModel.test.tsx +0 -612
  67. package/src/editor/plugins/__tests__/withPortableTextSelections.test.tsx +0 -103
  68. package/src/editor/plugins/__tests__/withUndoRedo.test.tsx +0 -147
  69. package/src/internal-utils/__tests__/valueNormalization.test.tsx +0 -79
  70. package/src/operations/behavior.operation.insert-inline-object.ts +0 -59
  71. package/src/operations/behavior.operation.insert-span.ts +0 -48
  72. package/src/utils/util.slice-blocks.test.ts +0 -465
@@ -1,465 +0,0 @@
1
- import {compileSchema, defineSchema} from '@portabletext/schema'
2
- import type {PortableTextBlock, PortableTextTextBlock} from '@sanity/types'
3
- import {describe, expect, test} from 'vitest'
4
- import {sliceBlocks} from './util.slice-blocks'
5
-
6
- const b1: PortableTextTextBlock = {
7
- _type: 'block',
8
- _key: 'b1',
9
- children: [
10
- {
11
- _type: 'span',
12
- _key: 'b1c1',
13
- text: 'foo',
14
- },
15
- {
16
- _type: 'span',
17
- _key: 'b1c2',
18
- text: 'bar',
19
- },
20
- ],
21
- }
22
- const b2: PortableTextBlock = {
23
- _type: 'image',
24
- _key: 'b2',
25
- src: 'https://example.com/image.jpg',
26
- alt: 'Example',
27
- }
28
- const b3: PortableTextTextBlock = {
29
- _type: 'block',
30
- _key: 'b3',
31
- children: [
32
- {
33
- _type: 'span',
34
- _key: 'b3c1',
35
- text: 'baz',
36
- },
37
- ],
38
- }
39
- const b4: PortableTextTextBlock = {
40
- _type: 'block',
41
- _key: 'b4',
42
- children: [
43
- {
44
- _type: 'span',
45
- _key: 'b4c1',
46
- text: 'fizz',
47
- },
48
- {
49
- _type: 'stock-ticker',
50
- _key: 'b4c2',
51
- symbol: 'AAPL',
52
- },
53
- {
54
- _type: 'span',
55
- _key: 'b4c3',
56
- text: 'buzz',
57
- },
58
- ],
59
- }
60
-
61
- const schema = compileSchema(defineSchema({}))
62
- const blocks: Array<PortableTextBlock> = [b1, b2, b3, b4]
63
-
64
- describe(sliceBlocks.name, () => {
65
- test('sensible defaults', () => {
66
- expect(
67
- sliceBlocks({
68
- context: {
69
- schema,
70
- selection: null,
71
- },
72
- blocks: [],
73
- }),
74
- ).toEqual([])
75
- expect(
76
- sliceBlocks({
77
- context: {
78
- schema,
79
- selection: null,
80
- },
81
- blocks,
82
- }),
83
- ).toEqual([])
84
- })
85
-
86
- test('slicing a single block', () => {
87
- expect(
88
- sliceBlocks({
89
- context: {
90
- schema,
91
- selection: {
92
- anchor: {
93
- path: [{_key: b1._key}, 'children', {_key: b1.children[0]._key}],
94
- offset: 0,
95
- },
96
- focus: {
97
- path: [{_key: b1._key}, 'children', {_key: b1.children[0]._key}],
98
- offset: 3,
99
- },
100
- },
101
- },
102
- blocks,
103
- }),
104
- ).toEqual([
105
- {
106
- ...b1,
107
- children: [b1.children[0]],
108
- },
109
- ])
110
- })
111
-
112
- test('slicing a single span', () => {
113
- expect(
114
- sliceBlocks({
115
- context: {
116
- schema,
117
- selection: {
118
- anchor: {
119
- path: [{_key: b1._key}, 'children', {_key: b1.children[0]._key}],
120
- offset: 1,
121
- },
122
- focus: {
123
- path: [{_key: b1._key}, 'children', {_key: b1.children[0]._key}],
124
- offset: 2,
125
- },
126
- },
127
- },
128
- blocks,
129
- }),
130
- ).toEqual([
131
- {
132
- ...b1,
133
- children: [
134
- {
135
- ...b1.children[0],
136
- text: 'o',
137
- },
138
- ],
139
- },
140
- ])
141
- })
142
-
143
- test('starting and ending selection on a block object', () => {
144
- expect(
145
- sliceBlocks({
146
- context: {
147
- schema,
148
- selection: {
149
- anchor: {
150
- path: [{_key: b2._key}],
151
- offset: 0,
152
- },
153
- focus: {
154
- path: [{_key: b2._key}],
155
- offset: 0,
156
- },
157
- },
158
- },
159
- blocks,
160
- }),
161
- ).toEqual([b2])
162
- })
163
-
164
- test('starting selection on a block object', () => {
165
- expect(
166
- sliceBlocks({
167
- context: {
168
- schema,
169
- selection: {
170
- anchor: {
171
- path: [{_key: b2._key}],
172
- offset: 0,
173
- },
174
- focus: {
175
- path: [{_key: b3._key}, 'children', {_key: b3.children[0]._key}],
176
- offset: 3,
177
- },
178
- },
179
- },
180
- blocks,
181
- }),
182
- ).toEqual([b2, b3])
183
- })
184
-
185
- test('ending selection on a block object', () => {
186
- expect(
187
- sliceBlocks({
188
- context: {
189
- schema,
190
- selection: {
191
- anchor: {
192
- path: [{_key: b1._key}, 'children', {_key: b1.children[0]._key}],
193
- offset: 3,
194
- },
195
- focus: {
196
- path: [{_key: b2._key}],
197
- offset: 0,
198
- },
199
- },
200
- },
201
- blocks,
202
- }),
203
- ).toEqual([
204
- {
205
- ...b1,
206
- children: [
207
- {
208
- ...b1.children[0],
209
- text: '',
210
- },
211
- ...b1.children.slice(1),
212
- ],
213
- },
214
- blocks[1],
215
- ])
216
- })
217
-
218
- test('slicing across block object', () => {
219
- expect(
220
- sliceBlocks({
221
- context: {
222
- schema,
223
- selection: {
224
- anchor: {
225
- path: [{_key: b1._key}, 'children', {_key: b1.children[0]._key}],
226
- offset: 0,
227
- },
228
- focus: {
229
- path: [{_key: b3._key}, 'children', {_key: b3.children[0]._key}],
230
- offset: 3,
231
- },
232
- },
233
- },
234
- blocks,
235
- }),
236
- ).toEqual([b1, b2, b3])
237
- })
238
-
239
- test('starting and ending mid-span', () => {
240
- expect(
241
- sliceBlocks({
242
- context: {
243
- schema,
244
- selection: {
245
- anchor: {
246
- path: [{_key: b3._key}, 'children', {_key: b3.children[0]._key}],
247
- offset: 2,
248
- },
249
- focus: {
250
- path: [{_key: b4._key}, 'children', {_key: b4.children[0]._key}],
251
- offset: 1,
252
- },
253
- },
254
- },
255
- blocks,
256
- }),
257
- ).toEqual([
258
- {
259
- ...b3,
260
- children: [
261
- {
262
- ...b3.children[0],
263
- text: 'z',
264
- },
265
- ],
266
- },
267
- {
268
- ...b4,
269
- children: [
270
- {
271
- ...b4.children[0],
272
- text: 'f',
273
- },
274
- ],
275
- },
276
- ])
277
- })
278
-
279
- test('starting mid-span and ending end-span', () => {
280
- expect(
281
- sliceBlocks({
282
- context: {
283
- schema,
284
- selection: {
285
- anchor: {
286
- path: [{_key: b3._key}, 'children', {_key: b3.children[0]._key}],
287
- offset: 2,
288
- },
289
- focus: {
290
- path: [{_key: b4._key}, 'children', {_key: b4.children[0]._key}],
291
- offset: 4,
292
- },
293
- },
294
- },
295
- blocks,
296
- }),
297
- ).toEqual([
298
- {
299
- ...b3,
300
- children: [
301
- {
302
- ...b3.children[0],
303
- text: 'z',
304
- },
305
- ],
306
- },
307
- {
308
- ...b4,
309
- children: [
310
- {
311
- ...b4.children[0],
312
- },
313
- ],
314
- },
315
- ])
316
- })
317
-
318
- test('starting on inline object', () => {
319
- expect(
320
- sliceBlocks({
321
- context: {
322
- schema,
323
- selection: {
324
- anchor: {
325
- path: [{_key: b4._key}, 'children', {_key: b4.children[1]._key}],
326
- offset: 0,
327
- },
328
- focus: {
329
- path: [{_key: b4._key}, 'children', {_key: b4.children[2]._key}],
330
- offset: 4,
331
- },
332
- },
333
- },
334
- blocks,
335
- }),
336
- ).toEqual([
337
- {
338
- ...b4,
339
- children: [b4.children[1], b4.children[2]],
340
- },
341
- ])
342
- })
343
-
344
- test('ending on inline object', () => {
345
- expect(
346
- sliceBlocks({
347
- context: {
348
- schema,
349
- selection: {
350
- anchor: {
351
- path: [{_key: b4._key}, 'children', {_key: b4.children[0]._key}],
352
- offset: 0,
353
- },
354
- focus: {
355
- path: [{_key: b4._key}, 'children', {_key: b4.children[1]._key}],
356
- offset: 0,
357
- },
358
- },
359
- },
360
- blocks,
361
- }),
362
- ).toEqual([
363
- {
364
- ...b4,
365
- children: [b4.children[0], b4.children[1]],
366
- },
367
- ])
368
- })
369
-
370
- test('starting and ending on inline object', () => {
371
- expect(
372
- sliceBlocks({
373
- context: {
374
- schema,
375
- selection: {
376
- anchor: {
377
- path: [{_key: b4._key}, 'children', {_key: b4.children[1]._key}],
378
- offset: 0,
379
- },
380
- focus: {
381
- path: [{_key: b4._key}, 'children', {_key: b4.children[1]._key}],
382
- offset: 0,
383
- },
384
- },
385
- },
386
- blocks,
387
- }),
388
- ).toEqual([
389
- {
390
- ...b4,
391
- children: [b4.children[1]],
392
- },
393
- ])
394
- })
395
-
396
- test('slicing text block with custom props', () => {
397
- expect(
398
- sliceBlocks({
399
- context: {
400
- schema,
401
- selection: {
402
- anchor: {
403
- path: [{_key: 'b0'}, 'children', {_key: 's0'}],
404
- offset: 7,
405
- },
406
- focus: {
407
- path: [{_key: 'b0'}, 'children', {_key: 's0'}],
408
- offset: 12,
409
- },
410
- },
411
- },
412
- blocks: [
413
- {
414
- _key: 'b0',
415
- _type: 'block',
416
- children: [{_key: 's0', _type: 'span', text: 'Hello, world!'}],
417
- _map: {},
418
- },
419
- ],
420
- }),
421
- ).toEqual([
422
- {
423
- _key: 'b0',
424
- _type: 'block',
425
- children: [{_key: 's0', _type: 'span', text: 'world'}],
426
- _map: {},
427
- },
428
- ])
429
- })
430
-
431
- test('slicing span with custom props', () => {
432
- expect(
433
- sliceBlocks({
434
- context: {
435
- schema,
436
- selection: {
437
- anchor: {
438
- path: [{_key: 'b0'}, 'children', {_key: 's0'}],
439
- offset: 7,
440
- },
441
- focus: {
442
- path: [{_key: 'b0'}, 'children', {_key: 's0'}],
443
- offset: 12,
444
- },
445
- },
446
- },
447
- blocks: [
448
- {
449
- _key: 'b0',
450
- _type: 'block',
451
- children: [
452
- {_key: 's0', _type: 'span', text: 'Hello, world!', _map: {}},
453
- ],
454
- },
455
- ],
456
- }),
457
- ).toEqual([
458
- {
459
- _key: 'b0',
460
- _type: 'block',
461
- children: [{_key: 's0', _type: 'span', text: 'world', _map: {}}],
462
- },
463
- ])
464
- })
465
- })