@lexical/yjs 0.7.9 → 0.8.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 CHANGED
@@ -1415,7 +1415,7 @@ function syncEvent(binding, event) {
1415
1415
  }
1416
1416
  }
1417
1417
  }
1418
- function syncYjsChangesToLexical(binding, provider, events) {
1418
+ function syncYjsChangesToLexical(binding, provider, events, isFromUndoManger) {
1419
1419
  const editor = binding.editor;
1420
1420
  const currentEditorState = editor._editorState;
1421
1421
  editor.update(() => {
@@ -1465,7 +1465,7 @@ function syncYjsChangesToLexical(binding, provider, events) {
1465
1465
  syncCursorPositions(binding, provider);
1466
1466
  },
1467
1467
  skipTransforms: true,
1468
- tag: 'collaboration'
1468
+ tag: isFromUndoManger ? 'historic' : 'collaboration'
1469
1469
  });
1470
1470
  }
1471
1471
  function handleNormalizationMergeConflicts(binding, normalizedNodes) {
@@ -1514,7 +1514,7 @@ function syncLexicalUpdateToYjs(binding, provider, prevEditorState, currEditorSt
1514
1514
  // types a character and we get it, we don't want to then insert
1515
1515
  // the same character again. The exception to this heuristic is
1516
1516
  // when we need to handle normalization merge conflicts.
1517
- if (tags.has('collaboration')) {
1517
+ if (tags.has('collaboration') || tags.has('historic')) {
1518
1518
  if (normalizedNodes.size > 0) {
1519
1519
  handleNormalizationMergeConflicts(binding, normalizedNodes);
1520
1520
  }
@@ -11,22 +11,22 @@ a=this.getOffset()+1+a;0!==b&&d.delete(a,b);""!==c&&d.insert(a,c)}syncProperties
11
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 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=t.$getNodeByKey(a);null===a&&B(85);return a}
12
12
  function L(a,b,c){let d=b.__type;if(t.$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 t.$isTextNode(b)?(e=new v.Map,e=I(e,b.__text,c,d),e.syncPropertiesAndTextFromLexical(a,b,null)):t.$isLineBreakNode(b)?(a=new v.Map,a.set("__type","linebreak"),e=D(a,c)):t.$isDecoratorNode(b)?(e=new v.XmlElement,e=N(e,c,d),e.syncPropertiesFromLexical(a,b,null)):B(86);e._key=b.__key;return e}
13
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&&B(87);void 0===e.get(f)&&B(88);e=b.parent;a=void 0===c&&null!==e?O(a,e):c||null;a instanceof P||B(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=t.createEditor();let q=k.guid;f._key=q;n.set(q,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 q=a.docMap,m;g instanceof e&&(g=g._key,m=q.get(g),q.delete(g));g=m||new v.Doc;let l=g.guid;n._key=l;q.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}}
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 l=b instanceof v.Map?b.get(g):b.getAttribute(g);if(f!==l){if(l instanceof v.Doc){let p=a.docMap;f instanceof v.Doc&&p.delete(f.guid);f=t.createEditor();let m=l.guid;f._key=m;p.set(m,l);l=f}void 0===e&&(e=c.getWritable());e[g]=l}}}
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(l=>!J.has(l)),f.set(e,h));e=a.editor.constructor;for(f=0;f<h.length;f++){let l=h[f];var g=null===c?void 0:c[l];let p=d[l];if(g!==p){if(p instanceof e){let m=a.docMap,n;g instanceof e&&(g=g._key,n=m.get(g),m.delete(g));g=n||new v.Doc;let k=g.guid;p._key=k;m.set(k,g);p=g;a.editor.update(()=>{d.markDirty()})}b instanceof v.Map?b.set(l,p):b.setAttribute(l,p)}}}
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,l=a.getSize();d+=l;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
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()||t.$isTextNode(d)&&b.offset>d.getTextContentSize()||t.$isTextNode(e)&&a.offset>e.getTextContentSize())c=!0}catch(d){c=!0}return c}function aa(a,b){a.doc.transact(b,a)}function S(a,b){let c=[];for(a=a.__first;null!==a;){let d=null===b?t.$getNodeByKey(a):b.get(a);null!==d&&void 0!==d||B(101);c.push(a);a=d.__next}return c}
18
18
  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,
19
19
  a.__prev=c.__key):a.__prev=null,e.__next=null);b.__size--;e.__parent=null}}
