UNPKG

@lexical/yjs

Version:

The library provides Yjs editor bindings for Lexical.

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