@portabletext/editor 1.44.10 → 1.44.11
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/_chunks-cjs/editor-provider.cjs +1 -1
- package/lib/_chunks-cjs/editor-provider.cjs.map +1 -1
- package/lib/_chunks-es/editor-provider.js +1 -1
- package/lib/_chunks-es/editor-provider.js.map +1 -1
- package/package.json +1 -1
- package/src/editor/__tests__/PortableTextEditor.test.tsx +10 -0
- package/src/editor/__tests__/PortableTextEditorTester.tsx +3 -17
- package/src/editor/__tests__/RangeDecorations.test.tsx +6 -0
- package/src/editor/__tests__/pteWarningsSelfSolving.test.tsx +12 -5
- package/src/editor/__tests__/sync-value.test.tsx +5 -0
- package/src/editor/plugins/__tests__/withEditableAPIDelete.test.tsx +8 -3
- package/src/editor/plugins/__tests__/withEditableAPIGetFragment.test.tsx +3 -0
- package/src/editor/plugins/__tests__/withEditableAPIInsert.test.tsx +20 -12
- package/src/editor/plugins/__tests__/withEditableAPISelectionsOverlapping.test.tsx +5 -0
- package/src/editor/plugins/__tests__/withPortableTextLists.test.tsx +2 -0
- package/src/editor/plugins/__tests__/withPortableTextMarkModel.test.tsx +10 -289
- package/src/editor/plugins/__tests__/withPortableTextSelections.test.tsx +2 -0
- package/src/editor/plugins/__tests__/withUndoRedo.test.tsx +3 -0
- package/src/editor/sync-machine.ts +8 -6
- package/src/internal-utils/__tests__/valueNormalization.test.tsx +2 -0
|
@@ -4,136 +4,13 @@ import {describe, expect, it, vi} from 'vitest'
|
|
|
4
4
|
import {
|
|
5
5
|
PortableTextEditorTester,
|
|
6
6
|
schemaType,
|
|
7
|
-
schemaTypeWithColorAndLink,
|
|
8
7
|
} from '../../__tests__/PortableTextEditorTester'
|
|
8
|
+
import {createTestKeyGenerator} from '../../../internal-utils/test-key-generator'
|
|
9
9
|
import type {EditorSelection} from '../../../types/editor'
|
|
10
10
|
import {PortableTextEditor} from '../../PortableTextEditor'
|
|
11
11
|
|
|
12
12
|
describe('plugin:withPortableTextMarksModel', () => {
|
|
13
13
|
describe('normalization', () => {
|
|
14
|
-
it('merges children correctly when toggling marks in various ranges', async () => {
|
|
15
|
-
const editorRef: RefObject<PortableTextEditor | null> = createRef()
|
|
16
|
-
const initialValue = [
|
|
17
|
-
{
|
|
18
|
-
_key: 'a',
|
|
19
|
-
_type: 'myTestBlockType',
|
|
20
|
-
children: [
|
|
21
|
-
{
|
|
22
|
-
_key: 'a1',
|
|
23
|
-
_type: 'span',
|
|
24
|
-
marks: [],
|
|
25
|
-
text: '1234',
|
|
26
|
-
},
|
|
27
|
-
],
|
|
28
|
-
markDefs: [],
|
|
29
|
-
style: 'normal',
|
|
30
|
-
},
|
|
31
|
-
]
|
|
32
|
-
const onChange = vi.fn()
|
|
33
|
-
await waitFor(() => {
|
|
34
|
-
render(
|
|
35
|
-
<PortableTextEditorTester
|
|
36
|
-
onChange={onChange}
|
|
37
|
-
ref={editorRef}
|
|
38
|
-
schemaType={schemaType}
|
|
39
|
-
value={initialValue}
|
|
40
|
-
/>,
|
|
41
|
-
)
|
|
42
|
-
})
|
|
43
|
-
const editor = editorRef.current!
|
|
44
|
-
expect(editor).toBeDefined()
|
|
45
|
-
await waitFor(() => {
|
|
46
|
-
PortableTextEditor.focus(editor)
|
|
47
|
-
PortableTextEditor.select(editor, {
|
|
48
|
-
focus: {path: [{_key: 'a'}, 'children', {_key: 'a1'}], offset: 0},
|
|
49
|
-
anchor: {path: [{_key: 'a'}, 'children', {_key: 'a1'}], offset: 4},
|
|
50
|
-
})
|
|
51
|
-
PortableTextEditor.toggleMark(editor, 'strong')
|
|
52
|
-
expect(PortableTextEditor.getValue(editor)).toEqual([
|
|
53
|
-
{
|
|
54
|
-
_key: 'a',
|
|
55
|
-
_type: 'myTestBlockType',
|
|
56
|
-
children: [
|
|
57
|
-
{
|
|
58
|
-
_key: 'a1',
|
|
59
|
-
_type: 'span',
|
|
60
|
-
marks: ['strong'],
|
|
61
|
-
text: '1234',
|
|
62
|
-
},
|
|
63
|
-
],
|
|
64
|
-
markDefs: [],
|
|
65
|
-
style: 'normal',
|
|
66
|
-
},
|
|
67
|
-
])
|
|
68
|
-
})
|
|
69
|
-
await waitFor(() => {
|
|
70
|
-
if (editorRef.current) {
|
|
71
|
-
PortableTextEditor.select(editorRef.current, {
|
|
72
|
-
focus: {path: [{_key: 'a'}, 'children', {_key: 'a1'}], offset: 1},
|
|
73
|
-
anchor: {path: [{_key: 'a'}, 'children', {_key: 'a1'}], offset: 3},
|
|
74
|
-
})
|
|
75
|
-
PortableTextEditor.toggleMark(editorRef.current, 'strong')
|
|
76
|
-
expect(PortableTextEditor.getValue(editorRef.current)).toEqual([
|
|
77
|
-
{
|
|
78
|
-
_key: 'a',
|
|
79
|
-
_type: 'myTestBlockType',
|
|
80
|
-
children: [
|
|
81
|
-
{
|
|
82
|
-
_key: 'a1',
|
|
83
|
-
_type: 'span',
|
|
84
|
-
marks: ['strong'],
|
|
85
|
-
text: '1',
|
|
86
|
-
},
|
|
87
|
-
{
|
|
88
|
-
_key: '2',
|
|
89
|
-
_type: 'span',
|
|
90
|
-
marks: [],
|
|
91
|
-
text: '23',
|
|
92
|
-
},
|
|
93
|
-
{
|
|
94
|
-
_key: '1',
|
|
95
|
-
_type: 'span',
|
|
96
|
-
marks: ['strong'],
|
|
97
|
-
text: '4',
|
|
98
|
-
},
|
|
99
|
-
],
|
|
100
|
-
markDefs: [],
|
|
101
|
-
style: 'normal',
|
|
102
|
-
},
|
|
103
|
-
])
|
|
104
|
-
}
|
|
105
|
-
})
|
|
106
|
-
await waitFor(() => {
|
|
107
|
-
if (editor) {
|
|
108
|
-
PortableTextEditor.select(editor, {
|
|
109
|
-
focus: {path: [{_key: 'a'}, 'children', {_key: 'a1'}], offset: 0},
|
|
110
|
-
anchor: {path: [{_key: 'a'}, 'children', {_key: '1'}], offset: 1},
|
|
111
|
-
})
|
|
112
|
-
PortableTextEditor.toggleMark(editor, 'strong')
|
|
113
|
-
expect(PortableTextEditor.getValue(editor)).toMatchInlineSnapshot(`
|
|
114
|
-
[
|
|
115
|
-
{
|
|
116
|
-
"_key": "a",
|
|
117
|
-
"_type": "myTestBlockType",
|
|
118
|
-
"children": [
|
|
119
|
-
{
|
|
120
|
-
"_key": "a1",
|
|
121
|
-
"_type": "span",
|
|
122
|
-
"marks": [
|
|
123
|
-
"strong",
|
|
124
|
-
],
|
|
125
|
-
"text": "1234",
|
|
126
|
-
},
|
|
127
|
-
],
|
|
128
|
-
"markDefs": [],
|
|
129
|
-
"style": "normal",
|
|
130
|
-
},
|
|
131
|
-
]
|
|
132
|
-
`)
|
|
133
|
-
}
|
|
134
|
-
})
|
|
135
|
-
})
|
|
136
|
-
|
|
137
14
|
it('toggles marks on children with annotation marks correctly', async () => {
|
|
138
15
|
const editorRef: RefObject<PortableTextEditor | null> = createRef()
|
|
139
16
|
const initialValue = [
|
|
@@ -172,6 +49,7 @@ describe('plugin:withPortableTextMarksModel', () => {
|
|
|
172
49
|
ref={editorRef}
|
|
173
50
|
schemaType={schemaType}
|
|
174
51
|
value={initialValue}
|
|
52
|
+
keyGenerator={createTestKeyGenerator()}
|
|
175
53
|
/>,
|
|
176
54
|
)
|
|
177
55
|
|
|
@@ -300,6 +178,7 @@ describe('plugin:withPortableTextMarksModel', () => {
|
|
|
300
178
|
await waitFor(() => {
|
|
301
179
|
render(
|
|
302
180
|
<PortableTextEditorTester
|
|
181
|
+
keyGenerator={createTestKeyGenerator()}
|
|
303
182
|
onChange={onChange}
|
|
304
183
|
ref={editorRef}
|
|
305
184
|
schemaType={schemaType}
|
|
@@ -416,6 +295,7 @@ describe('plugin:withPortableTextMarksModel', () => {
|
|
|
416
295
|
ref={editorRef}
|
|
417
296
|
schemaType={schemaType}
|
|
418
297
|
value={initialValue}
|
|
298
|
+
keyGenerator={createTestKeyGenerator()}
|
|
419
299
|
/>,
|
|
420
300
|
)
|
|
421
301
|
|
|
@@ -454,11 +334,11 @@ describe('plugin:withPortableTextMarksModel', () => {
|
|
|
454
334
|
style: 'normal',
|
|
455
335
|
},
|
|
456
336
|
{
|
|
457
|
-
_key: '
|
|
337
|
+
_key: 'k2',
|
|
458
338
|
_type: 'myTestBlockType',
|
|
459
339
|
children: [
|
|
460
340
|
{
|
|
461
|
-
_key: '
|
|
341
|
+
_key: 'k3',
|
|
462
342
|
_type: 'span',
|
|
463
343
|
marks: [],
|
|
464
344
|
text: '',
|
|
@@ -519,6 +399,7 @@ describe('plugin:withPortableTextMarksModel', () => {
|
|
|
519
399
|
ref={editorRef}
|
|
520
400
|
schemaType={schemaType}
|
|
521
401
|
value={initialValue}
|
|
402
|
+
keyGenerator={createTestKeyGenerator()}
|
|
522
403
|
/>,
|
|
523
404
|
)
|
|
524
405
|
|
|
@@ -589,6 +470,7 @@ describe('plugin:withPortableTextMarksModel', () => {
|
|
|
589
470
|
ref={editorRef}
|
|
590
471
|
schemaType={schemaType}
|
|
591
472
|
value={initialValue}
|
|
473
|
+
keyGenerator={createTestKeyGenerator()}
|
|
592
474
|
/>,
|
|
593
475
|
)
|
|
594
476
|
})
|
|
@@ -644,6 +526,7 @@ describe('plugin:withPortableTextMarksModel', () => {
|
|
|
644
526
|
ref={editorRef}
|
|
645
527
|
schemaType={schemaType}
|
|
646
528
|
value={initialValue}
|
|
529
|
+
keyGenerator={createTestKeyGenerator()}
|
|
647
530
|
/>,
|
|
648
531
|
)
|
|
649
532
|
})
|
|
@@ -662,169 +545,6 @@ describe('plugin:withPortableTextMarksModel', () => {
|
|
|
662
545
|
})
|
|
663
546
|
})
|
|
664
547
|
|
|
665
|
-
describe('removing annotations', () => {
|
|
666
|
-
it('preserves other marks that apply to the spans', async () => {
|
|
667
|
-
const editorRef: RefObject<PortableTextEditor | null> = createRef()
|
|
668
|
-
const initialValue = [
|
|
669
|
-
{
|
|
670
|
-
_key: '5fc57af23597',
|
|
671
|
-
_type: 'myTestBlockType',
|
|
672
|
-
children: [
|
|
673
|
-
{
|
|
674
|
-
_key: 'be1c67c6971a',
|
|
675
|
-
_type: 'span',
|
|
676
|
-
marks: ['fde1fd54b544', '7b6d3d5de30c'],
|
|
677
|
-
text: 'This is a link',
|
|
678
|
-
},
|
|
679
|
-
],
|
|
680
|
-
markDefs: [
|
|
681
|
-
{
|
|
682
|
-
_key: 'fde1fd54b544',
|
|
683
|
-
_type: 'link',
|
|
684
|
-
url: '1',
|
|
685
|
-
},
|
|
686
|
-
{
|
|
687
|
-
_key: '7b6d3d5de30c',
|
|
688
|
-
_type: 'color',
|
|
689
|
-
color: 'blue',
|
|
690
|
-
},
|
|
691
|
-
],
|
|
692
|
-
style: 'normal',
|
|
693
|
-
},
|
|
694
|
-
]
|
|
695
|
-
const onChange = vi.fn()
|
|
696
|
-
await waitFor(() => {
|
|
697
|
-
render(
|
|
698
|
-
<PortableTextEditorTester
|
|
699
|
-
onChange={onChange}
|
|
700
|
-
ref={editorRef}
|
|
701
|
-
schemaType={schemaTypeWithColorAndLink}
|
|
702
|
-
value={initialValue}
|
|
703
|
-
/>,
|
|
704
|
-
)
|
|
705
|
-
})
|
|
706
|
-
|
|
707
|
-
await waitFor(() => {
|
|
708
|
-
if (editorRef.current) {
|
|
709
|
-
PortableTextEditor.focus(editorRef.current)
|
|
710
|
-
// Selects `a link` from `This is a link`, so the mark should be kept in the first span, color mark in both.
|
|
711
|
-
PortableTextEditor.select(editorRef.current, {
|
|
712
|
-
focus: {
|
|
713
|
-
path: [
|
|
714
|
-
{_key: '5fc57af23597'},
|
|
715
|
-
'children',
|
|
716
|
-
{_key: 'be1c67c6971a'},
|
|
717
|
-
],
|
|
718
|
-
offset: 14,
|
|
719
|
-
},
|
|
720
|
-
anchor: {
|
|
721
|
-
path: [
|
|
722
|
-
{_key: '5fc57af23597'},
|
|
723
|
-
'children',
|
|
724
|
-
{_key: 'be1c67c6971a'},
|
|
725
|
-
],
|
|
726
|
-
offset: 8,
|
|
727
|
-
},
|
|
728
|
-
})
|
|
729
|
-
|
|
730
|
-
const linkType = editorRef.current.schemaTypes.annotations.find(
|
|
731
|
-
(a) => a.name === 'link',
|
|
732
|
-
)
|
|
733
|
-
if (!linkType) {
|
|
734
|
-
throw new Error('No link type found')
|
|
735
|
-
}
|
|
736
|
-
PortableTextEditor.removeAnnotation(editorRef.current, linkType)
|
|
737
|
-
expect(PortableTextEditor.getValue(editorRef.current)).toEqual([
|
|
738
|
-
{
|
|
739
|
-
_key: '5fc57af23597',
|
|
740
|
-
_type: 'myTestBlockType',
|
|
741
|
-
children: [
|
|
742
|
-
{
|
|
743
|
-
_key: 'be1c67c6971a',
|
|
744
|
-
_type: 'span',
|
|
745
|
-
marks: ['fde1fd54b544', '7b6d3d5de30c'], // It has both marks, the link was only removed from the second span
|
|
746
|
-
text: 'This is ',
|
|
747
|
-
},
|
|
748
|
-
{
|
|
749
|
-
_key: '1',
|
|
750
|
-
_type: 'span',
|
|
751
|
-
marks: ['7b6d3d5de30c'],
|
|
752
|
-
text: 'a link',
|
|
753
|
-
},
|
|
754
|
-
],
|
|
755
|
-
markDefs: [
|
|
756
|
-
{
|
|
757
|
-
_key: 'fde1fd54b544',
|
|
758
|
-
_type: 'link',
|
|
759
|
-
url: '1',
|
|
760
|
-
},
|
|
761
|
-
{
|
|
762
|
-
_key: '7b6d3d5de30c',
|
|
763
|
-
_type: 'color',
|
|
764
|
-
color: 'blue',
|
|
765
|
-
},
|
|
766
|
-
],
|
|
767
|
-
style: 'normal',
|
|
768
|
-
},
|
|
769
|
-
])
|
|
770
|
-
|
|
771
|
-
// removes the color from both
|
|
772
|
-
PortableTextEditor.select(editorRef.current, {
|
|
773
|
-
focus: {
|
|
774
|
-
path: [{_key: '5fc57af23597'}, 'children', {_key: '1'}],
|
|
775
|
-
offset: 6,
|
|
776
|
-
},
|
|
777
|
-
anchor: {
|
|
778
|
-
path: [
|
|
779
|
-
{_key: '5fc57af23597'},
|
|
780
|
-
'children',
|
|
781
|
-
{_key: 'be1c67c6971a'},
|
|
782
|
-
],
|
|
783
|
-
offset: 0,
|
|
784
|
-
},
|
|
785
|
-
})
|
|
786
|
-
const colorType = editorRef.current.schemaTypes.annotations.find(
|
|
787
|
-
(a) => a.name === 'color',
|
|
788
|
-
)
|
|
789
|
-
if (!colorType) {
|
|
790
|
-
throw new Error('No color type found')
|
|
791
|
-
}
|
|
792
|
-
|
|
793
|
-
PortableTextEditor.removeAnnotation(editorRef.current, colorType)
|
|
794
|
-
|
|
795
|
-
expect(PortableTextEditor.getValue(editorRef.current)).toEqual([
|
|
796
|
-
{
|
|
797
|
-
_key: '5fc57af23597',
|
|
798
|
-
_type: 'myTestBlockType',
|
|
799
|
-
children: [
|
|
800
|
-
{
|
|
801
|
-
_key: 'be1c67c6971a',
|
|
802
|
-
_type: 'span',
|
|
803
|
-
marks: ['fde1fd54b544'], // The color was removed from both
|
|
804
|
-
text: 'This is ',
|
|
805
|
-
},
|
|
806
|
-
{
|
|
807
|
-
_key: '1',
|
|
808
|
-
_type: 'span',
|
|
809
|
-
marks: [], // The color was removed from both
|
|
810
|
-
text: 'a link',
|
|
811
|
-
},
|
|
812
|
-
],
|
|
813
|
-
markDefs: [
|
|
814
|
-
{
|
|
815
|
-
_key: 'fde1fd54b544',
|
|
816
|
-
_type: 'link',
|
|
817
|
-
url: '1',
|
|
818
|
-
},
|
|
819
|
-
],
|
|
820
|
-
style: 'normal',
|
|
821
|
-
},
|
|
822
|
-
])
|
|
823
|
-
}
|
|
824
|
-
})
|
|
825
|
-
})
|
|
826
|
-
})
|
|
827
|
-
|
|
828
548
|
describe('removing nodes', () => {
|
|
829
549
|
it('should not insert a new block if we have more blocks available', async () => {
|
|
830
550
|
const editorRef: RefObject<PortableTextEditor | null> = createRef()
|
|
@@ -852,6 +572,7 @@ describe('plugin:withPortableTextMarksModel', () => {
|
|
|
852
572
|
await waitFor(() => {
|
|
853
573
|
render(
|
|
854
574
|
<PortableTextEditorTester
|
|
575
|
+
keyGenerator={createTestKeyGenerator()}
|
|
855
576
|
onChange={onChange}
|
|
856
577
|
ref={editorRef}
|
|
857
578
|
schemaType={schemaType}
|
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
PortableTextEditorTester,
|
|
6
6
|
schemaType,
|
|
7
7
|
} from '../../__tests__/PortableTextEditorTester'
|
|
8
|
+
import {createTestKeyGenerator} from '../../../internal-utils/test-key-generator'
|
|
8
9
|
import {PortableTextEditor} from '../../PortableTextEditor'
|
|
9
10
|
|
|
10
11
|
const initialValue = [
|
|
@@ -44,6 +45,7 @@ describe('plugin:withPortableTextSelections', () => {
|
|
|
44
45
|
const onChange = vi.fn()
|
|
45
46
|
render(
|
|
46
47
|
<PortableTextEditorTester
|
|
48
|
+
keyGenerator={createTestKeyGenerator()}
|
|
47
49
|
onChange={onChange}
|
|
48
50
|
ref={editorRef}
|
|
49
51
|
schemaType={schemaType}
|
|
@@ -5,6 +5,7 @@ import {
|
|
|
5
5
|
PortableTextEditorTester,
|
|
6
6
|
schemaType,
|
|
7
7
|
} from '../../__tests__/PortableTextEditorTester'
|
|
8
|
+
import {createTestKeyGenerator} from '../../../internal-utils/test-key-generator'
|
|
8
9
|
import {PortableTextEditor} from '../../PortableTextEditor'
|
|
9
10
|
|
|
10
11
|
const initialValue = [
|
|
@@ -49,6 +50,7 @@ describe('plugin:withUndoRedo', () => {
|
|
|
49
50
|
const onChange = vi.fn()
|
|
50
51
|
render(
|
|
51
52
|
<PortableTextEditorTester
|
|
53
|
+
keyGenerator={createTestKeyGenerator()}
|
|
52
54
|
onChange={onChange}
|
|
53
55
|
ref={editorRef}
|
|
54
56
|
schemaType={schemaType}
|
|
@@ -107,6 +109,7 @@ describe('plugin:withUndoRedo', () => {
|
|
|
107
109
|
|
|
108
110
|
render(
|
|
109
111
|
<PortableTextEditorTester
|
|
112
|
+
keyGenerator={createTestKeyGenerator()}
|
|
110
113
|
onChange={onChange}
|
|
111
114
|
ref={editorRef}
|
|
112
115
|
schemaType={schemaType}
|
|
@@ -735,12 +735,14 @@ function _updateBlock(
|
|
|
735
735
|
Text.isText(oldBlockChild) &&
|
|
736
736
|
oldBlockChild._type === 'span'
|
|
737
737
|
if (isSpanNode && isTextChanged) {
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
738
|
+
if (oldBlockChild.text.length > 0) {
|
|
739
|
+
Transforms.delete(slateEditor, {
|
|
740
|
+
at: {
|
|
741
|
+
focus: {path, offset: 0},
|
|
742
|
+
anchor: {path, offset: oldBlockChild.text.length},
|
|
743
|
+
},
|
|
744
|
+
})
|
|
745
|
+
}
|
|
744
746
|
Transforms.insertText(slateEditor, currentBlockChild.text, {
|
|
745
747
|
at: path,
|
|
746
748
|
})
|
|
@@ -6,6 +6,7 @@ import {
|
|
|
6
6
|
schemaType,
|
|
7
7
|
} from '../../editor/__tests__/PortableTextEditorTester'
|
|
8
8
|
import {PortableTextEditor} from '../../editor/PortableTextEditor'
|
|
9
|
+
import {createTestKeyGenerator} from '../test-key-generator'
|
|
9
10
|
|
|
10
11
|
describe('values: normalization', () => {
|
|
11
12
|
it("accepts incoming value with blocks without a style or markDefs prop, but doesn't leave them without them when editing them", async () => {
|
|
@@ -28,6 +29,7 @@ describe('values: normalization', () => {
|
|
|
28
29
|
const onChange = vi.fn()
|
|
29
30
|
render(
|
|
30
31
|
<PortableTextEditorTester
|
|
32
|
+
keyGenerator={createTestKeyGenerator()}
|
|
31
33
|
onChange={onChange}
|
|
32
34
|
ref={editorRef}
|
|
33
35
|
schemaType={schemaType}
|