@portabletext/editor 1.1.1 → 1.1.3
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/README.md +3 -0
- package/lib/index.d.mts +1668 -1
- package/lib/index.d.ts +1668 -1
- package/lib/index.esm.js +320 -172
- package/lib/index.esm.js.map +1 -1
- package/lib/index.js +320 -173
- package/lib/index.js.map +1 -1
- package/lib/index.mjs +320 -172
- package/lib/index.mjs.map +1 -1
- package/package.json +23 -23
- package/src/editor/Editable.tsx +32 -34
- package/src/editor/PortableTextEditor.tsx +23 -7
- package/src/editor/__tests__/PortableTextEditor.test.tsx +9 -9
- package/src/editor/__tests__/PortableTextEditorTester.tsx +2 -5
- package/src/editor/__tests__/RangeDecorations.test.tsx +2 -2
- package/src/editor/__tests__/handleClick.test.tsx +27 -7
- package/src/editor/__tests__/insert-block.test.tsx +4 -4
- package/src/editor/__tests__/pteWarningsSelfSolving.test.tsx +7 -7
- package/src/editor/__tests__/self-solving.test.tsx +176 -0
- package/src/editor/components/Leaf.tsx +28 -23
- package/src/editor/components/Synchronizer.tsx +60 -32
- package/src/editor/editor-machine.ts +195 -0
- package/src/editor/hooks/usePortableTextEditorSelection.tsx +12 -14
- package/src/editor/hooks/useSyncValue.test.tsx +9 -9
- package/src/editor/hooks/useSyncValue.ts +14 -13
- package/src/editor/plugins/__tests__/createWithInsertData.test.tsx +1 -1
- package/src/editor/plugins/__tests__/withEditableAPIDelete.test.tsx +28 -28
- package/src/editor/plugins/__tests__/withEditableAPIGetFragment.test.tsx +17 -17
- package/src/editor/plugins/__tests__/withEditableAPIInsert.test.tsx +8 -8
- package/src/editor/plugins/__tests__/withEditableAPISelectionsOverlapping.test.tsx +5 -5
- package/src/editor/plugins/__tests__/withPortableTextLists.test.tsx +2 -2
- package/src/editor/plugins/__tests__/withPortableTextMarkModel.test.tsx +46 -46
- package/src/editor/plugins/__tests__/withPortableTextSelections.test.tsx +22 -11
- package/src/editor/plugins/__tests__/withUndoRedo.test.tsx +9 -9
- package/src/editor/plugins/createWithEditableAPI.ts +5 -7
- package/src/editor/plugins/createWithInsertData.ts +4 -9
- package/src/editor/plugins/createWithObjectKeys.ts +7 -0
- package/src/editor/plugins/createWithPatches.ts +5 -6
- package/src/editor/plugins/createWithPortableTextBlockStyle.ts +10 -2
- package/src/editor/plugins/createWithPortableTextMarkModel.ts +40 -36
- package/src/editor/plugins/createWithPortableTextSelections.ts +4 -5
- package/src/editor/plugins/createWithSchemaTypes.ts +9 -0
- package/src/editor/plugins/index.ts +18 -8
- package/src/index.ts +9 -3
- package/src/utils/__tests__/dmpToOperations.test.ts +1 -1
- package/src/utils/__tests__/operationToPatches.test.ts +61 -61
- package/src/utils/__tests__/patchToOperations.test.ts +39 -39
- package/src/utils/__tests__/ranges.test.ts +1 -1
- package/src/utils/__tests__/valueNormalization.test.tsx +13 -2
- package/src/utils/__tests__/values.test.ts +17 -17
- package/src/utils/applyPatch.ts +4 -10
- package/src/utils/validateValue.ts +0 -22
- package/src/editor/__tests__/utils.ts +0 -44
|
@@ -7,19 +7,8 @@
|
|
|
7
7
|
import {isPortableTextBlock, isPortableTextSpan} from '@portabletext/toolkit'
|
|
8
8
|
import type {PortableTextObject} from '@sanity/types'
|
|
9
9
|
import {isEqual, uniq} from 'lodash'
|
|
10
|
-
import
|
|
11
|
-
import {
|
|
12
|
-
Editor,
|
|
13
|
-
Element,
|
|
14
|
-
Node,
|
|
15
|
-
Path,
|
|
16
|
-
Range,
|
|
17
|
-
Text,
|
|
18
|
-
Transforms,
|
|
19
|
-
type Descendant,
|
|
20
|
-
} from 'slate'
|
|
10
|
+
import {Editor, Element, Node, Path, Range, Text, Transforms} from 'slate'
|
|
21
11
|
import type {
|
|
22
|
-
EditorChange,
|
|
23
12
|
PortableTextMemberSchemaTypes,
|
|
24
13
|
PortableTextSlateEditor,
|
|
25
14
|
} from '../../types/editor'
|
|
@@ -27,12 +16,13 @@ import {debugWithName} from '../../utils/debug'
|
|
|
27
16
|
import {toPortableTextRange} from '../../utils/ranges'
|
|
28
17
|
import {isChangingRemotely} from '../../utils/withChanges'
|
|
29
18
|
import {isRedoing, isUndoing} from '../../utils/withUndoRedo'
|
|
19
|
+
import type {EditorActor} from '../editor-machine'
|
|
30
20
|
|
|
31
21
|
const debug = debugWithName('plugin:withPortableTextMarkModel')
|
|
32
22
|
|
|
33
23
|
export function createWithPortableTextMarkModel(
|
|
24
|
+
editorActor: EditorActor,
|
|
34
25
|
types: PortableTextMemberSchemaTypes,
|
|
35
|
-
change$: Subject<EditorChange>,
|
|
36
26
|
keyGenerator: () => string,
|
|
37
27
|
): (editor: PortableTextSlateEditor) => PortableTextSlateEditor {
|
|
38
28
|
return function withPortableTextMarkModel(editor: PortableTextSlateEditor) {
|
|
@@ -59,7 +49,7 @@ export function createWithPortableTextMarkModel(
|
|
|
59
49
|
editor.selection,
|
|
60
50
|
types,
|
|
61
51
|
)
|
|
62
|
-
|
|
52
|
+
editorActor.send({type: 'selection', selection: ptRange})
|
|
63
53
|
}
|
|
64
54
|
|
|
65
55
|
// Extend Slate's default normalization. Merge spans with same set of .marks when doing merge_node operations, and clean up markDefs / marks
|
|
@@ -82,10 +72,12 @@ export function createWithPortableTextMarkModel(
|
|
|
82
72
|
JSON.stringify(child, null, 2),
|
|
83
73
|
JSON.stringify(nextNode, null, 2),
|
|
84
74
|
)
|
|
75
|
+
editorActor.send({type: 'normalizing'})
|
|
85
76
|
Transforms.mergeNodes(editor, {
|
|
86
77
|
at: [childPath[0], childPath[1] + 1],
|
|
87
78
|
voids: true,
|
|
88
79
|
})
|
|
80
|
+
editorActor.send({type: 'done normalizing'})
|
|
89
81
|
return
|
|
90
82
|
}
|
|
91
83
|
}
|
|
@@ -96,7 +88,9 @@ export function createWithPortableTextMarkModel(
|
|
|
96
88
|
*/
|
|
97
89
|
if (editor.isTextBlock(node) && !Array.isArray(node.markDefs)) {
|
|
98
90
|
debug('Adding .markDefs to block node')
|
|
91
|
+
editorActor.send({type: 'normalizing'})
|
|
99
92
|
Transforms.setNodes(editor, {markDefs: []}, {at: path})
|
|
93
|
+
editorActor.send({type: 'done normalizing'})
|
|
100
94
|
return
|
|
101
95
|
}
|
|
102
96
|
|
|
@@ -105,7 +99,9 @@ export function createWithPortableTextMarkModel(
|
|
|
105
99
|
*/
|
|
106
100
|
if (editor.isTextSpan(node) && !Array.isArray(node.marks)) {
|
|
107
101
|
debug('Adding .marks to span node')
|
|
102
|
+
editorActor.send({type: 'normalizing'})
|
|
108
103
|
Transforms.setNodes(editor, {marks: []}, {at: path})
|
|
104
|
+
editorActor.send({type: 'done normalizing'})
|
|
109
105
|
return
|
|
110
106
|
}
|
|
111
107
|
|
|
@@ -123,11 +119,13 @@ export function createWithPortableTextMarkModel(
|
|
|
123
119
|
if (editor.isTextBlock(block)) {
|
|
124
120
|
if (node.text === '' && annotations && annotations.length > 0) {
|
|
125
121
|
debug('Removing annotations from empty span node')
|
|
122
|
+
editorActor.send({type: 'normalizing'})
|
|
126
123
|
Transforms.setNodes(
|
|
127
124
|
editor,
|
|
128
125
|
{marks: node.marks?.filter((mark) => decorators.includes(mark))},
|
|
129
126
|
{at: path},
|
|
130
127
|
)
|
|
128
|
+
editorActor.send({type: 'done normalizing'})
|
|
131
129
|
return
|
|
132
130
|
}
|
|
133
131
|
}
|
|
@@ -151,6 +149,7 @@ export function createWithPortableTextMarkModel(
|
|
|
151
149
|
|
|
152
150
|
if (orphanedAnnotations.length > 0) {
|
|
153
151
|
debug('Removing orphaned annotations from span node')
|
|
152
|
+
editorActor.send({type: 'normalizing'})
|
|
154
153
|
Transforms.setNodes(
|
|
155
154
|
editor,
|
|
156
155
|
{
|
|
@@ -160,6 +159,7 @@ export function createWithPortableTextMarkModel(
|
|
|
160
159
|
},
|
|
161
160
|
{at: childPath},
|
|
162
161
|
)
|
|
162
|
+
editorActor.send({type: 'done normalizing'})
|
|
163
163
|
return
|
|
164
164
|
}
|
|
165
165
|
}
|
|
@@ -187,6 +187,7 @@ export function createWithPortableTextMarkModel(
|
|
|
187
187
|
|
|
188
188
|
if (orphanedAnnotations.length > 0) {
|
|
189
189
|
debug('Removing orphaned annotations from span node')
|
|
190
|
+
editorActor.send({type: 'normalizing'})
|
|
190
191
|
Transforms.setNodes(
|
|
191
192
|
editor,
|
|
192
193
|
{
|
|
@@ -196,6 +197,7 @@ export function createWithPortableTextMarkModel(
|
|
|
196
197
|
},
|
|
197
198
|
{at: path},
|
|
198
199
|
)
|
|
200
|
+
editorActor.send({type: 'done normalizing'})
|
|
199
201
|
return
|
|
200
202
|
}
|
|
201
203
|
}
|
|
@@ -216,7 +218,10 @@ export function createWithPortableTextMarkModel(
|
|
|
216
218
|
|
|
217
219
|
if (markDefs.length !== newMarkDefs.length) {
|
|
218
220
|
debug('Removing duplicate markDefs')
|
|
221
|
+
editorActor.send({type: 'normalizing'})
|
|
219
222
|
Transforms.setNodes(editor, {markDefs: newMarkDefs}, {at: path})
|
|
223
|
+
editorActor.send({type: 'done normalizing'})
|
|
224
|
+
return
|
|
220
225
|
}
|
|
221
226
|
}
|
|
222
227
|
|
|
@@ -241,6 +246,7 @@ export function createWithPortableTextMarkModel(
|
|
|
241
246
|
})
|
|
242
247
|
if (node.markDefs && !isEqual(newMarkDefs, node.markDefs)) {
|
|
243
248
|
debug('Removing markDef not in use')
|
|
249
|
+
editorActor.send({type: 'normalizing'})
|
|
244
250
|
Transforms.setNodes(
|
|
245
251
|
editor,
|
|
246
252
|
{
|
|
@@ -248,6 +254,7 @@ export function createWithPortableTextMarkModel(
|
|
|
248
254
|
},
|
|
249
255
|
{at: path},
|
|
250
256
|
)
|
|
257
|
+
editorActor.send({type: 'done normalizing'})
|
|
251
258
|
return
|
|
252
259
|
}
|
|
253
260
|
}
|
|
@@ -274,41 +281,38 @@ export function createWithPortableTextMarkModel(
|
|
|
274
281
|
return
|
|
275
282
|
}
|
|
276
283
|
|
|
277
|
-
// Special hook before inserting text at the end of an annotation.
|
|
278
284
|
if (op.type === 'insert_text') {
|
|
279
285
|
const {selection} = editor
|
|
280
|
-
|
|
281
|
-
selection
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
) {
|
|
287
|
-
const [node] = Array.from(
|
|
286
|
+
const collapsedSelection = selection
|
|
287
|
+
? Range.isCollapsed(selection)
|
|
288
|
+
: false
|
|
289
|
+
|
|
290
|
+
if (selection && collapsedSelection) {
|
|
291
|
+
const [span] = Array.from(
|
|
288
292
|
Editor.nodes(editor, {
|
|
289
293
|
mode: 'lowest',
|
|
290
294
|
at: selection.focus,
|
|
291
|
-
match: (n) =>
|
|
292
|
-
(n as unknown as Descendant)._type === types.span.name,
|
|
295
|
+
match: (n) => editor.isTextSpan(n),
|
|
293
296
|
voids: false,
|
|
294
297
|
}),
|
|
295
|
-
)[0]
|
|
298
|
+
)[0]
|
|
299
|
+
const marks = span.marks ?? []
|
|
300
|
+
const marksWithoutAnnotations = marks.filter((mark) =>
|
|
301
|
+
decorators.includes(mark),
|
|
302
|
+
)
|
|
303
|
+
const spanHasAnnotations =
|
|
304
|
+
marks.length > marksWithoutAnnotations.length
|
|
305
|
+
|
|
296
306
|
if (
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
node.marks.length > 0
|
|
307
|
+
spanHasAnnotations &&
|
|
308
|
+
(selection.anchor.offset === 0 ||
|
|
309
|
+
span.text.length === selection.focus.offset)
|
|
301
310
|
) {
|
|
302
|
-
const marksWithoutAnnotationMarks: string[] = (
|
|
303
|
-
{
|
|
304
|
-
...(Editor.marks(editor) || {}),
|
|
305
|
-
}.marks || []
|
|
306
|
-
).filter((mark) => decorators.includes(mark))
|
|
307
311
|
Transforms.insertNodes(editor, {
|
|
308
312
|
_type: 'span',
|
|
309
313
|
_key: keyGenerator(),
|
|
310
314
|
text: op.text,
|
|
311
|
-
marks:
|
|
315
|
+
marks: marksWithoutAnnotations,
|
|
312
316
|
})
|
|
313
317
|
debug('Inserting text at end of annotation')
|
|
314
318
|
return
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import type {Subject} from 'rxjs'
|
|
2
1
|
import type {BaseRange} from 'slate'
|
|
3
2
|
import type {
|
|
4
|
-
EditorChange,
|
|
5
3
|
EditorSelection,
|
|
6
4
|
PortableTextMemberSchemaTypes,
|
|
7
5
|
PortableTextSlateEditor,
|
|
@@ -12,13 +10,14 @@ import {
|
|
|
12
10
|
type ObjectWithKeyAndType,
|
|
13
11
|
} from '../../utils/ranges'
|
|
14
12
|
import {SLATE_TO_PORTABLE_TEXT_RANGE} from '../../utils/weakMaps'
|
|
13
|
+
import type {EditorActor} from '../editor-machine'
|
|
15
14
|
|
|
16
15
|
const debug = debugWithName('plugin:withPortableTextSelections')
|
|
17
16
|
const debugVerbose = debug.enabled && false
|
|
18
17
|
|
|
19
18
|
// This plugin will make sure that we emit a PT selection whenever the editor has changed.
|
|
20
19
|
export function createWithPortableTextSelections(
|
|
21
|
-
|
|
20
|
+
editorActor: EditorActor,
|
|
22
21
|
types: PortableTextMemberSchemaTypes,
|
|
23
22
|
): (editor: PortableTextSlateEditor) => PortableTextSlateEditor {
|
|
24
23
|
let prevSelection: BaseRange | null = null
|
|
@@ -46,9 +45,9 @@ export function createWithPortableTextSelections(
|
|
|
46
45
|
)
|
|
47
46
|
}
|
|
48
47
|
if (ptRange) {
|
|
49
|
-
|
|
48
|
+
editorActor.send({type: 'selection', selection: ptRange})
|
|
50
49
|
} else {
|
|
51
|
-
|
|
50
|
+
editorActor.send({type: 'selection', selection: null})
|
|
52
51
|
}
|
|
53
52
|
}
|
|
54
53
|
prevSelection = editor.selection
|
|
@@ -12,6 +12,7 @@ import type {
|
|
|
12
12
|
PortableTextSlateEditor,
|
|
13
13
|
} from '../../types/editor'
|
|
14
14
|
import {debugWithName} from '../../utils/debug'
|
|
15
|
+
import type {EditorActor} from '../editor-machine'
|
|
15
16
|
|
|
16
17
|
const debug = debugWithName('plugin:withSchemaTypes')
|
|
17
18
|
/**
|
|
@@ -19,9 +20,11 @@ const debug = debugWithName('plugin:withSchemaTypes')
|
|
|
19
20
|
*
|
|
20
21
|
*/
|
|
21
22
|
export function createWithSchemaTypes({
|
|
23
|
+
editorActor,
|
|
22
24
|
schemaTypes,
|
|
23
25
|
keyGenerator,
|
|
24
26
|
}: {
|
|
27
|
+
editorActor: EditorActor
|
|
25
28
|
schemaTypes: PortableTextMemberSchemaTypes
|
|
26
29
|
keyGenerator: () => string
|
|
27
30
|
}) {
|
|
@@ -71,18 +74,24 @@ export function createWithSchemaTypes({
|
|
|
71
74
|
debug('Setting span type on text node without a type')
|
|
72
75
|
const span = node as PortableTextSpan
|
|
73
76
|
const key = span._key || keyGenerator()
|
|
77
|
+
editorActor.send({type: 'normalizing'})
|
|
74
78
|
Transforms.setNodes(
|
|
75
79
|
editor,
|
|
76
80
|
{...span, _type: schemaTypes.span.name, _key: key},
|
|
77
81
|
{at: path},
|
|
78
82
|
)
|
|
83
|
+
editorActor.send({type: 'done normalizing'})
|
|
84
|
+
return
|
|
79
85
|
}
|
|
80
86
|
|
|
81
87
|
// catches cases when the children are missing keys but excludes it when the normalize is running the node as the editor object
|
|
82
88
|
if (node._key === undefined && (path.length === 1 || path.length === 2)) {
|
|
83
89
|
debug('Setting missing key on child node without a key')
|
|
84
90
|
const key = keyGenerator()
|
|
91
|
+
editorActor.send({type: 'normalizing'})
|
|
85
92
|
Transforms.setNodes(editor, {_key: key}, {at: path})
|
|
93
|
+
editorActor.send({type: 'done normalizing'})
|
|
94
|
+
return
|
|
86
95
|
}
|
|
87
96
|
|
|
88
97
|
normalizeNode(entry)
|
|
@@ -49,7 +49,7 @@ export const withPlugins = <T extends Editor>(
|
|
|
49
49
|
const e = editor as T & PortableTextSlateEditor
|
|
50
50
|
const {keyGenerator, portableTextEditor, patches$, readOnly, maxBlocks} =
|
|
51
51
|
options
|
|
52
|
-
const {
|
|
52
|
+
const {editorActor, schemaTypes} = portableTextEditor
|
|
53
53
|
e.subscriptions = []
|
|
54
54
|
if (e.destroy) {
|
|
55
55
|
e.destroy()
|
|
@@ -63,15 +63,23 @@ export const withPlugins = <T extends Editor>(
|
|
|
63
63
|
})
|
|
64
64
|
}
|
|
65
65
|
const operationToPatches = createOperationToPatches(schemaTypes)
|
|
66
|
-
const withObjectKeys = createWithObjectKeys(
|
|
67
|
-
|
|
66
|
+
const withObjectKeys = createWithObjectKeys(
|
|
67
|
+
editorActor,
|
|
68
|
+
schemaTypes,
|
|
69
|
+
keyGenerator,
|
|
70
|
+
)
|
|
71
|
+
const withSchemaTypes = createWithSchemaTypes({
|
|
72
|
+
editorActor,
|
|
73
|
+
schemaTypes,
|
|
74
|
+
keyGenerator,
|
|
75
|
+
})
|
|
68
76
|
const withEditableAPI = createWithEditableAPI(
|
|
69
77
|
portableTextEditor,
|
|
70
78
|
schemaTypes,
|
|
71
79
|
keyGenerator,
|
|
72
80
|
)
|
|
73
81
|
const withPatches = createWithPatches({
|
|
74
|
-
|
|
82
|
+
editorActor,
|
|
75
83
|
keyGenerator,
|
|
76
84
|
patches$,
|
|
77
85
|
patchFunctions: operationToPatches,
|
|
@@ -86,12 +94,14 @@ export const withPlugins = <T extends Editor>(
|
|
|
86
94
|
blockSchemaType: schemaTypes.block,
|
|
87
95
|
})
|
|
88
96
|
const withPortableTextMarkModel = createWithPortableTextMarkModel(
|
|
97
|
+
editorActor,
|
|
89
98
|
schemaTypes,
|
|
90
|
-
change$,
|
|
91
99
|
keyGenerator,
|
|
92
100
|
)
|
|
93
|
-
const withPortableTextBlockStyle =
|
|
94
|
-
|
|
101
|
+
const withPortableTextBlockStyle = createWithPortableTextBlockStyle(
|
|
102
|
+
editorActor,
|
|
103
|
+
schemaTypes,
|
|
104
|
+
)
|
|
95
105
|
|
|
96
106
|
const withPlaceholderBlock = createWithPlaceholderBlock()
|
|
97
107
|
|
|
@@ -103,7 +113,7 @@ export const withPlugins = <T extends Editor>(
|
|
|
103
113
|
portableTextEditor,
|
|
104
114
|
})
|
|
105
115
|
const withPortableTextSelections = createWithPortableTextSelections(
|
|
106
|
-
|
|
116
|
+
editorActor,
|
|
107
117
|
schemaTypes,
|
|
108
118
|
)
|
|
109
119
|
|
package/src/index.ts
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
|
-
export type
|
|
1
|
+
export {type Patch} from '@portabletext/patches'
|
|
2
2
|
export {PortableTextEditable} from './editor/Editable'
|
|
3
|
+
export type {PortableTextEditableProps} from './editor/Editable'
|
|
4
|
+
export {
|
|
5
|
+
editorMachine,
|
|
6
|
+
type EditorActor,
|
|
7
|
+
type MutationEvent,
|
|
8
|
+
type PatchEvent,
|
|
9
|
+
} from './editor/editor-machine'
|
|
3
10
|
export {usePortableTextEditor} from './editor/hooks/usePortableTextEditor'
|
|
4
11
|
export {defaultKeyGenerator as keyGenerator} from './editor/hooks/usePortableTextEditorKeyGenerator'
|
|
5
12
|
export {usePortableTextEditorSelection} from './editor/hooks/usePortableTextEditorSelection'
|
|
6
|
-
export type {PortableTextEditorProps} from './editor/PortableTextEditor'
|
|
7
13
|
export {PortableTextEditor} from './editor/PortableTextEditor'
|
|
14
|
+
export type {PortableTextEditorProps} from './editor/PortableTextEditor'
|
|
8
15
|
export * from './types/editor'
|
|
9
16
|
export * from './types/options'
|
|
10
|
-
export {type Patch} from '@portabletext/patches'
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import {describe, expect, test} from '@jest/globals'
|
|
2
1
|
import type {DiffMatchPatch} from '@portabletext/patches'
|
|
3
2
|
import {makeDiff, makePatches, stringifyPatches} from '@sanity/diff-match-patch'
|
|
4
3
|
import {
|
|
@@ -10,6 +9,7 @@ import {
|
|
|
10
9
|
type PortableTextTextBlock,
|
|
11
10
|
} from '@sanity/types'
|
|
12
11
|
import type {Descendant, Operation} from 'slate'
|
|
12
|
+
import {describe, expect, test} from 'vitest'
|
|
13
13
|
import type {PortableTextSlateEditor} from '../../types/editor'
|
|
14
14
|
import {diffMatchPatch} from '../applyPatch'
|
|
15
15
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import {beforeEach, describe, expect, it} from '@jest/globals'
|
|
2
1
|
import type {PortableTextTextBlock} from '@sanity/types'
|
|
3
2
|
import {createEditor, type Descendant} from 'slate'
|
|
3
|
+
import {beforeEach, describe, expect, it} from 'vitest'
|
|
4
4
|
import {PortableTextEditor, type PortableTextEditorProps} from '../..'
|
|
5
5
|
import {schemaType} from '../../editor/__tests__/PortableTextEditorTester'
|
|
6
6
|
import {defaultKeyGenerator} from '../../editor/hooks/usePortableTextEditorKeyGenerator'
|
|
@@ -61,34 +61,34 @@ describe('operationToPatches', () => {
|
|
|
61
61
|
createDefaultValue(),
|
|
62
62
|
),
|
|
63
63
|
).toMatchInlineSnapshot(`
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
"items":
|
|
67
|
-
|
|
64
|
+
[
|
|
65
|
+
{
|
|
66
|
+
"items": [
|
|
67
|
+
{
|
|
68
68
|
"_key": "773866318fa8",
|
|
69
69
|
"_type": "someObject",
|
|
70
70
|
"title": "The Object",
|
|
71
71
|
},
|
|
72
72
|
],
|
|
73
|
-
"path":
|
|
74
|
-
|
|
73
|
+
"path": [
|
|
74
|
+
{
|
|
75
75
|
"_key": "1f2e64b47787",
|
|
76
76
|
},
|
|
77
77
|
"children",
|
|
78
|
-
|
|
78
|
+
{
|
|
79
79
|
"_key": "c130395c640c",
|
|
80
80
|
},
|
|
81
81
|
],
|
|
82
82
|
"position": "after",
|
|
83
83
|
"type": "insert",
|
|
84
84
|
},
|
|
85
|
-
|
|
86
|
-
"path":
|
|
87
|
-
|
|
85
|
+
{
|
|
86
|
+
"path": [
|
|
87
|
+
{
|
|
88
88
|
"_key": "1f2e64b47787",
|
|
89
89
|
},
|
|
90
90
|
"children",
|
|
91
|
-
|
|
91
|
+
{
|
|
92
92
|
"_key": "c130395c640c",
|
|
93
93
|
},
|
|
94
94
|
"text",
|
|
@@ -118,17 +118,17 @@ describe('operationToPatches', () => {
|
|
|
118
118
|
createDefaultValue(),
|
|
119
119
|
),
|
|
120
120
|
).toMatchInlineSnapshot(`
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
"items":
|
|
124
|
-
|
|
121
|
+
[
|
|
122
|
+
{
|
|
123
|
+
"items": [
|
|
124
|
+
{
|
|
125
125
|
"_key": "c130395c640c",
|
|
126
126
|
"_type": "someObject",
|
|
127
127
|
"title": "The Object",
|
|
128
128
|
},
|
|
129
129
|
],
|
|
130
|
-
"path":
|
|
131
|
-
|
|
130
|
+
"path": [
|
|
131
|
+
{
|
|
132
132
|
"_key": "1f2e64b47787",
|
|
133
133
|
},
|
|
134
134
|
],
|
|
@@ -160,20 +160,20 @@ describe('operationToPatches', () => {
|
|
|
160
160
|
[],
|
|
161
161
|
),
|
|
162
162
|
).toMatchInlineSnapshot(`
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
"path":
|
|
163
|
+
[
|
|
164
|
+
{
|
|
165
|
+
"path": [],
|
|
166
166
|
"type": "setIfMissing",
|
|
167
|
-
"value":
|
|
167
|
+
"value": [],
|
|
168
168
|
},
|
|
169
|
-
|
|
170
|
-
"items":
|
|
171
|
-
|
|
169
|
+
{
|
|
170
|
+
"items": [
|
|
171
|
+
{
|
|
172
172
|
"_key": "c130395c640c",
|
|
173
173
|
"_type": "someObject",
|
|
174
174
|
},
|
|
175
175
|
],
|
|
176
|
-
"path":
|
|
176
|
+
"path": [
|
|
177
177
|
0,
|
|
178
178
|
],
|
|
179
179
|
"position": "before",
|
|
@@ -202,21 +202,21 @@ describe('operationToPatches', () => {
|
|
|
202
202
|
createDefaultValue(),
|
|
203
203
|
),
|
|
204
204
|
).toMatchInlineSnapshot(`
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
"items":
|
|
208
|
-
|
|
205
|
+
[
|
|
206
|
+
{
|
|
207
|
+
"items": [
|
|
208
|
+
{
|
|
209
209
|
"_key": "c130395c640c",
|
|
210
210
|
"_type": "someObject",
|
|
211
211
|
"title": "The Object",
|
|
212
212
|
},
|
|
213
213
|
],
|
|
214
|
-
"path":
|
|
215
|
-
|
|
214
|
+
"path": [
|
|
215
|
+
{
|
|
216
216
|
"_key": "1f2e64b47787",
|
|
217
217
|
},
|
|
218
218
|
"children",
|
|
219
|
-
|
|
219
|
+
{
|
|
220
220
|
"_key": "fd9b4a4e6c0b",
|
|
221
221
|
},
|
|
222
222
|
],
|
|
@@ -243,14 +243,14 @@ describe('operationToPatches', () => {
|
|
|
243
243
|
createDefaultValue(),
|
|
244
244
|
),
|
|
245
245
|
).toMatchInlineSnapshot(`
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
"path":
|
|
249
|
-
|
|
246
|
+
[
|
|
247
|
+
{
|
|
248
|
+
"path": [
|
|
249
|
+
{
|
|
250
250
|
"_key": "1f2e64b47787",
|
|
251
251
|
},
|
|
252
252
|
"children",
|
|
253
|
-
|
|
253
|
+
{
|
|
254
254
|
"_key": "fd9b4a4e6c0b",
|
|
255
255
|
},
|
|
256
256
|
"text",
|
|
@@ -280,14 +280,14 @@ describe('operationToPatches', () => {
|
|
|
280
280
|
before,
|
|
281
281
|
),
|
|
282
282
|
).toMatchInlineSnapshot(`
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
"path":
|
|
286
|
-
|
|
283
|
+
[
|
|
284
|
+
{
|
|
285
|
+
"path": [
|
|
286
|
+
{
|
|
287
287
|
"_key": "1f2e64b47787",
|
|
288
288
|
},
|
|
289
289
|
"children",
|
|
290
|
-
|
|
290
|
+
{
|
|
291
291
|
"_key": "fd9b4a4e6c0b",
|
|
292
292
|
},
|
|
293
293
|
"text",
|
|
@@ -320,14 +320,14 @@ describe('operationToPatches', () => {
|
|
|
320
320
|
createDefaultValue(),
|
|
321
321
|
),
|
|
322
322
|
).toMatchInlineSnapshot(`
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
"path":
|
|
326
|
-
|
|
323
|
+
[
|
|
324
|
+
{
|
|
325
|
+
"path": [
|
|
326
|
+
{
|
|
327
327
|
"_key": "1f2e64b47787",
|
|
328
328
|
},
|
|
329
329
|
"children",
|
|
330
|
-
|
|
330
|
+
{
|
|
331
331
|
"_key": "773866318fa8",
|
|
332
332
|
},
|
|
333
333
|
],
|
|
@@ -351,10 +351,10 @@ describe('operationToPatches', () => {
|
|
|
351
351
|
val,
|
|
352
352
|
),
|
|
353
353
|
).toMatchInlineSnapshot(`
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
"path":
|
|
357
|
-
|
|
354
|
+
[
|
|
355
|
+
{
|
|
356
|
+
"path": [
|
|
357
|
+
{
|
|
358
358
|
"_key": "1f2e64b47787",
|
|
359
359
|
},
|
|
360
360
|
],
|
|
@@ -389,14 +389,14 @@ describe('operationToPatches', () => {
|
|
|
389
389
|
val,
|
|
390
390
|
),
|
|
391
391
|
).toMatchInlineSnapshot(`
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
"path":
|
|
395
|
-
|
|
392
|
+
[
|
|
393
|
+
{
|
|
394
|
+
"path": [
|
|
395
|
+
{
|
|
396
396
|
"_key": "1f2e64b47787",
|
|
397
397
|
},
|
|
398
398
|
"children",
|
|
399
|
-
|
|
399
|
+
{
|
|
400
400
|
"_key": "fd9b4a4e6c0b",
|
|
401
401
|
},
|
|
402
402
|
"text",
|
|
@@ -404,13 +404,13 @@ describe('operationToPatches', () => {
|
|
|
404
404
|
"type": "set",
|
|
405
405
|
"value": "1234",
|
|
406
406
|
},
|
|
407
|
-
|
|
408
|
-
"path":
|
|
409
|
-
|
|
407
|
+
{
|
|
408
|
+
"path": [
|
|
409
|
+
{
|
|
410
410
|
"_key": "1f2e64b47787",
|
|
411
411
|
},
|
|
412
412
|
"children",
|
|
413
|
-
|
|
413
|
+
{
|
|
414
414
|
"_key": "r4wr323432",
|
|
415
415
|
},
|
|
416
416
|
],
|