@lexical/yjs 0.6.4 → 0.7.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/CollabElementNode.d.ts +3 -1
- package/LexicalYjs.dev.js +119 -27
- package/LexicalYjs.js.flow +2 -1
- package/LexicalYjs.prod.js +43 -39
- package/SyncEditorStates.d.ts +3 -1
- package/Utils.d.ts +3 -1
- package/package.json +3 -3
package/CollabElementNode.d.ts
CHANGED
|
@@ -6,11 +6,12 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
import type { Binding } from '.';
|
|
9
|
-
import type { ElementNode,
|
|
9
|
+
import type { ElementNode, NodeKey, NodeMap } from 'lexical';
|
|
10
10
|
import type { AbstractType, XmlText } from 'yjs';
|
|
11
11
|
import { CollabDecoratorNode } from './CollabDecoratorNode';
|
|
12
12
|
import { CollabLineBreakNode } from './CollabLineBreakNode';
|
|
13
13
|
import { CollabTextNode } from './CollabTextNode';
|
|
14
|
+
declare type IntentionallyMarkedAsDirtyElement = boolean;
|
|
14
15
|
export declare class CollabElementNode {
|
|
15
16
|
_key: NodeKey;
|
|
16
17
|
_children: Array<CollabElementNode | CollabTextNode | CollabDecoratorNode | CollabLineBreakNode>;
|
|
@@ -45,3 +46,4 @@ export declare class CollabElementNode {
|
|
|
45
46
|
destroy(binding: Binding): void;
|
|
46
47
|
}
|
|
47
48
|
export declare function $createCollabElementNode(xmlText: XmlText, parent: null | CollabElementNode, type: string): CollabElementNode;
|
|
49
|
+
export {};
|
package/LexicalYjs.dev.js
CHANGED
|
@@ -237,7 +237,7 @@ function $createCollabTextNode(map, text, parent, type) {
|
|
|
237
237
|
* LICENSE file in the root directory of this source tree.
|
|
238
238
|
*
|
|
239
239
|
*/
|
|
240
|
-
const excludedProperties = new Set(['__key', '
|
|
240
|
+
const excludedProperties = new Set(['__key', '__parent', '__cachedText', '__text', '__size', '__next', '__prev', '__first', '__last']);
|
|
241
241
|
function $getNodeByKeyOrThrow(key) {
|
|
242
242
|
const node = lexical.$getNodeByKey(key);
|
|
243
243
|
|
|
@@ -521,6 +521,82 @@ function doesSelectionNeedRecovering(selection) {
|
|
|
521
521
|
function syncWithTransaction(binding, fn) {
|
|
522
522
|
binding.doc.transact(fn, binding);
|
|
523
523
|
}
|
|
524
|
+
function createChildrenArray(element, nodeMap) {
|
|
525
|
+
const children = [];
|
|
526
|
+
let nodeKey = element.__first;
|
|
527
|
+
|
|
528
|
+
while (nodeKey !== null) {
|
|
529
|
+
const node = nodeMap === null ? lexical.$getNodeByKey(nodeKey) : nodeMap.get(nodeKey);
|
|
530
|
+
|
|
531
|
+
if (node === null || node === undefined) {
|
|
532
|
+
{
|
|
533
|
+
throw Error(`createChildrenArray: node does not exist in nodeMap`);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
children.push(nodeKey);
|
|
538
|
+
nodeKey = node.__next;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
return children;
|
|
542
|
+
}
|
|
543
|
+
function removeFromParent(node) {
|
|
544
|
+
const oldParent = node.getParent();
|
|
545
|
+
|
|
546
|
+
if (oldParent !== null) {
|
|
547
|
+
const writableNode = node.getWritable();
|
|
548
|
+
const writableParent = oldParent.getWritable();
|
|
549
|
+
const prevSibling = node.getPreviousSibling();
|
|
550
|
+
const nextSibling = node.getNextSibling(); // TODO: this function duplicates a bunch of operations, can be simplified.
|
|
551
|
+
|
|
552
|
+
if (prevSibling === null) {
|
|
553
|
+
if (nextSibling !== null) {
|
|
554
|
+
const writableNextSibling = nextSibling.getWritable();
|
|
555
|
+
writableParent.__first = nextSibling.__key;
|
|
556
|
+
writableNextSibling.__prev = null;
|
|
557
|
+
} else {
|
|
558
|
+
writableParent.__first = null;
|
|
559
|
+
}
|
|
560
|
+
} else {
|
|
561
|
+
const writablePrevSibling = prevSibling.getWritable();
|
|
562
|
+
|
|
563
|
+
if (nextSibling !== null) {
|
|
564
|
+
const writableNextSibling = nextSibling.getWritable();
|
|
565
|
+
writableNextSibling.__prev = writablePrevSibling.__key;
|
|
566
|
+
writablePrevSibling.__next = writableNextSibling.__key;
|
|
567
|
+
} else {
|
|
568
|
+
writablePrevSibling.__next = null;
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
writableNode.__prev = null;
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
if (nextSibling === null) {
|
|
575
|
+
if (prevSibling !== null) {
|
|
576
|
+
const writablePrevSibling = prevSibling.getWritable();
|
|
577
|
+
writableParent.__last = prevSibling.__key;
|
|
578
|
+
writablePrevSibling.__next = null;
|
|
579
|
+
} else {
|
|
580
|
+
writableParent.__last = null;
|
|
581
|
+
}
|
|
582
|
+
} else {
|
|
583
|
+
const writableNextSibling = nextSibling.getWritable();
|
|
584
|
+
|
|
585
|
+
if (prevSibling !== null) {
|
|
586
|
+
const writablePrevSibling = prevSibling.getWritable();
|
|
587
|
+
writablePrevSibling.__next = writableNextSibling.__key;
|
|
588
|
+
writableNextSibling.__prev = writablePrevSibling.__key;
|
|
589
|
+
} else {
|
|
590
|
+
writableNextSibling.__prev = null;
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
writableNode.__next = null;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
writableParent.__size--;
|
|
597
|
+
writableNode.__parent = null;
|
|
598
|
+
}
|
|
599
|
+
}
|
|
524
600
|
|
|
525
601
|
/**
|
|
526
602
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
@@ -770,23 +846,21 @@ class CollabElementNode {
|
|
|
770
846
|
}
|
|
771
847
|
|
|
772
848
|
const key = lexicalNode.__key;
|
|
773
|
-
const prevLexicalChildrenKeys = lexicalNode
|
|
774
|
-
const nextLexicalChildrenKeys = [];
|
|
849
|
+
const prevLexicalChildrenKeys = createChildrenArray(lexicalNode, null);
|
|
775
850
|
const lexicalChildrenKeysLength = prevLexicalChildrenKeys.length;
|
|
776
851
|
const collabChildren = this._children;
|
|
777
852
|
const collabChildrenLength = collabChildren.length;
|
|
778
853
|
const collabNodeMap = binding.collabNodeMap;
|
|
779
854
|
const visitedKeys = new Set();
|
|
780
|
-
let collabKeys;
|
|
781
|
-
|
|
855
|
+
let collabKeys;
|
|
782
856
|
let writableLexicalNode;
|
|
857
|
+
let prevIndex = 0;
|
|
858
|
+
let prevChildNode = null;
|
|
783
859
|
|
|
784
860
|
if (collabChildrenLength !== lexicalChildrenKeysLength) {
|
|
785
|
-
writableLexicalNode =
|
|
861
|
+
writableLexicalNode = lexicalNode.getWritable();
|
|
786
862
|
}
|
|
787
863
|
|
|
788
|
-
let prevIndex = 0;
|
|
789
|
-
|
|
790
864
|
for (let i = 0; i < collabChildrenLength; i++) {
|
|
791
865
|
const lexicalChildKey = prevLexicalChildrenKeys[prevIndex];
|
|
792
866
|
const childCollabNode = collabChildren[i];
|
|
@@ -816,8 +890,7 @@ class CollabElementNode {
|
|
|
816
890
|
}
|
|
817
891
|
}
|
|
818
892
|
}
|
|
819
|
-
|
|
820
|
-
nextLexicalChildrenKeys[i] = lexicalChildKey;
|
|
893
|
+
prevChildNode = collabLexicalChildNode;
|
|
821
894
|
prevIndex++;
|
|
822
895
|
} else {
|
|
823
896
|
if (collabKeys === undefined) {
|
|
@@ -834,17 +907,47 @@ class CollabElementNode {
|
|
|
834
907
|
}
|
|
835
908
|
|
|
836
909
|
if (collabLexicalChildNode !== null && lexicalChildKey !== undefined && !collabKeys.has(lexicalChildKey)) {
|
|
910
|
+
const nodeToRemove = $getNodeByKeyOrThrow(lexicalChildKey);
|
|
911
|
+
removeFromParent(nodeToRemove);
|
|
837
912
|
i--;
|
|
838
913
|
prevIndex++;
|
|
839
914
|
continue;
|
|
840
915
|
}
|
|
841
916
|
|
|
842
|
-
writableLexicalNode =
|
|
917
|
+
writableLexicalNode = lexicalNode.getWritable(); // Create/Replace
|
|
843
918
|
|
|
844
919
|
const lexicalChildNode = createLexicalNodeFromCollabNode(binding, childCollabNode, key);
|
|
845
920
|
const childKey = lexicalChildNode.__key;
|
|
846
921
|
collabNodeMap.set(childKey, childCollabNode);
|
|
847
|
-
|
|
922
|
+
|
|
923
|
+
if (prevChildNode === null) {
|
|
924
|
+
const nextSibling = writableLexicalNode.getFirstChild();
|
|
925
|
+
writableLexicalNode.__first = childKey;
|
|
926
|
+
|
|
927
|
+
if (nextSibling !== null) {
|
|
928
|
+
const writableNextSibling = nextSibling.getWritable();
|
|
929
|
+
writableNextSibling.__prev = childKey;
|
|
930
|
+
lexicalChildNode.__next = writableNextSibling.__key;
|
|
931
|
+
}
|
|
932
|
+
} else {
|
|
933
|
+
const writablePrevChildNode = prevChildNode.getWritable();
|
|
934
|
+
const nextSibling = prevChildNode.getNextSibling();
|
|
935
|
+
writablePrevChildNode.__next = childKey;
|
|
936
|
+
lexicalChildNode.__prev = prevChildNode.__key;
|
|
937
|
+
|
|
938
|
+
if (nextSibling !== null) {
|
|
939
|
+
const writableNextSibling = nextSibling.getWritable();
|
|
940
|
+
writableNextSibling.__prev = childKey;
|
|
941
|
+
lexicalChildNode.__next = writableNextSibling.__key;
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
if (i === collabChildrenLength - 1) {
|
|
946
|
+
writableLexicalNode.__last = childKey;
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
writableLexicalNode.__size++;
|
|
950
|
+
prevChildNode = lexicalChildNode;
|
|
848
951
|
}
|
|
849
952
|
}
|
|
850
953
|
|
|
@@ -853,14 +956,14 @@ class CollabElementNode {
|
|
|
853
956
|
|
|
854
957
|
if (!visitedKeys.has(lexicalChildKey)) {
|
|
855
958
|
// Remove
|
|
856
|
-
const lexicalChildNode = $getNodeByKeyOrThrow(lexicalChildKey)
|
|
959
|
+
const lexicalChildNode = $getNodeByKeyOrThrow(lexicalChildKey);
|
|
857
960
|
const collabNode = binding.collabNodeMap.get(lexicalChildKey);
|
|
858
961
|
|
|
859
962
|
if (collabNode !== undefined) {
|
|
860
963
|
collabNode.destroy(binding);
|
|
861
964
|
}
|
|
862
965
|
|
|
863
|
-
lexicalChildNode
|
|
966
|
+
removeFromParent(lexicalChildNode);
|
|
864
967
|
}
|
|
865
968
|
}
|
|
866
969
|
}
|
|
@@ -886,8 +989,8 @@ class CollabElementNode {
|
|
|
886
989
|
|
|
887
990
|
syncChildrenFromLexical(binding, nextLexicalNode, prevNodeMap, dirtyElements, dirtyLeaves) {
|
|
888
991
|
const prevLexicalNode = this.getPrevNode(prevNodeMap);
|
|
889
|
-
const prevChildren = prevLexicalNode === null ? [] : prevLexicalNode
|
|
890
|
-
const nextChildren = nextLexicalNode
|
|
992
|
+
const prevChildren = prevLexicalNode === null ? [] : createChildrenArray(prevLexicalNode, prevNodeMap);
|
|
993
|
+
const nextChildren = createChildrenArray(nextLexicalNode, null);
|
|
891
994
|
const prevEndIndex = prevChildren.length - 1;
|
|
892
995
|
const nextEndIndex = nextChildren.length - 1;
|
|
893
996
|
const collabNodeMap = binding.collabNodeMap;
|
|
@@ -1070,17 +1173,6 @@ class CollabElementNode {
|
|
|
1070
1173
|
}
|
|
1071
1174
|
|
|
1072
1175
|
}
|
|
1073
|
-
|
|
1074
|
-
function lazilyCloneElementNode(lexicalNode, writableLexicalNode, nextLexicalChildrenKeys) {
|
|
1075
|
-
if (writableLexicalNode === undefined) {
|
|
1076
|
-
const clone = lexicalNode.getWritable();
|
|
1077
|
-
clone.__children = nextLexicalChildrenKeys;
|
|
1078
|
-
return clone;
|
|
1079
|
-
}
|
|
1080
|
-
|
|
1081
|
-
return writableLexicalNode;
|
|
1082
|
-
}
|
|
1083
|
-
|
|
1084
1176
|
function $createCollabElementNode(xmlText, parent, type) {
|
|
1085
1177
|
const collabNode = new CollabElementNode(xmlText, parent, type); // @ts-expect-error: internal field
|
|
1086
1178
|
|
package/LexicalYjs.js.flow
CHANGED
|
@@ -18,7 +18,6 @@ import type {
|
|
|
18
18
|
NodeMap,
|
|
19
19
|
NodeKey,
|
|
20
20
|
TextNode,
|
|
21
|
-
IntentionallyMarkedAsDirtyElement,
|
|
22
21
|
} from 'lexical';
|
|
23
22
|
|
|
24
23
|
// $FlowFixMe: todo
|
|
@@ -220,6 +219,8 @@ declare export class CollabTextNode {
|
|
|
220
219
|
destroy(binding: Binding): void;
|
|
221
220
|
}
|
|
222
221
|
|
|
222
|
+
type IntentionallyMarkedAsDirtyElement = boolean;
|
|
223
|
+
|
|
223
224
|
declare export class CollabElementNode {
|
|
224
225
|
_key: NodeKey;
|
|
225
226
|
_children: Array<
|
package/LexicalYjs.prod.js
CHANGED
|
@@ -4,42 +4,46 @@
|
|
|
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
|
-
'use strict';var
|
|
8
|
-
class C{constructor(a,b){this._key="";this._map=a;this._parent=b;this._type="linebreak"}getNode(){let a=
|
|
9
|
-
class E{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
|
|
10
|
-
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=
|
|
11
|
-
d,a);this._text=c}}syncPropertiesAndTextFromYjs(a,b){let c=this.getNode();null===c&&
|
|
12
|
-
function L(a,b,c){let d=b.__type;if(
|
|
13
|
-
function O(a,b,c){let d=b._collabNode;if(void 0===d){var e=a.editor._nodes;let
|
|
14
|
-
function G(a,b,c,d){d=null===d?b instanceof v.Map?Array.from(b.keys()):Object.keys(b.getAttributes()):Array.from(d);let e;for(let h=0;h<d.length;h++){let
|
|
15
|
-
function F(a,b,c,d){var e=d.__type,
|
|
16
|
-
function Q(a,b,c){let d=0,e=0,
|
|
17
|
-
function R(a){let b=a.anchor;a=a.focus;let c=!1;try{let d=b.getNode(),e=a.getNode();if(!d.isAttached()||!e.isAttached()||
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
function
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
exports.
|
|
44
|
-
|
|
45
|
-
b)
|
|
7
|
+
'use strict';var u=require("lexical"),v=require("yjs"),y=require("@lexical/selection"),z=require("@lexical/offset");function A(a){throw Error(`Minified Lexical error #${a}; visit https://lexical.dev/docs/error?code=${a} for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}
|
|
8
|
+
class C{constructor(a,b){this._key="";this._map=a;this._parent=b;this._type="linebreak"}getNode(){let a=u.$getNodeByKey(this._key);return u.$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 D(a,b){b=new C(a,b);return a._collabNode=b}
|
|
9
|
+
class E{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 u.$isTextNode(a)?a:null}getNode(){let a=u.$getNodeByKey(this._key);return u.$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){let d=this._parent._xmlText;
|
|
10
|
+
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=u.$getSelection();a=c.length;u.$isRangeSelection(e)&&e.isCollapsed()&&(e=e.anchor,e.key===d&&(a=e.offset));d=b.length;let f=c.length,h=e=0;for(;e<d&&e<f&&b[e]===c[e]&&e<a;)e++;for(;h+e<d&&h+e<f&&b[d-h-1]===c[f-h-1];)h++;for(;h+e<d&&h+e<f&&b[e]===c[e];)e++;b=e;a=c.slice(e,f-h);d=d-e-h;this.spliceText(b,
|
|
11
|
+
d,a);this._text=c}}syncPropertiesAndTextFromYjs(a,b){let c=this.getNode();null===c&&A(84);G(a,this._map,c,b);a=this._text;c.__text!==a&&(c.getWritable().__text=a)}destroy(a){a.collabNodeMap.delete(this._key)}}function I(a,b,c,d){b=new E(a,b,c,d);return a._collabNode=b}let J=new Set("__key __parent __cachedText __text __size __next __prev __first __last".split(" "));function K(a){a=u.$getNodeByKey(a);null===a&&A(85);return a}
|
|
12
|
+
function L(a,b,c){let d=b.__type;if(u.$isElementNode(b)){var e=new v.XmlText;e=M(e,c,d);e.syncPropertiesFromLexical(a,b,null);e.syncChildrenFromLexical(a,b,null,null,null)}else u.$isTextNode(b)?(e=new v.Map,e=I(e,b.__text,c,d),e.syncPropertiesAndTextFromLexical(a,b,null)):u.$isLineBreakNode(b)?(a=new v.Map,a.set("__type","linebreak"),e=D(a,c)):u.$isDecoratorNode(b)?(e=new v.XmlElement,e=N(e,c,d),e.syncPropertiesFromLexical(a,b,null)):A(86);e._key=b.__key;return e}
|
|
13
|
+
function O(a,b,c){let d=b._collabNode;if(void 0===d){var e=a.editor._nodes;let f=b instanceof v.Map?b.get("__type"):b.getAttribute("__type");null==f&&A(87);void 0===e.get(f)&&A(88);e=b.parent;a=void 0===c&&null!==e?O(a,e):c||null;a instanceof P||A(89);if(b instanceof v.XmlText)return M(b,a,f);if(b instanceof v.Map)return"linebreak"===f?D(b,a):I(b,"",a,f);if(b instanceof v.XmlElement)return N(b,a,f)}return d}
|
|
14
|
+
function G(a,b,c,d){d=null===d?b instanceof v.Map?Array.from(b.keys()):Object.keys(b.getAttributes()):Array.from(d);let e;for(let h=0;h<d.length;h++){let g=d[h];if(J.has(g))continue;var f=c[g];let k=b instanceof v.Map?b.get(g):b.getAttribute(g);if(f!==k){if(k instanceof v.Doc){let n=a.docMap;f instanceof v.Doc&&n.delete(f.guid);f=u.createEditor();let p=k.guid;f._key=p;n.set(p,k);k=f}void 0===e&&(e=c.getWritable());e[g]=k}}}
|
|
15
|
+
function F(a,b,c,d){var e=d.__type,f=a.nodeProperties;let h=f.get(e);void 0===h&&(h=Object.keys(d).filter(k=>!J.has(k)),f.set(e,h));e=a.editor.constructor;for(f=0;f<h.length;f++){let k=h[f];var g=null===c?void 0:c[k];let n=d[k];if(g!==n){if(n instanceof e){let p=a.docMap,m;g instanceof e&&(g=g._key,m=p.get(g),p.delete(g));g=m||new v.Doc;let l=g.guid;n._key=l;p.set(l,g);n=g;a.editor.update(()=>{d.markDirty()})}b instanceof v.Map?b.set(k,n):b.setAttribute(k,n)}}}
|
|
16
|
+
function Q(a,b,c){let d=0,e=0,f=a._children,h=f.length;for(;e<h;e++){a=f[e];let g=d,k=a.getSize();d+=k;if((c?d>=b:d>b)&&a instanceof E)return c=b-g-1,0>c&&(c=0),{length:d-b,node:a,nodeIndex:e,offset:c};if(d>b)return{length:0,node:a,nodeIndex:e,offset:g};if(e===h-1)return{length:0,node:null,nodeIndex:e+1,offset:g+1}}return{length:0,node:null,nodeIndex:0,offset:0}}
|
|
17
|
+
function R(a){let b=a.anchor;a=a.focus;let c=!1;try{let d=b.getNode(),e=a.getNode();if(!d.isAttached()||!e.isAttached()||u.$isTextNode(d)&&b.offset>d.getTextContentSize()||u.$isTextNode(e)&&a.offset>e.getTextContentSize())c=!0}catch(d){c=!0}return c}function aa(a,b){a.doc.transact(b,a)}
|
|
18
|
+
function S(a,b){let c=[];for(a=a.__first;null!==a;){let d=null===b?u.$getNodeByKey(a):b.get(a);if(null===d||void 0===d)throw Error("createChildrenArray: node does not exist in nodeMap");c.push(a);a=d.__next}return c}
|
|
19
|
+
function T(a){var b=a.getParent();if(null!==b){let e=a.getWritable();b=b.getWritable();var c=a.getPreviousSibling();a=a.getNextSibling();if(null===c)if(null!==a){var d=a.getWritable();b.__first=a.__key;d.__prev=null}else b.__first=null;else{d=c.getWritable();if(null!==a){let f=a.getWritable();f.__prev=d.__key;d.__next=f.__key}else d.__next=null;e.__prev=null}null===a?null!==c?(a=c.getWritable(),b.__last=c.__key,a.__next=null):b.__last=null:(a=a.getWritable(),null!==c?(c=c.getWritable(),c.__next=a.__key,
|
|
20
|
+
a.__prev=c.__key):a.__prev=null,e.__next=null);b.__size--;e.__parent=null}}
|
|
21
|
+
class U{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 u.$isDecoratorNode(a)?a:null}getNode(){let a=u.$getNodeByKey(this._key);return u.$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);
|
|
22
|
+
F(a,this._xmlElem,c,b)}syncPropertiesFromYjs(a,b){let c=this.getNode();null===c&&A(83);G(a,this._xmlElem,c,b)}destroy(a){a.collabNodeMap.delete(this._key);this._unobservers.forEach(b=>b());this._unobservers.clear()}}function N(a,b,c){b=new U(a,b,c);return a._collabNode=b}
|
|
23
|
+
class P{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 u.$isElementNode(a)?a:null}getNode(){let a=u.$getNodeByKey(this._key);return u.$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(){let a=this._parent;null===a&&A(90);return a.getChildOffset(this)}syncPropertiesFromYjs(a,
|
|
24
|
+
b){let c=this.getNode();null===c&&A(91);G(a,this._xmlText,c,b)}applyChildrenYjsDelta(a,b){let c=this._children,d=0;for(let p=0;p<b.length;p++){var e=b[p],f=e.insert,h=e.delete;if(null!=e.retain)d+=e.retain;else if("number"===typeof h)for(f=h;0<f;){let {node:m,nodeIndex:l,offset:r,length:t}=Q(this,d,!1);if(m instanceof P||m instanceof C||m instanceof U)c.splice(l,1),--f;else if(m instanceof E){e=Math.min(f,t);h=0!==l?c[l-1]:null;var g=m.getSize();if(0===r&&1===e&&0<l&&h instanceof E&&t===g&&0===Array.from(m._map.keys()).length)h._text+=
|
|
25
|
+
m._text,c.splice(l,1);else if(0===r&&e===g)c.splice(l,1);else{h=m;g=m._text;var k=r,n=e;g=g.slice(0,k)+""+g.slice(k+n);h._text=g}f-=e}else break}else if(null!=f)if("string"===typeof f){let {node:m,offset:l}=Q(this,d,!0);m instanceof E?(e=m,h=m._text,g=l,k=f,h=h.slice(0,g)+k+h.slice(g+0),e._text=h):this._xmlText.delete(l,f.length);d+=f.length}else e=f,{nodeIndex:f}=Q(this,d,!1),e=O(a,e,this),c.splice(f,0,e),d+=1;else throw Error("Unexpected delta format");}}syncChildrenFromYjs(a){var b=this.getNode();
|
|
26
|
+
null===b&&A(92);var c=b.__key;let d=S(b,null),e=d.length;var f=this._children;let h=f.length,g=a.collabNodeMap,k=new Set,n;let p=0;var m=null;h!==e&&b.getWritable();for(let B=0;B<h;B++){var l=d[p],r=f[B];var t=r.getNode();var q=r._key;if(null!==t&&l===q)m=u.$isTextNode(t),k.add(l),m&&(r._key=l,r instanceof P?(m=r._xmlText,r.syncPropertiesFromYjs(a,null),r.applyChildrenYjsDelta(a,m.toDelta()),r.syncChildrenFromYjs(a)):r instanceof E?r.syncPropertiesAndTextFromYjs(a,null):r instanceof U?r.syncPropertiesFromYjs(a,
|
|
27
|
+
null):r instanceof C||A(93)),m=t,p++;else{if(void 0===n)for(n=new Set,q=0;q<h;q++){var x=f[q]._key;""!==x&&n.add(x)}if(null===t||void 0===l||n.has(l)){t=b.getWritable();l=a;q=r;x=c;var w=q.getType();w=l.editor._nodes.get(w);void 0===w&&A(88);w=new w.klass;w.__parent=x;q._key=w.__key;q instanceof P?(x=q._xmlText,q.syncPropertiesFromYjs(l,null),q.applyChildrenYjsDelta(l,x.toDelta()),q.syncChildrenFromYjs(l)):q instanceof E?q.syncPropertiesAndTextFromYjs(l,null):q instanceof U&&q.syncPropertiesFromYjs(l,
|
|
28
|
+
null);l.collabNodeMap.set(w.__key,q);l=w;q=l.__key;g.set(q,r);null===m?(m=t.getFirstChild(),t.__first=q,null!==m&&(m=m.getWritable(),m.__prev=q,l.__next=m.__key)):(r=m.getWritable(),x=m.getNextSibling(),r.__next=q,l.__prev=m.__key,null!==x&&(m=x.getWritable(),m.__prev=q,l.__next=m.__key));B===h-1&&(t.__last=q);t.__size++;m=l}else r=K(l),T(r),B--,p++}}for(b=0;b<e;b++)f=d[b],k.has(f)||(c=K(f),f=a.collabNodeMap.get(f),void 0!==f&&f.destroy(a),T(c))}syncPropertiesFromLexical(a,b,c){F(a,this._xmlText,
|
|
29
|
+
this.getPrevNode(c),b)}_syncChildFromLexical(a,b,c,d,e,f){b=this._children[b];c=K(c);b instanceof P&&u.$isElementNode(c)?(b.syncPropertiesFromLexical(a,c,d),b.syncChildrenFromLexical(a,c,d,e,f)):b instanceof E&&u.$isTextNode(c)?b.syncPropertiesAndTextFromLexical(a,c,d):b instanceof U&&u.$isDecoratorNode(c)&&b.syncPropertiesFromLexical(a,c,d)}syncChildrenFromLexical(a,b,c,d,e){var f=this.getPrevNode(c);let h=null===f?[]:S(f,c);f=S(b,null);let g=h.length-1,k=f.length-1,n=a.collabNodeMap,p,m,l=0;for(b=
|
|
30
|
+
0;l<=g&&b<=k;){var r=h[l];let q=f[b];if(r===q)this._syncChildFromLexical(a,b,q,c,d,e),l++,b++;else{void 0===p&&(p=new Set(h));void 0===m&&(m=new Set(f));var t=m.has(r);r=p.has(q);t?(t=K(q),t=L(a,t,this),n.set(q,t),r?(this.splice(a,b,1,t),l++):this.splice(a,b,0,t),b++):(this.splice(a,b,1),l++)}}c=l>g;d=b>k;if(c&&!d)for(;b<=k;++b)c=f[b],d=K(c),d=L(a,d,this),this.append(d),n.set(c,d);else if(d&&!c)for(f=this._children.length-1;f>=b;f--)this.splice(a,f,1)}append(a){let b=this._xmlText;var c=this._children;
|
|
31
|
+
c=c[c.length-1];c=void 0!==c?c.getOffset()+c.getSize():0;if(a instanceof P)b.insertEmbed(c,a._xmlText);else if(a instanceof E){let d=a._map;null===d.parent&&b.insertEmbed(c,d);b.insert(c+1,a._text)}else a instanceof C?b.insertEmbed(c,a._map):a instanceof U&&b.insertEmbed(c,a._xmlElem);this._children.push(a)}splice(a,b,c,d){let e=this._children;var f=e[b];if(void 0===f)void 0===d&&A(94),this.append(d);else{var h=f.getOffset();-1===h&&A(95);var g=this._xmlText;0!==c&&g.delete(h,f.getSize());d instanceof
|
|
32
|
+
P?g.insertEmbed(h,d._xmlText):d instanceof E?(f=d._map,null===f.parent&&g.insertEmbed(h,f),g.insert(h+1,d._text)):d instanceof C?g.insertEmbed(h,d._map):d instanceof U&&g.insertEmbed(h,d._xmlElem);if(0!==c)for(h=e.slice(b,b+c),g=0;g<h.length;g++)h[g].destroy(a);void 0!==d?e.splice(b,c,d):e.splice(b,c)}}getChildOffset(a){let b=0,c=this._children;for(let d=0;d<c.length;d++){let e=c[d];if(e===a)return b;b+=e.getSize()}return-1}destroy(a){let b=a.collabNodeMap,c=this._children;for(let d=0;d<c.length;d++)c[d].destroy(a);
|
|
33
|
+
b.delete(this._key)}}function M(a,b,c){b=new P(a,b,c);return a._collabNode=b}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 E){c=b._parent._xmlText;b=b.getOffset();if(-1===b)return null;a=b+1+a}return v.createRelativePositionFromTypeIndex(c,a)}function W(a,b){if(null==a){if(null!=b)return!0}else if(null==b||!v.compareRelativePositions(a,b))return!0;return!1}
|
|
34
|
+
function X(a,b){a=a.cursorsContainer;if(null!==a){b=b.selections;let c=b.length;for(let d=0;d<c;d++)a.removeChild(b[d])}}
|
|
35
|
+
function Y(a,b){var c=b.awareness.getLocalState();if(null!==c&&(b=c.anchorPos,c=c.focusPos,null!==b&&null!==c&&(b=v.createAbsolutePositionFromRelativePosition(b,a.doc),a=v.createAbsolutePositionFromRelativePosition(c,a.doc),null!==b&&null!==a))){let [e,f]=Z(b.type,b.index),[h,g]=Z(a.type,a.index);if(null!==e&&null!==h){let k=e.getKey();a=h.getKey();b=u.$getSelection();if(u.$isRangeSelection(b)){var d=b.anchor;c=b.focus;if(d.key!==k||d.offset!==f)d=u.$getNodeByKey(k),b.anchor.set(k,f,u.$isElementNode(d)?
|
|
36
|
+
"element":"text");if(c.key!==a||c.offset!==g)c=u.$getNodeByKey(a),b.focus.set(a,g,u.$isElementNode(c)?"element":"text")}}}}function Z(a,b){a=a._collabNode;if(void 0===a)return[null,0];if(a instanceof P){let {node:c,offset:d}=Q(a,b,!0);return null===c?[a,0]:[c,d]}return[null,0]}
|
|
37
|
+
function ha(a,b){var c=Array.from(b.awareness.getStates()),d=a.clientID;b=a.cursors;var e=a.editor._editorState._nodeMap;let f=new Set;for(var h=0;h<c.length;h++){let [B,ja]=c[h];if(B!==d){f.add(B);let {anchorPos:ba,focusPos:ca,name:ka,color:la,focusing:ma}=ja;var g=null,k=b.get(B);void 0===k&&(k={color:la,name:ka,selection:null},b.set(B,k));if(null!==ba&&null!==ca&&ma){var n=v.createAbsolutePositionFromRelativePosition(ba,a.doc),p=v.createAbsolutePositionFromRelativePosition(ca,a.doc);if(null!==
|
|
38
|
+
n&&null!==p){let [H,da]=Z(n.type,n.index),[ea,fa]=Z(p.type,p.index);if(null!==H&&null!==ea){n=H.getKey();var m=ea.getKey();g=k.selection;if(null===g){g=k;p=da;var l=fa,r=g.color,t=document.createElement("span");t.style.cssText=`position:absolute;top:0;bottom:0;right:-1px;width:1px;background-color:${r};z-index:10;`;var q=document.createElement("span");q.textContent=g.name;q.style.cssText=`position:absolute;left:-2px;top:-16px;background-color:${r};color:#fff;line-height:12px;font-size:12px;padding:2px;font-family:Arial;font-weight:bold;white-space:nowrap;`;
|
|
39
|
+
t.appendChild(q);g={anchor:{key:n,offset:p},caret:t,color:r,focus:{key:m,offset:l},name:q,selections:[]}}else p=g.anchor,l=g.focus,p.key=n,p.offset=da,l.key=m,l.offset=fa}}}n=a;p=k;t=g;r=e;l=n.editor;g=l.getRootElement();k=n.cursorsContainer;if(null!==k&&null!==g&&(g=k.offsetParent,null!==g))if(g=g.getBoundingClientRect(),m=p.selection,null===t)null!==m&&(p.selection=null,X(n,m));else{p.selection=t;p=t.caret;m=t.color;n=t.selections;q=t.anchor;t=t.focus;var x=t.key,w=r.get(q.key);r=r.get(x);if(null!=
|
|
40
|
+
w&&null!=r&&(t=y.createDOMRange(l,w,q.offset,r,t.offset),null!==t)){r=n.length;t=y.createRectsFromDOMRange(l,t);l=t.length;for(q=0;q<l;q++){x=t[q];w=n[q];if(void 0===w){w=document.createElement("span");n[q]=w;let H=document.createElement("span");w.appendChild(H);k.appendChild(w)}x=`position:absolute;top:${x.top-g.top}px;left:${x.left-g.left}px;height:${x.height}px;width:${x.width}px;pointer-events:none;z-index:5;`;w.style.cssText=x;w.firstChild.style.cssText=`${x}left:0;top:0;background-color:${m};opacity:0.3;`;
|
|
41
|
+
q===l-1&&p.parentNode!==w&&w.appendChild(p)}for(g=r-1;g>=l;g--)k.removeChild(n[g]),n.pop()}}}}c=Array.from(b.keys());for(d=0;d<c.length;d++)e=c[d],f.has(e)||(h=b.get(e),void 0!==h&&(h=h.selection,null!==h&&X(a,h),b.delete(e)))}
|
|
42
|
+
function ia(a,b,c,d){b=b.awareness;var e=b.getLocalState();if(null!==e){var {anchorPos:f,focusPos:h,name:g,color:k,focusing:n}=e,p=e=null;if(null!==d&&(null===f||d.is(c))||null!==c)u.$isRangeSelection(d)&&(e=V(d.anchor,a),p=V(d.focus,a)),(W(f,e)||W(h,p))&&b.setLocalState({anchorPos:e,color:k,focusPos:p,focusing:n,name:g})}}let na=u.createCommand("CONNECTED_COMMAND"),oa=u.createCommand("TOGGLE_CONNECT_COMMAND");exports.CONNECTED_COMMAND=na;exports.TOGGLE_CONNECT_COMMAND=oa;
|
|
43
|
+
exports.createBinding=function(a,b,c,d,e){void 0!==d&&null!==d||A(81);b=d.get("root",v.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 v.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})};
|
|
44
|
+
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=ha;
|
|
45
|
+
exports.syncLexicalUpdateToYjs=function(a,b,c,d,e,f,h,g){aa(a,()=>{d.read(()=>{if(g.has("collaboration")){if(0<h.size){var k=Array.from(h),n=a.collabNodeMap,p=[];for(let t=0;t<k.length;t++){var m=k[t],l=u.$getNodeByKey(m),r=n.get(m);if(r instanceof E)if(u.$isTextNode(l))p.push([r,l.__text]);else{l=r.getOffset();if(-1===l)continue;let q=r._parent;r._normalized=!0;q._xmlText.delete(l,1);n.delete(m);m=q._children;r=m.indexOf(r);m.splice(r,1)}}for(k=0;k<p.length;k++){let [t,q]=p[k];t instanceof E&&"string"===
|
|
46
|
+
typeof q&&(t._text=q)}}}else e.has("root")&&(p=c._nodeMap,k=u.$getRoot(),n=a.root,n.syncPropertiesFromLexical(a,k,p),n.syncChildrenFromLexical(a,k,p,e,f)),p=u.$getSelection(),ia(a,b,c._selection,p)})})};
|
|
47
|
+
exports.syncYjsChangesToLexical=function(a,b,c){let d=a.editor,e=d._editorState;d.update(()=>{var f=d._pendingEditorState;for(var h=0;h<c.length;h++){var g=a,k=c[h],{target:n}=k;n=O(g,n);if(n instanceof P&&k instanceof v.YTextEvent){let {keysChanged:p,childListChanged:m,delta:l}=k;0<p.size&&n.syncPropertiesFromYjs(g,p);m&&(n.applyChildrenYjsDelta(g,l),n.syncChildrenFromYjs(g))}else n instanceof E&&k instanceof v.YMapEvent?({keysChanged:k}=k,0<k.size&&n.syncPropertiesAndTextFromYjs(g,k)):n instanceof
|
|
48
|
+
U&&k instanceof v.YXmlEvent?({attributesChanged:k}=k,0<k.size&&n.syncPropertiesFromYjs(g,k)):A(82)}h=u.$getSelection();if(u.$isRangeSelection(h))if(R(h)){g=e._selection;if(u.$isRangeSelection(g)){n=z.$createOffsetView(d,0,e);f=z.$createOffsetView(d,0,f);let [p,m]=n.getOffsetsFromSelection(g);f=f.createSelectionFromOffsets(p,m,n);null!==f?u.$setSelection(f):(Y(a,b),R(h)&&(f=u.$getRoot(),0===f.getChildrenSize()&&f.append(u.$createParagraphNode()),u.$getRoot().selectEnd()))}ia(a,b,g,u.$getSelection())}else Y(a,
|
|
49
|
+
b)},{onUpdate:()=>{ha(a,b)},skipTransforms:!0,tag:"collaboration"})}
|
package/SyncEditorStates.d.ts
CHANGED
|
@@ -5,9 +5,11 @@
|
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
|
-
import type { EditorState,
|
|
8
|
+
import type { EditorState, NodeKey } from 'lexical';
|
|
9
9
|
import { WebsocketProvider } from 'y-websocket';
|
|
10
10
|
import { Text as YText, YEvent } from 'yjs';
|
|
11
11
|
import { Binding } from '.';
|
|
12
12
|
export declare function syncYjsChangesToLexical(binding: Binding, provider: WebsocketProvider, events: Array<YEvent<YText>>): void;
|
|
13
|
+
declare type IntentionallyMarkedAsDirtyElement = boolean;
|
|
13
14
|
export declare function syncLexicalUpdateToYjs(binding: Binding, provider: WebsocketProvider, prevEditorState: EditorState, currEditorState: EditorState, dirtyElements: Map<NodeKey, IntentionallyMarkedAsDirtyElement>, dirtyLeaves: Set<NodeKey>, normalizedNodes: Set<NodeKey>, tags: Set<string>): void;
|
|
15
|
+
export {};
|
package/Utils.d.ts
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
import type { Binding, YjsNode } from '.';
|
|
9
|
-
import type { LexicalNode, RangeSelection } from 'lexical';
|
|
9
|
+
import type { ElementNode, LexicalNode, NodeMap, RangeSelection } from 'lexical';
|
|
10
10
|
import { NodeKey } from 'lexical';
|
|
11
11
|
import { Map as YMap, XmlElement, XmlText } from 'yjs';
|
|
12
12
|
import { CollabDecoratorNode } from './CollabDecoratorNode';
|
|
@@ -29,3 +29,5 @@ export declare function getPositionFromElementAndOffset(node: CollabElementNode,
|
|
|
29
29
|
};
|
|
30
30
|
export declare function doesSelectionNeedRecovering(selection: RangeSelection): boolean;
|
|
31
31
|
export declare function syncWithTransaction(binding: Binding, fn: () => void): void;
|
|
32
|
+
export declare function createChildrenArray(element: ElementNode, nodeMap: null | NodeMap): Array<NodeKey>;
|
|
33
|
+
export declare function removeFromParent(node: LexicalNode): void;
|
package/package.json
CHANGED
|
@@ -11,13 +11,13 @@
|
|
|
11
11
|
"crdt"
|
|
12
12
|
],
|
|
13
13
|
"license": "MIT",
|
|
14
|
-
"version": "0.
|
|
14
|
+
"version": "0.7.0",
|
|
15
15
|
"main": "LexicalYjs.js",
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@lexical/offset": "0.
|
|
17
|
+
"@lexical/offset": "0.7.0"
|
|
18
18
|
},
|
|
19
19
|
"peerDependencies": {
|
|
20
|
-
"lexical": "0.
|
|
20
|
+
"lexical": "0.7.0",
|
|
21
21
|
"yjs": ">=13.5.22"
|
|
22
22
|
},
|
|
23
23
|
"repository": {
|