20
20
  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 t.$isDecoratorNode(a)?a:null}getNode(){let a=t.$getNodeByKey(this._key);return t.$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);
21
21
  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 N(a,b,c){b=new U(a,b,c);return a._collabNode=b}
22
22
  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 t.$isElementNode(a)?a:null}getNode(){let a=t.$getNodeByKey(this._key);return t.$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,
23
- 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 q=0;q<b.length;q++){var e=b[q],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:p,length:u}=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,u);h=0!==l?c[l-1]:null;var g=m.getSize();if(0===p&&1===e&&0<l&&h instanceof E&&u===g&&0===Array.from(m._map.keys()).length)h._text+=
24
- m._text,c.splice(l,1);else if(0===p&&e===g)c.splice(l,1);else{h=m;g=m._text;var k=p,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();
25
- null===b&&B(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 q=0;var m=null;h!==e&&b.getWritable();for(let x=0;x<h;x++){var l=d[q],p=f[x];var u=p.getNode();var r=p._key;if(null!==u&&l===r)m=t.$isTextNode(u),k.add(l),m&&(p._key=l,p instanceof P?(m=p._xmlText,p.syncPropertiesFromYjs(a,null),p.applyChildrenYjsDelta(a,m.toDelta()),p.syncChildrenFromYjs(a)):p instanceof E?p.syncPropertiesAndTextFromYjs(a,null):p instanceof U?p.syncPropertiesFromYjs(a,
26
- null):p instanceof C||B(93)),m=u,q++;else{if(void 0===n)for(n=new Set,r=0;r<h;r++){var w=f[r]._key;""!==w&&n.add(w)}if(null===u||void 0===l||n.has(l)){u=b.getWritable();l=a;r=p;w=c;var A=r.getType();A=l.editor._nodes.get(A);void 0===A&&B(88);A=new A.klass;A.__parent=w;r._key=A.__key;r instanceof P?(w=r._xmlText,r.syncPropertiesFromYjs(l,null),r.applyChildrenYjsDelta(l,w.toDelta()),r.syncChildrenFromYjs(l)):r instanceof E?r.syncPropertiesAndTextFromYjs(l,null):r instanceof U&&r.syncPropertiesFromYjs(l,
27
- null);l.collabNodeMap.set(A.__key,r);l=A;r=l.__key;g.set(r,p);null===m?(m=u.getFirstChild(),u.__first=r,null!==m&&(m=m.getWritable(),m.__prev=r,l.__next=m.__key)):(p=m.getWritable(),w=m.getNextSibling(),p.__next=r,l.__prev=m.__key,null!==w&&(m=w.getWritable(),m.__prev=r,l.__next=m.__key));x===h-1&&(u.__last=r);u.__size++;m=l}else p=K(l),T(p),x--,q++}}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,
28
- this.getPrevNode(c),b)}_syncChildFromLexical(a,b,c,d,e,f){b=this._children[b];c=K(c);b instanceof P&&t.$isElementNode(c)?(b.syncPropertiesFromLexical(a,c,d),b.syncChildrenFromLexical(a,c,d,e,f)):b instanceof E&&t.$isTextNode(c)?b.syncPropertiesAndTextFromLexical(a,c,d):b instanceof U&&t.$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,q,m,l=0;for(b=
29
- 0;l<=g&&b<=k;){var p=h[l];let r=f[b];if(p===r)this._syncChildFromLexical(a,b,r,c,d,e),l++,b++;else{void 0===q&&(q=new Set(h));void 0===m&&(m=new Set(f));var u=m.has(p);p=q.has(r);u?(u=K(r),u=L(a,u,this),n.set(r,u),p?(this.splice(a,b,1,u),l++):this.splice(a,b,0,u),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;
23
+ 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 m=0;m<b.length;m++){var e=b[m],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:n,nodeIndex:k,offset:q,length:u}=Q(this,d,!1);if(n instanceof P||n instanceof C||n instanceof U)c.splice(k,1),--f;else if(n instanceof E){e=Math.min(f,u);h=0!==k?c[k-1]:null;var g=n.getSize();if(0===q&&1===e&&0<k&&h instanceof E&&u===g&&0===Array.from(n._map.keys()).length)h._text+=
24
+ n._text,c.splice(k,1);else if(0===q&&e===g)c.splice(k,1);else{h=n;g=n._text;var l=q,p=e;g=g.slice(0,l)+""+g.slice(l+p);h._text=g}f-=e}else break}else if(null!=f)if("string"===typeof f){let {node:n,offset:k}=Q(this,d,!0);n instanceof E?(e=n,h=n._text,g=k,l=f,h=h.slice(0,g)+l+h.slice(g+0),e._text=h):this._xmlText.delete(k,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();
25
+ null===b&&B(92);var c=b.__key;let d=S(b,null),e=d.length;var f=this._children;let h=f.length,g=a.collabNodeMap,l=new Set,p;let m=0;var n=null;h!==e&&b.getWritable();for(let x=0;x<h;x++){var k=d[m],q=f[x];var u=q.getNode();var r=q._key;if(null!==u&&k===r)n=t.$isTextNode(u),l.add(k),n&&(q._key=k,q instanceof P?(n=q._xmlText,q.syncPropertiesFromYjs(a,null),q.applyChildrenYjsDelta(a,n.toDelta()),q.syncChildrenFromYjs(a)):q instanceof E?q.syncPropertiesAndTextFromYjs(a,null):q instanceof U?q.syncPropertiesFromYjs(a,
26
+ null):q instanceof C||B(93)),n=u,m++;else{if(void 0===p)for(p=new Set,r=0;r<h;r++){var w=f[r]._key;""!==w&&p.add(w)}if(null===u||void 0===k||p.has(k)){u=b.getWritable();k=a;r=q;w=c;var A=r.getType();A=k.editor._nodes.get(A);void 0===A&&B(88);A=new A.klass;A.__parent=w;r._key=A.__key;r instanceof P?(w=r._xmlText,r.syncPropertiesFromYjs(k,null),r.applyChildrenYjsDelta(k,w.toDelta()),r.syncChildrenFromYjs(k)):r instanceof E?r.syncPropertiesAndTextFromYjs(k,null):r instanceof U&&r.syncPropertiesFromYjs(k,
27
+ null);k.collabNodeMap.set(A.__key,r);k=A;r=k.__key;g.set(r,q);null===n?(n=u.getFirstChild(),u.__first=r,null!==n&&(n=n.getWritable(),n.__prev=r,k.__next=n.__key)):(q=n.getWritable(),w=n.getNextSibling(),q.__next=r,k.__prev=n.__key,null!==w&&(n=w.getWritable(),n.__prev=r,k.__next=n.__key));x===h-1&&(u.__last=r);u.__size++;n=k}else q=K(k),T(q),x--,m++}}for(b=0;b<e;b++)f=d[b],l.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,
28
+ this.getPrevNode(c),b)}_syncChildFromLexical(a,b,c,d,e,f){b=this._children[b];c=K(c);b instanceof P&&t.$isElementNode(c)?(b.syncPropertiesFromLexical(a,c,d),b.syncChildrenFromLexical(a,c,d,e,f)):b instanceof E&&t.$isTextNode(c)?b.syncPropertiesAndTextFromLexical(a,c,d):b instanceof U&&t.$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,l=f.length-1,p=a.collabNodeMap,m,n,k=0;for(b=
29
+ 0;k<=g&&b<=l;){var q=h[k];let r=f[b];if(q===r)this._syncChildFromLexical(a,b,r,c,d,e),k++,b++;else{void 0===m&&(m=new Set(h));void 0===n&&(n=new Set(f));var u=n.has(q);q=m.has(r);u?(u=K(r),u=L(a,u,this),p.set(r,u),q?(this.splice(a,b,1,u),k++):this.splice(a,b,0,u),b++):(this.splice(a,b,1),k++)}}c=k>g;d=b>l;if(c&&!d)for(;b<=l;++b)c=f[b],d=K(c),d=L(a,d,this),this.append(d),p.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;
30
30
  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&&B(94),this.append(d);else{var h=f.getOffset();-1===h&&B(95);var g=this._xmlText;0!==c&&g.delete(h,f.getSize());d instanceof
31
31
  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);
32
32
  b.delete(this._key)}}function M(a,b,c){b=new P(a,b,c);return a._collabNode=b}
@@ -34,16 +34,16 @@ function V(a,b){var c=b.collabNodeMap.get(a.key);if(void 0===c)return null;b=a.o
34
34
  function W(a,b){if(null==a){if(null!=b)return!0}else if(null==b||!v.compareRelativePositions(a,b))return!0;return!1}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
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 [d,e]=Z(b.type,b.index),[f,h]=Z(a.type,a.index);if(null!==d&&null!==f){b=d.getKey();c=f.getKey();let g=t.$getSelection();t.$isRangeSelection(g)&&(a=g.focus,ba(g.anchor,b,e),ba(a,c,h))}}}
36
36
  function ba(a,b,c){if(a.key!==b||a.offset!==c){let d=t.$getNodeByKey(b);if(null!==d&&!t.$isElementNode(d)&&!t.$isTextNode(d)){let e=d.getParentOrThrow();b=e.getKey();c=d.getIndexWithinParent();d=e}a.set(b,c,t.$isElementNode(d)?"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 ja(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 [H,la]=c[h];if(H!==d){f.add(H);let {anchorPos:ca,focusPos:da,name:ma,color:na,focusing:oa}=la;var g=null,k=b.get(H);void 0===k&&(k={color:na,name:ma,selection:null},b.set(H,k));if(null!==ca&&null!==da&&oa){var n=v.createAbsolutePositionFromRelativePosition(ca,a.doc),q=v.createAbsolutePositionFromRelativePosition(da,a.doc);if(null!==
38
- n&&null!==q){let [ea,fa]=Z(n.type,n.index),[ha,ia]=Z(q.type,q.index);if(null!==ea&&null!==ha){n=ea.getKey();var m=ha.getKey();g=k.selection;if(null===g){g=k;q=fa;var l=ia,p=g.color,u=document.createElement("span");u.style.cssText=`position:absolute;top:0;bottom:0;right:-1px;width:1px;background-color:${p};z-index:10;`;var r=document.createElement("span");r.textContent=g.name;r.style.cssText=`position:absolute;left:-2px;top:-16px;background-color:${p};color:#fff;line-height:12px;font-size:12px;padding:2px;font-family:Arial;font-weight:bold;white-space:nowrap;`;
39
- u.appendChild(r);g={anchor:{key:n,offset:q},caret:u,color:p,focus:{key:m,offset:l},name:r,selections:[]}}else q=g.anchor,l=g.focus,q.key=n,q.offset=fa,l.key=m,l.offset=ia}}}a:if(n=a,q=k,u=g,p=e,l=n.editor,g=l.getRootElement(),k=n.cursorsContainer,null!==k&&null!==g&&(g=k.offsetParent,null!==g))if(g=g.getBoundingClientRect(),m=q.selection,null===u)null!==m&&(q.selection=null,X(n,m));else{q.selection=u;q=u.caret;m=u.color;n=u.selections;r=u.anchor;u=u.focus;var w=r.key,A=u.key,x=p.get(w);p=p.get(A);
40
- if(null!=x&&null!=p){if(x===p&&t.$isLineBreakNode(x))p=[l.getElementByKey(w).getBoundingClientRect()];else{p=y.createDOMRange(l,x,r.offset,p,u.offset);if(null===p)break a;p=y.createRectsFromDOMRange(l,p)}u=n.length;l=p.length;for(r=0;r<l;r++)x=p[r],w=n[r],void 0===w&&(w=document.createElement("span"),n[r]=w,A=document.createElement("span"),w.appendChild(A),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;`,
41
- w.style.cssText=x,w.firstChild.style.cssText=`${x}left:0;top:0;background-color:${m};opacity:0.3;`,r===l-1&&q.parentNode!==w&&w.appendChild(q);for(g=u-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 ka(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,q=e=null;if(null!==d&&(null===f||d.is(c))||null!==c)t.$isRangeSelection(d)&&(e=V(d.anchor,a),q=V(d.focus,a)),(W(f,e)||W(h,q))&&b.setLocalState({anchorPos:e,color:k,focusPos:q,focusing:n,name:g})}}let pa=t.createCommand("CONNECTED_COMMAND"),qa=t.createCommand("TOGGLE_CONNECT_COMMAND");exports.CONNECTED_COMMAND=pa;exports.TOGGLE_CONNECT_COMMAND=qa;
37
+ function ja(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 [H,la]=c[h];if(H!==d){f.add(H);let {anchorPos:ca,focusPos:da,name:ma,color:na,focusing:oa}=la;var g=null,l=b.get(H);void 0===l&&(l={color:na,name:ma,selection:null},b.set(H,l));if(null!==ca&&null!==da&&oa){var p=v.createAbsolutePositionFromRelativePosition(ca,a.doc),m=v.createAbsolutePositionFromRelativePosition(da,a.doc);if(null!==
38
+ p&&null!==m){let [ea,fa]=Z(p.type,p.index),[ha,ia]=Z(m.type,m.index);if(null!==ea&&null!==ha){p=ea.getKey();var n=ha.getKey();g=l.selection;if(null===g){g=l;m=fa;var k=ia,q=g.color,u=document.createElement("span");u.style.cssText=`position:absolute;top:0;bottom:0;right:-1px;width:1px;background-color:${q};z-index:10;`;var r=document.createElement("span");r.textContent=g.name;r.style.cssText=`position:absolute;left:-2px;top:-16px;background-color:${q};color:#fff;line-height:12px;font-size:12px;padding:2px;font-family:Arial;font-weight:bold;white-space:nowrap;`;
39
+ u.appendChild(r);g={anchor:{key:p,offset:m},caret:u,color:q,focus:{key:n,offset:k},name:r,selections:[]}}else m=g.anchor,k=g.focus,m.key=p,m.offset=fa,k.key=n,k.offset=ia}}}a:if(p=a,m=l,u=g,q=e,k=p.editor,g=k.getRootElement(),l=p.cursorsContainer,null!==l&&null!==g&&(g=l.offsetParent,null!==g))if(g=g.getBoundingClientRect(),n=m.selection,null===u)null!==n&&(m.selection=null,X(p,n));else{m.selection=u;m=u.caret;n=u.color;p=u.selections;r=u.anchor;u=u.focus;var w=r.key,A=u.key,x=q.get(w);q=q.get(A);
40
+ if(null!=x&&null!=q){if(x===q&&t.$isLineBreakNode(x))q=[k.getElementByKey(w).getBoundingClientRect()];else{q=y.createDOMRange(k,x,r.offset,q,u.offset);if(null===q)break a;q=y.createRectsFromDOMRange(k,q)}u=p.length;k=q.length;for(r=0;r<k;r++)x=q[r],w=p[r],void 0===w&&(w=document.createElement("span"),p[r]=w,A=document.createElement("span"),w.appendChild(A),l.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;`,
41
+ w.style.cssText=x,w.firstChild.style.cssText=`${x}left:0;top:0;background-color:${n};opacity:0.3;`,r===k-1&&m.parentNode!==w&&w.appendChild(m);for(g=u-1;g>=k;g--)l.removeChild(p[g]),p.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 ka(a,b,c,d){b=b.awareness;var e=b.getLocalState();if(null!==e){var {anchorPos:f,focusPos:h,name:g,color:l,focusing:p}=e,m=e=null;if(null!==d&&(null===f||d.is(c))||null!==c)t.$isRangeSelection(d)&&(e=V(d.anchor,a),m=V(d.focus,a)),(W(f,e)||W(h,m))&&b.setLocalState({anchorPos:e,color:l,focusPos:m,focusing:p,name:g})}}let pa=t.createCommand("CONNECTED_COMMAND"),qa=t.createCommand("TOGGLE_CONNECT_COMMAND");exports.CONNECTED_COMMAND=pa;exports.TOGGLE_CONNECT_COMMAND=qa;
43
43
  exports.createBinding=function(a,b,c,d,e){void 0!==d&&null!==d||B(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
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=ja;
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,q=[];for(let u=0;u<k.length;u++){var m=k[u],l=t.$getNodeByKey(m),p=n.get(m);if(p instanceof E)if(t.$isTextNode(l))q.push([p,l.__text]);else{l=p.getOffset();if(-1===l)continue;let r=p._parent;p._normalized=!0;r._xmlText.delete(l,1);n.delete(m);m=r._children;p=m.indexOf(p);m.splice(p,1)}}for(k=0;k<q.length;k++){let [u,r]=q[k];u instanceof E&&"string"===
46
- typeof r&&(u._text=r)}}}else e.has("root")&&(q=c._nodeMap,k=t.$getRoot(),n=a.root,n.syncPropertiesFromLexical(a,k,q),n.syncChildrenFromLexical(a,k,q,e,f)),q=t.$getSelection(),ka(a,b,c._selection,q)})})};
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:q,childListChanged:m,delta:l}=k;0<q.size&&n.syncPropertiesFromYjs(g,q);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)):B(82)}h=t.$getSelection();if(t.$isRangeSelection(h))if(R(h)){g=e._selection;if(t.$isRangeSelection(g)){n=z.$createOffsetView(d,0,e);f=z.$createOffsetView(d,0,f);let [q,m]=n.getOffsetsFromSelection(g);f=f.createSelectionFromOffsets(q,m,n);null!==f?t.$setSelection(f):(Y(a,b),R(h)&&(f=t.$getRoot(),0===f.getChildrenSize()&&f.append(t.$createParagraphNode()),t.$getRoot().selectEnd()))}ka(a,b,g,t.$getSelection())}else Y(a,
49
- b)},{onUpdate:()=>{ja(a,b)},skipTransforms:!0,tag:"collaboration"})}
45
+ exports.syncLexicalUpdateToYjs=function(a,b,c,d,e,f,h,g){aa(a,()=>{d.read(()=>{if(g.has("collaboration")||g.has("historic")){if(0<h.size){var l=Array.from(h),p=a.collabNodeMap,m=[];for(let u=0;u<l.length;u++){var n=l[u],k=t.$getNodeByKey(n),q=p.get(n);if(q instanceof E)if(t.$isTextNode(k))m.push([q,k.__text]);else{k=q.getOffset();if(-1===k)continue;let r=q._parent;q._normalized=!0;r._xmlText.delete(k,1);p.delete(n);n=r._children;q=n.indexOf(q);n.splice(q,1)}}for(l=0;l<m.length;l++){let [u,r]=m[l];
46
+ u instanceof E&&"string"===typeof r&&(u._text=r)}}}else e.has("root")&&(m=c._nodeMap,l=t.$getRoot(),p=a.root,p.syncPropertiesFromLexical(a,l,m),p.syncChildrenFromLexical(a,l,m,e,f)),m=t.$getSelection(),ka(a,b,c._selection,m)})})};
47
+ exports.syncYjsChangesToLexical=function(a,b,c,d){let e=a.editor,f=e._editorState;e.update(()=>{var h=e._pendingEditorState;for(var g=0;g<c.length;g++){var l=a,p=c[g],{target:m}=p;m=O(l,m);if(m instanceof P&&p instanceof v.YTextEvent){let {keysChanged:n,childListChanged:k,delta:q}=p;0<n.size&&m.syncPropertiesFromYjs(l,n);k&&(m.applyChildrenYjsDelta(l,q),m.syncChildrenFromYjs(l))}else m instanceof E&&p instanceof v.YMapEvent?({keysChanged:p}=p,0<p.size&&m.syncPropertiesAndTextFromYjs(l,p)):m instanceof
48
+ U&&p instanceof v.YXmlEvent?({attributesChanged:p}=p,0<p.size&&m.syncPropertiesFromYjs(l,p)):B(82)}g=t.$getSelection();if(t.$isRangeSelection(g))if(R(g)){l=f._selection;if(t.$isRangeSelection(l)){m=z.$createOffsetView(e,0,f);h=z.$createOffsetView(e,0,h);let [n,k]=m.getOffsetsFromSelection(l);h=h.createSelectionFromOffsets(n,k,m);null!==h?t.$setSelection(h):(Y(a,b),R(g)&&(h=t.$getRoot(),0===h.getChildrenSize()&&h.append(t.$createParagraphNode()),t.$getRoot().selectEnd()))}ka(a,b,l,t.$getSelection())}else Y(a,
49
+ b)},{onUpdate:()=>{ja(a,b)},skipTransforms:!0,tag:d?"historic":"collaboration"})}
@@ -9,7 +9,7 @@ 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
- export declare function syncYjsChangesToLexical(binding: Binding, provider: WebsocketProvider, events: Array<YEvent<YText>>): void;
12
+ export declare function syncYjsChangesToLexical(binding: Binding, provider: WebsocketProvider, events: Array<YEvent<YText>>, isFromUndoManger: boolean): void;
13
13
  declare type IntentionallyMarkedAsDirtyElement = boolean;
14
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
15
  export {};
package/package.json CHANGED
@@ -11,13 +11,13 @@
11
11
  "crdt"
12
12
  ],
13
13
  "license": "MIT",
14
- "version": "0.7.9",
14
+ "version": "0.8.0",
15
15
  "main": "LexicalYjs.js",
16
16
  "dependencies": {
17
- "@lexical/offset": "0.7.9"
17
+ "@lexical/offset": "0.8.0"
18
18
  },
19
19
  "peerDependencies": {
20
- "lexical": "0.7.9",
20
+ "lexical": "0.8.0",
21
21
  "yjs": ">=13.5.22"
22
22
  },
23
23
  "repository": {