@lexical/yjs
Version:
The library provides Yjs editor bindings for Lexical.
10 lines (8 loc) • 20.4 kB
JavaScript
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
import{$getNodeByKey as e,$isLineBreakNode as t,$isTextNode as n,$getSelection as o,$isRangeSelection as s,$isElementNode as i,$isDecoratorNode as l,createEditor as r,$getWritableNodeState as c,$getRoot as f,$isRootNode as a,$getNodeByKeyOrThrow as u,removeFromParent as d,$addUpdateTag as h,SKIP_SCROLL_INTO_VIEW_TAG as _,$createParagraphNode as p,HISTORIC_TAG as g,COLLABORATION_TAG as y,createCommand as x}from"lexical";import{XmlText as m,Map as k,XmlElement as b,Doc as C,createAbsolutePositionFromRelativePosition as N,createRelativePositionFromTypeIndex as v,compareRelativePositions as w,YMapEvent as S,YTextEvent as P,YXmlEvent as T,UndoManager as F}from"yjs";import{$createChildrenArray as O}from"@lexical/offset";import{createDOMRange as E,createRectsFromDOMRange as M}from"@lexical/selection";function L(e,...t){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",e);for(const e of t)o.append("v",e);throw n.search=o.toString(),Error(`Minified Lexical error #${e}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}class z{constructor(e,t){this._key="",this._map=e,this._parent=t,this._type="linebreak"}getNode(){const n=e(this._key);return t(n)?n:null}getKey(){return this._key}getSharedType(){return this._map}getType(){return this._type}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function j(e,t){const n=new z(e,t);return e._collabNode=n,n}class A{constructor(e,t,n,o){this._key="",this._map=e,this._parent=n,this._text=t,this._type=o,this._normalized=!1}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return n(t)?t:null}getNode(){const t=e(this._key);return n(t)?t: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(e,t,n){const o=this._parent._xmlText,s=this.getOffset()+1+e;0!==t&&o.delete(s,t),""!==n&&o.insert(s,n)}syncPropertiesAndTextFromLexical(e,t,n){const i=this.getPrevNode(n),l=t.__text;if(V(e,this._map,i,t),null!==i){const e=i.__text;if(e!==l){!function(e,t,n,i){const l=o();let r=i.length;if(s(l)&&l.isCollapsed()){const e=l.anchor;e.key===t&&(r=e.offset)}const c=function(e,t,n){const o=e.length,s=t.length;let i=0,l=0;for(;i<o&&i<s&&e[i]===t[i]&&i<n;)i++;for(;l+i<o&&l+i<s&&e[o-l-1]===t[s-l-1];)l++;for(;l+i<o&&l+i<s&&e[i]===t[i];)i++;return{index:i,insert:t.slice(i,s-l),remove:o-i-l}}(n,i,r);e.spliceText(c.index,c.remove,c.insert)}(this,t.__key,e,l),this._text=l}}}syncPropertiesAndTextFromYjs(e,t){const n=this.getNode();null===n&&L(84),J(e,this._map,n,t);const o=this._text;if(n.__text!==o){n.getWritable().__text=o}}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function Y(e,t,n,o){const s=new A(e,t,n,o);return e._collabNode=s,s}const D=new Set(["__key","__parent","__next","__prev","__state"]),I=new Set(["__first","__last","__size","__dir"]),K=new Set(["__cachedText"]),W=new Set(["__text"]);function $(e,t,o){if(D.has(e)||"function"==typeof t[e])return!0;if(n(t)){if(W.has(e))return!0}else if(i(t)&&(I.has(e)||a(t)&&K.has(e)))return!0;const s=t.constructor,l=o.excludedProperties.get(s);return null!=l&&l.has(e)}function R(e,o,s){const r=o.__type;let c;if(i(o)){c=ie(new m,s,r),c.syncPropertiesFromLexical(e,o,null),c.syncChildrenFromLexical(e,o,null,null,null)}else if(n(o)){c=Y(new k,o.__text,s,r),c.syncPropertiesAndTextFromLexical(e,o,null)}else if(t(o)){const e=new k;e.set("__type","linebreak"),c=j(e,s)}else if(l(o)){c=oe(new b,s,r),c.syncPropertiesFromLexical(e,o,null)}else L(86);return c._key=o.__key,c}function U(e){const t=q(e,"__type");return"string"!=typeof t&&void 0!==t&&L(87),t}function B(e,t,n){const o=t._collabNode;if(void 0===o){const o=e.editor._nodes,s=U(t);"string"!=typeof s&&L(87);void 0===o.get(s)&&L(88,s);const i=t.parent,l=void 0===n&&null!==i?B(e,i):n||null;if(l instanceof se||L(89),t instanceof m)return ie(t,l,s);if(t instanceof k)return"linebreak"===s?j(t,l):Y(t,"",l,s);if(t instanceof b)return oe(t,l,s)}return o}function G(e,t,n){const o=t.getType(),s=e.editor._nodes.get(o);void 0===s&&L(88,o);const i=new s.klass;if(i.__parent=n,t._key=i.__key,t instanceof se){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof A?t.syncPropertiesAndTextFromYjs(e,null):t instanceof ne&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(i.__key,t),i}function J(e,t,n,o){const s=null===o?t instanceof k?Array.from(t.keys()):Object.keys(t.getAttributes()):Array.from(o);let i;for(let o=0;o<s.length;o++){const l=s[o];if($(l,n,e)){"__state"===l&&(i||(i=n.getWritable()),Q(e,t,i));continue}const c=n[l];let f=q(t,l);if(c!==f){if(f instanceof C){const t=e.docMap;c instanceof C&&t.delete(c.guid);const n=r(),o=f.guid;n._key=o,t.set(o,f),f=n}void 0===i&&(i=n.getWritable()),i[l]=f}}}function q(e,t){return e instanceof k?e.get(t):e.getAttribute(t)}function H(e,t,n){e instanceof k?e.set(t,n):e.setAttribute(t,n)}function Q(e,t,n){const o=q(t,"__state");o instanceof k&&c(n).updateFromJSON(o.toJSON())}function V(e,t,n,o){const s=o.__type,i=e.nodeProperties;let l=i.get(s);void 0===l&&(l=Object.keys(o).filter((t=>!$(t,o,e))),i.set(s,l));const r=e.editor.constructor;!function(e,t,n,o){const s=o.__state,i=q(t,"__state");if(!s)return;const[l,r]=s.getInternalState(),c=n&&n.__state,f=i instanceof k?i:new k;if(c===s)return;const[a,u]=c&&f.doc?c.getInternalState():[void 0,new Map];if(l)for(const[e,t]of Object.entries(l))a&&t!==a[e]&&f.set(e,t);for(const[e,t]of r)u.get(e)!==t&&f.set(e.key,e.unparse(t));i||H(t,"__state",f)}(0,t,n,o);for(let s=0;s<l.length;s++){const i=l[s],c=null===n?void 0:n[i];let f=o[i];if(c!==f){if(f instanceof r){const t=e.docMap;let n;if(c instanceof r){const e=c._key;n=t.get(e),t.delete(e)}const s=n||new C,i=s.guid;f._key=i,t.set(i,s),f=s,e.editor.update((()=>{o.markDirty()}))}H(t,i,f)}}}function X(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function Z(e,t,n){let o=0,s=0;const i=e._children,l=i.length;for(;s<l;s++){const e=i[s],r=o;o+=e.getSize();if((n?o>=t:o>t)&&e instanceof A){let n=t-r-1;n<0&&(n=0);return{length:o-t,node:e,nodeIndex:s,offset:n}}if(o>t)return{length:0,node:e,nodeIndex:s,offset:r};if(s===l-1)return{length:0,node:null,nodeIndex:s+1,offset:r+1}}return{length:0,node:null,nodeIndex:0,offset:0}}function ee(e){const t=e.anchor,o=e.focus;let s=!1;try{const e=t.getNode(),i=o.getNode();(!e.isAttached()||!i.isAttached()||n(e)&&t.offset>e.getTextContentSize()||n(i)&&o.offset>i.getTextContentSize())&&(s=!0)}catch(e){s=!0}return s}function te(t,n){const o=n._nodeMap.get(t);if(!o)return void f().selectStart();const s=o.__prev;let i=null;s&&(i=e(s)),null===i&&null!==o.__parent&&(i=e(o.__parent)),null!==i?null!==i&&i.isAttached()?i.selectEnd():te(i.__key,n):f().selectStart()}class ne{constructor(e,t,n){this._key="",this._xmlElem=e,this._parent=t,this._type=n}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return l(t)?t:null}getNode(){const t=e(this._key);return l(t)?t:null}getSharedType(){return this._xmlElem}getType(){return this._type}getKey(){return this._key}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}syncPropertiesFromLexical(e,t,n){const o=this.getPrevNode(n);V(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&L(83);J(e,this._xmlElem,n,t)}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function oe(e,t,n){const o=new ne(e,t,n);return e._collabNode=o,o}class se{constructor(e,t,n){this._key="",this._children=[],this._xmlText=e,this._type=n,this._parent=t}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return i(t)?t:null}getNode(){const t=e(this._key);return i(t)?t:null}getSharedType(){return this._xmlText}getType(){return this._type}getKey(){return this._key}isEmpty(){return 0===this._children.length}getSize(){return 1}getOffset(){const e=this._parent;return null===e&&L(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&L(91),J(e,this._xmlText,n,t)}applyChildrenYjsDelta(e,t){const n=this._children;let o=0,s=null;for(let i=0;i<t.length;i++){const l=t[i],r=l.insert,c=l.delete;if(null!=l.retain)o+=l.retain;else if("number"==typeof c){let e=c;for(;e>0;){const{node:t,nodeIndex:s,offset:i,length:l}=Z(this,o,!1);if(t instanceof se||t instanceof z||t instanceof ne)n.splice(s,1),e-=1;else{if(!(t instanceof A))break;{const o=Math.min(e,l),r=0!==s?n[s-1]:null,c=t.getSize();if(0===i&&l===c){n.splice(s,1);const e=X(t._text,i,o-1,"");e.length>0&&(r instanceof A?r._text+=e:this._xmlText.delete(i,e.length))}else t._text=X(t._text,i,o,"");e-=o}}}}else{if(null==r)throw new Error("Unexpected delta format");if("string"==typeof r){const{node:e,offset:t}=Z(this,o,!0);e instanceof A?e._text=X(e._text,t,0,r):this._xmlText.delete(t,r.length),o+=r.length}else{const t=r,{node:i,nodeIndex:l,length:c}=Z(this,o,!1),f=B(e,t,this);if(i instanceof A&&c>0&&c<i._text.length){const e=i._text,t=e.length-c;i._text=X(e,t,c,""),n.splice(l+1,0,f),s=X(e,0,t,"")}else n.splice(l,0,f);null!==s&&f instanceof A&&(f._text=s+f._text,s=null),o+=1}}}}syncChildrenFromYjs(e){const t=this.getNode();null===t&&L(92);const o=t.__key,s=O(t,null),i=s.length,l=this._children,r=l.length,c=e.collabNodeMap,f=new Set;let a,h,_=0,p=null;r!==i&&(h=t.getWritable());for(let i=0;i<r;i++){const g=s[_],y=l[i],x=y.getNode(),m=y._key;if(null!==x&&g===m){const t=n(x);if(f.add(g),t)if(y._key=g,y instanceof se){const t=y._xmlText;y.syncPropertiesFromYjs(e,null),y.applyChildrenYjsDelta(e,t.toDelta()),y.syncChildrenFromYjs(e)}else y instanceof A?y.syncPropertiesAndTextFromYjs(e,null):y instanceof ne?y.syncPropertiesFromYjs(e,null):y instanceof z||L(93);p=x,_++}else{if(void 0===a){a=new Set;for(let e=0;e<r;e++){const t=l[e]._key;""!==t&&a.add(t)}}if(null!==x&&void 0!==g&&!a.has(g)){const e=u(g);d(e),i--,_++;continue}h=t.getWritable();const n=G(e,y,o),s=n.__key;if(c.set(s,y),null===p){const e=h.getFirstChild();if(h.__first=s,null!==e){const t=e.getWritable();t.__prev=s,n.__next=t.__key}}else{const e=p.getWritable(),t=p.getNextSibling();if(e.__next=s,n.__prev=p.__key,null!==t){const e=t.getWritable();e.__prev=s,n.__next=e.__key}}i===r-1&&(h.__last=s),h.__size++,p=n}}for(let t=0;t<i;t++){const n=s[t];if(!f.has(n)){const t=u(n),o=e.collabNodeMap.get(n);void 0!==o&&o.destroy(e),d(t)}}}syncPropertiesFromLexical(e,t,n){V(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(e,t,o,s,r,c){const f=this._children[t],a=u(o);f instanceof se&&i(a)?(f.syncPropertiesFromLexical(e,a,s),f.syncChildrenFromLexical(e,a,s,r,c)):f instanceof A&&n(a)?f.syncPropertiesAndTextFromLexical(e,a,s):f instanceof ne&&l(a)&&f.syncPropertiesFromLexical(e,a,s)}syncChildrenFromLexical(e,t,n,o,s){const i=this.getPrevNode(n),l=null===i?[]:O(i,n),r=O(t,null),c=l.length-1,f=r.length-1,a=e.collabNodeMap;let d,h,_=0,p=0;for(;_<=c&&p<=f;){const t=l[_],i=r[p];if(t===i)this._syncChildFromLexical(e,p,i,n,o,s),_++,p++;else{void 0===d&&(d=new Set(l)),void 0===h&&(h=new Set(r));const n=h.has(t),o=d.has(i);if(n){const t=R(e,u(i),this);a.set(i,t),o?(this.splice(e,p,1,t),_++,p++):(this.splice(e,p,0,t),p++)}else this.splice(e,p,1),_++}}const g=_>c,y=p>f;if(g&&!y)for(;p<=f;++p){const t=r[p],n=R(e,u(t),this);this.append(n),a.set(t,n)}else if(y&&!g)for(let t=this._children.length-1;t>=p;t--)this.splice(e,t,1)}append(e){const t=this._xmlText,n=this._children,o=n[n.length-1],s=void 0!==o?o.getOffset()+o.getSize():0;if(e instanceof se)t.insertEmbed(s,e._xmlText);else if(e instanceof A){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof z?t.insertEmbed(s,e._map):e instanceof ne&&t.insertEmbed(s,e._xmlElem);this._children.push(e)}splice(e,t,n,o){const s=this._children,i=s[t];if(void 0===i)return void 0===o&&L(94),void this.append(o);const l=i.getOffset();-1===l&&L(95);const r=this._xmlText;if(0!==n&&r.delete(l,i.getSize()),o instanceof se)r.insertEmbed(l,o._xmlText);else if(o instanceof A){const e=o._map;null===e.parent&&r.insertEmbed(l,e),r.insert(l+1,o._text)}else o instanceof z?r.insertEmbed(l,o._map):o instanceof ne&&r.insertEmbed(l,o._xmlElem);if(0!==n){const o=s.slice(t,t+n);for(let t=0;t<o.length;t++)o[t].destroy(e)}void 0!==o?s.splice(t,n,o):s.splice(t,n)}getChildOffset(e){let t=0;const n=this._children;for(let o=0;o<n.length;o++){const s=n[o];if(s===e)return t;t+=s.getSize()}return-1}destroy(e){const t=e.collabNodeMap,n=this._children;for(let t=0;t<n.length;t++)n[t].destroy(e);t.get(this._key)===this&&t.delete(this._key)}}function ie(e,t,n){const o=new se(e,t,n);return e._collabNode=o,o}function le(e,t,n,o,s,i){null==o&&L(81);const l=ie(o.get("root",m),null,"root");return l._key="root",{clientID:o.clientID,collabNodeMap:new Map,cursors:new Map,cursorsContainer:null,doc:o,docMap:s,editor:e,excludedProperties:i||new Map,id:n,nodeProperties:new Map,root:l}}function re(e,t){const o=t.collabNodeMap.get(e.key);if(void 0===o)return null;let s=e.offset,l=o.getSharedType();if(o instanceof A){l=o._parent._xmlText;const e=o.getOffset();if(-1===e)return null;s=e+1+s}else if(o instanceof se&&"element"===e.type){const t=e.getNode();i(t)||L(184);let o=0,l=0,r=t.getFirstChild();for(;null!==r&&l++<s;)n(r)?o+=r.getTextContentSize()+1:o++,r=r.getNextSibling();s=o}return v(l,s)}function ce(e,t){return N(e,t.doc)}function fe(e,t){if(null==e){if(null!=t)return!0}else if(null==t||!w(e,t))return!0;return!1}function ae(e,t){return{color:t,name:e,selection:null}}function ue(e,t){const n=e.cursorsContainer;if(null!==n){const e=t.selections,o=e.length;for(let t=0;t<o;t++)n.removeChild(e[t])}}function de(e,t){const n=t.selection;null!==n&&ue(e,n)}function he(e,t,n,o,s){const i=e.color,l=document.createElement("span");l.style.cssText=`position:absolute;top:0;bottom:0;right:-1px;width:1px;background-color:${i};z-index:10;`;const r=document.createElement("span");return r.textContent=e.name,r.style.cssText=`position:absolute;left:-2px;top:-16px;background-color:${i};color:#fff;line-height:12px;font-size:12px;padding:2px;font-family:Arial;font-weight:bold;white-space:nowrap;`,l.appendChild(r),{anchor:{key:t,offset:n},caret:l,color:i,focus:{key:o,offset:s},name:r,selections:[]}}function _e(e,n,o,s){const i=e.editor,l=i.getRootElement(),r=e.cursorsContainer;if(null===r||null===l)return;const c=r.offsetParent;if(null===c)return;const f=c.getBoundingClientRect(),a=n.selection;if(null===o)return null===a?void 0:(n.selection=null,void ue(e,a));n.selection=o;const u=o.caret,d=o.color,h=o.selections,_=o.anchor,p=o.focus,g=_.key,y=p.key,x=s.get(g),m=s.get(y);if(null==x||null==m)return;let k;if(x===m&&t(x)){k=[i.getElementByKey(g).getBoundingClientRect()]}else{const e=E(i,x,_.offset,m,p.offset);if(null===e)return;k=M(i,e)}const b=h.length,C=k.length;for(let e=0;e<C;e++){const t=k[e];let n=h[e];if(void 0===n){n=document.createElement("span"),h[e]=n;const t=document.createElement("span");n.appendChild(t),r.appendChild(n)}const o=`position:absolute;top:${t.top-f.top}px;left:${t.left-f.left}px;height:${t.height}px;width:${t.width}px;pointer-events:none;z-index:5;`;n.style.cssText=o,n.firstChild.style.cssText=`${o}left:0;top:0;background-color:${d};opacity:0.3;`,e===C-1&&u.parentNode!==n&&n.appendChild(u)}for(let e=b-1;e>=C;e--){const t=h[e];r.removeChild(t),h.pop()}}function pe(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,i=0,l=null,r=0;if(null!==n&&null!==o){const t=ce(n,e),c=ce(o,e);null!==t&&null!==c&&([s,i]=xe(t.type,t.index),[l,r]=xe(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:i,focusCollabNode:l,focusOffset:r}}function ge(e,t){const n=t.awareness.getLocalState();if(null===n)return;const{anchorCollabNode:i,anchorOffset:l,focusCollabNode:r,focusOffset:c}=pe(e,n);if(null!==i&&null!==r){const e=i.getKey(),t=r.getKey(),n=o();if(!s(n))return;ye(n.anchor,e,l),ye(n.focus,t,c)}}function ye(t,o,s){if(t.key!==o||t.offset!==s){let l=e(o);if(null!==l&&!i(l)&&!n(l)){const e=l.getParentOrThrow();o=e.getKey(),s=l.getIndexWithinParent(),l=e}t.set(o,s,i(l)?"element":"text")}}function xe(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof se){const{node:e,offset:o}=Z(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function me(e,t){return t.awareness.getStates()}function ke(e,t,n){const{getAwarenessStates:o=me}=n??{},s=Array.from(o(e,t)),i=e.clientID,l=e.cursors,r=e.editor._editorState._nodeMap,c=new Set;for(let t=0;t<s.length;t++){const n=s[t],[o,f]=n;if(o!==i){c.add(o);const{name:t,color:n,focusing:s}=f;let i=null,a=l.get(o);if(void 0===a&&(a=ae(t,n),l.set(o,a)),s){const{anchorCollabNode:t,anchorOffset:n,focusCollabNode:o,focusOffset:s}=pe(e,f);if(null!==t&&null!==o){const e=t.getKey(),l=o.getKey();if(i=a.selection,null===i)i=he(a,e,n,l,s);else{const t=i.anchor,o=i.focus;t.key=e,t.offset=n,o.key=l,o.offset=s}}}_e(e,a,i,r)}}const f=Array.from(l.keys());for(let t=0;t<f.length;t++){const n=f[t];if(!c.has(n)){const t=l.get(n);void 0!==t&&(de(e,t),l.delete(n))}}}function be(e,t,n,o){const i=t.awareness,l=i.getLocalState();if(null===l)return;const{anchorPos:r,focusPos:c,name:f,color:a,focusing:u,awarenessData:d}=l;let h=null,_=null;(null!==o&&(null===r||o.is(n))||null!==n)&&(s(o)&&(h=re(o.anchor,e),_=re(o.focus,e)),(fe(r,h)||fe(c,_))&&i.setLocalState({...l,anchorPos:h,awarenessData:d,color:a,focusPos:_,focusing:u,name:f}))}function Ce(e,t){if(t instanceof S&&function(e,t){const{target:n}=t;if(!n._item||"__state"!==n._item.parentSub||void 0!==U(n)||!(n.parent instanceof m||n.parent instanceof b||n.parent instanceof k))return!1;const o=B(e,n.parent).getNode();if(o){const e=c(o.getWritable());for(const o of t.keysChanged)e.updateFromUnknown(o,n.get(o))}return!0}(e,t))return;const{target:n}=t,o=B(e,n);if(o instanceof se&&t instanceof P){const{keysChanged:n,childListChanged:s,delta:i}=t;n.size>0&&o.syncPropertiesFromYjs(e,n),s&&(o.applyChildrenYjsDelta(e,i),o.syncChildrenFromYjs(e))}else if(o instanceof A&&t instanceof S){const{keysChanged:n}=t;n.size>0&&o.syncPropertiesAndTextFromYjs(e,n)}else if(o instanceof ne&&t instanceof T){const{attributesChanged:n}=t;n.size>0&&o.syncPropertiesFromYjs(e,n)}else L(82)}function Ne(e,t,n,i,l=ke){const r=e.editor,c=r._editorState;n.forEach((e=>e.delta)),r.update((()=>{for(let t=0;t<n.length;t++){const o=n[t];Ce(e,o)}const l=o();if(s(l))if(ee(l)){const n=c._selection;if(s(n)&&(ge(e,t),ee(l))){te(l.anchor.key,c)}be(e,t,n,o())}else ge(e,t);i||h(_)}),{onUpdate:()=>{l(e,t),r.update((()=>{0===f().getChildrenSize()&&f().append(p())}))},skipTransforms:!0,tag:i?g:y})}function ve(t,s,i,l,r,c,a,u){!function(e,t){e.doc.transact(t,e)}(t,(()=>{l.read((()=>{if(u.has(y)||u.has(g))return void(a.size>0&&function(t,o){const s=Array.from(o),i=t.collabNodeMap,l=[],r=[];for(let t=0;t<s.length;t++){const o=s[t],c=e(o),f=i.get(o);if(f instanceof A)if(n(c))l.push([f,c.__text]);else{const e=f.getOffset();if(-1===e)continue;const t=f._parent;f._normalized=!0,t._xmlText.delete(e,1),r.push(f)}}for(let e=0;e<r.length;e++){const t=r[e],n=t.getKey();i.delete(n);const o=t._parent._children,s=o.indexOf(t);o.splice(s,1)}for(let e=0;e<l.length;e++){const[t,n]=l[e];t._text=n}}(t,a));if(r.has("root")){const e=i._nodeMap,n=f(),o=t.root;o.syncPropertiesFromLexical(t,n,e),o.syncChildrenFromLexical(t,n,e,r,c)}const l=o(),d=i._selection;be(t,s,d,l)}))}))}const we=x("CONNECTED_COMMAND"),Se=x("TOGGLE_CONNECT_COMMAND");function Pe(e,t){return new F(t,{trackedOrigins:new Set([e,null])})}function Te(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})}function Fe(e,t,n,o,s){const{awareness:i}=e;let l=i.getLocalState();null===l&&(l={anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t}),l.focusing=o,i.setLocalState(l)}export{we as CONNECTED_COMMAND,Se as TOGGLE_CONNECT_COMMAND,le as createBinding,Pe as createUndoManager,pe as getAnchorAndFocusCollabNodesForUserState,Te as initLocalState,Fe as setLocalStateFocus,ke as syncCursorPositions,ve as syncLexicalUpdateToYjs,Ne as syncYjsChangesToLexical};