@lexical/yjs 0.1.21 → 0.2.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/LexicalYjs.dev.js +37 -383
- package/LexicalYjs.prod.js +42 -54
- package/package.json +3 -3
package/LexicalYjs.dev.js
CHANGED
|
@@ -232,7 +232,7 @@ function $createCollabTextNode(map, text, parent, type) {
|
|
|
232
232
|
*
|
|
233
233
|
*
|
|
234
234
|
*/
|
|
235
|
-
const excludedProperties = new Set(['__key', '__children', '__parent', '__cachedText', '__text'
|
|
235
|
+
const excludedProperties = new Set(['__key', '__children', '__parent', '__cachedText', '__text']);
|
|
236
236
|
function $getNodeByKeyOrThrow(key) {
|
|
237
237
|
const node = lexical.$getNodeByKey(key);
|
|
238
238
|
|
|
@@ -362,9 +362,23 @@ function syncPropertiesFromYjs(binding, sharedType, lexicalNode, keysChanged) {
|
|
|
362
362
|
|
|
363
363
|
|
|
364
364
|
const prevValue = lexicalNode[property];
|
|
365
|
-
|
|
365
|
+
let nextValue = sharedType instanceof yjs.Map ? sharedType.get(property) : sharedType.getAttribute(property);
|
|
366
366
|
|
|
367
367
|
if (prevValue !== nextValue) {
|
|
368
|
+
if (nextValue instanceof yjs.Doc) {
|
|
369
|
+
const yjsDocMap = binding.docMap;
|
|
370
|
+
|
|
371
|
+
if (prevValue instanceof yjs.Doc) {
|
|
372
|
+
yjsDocMap.delete(prevValue.guid);
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
const nestedEditor = lexical.createEditor();
|
|
376
|
+
const key = nextValue.guid;
|
|
377
|
+
nestedEditor._key = key;
|
|
378
|
+
yjsDocMap.set(key, nextValue);
|
|
379
|
+
nextValue = nestedEditor;
|
|
380
|
+
}
|
|
381
|
+
|
|
368
382
|
if (writableNode === undefined) {
|
|
369
383
|
writableNode = lexicalNode.getWritable();
|
|
370
384
|
} // $FlowFixMe
|
|
@@ -386,14 +400,31 @@ function syncPropertiesFromLexical(binding, sharedType, prevLexicalNode, nextLex
|
|
|
386
400
|
nodeProperties.set(type, properties);
|
|
387
401
|
}
|
|
388
402
|
|
|
403
|
+
const EditorClass = binding.editor.constructor;
|
|
404
|
+
|
|
389
405
|
for (let i = 0; i < properties.length; i++) {
|
|
390
406
|
const property = properties[i];
|
|
391
407
|
const prevValue = // $FlowFixMe: intentional override
|
|
392
408
|
prevLexicalNode === null ? undefined : prevLexicalNode[property]; // $FlowFixMe: intentional override
|
|
393
409
|
|
|
394
|
-
|
|
410
|
+
let nextValue = nextLexicalNode[property];
|
|
395
411
|
|
|
396
412
|
if (prevValue !== nextValue) {
|
|
413
|
+
if (nextValue instanceof EditorClass) {
|
|
414
|
+
const yjsDocMap = binding.docMap;
|
|
415
|
+
|
|
416
|
+
if (prevValue instanceof EditorClass) {
|
|
417
|
+
yjsDocMap.delete(prevValue._key);
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
const doc = new yjs.Doc(); // $FlowFixMe: guid exists
|
|
421
|
+
|
|
422
|
+
const key = doc.guid;
|
|
423
|
+
nextValue._key = key;
|
|
424
|
+
yjsDocMap.set(key, doc);
|
|
425
|
+
nextValue = doc;
|
|
426
|
+
}
|
|
427
|
+
|
|
397
428
|
if (sharedType instanceof yjs.Map) {
|
|
398
429
|
sharedType.set(property, nextValue);
|
|
399
430
|
} else {
|
|
@@ -484,320 +515,6 @@ function syncWithTransaction(binding, fn) {
|
|
|
484
515
|
binding.doc.transact(fn, binding);
|
|
485
516
|
}
|
|
486
517
|
|
|
487
|
-
/**
|
|
488
|
-
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
489
|
-
*
|
|
490
|
-
* This source code is licensed under the MIT license found in the
|
|
491
|
-
* LICENSE file in the root directory of this source tree.
|
|
492
|
-
*
|
|
493
|
-
*
|
|
494
|
-
*/
|
|
495
|
-
let isMutationFromCollab = false;
|
|
496
|
-
function mutationFromCollab(fn) {
|
|
497
|
-
const prevIsMutationFromCollab = isMutationFromCollab;
|
|
498
|
-
|
|
499
|
-
try {
|
|
500
|
-
isMutationFromCollab = true;
|
|
501
|
-
fn();
|
|
502
|
-
} finally {
|
|
503
|
-
isMutationFromCollab = prevIsMutationFromCollab;
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
function observeDecoratorMap(binding, collabNode, decoratorMap, yjsMap) {
|
|
507
|
-
const unobserve = decoratorMap.observe(changedKey => {
|
|
508
|
-
if (isMutationFromCollab) {
|
|
509
|
-
return;
|
|
510
|
-
}
|
|
511
|
-
|
|
512
|
-
syncWithTransaction(binding, () => syncLexicalDecoratorMapKeyToYjs(binding, collabNode, decoratorMap._map, yjsMap, changedKey));
|
|
513
|
-
});
|
|
514
|
-
|
|
515
|
-
collabNode._unobservers.add(unobserve);
|
|
516
|
-
}
|
|
517
|
-
|
|
518
|
-
function observeDecoratorArray(binding, collabNode, decoratorArray, yjsArray) {
|
|
519
|
-
const unobserve = decoratorArray.observe((changedIndex, delCount) => {
|
|
520
|
-
if (isMutationFromCollab) {
|
|
521
|
-
return;
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
syncWithTransaction(binding, () => {
|
|
525
|
-
if (delCount > 0) {
|
|
526
|
-
yjsArray.delete(changedIndex, delCount);
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
syncLexicalDecoratorArrayValueToYjs(binding, collabNode, decoratorArray._array, yjsArray, changedIndex);
|
|
530
|
-
});
|
|
531
|
-
});
|
|
532
|
-
|
|
533
|
-
collabNode._unobservers.add(unobserve);
|
|
534
|
-
}
|
|
535
|
-
|
|
536
|
-
function syncLexicalDecoratorMapKeyToYjs(binding, collabNode, internalMap, yjsMap, key) {
|
|
537
|
-
const lexicalValue = internalMap.get(key);
|
|
538
|
-
let yjsValue = yjsMap.get(key);
|
|
539
|
-
|
|
540
|
-
if (lexicalValue !== yjsValue) {
|
|
541
|
-
if (lexical.isDecoratorMap(lexicalValue)) {
|
|
542
|
-
if (yjsValue === undefined) {
|
|
543
|
-
yjsValue = new yjs.Map(); // $FlowFixMe: internal field
|
|
544
|
-
|
|
545
|
-
yjsValue._lexicalValue = lexicalValue; // $FlowFixMe: internal field
|
|
546
|
-
|
|
547
|
-
yjsValue._collabNode = collabNode;
|
|
548
|
-
yjsValue.set('type', 'map');
|
|
549
|
-
yjsMap.set(key, yjsValue);
|
|
550
|
-
observeDecoratorMap(binding, collabNode, lexicalValue, yjsValue);
|
|
551
|
-
}
|
|
552
|
-
|
|
553
|
-
syncLexicalDecoratorMapToYjs(binding, collabNode, lexicalValue, yjsValue);
|
|
554
|
-
} else if (lexical.isDecoratorEditor(lexicalValue)) {
|
|
555
|
-
let doc;
|
|
556
|
-
|
|
557
|
-
if (yjsValue === undefined) {
|
|
558
|
-
yjsValue = new yjs.Map(); // $FlowFixMe: internal field
|
|
559
|
-
|
|
560
|
-
yjsValue._lexicalValue = lexicalValue; // $FlowFixMe: internal field
|
|
561
|
-
|
|
562
|
-
yjsValue._collabNode = collabNode; // Create a subdocument
|
|
563
|
-
|
|
564
|
-
doc = new yjs.Doc();
|
|
565
|
-
yjsValue.set('doc', doc);
|
|
566
|
-
yjsValue.set('type', 'editor');
|
|
567
|
-
yjsMap.set(key, yjsValue);
|
|
568
|
-
}
|
|
569
|
-
|
|
570
|
-
doc = doc || yjsValue.get('doc');
|
|
571
|
-
const yjsId = yjsValue.get('id');
|
|
572
|
-
const lexicalId = lexicalValue.id;
|
|
573
|
-
|
|
574
|
-
if (yjsId !== lexicalId) {
|
|
575
|
-
const yjsDocMap = binding.docMap;
|
|
576
|
-
yjsDocMap.delete(yjsId);
|
|
577
|
-
yjsValue.set('id', lexicalId);
|
|
578
|
-
yjsDocMap.set(lexicalId, doc);
|
|
579
|
-
}
|
|
580
|
-
} else if (lexical.isDecoratorArray(lexicalValue)) {
|
|
581
|
-
if (yjsValue === undefined) {
|
|
582
|
-
yjsValue = new yjs.Array(); // $FlowFixMe: internal field
|
|
583
|
-
|
|
584
|
-
yjsValue._lexicalValue = lexicalValue; // $FlowFixMe: internal field
|
|
585
|
-
|
|
586
|
-
yjsValue._collabNode = collabNode;
|
|
587
|
-
yjsMap.set(key, yjsValue);
|
|
588
|
-
observeDecoratorArray(binding, collabNode, lexicalValue, yjsValue);
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
syncLexicalDecoratorArrayToYjs(binding, collabNode, lexicalValue, yjsValue);
|
|
592
|
-
} else {
|
|
593
|
-
if (lexical.isDecoratorArray(yjsValue) || lexical.isDecoratorMap(yjsValue)) {
|
|
594
|
-
yjsValue.destroy();
|
|
595
|
-
}
|
|
596
|
-
|
|
597
|
-
yjsMap.set(key, lexicalValue);
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
}
|
|
601
|
-
|
|
602
|
-
function syncLexicalDecoratorArrayValueToYjs(binding, collabNode, internalArray, yjsArray, index) {
|
|
603
|
-
const lexicalValue = internalArray[index];
|
|
604
|
-
let yjsValue = yjsArray.get(index);
|
|
605
|
-
|
|
606
|
-
if (lexicalValue !== yjsValue) {
|
|
607
|
-
if (lexical.isDecoratorMap(lexicalValue)) {
|
|
608
|
-
if (yjsValue === undefined) {
|
|
609
|
-
yjsValue = new yjs.Map(); // $FlowFixMe: internal field
|
|
610
|
-
|
|
611
|
-
yjsValue._lexicalValue = lexicalValue; // $FlowFixMe: internal field
|
|
612
|
-
|
|
613
|
-
yjsValue._collabNode = collabNode;
|
|
614
|
-
yjsValue.set('type', 'map');
|
|
615
|
-
yjsArray.insert(index, [yjsValue]);
|
|
616
|
-
observeDecoratorMap(binding, collabNode, lexicalValue, yjsValue);
|
|
617
|
-
}
|
|
618
|
-
|
|
619
|
-
syncLexicalDecoratorMapToYjs(binding, collabNode, lexicalValue, yjsValue);
|
|
620
|
-
} else if (lexical.isDecoratorEditor(lexicalValue)) {
|
|
621
|
-
let doc;
|
|
622
|
-
|
|
623
|
-
if (yjsValue === undefined) {
|
|
624
|
-
yjsValue = new yjs.Map(); // $FlowFixMe: internal field
|
|
625
|
-
|
|
626
|
-
yjsValue._lexicalValue = lexicalValue; // $FlowFixMe: internal field
|
|
627
|
-
|
|
628
|
-
yjsValue._collabNode = collabNode; // Create a subdocument
|
|
629
|
-
|
|
630
|
-
doc = new yjs.Doc();
|
|
631
|
-
yjsValue.set('doc', doc);
|
|
632
|
-
yjsValue.set('type', 'editor');
|
|
633
|
-
yjsArray.insert(index, [yjsValue]);
|
|
634
|
-
}
|
|
635
|
-
|
|
636
|
-
doc = doc || yjsValue.get('doc');
|
|
637
|
-
const yjsId = yjsValue.get('id');
|
|
638
|
-
const lexicalId = lexicalValue.id;
|
|
639
|
-
|
|
640
|
-
if (yjsId !== lexicalId) {
|
|
641
|
-
const yjsDocMap = binding.docMap;
|
|
642
|
-
yjsDocMap.delete(yjsId);
|
|
643
|
-
yjsValue.set('id', lexicalId);
|
|
644
|
-
yjsDocMap.set(lexicalId, doc);
|
|
645
|
-
}
|
|
646
|
-
} else if (lexical.isDecoratorArray(lexicalValue)) {
|
|
647
|
-
if (yjsValue === undefined) {
|
|
648
|
-
yjsValue = new yjs.Array(); // $FlowFixMe: internal field
|
|
649
|
-
|
|
650
|
-
yjsValue._lexicalValue = lexicalValue; // $FlowFixMe: internal field
|
|
651
|
-
|
|
652
|
-
yjsValue._collabNode = collabNode;
|
|
653
|
-
yjsArray.insert(index, [yjsValue]);
|
|
654
|
-
observeDecoratorArray(binding, collabNode, lexicalValue, yjsValue);
|
|
655
|
-
}
|
|
656
|
-
|
|
657
|
-
syncLexicalDecoratorArrayToYjs(binding, collabNode, lexicalValue, yjsValue);
|
|
658
|
-
} else {
|
|
659
|
-
yjsArray.insert(index, [lexicalValue]);
|
|
660
|
-
}
|
|
661
|
-
}
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
function syncLexicalDecoratorMapToYjs(binding, collabNode, decoratorMap, yjsMap) {
|
|
665
|
-
const internalMap = decoratorMap._map;
|
|
666
|
-
|
|
667
|
-
for (const [key] of internalMap) {
|
|
668
|
-
syncLexicalDecoratorMapKeyToYjs(binding, collabNode, internalMap, yjsMap, key);
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
|
|
672
|
-
function syncLexicalDecoratorArrayToYjs(binding, collabNode, decoratorArray, yjsArray) {
|
|
673
|
-
const internalArray = decoratorArray._array;
|
|
674
|
-
|
|
675
|
-
for (let i = 0; i < internalArray.length; i++) {
|
|
676
|
-
syncLexicalDecoratorArrayValueToYjs(binding, collabNode, internalArray, yjsArray, i);
|
|
677
|
-
}
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
function syncYjsDecoratorMapKeyToLexical(binding, collabNode, yjsMap, decoratorMap, key) {
|
|
681
|
-
const lexicalValue = decoratorMap.get(key);
|
|
682
|
-
const yjsValue = yjsMap.get(key);
|
|
683
|
-
|
|
684
|
-
if (lexicalValue !== yjsValue) {
|
|
685
|
-
// $FlowFixMe: internal field
|
|
686
|
-
let nextValue = yjsValue._lexicalValue;
|
|
687
|
-
|
|
688
|
-
if (yjsValue instanceof yjs.Map) {
|
|
689
|
-
const type = yjsValue.get('type');
|
|
690
|
-
|
|
691
|
-
if (type === 'editor') {
|
|
692
|
-
if (nextValue === undefined) {
|
|
693
|
-
const yjsDocMap = binding.docMap;
|
|
694
|
-
const id = yjsValue.get('id');
|
|
695
|
-
const doc = yjsValue.get('doc');
|
|
696
|
-
nextValue = lexical.createDecoratorEditor(id);
|
|
697
|
-
yjsValue._lexicalValue = nextValue;
|
|
698
|
-
yjsValue._collabNode = collabNode;
|
|
699
|
-
yjsDocMap.set(id, doc);
|
|
700
|
-
mutationFromCollab(() => decoratorMap.set(key, nextValue));
|
|
701
|
-
}
|
|
702
|
-
} else if (type === 'map') {
|
|
703
|
-
if (nextValue === undefined) {
|
|
704
|
-
nextValue = lexical.createDecoratorMap(binding.editor);
|
|
705
|
-
observeDecoratorMap(binding, collabNode, nextValue, yjsValue);
|
|
706
|
-
yjsValue._lexicalValue = nextValue;
|
|
707
|
-
yjsValue._collabNode = collabNode;
|
|
708
|
-
mutationFromCollab(() => decoratorMap.set(key, nextValue));
|
|
709
|
-
}
|
|
710
|
-
|
|
711
|
-
syncYjsDecoratorMapToLexical(binding, collabNode, yjsValue, nextValue, null);
|
|
712
|
-
} else {
|
|
713
|
-
{
|
|
714
|
-
throw Error(`Should never happen`);
|
|
715
|
-
}
|
|
716
|
-
}
|
|
717
|
-
} else if (yjsValue instanceof yjs.Array) {
|
|
718
|
-
if (nextValue === undefined) {
|
|
719
|
-
nextValue = lexical.createDecoratorArray(binding.editor);
|
|
720
|
-
observeDecoratorArray(binding, collabNode, nextValue, yjsValue);
|
|
721
|
-
yjsValue._lexicalValue = nextValue;
|
|
722
|
-
yjsValue._collabNode = collabNode;
|
|
723
|
-
mutationFromCollab(() => decoratorMap.set(key, nextValue));
|
|
724
|
-
}
|
|
725
|
-
|
|
726
|
-
syncYjsDecoratorArrayToLexical(binding, collabNode, yjsValue, nextValue);
|
|
727
|
-
} else {
|
|
728
|
-
mutationFromCollab(() => decoratorMap.set(key, yjsValue));
|
|
729
|
-
}
|
|
730
|
-
}
|
|
731
|
-
}
|
|
732
|
-
|
|
733
|
-
function syncYjsDecoratorArrayValueToLexical(binding, collabNode, yjsArray, decoratorArray, index) {
|
|
734
|
-
const lexicalValue = decoratorArray._array[index];
|
|
735
|
-
const yjsValue = yjsArray.get(index);
|
|
736
|
-
|
|
737
|
-
if (lexicalValue !== yjsValue) {
|
|
738
|
-
// $FlowFixMe: internal field
|
|
739
|
-
let nextValue = yjsValue._lexicalValue;
|
|
740
|
-
|
|
741
|
-
if (yjsValue instanceof yjs.Map) {
|
|
742
|
-
const type = yjsValue.get('type');
|
|
743
|
-
|
|
744
|
-
if (type === 'editor') {
|
|
745
|
-
if (nextValue === undefined) {
|
|
746
|
-
const yjsDocMap = binding.docMap;
|
|
747
|
-
const id = yjsValue.get('id');
|
|
748
|
-
const doc = yjsValue.get('doc');
|
|
749
|
-
nextValue = lexical.createDecoratorEditor(id);
|
|
750
|
-
yjsValue._lexicalValue = nextValue;
|
|
751
|
-
yjsValue._collabNode = collabNode;
|
|
752
|
-
yjsDocMap.set(id, doc);
|
|
753
|
-
mutationFromCollab(() => decoratorArray.splice(index, 0, nextValue));
|
|
754
|
-
}
|
|
755
|
-
} else if (type === 'map') {
|
|
756
|
-
if (nextValue === undefined) {
|
|
757
|
-
nextValue = lexical.createDecoratorMap(binding.editor);
|
|
758
|
-
observeDecoratorMap(binding, collabNode, nextValue, yjsValue);
|
|
759
|
-
yjsValue._lexicalValue = nextValue;
|
|
760
|
-
yjsValue._collabNode = collabNode;
|
|
761
|
-
mutationFromCollab(() => decoratorArray.splice(index, 0, nextValue));
|
|
762
|
-
}
|
|
763
|
-
|
|
764
|
-
syncYjsDecoratorMapToLexical(binding, collabNode, yjsValue, nextValue, null);
|
|
765
|
-
} else {
|
|
766
|
-
{
|
|
767
|
-
throw Error(`Should never happen`);
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
} else if (yjsValue instanceof yjs.Array) {
|
|
771
|
-
if (nextValue === undefined) {
|
|
772
|
-
nextValue = lexical.createDecoratorArray(binding.editor);
|
|
773
|
-
observeDecoratorArray(binding, collabNode, nextValue, yjsValue);
|
|
774
|
-
yjsValue._lexicalValue = nextValue;
|
|
775
|
-
yjsValue._collabNode = collabNode;
|
|
776
|
-
mutationFromCollab(() => decoratorArray.splice(index, 0, nextValue));
|
|
777
|
-
}
|
|
778
|
-
|
|
779
|
-
syncYjsDecoratorArrayToLexical(binding, collabNode, yjsValue, nextValue);
|
|
780
|
-
} else {
|
|
781
|
-
mutationFromCollab(() => decoratorArray.splice(index, 0, yjsValue));
|
|
782
|
-
}
|
|
783
|
-
}
|
|
784
|
-
}
|
|
785
|
-
function syncYjsDecoratorArrayToLexical(binding, collabNode, yjsArray, decoratorArray) {
|
|
786
|
-
const length = Math.max(yjsArray.length, decoratorArray.getLength());
|
|
787
|
-
|
|
788
|
-
for (let i = 0; i < length; i++) {
|
|
789
|
-
syncYjsDecoratorArrayValueToLexical(binding, collabNode, yjsArray, decoratorArray, i);
|
|
790
|
-
}
|
|
791
|
-
}
|
|
792
|
-
function syncYjsDecoratorMapToLexical(binding, collabNode, yjsMap, decoratorMap, keysChanged) {
|
|
793
|
-
const keys = keysChanged === null ? Array.from(yjsMap.keys()) : Array.from(keysChanged);
|
|
794
|
-
|
|
795
|
-
for (let i = 0; i < keys.length; i++) {
|
|
796
|
-
const key = keys[i];
|
|
797
|
-
syncYjsDecoratorMapKeyToLexical(binding, collabNode, yjsMap, decoratorMap, key);
|
|
798
|
-
}
|
|
799
|
-
}
|
|
800
|
-
|
|
801
518
|
/**
|
|
802
519
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
803
520
|
*
|
|
@@ -853,20 +570,7 @@ class CollabDecoratorNode {
|
|
|
853
570
|
syncPropertiesFromLexical(binding, nextLexicalNode, prevNodeMap) {
|
|
854
571
|
const prevLexicalNode = this.getPrevNode(prevNodeMap);
|
|
855
572
|
const xmlElem = this._xmlElem;
|
|
856
|
-
|
|
857
|
-
const nextDecoratorMap = nextLexicalNode.__state;
|
|
858
|
-
syncPropertiesFromLexical(binding, xmlElem, prevLexicalNode, nextLexicalNode); // Handle bindings
|
|
859
|
-
|
|
860
|
-
if (prevDecoratorMap !== nextDecoratorMap) {
|
|
861
|
-
const yjsMap = new yjs.Map();
|
|
862
|
-
xmlElem.insert(0, [yjsMap]); // $FlowFixMe: internal field
|
|
863
|
-
|
|
864
|
-
yjsMap._lexicalValue = nextDecoratorMap; // $FlowFixMe: internal field
|
|
865
|
-
|
|
866
|
-
yjsMap._collabNode = this;
|
|
867
|
-
syncLexicalDecoratorMapToYjs(binding, this, nextDecoratorMap, yjsMap);
|
|
868
|
-
observeDecoratorMap(binding, this, nextDecoratorMap, yjsMap);
|
|
869
|
-
}
|
|
573
|
+
syncPropertiesFromLexical(binding, xmlElem, prevLexicalNode, nextLexicalNode);
|
|
870
574
|
}
|
|
871
575
|
|
|
872
576
|
syncPropertiesFromYjs(binding, keysChanged) {
|
|
@@ -876,17 +580,8 @@ class CollabDecoratorNode {
|
|
|
876
580
|
throw new Error('Should never happen');
|
|
877
581
|
}
|
|
878
582
|
|
|
879
|
-
const xmlElem = this._xmlElem;
|
|
880
|
-
|
|
881
|
-
const yjsMap = xmlElem.firstChild;
|
|
882
|
-
const decoratorMap = lexicalNode.__state; // $FlowFixMe: internal field
|
|
883
|
-
|
|
884
|
-
yjsMap._lexicalValue = decoratorMap; // $FlowFixMe: internal field
|
|
885
|
-
|
|
886
|
-
yjsMap._collabNode = this;
|
|
583
|
+
const xmlElem = this._xmlElem;
|
|
887
584
|
syncPropertiesFromYjs(binding, xmlElem, lexicalNode, keysChanged);
|
|
888
|
-
syncYjsDecoratorMapToLexical(binding, this, yjsMap, decoratorMap, null);
|
|
889
|
-
observeDecoratorMap(binding, this, decoratorMap, yjsMap);
|
|
890
585
|
}
|
|
891
586
|
|
|
892
587
|
destroy(binding) {
|
|
@@ -1867,48 +1562,7 @@ function syncEvent(binding, event) {
|
|
|
1867
1562
|
const {
|
|
1868
1563
|
target
|
|
1869
1564
|
} = event;
|
|
1870
|
-
const collabNode = getOrInitCollabNodeFromSharedType(binding, target);
|
|
1871
|
-
|
|
1872
|
-
const decoratorStateValue = target._lexicalValue; // Check if this event relates to a decorator state change.
|
|
1873
|
-
|
|
1874
|
-
if (decoratorStateValue !== undefined && collabNode instanceof CollabDecoratorNode) {
|
|
1875
|
-
if (target instanceof yjs.Map) {
|
|
1876
|
-
// Sync decorator state value
|
|
1877
|
-
syncYjsDecoratorMapToLexical(binding, collabNode, target, decoratorStateValue, event.keysChanged);
|
|
1878
|
-
} else if (target instanceof yjs.Array && lexical.isDecoratorArray(decoratorStateValue)) {
|
|
1879
|
-
// Sync decorator state value
|
|
1880
|
-
const deltas = event.delta;
|
|
1881
|
-
let offset = 0;
|
|
1882
|
-
|
|
1883
|
-
for (let i = 0; i < deltas.length; i++) {
|
|
1884
|
-
const delta = deltas[i];
|
|
1885
|
-
const retainOp = delta.retain;
|
|
1886
|
-
const deleteOp = delta.delete;
|
|
1887
|
-
const insertOp = delta.insert;
|
|
1888
|
-
|
|
1889
|
-
if (retainOp !== undefined) {
|
|
1890
|
-
offset += retainOp;
|
|
1891
|
-
} else if (deleteOp !== undefined) {
|
|
1892
|
-
mutationFromCollab(() => {
|
|
1893
|
-
const elements = decoratorStateValue._array.slice(offset, offset + deleteOp);
|
|
1894
|
-
|
|
1895
|
-
elements.forEach(element => {
|
|
1896
|
-
if (lexical.isDecoratorArray(element) || lexical.isDecoratorMap(element)) {
|
|
1897
|
-
element.destroy();
|
|
1898
|
-
}
|
|
1899
|
-
});
|
|
1900
|
-
decoratorStateValue.splice(offset, deleteOp);
|
|
1901
|
-
});
|
|
1902
|
-
} else if (insertOp !== undefined) {
|
|
1903
|
-
syncYjsDecoratorArrayValueToLexical(binding, collabNode, target, decoratorStateValue, offset);
|
|
1904
|
-
} else {
|
|
1905
|
-
throw new Error('Not supported');
|
|
1906
|
-
}
|
|
1907
|
-
}
|
|
1908
|
-
}
|
|
1909
|
-
|
|
1910
|
-
return;
|
|
1911
|
-
}
|
|
1565
|
+
const collabNode = getOrInitCollabNodeFromSharedType(binding, target);
|
|
1912
1566
|
|
|
1913
1567
|
if (collabNode instanceof CollabElementNode && event instanceof yjs.YTextEvent) {
|
|
1914
1568
|
const {
|
package/LexicalYjs.prod.js
CHANGED
|
@@ -4,57 +4,45 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
var
|
|
8
|
-
class
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
function
|
|
13
|
-
|
|
14
|
-
b.
|
|
15
|
-
function
|
|
16
|
-
|
|
17
|
-
function
|
|
18
|
-
function
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
function
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
function
|
|
44
|
-
function
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
m.appendChild(w);h={anchor:{key:h,offset:l},caret:m,color:n,focus:{key:t,offset:u},name:w,range:document.createRange(),selections:[]}}else q=h.anchor,u=h.focus,q.key=l,q.offset=U,u.key=t,u.offset=ua}}}a:{q=a;l=p;m=h;var v=e;n=q.editor;t=n.getRootElement();p=q.cursorsContainer;if(null===p||null===t)break a;h=l.selection;if(null===m){null!==h&&(l.selection=null,na(q,h));break a}else l.selection=m;u=m.range;h=m.caret;l=m.color;q=m.selections;var y=m.anchor,x=m.focus;m=y.key;w=x.key;var A=v.get(m);const H=
|
|
50
|
-
v.get(w);v=n.getElementByKey(m);n=n.getElementByKey(w);y=y.offset;x=x.offset;k.$isTextNode(A)&&(v=oa(v));k.$isTextNode(H)&&(n=oa(n));if(void 0!==A&&void 0!==H&&null!==v&&null!==n){"BR"===v.nodeName&&([v,y]=pa(v));"BR"===n.nodeName&&([n,x]=pa(n));A=v.firstChild;v===n&&null!=A&&"BR"===A.nodeName&&0===y&&0===x&&(x=1);try{u.setStart(v,y),u.setEnd(n,x)}catch(U){break a}!u.collapsed||y===x&&m===w||(u.setStart(n,x),u.setEnd(v,y));n=t.getBoundingClientRect();t=getComputedStyle(t);t=parseFloat(t.paddingLeft)+
|
|
51
|
-
parseFloat(t.paddingRight);m=Array.from(u.getClientRects());u=m.length;w=q.length;for(A=0;A<u;A++)v=m[A],v.width+t===n.width?(m.splice(A--,1),u--):(y=q[A],void 0===y&&(y=document.createElement("span"),q[A]=y,p.appendChild(y)),y.style.cssText=`position:absolute;top:${v.top}px;left:${v.left}px;height:${v.height}px;width:${v.width}px;background-color:rgba(${l}, 0.3);pointer-events:none;z-index:10;`,A===u-1&&h.parentNode!==y&&y.appendChild(h));for(h=w-1;h>=u;h--)p.removeChild(q[h]),q.pop()}}}}c=Array.from(b.keys());
|
|
52
|
-
for(d=0;d<c.length;d++)e=c[d],f.has(e)||(g=b.get(e),void 0!==g&&(g=g.selection,null!==g&&na(a,g),b.delete(e)))}function wa(a,b,c,d){b=b.awareness;var e=b.getLocalState();if(null!==e){var {anchorPos:f,focusPos:g,name:h,color:p,focusing:q}=e,l=e=null;if(null!==d&&(null===f||d.is(c))||null!==c)k.$isRangeSelection(d)&&(e=la(d.anchor,a),l=la(d.focus,a)),(ma(f,e)||ma(g,l))&&b.setLocalState({anchorPos:e,color:p,focusPos:l,focusing:q,name:h})}}
|
|
53
|
-
function Ba(a,b){var {target:c}=b;const d=M(a,c),e=c._lexicalValue;if(void 0!==e&&d instanceof Y)if(c instanceof r.Map)X(a,d,c,e,b.keysChanged);else{if(c instanceof r.Array&&k.isDecoratorArray(e)){b=b.delta;let g=0;for(let h=0;h<b.length;h++){var f=b[h];const p=f.retain,q=f.delete;f=f.insert;if(void 0!==p)g+=p;else if(void 0!==q)S(()=>{e._array.slice(g,g+q).forEach(l=>{(k.isDecoratorArray(l)||k.isDecoratorMap(l))&&l.destroy()});e.splice(g,q)});else if(void 0!==f)ja(a,d,c,e,g);else throw Error("Not supported");
|
|
54
|
-
}}}else if(d instanceof O&&b instanceof r.YTextEvent){const {keysChanged:g,childListChanged:h,delta:p}=b;0<g.size&&d.syncPropertiesFromYjs(a,g);h&&(d.applyChildrenYjsDelta(a,p),d.syncChildrenFromYjs(a))}else if(d instanceof E&&b instanceof r.YMapEvent)({keysChanged:c}=b),0<c.size&&d.syncPropertiesAndTextFromYjs(a,c);else if(d instanceof Y&&b instanceof r.YXmlEvent)({attributesChanged:c}=b),0<c.size&&d.syncPropertiesFromYjs(a,c);else throw Error("Should never happen");}
|
|
55
|
-
const Ca=k.createCommand(),Da=k.createCommand();exports.CONNECTED_COMMAND=Ca;exports.TOGGLE_CONNECT_COMMAND=Da;exports.createBinding=function(a,b,c,d,e){if(void 0===d||null===d)throw Error("Should never happen");b=d.get("root",r.XmlText);b=L(b,null,"root");b._key="root";return{clientID:d.clientID,collabNodeMap:new Map,cursors:new Map,cursorsContainer:null,doc:d,docMap:e,editor:a,id:c,nodeProperties:new Map,root:b}};
|
|
56
|
-
exports.createUndoManager=function(a,b){return new r.UndoManager(b,{trackedOrigins:new Set([a,null])})};exports.initLocalState=function(a,b,c,d){a.awareness.setLocalState({anchorPos:null,color:c,focusPos:null,focusing:d,name:b})};exports.setLocalStateFocus=function(a,b,c,d){({awareness:a}=a);let e=a.getLocalState();null===e&&(e={anchorPos:null,color:c,focusPos:null,focusing:d,name:b});e.focusing=d;a.setLocalState(e)};exports.syncCursorPositions=va;
|
|
57
|
-
exports.syncLexicalUpdateToYjs=function(a,b,c,d,e,f,g,h){Q(a,()=>{d.read(()=>{if(h.has("collaboration")){if(0<g.size){var p=Array.from(g),q=a.collabNodeMap,l=[];for(let m=0;m<p.length;m++){var t=p[m],u=k.$getNodeByKey(t),n=q.get(t);if(n instanceof E)if(k.$isTextNode(u))l.push([n,u.__text]);else{u=n.getOffset();if(-1===u)continue;const w=n._parent;n._normalized=!0;w._xmlText.delete(u,1);q.delete(t);t=w._children;n=t.indexOf(n);t.splice(n,1)}}for(p=0;p<l.length;p++){const [m,w]=l[p];m._text=w}}}else e.has("root")&&
|
|
58
|
-
(l=c._nodeMap,p=k.$getRoot(),q=a.root,q.syncPropertiesFromLexical(a,p,l),q.syncChildrenFromLexical(a,p,l,e,f)),l=k.$getSelection(),wa(a,b,c._selection,l)})})};
|
|
59
|
-
exports.syncYjsChangesToLexical=function(a,b,c){const d=a.editor,e=d._editorState;d.update(()=>{var f=d._pendingEditorState;for(var g=0;g<c.length;g++)Ba(a,c[g]);var h=k.$getSelection();if(k.$isRangeSelection(h))if(ca(h)){g=e._selection;if(k.$isRangeSelection(g)){const p=z.$createOffsetView(d,0,e);f=z.$createOffsetView(d,0,f);const [q,l]=p.getOffsetsFromSelection(g);f=f.createSelectionFromOffsets(q,l,p);null!==f?k.$setSelection(f):(qa(a,b),ca(h)&&(h=k.$getRoot(),0===h.getChildrenSize()&&h.append(k.$createParagraphNode()),
|
|
60
|
-
k.$getRoot().selectEnd()))}wa(a,b,g,k.$getSelection())}else qa(a,b)},{onUpdate:()=>{va(a,b)},skipTransforms:!0,tag:"collaboration"})};
|
|
7
|
+
var q=require("lexical"),w=require("yjs"),z=require("@lexical/offset");class B{constructor(a,b){this._key="";this._map=a;this._parent=b;this._type="linebreak"}getNode(){const a=q.$getNodeByKey(this._key);return q.$isLineBreakNode(a)?a:null}getKey(){return this._key}getSharedType(){return this._map}getType(){return this._type}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}destroy(a){a.collabNodeMap.delete(this._key)}}function C(a,b){b=new B(a,b);return a._collabNode=b}
|
|
8
|
+
class D{constructor(a,b,c,d){this._key="";this._map=a;this._parent=c;this._text=b;this._type=d;this._normalized=!1}getPrevNode(a){if(null===a)return null;a=a.get(this._key);return q.$isTextNode(a)?a:null}getNode(){const a=q.$getNodeByKey(this._key);return q.$isTextNode(a)?a:null}getSharedType(){return this._map}getType(){return this._type}getKey(){return this._key}getSize(){return this._text.length+(this._normalized?0:1)}getOffset(){return this._parent.getChildOffset(this)}spliceText(a,b,c){const d=
|
|
9
|
+
this._parent._xmlText;a=this.getOffset()+1+a;0!==b&&d.delete(a,b);""!==c&&d.insert(a,c)}syncPropertiesAndTextFromLexical(a,b,c){var d=this.getPrevNode(c);c=b.__text;F(a,this._map,d,b);if(null!==d&&(a=d.__text,a!==c)){d=b.__key;b=a;var e=q.$getSelection();a=c.length;q.$isRangeSelection(e)&&e.isCollapsed()&&(e=e.anchor,e.key===d&&(a=e.offset));d=b.length;const f=c.length;let g=e=0;for(;e<d&&e<f&&b[e]===c[e]&&e<a;)e++;for(;g+e<d&&g+e<f&&b[d-g-1]===c[f-g-1];)g++;for(;g+e<d&&g+e<f&&b[e]===c[e];)e++;b=
|
|
10
|
+
e;a=c.slice(e,f-g);d=d-e-g;this.spliceText(b,d,a);this._text=c}}syncPropertiesAndTextFromYjs(a,b){const c=this.getNode();if(null===c)throw Error("Should never happen");G(a,this._map,c,b);a=this._text;c.__text!==a&&(c.getWritable().__text=a)}destroy(a){a.collabNodeMap.delete(this._key)}}function H(a,b,c,d){b=new D(a,b,c,d);return a._collabNode=b}const J=new Set(["__key","__children","__parent","__cachedText","__text"]);
|
|
11
|
+
function K(a){a=q.$getNodeByKey(a);if(null===a)throw Error("Should never happen");return a}
|
|
12
|
+
function L(a,b,c){const d=b.__type;if(q.$isElementNode(b)){var e=new w.XmlText;c=M(e,c,d);c.syncPropertiesFromLexical(a,b,null);c.syncChildrenFromLexical(a,b,null,null,null)}else if(q.$isTextNode(b))e=new w.Map,c=H(e,b.__text,c,d),c.syncPropertiesAndTextFromLexical(a,b,null);else if(q.$isLineBreakNode(b))a=new w.Map,a.set("__type","linebreak"),c=C(a,c);else if(q.$isDecoratorNode(b))e=new w.XmlElement,c=O(e,c,d),c.syncPropertiesFromLexical(a,b,null);else throw Error("Should never happen");c._key=b.__key;
|
|
13
|
+
return c}
|
|
14
|
+
function P(a,b,c){const d=b._collabNode;if(void 0===d){var e=a.editor._nodes;const f=b instanceof w.Map?b.get("__type"):b.getAttribute("__type");if(null==f)throw Error("Should never happen");if(void 0===e.get(f))throw Error("Should never happen");e=b.parent;a=void 0===c&&null!==e?P(a,e):c||null;if(!(a instanceof Q))throw Error("Should never happen");if(b instanceof w.XmlText)return M(b,a,f);if(b instanceof w.Map){if(null===a)throw Error("Should never happen");return"linebreak"===f?C(b,a):H(b,"",a,
|
|
15
|
+
f)}if(b instanceof w.XmlElement)return O(b,a,f)}return d}function G(a,b,c,d){d=null===d?b instanceof w.Map?Array.from(b.keys()):Object.keys(b.getAttributes()):Array.from(d);let e;for(let g=0;g<d.length;g++){const h=d[g];if(J.has(h))continue;var f=c[h];let k=b instanceof w.Map?b.get(h):b.getAttribute(h);if(f!==k){if(k instanceof w.Doc){const l=a.docMap;f instanceof w.Doc&&l.delete(f.guid);f=q.createEditor();const m=k.guid;f._key=m;l.set(m,k);k=f}void 0===e&&(e=c.getWritable());e[h]=k}}}
|
|
16
|
+
function F(a,b,c,d){var e=d.__type,f=a.nodeProperties;let g=f.get(e);void 0===g&&(g=Object.keys(d).filter(k=>!J.has(k)),f.set(e,g));e=a.editor.constructor;for(f=0;f<g.length;f++){const k=g[f];var h=null===c?void 0:c[k];let l=d[k];if(h!==l){if(l instanceof e){const m=a.docMap;h instanceof e&&m.delete(h._key);h=new w.Doc;const r=h.guid;l._key=r;m.set(r,h);l=h}b instanceof w.Map?b.set(k,l):b.setAttribute(k,l)}}}
|
|
17
|
+
function R(a,b,c){let d=0,e=0;const f=a._children,g=f.length;for(;e<g;e++){a=f[e];const h=d,k=a.getSize();d+=k;if((c?d>=b:d>b)&&a instanceof D)return b=b-h-1,0>b&&(b=0),{length:k-b,node:a,nodeIndex:e,offset:b};if(d>b)return{length:0,node:a,nodeIndex:e,offset:h};if(e===g-1)return{length:0,node:null,nodeIndex:e+1,offset:h+1}}return{length:0,node:null,nodeIndex:0,offset:0}}
|
|
18
|
+
function S(a){const b=a.anchor;a=a.focus;let c=!1;try{const d=b.getNode(),e=a.getNode();if(!d.isAttached()||!e.isAttached()||q.$isTextNode(d)&&b.offset>d.getTextContentSize()||q.$isTextNode(e)&&a.offset>e.getTextContentSize())c=!0}catch(d){c=!0}return c}function aa(a,b){a.doc.transact(b,a)}
|
|
19
|
+
class T{constructor(a,b,c){this._key="";this._xmlElem=a;this._parent=b;this._type=c;this._unobservers=new Set}getPrevNode(a){if(null===a)return null;a=a.get(this._key);return q.$isDecoratorNode(a)?a:null}getNode(){const a=q.$getNodeByKey(this._key);return q.$isDecoratorNode(a)?a:null}getSharedType(){return this._xmlElem}getType(){return this._type}getKey(){return this._key}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}syncPropertiesFromLexical(a,b,c){c=this.getPrevNode(c);
|
|
20
|
+
F(a,this._xmlElem,c,b)}syncPropertiesFromYjs(a,b){const c=this.getNode();if(null===c)throw Error("Should never happen");G(a,this._xmlElem,c,b)}destroy(a){a.collabNodeMap.delete(this._key);this._unobservers.forEach(b=>b());this._unobservers.clear()}}function O(a,b,c){b=new T(a,b,c);return a._collabNode=b}
|
|
21
|
+
class Q{constructor(a,b,c){this._key="";this._children=[];this._xmlText=a;this._type=c;this._parent=b}getPrevNode(a){if(null===a)return null;a=a.get(this._key);return q.$isElementNode(a)?a:null}getNode(){const a=q.$getNodeByKey(this._key);return q.$isElementNode(a)?a:null}getSharedType(){return this._xmlText}getType(){return this._type}getKey(){return this._key}isEmpty(){return 0===this._children.length}getSize(){return 1}getOffset(){const a=this._parent;if(null===a)throw Error("Should never happen");
|
|
22
|
+
return a.getChildOffset(this)}syncPropertiesFromYjs(a,b){const c=this.getNode();if(null===c)throw this.getNode(),Error("Should never happen");G(a,this._xmlText,c,b)}applyChildrenYjsDelta(a,b){const c=this._children;let d=0;for(let l=0;l<b.length;l++){var e=b[l],f=e.insert,g=e.delete;if(null!=e.retain)d+=e.retain;else if("number"===typeof g)for(f=g;0<f;){const {node:m,nodeIndex:r,offset:t,length:p}=R(this,d,!1);if(m instanceof Q||m instanceof B||m instanceof T)c.splice(r,1),--f;else if(m instanceof
|
|
23
|
+
D){e=Math.min(f,p);g=0!==r?c[r-1]:null;var h=m.getSize();if(0===t&&1===e&&0<r&&g instanceof D&&p===h&&0===Array.from(m._map.keys()).length)g._text+=m._text,c.splice(r,1);else if(0===t&&e===h)c.splice(r,1);else{g=m._text;h=t;var k=e;g=g.slice(0,h)+""+g.slice(h+k);m._text=g}f-=e}else break}else if(null!=f)if("string"===typeof f){const {node:m,offset:r}=R(this,d,!0);m instanceof D?(e=m._text,g=r,h=f,e=e.slice(0,g)+h+e.slice(g+0),m._text=e):this._xmlText.delete(r,f.length);d+=f.length}else e=f,{nodeIndex:f}=
|
|
24
|
+
R(this,d,!1),e=P(a,e,this),c.splice(f,0,e),d+=1;else throw Error("Unexpected delta format");}}syncChildrenFromYjs(a){var b=this.getNode();if(null===b)throw this.getNode(),Error("Should never happen");var c=b.__key;const d=b.__children;var e=[];const f=d.length,g=this._children,h=g.length,k=a.collabNodeMap,l=new Set;let m,r;h!==f&&(r=U(b,r,e));let t=0;for(let y=0;y<h;y++){var p=d[t];const x=g[y];var n=x.getNode(),v=x._key;if(null!==n&&p===v){n=q.$isTextNode(n);l.add(p);if(n)if(x._key=p,x instanceof
|
|
25
|
+
Q)n=x._xmlText,x.syncPropertiesFromYjs(a,null),x.applyChildrenYjsDelta(a,n.toDelta()),x.syncChildrenFromYjs(a);else if(x instanceof D)x.syncPropertiesAndTextFromYjs(a,null);else if(x instanceof T)x.syncPropertiesFromYjs(a,null);else if(!(x instanceof B))throw Error("Should never happen");e[y]=p;t++}else{if(void 0===m)for(m=new Set,v=0;v<h;v++){var u=g[v]._key;""!==u&&m.add(u)}if(null===n||void 0===p||m.has(p)){r=U(b,r,e);p=a;n=x;v=c;u=n.getType();u=p.editor._nodes.get(u);if(void 0===u)throw Error("createLexicalNode failed");
|
|
26
|
+
u=new u.klass;u.__parent=v;n._key=u.__key;n instanceof Q?(v=n._xmlText,n.syncPropertiesFromYjs(p,null),n.applyChildrenYjsDelta(p,v.toDelta()),n.syncChildrenFromYjs(p)):n instanceof D?n.syncPropertiesAndTextFromYjs(p,null):n instanceof T&&n.syncPropertiesFromYjs(p,null);p.collabNodeMap.set(u.__key,n);p=u.__key;k.set(p,x);e[y]=p}else y--,t++}}for(b=0;b<f;b++)e=d[b],l.has(e)||(c=K(e).getWritable(),e=a.collabNodeMap.get(e),void 0!==e&&e.destroy(a),c.__parent=null)}syncPropertiesFromLexical(a,b,c){F(a,
|
|
27
|
+
this._xmlText,this.getPrevNode(c),b)}_syncChildFromLexical(a,b,c,d,e,f){b=this._children[b];c=K(c);b instanceof Q&&q.$isElementNode(c)?(b.syncPropertiesFromLexical(a,c,d),b.syncChildrenFromLexical(a,c,d,e,f)):b instanceof D&&q.$isTextNode(c)?b.syncPropertiesAndTextFromLexical(a,c,d):b instanceof T&&q.$isDecoratorNode(c)&&b.syncPropertiesFromLexical(a,c,d)}syncChildrenFromLexical(a,b,c,d,e){var f=this.getPrevNode(c);const g=null===f?[]:f.__children;f=b.__children;const h=g.length-1,k=f.length-1,l=
|
|
28
|
+
a.collabNodeMap;let m,r,t=0;for(b=0;t<=h&&b<=k;){var p=g[t];const v=f[b];if(p===v)this._syncChildFromLexical(a,b,v,c,d,e),t++,b++;else{void 0===m&&(m=new Set(g));void 0===r&&(r=new Set(f));var n=r.has(p);p=m.has(v);n?(n=K(v),n=L(a,n,this),l.set(v,n),p?(this.splice(a,b,1,n),t++):this.splice(a,b,0,n),b++):(this.splice(a,b,1),t++)}}c=t>h;d=b>k;if(c&&!d)for(;b<=k;++b)c=f[b],d=K(c),d=L(a,d,this),this.append(d),l.set(c,d);else if(d&&!c)for(f=this._children.length-1;f>=b;f--)this.splice(a,f,1)}append(a){const b=
|
|
29
|
+
this._xmlText;var c=this._children;c=c[c.length-1];c=void 0!==c?c.getOffset()+c.getSize():0;if(a instanceof Q)b.insertEmbed(c,a._xmlText);else if(a instanceof D){const d=a._map;null===d.parent&&b.insertEmbed(c,d);b.insert(c+1,a._text)}else a instanceof B?b.insertEmbed(c,a._map):a instanceof T&&b.insertEmbed(c,a._xmlElem);this._children.push(a)}splice(a,b,c,d){const e=this._children;var f=e[b];if(void 0===f)if(void 0!==d)this.append(d);else throw Error("Should never happen");else{var g=f.getOffset();
|
|
30
|
+
if(-1===g)throw Error("Should never happen");var h=this._xmlText;0!==c&&h.delete(g,f.getSize());d instanceof Q?h.insertEmbed(g,d._xmlText):d instanceof D?(f=d._map,null===f.parent&&h.insertEmbed(g,f),h.insert(g+1,d._text)):d instanceof B?h.insertEmbed(g,d._map):d instanceof T&&h.insertEmbed(g,d._xmlElem);if(0!==c)for(g=e.slice(b,b+c),h=0;h<g.length;h++)g[h].destroy(a);void 0!==d?e.splice(b,c,d):e.splice(b,c)}}getChildOffset(a){let b=0;const c=this._children;for(let d=0;d<c.length;d++){const e=c[d];
|
|
31
|
+
if(e===a)return b;b+=e.getSize()}return-1}destroy(a){const b=a.collabNodeMap,c=this._children;for(let d=0;d<c.length;d++)c[d].destroy(a);b.delete(this._key)}}function U(a,b,c){return void 0===b?(a=a.getWritable(),a.__children=c,a):b}function M(a,b,c){b=new Q(a,b,c);return a._collabNode=b}
|
|
32
|
+
function V(a,b){b=b.collabNodeMap.get(a.key);if(void 0===b)return null;a=a.offset;let c=b.getSharedType();if(b instanceof D){c=b._parent._xmlText;b=b.getOffset();if(-1===b)return null;a=b+1+a}return w.createRelativePositionFromTypeIndex(c,a)}function W(a,b){if(null==a){if(null!=b)return!0}else if(null==b||!w.compareRelativePositions(a,b))return!0;return!1}function X(a,b){a=a.cursorsContainer;if(null!==a){b=b.selections;const c=b.length;for(let d=0;d<c;d++)a.removeChild(b[d])}}
|
|
33
|
+
function Y(a){for(;null!=a;){if(3===a.nodeType)return a;a=a.firstChild}return null}function ba(a){const b=a.parentNode;if(null==b)throw Error("Should never happen");return[b,Array.from(b.childNodes).indexOf(a)]}
|
|
34
|
+
function ca(a,b){var c=b.awareness.getLocalState();if(null!==c&&(b=c.anchorPos,c=c.focusPos,null!==b&&null!==c&&(b=w.createAbsolutePositionFromRelativePosition(b,a.doc),a=w.createAbsolutePositionFromRelativePosition(c,a.doc),null!==b&&null!==a))){const [e,f]=Z(b.type,b.index),[g,h]=Z(a.type,a.index);if(null!==e&&null!==g){const k=e.getKey();a=g.getKey();b=q.$getSelection();if(q.$isRangeSelection(b)){var d=b.anchor;c=b.focus;if(d.key!==k||d.offset!==f)d=q.$getNodeByKey(k),b.anchor.set(k,f,q.$isElementNode(d)?
|
|
35
|
+
"element":"text");if(c.key!==a||c.offset!==h)c=q.$getNodeByKey(a),b.focus.set(a,h,q.$isElementNode(c)?"element":"text")}}}}function Z(a,b){a=a._collabNode;if(void 0===a)return[null,0];if(a instanceof Q){const {node:c,offset:d}=R(a,b,!0);return null===c?[a,0]:[c,d]}return[null,0]}
|
|
36
|
+
function ia(a,b){var c=Array.from(b.awareness.getStates()),d=a.clientID;b=a.cursors;var e=a.editor._editorState._nodeMap;const f=new Set;for(var g=0;g<c.length;g++){const [I,ka]=c[g];if(I!==d){f.add(I);const {anchorPos:da,focusPos:ea,name:la,color:ma,focusing:na}=ka;var h=null,k=b.get(I);void 0===k&&(k={color:ma,name:la,selection:null},b.set(I,k));if(null!==da&&null!==ea&&na){var l=w.createAbsolutePositionFromRelativePosition(da,a.doc),m=w.createAbsolutePositionFromRelativePosition(ea,a.doc);if(null!==
|
|
37
|
+
l&&null!==m){const [E,N]=Z(l.type,l.index),[fa,ha]=Z(m.type,m.index);if(null!==E&&null!==fa){m=E.getKey();var r=fa.getKey();h=k.selection;if(null===h){l=k;h=m;m=N;var t=ha,p=l.color,n=document.createElement("span");n.style.cssText=`position:absolute;top:0;bottom:0;right:-1px;width:1px;background-color:rgb(${p});z-index:10;`;var v=document.createElement("span");v.textContent=l.name;v.style.cssText=`position:absolute;left:-2px;top:-16px;background-color:rgb(${p});color:#fff;line-height:12px;height:12px;font-size:12px;padding:2px;font-family:Arial;font-weight:bold;white-space:nowrap;`;
|
|
38
|
+
n.appendChild(v);h={anchor:{key:h,offset:m},caret:n,color:p,focus:{key:r,offset:t},name:v,range:document.createRange(),selections:[]}}else l=h.anchor,t=h.focus,l.key=m,l.offset=N,t.key=r,t.offset=ha}}}a:{l=a;m=k;n=h;var u=e;p=l.editor;r=p.getRootElement();k=l.cursorsContainer;if(null===k||null===r)break a;h=m.selection;if(null===n){null!==h&&(m.selection=null,X(l,h));break a}else m.selection=n;t=n.range;h=n.caret;m=n.color;l=n.selections;var y=n.anchor,x=n.focus;n=y.key;v=x.key;var A=u.get(n);const E=
|
|
39
|
+
u.get(v);u=p.getElementByKey(n);p=p.getElementByKey(v);y=y.offset;x=x.offset;q.$isTextNode(A)&&(u=Y(u));q.$isTextNode(E)&&(p=Y(p));if(void 0!==A&&void 0!==E&&null!==u&&null!==p){"BR"===u.nodeName&&([u,y]=ba(u));"BR"===p.nodeName&&([p,x]=ba(p));A=u.firstChild;u===p&&null!=A&&"BR"===A.nodeName&&0===y&&0===x&&(x=1);try{t.setStart(u,y),t.setEnd(p,x)}catch(N){break a}!t.collapsed||y===x&&n===v||(t.setStart(p,x),t.setEnd(u,y));p=r.getBoundingClientRect();r=getComputedStyle(r);r=parseFloat(r.paddingLeft)+
|
|
40
|
+
parseFloat(r.paddingRight);n=Array.from(t.getClientRects());t=n.length;v=l.length;for(A=0;A<t;A++)u=n[A],u.width+r===p.width?(n.splice(A--,1),t--):(y=l[A],void 0===y&&(y=document.createElement("span"),l[A]=y,k.appendChild(y)),y.style.cssText=`position:absolute;top:${u.top}px;left:${u.left}px;height:${u.height}px;width:${u.width}px;background-color:rgba(${m}, 0.3);pointer-events:none;z-index:10;`,A===t-1&&h.parentNode!==y&&y.appendChild(h));for(h=v-1;h>=t;h--)k.removeChild(l[h]),l.pop()}}}}c=Array.from(b.keys());
|
|
41
|
+
for(d=0;d<c.length;d++)e=c[d],f.has(e)||(g=b.get(e),void 0!==g&&(g=g.selection,null!==g&&X(a,g),b.delete(e)))}function ja(a,b,c,d){b=b.awareness;var e=b.getLocalState();if(null!==e){var {anchorPos:f,focusPos:g,name:h,color:k,focusing:l}=e,m=e=null;if(null!==d&&(null===f||d.is(c))||null!==c)q.$isRangeSelection(d)&&(e=V(d.anchor,a),m=V(d.focus,a)),(W(f,e)||W(g,m))&&b.setLocalState({anchorPos:e,color:k,focusPos:m,focusing:l,name:h})}}const oa=q.createCommand(),pa=q.createCommand();
|
|
42
|
+
exports.CONNECTED_COMMAND=oa;exports.TOGGLE_CONNECT_COMMAND=pa;exports.createBinding=function(a,b,c,d,e){if(void 0===d||null===d)throw Error("Should never happen");b=d.get("root",w.XmlText);b=M(b,null,"root");b._key="root";return{clientID:d.clientID,collabNodeMap:new Map,cursors:new Map,cursorsContainer:null,doc:d,docMap:e,editor:a,id:c,nodeProperties:new Map,root:b}};exports.createUndoManager=function(a,b){return new w.UndoManager(b,{trackedOrigins:new Set([a,null])})};
|
|
43
|
+
exports.initLocalState=function(a,b,c,d){a.awareness.setLocalState({anchorPos:null,color:c,focusPos:null,focusing:d,name:b})};exports.setLocalStateFocus=function(a,b,c,d){({awareness:a}=a);let e=a.getLocalState();null===e&&(e={anchorPos:null,color:c,focusPos:null,focusing:d,name:b});e.focusing=d;a.setLocalState(e)};exports.syncCursorPositions=ia;
|
|
44
|
+
exports.syncLexicalUpdateToYjs=function(a,b,c,d,e,f,g,h){aa(a,()=>{d.read(()=>{if(h.has("collaboration")){if(0<g.size){var k=Array.from(g),l=a.collabNodeMap,m=[];for(let n=0;n<k.length;n++){var r=k[n],t=q.$getNodeByKey(r),p=l.get(r);if(p instanceof D)if(q.$isTextNode(t))m.push([p,t.__text]);else{t=p.getOffset();if(-1===t)continue;const v=p._parent;p._normalized=!0;v._xmlText.delete(t,1);l.delete(r);r=v._children;p=r.indexOf(p);r.splice(p,1)}}for(k=0;k<m.length;k++){const [n,v]=m[k];n._text=v}}}else e.has("root")&&
|
|
45
|
+
(m=c._nodeMap,k=q.$getRoot(),l=a.root,l.syncPropertiesFromLexical(a,k,m),l.syncChildrenFromLexical(a,k,m,e,f)),m=q.$getSelection(),ja(a,b,c._selection,m)})})};
|
|
46
|
+
exports.syncYjsChangesToLexical=function(a,b,c){const d=a.editor,e=d._editorState;d.update(()=>{var f=d._pendingEditorState;for(var g=0;g<c.length;g++){var h=a,k=c[g],{target:l}=k;l=P(h,l);if(l instanceof Q&&k instanceof w.YTextEvent){const {keysChanged:m,childListChanged:r,delta:t}=k;0<m.size&&l.syncPropertiesFromYjs(h,m);r&&(l.applyChildrenYjsDelta(h,t),l.syncChildrenFromYjs(h))}else if(l instanceof D&&k instanceof w.YMapEvent)({keysChanged:k}=k),0<k.size&&l.syncPropertiesAndTextFromYjs(h,k);else if(l instanceof
|
|
47
|
+
T&&k instanceof w.YXmlEvent)({attributesChanged:k}=k),0<k.size&&l.syncPropertiesFromYjs(h,k);else throw Error("Should never happen");}g=q.$getSelection();if(q.$isRangeSelection(g))if(S(g)){h=e._selection;if(q.$isRangeSelection(h)){l=z.$createOffsetView(d,0,e);f=z.$createOffsetView(d,0,f);const [m,r]=l.getOffsetsFromSelection(h);f=f.createSelectionFromOffsets(m,r,l);null!==f?q.$setSelection(f):(ca(a,b),S(g)&&(f=q.$getRoot(),0===f.getChildrenSize()&&f.append(q.$createParagraphNode()),q.$getRoot().selectEnd()))}ja(a,
|
|
48
|
+
b,h,q.$getSelection())}else ca(a,b)},{onUpdate:()=>{ia(a,b)},skipTransforms:!0,tag:"collaboration"})};
|
package/package.json
CHANGED
|
@@ -11,13 +11,13 @@
|
|
|
11
11
|
"crdt"
|
|
12
12
|
],
|
|
13
13
|
"license": "MIT",
|
|
14
|
-
"version": "0.
|
|
14
|
+
"version": "0.2.0",
|
|
15
15
|
"main": "LexicalYjs.js",
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@lexical/offset": "0.
|
|
17
|
+
"@lexical/offset": "0.2.0"
|
|
18
18
|
},
|
|
19
19
|
"peerDependencies": {
|
|
20
|
-
"lexical": "0.
|
|
20
|
+
"lexical": "0.2.0",
|
|
21
21
|
"yjs": ">=13.5.22"
|
|
22
22
|
},
|
|
23
23
|
"repository": {
|