@lexical/yjs 0.4.1 → 0.5.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 +46 -38
- package/LexicalYjs.prod.js +38 -40
- package/package.json +3 -3
package/LexicalYjs.dev.js
CHANGED
|
@@ -204,8 +204,8 @@ class CollabTextNode {
|
|
|
204
204
|
syncPropertiesAndTextFromYjs(binding, keysChanged) {
|
|
205
205
|
const lexicalNode = this.getNode();
|
|
206
206
|
|
|
207
|
-
if (lexicalNode
|
|
208
|
-
throw
|
|
207
|
+
if (!(lexicalNode !== null)) {
|
|
208
|
+
throw Error(`syncPropertiesAndTextFromYjs: cound not find decorator node`);
|
|
209
209
|
}
|
|
210
210
|
|
|
211
211
|
syncPropertiesFromYjs(binding, this._map, lexicalNode, keysChanged);
|
|
@@ -241,8 +241,8 @@ const excludedProperties = new Set(['__key', '__children', '__parent', '__cached
|
|
|
241
241
|
function $getNodeByKeyOrThrow(key) {
|
|
242
242
|
const node = lexical.$getNodeByKey(key);
|
|
243
243
|
|
|
244
|
-
if (node
|
|
245
|
-
throw
|
|
244
|
+
if (!(node !== null)) {
|
|
245
|
+
throw Error(`could not find node by key`);
|
|
246
246
|
}
|
|
247
247
|
|
|
248
248
|
return node;
|
|
@@ -257,7 +257,7 @@ function $createCollabNodeFromLexicalNode(binding, lexicalNode, parent) {
|
|
|
257
257
|
collabNode.syncPropertiesFromLexical(binding, lexicalNode, null);
|
|
258
258
|
collabNode.syncChildrenFromLexical(binding, lexicalNode, null, null, null);
|
|
259
259
|
} else if (lexical.$isTextNode(lexicalNode)) {
|
|
260
|
-
// TODO create a token text node for token, segmented
|
|
260
|
+
// TODO create a token text node for token, segmented nodes.
|
|
261
261
|
const map = new yjs.Map();
|
|
262
262
|
collabNode = $createCollabTextNode(map, lexicalNode.__text, parent, nodeType);
|
|
263
263
|
collabNode.syncPropertiesAndTextFromLexical(binding, lexicalNode, null);
|
|
@@ -270,7 +270,9 @@ function $createCollabNodeFromLexicalNode(binding, lexicalNode, parent) {
|
|
|
270
270
|
collabNode = $createCollabDecoratorNode(xmlElem, parent, nodeType);
|
|
271
271
|
collabNode.syncPropertiesFromLexical(binding, lexicalNode, null);
|
|
272
272
|
} else {
|
|
273
|
-
|
|
273
|
+
{
|
|
274
|
+
throw Error(`Expected text, element, decorator, or linebreak node`);
|
|
275
|
+
}
|
|
274
276
|
}
|
|
275
277
|
|
|
276
278
|
collabNode._key = lexicalNode.__key;
|
|
@@ -280,8 +282,8 @@ function $createCollabNodeFromLexicalNode(binding, lexicalNode, parent) {
|
|
|
280
282
|
function getNodeTypeFromSharedType(sharedType) {
|
|
281
283
|
const type = sharedType instanceof yjs.Map ? sharedType.get('__type') : sharedType.getAttribute('__type');
|
|
282
284
|
|
|
283
|
-
if (type
|
|
284
|
-
throw
|
|
285
|
+
if (!(type != null)) {
|
|
286
|
+
throw Error(`Expected shared type to include type attribute`);
|
|
285
287
|
}
|
|
286
288
|
|
|
287
289
|
return type;
|
|
@@ -296,24 +298,20 @@ function getOrInitCollabNodeFromSharedType(binding, sharedType, parent) {
|
|
|
296
298
|
const type = getNodeTypeFromSharedType(sharedType);
|
|
297
299
|
const nodeInfo = registeredNodes.get(type);
|
|
298
300
|
|
|
299
|
-
if (nodeInfo
|
|
300
|
-
throw
|
|
301
|
+
if (!(nodeInfo !== undefined)) {
|
|
302
|
+
throw Error(`Node ${type} is not registered`);
|
|
301
303
|
}
|
|
302
304
|
|
|
303
305
|
const sharedParent = sharedType.parent;
|
|
304
306
|
const targetParent = parent === undefined && sharedParent !== null ? getOrInitCollabNodeFromSharedType(binding, sharedParent) : parent || null;
|
|
305
307
|
|
|
306
308
|
if (!(targetParent instanceof CollabElementNode)) {
|
|
307
|
-
throw
|
|
309
|
+
throw Error(`Expected parent to be a collab element node`);
|
|
308
310
|
}
|
|
309
311
|
|
|
310
312
|
if (sharedType instanceof yjs.XmlText) {
|
|
311
313
|
return $createCollabElementNode(sharedType, targetParent, type);
|
|
312
314
|
} else if (sharedType instanceof yjs.Map) {
|
|
313
|
-
if (targetParent === null) {
|
|
314
|
-
throw new Error('Should never happen');
|
|
315
|
-
}
|
|
316
|
-
|
|
317
315
|
if (type === 'linebreak') {
|
|
318
316
|
return $createCollabLineBreakNode(sharedType, targetParent);
|
|
319
317
|
}
|
|
@@ -331,8 +329,8 @@ function createLexicalNodeFromCollabNode(binding, collabNode, parentKey) {
|
|
|
331
329
|
const registeredNodes = binding.editor._nodes;
|
|
332
330
|
const nodeInfo = registeredNodes.get(type);
|
|
333
331
|
|
|
334
|
-
if (nodeInfo
|
|
335
|
-
throw
|
|
332
|
+
if (!(nodeInfo !== undefined)) {
|
|
333
|
+
throw Error(`Node ${type} is not registered`);
|
|
336
334
|
}
|
|
337
335
|
|
|
338
336
|
const lexicalNode = new nodeInfo.klass();
|
|
@@ -584,8 +582,8 @@ class CollabDecoratorNode {
|
|
|
584
582
|
syncPropertiesFromYjs(binding, keysChanged) {
|
|
585
583
|
const lexicalNode = this.getNode();
|
|
586
584
|
|
|
587
|
-
if (lexicalNode
|
|
588
|
-
throw
|
|
585
|
+
if (!(lexicalNode !== null)) {
|
|
586
|
+
throw Error(`syncPropertiesFromYjs: cound not find decorator node`);
|
|
589
587
|
}
|
|
590
588
|
|
|
591
589
|
const xmlElem = this._xmlElem;
|
|
@@ -662,8 +660,8 @@ class CollabElementNode {
|
|
|
662
660
|
getOffset() {
|
|
663
661
|
const collabElementNode = this._parent;
|
|
664
662
|
|
|
665
|
-
if (collabElementNode
|
|
666
|
-
throw
|
|
663
|
+
if (!(collabElementNode !== null)) {
|
|
664
|
+
throw Error(`getOffset: cound not find collab element node`);
|
|
667
665
|
}
|
|
668
666
|
|
|
669
667
|
return collabElementNode.getChildOffset(this);
|
|
@@ -672,9 +670,8 @@ class CollabElementNode {
|
|
|
672
670
|
syncPropertiesFromYjs(binding, keysChanged) {
|
|
673
671
|
const lexicalNode = this.getNode();
|
|
674
672
|
|
|
675
|
-
if (lexicalNode
|
|
676
|
-
|
|
677
|
-
throw new Error('Should never happen');
|
|
673
|
+
if (!(lexicalNode !== null)) {
|
|
674
|
+
throw Error(`syncPropertiesFromYjs: cound not find element node`);
|
|
678
675
|
}
|
|
679
676
|
|
|
680
677
|
syncPropertiesFromYjs(binding, this._xmlText, lexicalNode, keysChanged);
|
|
@@ -768,9 +765,8 @@ class CollabElementNode {
|
|
|
768
765
|
// Now diff the children of the collab node with that of our existing Lexical node.
|
|
769
766
|
const lexicalNode = this.getNode();
|
|
770
767
|
|
|
771
|
-
if (lexicalNode
|
|
772
|
-
|
|
773
|
-
throw new Error('Should never happen');
|
|
768
|
+
if (!(lexicalNode !== null)) {
|
|
769
|
+
throw Error(`syncChildrenFromYjs: cound not find element node`);
|
|
774
770
|
}
|
|
775
771
|
|
|
776
772
|
const key = lexicalNode.__key;
|
|
@@ -815,7 +811,9 @@ class CollabElementNode {
|
|
|
815
811
|
} else if (childCollabNode instanceof CollabDecoratorNode) {
|
|
816
812
|
childCollabNode.syncPropertiesFromYjs(binding, null);
|
|
817
813
|
} else if (!(childCollabNode instanceof CollabLineBreakNode)) {
|
|
818
|
-
|
|
814
|
+
{
|
|
815
|
+
throw Error(`syncChildrenFromYjs: expected text, element, decorator, or linebreak collab node`);
|
|
816
|
+
}
|
|
819
817
|
}
|
|
820
818
|
}
|
|
821
819
|
|
|
@@ -990,19 +988,18 @@ class CollabElementNode {
|
|
|
990
988
|
const child = children[index];
|
|
991
989
|
|
|
992
990
|
if (child === undefined) {
|
|
993
|
-
if (collabNode !== undefined) {
|
|
994
|
-
|
|
995
|
-
} else {
|
|
996
|
-
throw new Error('Should never happen');
|
|
991
|
+
if (!(collabNode !== undefined)) {
|
|
992
|
+
throw Error(`splice: could not find collab element node`);
|
|
997
993
|
}
|
|
998
994
|
|
|
995
|
+
this.append(collabNode);
|
|
999
996
|
return;
|
|
1000
997
|
}
|
|
1001
998
|
|
|
1002
999
|
const offset = child.getOffset();
|
|
1003
1000
|
|
|
1004
|
-
if (offset
|
|
1005
|
-
throw
|
|
1001
|
+
if (!(offset !== -1)) {
|
|
1002
|
+
throw Error(`splice: expected offset to be greater than zero`);
|
|
1006
1003
|
}
|
|
1007
1004
|
|
|
1008
1005
|
const xmlText = this._xmlText;
|
|
@@ -1099,8 +1096,8 @@ function $createCollabElementNode(xmlText, parent, type) {
|
|
|
1099
1096
|
*
|
|
1100
1097
|
*/
|
|
1101
1098
|
function createBinding(editor, provider, id, doc, docMap) {
|
|
1102
|
-
if (doc
|
|
1103
|
-
throw
|
|
1099
|
+
if (!(doc !== undefined && doc !== null)) {
|
|
1100
|
+
throw Error(`createBinding: doc is null or undefined`);
|
|
1104
1101
|
}
|
|
1105
1102
|
|
|
1106
1103
|
const rootXmlText = doc.get('root', yjs.XmlText);
|
|
@@ -1231,6 +1228,13 @@ function updateCursor(binding, cursor, nextSelection, nodeMap) {
|
|
|
1231
1228
|
return;
|
|
1232
1229
|
}
|
|
1233
1230
|
|
|
1231
|
+
const cursorsContainerOffsetParent = cursorsContainer.offsetParent;
|
|
1232
|
+
|
|
1233
|
+
if (cursorsContainerOffsetParent === null) {
|
|
1234
|
+
return;
|
|
1235
|
+
}
|
|
1236
|
+
|
|
1237
|
+
const containerRect = cursorsContainerOffsetParent.getBoundingClientRect();
|
|
1234
1238
|
const prevSelection = cursor.selection;
|
|
1235
1239
|
|
|
1236
1240
|
if (nextSelection === null) {
|
|
@@ -1279,7 +1283,9 @@ function updateCursor(binding, cursor, nextSelection, nodeMap) {
|
|
|
1279
1283
|
cursorsContainer.appendChild(selection);
|
|
1280
1284
|
}
|
|
1281
1285
|
|
|
1282
|
-
const
|
|
1286
|
+
const top = selectionRect.top - containerRect.top;
|
|
1287
|
+
const left = selectionRect.left - containerRect.left;
|
|
1288
|
+
const style = `position:absolute;top:${top}px;left:${left}px;height:${selectionRect.height}px;width:${selectionRect.width}px;background-color:rgba(${color}, 0.3);pointer-events:none;z-index:5;`;
|
|
1283
1289
|
selection.style.cssText = style;
|
|
1284
1290
|
|
|
1285
1291
|
if (i === selectionRectsLength - 1) {
|
|
@@ -1527,7 +1533,9 @@ function syncEvent(binding, event) {
|
|
|
1527
1533
|
collabNode.syncPropertiesFromYjs(binding, attributesChanged);
|
|
1528
1534
|
}
|
|
1529
1535
|
} else {
|
|
1530
|
-
|
|
1536
|
+
{
|
|
1537
|
+
throw Error(`Expected text, element, or decorator event`);
|
|
1538
|
+
}
|
|
1531
1539
|
}
|
|
1532
1540
|
}
|
|
1533
1541
|
|
package/LexicalYjs.prod.js
CHANGED
|
@@ -4,43 +4,41 @@
|
|
|
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
|
-
|
|
9
|
-
class
|
|
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;
|
|
11
|
-
d,a);this._text=c}}syncPropertiesAndTextFromYjs(a,b){let c=this.getNode();
|
|
12
|
-
function
|
|
13
|
-
return
|
|
14
|
-
function
|
|
15
|
-
|
|
16
|
-
function
|
|
17
|
-
function
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
a
|
|
32
|
-
|
|
33
|
-
function
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
exports.
|
|
41
|
-
|
|
42
|
-
exports.
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
Q&&k instanceof v.YXmlEvent)({attributesChanged:k}=k),0<k.size&&l.syncPropertiesFromYjs(g,k);else throw Error("Should never happen");}h=r.$getSelection();if(r.$isRangeSelection(h))if(P(h)){g=e._selection;if(r.$isRangeSelection(g)){l=A.$createOffsetView(d,0,e);f=A.$createOffsetView(d,0,f);let [n,q]=l.getOffsetsFromSelection(g);f=f.createSelectionFromOffsets(n,q,l);null!==f?r.$setSelection(f):(V(a,b),P(h)&&(f=r.$getRoot(),0===f.getChildrenSize()&&f.append(r.$createParagraphNode()),r.$getRoot().selectEnd()))}fa(a,
|
|
46
|
-
b,g,r.$getSelection())}else V(a,b)},{onUpdate:()=>{X(a,b)},skipTransforms:!0,tag:"collaboration"})}
|
|
7
|
+
'use strict';var q=require("lexical"),v=require("yjs"),y=require("@lexical/selection"),z=require("@lexical/offset");function B(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=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 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 q.$isTextNode(a)?a:null}getNode(){let 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){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=q.$getSelection();a=c.length;q.$isRangeSelection(e)&&e.isCollapsed()&&(e=e.anchor,e.key===d&&(a=e.offset));d=b.length;let g=c.length,h=e=0;for(;e<d&&e<g&&b[e]===c[e]&&e<a;)e++;for(;h+e<d&&h+e<g&&b[d-h-1]===c[g-h-1];)h++;for(;h+e<d&&h+e<g&&b[e]===c[e];)e++;b=e;a=c.slice(e,g-h);d=d-e-h;this.spliceText(b,
|
|
11
|
+
d,a);this._text=c}}syncPropertiesAndTextFromYjs(a,b){let c=this.getNode();null===c&&B(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 H(a,b,c,d){b=new E(a,b,c,d);return a._collabNode=b}let I=new Set(["__key","__children","__parent","__cachedText","__text"]);function J(a){a=q.$getNodeByKey(a);null===a&&B(85);return a}
|
|
12
|
+
function K(a,b,c){let d=b.__type;if(q.$isElementNode(b)){var e=new v.XmlText;e=L(e,c,d);e.syncPropertiesFromLexical(a,b,null);e.syncChildrenFromLexical(a,b,null,null,null)}else q.$isTextNode(b)?(e=new v.Map,e=H(e,b.__text,c,d),e.syncPropertiesAndTextFromLexical(a,b,null)):q.$isLineBreakNode(b)?(a=new v.Map,a.set("__type","linebreak"),e=D(a,c)):q.$isDecoratorNode(b)?(e=new v.XmlElement,e=M(e,c,d),e.syncPropertiesFromLexical(a,b,null)):B(86);e._key=b.__key;return e}
|
|
13
|
+
function N(a,b,c){let d=b._collabNode;if(void 0===d){var e=a.editor._nodes;let g=b instanceof v.Map?b.get("__type"):b.getAttribute("__type");null==g&&B(87);void 0===e.get(g)&&B(88);e=b.parent;a=void 0===c&&null!==e?N(a,e):c||null;a instanceof O||B(89);if(b instanceof v.XmlText)return L(b,a,g);if(b instanceof v.Map)return"linebreak"===g?D(b,a):H(b,"",a,g);if(b instanceof v.XmlElement)return M(b,a,g)}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 f=d[h];if(I.has(f))continue;var g=c[f];let k=b instanceof v.Map?b.get(f):b.getAttribute(f);if(g!==k){if(k instanceof v.Doc){let m=a.docMap;g instanceof v.Doc&&m.delete(g.guid);g=q.createEditor();let n=k.guid;g._key=n;m.set(n,k);k=g}void 0===e&&(e=c.getWritable());e[f]=k}}}
|
|
15
|
+
function F(a,b,c,d){var e=d.__type,g=a.nodeProperties;let h=g.get(e);void 0===h&&(h=Object.keys(d).filter(k=>!I.has(k)),g.set(e,h));e=a.editor.constructor;for(g=0;g<h.length;g++){let k=h[g];var f=null===c?void 0:c[k];let m=d[k];if(f!==m){if(m instanceof e){let n=a.docMap,r;f instanceof e&&(f=f._key,r=n.get(f),n.delete(f));f=r||new v.Doc;let t=f.guid;m._key=t;n.set(t,f);m=f;a.editor.update(()=>{d.markDirty()})}b instanceof v.Map?b.set(k,m):b.setAttribute(k,m)}}}
|
|
16
|
+
function P(a,b,c){let d=0,e=0,g=a._children,h=g.length;for(;e<h;e++){a=g[e];let f=d,k=a.getSize();d+=k;if((c?d>=b:d>b)&&a instanceof E)return c=b-f-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:f};if(e===h-1)return{length:0,node:null,nodeIndex:e+1,offset:f+1}}return{length:0,node:null,nodeIndex:0,offset:0}}
|
|
17
|
+
function Q(a){let b=a.anchor;a=a.focus;let c=!1;try{let 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 fa(a,b){a.doc.transact(b,a)}
|
|
18
|
+
class R{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(){let 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);
|
|
19
|
+
F(a,this._xmlElem,c,b)}syncPropertiesFromYjs(a,b){let c=this.getNode();null===c&&B(83);G(a,this._xmlElem,c,b)}destroy(a){a.collabNodeMap.delete(this._key);this._unobservers.forEach(b=>b());this._unobservers.clear()}}function M(a,b,c){b=new R(a,b,c);return a._collabNode=b}
|
|
20
|
+
class O{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(){let 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(){let a=this._parent;null===a&&B(90);return a.getChildOffset(this)}syncPropertiesFromYjs(a,
|
|
21
|
+
b){let c=this.getNode();null===c&&B(91);G(a,this._xmlText,c,b)}applyChildrenYjsDelta(a,b){let c=this._children,d=0;for(let n=0;n<b.length;n++){var e=b[n],g=e.insert,h=e.delete;if(null!=e.retain)d+=e.retain;else if("number"===typeof h)for(g=h;0<g;){let {node:r,nodeIndex:t,offset:p,length:l}=P(this,d,!1);if(r instanceof O||r instanceof C||r instanceof R)c.splice(t,1),--g;else if(r instanceof E){e=Math.min(g,l);h=0!==t?c[t-1]:null;var f=r.getSize();if(0===p&&1===e&&0<t&&h instanceof E&&l===f&&0===Array.from(r._map.keys()).length)h._text+=
|
|
22
|
+
r._text,c.splice(t,1);else if(0===p&&e===f)c.splice(t,1);else{h=r;f=r._text;var k=p,m=e;f=f.slice(0,k)+""+f.slice(k+m);h._text=f}g-=e}else break}else if(null!=g)if("string"===typeof g){let {node:r,offset:t}=P(this,d,!0);r instanceof E?(e=r,h=r._text,f=t,k=g,h=h.slice(0,f)+k+h.slice(f+0),e._text=h):this._xmlText.delete(t,g.length);d+=g.length}else e=g,{nodeIndex:g}=P(this,d,!1),e=N(a,e,this),c.splice(g,0,e),d+=1;else throw Error("Unexpected delta format");}}syncChildrenFromYjs(a){var b=this.getNode();
|
|
23
|
+
null===b&&B(92);var c=b.__key;let d=b.__children;var e=[];let g=d.length,h=this._children,f=h.length,k=a.collabNodeMap,m=new Set,n,r;f!==g&&(r=S(b,r,e));let t=0;for(let A=0;A<f;A++){var p=d[t];let x=h[A];var l=x.getNode(),u=x._key;if(null!==l&&p===u)l=q.$isTextNode(l),m.add(p),l&&(x._key=p,x instanceof O?(l=x._xmlText,x.syncPropertiesFromYjs(a,null),x.applyChildrenYjsDelta(a,l.toDelta()),x.syncChildrenFromYjs(a)):x instanceof E?x.syncPropertiesAndTextFromYjs(a,null):x instanceof R?x.syncPropertiesFromYjs(a,
|
|
24
|
+
null):x instanceof C||B(93)),e[A]=p,t++;else{if(void 0===n)for(n=new Set,u=0;u<f;u++){var w=h[u]._key;""!==w&&n.add(w)}null===l||void 0===p||n.has(p)?(r=S(b,r,e),p=a,l=x,u=c,w=l.getType(),w=p.editor._nodes.get(w),void 0===w&&B(88),w=new w.klass,w.__parent=u,l._key=w.__key,l instanceof O?(u=l._xmlText,l.syncPropertiesFromYjs(p,null),l.applyChildrenYjsDelta(p,u.toDelta()),l.syncChildrenFromYjs(p)):l instanceof E?l.syncPropertiesAndTextFromYjs(p,null):l instanceof R&&l.syncPropertiesFromYjs(p,null),
|
|
25
|
+
p.collabNodeMap.set(w.__key,l),p=w.__key,k.set(p,x),e[A]=p):(A--,t++)}}for(b=0;b<g;b++)e=d[b],m.has(e)||(c=J(e).getWritable(),e=a.collabNodeMap.get(e),void 0!==e&&e.destroy(a),c.__parent=null)}syncPropertiesFromLexical(a,b,c){F(a,this._xmlText,this.getPrevNode(c),b)}_syncChildFromLexical(a,b,c,d,e,g){b=this._children[b];c=J(c);b instanceof O&&q.$isElementNode(c)?(b.syncPropertiesFromLexical(a,c,d),b.syncChildrenFromLexical(a,c,d,e,g)):b instanceof E&&q.$isTextNode(c)?b.syncPropertiesAndTextFromLexical(a,
|
|
26
|
+
c,d):b instanceof R&&q.$isDecoratorNode(c)&&b.syncPropertiesFromLexical(a,c,d)}syncChildrenFromLexical(a,b,c,d,e){var g=this.getPrevNode(c);let h=null===g?[]:g.__children;g=b.__children;let f=h.length-1,k=g.length-1,m=a.collabNodeMap,n,r,t=0;for(b=0;t<=f&&b<=k;){var p=h[t];let u=g[b];if(p===u)this._syncChildFromLexical(a,b,u,c,d,e),t++,b++;else{void 0===n&&(n=new Set(h));void 0===r&&(r=new Set(g));var l=r.has(p);p=n.has(u);l?(l=J(u),l=K(a,l,this),m.set(u,l),p?(this.splice(a,b,1,l),t++):this.splice(a,
|
|
27
|
+
b,0,l),b++):(this.splice(a,b,1),t++)}}c=t>f;d=b>k;if(c&&!d)for(;b<=k;++b)c=g[b],d=J(c),d=K(a,d,this),this.append(d),m.set(c,d);else if(d&&!c)for(g=this._children.length-1;g>=b;g--)this.splice(a,g,1)}append(a){let b=this._xmlText;var c=this._children;c=c[c.length-1];c=void 0!==c?c.getOffset()+c.getSize():0;if(a instanceof O)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
|
|
28
|
+
R&&b.insertEmbed(c,a._xmlElem);this._children.push(a)}splice(a,b,c,d){let e=this._children;var g=e[b];if(void 0===g)void 0===d&&B(94),this.append(d);else{var h=g.getOffset();-1===h&&B(95);var f=this._xmlText;0!==c&&f.delete(h,g.getSize());d instanceof O?f.insertEmbed(h,d._xmlText):d instanceof E?(g=d._map,null===g.parent&&f.insertEmbed(h,g),f.insert(h+1,d._text)):d instanceof C?f.insertEmbed(h,d._map):d instanceof R&&f.insertEmbed(h,d._xmlElem);if(0!==c)for(h=e.slice(b,b+c),f=0;f<h.length;f++)h[f].destroy(a);
|
|
29
|
+
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);b.delete(this._key)}}function S(a,b,c){return void 0===b?(a=a.getWritable(),a.__children=c,a):b}function L(a,b,c){b=new O(a,b,c);return a._collabNode=b}
|
|
30
|
+
function T(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 U(a,b){if(null==a){if(null!=b)return!0}else if(null==b||!v.compareRelativePositions(a,b))return!0;return!1}function V(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])}}
|
|
31
|
+
function W(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,g]=X(b.type,b.index),[h,f]=X(a.type,a.index);if(null!==e&&null!==h){let k=e.getKey();a=h.getKey();b=q.$getSelection();if(q.$isRangeSelection(b)){var d=b.anchor;c=b.focus;if(d.key!==k||d.offset!==g)d=q.$getNodeByKey(k),b.anchor.set(k,g,q.$isElementNode(d)?
|
|
32
|
+
"element":"text");if(c.key!==a||c.offset!==f)c=q.$getNodeByKey(a),b.focus.set(a,f,q.$isElementNode(c)?"element":"text")}}}}function X(a,b){a=a._collabNode;if(void 0===a)return[null,0];if(a instanceof O){let {node:c,offset:d}=P(a,b,!0);return null===c?[a,0]:[c,d]}return[null,0]}
|
|
33
|
+
function Y(a,b){var c=Array.from(b.awareness.getStates()),d=a.clientID;b=a.cursors;var e=a.editor._editorState._nodeMap;let g=new Set;for(var h=0;h<c.length;h++){let [x,ia]=c[h];if(x!==d){g.add(x);let {anchorPos:Z,focusPos:aa,name:ja,color:ka,focusing:la}=ia;var f=null,k=b.get(x);void 0===k&&(k={color:ka,name:ja,selection:null},b.set(x,k));if(null!==Z&&null!==aa&&la){var m=v.createAbsolutePositionFromRelativePosition(Z,a.doc),n=v.createAbsolutePositionFromRelativePosition(aa,a.doc);if(null!==m&&null!==
|
|
34
|
+
n){let [ba,ca]=X(m.type,m.index),[da,ea]=X(n.type,n.index);if(null!==ba&&null!==da){m=ba.getKey();var r=da.getKey();f=k.selection;if(null===f){f=k;n=ca;var t=ea,p=f.color,l=document.createElement("span");l.style.cssText=`position:absolute;top:0;bottom:0;right:-1px;width:1px;background-color:rgb(${p});z-index:10;`;var u=document.createElement("span");u.textContent=f.name;u.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;`;
|
|
35
|
+
l.appendChild(u);f={anchor:{key:m,offset:n},caret:l,color:p,focus:{key:r,offset:t},name:u,selections:[]}}else n=f.anchor,t=f.focus,n.key=m,n.offset=ca,t.key=r,t.offset=ea}}}m=a;n=k;l=f;p=e;t=m.editor;f=t.getRootElement();k=m.cursorsContainer;if(null!==k&&null!==f&&(f=k.offsetParent,null!==f))if(f=f.getBoundingClientRect(),r=n.selection,null===l)null!==r&&(n.selection=null,V(m,r));else{n.selection=l;n=l.caret;r=l.color;m=l.selections;u=l.anchor;l=l.focus;var w=l.key,A=p.get(u.key);p=p.get(w);if(null!=
|
|
36
|
+
A&&null!=p&&(l=y.createDOMRange(t,A,u.offset,p,l.offset),null!==l)){p=m.length;l=y.createRectsFromDOMRange(t,l);t=l.length;for(u=0;u<t;u++)A=l[u],w=m[u],void 0===w&&(w=document.createElement("span"),m[u]=w,k.appendChild(w)),w.style.cssText=`position:absolute;top:${A.top-f.top}px;left:${A.left-f.left}px;height:${A.height}px;width:${A.width}px;background-color:rgba(${r}, 0.3);pointer-events:none;z-index:5;`,u===t-1&&n.parentNode!==w&&w.appendChild(n);for(f=p-1;f>=t;f--)k.removeChild(m[f]),m.pop()}}}}c=
|
|
37
|
+
Array.from(b.keys());for(d=0;d<c.length;d++)e=c[d],g.has(e)||(h=b.get(e),void 0!==h&&(h=h.selection,null!==h&&V(a,h),b.delete(e)))}function ha(a,b,c,d){b=b.awareness;var e=b.getLocalState();if(null!==e){var {anchorPos:g,focusPos:h,name:f,color:k,focusing:m}=e,n=e=null;if(null!==d&&(null===g||d.is(c))||null!==c)q.$isRangeSelection(d)&&(e=T(d.anchor,a),n=T(d.focus,a)),(U(g,e)||U(h,n))&&b.setLocalState({anchorPos:e,color:k,focusPos:n,focusing:m,name:f})}}let ma=q.createCommand(),na=q.createCommand();
|
|
38
|
+
exports.CONNECTED_COMMAND=ma;exports.TOGGLE_CONNECT_COMMAND=na;exports.createBinding=function(a,b,c,d,e){void 0!==d&&null!==d||B(81);b=d.get("root",v.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}};exports.createUndoManager=function(a,b){return new v.UndoManager(b,{trackedOrigins:new Set([a,null])})};
|
|
39
|
+
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=Y;
|
|
40
|
+
exports.syncLexicalUpdateToYjs=function(a,b,c,d,e,g,h,f){fa(a,()=>{d.read(()=>{if(f.has("collaboration")){if(0<h.size){var k=Array.from(h),m=a.collabNodeMap,n=[];for(let l=0;l<k.length;l++){var r=k[l],t=q.$getNodeByKey(r),p=m.get(r);if(p instanceof E)if(q.$isTextNode(t))n.push([p,t.__text]);else{t=p.getOffset();if(-1===t)continue;let u=p._parent;p._normalized=!0;u._xmlText.delete(t,1);m.delete(r);r=u._children;p=r.indexOf(p);r.splice(p,1)}}for(k=0;k<n.length;k++){let [l,u]=n[k];l instanceof E&&"string"===
|
|
41
|
+
typeof u&&(l._text=u)}}}else e.has("root")&&(n=c._nodeMap,k=q.$getRoot(),m=a.root,m.syncPropertiesFromLexical(a,k,n),m.syncChildrenFromLexical(a,k,n,e,g)),n=q.$getSelection(),ha(a,b,c._selection,n)})})};
|
|
42
|
+
exports.syncYjsChangesToLexical=function(a,b,c){let d=a.editor,e=d._editorState;d.update(()=>{var g=d._pendingEditorState;for(var h=0;h<c.length;h++){var f=a,k=c[h],{target:m}=k;m=N(f,m);if(m instanceof O&&k instanceof v.YTextEvent){let {keysChanged:n,childListChanged:r,delta:t}=k;0<n.size&&m.syncPropertiesFromYjs(f,n);r&&(m.applyChildrenYjsDelta(f,t),m.syncChildrenFromYjs(f))}else m instanceof E&&k instanceof v.YMapEvent?({keysChanged:k}=k,0<k.size&&m.syncPropertiesAndTextFromYjs(f,k)):m instanceof
|
|
43
|
+
R&&k instanceof v.YXmlEvent?({attributesChanged:k}=k,0<k.size&&m.syncPropertiesFromYjs(f,k)):B(82)}h=q.$getSelection();if(q.$isRangeSelection(h))if(Q(h)){f=e._selection;if(q.$isRangeSelection(f)){m=z.$createOffsetView(d,0,e);g=z.$createOffsetView(d,0,g);let [n,r]=m.getOffsetsFromSelection(f);g=g.createSelectionFromOffsets(n,r,m);null!==g?q.$setSelection(g):(W(a,b),Q(h)&&(g=q.$getRoot(),0===g.getChildrenSize()&&g.append(q.$createParagraphNode()),q.$getRoot().selectEnd()))}ha(a,b,f,q.$getSelection())}else W(a,
|
|
44
|
+
b)},{onUpdate:()=>{Y(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.5.0",
|
|
15
15
|
"main": "LexicalYjs.js",
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@lexical/offset": "0.
|
|
17
|
+
"@lexical/offset": "0.5.0"
|
|
18
18
|
},
|
|
19
19
|
"peerDependencies": {
|
|
20
|
-
"lexical": "0.
|
|
20
|
+
"lexical": "0.5.0",
|
|
21
21
|
"yjs": ">=13.5.22"
|
|
22
22
|
},
|
|
23
23
|
"repository": {
|