@portabletext/editor 2.7.2 → 2.8.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/lib/_chunks-cjs/selector.is-selecting-entire-blocks.cjs +3 -1
- package/lib/_chunks-cjs/selector.is-selecting-entire-blocks.cjs.map +1 -1
- package/lib/_chunks-cjs/util.slice-blocks.cjs +60 -6
- package/lib/_chunks-cjs/util.slice-blocks.cjs.map +1 -1
- package/lib/_chunks-dts/behavior.types.action.d.cts +95 -95
- package/lib/_chunks-dts/behavior.types.action.d.ts +95 -95
- package/lib/_chunks-es/selector.is-selecting-entire-blocks.js +4 -2
- package/lib/_chunks-es/selector.is-selecting-entire-blocks.js.map +1 -1
- package/lib/_chunks-es/util.slice-blocks.js +56 -5
- package/lib/_chunks-es/util.slice-blocks.js.map +1 -1
- package/lib/index.cjs +94 -121
- package/lib/index.cjs.map +1 -1
- package/lib/index.js +90 -118
- package/lib/index.js.map +1 -1
- package/lib/plugins/index.d.cts +3 -3
- package/lib/plugins/index.d.ts +3 -3
- package/lib/selectors/index.d.cts +13 -3
- package/lib/selectors/index.d.ts +13 -3
- package/lib/utils/index.d.ts +2 -2
- package/package.json +13 -14
- package/src/behaviors/behavior.abstract.insert.ts +58 -1
- package/src/behaviors/behavior.core.annotations.ts +24 -2
- package/src/behaviors/behavior.core.ts +1 -1
- package/src/behaviors/behavior.types.event.ts +18 -18
- package/src/converters/converter.text-html.serialize.test.ts +27 -17
- package/src/converters/converter.text-plain.test.ts +1 -1
- package/src/editor/plugins/createWithEditableAPI.ts +16 -0
- package/src/internal-utils/parse-blocks.ts +2 -1
- package/src/operations/behavior.operation.annotation.add.ts +1 -12
- package/src/operations/behavior.operations.ts +0 -18
- package/src/selectors/selector.is-active-annotation.test.ts +320 -0
- package/src/selectors/selector.is-active-annotation.ts +24 -0
- package/src/utils/util.slice-blocks.test.ts +39 -5
- package/src/utils/util.slice-blocks.ts +36 -3
- package/src/editor/__tests__/PortableTextEditor.test.tsx +0 -430
- package/src/editor/__tests__/PortableTextEditorTester.tsx +0 -58
- package/src/editor/__tests__/RangeDecorations.test.tsx +0 -213
- package/src/editor/__tests__/insert-block.test.tsx +0 -224
- package/src/editor/__tests__/self-solving.test.tsx +0 -183
- package/src/editor/plugins/__tests__/withEditableAPIDelete.test.tsx +0 -298
- package/src/editor/plugins/__tests__/withEditableAPIGetFragment.test.tsx +0 -177
- package/src/editor/plugins/__tests__/withEditableAPIInsert.test.tsx +0 -538
- package/src/editor/plugins/__tests__/withEditableAPISelectionsOverlapping.test.tsx +0 -162
- package/src/editor/plugins/__tests__/withPortableTextLists.test.tsx +0 -65
- package/src/editor/plugins/__tests__/withPortableTextMarkModel.test.tsx +0 -612
- package/src/editor/plugins/__tests__/withPortableTextSelections.test.tsx +0 -103
- package/src/editor/plugins/__tests__/withUndoRedo.test.tsx +0 -147
- package/src/internal-utils/__tests__/valueNormalization.test.tsx +0 -79
- package/src/operations/behavior.operation.insert-inline-object.ts +0 -59
- package/src/operations/behavior.operation.insert-span.ts +0 -48
|
@@ -1,224 +0,0 @@
|
|
|
1
|
-
import type {PortableTextBlock} from '@sanity/types'
|
|
2
|
-
import {render, waitFor} from '@testing-library/react'
|
|
3
|
-
import {createRef, type RefObject} from 'react'
|
|
4
|
-
import {describe, expect, test, vi} from 'vitest'
|
|
5
|
-
import type {EditorChange, EditorSelection} from '../../types/editor'
|
|
6
|
-
import {PortableTextEditor} from '../PortableTextEditor'
|
|
7
|
-
import {PortableTextEditorTester} from './PortableTextEditorTester'
|
|
8
|
-
|
|
9
|
-
describe(PortableTextEditor.insertBlock.name, () => {
|
|
10
|
-
test('Scenario: Inserting a custom block without a selection #1', async () => {
|
|
11
|
-
const editorRef: RefObject<PortableTextEditor | null> = createRef()
|
|
12
|
-
const emptyTextBlock: PortableTextBlock = {
|
|
13
|
-
_key: 'ba',
|
|
14
|
-
_type: 'block',
|
|
15
|
-
children: [
|
|
16
|
-
{
|
|
17
|
-
_type: 'span',
|
|
18
|
-
_key: 'sa',
|
|
19
|
-
text: '',
|
|
20
|
-
marks: [],
|
|
21
|
-
},
|
|
22
|
-
],
|
|
23
|
-
style: 'normal',
|
|
24
|
-
}
|
|
25
|
-
const initialValue: Array<PortableTextBlock> = [emptyTextBlock]
|
|
26
|
-
const onChange: (change: EditorChange) => void = vi.fn()
|
|
27
|
-
|
|
28
|
-
render(
|
|
29
|
-
<PortableTextEditorTester
|
|
30
|
-
ref={editorRef}
|
|
31
|
-
value={initialValue}
|
|
32
|
-
keyGenerator={() => 'bb'}
|
|
33
|
-
onChange={onChange}
|
|
34
|
-
/>,
|
|
35
|
-
)
|
|
36
|
-
|
|
37
|
-
// Given an empty text block
|
|
38
|
-
await waitFor(() => {
|
|
39
|
-
if (editorRef.current) {
|
|
40
|
-
expect(onChange).toHaveBeenCalledWith({
|
|
41
|
-
type: 'value',
|
|
42
|
-
value: initialValue,
|
|
43
|
-
})
|
|
44
|
-
expect(onChange).toHaveBeenCalledWith({type: 'ready'})
|
|
45
|
-
}
|
|
46
|
-
})
|
|
47
|
-
|
|
48
|
-
// And no selection
|
|
49
|
-
await waitFor(() => {
|
|
50
|
-
if (editorRef.current) {
|
|
51
|
-
expect(PortableTextEditor.getSelection(editorRef.current)).toBeNull()
|
|
52
|
-
}
|
|
53
|
-
})
|
|
54
|
-
|
|
55
|
-
// When a new image is inserted
|
|
56
|
-
await waitFor(() => {
|
|
57
|
-
if (editorRef.current) {
|
|
58
|
-
const imageBlockType = editorRef.current.schemaTypes.blockObjects.find(
|
|
59
|
-
(object) => object.name === 'custom image',
|
|
60
|
-
)!
|
|
61
|
-
PortableTextEditor.insertBlock(editorRef.current, imageBlockType)
|
|
62
|
-
}
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
// Then the empty text block is replaced with the new image
|
|
66
|
-
await waitFor(() => {
|
|
67
|
-
if (editorRef.current) {
|
|
68
|
-
expect(PortableTextEditor.getValue(editorRef.current)).toEqual([
|
|
69
|
-
{_key: 'bb', _type: 'custom image'},
|
|
70
|
-
])
|
|
71
|
-
}
|
|
72
|
-
})
|
|
73
|
-
})
|
|
74
|
-
|
|
75
|
-
test('Scenario: Inserting a custom block without a selection #2', async () => {
|
|
76
|
-
const editorRef: RefObject<PortableTextEditor | null> = createRef()
|
|
77
|
-
const nonEmptyTextBlock: PortableTextBlock = {
|
|
78
|
-
_key: 'ba',
|
|
79
|
-
_type: 'block',
|
|
80
|
-
children: [
|
|
81
|
-
{
|
|
82
|
-
_type: 'span',
|
|
83
|
-
_key: 'xs',
|
|
84
|
-
text: 'foo',
|
|
85
|
-
marks: [],
|
|
86
|
-
},
|
|
87
|
-
],
|
|
88
|
-
markDefs: [],
|
|
89
|
-
style: 'normal',
|
|
90
|
-
}
|
|
91
|
-
const initialValue: Array<PortableTextBlock> = [nonEmptyTextBlock]
|
|
92
|
-
const onChange: (change: EditorChange) => void = vi.fn()
|
|
93
|
-
|
|
94
|
-
render(
|
|
95
|
-
<PortableTextEditorTester
|
|
96
|
-
ref={editorRef}
|
|
97
|
-
value={initialValue}
|
|
98
|
-
keyGenerator={() => 'bb'}
|
|
99
|
-
onChange={onChange}
|
|
100
|
-
/>,
|
|
101
|
-
)
|
|
102
|
-
|
|
103
|
-
// Given an non-empty text block
|
|
104
|
-
await waitFor(() => {
|
|
105
|
-
if (editorRef.current) {
|
|
106
|
-
expect(onChange).toHaveBeenCalledWith({
|
|
107
|
-
type: 'value',
|
|
108
|
-
value: initialValue,
|
|
109
|
-
})
|
|
110
|
-
expect(onChange).toHaveBeenCalledWith({type: 'ready'})
|
|
111
|
-
}
|
|
112
|
-
})
|
|
113
|
-
|
|
114
|
-
// And no selection
|
|
115
|
-
await waitFor(() => {
|
|
116
|
-
if (editorRef.current) {
|
|
117
|
-
expect(PortableTextEditor.getSelection(editorRef.current)).toBeNull()
|
|
118
|
-
}
|
|
119
|
-
})
|
|
120
|
-
|
|
121
|
-
// When a new image is inserted
|
|
122
|
-
await waitFor(() => {
|
|
123
|
-
if (editorRef.current) {
|
|
124
|
-
const imageBlockType = editorRef.current.schemaTypes.blockObjects.find(
|
|
125
|
-
(object) => object.name === 'custom image',
|
|
126
|
-
)!
|
|
127
|
-
PortableTextEditor.insertBlock(editorRef.current, imageBlockType)
|
|
128
|
-
}
|
|
129
|
-
})
|
|
130
|
-
|
|
131
|
-
// Then the empty text block is replaced with the new image
|
|
132
|
-
await waitFor(() => {
|
|
133
|
-
if (editorRef.current) {
|
|
134
|
-
expect(PortableTextEditor.getValue(editorRef.current)).toEqual([
|
|
135
|
-
nonEmptyTextBlock,
|
|
136
|
-
{_key: 'bb', _type: 'custom image'},
|
|
137
|
-
])
|
|
138
|
-
}
|
|
139
|
-
})
|
|
140
|
-
})
|
|
141
|
-
|
|
142
|
-
test('Scenario: Replacing an empty text block with a custom block', async () => {
|
|
143
|
-
const editorRef: RefObject<PortableTextEditor | null> = createRef()
|
|
144
|
-
const emptyTextBlock: PortableTextBlock = {
|
|
145
|
-
_key: 'ba',
|
|
146
|
-
_type: 'block',
|
|
147
|
-
children: [
|
|
148
|
-
{
|
|
149
|
-
_type: 'span',
|
|
150
|
-
_key: 'sa',
|
|
151
|
-
text: '',
|
|
152
|
-
marks: [],
|
|
153
|
-
},
|
|
154
|
-
],
|
|
155
|
-
style: 'normal',
|
|
156
|
-
}
|
|
157
|
-
const imageBlock: PortableTextBlock = {
|
|
158
|
-
_key: 'bb',
|
|
159
|
-
_type: 'custom image',
|
|
160
|
-
}
|
|
161
|
-
const initialValue: Array<PortableTextBlock> = [emptyTextBlock, imageBlock]
|
|
162
|
-
const onChange: (change: EditorChange) => void = vi.fn()
|
|
163
|
-
|
|
164
|
-
render(
|
|
165
|
-
<PortableTextEditorTester
|
|
166
|
-
ref={editorRef}
|
|
167
|
-
value={initialValue}
|
|
168
|
-
keyGenerator={() => 'bc'}
|
|
169
|
-
onChange={onChange}
|
|
170
|
-
/>,
|
|
171
|
-
)
|
|
172
|
-
|
|
173
|
-
// Given an empty text block followed by an image
|
|
174
|
-
await waitFor(() => {
|
|
175
|
-
if (editorRef.current) {
|
|
176
|
-
expect(onChange).toHaveBeenCalledWith({
|
|
177
|
-
type: 'value',
|
|
178
|
-
value: initialValue,
|
|
179
|
-
})
|
|
180
|
-
expect(onChange).toHaveBeenCalledWith({type: 'ready'})
|
|
181
|
-
}
|
|
182
|
-
})
|
|
183
|
-
|
|
184
|
-
// And a selection in the empty text block
|
|
185
|
-
const initialSelection: EditorSelection = {
|
|
186
|
-
anchor: {path: [{_key: 'ba'}, 'children', {_key: 'sa'}], offset: 0},
|
|
187
|
-
focus: {path: [{_key: 'ba'}, 'children', {_key: 'sa'}], offset: 0},
|
|
188
|
-
backward: false,
|
|
189
|
-
}
|
|
190
|
-
await waitFor(() => {
|
|
191
|
-
if (editorRef.current) {
|
|
192
|
-
PortableTextEditor.select(editorRef.current, initialSelection)
|
|
193
|
-
}
|
|
194
|
-
})
|
|
195
|
-
await waitFor(() => {
|
|
196
|
-
if (editorRef.current) {
|
|
197
|
-
expect(onChange).toHaveBeenCalledWith({
|
|
198
|
-
type: 'selection',
|
|
199
|
-
selection: initialSelection,
|
|
200
|
-
})
|
|
201
|
-
}
|
|
202
|
-
})
|
|
203
|
-
|
|
204
|
-
// When a new image is inserted
|
|
205
|
-
await waitFor(() => {
|
|
206
|
-
if (editorRef.current) {
|
|
207
|
-
const imageBlockType = editorRef.current.schemaTypes.blockObjects.find(
|
|
208
|
-
(object) => object.name === 'custom image',
|
|
209
|
-
)!
|
|
210
|
-
PortableTextEditor.insertBlock(editorRef.current, imageBlockType)
|
|
211
|
-
}
|
|
212
|
-
})
|
|
213
|
-
|
|
214
|
-
// Then the empty text block is replaced with the new image
|
|
215
|
-
await waitFor(() => {
|
|
216
|
-
if (editorRef.current) {
|
|
217
|
-
expect(PortableTextEditor.getValue(editorRef.current)).toEqual([
|
|
218
|
-
{_key: 'bc', _type: 'custom image'},
|
|
219
|
-
{_key: 'bb', _type: 'custom image'},
|
|
220
|
-
])
|
|
221
|
-
}
|
|
222
|
-
})
|
|
223
|
-
})
|
|
224
|
-
})
|
|
@@ -1,183 +0,0 @@
|
|
|
1
|
-
import type {JSONValue, Patch} from '@portabletext/patches'
|
|
2
|
-
import {compileSchema, defineSchema} from '@portabletext/schema'
|
|
3
|
-
import {createTestKeyGenerator} from '@portabletext/test'
|
|
4
|
-
import type {PortableTextBlock, PortableTextSpan} from '@sanity/types'
|
|
5
|
-
import {render, waitFor} from '@testing-library/react'
|
|
6
|
-
import {createRef, type ComponentProps, type RefObject} from 'react'
|
|
7
|
-
import {describe, expect, it, vi} from 'vitest'
|
|
8
|
-
import {getTextSelection} from '../../internal-utils/text-selection'
|
|
9
|
-
import {PortableTextEditor} from '../PortableTextEditor'
|
|
10
|
-
import {PortableTextEditorTester} from './PortableTextEditorTester'
|
|
11
|
-
|
|
12
|
-
type OnChange = ComponentProps<typeof PortableTextEditor>['onChange']
|
|
13
|
-
|
|
14
|
-
function block(
|
|
15
|
-
props?: Partial<Omit<PortableTextBlock, '_type'>>,
|
|
16
|
-
): PortableTextBlock {
|
|
17
|
-
return {
|
|
18
|
-
_type: 'block',
|
|
19
|
-
...(props ?? {}),
|
|
20
|
-
} as PortableTextBlock
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function span(
|
|
24
|
-
props?: Partial<Omit<PortableTextSpan, '_type'>>,
|
|
25
|
-
): PortableTextSpan {
|
|
26
|
-
return {
|
|
27
|
-
_type: 'span',
|
|
28
|
-
...(props ?? {}),
|
|
29
|
-
} as PortableTextSpan
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
describe('Feature: Self-solving', () => {
|
|
33
|
-
it('Scenario: Missing .markDefs and .marks are added after the editor is made dirty', async () => {
|
|
34
|
-
const schemaDefinition = defineSchema({decorators: [{name: 'strong'}]})
|
|
35
|
-
const editorRef: RefObject<PortableTextEditor | null> = createRef()
|
|
36
|
-
const onChange = vi.fn<OnChange>()
|
|
37
|
-
const initialValue = [
|
|
38
|
-
block({
|
|
39
|
-
_key: 'b1',
|
|
40
|
-
children: [
|
|
41
|
-
span({
|
|
42
|
-
_key: 's1',
|
|
43
|
-
text: 'foo',
|
|
44
|
-
}),
|
|
45
|
-
],
|
|
46
|
-
style: 'normal',
|
|
47
|
-
}),
|
|
48
|
-
]
|
|
49
|
-
const spanPatch: Patch = {
|
|
50
|
-
type: 'set',
|
|
51
|
-
path: [{_key: 'b1'}, 'children', {_key: 's1'}, 'marks'],
|
|
52
|
-
value: [],
|
|
53
|
-
origin: 'local',
|
|
54
|
-
}
|
|
55
|
-
const blockPatch: Patch = {
|
|
56
|
-
type: 'set',
|
|
57
|
-
path: [{_key: 'b1'}],
|
|
58
|
-
value: block({
|
|
59
|
-
_key: 'b1',
|
|
60
|
-
children: [
|
|
61
|
-
span({
|
|
62
|
-
_key: 's1',
|
|
63
|
-
text: 'foo',
|
|
64
|
-
marks: [],
|
|
65
|
-
}),
|
|
66
|
-
],
|
|
67
|
-
style: 'normal',
|
|
68
|
-
markDefs: [],
|
|
69
|
-
}) as JSONValue,
|
|
70
|
-
origin: 'local',
|
|
71
|
-
}
|
|
72
|
-
const strongPatch: Patch = {
|
|
73
|
-
type: 'set',
|
|
74
|
-
path: [{_key: 'b1'}, 'children', {_key: 's1'}, 'marks'],
|
|
75
|
-
value: ['strong'],
|
|
76
|
-
origin: 'local',
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
render(
|
|
80
|
-
<PortableTextEditorTester
|
|
81
|
-
ref={editorRef}
|
|
82
|
-
schemaDefinition={schemaDefinition}
|
|
83
|
-
keyGenerator={createTestKeyGenerator()}
|
|
84
|
-
value={initialValue}
|
|
85
|
-
onChange={onChange}
|
|
86
|
-
/>,
|
|
87
|
-
)
|
|
88
|
-
|
|
89
|
-
await waitFor(() => {
|
|
90
|
-
if (editorRef.current) {
|
|
91
|
-
expect(onChange).toHaveBeenNthCalledWith(1, {
|
|
92
|
-
type: 'value',
|
|
93
|
-
value: initialValue,
|
|
94
|
-
})
|
|
95
|
-
expect(onChange).toHaveBeenNthCalledWith(2, {
|
|
96
|
-
type: 'ready',
|
|
97
|
-
})
|
|
98
|
-
}
|
|
99
|
-
})
|
|
100
|
-
|
|
101
|
-
await waitFor(() => {
|
|
102
|
-
if (editorRef.current) {
|
|
103
|
-
PortableTextEditor.select(
|
|
104
|
-
editorRef.current,
|
|
105
|
-
getTextSelection(
|
|
106
|
-
{schema: compileSchema(schemaDefinition), value: initialValue},
|
|
107
|
-
'foo',
|
|
108
|
-
),
|
|
109
|
-
)
|
|
110
|
-
PortableTextEditor.toggleMark(editorRef.current, 'strong')
|
|
111
|
-
}
|
|
112
|
-
})
|
|
113
|
-
|
|
114
|
-
await waitFor(() => {
|
|
115
|
-
if (editorRef.current) {
|
|
116
|
-
expect(onChange).toHaveBeenNthCalledWith(3, {
|
|
117
|
-
type: 'selection',
|
|
118
|
-
selection: {
|
|
119
|
-
...getTextSelection(
|
|
120
|
-
{schema: compileSchema(schemaDefinition), value: initialValue},
|
|
121
|
-
'foo',
|
|
122
|
-
),
|
|
123
|
-
backward: false,
|
|
124
|
-
},
|
|
125
|
-
})
|
|
126
|
-
expect(onChange).toHaveBeenNthCalledWith(4, {
|
|
127
|
-
type: 'patch',
|
|
128
|
-
patch: spanPatch,
|
|
129
|
-
})
|
|
130
|
-
expect(onChange).toHaveBeenNthCalledWith(5, {
|
|
131
|
-
type: 'patch',
|
|
132
|
-
patch: blockPatch,
|
|
133
|
-
})
|
|
134
|
-
expect(onChange).toHaveBeenNthCalledWith(6, {
|
|
135
|
-
type: 'patch',
|
|
136
|
-
patch: strongPatch,
|
|
137
|
-
})
|
|
138
|
-
expect(onChange).toHaveBeenNthCalledWith(7, {
|
|
139
|
-
type: 'selection',
|
|
140
|
-
selection: {
|
|
141
|
-
...getTextSelection(
|
|
142
|
-
{schema: compileSchema(schemaDefinition), value: initialValue},
|
|
143
|
-
'foo',
|
|
144
|
-
),
|
|
145
|
-
backward: false,
|
|
146
|
-
},
|
|
147
|
-
})
|
|
148
|
-
expect(onChange).toHaveBeenNthCalledWith(8, {
|
|
149
|
-
type: 'mutation',
|
|
150
|
-
patches: [spanPatch, blockPatch, strongPatch],
|
|
151
|
-
snapshot: [
|
|
152
|
-
block({
|
|
153
|
-
_key: 'b1',
|
|
154
|
-
children: [
|
|
155
|
-
span({
|
|
156
|
-
_key: 's1',
|
|
157
|
-
text: 'foo',
|
|
158
|
-
marks: ['strong'],
|
|
159
|
-
}),
|
|
160
|
-
],
|
|
161
|
-
style: 'normal',
|
|
162
|
-
markDefs: [],
|
|
163
|
-
}),
|
|
164
|
-
],
|
|
165
|
-
value: [
|
|
166
|
-
block({
|
|
167
|
-
_key: 'b1',
|
|
168
|
-
children: [
|
|
169
|
-
span({
|
|
170
|
-
_key: 's1',
|
|
171
|
-
text: 'foo',
|
|
172
|
-
marks: ['strong'],
|
|
173
|
-
}),
|
|
174
|
-
],
|
|
175
|
-
style: 'normal',
|
|
176
|
-
markDefs: [],
|
|
177
|
-
}),
|
|
178
|
-
],
|
|
179
|
-
})
|
|
180
|
-
}
|
|
181
|
-
})
|
|
182
|
-
})
|
|
183
|
-
})
|
|
@@ -1,298 +0,0 @@
|
|
|
1
|
-
import {createTestKeyGenerator} from '@portabletext/test'
|
|
2
|
-
import {render, waitFor} from '@testing-library/react'
|
|
3
|
-
import {createRef, type RefObject} from 'react'
|
|
4
|
-
import {describe, expect, it, vi} from 'vitest'
|
|
5
|
-
import {PortableTextEditorTester} from '../../__tests__/PortableTextEditorTester'
|
|
6
|
-
import {PortableTextEditor} from '../../PortableTextEditor'
|
|
7
|
-
|
|
8
|
-
const initialValue = [
|
|
9
|
-
{
|
|
10
|
-
_key: 'a',
|
|
11
|
-
_type: 'block',
|
|
12
|
-
children: [
|
|
13
|
-
{
|
|
14
|
-
_key: 'a1',
|
|
15
|
-
_type: 'span',
|
|
16
|
-
marks: [],
|
|
17
|
-
text: 'Block A',
|
|
18
|
-
},
|
|
19
|
-
],
|
|
20
|
-
markDefs: [],
|
|
21
|
-
style: 'normal',
|
|
22
|
-
},
|
|
23
|
-
{
|
|
24
|
-
_key: 'b',
|
|
25
|
-
_type: 'block',
|
|
26
|
-
children: [
|
|
27
|
-
{
|
|
28
|
-
_key: 'b1',
|
|
29
|
-
_type: 'span',
|
|
30
|
-
marks: [],
|
|
31
|
-
text: 'Block B',
|
|
32
|
-
},
|
|
33
|
-
],
|
|
34
|
-
markDefs: [],
|
|
35
|
-
style: 'normal',
|
|
36
|
-
},
|
|
37
|
-
]
|
|
38
|
-
|
|
39
|
-
const initialSelection = {
|
|
40
|
-
focus: {path: [{_key: 'b'}, 'children', {_key: 'b1'}], offset: 7},
|
|
41
|
-
anchor: {path: [{_key: 'b'}, 'children', {_key: 'b1'}], offset: 7},
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
describe('plugin:withEditableAPI: .delete()', () => {
|
|
45
|
-
it('deletes block', async () => {
|
|
46
|
-
const editorRef: RefObject<PortableTextEditor | null> = createRef()
|
|
47
|
-
const onChange = vi.fn()
|
|
48
|
-
render(
|
|
49
|
-
<PortableTextEditorTester
|
|
50
|
-
onChange={onChange}
|
|
51
|
-
ref={editorRef}
|
|
52
|
-
value={initialValue}
|
|
53
|
-
keyGenerator={createTestKeyGenerator()}
|
|
54
|
-
/>,
|
|
55
|
-
)
|
|
56
|
-
|
|
57
|
-
await waitFor(() => {
|
|
58
|
-
if (editorRef.current) {
|
|
59
|
-
expect(onChange).toHaveBeenCalledWith({
|
|
60
|
-
type: 'value',
|
|
61
|
-
value: initialValue,
|
|
62
|
-
})
|
|
63
|
-
expect(onChange).toHaveBeenCalledWith({type: 'ready'})
|
|
64
|
-
}
|
|
65
|
-
})
|
|
66
|
-
|
|
67
|
-
await waitFor(() => {
|
|
68
|
-
if (editorRef.current) {
|
|
69
|
-
PortableTextEditor.focus(editorRef.current)
|
|
70
|
-
PortableTextEditor.select(editorRef.current, initialSelection)
|
|
71
|
-
PortableTextEditor.delete(
|
|
72
|
-
editorRef.current,
|
|
73
|
-
PortableTextEditor.getSelection(editorRef.current),
|
|
74
|
-
{mode: 'blocks'},
|
|
75
|
-
)
|
|
76
|
-
expect(PortableTextEditor.getValue(editorRef.current))
|
|
77
|
-
.toMatchInlineSnapshot(`
|
|
78
|
-
[
|
|
79
|
-
{
|
|
80
|
-
"_key": "a",
|
|
81
|
-
"_type": "block",
|
|
82
|
-
"children": [
|
|
83
|
-
{
|
|
84
|
-
"_key": "a1",
|
|
85
|
-
"_type": "span",
|
|
86
|
-
"marks": [],
|
|
87
|
-
"text": "Block A",
|
|
88
|
-
},
|
|
89
|
-
],
|
|
90
|
-
"markDefs": [],
|
|
91
|
-
"style": "normal",
|
|
92
|
-
},
|
|
93
|
-
]
|
|
94
|
-
`)
|
|
95
|
-
}
|
|
96
|
-
})
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
it('deletes all the blocks, but leaves a placeholder block', async () => {
|
|
100
|
-
const editorRef: RefObject<PortableTextEditor | null> = createRef()
|
|
101
|
-
const onChange = vi.fn()
|
|
102
|
-
render(
|
|
103
|
-
<PortableTextEditorTester
|
|
104
|
-
keyGenerator={createTestKeyGenerator()}
|
|
105
|
-
onChange={onChange}
|
|
106
|
-
ref={editorRef}
|
|
107
|
-
value={initialValue}
|
|
108
|
-
/>,
|
|
109
|
-
)
|
|
110
|
-
|
|
111
|
-
await waitFor(() => {
|
|
112
|
-
expect(onChange).toHaveBeenCalledWith({
|
|
113
|
-
type: 'value',
|
|
114
|
-
value: initialValue,
|
|
115
|
-
})
|
|
116
|
-
expect(onChange).toHaveBeenCalledWith({type: 'ready'})
|
|
117
|
-
})
|
|
118
|
-
|
|
119
|
-
await waitFor(() => {
|
|
120
|
-
if (editorRef.current) {
|
|
121
|
-
PortableTextEditor.delete(
|
|
122
|
-
editorRef.current,
|
|
123
|
-
{
|
|
124
|
-
focus: {path: [{_key: 'b'}, 'children', {_key: 'b1'}], offset: 7},
|
|
125
|
-
anchor: {path: [{_key: 'a'}, 'children', {_key: 'a1'}], offset: 0},
|
|
126
|
-
},
|
|
127
|
-
{mode: 'blocks'},
|
|
128
|
-
)
|
|
129
|
-
}
|
|
130
|
-
})
|
|
131
|
-
await waitFor(() => {
|
|
132
|
-
if (editorRef.current) {
|
|
133
|
-
// New keys here confirms that a placeholder block has been created
|
|
134
|
-
expect(PortableTextEditor.getValue(editorRef.current)).toEqual([
|
|
135
|
-
{
|
|
136
|
-
_key: 'k2',
|
|
137
|
-
_type: 'block',
|
|
138
|
-
children: [
|
|
139
|
-
{
|
|
140
|
-
_key: 'k3',
|
|
141
|
-
_type: 'span',
|
|
142
|
-
marks: [],
|
|
143
|
-
text: '',
|
|
144
|
-
},
|
|
145
|
-
],
|
|
146
|
-
markDefs: [],
|
|
147
|
-
style: 'normal',
|
|
148
|
-
},
|
|
149
|
-
])
|
|
150
|
-
}
|
|
151
|
-
})
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
it('deletes children', async () => {
|
|
155
|
-
const editorRef: RefObject<PortableTextEditor | null> = createRef()
|
|
156
|
-
const onChange = vi.fn()
|
|
157
|
-
|
|
158
|
-
render(
|
|
159
|
-
<PortableTextEditorTester
|
|
160
|
-
keyGenerator={createTestKeyGenerator()}
|
|
161
|
-
onChange={onChange}
|
|
162
|
-
ref={editorRef}
|
|
163
|
-
value={initialValue}
|
|
164
|
-
/>,
|
|
165
|
-
)
|
|
166
|
-
|
|
167
|
-
await waitFor(() => {
|
|
168
|
-
if (editorRef.current) {
|
|
169
|
-
expect(onChange).toHaveBeenCalledWith({
|
|
170
|
-
type: 'value',
|
|
171
|
-
value: initialValue,
|
|
172
|
-
})
|
|
173
|
-
expect(onChange).toHaveBeenCalledWith({type: 'ready'})
|
|
174
|
-
}
|
|
175
|
-
})
|
|
176
|
-
|
|
177
|
-
await waitFor(() => {
|
|
178
|
-
if (editorRef.current) {
|
|
179
|
-
PortableTextEditor.select(editorRef.current, {
|
|
180
|
-
focus: {path: [{_key: 'b'}, 'children', {_key: 'b1'}], offset: 5},
|
|
181
|
-
anchor: {path: [{_key: 'b'}, 'children', {_key: 'b1'}], offset: 7},
|
|
182
|
-
})
|
|
183
|
-
}
|
|
184
|
-
})
|
|
185
|
-
|
|
186
|
-
await waitFor(() => {
|
|
187
|
-
if (editorRef.current) {
|
|
188
|
-
PortableTextEditor.delete(
|
|
189
|
-
editorRef.current,
|
|
190
|
-
PortableTextEditor.getSelection(editorRef.current),
|
|
191
|
-
{mode: 'children'},
|
|
192
|
-
)
|
|
193
|
-
}
|
|
194
|
-
})
|
|
195
|
-
|
|
196
|
-
await waitFor(() => {
|
|
197
|
-
if (editorRef.current) {
|
|
198
|
-
expect(PortableTextEditor.getValue(editorRef.current))
|
|
199
|
-
.toMatchInlineSnapshot(`
|
|
200
|
-
[
|
|
201
|
-
{
|
|
202
|
-
"_key": "a",
|
|
203
|
-
"_type": "block",
|
|
204
|
-
"children": [
|
|
205
|
-
{
|
|
206
|
-
"_key": "a1",
|
|
207
|
-
"_type": "span",
|
|
208
|
-
"marks": [],
|
|
209
|
-
"text": "Block A",
|
|
210
|
-
},
|
|
211
|
-
],
|
|
212
|
-
"markDefs": [],
|
|
213
|
-
"style": "normal",
|
|
214
|
-
},
|
|
215
|
-
{
|
|
216
|
-
"_key": "b",
|
|
217
|
-
"_type": "block",
|
|
218
|
-
"children": [
|
|
219
|
-
{
|
|
220
|
-
"_key": "k2",
|
|
221
|
-
"_type": "span",
|
|
222
|
-
"marks": [],
|
|
223
|
-
"text": "",
|
|
224
|
-
},
|
|
225
|
-
],
|
|
226
|
-
"markDefs": [],
|
|
227
|
-
"style": "normal",
|
|
228
|
-
},
|
|
229
|
-
]
|
|
230
|
-
`)
|
|
231
|
-
}
|
|
232
|
-
})
|
|
233
|
-
})
|
|
234
|
-
|
|
235
|
-
it('deletes selected', async () => {
|
|
236
|
-
const editorRef: RefObject<PortableTextEditor | null> = createRef()
|
|
237
|
-
const onChange = vi.fn()
|
|
238
|
-
|
|
239
|
-
render(
|
|
240
|
-
<PortableTextEditorTester
|
|
241
|
-
onChange={onChange}
|
|
242
|
-
ref={editorRef}
|
|
243
|
-
value={initialValue}
|
|
244
|
-
keyGenerator={createTestKeyGenerator()}
|
|
245
|
-
/>,
|
|
246
|
-
)
|
|
247
|
-
|
|
248
|
-
await waitFor(() => {
|
|
249
|
-
if (editorRef.current) {
|
|
250
|
-
expect(onChange).toHaveBeenCalledWith({
|
|
251
|
-
type: 'value',
|
|
252
|
-
value: initialValue,
|
|
253
|
-
})
|
|
254
|
-
expect(onChange).toHaveBeenCalledWith({type: 'ready'})
|
|
255
|
-
}
|
|
256
|
-
})
|
|
257
|
-
|
|
258
|
-
await waitFor(() => {
|
|
259
|
-
if (editorRef.current) {
|
|
260
|
-
PortableTextEditor.select(editorRef.current, {
|
|
261
|
-
focus: {path: [{_key: 'b'}, 'children', {_key: 'b1'}], offset: 5},
|
|
262
|
-
anchor: {path: [{_key: 'a'}, 'children', {_key: 'a1'}], offset: 0},
|
|
263
|
-
})
|
|
264
|
-
}
|
|
265
|
-
})
|
|
266
|
-
|
|
267
|
-
await waitFor(() => {
|
|
268
|
-
if (editorRef.current) {
|
|
269
|
-
PortableTextEditor.delete(
|
|
270
|
-
editorRef.current,
|
|
271
|
-
PortableTextEditor.getSelection(editorRef.current),
|
|
272
|
-
{mode: 'selected'},
|
|
273
|
-
)
|
|
274
|
-
}
|
|
275
|
-
})
|
|
276
|
-
|
|
277
|
-
await waitFor(() => {
|
|
278
|
-
if (editorRef.current) {
|
|
279
|
-
expect(PortableTextEditor.getValue(editorRef.current)).toEqual([
|
|
280
|
-
{
|
|
281
|
-
_key: 'b',
|
|
282
|
-
_type: 'block',
|
|
283
|
-
children: [
|
|
284
|
-
{
|
|
285
|
-
_key: 'b1',
|
|
286
|
-
_type: 'span',
|
|
287
|
-
marks: [],
|
|
288
|
-
text: ' B',
|
|
289
|
-
},
|
|
290
|
-
],
|
|
291
|
-
markDefs: [],
|
|
292
|
-
style: 'normal',
|
|
293
|
-
},
|
|
294
|
-
])
|
|
295
|
-
}
|
|
296
|
-
})
|
|
297
|
-
})
|
|
298
|
-
})
|