-
Notifications
You must be signed in to change notification settings - Fork 395
/
createWithPatches.ts
597 lines (564 loc) · 23 KB
/
createWithPatches.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/* eslint-disable max-nested-callbacks */
import {Observable, Subject, Subscription} from 'rxjs'
import {
Descendant,
Editor,
InsertNodeOperation,
InsertTextOperation,
MergeNodeOperation,
MoveNodeOperation,
Operation,
RemoveNodeOperation,
RemoveTextOperation,
SetNodeOperation,
SplitNodeOperation,
} from 'slate'
import {insert, setIfMissing, unset} from '../../patch/PatchEvent'
import type {Patch} from '../../types/patch'
import {fromSlateValue, isEqualToEmptyEditor} from '../../utils/values'
import {PortableTextBlock, PortableTextFeatures} from '../../types/portableText'
import {EditorChange, PortableTextSlateEditor} from '../../types/editor'
import {debugWithName} from '../../utils/debug'
import {PATCHING, isPatching, withoutPatching} from '../../utils/withoutPatching'
import {KEY_TO_VALUE_ELEMENT} from '../../utils/weakMaps'
import {createPatchToOperations} from '../../utils/patchToOperations'
import {keyGenerator} from '../..'
import {withPreserveKeys} from '../../utils/withPreserveKeys'
import {withoutSaving} from './createWithUndoRedo'
const debug = debugWithName('plugin:withPatches')
export type PatchFunctions = {
insertNodePatch: (
editor: PortableTextSlateEditor,
operation: InsertNodeOperation,
previousChildren: Descendant[]
) => Patch[]
insertTextPatch: (
editor: PortableTextSlateEditor,
operation: InsertTextOperation,
previousChildren: Descendant[]
) => Patch[]
mergeNodePatch: (
editor: PortableTextSlateEditor,
operation: MergeNodeOperation,
previousChildren: Descendant[]
) => Patch[]
moveNodePatch: (
editor: PortableTextSlateEditor,
operation: MoveNodeOperation,
previousChildren: Descendant[]
) => Patch[]
removeNodePatch: (
editor: PortableTextSlateEditor,
operation: RemoveNodeOperation,
previousChildren: Descendant[]
) => Patch[]
removeTextPatch: (
editor: PortableTextSlateEditor,
operation: RemoveTextOperation,
previousChildren: Descendant[]
) => Patch[]
setNodePatch: (
editor: PortableTextSlateEditor,
operation: SetNodeOperation,
previousChildren: Descendant[]
) => Patch[]
splitNodePatch: (
editor: PortableTextSlateEditor,
operation: SplitNodeOperation,
previousChildren: Descendant[]
) => Patch[]
}
export function createWithPatches(
{
insertNodePatch,
insertTextPatch,
mergeNodePatch,
moveNodePatch,
removeNodePatch,
removeTextPatch,
setNodePatch,
splitNodePatch,
}: PatchFunctions,
change$: Subject<EditorChange>,
portableTextFeatures: PortableTextFeatures,
syncValue: () => void,
incomingPatches$?: Observable<{
patches: Patch[]
snapshot: PortableTextBlock[] | undefined
}>
): [editor: (editor: PortableTextSlateEditor) => PortableTextSlateEditor, cleanupFn: () => void] {
let previousChildren: Descendant[]
const patchToOperations = createPatchToOperations(portableTextFeatures, keyGenerator)
let patchSubscription: Subscription
const cleanupFn = () => {
if (patchSubscription) {
debug('Unsubscribing to patches')
patchSubscription.unsubscribe()
}
}
return [
function withPatches(editor: PortableTextSlateEditor) {
PATCHING.set(editor, true)
previousChildren = [...editor.children]
// Inspect incoming patches and adjust editor selection accordingly.
if (incomingPatches$) {
debug('Subscribing to patches')
patchSubscription = incomingPatches$.subscribe(({patches, snapshot}) => {
const remotePatches = patches.filter((p) => p.origin !== 'local')
if (remotePatches.length !== 0) {
const prevOperations = [...editor.operations]
Editor.withoutNormalizing(editor, () => {
remotePatches.forEach((patch) => {
debug(`Handling remote patch ${JSON.stringify(patch)}`)
withoutPatching(editor, () => {
withoutSaving(editor, () => {
withPreserveKeys(editor, () => {
try {
patchToOperations(editor, patch, patches, snapshot)
} catch (err) {
debug('Got error trying to create operations from patch')
console.error(err)
}
})
})
})
})
})
if (editor.operations.length !== prevOperations.length) {
editor.onChange()
}
syncValue()
}
// Always sync the value if we had local ones
if (patches.some((p) => p.origin === 'local')) {
syncValue()
}
editor.onChange()
})
}
const {apply} = editor
editor.apply = (operation: Operation): void | Editor => {
if (editor.readOnly) {
editor.apply(operation)
return editor
}
let patches: Patch[] = []
// The previous value is needed to figure out the _key of deleted nodes. The editor.children would no
// longer contain that information if the node is already deleted.
// debug('setting previous children', operation, editor.children)
previousChildren = editor.children
const editorWasEmpty = isEqualToEmptyEditor(previousChildren, portableTextFeatures)
// Apply the operation
apply(operation)
const editorIsEmpty = isEqualToEmptyEditor(editor.children, portableTextFeatures)
if (!isPatching(editor)) {
debug(`Editor is not producing patch for operation ${operation.type}`, operation)
return editor
}
if (editorWasEmpty && operation.type !== 'set_selection') {
patches.push(setIfMissing([], []))
patches.push(
insert(
[
fromSlateValue(
previousChildren.length === 0
? [editor.createPlaceholderBlock()]
: previousChildren,
portableTextFeatures.types.block.name,
KEY_TO_VALUE_ELEMENT.get(editor)
)[0],
],
'before',
[0]
)
)
}
switch (operation.type) {
case 'insert_text':
patches = [...patches, ...insertTextPatch(editor, operation, previousChildren)]
break
case 'remove_text':
patches = [...patches, ...removeTextPatch(editor, operation, previousChildren)]
break
case 'remove_node':
patches = [...patches, ...removeNodePatch(editor, operation, previousChildren)]
break
case 'split_node':
patches = [...patches, ...splitNodePatch(editor, operation, previousChildren)]
break
case 'insert_node':
patches = [...patches, ...insertNodePatch(editor, operation, previousChildren)]
break
case 'set_node':
patches = [...patches, ...setNodePatch(editor, operation, previousChildren)]
break
case 'merge_node':
patches = [...patches, ...mergeNodePatch(editor, operation, previousChildren)]
break
case 'move_node':
patches = [...patches, ...moveNodePatch(editor, operation, previousChildren)]
break
case 'set_selection':
default:
// Do nothing
}
// Unset the value if the operation made the editor empty
if (editorIsEmpty && ['remove_text', 'remove_node'].includes(operation.type)) {
patches = [...patches, unset([])]
change$.next({
type: 'unset',
previousValue: fromSlateValue(
previousChildren,
portableTextFeatures.types.block.name,
KEY_TO_VALUE_ELEMENT.get(editor)
),
})
}
// Emit all patches
if (patches.length > 0) {
patches.forEach((patch) => {
change$.next({
type: 'patch',
patch: {...patch, origin: 'local'},
})
})
}
return editor
}
return editor
},
cleanupFn,
]
}
// function adjustSelection(
// editor: Editor,
// patch: Patch,
// previousChildren: (Node | Partial<Node>)[],
// previousSelection: Range | null,
// portableTextFeatures: PortableTextFeatures
// ): Range | undefined {
// const selection = editor.selection
// if (selection === null) {
// debug('No selection, not adjusting selection')
// return undefined
// }
// let newSelection = selection
// // Text patches on same line
// if (patch.type === 'diffMatchPatch') {
// const [block, blockIndex] = findBlockAndIndexFromPath(patch.path[0], editor.children)
// const [, childIndex] = block ? findChildAndIndexFromPath(patch.path[2], block) : [undefined, -1]
// const onSameBlock =
// selection.focus.path[0] === blockIndex && selection.focus.path[1] === childIndex
// if (onSameBlock) {
// const parsed = dmp.patch_fromText(patch.value)[0]
// if (parsed) {
// let testString = ''
// for (const diff of parsed.diffs) {
// // eslint-disable-next-line max-depth
// if (diff[0] === 0) {
// testString += diff[1]
// } else {
// break
// }
// }
// // This thing is exotic but actually works!
// const isBeforeUserSelection =
// parsed.start1 !== null &&
// parsed.start1 + testString.length <= selection.focus.offset &&
// parsed.start1 + testString.length <= selection.anchor.offset
// const distance = parsed.length2 - parsed.length1
// if (isBeforeUserSelection) {
// debug('Adjusting selection for diffMatchPatch on same line')
// // debug(
// // `Adjusting selection for diffMatchPatch on same line ${JSON.stringify({
// // parsed,
// // distance,
// // isBeforeUserSelection,
// // isRemove: parsed.diffs.some(diff => diff[0] === -1),
// // testString
// // })}`
// // )
// newSelection = {...selection}
// newSelection.focus = {...selection.focus}
// newSelection.anchor = {...selection.anchor}
// newSelection.anchor.offset += distance
// newSelection.focus.offset += distance
// }
// // TODO: account for intersecting selections!
// }
// }
// }
// // Unset patches on children within a block
// if (patch.type === 'unset' && patch.path.length === 3) {
// const [oldBlock, oldBlockIndex] = findBlockAndIndexFromPath(patch.path[0], previousChildren)
// const [, childIndex] = oldBlock
// ? findChildAndIndexFromPath(patch.path[2], oldBlock)
// : [undefined, -1]
// if (
// oldBlock &&
// selection.focus.path[0] === oldBlockIndex &&
// selection.focus.path[1] >= childIndex &&
// childIndex > -1
// ) {
// const prevIndexOrLastIndex =
// childIndex === -1 || oldBlock.children.length === 1
// ? oldBlock.children.length - 1
// : childIndex
// const prevOrLastChild = oldBlock.children[prevIndexOrLastIndex]
// const prevText = (SlateText.isText(prevOrLastChild) && prevOrLastChild.text) || ''
// newSelection = {...selection}
// const beforePrevOrLast = oldBlock.children[Math.max(0, prevIndexOrLastIndex - 1)]
// const textBeforePrevOrLast = SlateText.isText(beforePrevOrLast) ? beforePrevOrLast.text : ''
// const textBefore = SlateText.isText(beforePrevOrLast) ? beforePrevOrLast.text : ''
// if (
// Path.isAfter(selection.anchor.path, [oldBlockIndex, prevIndexOrLastIndex]) ||
// Path.endsAt(selection.anchor.path, [oldBlockIndex, prevIndexOrLastIndex])
// ) {
// newSelection.anchor = {...selection.anchor}
// newSelection.anchor.path = [
// newSelection.anchor.path[0],
// Math.max(0, prevIndexOrLastIndex - 1),
// ]
// newSelection.anchor.offset = textBefore.length + textBeforePrevOrLast.length
// }
// if (
// Path.isAfter(selection.focus.path, [oldBlockIndex, prevIndexOrLastIndex]) ||
// Path.endsAt(selection.focus.path, [oldBlockIndex, prevIndexOrLastIndex])
// ) {
// newSelection.focus = {...selection.focus}
// newSelection.focus.path = [
// newSelection.focus.path[0],
// Math.max(0, prevIndexOrLastIndex - 1),
// ]
// newSelection.focus.offset = textBefore.length + textBeforePrevOrLast.length
// }
// if (Path.isAfter(selection.anchor.path, [oldBlockIndex, prevIndexOrLastIndex])) {
// newSelection.anchor = {...selection.anchor}
// newSelection.anchor.path = [oldBlockIndex, prevIndexOrLastIndex]
// newSelection.anchor.offset = selection.anchor.offset + prevText.length
// }
// if (Path.isAfter(selection.focus.path, [oldBlockIndex, prevIndexOrLastIndex])) {
// newSelection.focus = {...selection.focus}
// newSelection.focus.path = [oldBlockIndex, prevIndexOrLastIndex]
// newSelection.focus.offset = selection.focus.offset + prevText.length
// }
// if (!isEqual(newSelection, selection)) {
// debug('adjusting selection for unset block child')
// }
// }
// }
// // // Unset patches on block level
// // if (patch.type === 'unset' && patch.path.length === 1) {
// // const blkAndIdx = findBlockAndIndexFromPath(patch.path[0], previousChildren)
// // let [, blockIndex] = blkAndIdx
// // const [block] = blkAndIdx
// // const [aboveBlock] = blockIndex
// // ? findBlockAndIndexFromPath(blockIndex - 1, previousChildren)
// // : []
// // // Deal with editing being done above the removed block
// // if (
// // block &&
// // blockIndex !== undefined &&
// // previousSelection &&
// // !Path.isAfter(selection.anchor.path, [blockIndex]) &&
// // !Path.isAfter(selection.focus.path, [blockIndex])
// // ) {
// // newSelection = {...previousSelection}
// // newSelection.anchor = {...previousSelection.anchor}
// // newSelection.anchor.path = [
// // Math.max(0, previousSelection.anchor.path[0] - 1),
// // ...previousSelection.anchor.path.slice(1),
// // ]
// // newSelection.focus = {...previousSelection.focus}
// // newSelection.focus.path = [
// // Math.max(0, previousSelection.focus.path[0] - 1),
// // ...previousSelection.focus.path.slice(1),
// // ]
// // }
// // // Deal with editing being done below or on the removed block
// // if (
// // block &&
// // blockIndex !== undefined &&
// // !Path.isBefore(selection.anchor.path, [blockIndex]) &&
// // !Path.isBefore(selection.focus.path, [blockIndex])
// // ) {
// // debug('block', JSON.stringify(block, null, 2))
// // debug('blockIndex', blockIndex)
// // const isTextBlock = aboveBlock && aboveBlock._type === portableTextFeatures.types.block.name
// // const addToOffset =
// // isTextBlock &&
// // isEqual(selection.anchor.path[0], blockIndex) &&
// // isEqual(selection.focus.path[0], blockIndex)
// // ? aboveBlock.children
// // .map(
// // (child) =>
// // SlateText.isText(child) &&
// // child._type === portableTextFeatures.types.span.name &&
// // child.text
// // )
// // .filter(Boolean)
// // .join('').length
// // : 0
// // if (selection.anchor.path[0] === blockIndex && selection.focus.path[0] === blockIndex) {
// // blockIndex = Math.max(0, selection.focus.path[0] - 1)
// // }
// // if (Path.isAfter(selection.anchor.path, [blockIndex])) {
// // newSelection = {...selection}
// // newSelection.anchor = {...newSelection.anchor}
// // newSelection.anchor.path = [
// // Math.max(0, newSelection.anchor.path[0] - 1),
// // ...newSelection.anchor.path.slice(1),
// // ]
// // newSelection.anchor.offset = selection.anchor.offset + addToOffset
// // }
// // if (Path.isAfter(selection.focus.path, [blockIndex])) {
// // newSelection = {...(newSelection || selection)}
// // newSelection.focus = {...newSelection.focus}
// // newSelection.focus.path = [
// // Math.max(0, newSelection.focus.path[0] - 1),
// // ...newSelection.focus.path.slice(1),
// // ]
// // newSelection.focus.offset = selection.focus.offset + addToOffset
// // }
// // }
// // if (!isEqual(newSelection, selection)) {
// // debug('adjusting selection for unset block')
// // }
// // }
// // Unset patches on block level
// if (patch.type === 'unset' && patch.path.length === 1) {
// const [block, blockIndex] = findBlockAndIndexFromPath(patch.path[0], previousChildren)
// debug('block/index', JSON.stringify(block, null, 2), blockIndex)
// if (block && typeof blockIndex !== 'undefined') {
// newSelection = {...selection}
// if (Path.isAfter(selection.anchor.path, [blockIndex])) {
// newSelection.anchor = {...selection.anchor}
// newSelection.anchor.path = [
// newSelection.anchor.path[0] - 1,
// ...newSelection.anchor.path.slice(1),
// ]
// }
// if (Path.isAfter(selection.focus.path, [blockIndex])) {
// newSelection.focus = {...selection.focus}
// newSelection.focus.path = [
// newSelection.focus.path[0] - 1,
// ...newSelection.focus.path.slice(1),
// ]
// }
// }
// if (!isEqual(newSelection, selection)) {
// debug('adjusting selection for unset block')
// }
// }
// // Unset patches on child level
// if (patch.type === 'unset' && patch.path.length > 2) {
// const blkAndIdx = findBlockAndIndexFromPath(patch.path[0], previousChildren)
// const [, blockIndex] = blkAndIdx
// const [block] = blkAndIdx
// const isTextBlock = block && block._type === portableTextFeatures.types.block.name
// const first = patch.path[0]
// const offset =
// isTextBlock &&
// previousSelection &&
// isKeySegment(first) &&
// first._key === block._key &&
// previousSelection?.anchor.path[0] === blockIndex &&
// previousSelection?.focus.path[0] === blockIndex
// ? block.children
// .map(
// (child, index) =>
// previousSelection &&
// index < previousSelection.focus.path[1] &&
// index < previousSelection.anchor.path[1] &&
// SlateText.isText(child) &&
// child._type === portableTextFeatures.types.span.name &&
// child.text
// )
// .filter(Boolean)
// .join('').length
// : 0
// if (offset && previousSelection) {
// newSelection = {...selection}
// newSelection.anchor = {...newSelection.anchor}
// newSelection.focus = {...newSelection.focus}
// newSelection.anchor.offset = offset + previousSelection.anchor.offset
// newSelection.focus.offset = offset + previousSelection.focus.offset
// }
// if (!isEqual(newSelection, selection)) {
// debug('adjusting selection for unset block child')
// }
// }
// // Insert patches on block level
// if (patch.type === 'insert' && patch.path.length === 1) {
// const [block, blockIndex] = findBlockAndIndexFromPath(patch.path[0], editor.children)
// if (block && typeof blockIndex !== 'undefined') {
// newSelection = {...selection}
// if (Path.isAfter(selection.anchor.path, [blockIndex])) {
// newSelection.anchor = {...selection.anchor}
// newSelection.anchor.path = [
// newSelection.anchor.path[0] + patch.items.length,
// ...newSelection.anchor.path.slice(1),
// ]
// }
// if (Path.isAfter(selection.focus.path, [blockIndex])) {
// newSelection.focus = {...selection.focus}
// newSelection.focus.path = [
// newSelection.focus.path[0] + patch.items.length,
// ...newSelection.focus.path.slice(1),
// ]
// }
// }
// if (!isEqual(newSelection, selection)) {
// debug('adjusting selection for insert block')
// }
// }
// // Insert patches on block children level
// if (patch.type === 'insert' && patch.path.length === 3) {
// const [block, blockIndex] = findBlockAndIndexFromPath(patch.path[0], editor.children)
// const [, childIndex] = block ? findChildAndIndexFromPath(patch.path[2], block) : [undefined, -1]
// if (selection.focus.path[0] === blockIndex && selection.focus.path[1] === childIndex) {
// const nextIndex = childIndex + patch.items.length
// const iBlock = editor.children[blockIndex]
// const blockChildren = SlateElement.isElement(iBlock) && (iBlock.children as Node[])
// const nextBlock = editor.children[blockIndex + 1]
// const item = patch.items[0] as PortableTextBlock
// const nextChild = blockChildren && blockChildren[nextIndex]
// const isSplitOperation =
// !nextChild &&
// Editor.isBlock(editor, nextBlock) &&
// nextBlock.children &&
// nextBlock.children[0] &&
// typeof nextBlock.children[0]._key === 'string' &&
// isEqual(nextBlock.children[0]._key, item._key)
// const [node] = Editor.node(editor, selection)
// const nodeText = SlateText.isText(node) && node.text
// if (nodeText && selection.focus.offset >= nodeText.length) {
// if (!isSplitOperation) {
// newSelection = {...selection}
// newSelection.focus = {...selection.focus}
// newSelection.anchor = {...selection.anchor}
// newSelection.anchor.path = Path.next(newSelection.anchor.path)
// newSelection.anchor.offset = nodeText.length - newSelection.anchor.offset
// newSelection.focus.path = Path.next(newSelection.focus.path)
// newSelection.focus.offset = nodeText.length - newSelection.focus.offset
// editor.selection = newSelection
// } else if (selection.focus.offset >= nodeText.length) {
// debug('adjusting selection for split node')
// newSelection = {...selection}
// newSelection.focus = {...selection.focus}
// newSelection.anchor = {...selection.anchor}
// newSelection.anchor.path = [blockIndex + 1, 0]
// newSelection.anchor.offset = selection.anchor.offset - nodeText.length || 0
// newSelection.focus.path = [blockIndex + 1, 0]
// newSelection.focus.offset = selection.focus.offset - nodeText.length || 0
// }
// }
// }
// }
// if (isEqual(newSelection, editor.selection)) {
// debug('Selection is the same, not adjusting', JSON.stringify(editor.selection))
// return undefined
// }
// debug('Selection is different, adjusting', JSON.stringify(newSelection))
// return newSelection
// }