@portabletext/editor 1.0.19 → 1.1.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.
- package/lib/index.d.mts +142 -67
- package/lib/index.d.ts +142 -67
- package/lib/index.esm.js +1130 -371
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +1130 -371
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +1130 -371
- package/lib/index.mjs.map +1 -1
- package/package.json +4 -18
- package/src/editor/Editable.tsx +128 -55
- package/src/editor/PortableTextEditor.tsx +66 -32
- package/src/editor/__tests__/PortableTextEditor.test.tsx +44 -18
- package/src/editor/__tests__/PortableTextEditorTester.tsx +50 -38
- package/src/editor/__tests__/RangeDecorations.test.tsx +4 -6
- package/src/editor/__tests__/handleClick.test.tsx +28 -9
- package/src/editor/__tests__/insert-block.test.tsx +24 -8
- package/src/editor/__tests__/pteWarningsSelfSolving.test.tsx +31 -63
- package/src/editor/__tests__/utils.ts +10 -4
- package/src/editor/components/DraggableBlock.tsx +36 -13
- package/src/editor/components/Element.tsx +73 -33
- package/src/editor/components/Leaf.tsx +114 -76
- package/src/editor/components/SlateContainer.tsx +14 -7
- package/src/editor/components/Synchronizer.tsx +8 -5
- package/src/editor/hooks/usePortableTextEditor.ts +3 -3
- package/src/editor/hooks/usePortableTextEditorSelection.tsx +10 -4
- package/src/editor/hooks/useSyncValue.test.tsx +9 -4
- package/src/editor/hooks/useSyncValue.ts +198 -133
- package/src/editor/nodes/DefaultAnnotation.tsx +6 -4
- package/src/editor/nodes/DefaultObject.tsx +1 -1
- package/src/editor/plugins/__tests__/createWithInsertData.test.tsx +23 -8
- package/src/editor/plugins/__tests__/withEditableAPIDelete.test.tsx +26 -9
- package/src/editor/plugins/__tests__/withEditableAPIGetFragment.test.tsx +15 -5
- package/src/editor/plugins/__tests__/withEditableAPIInsert.test.tsx +60 -19
- package/src/editor/plugins/__tests__/withEditableAPISelectionsOverlapping.test.tsx +5 -3
- package/src/editor/plugins/__tests__/withPortableTextLists.test.tsx +4 -2
- package/src/editor/plugins/__tests__/withPortableTextMarkModel.test.tsx +61 -19
- package/src/editor/plugins/__tests__/withPortableTextSelections.test.tsx +6 -3
- package/src/editor/plugins/__tests__/withUndoRedo.test.tsx +30 -13
- package/src/editor/plugins/createWithEditableAPI.ts +361 -131
- package/src/editor/plugins/createWithHotKeys.ts +46 -130
- package/src/editor/plugins/createWithInsertBreak.ts +167 -28
- package/src/editor/plugins/createWithInsertData.ts +66 -30
- package/src/editor/plugins/createWithMaxBlocks.ts +6 -3
- package/src/editor/plugins/createWithObjectKeys.ts +7 -3
- package/src/editor/plugins/createWithPatches.ts +66 -24
- package/src/editor/plugins/createWithPlaceholderBlock.ts +9 -5
- package/src/editor/plugins/createWithPortableTextBlockStyle.ts +17 -7
- package/src/editor/plugins/createWithPortableTextLists.ts +21 -9
- package/src/editor/plugins/createWithPortableTextMarkModel.ts +217 -52
- package/src/editor/plugins/createWithPortableTextSelections.ts +11 -9
- package/src/editor/plugins/createWithSchemaTypes.ts +26 -10
- package/src/editor/plugins/createWithUndoRedo.ts +106 -27
- package/src/editor/plugins/createWithUtils.ts +33 -11
- package/src/editor/plugins/index.ts +34 -13
- package/src/types/editor.ts +73 -44
- package/src/types/options.ts +7 -5
- package/src/types/slate.ts +6 -6
- package/src/utils/__tests__/dmpToOperations.test.ts +41 -16
- package/src/utils/__tests__/operationToPatches.test.ts +4 -3
- package/src/utils/__tests__/patchToOperations.test.ts +16 -5
- package/src/utils/__tests__/ranges.test.ts +9 -4
- package/src/utils/__tests__/valueNormalization.test.tsx +12 -4
- package/src/utils/__tests__/values.test.ts +0 -1
- package/src/utils/applyPatch.ts +78 -29
- package/src/utils/getPortableTextMemberSchemaTypes.ts +38 -23
- package/src/utils/operationToPatches.ts +123 -44
- package/src/utils/paths.ts +26 -9
- package/src/utils/ranges.ts +16 -10
- package/src/utils/selection.ts +21 -9
- package/src/utils/ucs2Indices.ts +2 -2
- package/src/utils/validateValue.ts +118 -45
- package/src/utils/values.ts +38 -17
- package/src/utils/weakMaps.ts +20 -10
- package/src/utils/withChanges.ts +5 -3
- package/src/utils/withUndoRedo.ts +1 -1
- package/src/utils/withoutPatching.ts +1 -1
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import {describe, expect, it, jest} from '@jest/globals'
|
|
2
2
|
import {render, waitFor} from '@testing-library/react'
|
|
3
3
|
import {createRef, type RefObject} from 'react'
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
import {
|
|
5
|
+
PortableTextEditorTester,
|
|
6
|
+
schemaType,
|
|
7
|
+
} from '../../__tests__/PortableTextEditorTester'
|
|
6
8
|
import {PortableTextEditor} from '../../PortableTextEditor'
|
|
7
9
|
|
|
8
10
|
const initialValue = [
|
|
@@ -43,8 +45,14 @@ const emptyTextBlock = [
|
|
|
43
45
|
},
|
|
44
46
|
]
|
|
45
47
|
const emptyBlockSelection = {
|
|
46
|
-
focus: {
|
|
47
|
-
|
|
48
|
+
focus: {
|
|
49
|
+
path: [{_key: 'emptyBlock'}, 'children', {_key: 'emptySpan'}],
|
|
50
|
+
offset: 0,
|
|
51
|
+
},
|
|
52
|
+
anchor: {
|
|
53
|
+
path: [{_key: 'emptyBlock'}, 'children', {_key: 'emptySpan'}],
|
|
54
|
+
offset: 0,
|
|
55
|
+
},
|
|
48
56
|
}
|
|
49
57
|
|
|
50
58
|
describe('plugin:withEditableAPI: .insertChild()', () => {
|
|
@@ -63,7 +71,10 @@ describe('plugin:withEditableAPI: .insertChild()', () => {
|
|
|
63
71
|
|
|
64
72
|
await waitFor(() => {
|
|
65
73
|
if (editorRef.current) {
|
|
66
|
-
expect(onChange).toHaveBeenCalledWith({
|
|
74
|
+
expect(onChange).toHaveBeenCalledWith({
|
|
75
|
+
type: 'value',
|
|
76
|
+
value: initialValue,
|
|
77
|
+
})
|
|
67
78
|
expect(onChange).toHaveBeenCalledWith({type: 'ready'})
|
|
68
79
|
}
|
|
69
80
|
})
|
|
@@ -80,7 +91,9 @@ describe('plugin:withEditableAPI: .insertChild()', () => {
|
|
|
80
91
|
const inlineType = editorRef.current.schemaTypes.inlineObjects.find(
|
|
81
92
|
(t) => t.name === 'someObject',
|
|
82
93
|
)!
|
|
83
|
-
PortableTextEditor.insertChild(editorRef.current, inlineType, {
|
|
94
|
+
PortableTextEditor.insertChild(editorRef.current, inlineType, {
|
|
95
|
+
color: 'red',
|
|
96
|
+
})
|
|
84
97
|
}
|
|
85
98
|
})
|
|
86
99
|
|
|
@@ -118,9 +131,13 @@ describe('plugin:withEditableAPI: .insertChild()', () => {
|
|
|
118
131
|
|
|
119
132
|
await waitFor(() => {
|
|
120
133
|
if (editorRef.current) {
|
|
121
|
-
PortableTextEditor.insertChild(
|
|
122
|
-
|
|
123
|
-
|
|
134
|
+
PortableTextEditor.insertChild(
|
|
135
|
+
editorRef.current,
|
|
136
|
+
editorRef.current.schemaTypes.span,
|
|
137
|
+
{
|
|
138
|
+
text: ' ',
|
|
139
|
+
},
|
|
140
|
+
)
|
|
124
141
|
}
|
|
125
142
|
})
|
|
126
143
|
|
|
@@ -214,7 +231,9 @@ describe('plugin:withEditableAPI: .insertBlock()', () => {
|
|
|
214
231
|
(t) => t.name === 'someObject',
|
|
215
232
|
)!
|
|
216
233
|
|
|
217
|
-
PortableTextEditor.insertBlock(editorRef.current, someObject, {
|
|
234
|
+
PortableTextEditor.insertBlock(editorRef.current, someObject, {
|
|
235
|
+
color: 'red',
|
|
236
|
+
})
|
|
218
237
|
}
|
|
219
238
|
})
|
|
220
239
|
|
|
@@ -242,7 +261,10 @@ describe('plugin:withEditableAPI: .insertBlock()', () => {
|
|
|
242
261
|
|
|
243
262
|
await waitFor(() => {
|
|
244
263
|
if (editorRef.current) {
|
|
245
|
-
expect(onChange).toHaveBeenCalledWith({
|
|
264
|
+
expect(onChange).toHaveBeenCalledWith({
|
|
265
|
+
type: 'value',
|
|
266
|
+
value: initialValue,
|
|
267
|
+
})
|
|
246
268
|
expect(onChange).toHaveBeenCalledWith({type: 'ready'})
|
|
247
269
|
}
|
|
248
270
|
})
|
|
@@ -260,7 +282,9 @@ describe('plugin:withEditableAPI: .insertBlock()', () => {
|
|
|
260
282
|
(t) => t.name === 'someObject',
|
|
261
283
|
)!
|
|
262
284
|
|
|
263
|
-
PortableTextEditor.insertBlock(editorRef.current, someObject, {
|
|
285
|
+
PortableTextEditor.insertBlock(editorRef.current, someObject, {
|
|
286
|
+
color: 'red',
|
|
287
|
+
})
|
|
264
288
|
}
|
|
265
289
|
})
|
|
266
290
|
|
|
@@ -289,7 +313,10 @@ describe('plugin:withEditableAPI: .insertBlock()', () => {
|
|
|
289
313
|
|
|
290
314
|
await waitFor(() => {
|
|
291
315
|
if (editorRef.current) {
|
|
292
|
-
expect(onChange).toHaveBeenCalledWith({
|
|
316
|
+
expect(onChange).toHaveBeenCalledWith({
|
|
317
|
+
type: 'value',
|
|
318
|
+
value: initialValue,
|
|
319
|
+
})
|
|
293
320
|
expect(onChange).toHaveBeenCalledWith({type: 'ready'})
|
|
294
321
|
}
|
|
295
322
|
})
|
|
@@ -309,7 +336,9 @@ describe('plugin:withEditableAPI: .insertBlock()', () => {
|
|
|
309
336
|
const someObject = editorRef.current.schemaTypes.inlineObjects.find(
|
|
310
337
|
(t) => t.name === 'someObject',
|
|
311
338
|
)!
|
|
312
|
-
PortableTextEditor.insertBlock(editorRef.current, someObject, {
|
|
339
|
+
PortableTextEditor.insertBlock(editorRef.current, someObject, {
|
|
340
|
+
color: 'red',
|
|
341
|
+
})
|
|
313
342
|
}
|
|
314
343
|
})
|
|
315
344
|
|
|
@@ -325,7 +354,10 @@ describe('plugin:withEditableAPI: .insertBlock()', () => {
|
|
|
325
354
|
|
|
326
355
|
it('should not add empty blank blocks: non text block', async () => {
|
|
327
356
|
const editorRef: RefObject<PortableTextEditor> = createRef()
|
|
328
|
-
const value = [
|
|
357
|
+
const value = [
|
|
358
|
+
...initialValue,
|
|
359
|
+
{_key: 'b', _type: 'someObject', color: 'red'},
|
|
360
|
+
]
|
|
329
361
|
const onChange = jest.fn()
|
|
330
362
|
|
|
331
363
|
render(
|
|
@@ -360,7 +392,9 @@ describe('plugin:withEditableAPI: .insertBlock()', () => {
|
|
|
360
392
|
const someObject = editorRef.current.schemaTypes.inlineObjects.find(
|
|
361
393
|
(t) => t.name === 'someObject',
|
|
362
394
|
)!
|
|
363
|
-
PortableTextEditor.insertBlock(editorRef.current, someObject, {
|
|
395
|
+
PortableTextEditor.insertBlock(editorRef.current, someObject, {
|
|
396
|
+
color: 'yellow',
|
|
397
|
+
})
|
|
364
398
|
}
|
|
365
399
|
})
|
|
366
400
|
|
|
@@ -376,7 +410,10 @@ describe('plugin:withEditableAPI: .insertBlock()', () => {
|
|
|
376
410
|
|
|
377
411
|
it('should not add empty blank blocks: in between blocks', async () => {
|
|
378
412
|
const editorRef: RefObject<PortableTextEditor> = createRef()
|
|
379
|
-
const value = [
|
|
413
|
+
const value = [
|
|
414
|
+
...initialValue,
|
|
415
|
+
{_key: 'b', _type: 'someObject', color: 'red'},
|
|
416
|
+
]
|
|
380
417
|
const onChange = jest.fn()
|
|
381
418
|
|
|
382
419
|
render(
|
|
@@ -408,7 +445,9 @@ describe('plugin:withEditableAPI: .insertBlock()', () => {
|
|
|
408
445
|
const someObject = editorRef.current.schemaTypes.inlineObjects.find(
|
|
409
446
|
(t) => t.name === 'someObject',
|
|
410
447
|
)!
|
|
411
|
-
PortableTextEditor.insertBlock(editorRef.current, someObject, {
|
|
448
|
+
PortableTextEditor.insertBlock(editorRef.current, someObject, {
|
|
449
|
+
color: 'yellow',
|
|
450
|
+
})
|
|
412
451
|
}
|
|
413
452
|
})
|
|
414
453
|
|
|
@@ -455,7 +494,9 @@ describe('plugin:withEditableAPI: .insertBlock()', () => {
|
|
|
455
494
|
const someObject = editorRef.current.schemaTypes.inlineObjects.find(
|
|
456
495
|
(t) => t.name === 'someObject',
|
|
457
496
|
)!
|
|
458
|
-
PortableTextEditor.insertBlock(editorRef.current, someObject, {
|
|
497
|
+
PortableTextEditor.insertBlock(editorRef.current, someObject, {
|
|
498
|
+
color: 'yellow',
|
|
499
|
+
})
|
|
459
500
|
}
|
|
460
501
|
})
|
|
461
502
|
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import {describe, expect, it, jest} from '@jest/globals'
|
|
2
|
-
import
|
|
2
|
+
import type {PortableTextBlock} from '@sanity/types'
|
|
3
3
|
import {render, waitFor} from '@testing-library/react'
|
|
4
4
|
import {createRef, type RefObject} from 'react'
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
import {
|
|
6
|
+
PortableTextEditorTester,
|
|
7
|
+
schemaType,
|
|
8
|
+
} from '../../__tests__/PortableTextEditorTester'
|
|
7
9
|
import {PortableTextEditor} from '../../PortableTextEditor'
|
|
8
10
|
|
|
9
11
|
const INITIAL_VALUE: PortableTextBlock[] = [
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import {describe, expect, it, jest} from '@jest/globals'
|
|
2
2
|
import {render, waitFor} from '@testing-library/react'
|
|
3
3
|
import {createRef, type RefObject} from 'react'
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
import {
|
|
5
|
+
PortableTextEditorTester,
|
|
6
|
+
schemaType,
|
|
7
|
+
} from '../../__tests__/PortableTextEditorTester'
|
|
6
8
|
import {PortableTextEditor} from '../../PortableTextEditor'
|
|
7
9
|
|
|
8
10
|
describe('plugin:withPortableTextLists', () => {
|
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
import {describe, expect, it, jest} from '@jest/globals'
|
|
2
|
-
/* eslint-disable max-nested-callbacks */
|
|
3
2
|
import {render, waitFor} from '@testing-library/react'
|
|
4
3
|
import {createRef, type RefObject} from 'react'
|
|
5
|
-
|
|
6
|
-
import {type EditorSelection} from '../../../types/editor'
|
|
7
4
|
import {
|
|
8
5
|
PortableTextEditorTester,
|
|
9
6
|
schemaType,
|
|
10
7
|
schemaTypeWithColorAndLink,
|
|
11
8
|
} from '../../__tests__/PortableTextEditorTester'
|
|
9
|
+
import type {EditorSelection} from '../../../types/editor'
|
|
12
10
|
import {PortableTextEditor} from '../../PortableTextEditor'
|
|
13
11
|
|
|
14
12
|
describe('plugin:withPortableTextMarksModel', () => {
|
|
@@ -79,7 +77,8 @@ describe('plugin:withPortableTextMarksModel', () => {
|
|
|
79
77
|
anchor: {path: [{_key: 'a'}, 'children', {_key: 'a1'}], offset: 3},
|
|
80
78
|
})
|
|
81
79
|
PortableTextEditor.toggleMark(editorRef.current, 'strong')
|
|
82
|
-
expect(PortableTextEditor.getValue(editorRef.current))
|
|
80
|
+
expect(PortableTextEditor.getValue(editorRef.current))
|
|
81
|
+
.toMatchInlineSnapshot(`
|
|
83
82
|
Array [
|
|
84
83
|
Object {
|
|
85
84
|
"_key": "a",
|
|
@@ -189,7 +188,10 @@ Array [
|
|
|
189
188
|
|
|
190
189
|
await waitFor(() => {
|
|
191
190
|
if (editorRef.current) {
|
|
192
|
-
expect(onChange).toHaveBeenCalledWith({
|
|
191
|
+
expect(onChange).toHaveBeenCalledWith({
|
|
192
|
+
type: 'value',
|
|
193
|
+
value: initialValue,
|
|
194
|
+
})
|
|
193
195
|
expect(onChange).toHaveBeenCalledWith({type: 'ready'})
|
|
194
196
|
}
|
|
195
197
|
})
|
|
@@ -296,8 +298,14 @@ Array [
|
|
|
296
298
|
},
|
|
297
299
|
]
|
|
298
300
|
const sel: EditorSelection = {
|
|
299
|
-
focus: {
|
|
300
|
-
|
|
301
|
+
focus: {
|
|
302
|
+
path: [{_key: '5fc57af23597'}, 'children', {_key: '11c8c9f783a8'}],
|
|
303
|
+
offset: 4,
|
|
304
|
+
},
|
|
305
|
+
anchor: {
|
|
306
|
+
path: [{_key: '7cd53af36712'}, 'children', {_key: '576c748e0cd2'}],
|
|
307
|
+
offset: 0,
|
|
308
|
+
},
|
|
301
309
|
}
|
|
302
310
|
const onChange = jest.fn()
|
|
303
311
|
await waitFor(() => {
|
|
@@ -424,7 +432,10 @@ Array [
|
|
|
424
432
|
|
|
425
433
|
await waitFor(() => {
|
|
426
434
|
if (editorRef.current) {
|
|
427
|
-
expect(onChange).toHaveBeenCalledWith({
|
|
435
|
+
expect(onChange).toHaveBeenCalledWith({
|
|
436
|
+
type: 'value',
|
|
437
|
+
value: initialValue,
|
|
438
|
+
})
|
|
428
439
|
expect(onChange).toHaveBeenCalledWith({type: 'ready'})
|
|
429
440
|
}
|
|
430
441
|
})
|
|
@@ -524,7 +535,10 @@ Array [
|
|
|
524
535
|
|
|
525
536
|
await waitFor(() => {
|
|
526
537
|
if (editorRef.current) {
|
|
527
|
-
expect(onChange).toHaveBeenCalledWith({
|
|
538
|
+
expect(onChange).toHaveBeenCalledWith({
|
|
539
|
+
type: 'value',
|
|
540
|
+
value: initialValue,
|
|
541
|
+
})
|
|
528
542
|
expect(onChange).toHaveBeenCalledWith({type: 'ready'})
|
|
529
543
|
}
|
|
530
544
|
})
|
|
@@ -537,12 +551,19 @@ Array [
|
|
|
537
551
|
|
|
538
552
|
await waitFor(() => {
|
|
539
553
|
if (editorRef.current) {
|
|
540
|
-
const currentSelectionObject = PortableTextEditor.getSelection(
|
|
554
|
+
const currentSelectionObject = PortableTextEditor.getSelection(
|
|
555
|
+
editorRef.current,
|
|
556
|
+
)
|
|
541
557
|
PortableTextEditor.toggleMark(editorRef.current, 'strong')
|
|
542
|
-
const nextSelectionObject = PortableTextEditor.getSelection(
|
|
558
|
+
const nextSelectionObject = PortableTextEditor.getSelection(
|
|
559
|
+
editorRef.current,
|
|
560
|
+
)
|
|
543
561
|
expect(currentSelectionObject).toEqual(nextSelectionObject)
|
|
544
562
|
expect(currentSelectionObject === nextSelectionObject).toBe(false)
|
|
545
|
-
expect(onChange).toHaveBeenCalledWith({
|
|
563
|
+
expect(onChange).toHaveBeenCalledWith({
|
|
564
|
+
type: 'selection',
|
|
565
|
+
selection: nextSelectionObject,
|
|
566
|
+
})
|
|
546
567
|
}
|
|
547
568
|
})
|
|
548
569
|
})
|
|
@@ -645,7 +666,9 @@ Array [
|
|
|
645
666
|
focus: {path: [{_key: 'a'}, 'children', {_key: 'a1'}], offset: 0},
|
|
646
667
|
anchor: {path: [{_key: 'a'}, 'children', {_key: '2'}], offset: 2},
|
|
647
668
|
})
|
|
648
|
-
expect(PortableTextEditor.isAnnotationActive(editor, 'link')).toBe(
|
|
669
|
+
expect(PortableTextEditor.isAnnotationActive(editor, 'link')).toBe(
|
|
670
|
+
false,
|
|
671
|
+
)
|
|
649
672
|
})
|
|
650
673
|
})
|
|
651
674
|
})
|
|
@@ -697,15 +720,27 @@ Array [
|
|
|
697
720
|
PortableTextEditor.focus(editorRef.current)
|
|
698
721
|
// Selects `a link` from `This is a link`, so the mark should be kept in the first span, color mark in both.
|
|
699
722
|
PortableTextEditor.select(editorRef.current, {
|
|
700
|
-
focus: {
|
|
723
|
+
focus: {
|
|
724
|
+
path: [
|
|
725
|
+
{_key: '5fc57af23597'},
|
|
726
|
+
'children',
|
|
727
|
+
{_key: 'be1c67c6971a'},
|
|
728
|
+
],
|
|
729
|
+
offset: 14,
|
|
730
|
+
},
|
|
701
731
|
anchor: {
|
|
702
|
-
path: [
|
|
732
|
+
path: [
|
|
733
|
+
{_key: '5fc57af23597'},
|
|
734
|
+
'children',
|
|
735
|
+
{_key: 'be1c67c6971a'},
|
|
736
|
+
],
|
|
703
737
|
offset: 8,
|
|
704
738
|
},
|
|
705
739
|
})
|
|
706
740
|
|
|
707
|
-
|
|
708
|
-
|
|
741
|
+
const linkType = editorRef.current.schemaTypes.annotations.find(
|
|
742
|
+
(a) => a.name === 'link',
|
|
743
|
+
)
|
|
709
744
|
if (!linkType) {
|
|
710
745
|
throw new Error('No link type found')
|
|
711
746
|
}
|
|
@@ -746,9 +781,16 @@ Array [
|
|
|
746
781
|
|
|
747
782
|
// removes the color from both
|
|
748
783
|
PortableTextEditor.select(editorRef.current, {
|
|
749
|
-
focus: {
|
|
784
|
+
focus: {
|
|
785
|
+
path: [{_key: '5fc57af23597'}, 'children', {_key: '1'}],
|
|
786
|
+
offset: 6,
|
|
787
|
+
},
|
|
750
788
|
anchor: {
|
|
751
|
-
path: [
|
|
789
|
+
path: [
|
|
790
|
+
{_key: '5fc57af23597'},
|
|
791
|
+
'children',
|
|
792
|
+
{_key: 'be1c67c6971a'},
|
|
793
|
+
],
|
|
752
794
|
offset: 0,
|
|
753
795
|
},
|
|
754
796
|
})
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import {describe, expect, it, jest} from '@jest/globals'
|
|
2
2
|
import {render, waitFor} from '@testing-library/react'
|
|
3
3
|
import {createRef, type RefObject} from 'react'
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
import {
|
|
5
|
+
PortableTextEditorTester,
|
|
6
|
+
schemaType,
|
|
7
|
+
} from '../../__tests__/PortableTextEditorTester'
|
|
6
8
|
import {PortableTextEditor} from '../../PortableTextEditor'
|
|
7
9
|
|
|
8
10
|
const initialValue = [
|
|
@@ -56,7 +58,8 @@ describe('plugin:withPortableTextSelections', () => {
|
|
|
56
58
|
if (editorRef.current) {
|
|
57
59
|
PortableTextEditor.focus(editorRef.current)
|
|
58
60
|
PortableTextEditor.select(editorRef.current, initialSelection)
|
|
59
|
-
expect(PortableTextEditor.getSelection(editorRef.current))
|
|
61
|
+
expect(PortableTextEditor.getSelection(editorRef.current))
|
|
62
|
+
.toMatchInlineSnapshot(`
|
|
60
63
|
Object {
|
|
61
64
|
"anchor": Object {
|
|
62
65
|
"offset": 9,
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import {describe, expect, it, jest} from '@jest/globals'
|
|
2
2
|
import {render, waitFor} from '@testing-library/react'
|
|
3
3
|
import {createRef, type RefObject} from 'react'
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
import {
|
|
5
|
+
PortableTextEditorTester,
|
|
6
|
+
schemaType,
|
|
7
|
+
} from '../../__tests__/PortableTextEditorTester'
|
|
6
8
|
import {PortableTextEditor} from '../../PortableTextEditor'
|
|
7
9
|
|
|
8
10
|
const initialValue = [
|
|
@@ -56,7 +58,10 @@ describe('plugin:withUndoRedo', () => {
|
|
|
56
58
|
|
|
57
59
|
await waitFor(() => {
|
|
58
60
|
if (editorRef.current) {
|
|
59
|
-
expect(onChange).toHaveBeenCalledWith({
|
|
61
|
+
expect(onChange).toHaveBeenCalledWith({
|
|
62
|
+
type: 'value',
|
|
63
|
+
value: initialValue,
|
|
64
|
+
})
|
|
60
65
|
expect(onChange).toHaveBeenCalledWith({type: 'ready'})
|
|
61
66
|
}
|
|
62
67
|
})
|
|
@@ -70,7 +75,8 @@ describe('plugin:withUndoRedo', () => {
|
|
|
70
75
|
PortableTextEditor.getSelection(editorRef.current),
|
|
71
76
|
{mode: 'blocks'},
|
|
72
77
|
)
|
|
73
|
-
expect(PortableTextEditor.getValue(editorRef.current))
|
|
78
|
+
expect(PortableTextEditor.getValue(editorRef.current))
|
|
79
|
+
.toMatchInlineSnapshot(`
|
|
74
80
|
Array [
|
|
75
81
|
Object {
|
|
76
82
|
"_key": "a",
|
|
@@ -89,7 +95,9 @@ describe('plugin:withUndoRedo', () => {
|
|
|
89
95
|
]
|
|
90
96
|
`)
|
|
91
97
|
PortableTextEditor.undo(editorRef.current)
|
|
92
|
-
expect(PortableTextEditor.getValue(editorRef.current)).toEqual(
|
|
98
|
+
expect(PortableTextEditor.getValue(editorRef.current)).toEqual(
|
|
99
|
+
initialValue,
|
|
100
|
+
)
|
|
93
101
|
}
|
|
94
102
|
})
|
|
95
103
|
})
|
|
@@ -108,7 +116,10 @@ describe('plugin:withUndoRedo', () => {
|
|
|
108
116
|
|
|
109
117
|
await waitFor(() => {
|
|
110
118
|
if (editorRef.current) {
|
|
111
|
-
expect(onChange).toHaveBeenCalledWith({
|
|
119
|
+
expect(onChange).toHaveBeenCalledWith({
|
|
120
|
+
type: 'value',
|
|
121
|
+
value: initialValue,
|
|
122
|
+
})
|
|
112
123
|
expect(onChange).toHaveBeenCalledWith({type: 'ready'})
|
|
113
124
|
}
|
|
114
125
|
})
|
|
@@ -117,15 +128,21 @@ describe('plugin:withUndoRedo', () => {
|
|
|
117
128
|
if (editorRef.current) {
|
|
118
129
|
PortableTextEditor.focus(editorRef.current)
|
|
119
130
|
PortableTextEditor.select(editorRef.current, initialSelection)
|
|
120
|
-
PortableTextEditor.insertBlock(
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
131
|
+
PortableTextEditor.insertBlock(
|
|
132
|
+
editorRef.current,
|
|
133
|
+
editorRef.current.schemaTypes.block,
|
|
134
|
+
{
|
|
135
|
+
children: [{_key: 'c1', _type: 'span', marks: [], text: 'Block C'}],
|
|
136
|
+
},
|
|
137
|
+
)
|
|
138
|
+
const producedKey = PortableTextEditor.getValue(
|
|
139
|
+
editorRef.current,
|
|
140
|
+
)?.slice(-1)[0]?._key
|
|
124
141
|
PortableTextEditor.undo(editorRef.current)
|
|
125
142
|
PortableTextEditor.redo(editorRef.current)
|
|
126
|
-
expect(
|
|
127
|
-
|
|
128
|
-
)
|
|
143
|
+
expect(
|
|
144
|
+
PortableTextEditor.getValue(editorRef.current)?.slice(-1)[0]?._key,
|
|
145
|
+
).toEqual(producedKey)
|
|
129
146
|
}
|
|
130
147
|
})
|
|
131
148
|
})
|