@@ -183,7 +183,19 @@ export function lexicalEditor(props?: LexicalEditorProps): LexicalRichTextAdapte
183
183
hooks : {
184
184
afterChange : [
185
185
async ( args ) => {
186
- const { collection, context : _context , global, operation, path, req, schemaPath } = args
186
+ const {
187
+ collection,
188
+ context : _context ,
189
+ data,
190
+ global,
191
+ operation,
192
+ originalDoc,
193
+ path,
194
+ previousDoc,
195
+ previousValue,
196
+ req,
197
+ schemaPath,
198
+ } = args
187
199
let { value } = args
188
200
if ( finalSanitizedEditorConfig ?. features ?. hooks ?. afterChange ?. length ) {
189
201
for ( const hook of finalSanitizedEditorConfig . features . hooks . afterChange ) {
@@ -203,6 +215,10 @@ export function lexicalEditor(props?: LexicalEditorProps): LexicalRichTextAdapte
203
215
[ key : string ] : SerializedLexicalNode
204
216
} = { }
205
217
218
+ const previousNodeIDMap : {
219
+ [ key : string ] : SerializedLexicalNode
220
+ } = { }
221
+
206
222
/**
207
223
* Get the originalNodeIDMap from the beforeValidate hook, which is always run before this hook.
208
224
*/
@@ -219,6 +235,11 @@ export function lexicalEditor(props?: LexicalEditorProps): LexicalRichTextAdapte
219
235
nodes : ( value as SerializedEditorState ) ?. root ?. children ?? [ ] ,
220
236
} )
221
237
238
+ recurseNodeTree ( {
239
+ nodeIDMap : previousNodeIDMap ,
240
+ nodes : ( previousValue as SerializedEditorState ) ?. root ?. children ?? [ ] ,
241
+ } )
242
+
222
243
// eslint-disable-next-line prefer-const
223
244
for ( let [ id , node ] of Object . entries ( nodeIDMap ) ) {
224
245
const afterChangeHooks = finalSanitizedEditorConfig . features . nodeHooks ?. afterChange
@@ -243,6 +264,7 @@ export function lexicalEditor(props?: LexicalEditorProps): LexicalRichTextAdapte
243
264
originalNode : originalNodeIDMap [ id ] ,
244
265
parentRichTextFieldPath : path ,
245
266
parentRichTextFieldSchemaPath : schemaPath ,
267
+ previousNode : previousNodeIDMap [ id ] ,
246
268
req,
247
269
} )
248
270
}
@@ -254,25 +276,27 @@ export function lexicalEditor(props?: LexicalEditorProps): LexicalRichTextAdapte
254
276
255
277
if ( subFieldFn && subFieldDataFn ) {
256
278
const subFields = subFieldFn ( { node, req } )
257
- const data = subFieldDataFn ( { node, req } ) ?? { }
258
- const originalData = subFieldDataFn ( { node : originalNodeIDMap [ id ] , req } ) ?? { }
279
+ const nodeSiblingData = subFieldDataFn ( { node, req } ) ?? { }
280
+ const nodeSiblingDoc = subFieldDataFn ( { node : originalNodeIDMap [ id ] , req } ) ?? { }
281
+ const nodePreviousSiblingDoc =
282
+ subFieldDataFn ( { node : previousNodeIDMap [ id ] , req } ) ?? { }
259
283
260
284
if ( subFields ?. length ) {
261
285
await afterChangeTraverseFields ( {
262
286
collection,
263
287
context,
264
- data : originalData ,
265
- doc : data ,
288
+ data : data ?? { } ,
289
+ doc : originalDoc ,
266
290
fields : subFields ,
267
291
global,
268
292
operation,
269
293
path,
270
- previousDoc : data ,
271
- previousSiblingDoc : { ...data } ,
294
+ previousDoc,
295
+ previousSiblingDoc : { ...nodePreviousSiblingDoc } ,
272
296
req,
273
297
schemaPath,
274
- siblingData : originalData || { } ,
275
- siblingDoc : { ...data } ,
298
+ siblingData : nodeSiblingData || { } ,
299
+ siblingDoc : { ...nodeSiblingDoc } ,
276
300
} )
277
301
}
278
302
}
@@ -297,6 +321,7 @@ export function lexicalEditor(props?: LexicalEditorProps): LexicalRichTextAdapte
297
321
flattenLocales,
298
322
global,
299
323
locale,
324
+ originalDoc,
300
325
overrideAccess,
301
326
path,
302
327
populate,
@@ -363,15 +388,15 @@ export function lexicalEditor(props?: LexicalEditorProps): LexicalRichTextAdapte
363
388
364
389
if ( subFieldFn && subFieldDataFn ) {
365
390
const subFields = subFieldFn ( { node, req } )
366
- const data = subFieldDataFn ( { node, req } ) ?? { }
391
+ const nodeSliblingData = subFieldDataFn ( { node, req } ) ?? { }
367
392
368
393
if ( subFields ?. length ) {
369
394
afterReadTraverseFields ( {
370
395
collection,
371
396
context,
372
397
currentDepth : currentDepth ! ,
373
398
depth : depth ! ,
374
- doc : data ,
399
+ doc : originalDoc ,
375
400
draft : draft ! ,
376
401
fallbackLocale : fallbackLocale ! ,
377
402
fieldPromises : fieldPromises ! ,
@@ -387,7 +412,7 @@ export function lexicalEditor(props?: LexicalEditorProps): LexicalRichTextAdapte
387
412
req,
388
413
schemaPath,
389
414
showHiddenFields : showHiddenFields ! ,
390
- siblingDoc : data ,
415
+ siblingDoc : nodeSliblingData ,
391
416
triggerAccessControl,
392
417
triggerHooks,
393
418
} )
@@ -403,12 +428,16 @@ export function lexicalEditor(props?: LexicalEditorProps): LexicalRichTextAdapte
403
428
const {
404
429
collection,
405
430
context : _context ,
431
+ data,
432
+ docWithLocales,
406
433
errors,
407
434
field,
408
435
global,
409
436
mergeLocaleActions,
410
437
operation,
438
+ originalDoc,
411
439
path,
440
+ previousValue,
412
441
req,
413
442
schemaPath,
414
443
siblingData,
@@ -447,7 +476,9 @@ export function lexicalEditor(props?: LexicalEditorProps): LexicalRichTextAdapte
447
476
if ( ! originalNodeIDMap || ! Object . keys ( originalNodeIDMap ) . length || ! value ) {
448
477
return value
449
478
}
450
-
479
+ const previousNodeIDMap : {
480
+ [ key : string ] : SerializedLexicalNode
481
+ } = { }
451
482
const originalNodeWithLocalesIDMap : {
452
483
[ key : string ] : SerializedLexicalNode
453
484
} = { }
@@ -457,6 +488,10 @@ export function lexicalEditor(props?: LexicalEditorProps): LexicalRichTextAdapte
457
488
nodes : ( value as SerializedEditorState ) ?. root ?. children ?? [ ] ,
458
489
} )
459
490
491
+ recurseNodeTree ( {
492
+ nodeIDMap : previousNodeIDMap ,
493
+ nodes : ( previousValue as SerializedEditorState ) ?. root ?. children ?? [ ] ,
494
+ } )
460
495
if ( field . name && siblingDocWithLocales ?. [ field . name ] ) {
461
496
recurseNodeTree ( {
462
497
nodeIDMap : originalNodeWithLocalesIDMap ,
@@ -493,6 +528,7 @@ export function lexicalEditor(props?: LexicalEditorProps): LexicalRichTextAdapte
493
528
originalNodeWithLocales : originalNodeWithLocalesIDMap [ id ] ,
494
529
parentRichTextFieldPath : path ,
495
530
parentRichTextFieldSchemaPath : schemaPath ,
531
+ previousNode : previousNodeIDMap [ id ] ,
496
532
req,
497
533
skipValidation : skipValidation ! ,
498
534
} )
@@ -506,22 +542,23 @@ export function lexicalEditor(props?: LexicalEditorProps): LexicalRichTextAdapte
506
542
507
543
if ( subFieldFn && subFieldDataFn ) {
508
544
const subFields = subFieldFn ( { node, req } )
509
- const data = subFieldDataFn ( { node, req } ) ?? { }
510
- const originalData = subFieldDataFn ( { node : originalNodeIDMap [ id ] , req } ) ?? { }
511
- const originalDataWithLocales =
545
+ const nodeSiblingData = subFieldDataFn ( { node, req } ) ?? { }
546
+ const nodeSiblingDocWithLocales =
512
547
subFieldDataFn ( {
513
548
node : originalNodeWithLocalesIDMap [ id ] ,
514
549
req,
515
550
} ) ?? { }
551
+ const nodePreviousSiblingDoc =
552
+ subFieldDataFn ( { node : previousNodeIDMap [ id ] , req } ) ?? { }
516
553
517
554
if ( subFields ?. length ) {
518
555
await beforeChangeTraverseFields ( {
519
556
id,
520
557
collection,
521
558
context,
522
- data,
523
- doc : originalData ,
524
- docWithLocales : originalDataWithLocales ?? { } ,
559
+ data : data ?? { } ,
560
+ doc : originalDoc ?? { } ,
561
+ docWithLocales : docWithLocales ?? { } ,
525
562
errors : errors ! ,
526
563
fields : subFields ,
527
564
global,
@@ -530,9 +567,9 @@ export function lexicalEditor(props?: LexicalEditorProps): LexicalRichTextAdapte
530
567
path,
531
568
req,
532
569
schemaPath,
533
- siblingData : data ,
534
- siblingDoc : originalData ,
535
- siblingDocWithLocales : originalDataWithLocales ?? { } ,
570
+ siblingData : nodeSiblingData ,
571
+ siblingDoc : nodePreviousSiblingDoc ,
572
+ siblingDocWithLocales : nodeSiblingDocWithLocales ?? { } ,
536
573
skipValidation,
537
574
} )
538
575
}
@@ -553,11 +590,11 @@ export function lexicalEditor(props?: LexicalEditorProps): LexicalRichTextAdapte
553
590
[ key : string ] : SerializedLexicalNode
554
591
} = { }
555
592
556
- const previousValue = siblingData [ field . name ! ]
593
+ const previousOriginalValue = siblingData [ field . name ! ]
557
594
558
595
recurseNodeTree ( {
559
596
nodeIDMap : newOriginalNodeIDMap ,
560
- nodes : ( previousValue as SerializedEditorState ) ?. root ?. children ?? [ ] ,
597
+ nodes : ( previousOriginalValue as SerializedEditorState ) ?. root ?. children ?? [ ] ,
561
598
} )
562
599
563
600
if ( ! context . internal ) {
@@ -579,8 +616,10 @@ export function lexicalEditor(props?: LexicalEditorProps): LexicalRichTextAdapte
579
616
const {
580
617
collection,
581
618
context,
619
+ data,
582
620
global,
583
621
operation,
622
+ originalDoc,
584
623
overrideAccess,
585
624
path,
586
625
previousValue,
@@ -700,25 +739,25 @@ export function lexicalEditor(props?: LexicalEditorProps): LexicalRichTextAdapte
700
739
701
740
if ( subFieldFn && subFieldDataFn ) {
702
741
const subFields = subFieldFn ( { node, req } )
703
- const data = subFieldDataFn ( { node, req } ) ?? { }
704
- const originalData = subFieldDataFn ( { node : originalNodeIDMap [ id ] , req } ) ?? { }
742
+ const nodeSiblingData = subFieldDataFn ( { node, req } ) ?? { }
743
+ const nodeSiblingDoc = subFieldDataFn ( { node : originalNodeIDMap [ id ] , req } ) ?? { }
705
744
706
745
if ( subFields ?. length ) {
707
746
await beforeValidateTraverseFields ( {
708
747
id,
709
748
collection,
710
749
context,
711
750
data,
712
- doc : originalData ,
751
+ doc : originalDoc ,
713
752
fields : subFields ,
714
753
global,
715
754
operation,
716
755
overrideAccess : overrideAccess ! ,
717
756
path,
718
757
req,
719
758
schemaPath,
720
- siblingData : data ,
721
- siblingDoc : originalData ,
759
+ siblingData : nodeSiblingData ,
760
+ siblingDoc : nodeSiblingDoc ,
722
761
} )
723
762
}
724
763
}
0 commit comments