@portabletext/editor 1.50.2 → 1.50.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.
@@ -1,489 +0,0 @@
1
- import {
2
- diffMatchPatch,
3
- insert,
4
- set,
5
- setIfMissing,
6
- unset,
7
- type InsertPosition,
8
- type Patch,
9
- } from '@portabletext/patches'
10
- import type {Path, PortableTextSpan, PortableTextTextBlock} from '@sanity/types'
11
- import {get, isUndefined, omitBy} from 'lodash'
12
- import {
13
- Text,
14
- type Descendant,
15
- type InsertNodeOperation,
16
- type InsertTextOperation,
17
- type MergeNodeOperation,
18
- type MoveNodeOperation,
19
- type RemoveNodeOperation,
20
- type RemoveTextOperation,
21
- type SetNodeOperation,
22
- type SplitNodeOperation,
23
- } from 'slate'
24
- import type {EditorActor} from '../editor/editor-machine'
25
- import type {PatchFunctions} from '../editor/plugins/createWithPatches'
26
- import type {PortableTextSlateEditor} from '../types/editor'
27
- import {debugWithName} from './debug'
28
- import {fromSlateValue} from './values'
29
-
30
- const debug = debugWithName('operationToPatches')
31
-
32
- export function createOperationToPatches(
33
- editorActor: EditorActor,
34
- ): PatchFunctions {
35
- const textBlockName = editorActor.getSnapshot().context.schema.block.name
36
- function insertTextPatch(
37
- editor: PortableTextSlateEditor,
38
- operation: InsertTextOperation,
39
- beforeValue: Descendant[],
40
- ) {
41
- if (debug.enabled) {
42
- debug('Operation', JSON.stringify(operation, null, 2))
43
- }
44
- const block =
45
- editor.isTextBlock(editor.children[operation.path[0]]) &&
46
- editor.children[operation.path[0]]
47
- if (!block) {
48
- throw new Error('Could not find block')
49
- }
50
- const textChild =
51
- editor.isTextBlock(block) &&
52
- editor.isTextSpan(block.children[operation.path[1]]) &&
53
- (block.children[operation.path[1]] as PortableTextSpan)
54
- if (!textChild) {
55
- throw new Error('Could not find child')
56
- }
57
- const path: Path = [
58
- {_key: block._key},
59
- 'children',
60
- {_key: textChild._key},
61
- 'text',
62
- ]
63
- const prevBlock = beforeValue[operation.path[0]]
64
- const prevChild =
65
- editor.isTextBlock(prevBlock) && prevBlock.children[operation.path[1]]
66
- const prevText = editor.isTextSpan(prevChild) ? prevChild.text : ''
67
- const patch = diffMatchPatch(prevText, textChild.text, path)
68
- return patch.value.length ? [patch] : []
69
- }
70
-
71
- function removeTextPatch(
72
- editor: PortableTextSlateEditor,
73
- operation: RemoveTextOperation,
74
- beforeValue: Descendant[],
75
- ) {
76
- const block = editor && editor.children[operation.path[0]]
77
- if (!block) {
78
- throw new Error('Could not find block')
79
- }
80
- const child =
81
- (editor.isTextBlock(block) && block.children[operation.path[1]]) ||
82
- undefined
83
- const textChild: PortableTextSpan | undefined = editor.isTextSpan(child)
84
- ? child
85
- : undefined
86
- if (child && !textChild) {
87
- throw new Error('Expected span')
88
- }
89
- if (!textChild) {
90
- throw new Error('Could not find child')
91
- }
92
- const path: Path = [
93
- {_key: block._key},
94
- 'children',
95
- {_key: textChild._key},
96
- 'text',
97
- ]
98
- const beforeBlock = beforeValue[operation.path[0]]
99
- const prevTextChild =
100
- editor.isTextBlock(beforeBlock) && beforeBlock.children[operation.path[1]]
101
- const prevText = editor.isTextSpan(prevTextChild) && prevTextChild.text
102
- const patch = diffMatchPatch(prevText || '', textChild.text, path)
103
- return patch.value ? [patch] : []
104
- }
105
-
106
- function setNodePatch(
107
- editor: PortableTextSlateEditor,
108
- operation: SetNodeOperation,
109
- ) {
110
- if (operation.path.length === 1) {
111
- const block = editor.children[operation.path[0]]
112
- if (typeof block._key !== 'string') {
113
- throw new Error('Expected block to have a _key')
114
- }
115
- const setNode = omitBy(
116
- {...editor.children[operation.path[0]], ...operation.newProperties},
117
- isUndefined,
118
- ) as unknown as Descendant
119
- return [
120
- set(fromSlateValue([setNode], textBlockName)[0], [{_key: block._key}]),
121
- ]
122
- } else if (operation.path.length === 2) {
123
- const block = editor.children[operation.path[0]]
124
- if (editor.isTextBlock(block)) {
125
- const child = block.children[operation.path[1]]
126
- if (child) {
127
- const blockKey = block._key
128
- const childKey = child._key
129
- const patches: Patch[] = []
130
- const keys = Object.keys(operation.newProperties)
131
- keys.forEach((keyName) => {
132
- // Special case for setting _key on a child. We have to target it by index and not the _key.
133
- if (keys.length === 1 && keyName === '_key') {
134
- const val = get(operation.newProperties, keyName)
135
- patches.push(
136
- set(val, [
137
- {_key: blockKey},
138
- 'children',
139
- block.children.indexOf(child),
140
- keyName,
141
- ]),
142
- )
143
- } else {
144
- const val = get(operation.newProperties, keyName)
145
- patches.push(
146
- set(val, [
147
- {_key: blockKey},
148
- 'children',
149
- {_key: childKey},
150
- keyName,
151
- ]),
152
- )
153
- }
154
- })
155
- return patches
156
- }
157
- throw new Error('Could not find a valid child')
158
- }
159
- throw new Error('Could not find a valid block')
160
- } else {
161
- throw new Error(
162
- `Unexpected path encountered: ${JSON.stringify(operation.path)}`,
163
- )
164
- }
165
- }
166
-
167
- function insertNodePatch(
168
- editor: PortableTextSlateEditor,
169
- operation: InsertNodeOperation,
170
- beforeValue: Descendant[],
171
- ): Patch[] {
172
- const block = beforeValue[operation.path[0]]
173
- const isTextBlock = editor.isTextBlock(block)
174
- if (operation.path.length === 1) {
175
- const position = operation.path[0] === 0 ? 'before' : 'after'
176
- const beforeBlock = beforeValue[operation.path[0] - 1]
177
- const targetKey =
178
- operation.path[0] === 0 ? block?._key : beforeBlock?._key
179
- if (targetKey) {
180
- return [
181
- insert(
182
- [fromSlateValue([operation.node as Descendant], textBlockName)[0]],
183
- position,
184
- [{_key: targetKey}],
185
- ),
186
- ]
187
- }
188
- return [
189
- setIfMissing(beforeValue, []),
190
- insert(
191
- [fromSlateValue([operation.node as Descendant], textBlockName)[0]],
192
- 'before',
193
- [operation.path[0]],
194
- ),
195
- ]
196
- } else if (
197
- isTextBlock &&
198
- operation.path.length === 2 &&
199
- editor.children[operation.path[0]]
200
- ) {
201
- const position =
202
- block.children.length === 0 || !block.children[operation.path[1] - 1]
203
- ? 'before'
204
- : 'after'
205
- const node = {...operation.node} as Descendant
206
- if (!node._type && Text.isText(node)) {
207
- node._type = 'span'
208
- node.marks = []
209
- }
210
- const blk = fromSlateValue(
211
- [
212
- {
213
- _key: 'bogus',
214
- _type: textBlockName,
215
- children: [node],
216
- },
217
- ],
218
- textBlockName,
219
- )[0] as PortableTextTextBlock
220
- const child = blk.children[0]
221
- return [
222
- insert([child], position, [
223
- {_key: block._key},
224
- 'children',
225
- block.children.length <= 1 || !block.children[operation.path[1] - 1]
226
- ? 0
227
- : {_key: block.children[operation.path[1] - 1]._key},
228
- ]),
229
- ]
230
- }
231
- debug(
232
- 'Something was inserted into a void block. Not producing editor patches.',
233
- )
234
- return []
235
- }
236
-
237
- function splitNodePatch(
238
- editor: PortableTextSlateEditor,
239
- operation: SplitNodeOperation,
240
- beforeValue: Descendant[],
241
- ) {
242
- const patches: Patch[] = []
243
- const splitBlock = editor.children[operation.path[0]]
244
- if (!editor.isTextBlock(splitBlock)) {
245
- throw new Error(
246
- `Block with path ${JSON.stringify(
247
- operation.path[0],
248
- )} is not a text block and can't be split`,
249
- )
250
- }
251
- if (operation.path.length === 1) {
252
- const oldBlock = beforeValue[operation.path[0]]
253
- if (editor.isTextBlock(oldBlock)) {
254
- const targetValue = fromSlateValue(
255
- [editor.children[operation.path[0] + 1]],
256
- textBlockName,
257
- )[0]
258
- if (targetValue) {
259
- patches.push(
260
- insert([targetValue], 'after', [{_key: splitBlock._key}]),
261
- )
262
- const spansToUnset = oldBlock.children.slice(operation.position)
263
- spansToUnset.forEach((span) => {
264
- const path = [{_key: oldBlock._key}, 'children', {_key: span._key}]
265
- patches.push(unset(path))
266
- })
267
- }
268
- }
269
- return patches
270
- }
271
- if (operation.path.length === 2) {
272
- const splitSpan = splitBlock.children[operation.path[1]]
273
- if (editor.isTextSpan(splitSpan)) {
274
- const targetSpans = (
275
- fromSlateValue(
276
- [
277
- {
278
- ...splitBlock,
279
- children: splitBlock.children.slice(
280
- operation.path[1] + 1,
281
- operation.path[1] + 2,
282
- ),
283
- } as Descendant,
284
- ],
285
- textBlockName,
286
- )[0] as PortableTextTextBlock
287
- ).children
288
-
289
- patches.push(
290
- insert(targetSpans, 'after', [
291
- {_key: splitBlock._key},
292
- 'children',
293
- {_key: splitSpan._key},
294
- ]),
295
- )
296
- patches.push(
297
- set(splitSpan.text, [
298
- {_key: splitBlock._key},
299
- 'children',
300
- {_key: splitSpan._key},
301
- 'text',
302
- ]),
303
- )
304
- }
305
- return patches
306
- }
307
- return patches
308
- }
309
-
310
- function removeNodePatch(
311
- editor: PortableTextSlateEditor,
312
- operation: RemoveNodeOperation,
313
- beforeValue: Descendant[],
314
- ) {
315
- const block = beforeValue[operation.path[0]]
316
- if (operation.path.length === 1) {
317
- // Remove a single block
318
- if (block && block._key) {
319
- return [unset([{_key: block._key}])]
320
- }
321
- throw new Error('Block not found')
322
- } else if (editor.isTextBlock(block) && operation.path.length === 2) {
323
- const spanToRemove = block.children[operation.path[1]]
324
-
325
- if (spanToRemove) {
326
- const spansMatchingKey = block.children.filter(
327
- (span) => span._key === operation.node._key,
328
- )
329
-
330
- if (spansMatchingKey.length > 1) {
331
- console.warn(
332
- `Multiple spans have \`_key\` ${operation.node._key}. It's ambiguous which one to remove.`,
333
- JSON.stringify(block, null, 2),
334
- )
335
- return []
336
- }
337
-
338
- return [
339
- unset([{_key: block._key}, 'children', {_key: spanToRemove._key}]),
340
- ]
341
- }
342
- debug('Span not found in editor trying to remove node')
343
- return []
344
- } else {
345
- debug('Not creating patch inside object block')
346
- return []
347
- }
348
- }
349
-
350
- function mergeNodePatch(
351
- editor: PortableTextSlateEditor,
352
- operation: MergeNodeOperation,
353
- beforeValue: Descendant[],
354
- ) {
355
- const patches: Patch[] = []
356
-
357
- const block = beforeValue[operation.path[0]]
358
- const updatedBlock = editor.children[operation.path[0]]
359
-
360
- if (operation.path.length === 1) {
361
- if (block?._key) {
362
- const newBlock = fromSlateValue(
363
- [editor.children[operation.path[0] - 1]],
364
- textBlockName,
365
- )[0]
366
- patches.push(set(newBlock, [{_key: newBlock._key}]))
367
- patches.push(unset([{_key: block._key}]))
368
- } else {
369
- throw new Error('Target key not found!')
370
- }
371
- } else if (
372
- editor.isTextBlock(block) &&
373
- editor.isTextBlock(updatedBlock) &&
374
- operation.path.length === 2
375
- ) {
376
- const updatedSpan =
377
- updatedBlock.children[operation.path[1] - 1] &&
378
- editor.isTextSpan(updatedBlock.children[operation.path[1] - 1])
379
- ? updatedBlock.children[operation.path[1] - 1]
380
- : undefined
381
- const removedSpan =
382
- block.children[operation.path[1]] &&
383
- editor.isTextSpan(block.children[operation.path[1]])
384
- ? block.children[operation.path[1]]
385
- : undefined
386
-
387
- if (updatedSpan) {
388
- const spansMatchingKey = block.children.filter(
389
- (span) => span._key === updatedSpan._key,
390
- )
391
-
392
- if (spansMatchingKey.length === 1) {
393
- patches.push(
394
- set(updatedSpan.text, [
395
- {_key: block._key},
396
- 'children',
397
- {_key: updatedSpan._key},
398
- 'text',
399
- ]),
400
- )
401
- } else {
402
- console.warn(
403
- `Multiple spans have \`_key\` ${updatedSpan._key}. It's ambiguous which one to update.`,
404
- JSON.stringify(block, null, 2),
405
- )
406
- }
407
- }
408
-
409
- if (removedSpan) {
410
- const spansMatchingKey = block.children.filter(
411
- (span) => span._key === removedSpan._key,
412
- )
413
-
414
- if (spansMatchingKey.length === 1) {
415
- patches.push(
416
- unset([{_key: block._key}, 'children', {_key: removedSpan._key}]),
417
- )
418
- } else {
419
- console.warn(
420
- `Multiple spans have \`_key\` ${removedSpan._key}. It's ambiguous which one to remove.`,
421
- JSON.stringify(block, null, 2),
422
- )
423
- }
424
- }
425
- } else {
426
- debug("Void nodes can't be merged, not creating any patches")
427
- }
428
- return patches
429
- }
430
-
431
- function moveNodePatch(
432
- editor: PortableTextSlateEditor,
433
- operation: MoveNodeOperation,
434
- beforeValue: Descendant[],
435
- ) {
436
- const patches: Patch[] = []
437
- const block = beforeValue[operation.path[0]]
438
- const targetBlock = beforeValue[operation.newPath[0]]
439
-
440
- if (!targetBlock) {
441
- return patches
442
- }
443
-
444
- if (operation.path.length === 1) {
445
- const position: InsertPosition =
446
- operation.path[0] > operation.newPath[0] ? 'before' : 'after'
447
- patches.push(unset([{_key: block._key}]))
448
- patches.push(
449
- insert([fromSlateValue([block], textBlockName)[0]], position, [
450
- {_key: targetBlock._key},
451
- ]),
452
- )
453
- } else if (
454
- operation.path.length === 2 &&
455
- editor.isTextBlock(block) &&
456
- editor.isTextBlock(targetBlock)
457
- ) {
458
- const child = block.children[operation.path[1]]
459
- const targetChild = targetBlock.children[operation.newPath[1]]
460
- const position =
461
- operation.newPath[1] === targetBlock.children.length
462
- ? 'after'
463
- : 'before'
464
- const childToInsert = (
465
- fromSlateValue([block], textBlockName)[0] as PortableTextTextBlock
466
- ).children[operation.path[1]]
467
- patches.push(unset([{_key: block._key}, 'children', {_key: child._key}]))
468
- patches.push(
469
- insert([childToInsert], position, [
470
- {_key: targetBlock._key},
471
- 'children',
472
- {_key: targetChild._key},
473
- ]),
474
- )
475
- }
476
- return patches
477
- }
478
-
479
- return {
480
- insertNodePatch,
481
- insertTextPatch,
482
- mergeNodePatch,
483
- moveNodePatch,
484
- removeNodePatch,
485
- removeTextPatch,
486
- setNodePatch,
487
- splitNodePatch,
488
- }
489
- }