@lexical/yjs
Version:
The library provides Yjs editor bindings for Lexical.
10 lines (8 loc) • 21.5 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.
*
*/
"use strict";var e=require("lexical"),t=require("yjs"),n=require("@lexical/offset"),o=require("@lexical/selection");function s(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 i{constructor(e,t){this._key="",this._map=e,this._parent=t,this._type="linebreak"}getNode(){const t=e.$getNodeByKey(this._key);return e.$isLineBreakNode(t)?t: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 l(e,t){const n=new i(e,t);return e._collabNode=n,n}class r{constructor(e,t,n,o){this._key="",this._map=e,this._parent=n,this._text=t,this._type=o,this._normalized=!1}getPrevNode(t){if(null===t)return null;const n=t.get(this._key);return e.$isTextNode(n)?n:null}getNode(){const t=e.$getNodeByKey(this._key);return e.$isTextNode(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(t,n,o){const s=this.getPrevNode(o),i=n.__text;if(b(t,this._map,s,n),null!==s){const t=s.__text;if(t!==i){!function(t,n,o,s){const i=e.$getSelection();let l=s.length;if(e.$isRangeSelection(i)&&i.isCollapsed()){const e=i.anchor;e.key===n&&(l=e.offset)}const r=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}}(o,s,l);t.spliceText(r.index,r.remove,r.insert)}(this,n.__key,t,i),this._text=i}}}syncPropertiesAndTextFromYjs(e,t){const n=this.getNode();null===n&&s(84),x(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 c(e,t,n,o){const s=new r(e,t,n,o);return e._collabNode=s,s}const a=new Set(["__key","__parent","__next","__prev","__state"]),f=new Set(["__first","__last","__size"]),u=new Set(["__cachedText"]),d=new Set(["__text"]);function _(t,n,o){if(a.has(t)||"function"==typeof n[t])return!0;if(e.$isTextNode(n)){if(d.has(t))return!0}else if(e.$isElementNode(n)&&(f.has(t)||e.$isRootNode(n)&&u.has(t)))return!0;const s=n.constructor,i=o.excludedProperties.get(s);return null!=i&&i.has(t)}function h(n,o,i){const r=o.__type;let a;if(e.$isElementNode(o)){a=M(new t.XmlText,i,r),a.syncPropertiesFromLexical(n,o,null),a.syncChildrenFromLexical(n,o,null,null,null)}else if(e.$isTextNode(o)){a=c(new t.Map,o.__text,i,r),a.syncPropertiesAndTextFromLexical(n,o,null)}else if(e.$isLineBreakNode(o)){const e=new t.Map;e.set("__type","linebreak"),a=l(e,i)}else if(e.$isDecoratorNode(o)){a=P(new t.XmlElement,i,r),a.syncPropertiesFromLexical(n,o,null)}else s(86);return a._key=o.__key,a}function g(e){const t=m(e,"__type");return"string"!=typeof t&&void 0!==t&&s(87),t}function p(e,n,o){const i=n._collabNode;if(void 0===i){const i=e.editor._nodes,r=g(n);"string"!=typeof r&&s(87);void 0===i.get(r)&&s(88,r);const a=n.parent,f=void 0===o&&null!==a?p(e,a):o||null;if(f instanceof E||s(89),n instanceof t.XmlText)return M(n,f,r);if(n instanceof t.Map)return"linebreak"===r?l(n,f):c(n,"",f,r);if(n instanceof t.XmlElement)return P(n,f,r)}return i}function y(e,t,n){const o=t.getType(),i=e.editor._nodes.get(o);void 0===i&&s(88,o);const l=new i.klass;if(l.__parent=n,t._key=l.__key,t instanceof E){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof r?t.syncPropertiesAndTextFromYjs(e,null):t instanceof w&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(l.__key,t),l}function x(n,o,s,i){const l=null===i?o instanceof t.Map?Array.from(o.keys()):Object.keys(o.getAttributes()):Array.from(i);let r;for(let i=0;i<l.length;i++){const c=l[i];if(_(c,s,n)){"__state"===c&&(r||(r=s.getWritable()),k(n,o,r));continue}const a=s[c];let f=m(o,c);if(a!==f){if(f instanceof t.Doc){const o=n.docMap;a instanceof t.Doc&&o.delete(a.guid);const s=e.createEditor(),i=f.guid;s._key=i,o.set(i,f),f=s}void 0===r&&(r=s.getWritable()),r[c]=f}}}function m(e,n){return e instanceof t.Map?e.get(n):e.getAttribute(n)}function N(e,n,o){e instanceof t.Map?e.set(n,o):e.setAttribute(n,o)}function k(n,o,s){const i=m(o,"__state");i instanceof t.Map&&e.$getWritableNodeState(s).updateFromJSON(i.toJSON())}function b(e,n,o,s){const i=s.__type,l=e.nodeProperties;let r=l.get(i);void 0===r&&(r=Object.keys(s).filter((t=>!_(t,s,e))),l.set(i,r));const c=e.editor.constructor;!function(e,n,o,s){const i=s.__state,l=m(n,"__state");if(!i)return;const[r,c]=i.getInternalState(),a=o&&o.__state,f=l instanceof t.Map?l:new t.Map;if(a===i)return;const[u,d]=a&&f.doc?a.getInternalState():[void 0,new Map];if(r)for(const[e,t]of Object.entries(r))u&&t!==u[e]&&f.set(e,t);for(const[e,t]of c)d.get(e)!==t&&f.set(e.key,e.unparse(t));l||N(n,"__state",f)}(0,n,o,s);for(let i=0;i<r.length;i++){const l=r[i],a=null===o?void 0:o[l];let f=s[l];if(a!==f){if(f instanceof c){const n=e.docMap;let o;if(a instanceof c){const e=a._key;o=n.get(e),n.delete(e)}const i=o||new t.Doc,l=i.guid;f._key=l,n.set(l,i),f=i,e.editor.update((()=>{s.markDirty()}))}N(n,l,f)}}}function T(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function $(e,t,n){let o=0,s=0;const i=e._children,l=i.length;for(;s<l;s++){const e=i[s],c=o;o+=e.getSize();if((n?o>=t:o>t)&&e instanceof r){let n=t-c-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:c};if(s===l-1)return{length:0,node:null,nodeIndex:s+1,offset:c+1}}return{length:0,node:null,nodeIndex:0,offset:0}}function C(t){const n=t.anchor,o=t.focus;let s=!1;try{const t=n.getNode(),i=o.getNode();(!t.isAttached()||!i.isAttached()||e.$isTextNode(t)&&n.offset>t.getTextContentSize()||e.$isTextNode(i)&&o.offset>i.getTextContentSize())&&(s=!0)}catch(e){s=!0}return s}function S(e){const t=e.getParent();if(null!==t){const n=e.getWritable(),o=t.getWritable(),s=e.getPreviousSibling(),i=e.getNextSibling();if(null===s)if(null!==i){const e=i.getWritable();o.__first=i.__key,e.__prev=null}else o.__first=null;else{const e=s.getWritable();if(null!==i){const t=i.getWritable();t.__prev=e.__key,e.__next=t.__key}else e.__next=null;n.__prev=null}if(null===i)if(null!==s){const e=s.getWritable();o.__last=s.__key,e.__next=null}else o.__last=null;else{const e=i.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 e.$getRoot().selectStart();const s=o.__prev;let i=null;s&&(i=e.$getNodeByKey(s)),null===i&&null!==o.__parent&&(i=e.$getNodeByKey(o.__parent)),null!==i?null!==i&&i.isAttached()?i.selectEnd():v(i.__key,n):e.$getRoot().selectStart()}class w{constructor(e,t,n){this._key="",this._xmlElem=e,this._parent=t,this._type=n}getPrevNode(t){if(null===t)return null;const n=t.get(this._key);return e.$isDecoratorNode(n)?n:null}getNode(){const t=e.$getNodeByKey(this._key);return e.$isDecoratorNode(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);b(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&s(83);x(e,this._xmlElem,n,t)}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function P(e,t,n){const o=new w(e,t,n);return e._collabNode=o,o}class E{constructor(e,t,n){this._key="",this._children=[],this._xmlText=e,this._type=n,this._parent=t}getPrevNode(t){if(null===t)return null;const n=t.get(this._key);return e.$isElementNode(n)?n:null}getNode(){const t=e.$getNodeByKey(this._key);return e.$isElementNode(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&&s(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&s(91),x(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 c=t[l],a=c.insert,f=c.delete;if(null!=c.retain)o+=c.retain;else if("number"==typeof f){let e=f;for(;e>0;){const{node:t,nodeIndex:s,offset:l,length:c}=$(this,o,!1);if(t instanceof E||t instanceof i||t instanceof w)n.splice(s,1),e-=1;else{if(!(t instanceof r))break;{const o=Math.min(e,c),i=0!==s?n[s-1]:null,a=t.getSize();if(0===l&&c===a){n.splice(s,1);const e=T(t._text,l,o-1,"");e.length>0&&(i instanceof r?i._text+=e:this._xmlText.delete(l,e.length))}else t._text=T(t._text,l,o,"");e-=o}}}}else{if(null==a)throw new Error("Unexpected delta format");if("string"==typeof a){const{node:e,offset:t}=$(this,o,!0);e instanceof r?e._text=T(e._text,t,0,a):this._xmlText.delete(t,a.length),o+=a.length}else{const t=a,{node:i,nodeIndex:l,length:c}=$(this,o,!1),f=p(e,t,this);if(i instanceof r&&c>0&&c<i._text.length){const e=i._text,t=e.length-c;i._text=T(e,t,c,""),n.splice(l+1,0,f),s=T(e,0,t,"")}else n.splice(l,0,f);null!==s&&f instanceof r&&(f._text=s+f._text,s=null),o+=1}}}}syncChildrenFromYjs(t){const o=this.getNode();null===o&&s(92);const l=o.__key,c=n.$createChildrenArray(o,null),a=c.length,f=this._children,u=f.length,d=t.collabNodeMap,_=new Set;let h,g,p=0,x=null;u!==a&&(g=o.getWritable());for(let n=0;n<u;n++){const a=c[p],m=f[n],N=m.getNode(),k=m._key;if(null!==N&&a===k){const n=e.$isTextNode(N);if(_.add(a),n)if(m._key=a,m instanceof E){const e=m._xmlText;m.syncPropertiesFromYjs(t,null),m.applyChildrenYjsDelta(t,e.toDelta()),m.syncChildrenFromYjs(t)}else m instanceof r?m.syncPropertiesAndTextFromYjs(t,null):m instanceof w?m.syncPropertiesFromYjs(t,null):m instanceof i||s(93);x=N,p++}else{if(void 0===h){h=new Set;for(let e=0;e<u;e++){const t=f[e]._key;""!==t&&h.add(t)}}if(null!==N&&void 0!==a&&!h.has(a)){S(e.$getNodeByKeyOrThrow(a)),n--,p++;continue}g=o.getWritable();const s=y(t,m,l),i=s.__key;if(d.set(i,m),null===x){const e=g.getFirstChild();if(g.__first=i,null!==e){const t=e.getWritable();t.__prev=i,s.__next=t.__key}}else{const e=x.getWritable(),t=x.getNextSibling();if(e.__next=i,s.__prev=x.__key,null!==t){const e=t.getWritable();e.__prev=i,s.__next=e.__key}}n===u-1&&(g.__last=i),g.__size++,x=s}}for(let n=0;n<a;n++){const o=c[n];if(!_.has(o)){const n=e.$getNodeByKeyOrThrow(o),s=t.collabNodeMap.get(o);void 0!==s&&s.destroy(t),S(n)}}}syncPropertiesFromLexical(e,t,n){b(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(t,n,o,s,i,l){const c=this._children[n],a=e.$getNodeByKeyOrThrow(o);c instanceof E&&e.$isElementNode(a)?(c.syncPropertiesFromLexical(t,a,s),c.syncChildrenFromLexical(t,a,s,i,l)):c instanceof r&&e.$isTextNode(a)?c.syncPropertiesAndTextFromLexical(t,a,s):c instanceof w&&e.$isDecoratorNode(a)&&c.syncPropertiesFromLexical(t,a,s)}syncChildrenFromLexical(t,o,s,i,l){const r=this.getPrevNode(s),c=null===r?[]:n.$createChildrenArray(r,s),a=n.$createChildrenArray(o,null),f=c.length-1,u=a.length-1,d=t.collabNodeMap;let _,g,p=0,y=0;for(;p<=f&&y<=u;){const n=c[p],o=a[y];if(n===o)this._syncChildFromLexical(t,y,o,s,i,l),p++,y++;else{void 0===_&&(_=new Set(c)),void 0===g&&(g=new Set(a));const s=g.has(n),i=_.has(o);if(s){const n=h(t,e.$getNodeByKeyOrThrow(o),this);d.set(o,n),i?(this.splice(t,y,1,n),p++,y++):(this.splice(t,y,0,n),y++)}else this.splice(t,y,1),p++}}const x=p>f,m=y>u;if(x&&!m)for(;y<=u;++y){const n=a[y],o=h(t,e.$getNodeByKeyOrThrow(n),this);this.append(o),d.set(n,o)}else if(m&&!x)for(let e=this._children.length-1;e>=y;e--)this.splice(t,e,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 E)t.insertEmbed(s,e._xmlText);else if(e instanceof r){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof i?t.insertEmbed(s,e._map):e instanceof w&&t.insertEmbed(s,e._xmlElem);this._children.push(e)}splice(e,t,n,o){const l=this._children,c=l[t];if(void 0===c)return void 0===o&&s(94),void this.append(o);const a=c.getOffset();-1===a&&s(95);const f=this._xmlText;if(0!==n&&f.delete(a,c.getSize()),o instanceof E)f.insertEmbed(a,o._xmlText);else if(o instanceof r){const e=o._map;null===e.parent&&f.insertEmbed(a,e),f.insert(a+1,o._text)}else o instanceof i?f.insertEmbed(a,o._map):o instanceof w&&f.insertEmbed(a,o._xmlElem);if(0!==n){const o=l.slice(t,t+n);for(let t=0;t<o.length;t++)o[t].destroy(e)}void 0!==o?l.splice(t,n,o):l.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 M(e,t,n){const o=new E(e,t,n);return e._collabNode=o,o}function O(n,o){const i=o.collabNodeMap.get(n.key);if(void 0===i)return null;let l=n.offset,c=i.getSharedType();if(i instanceof r){c=i._parent._xmlText;const e=i.getOffset();if(-1===e)return null;l=e+1+l}else if(i instanceof E&&"element"===n.type){const t=n.getNode();e.$isElementNode(t)||s(184);let o=0,i=0,r=t.getFirstChild();for(;null!==r&&i++<l;)e.$isTextNode(r)?o+=r.getTextContentSize()+1:o++,r=r.getNextSibling();l=o}return t.createRelativePositionFromTypeIndex(c,l)}function F(e,n){return t.createAbsolutePositionFromRelativePosition(e,n.doc)}function L(e,n){if(null==e){if(null!=n)return!0}else if(null==n||!t.compareRelativePositions(e,n))return!0;return!1}function A(e,t){return{color:t,name:e,selection:null}}function D(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 j(e,t){const n=t.selection;null!==n&&D(e,n)}function z(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 Y(t,n,s,i){const l=t.editor,r=l.getRootElement(),c=t.cursorsContainer;if(null===c||null===r)return;const a=c.offsetParent;if(null===a)return;const f=a.getBoundingClientRect(),u=n.selection;if(null===s)return null===u?void 0:(n.selection=null,void D(t,u));n.selection=s;const d=s.caret,_=s.color,h=s.selections,g=s.anchor,p=s.focus,y=g.key,x=p.key,m=i.get(y),N=i.get(x);if(null==m||null==N)return;let k;if(m===N&&e.$isLineBreakNode(m)){k=[l.getElementByKey(y).getBoundingClientRect()]}else{const e=o.createDOMRange(l,m,g.offset,N,p.offset);if(null===e)return;k=o.createRectsFromDOMRange(l,e)}const b=h.length,T=k.length;for(let e=0;e<T;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),c.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:${_};opacity:0.3;`,e===T-1&&d.parentNode!==n&&n.appendChild(d)}for(let e=b-1;e>=T;e--){const t=h[e];c.removeChild(t),h.pop()}}function K(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,i=0,l=null,r=0;if(null!==n&&null!==o){const t=F(n,e),c=F(o,e);null!==t&&null!==c&&([s,i]=W(t.type,t.index),[l,r]=W(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:i,focusCollabNode:l,focusOffset:r}}function R(t,n){const o=n.awareness.getLocalState();if(null===o)return;const{anchorCollabNode:s,anchorOffset:i,focusCollabNode:l,focusOffset:r}=K(t,o);if(null!==s&&null!==l){const t=s.getKey(),n=l.getKey(),o=e.$getSelection();if(!e.$isRangeSelection(o))return;B(o.anchor,t,i),B(o.focus,n,r)}}function B(t,n,o){if(t.key!==n||t.offset!==o){let s=e.$getNodeByKey(n);if(null!==s&&!e.$isElementNode(s)&&!e.$isTextNode(s)){const e=s.getParentOrThrow();n=e.getKey(),o=s.getIndexWithinParent(),s=e}t.set(n,o,e.$isElementNode(s)?"element":"text")}}function W(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof E){const{node:e,offset:o}=$(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function I(e,t){const n=Array.from(t.awareness.getStates()),o=e.clientID,s=e.cursors,i=e.editor._editorState._nodeMap,l=new Set;for(let t=0;t<n.length;t++){const r=n[t],[c,a]=r;if(c!==o){l.add(c);const{name:t,color:n,focusing:o}=a;let r=null,f=s.get(c);if(void 0===f&&(f=A(t,n),s.set(c,f)),o){const{anchorCollabNode:t,anchorOffset:n,focusCollabNode:o,focusOffset:s}=K(e,a);if(null!==t&&null!==o){const e=t.getKey(),i=o.getKey();if(r=f.selection,null===r)r=z(f,e,n,i,s);else{const t=r.anchor,o=r.focus;t.key=e,t.offset=n,o.key=i,o.offset=s}}}Y(e,f,r,i)}}const r=Array.from(s.keys());for(let t=0;t<r.length;t++){const n=r[t];if(!l.has(n)){const t=s.get(n);void 0!==t&&(j(e,t),s.delete(n))}}}function U(t,n,o,s){const i=n.awareness,l=i.getLocalState();if(null===l)return;const{anchorPos:r,focusPos:c,name:a,color:f,focusing:u,awarenessData:d}=l;let _=null,h=null;(null!==s&&(null===r||s.is(o))||null!==o)&&(e.$isRangeSelection(s)&&(_=O(s.anchor,t),h=O(s.focus,t)),(L(r,_)||L(c,h))&&i.setLocalState({...l,anchorPos:_,awarenessData:d,color:f,focusPos:h,focusing:u,name:a}))}function X(n,o){if(o instanceof t.YMapEvent&&function(n,o){const{target:s}=o;if(!s._item||"__state"!==s._item.parentSub||void 0!==g(s)||!(s.parent instanceof t.XmlText||s.parent instanceof t.XmlElement||s.parent instanceof t.Map))return!1;const i=p(n,s.parent).getNode();if(i){const t=e.$getWritableNodeState(i.getWritable());for(const e of o.keysChanged)t.updateFromUnknown(e,s.get(e))}return!0}(n,o))return;const{target:i}=o,l=p(n,i);if(l instanceof E&&o instanceof t.YTextEvent){const{keysChanged:e,childListChanged:t,delta:s}=o;e.size>0&&l.syncPropertiesFromYjs(n,e),t&&(l.applyChildrenYjsDelta(n,s),l.syncChildrenFromYjs(n))}else if(l instanceof r&&o instanceof t.YMapEvent){const{keysChanged:e}=o;e.size>0&&l.syncPropertiesAndTextFromYjs(n,e)}else if(l instanceof w&&o instanceof t.YXmlEvent){const{attributesChanged:e}=o;e.size>0&&l.syncPropertiesFromYjs(n,e)}else s(82)}const q=e.createCommand("CONNECTED_COMMAND"),G=e.createCommand("TOGGLE_CONNECT_COMMAND");exports.CONNECTED_COMMAND=q,exports.TOGGLE_CONNECT_COMMAND=G,exports.createBinding=function(e,n,o,i,l,r){null==i&&s(81);const c=M(i.get("root",t.XmlText),null,"root");return c._key="root",{clientID:i.clientID,collabNodeMap:new Map,cursors:new Map,cursorsContainer:null,doc:i,docMap:l,editor:e,excludedProperties:r||new Map,id:o,nodeProperties:new Map,root:c}},exports.createUndoManager=function(e,n){return new t.UndoManager(n,{trackedOrigins:new Set([e,null])})},exports.getAnchorAndFocusCollabNodesForUserState=K,exports.initLocalState=function(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})},exports.setLocalStateFocus=function(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)},exports.syncCursorPositions=I,exports.syncLexicalUpdateToYjs=function(t,n,o,s,i,l,c,a){!function(e,t){e.doc.transact(t,e)}(t,(()=>{s.read((()=>{if(a.has("collaboration")||a.has("historic"))return void(c.size>0&&function(t,n){const o=Array.from(n),s=t.collabNodeMap,i=[],l=[];for(let t=0;t<o.length;t++){const n=o[t],c=e.$getNodeByKey(n),a=s.get(n);if(a instanceof r)if(e.$isTextNode(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),l.push(a)}}for(let e=0;e<l.length;e++){const t=l[e],n=t.getKey();s.delete(n);const o=t._parent._children,i=o.indexOf(t);o.splice(i,1)}for(let e=0;e<i.length;e++){const[t,n]=i[e];t._text=n}}(t,c));if(i.has("root")){const n=o._nodeMap,s=e.$getRoot(),r=t.root;r.syncPropertiesFromLexical(t,s,n),r.syncChildrenFromLexical(t,s,n,i,l)}const s=e.$getSelection(),f=o._selection;U(t,n,f,s)}))}))},exports.syncYjsChangesToLexical=function(t,n,o,s,i=I){const l=t.editor,r=l._editorState;o.forEach((e=>e.delta)),l.update((()=>{for(let e=0;e<o.length;e++){const n=o[e];X(t,n)}const s=e.$getSelection();if(e.$isRangeSelection(s))if(C(s)){const o=r._selection;if(e.$isRangeSelection(o)&&(R(t,n),C(s))){v(s.anchor.key,r)}U(t,n,o,e.$getSelection())}else R(t,n)}),{onUpdate:()=>{i(t,n),l.update((()=>{0===e.$getRoot().getChildrenSize()&&e.$getRoot().append(e.$createParagraphNode())}))},skipTransforms:!0,tag:s?"historic":"collaboration"})};