UNPKG

@lexical/yjs

Version:

The library provides Yjs editor bindings for Lexical.

10 lines (8 loc) 21.1 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. * */ "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(k(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","__dir"]),d=new Set(["__cachedText"]),u=new Set(["__text"]);function h(t,n,o){if(a.has(t)||"function"==typeof n[t])return!0;if(e.$isTextNode(n)){if(u.has(t))return!0}else if(e.$isElementNode(n)&&(f.has(t)||e.$isRootNode(n)&&d.has(t)))return!0;const s=n.constructor,i=o.excludedProperties.get(s);return null!=i&&i.has(t)}function _(n,o,i){const r=o.__type;let a;if(e.$isElementNode(o)){a=O(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=w(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 P||s(89),n instanceof t.XmlText)return O(n,f,r);if(n instanceof t.Map)return"linebreak"===r?l(n,f):c(n,"",f,r);if(n instanceof t.XmlElement)return w(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 P){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 v&&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(h(c,s,n)){"__state"===c&&(r||(r=s.getWritable()),T(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 T(n,o,s){const i=m(o,"__state");i instanceof t.Map&&e.$getWritableNodeState(s).updateFromJSON(i.toJSON())}function k(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=>!h(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[d,u]=a&&f.doc?a.getInternalState():[void 0,new Map];if(r)for(const[e,t]of Object.entries(r))d&&t!==d[e]&&f.set(e,t);for(const[e,t]of c)u.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 C(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 S(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 b(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():b(i.__key,n):e.$getRoot().selectStart()}class v{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);k(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 w(e,t,n){const o=new v(e,t,n);return e._collabNode=o,o}class P{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 P||t instanceof i||t instanceof v)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=C(t._text,l,o-1,"");e.length>0&&(i instanceof r?i._text+=e:this._xmlText.delete(l,e.length))}else t._text=C(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=C(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=C(e,t,c,""),n.splice(l+1,0,f),s=C(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,d=f.length,u=t.collabNodeMap,h=new Set;let _,g,p=0,x=null;d!==a&&(g=o.getWritable());for(let n=0;n<d;n++){const a=c[p],m=f[n],N=m.getNode(),T=m._key;if(null!==N&&a===T){const n=e.$isTextNode(N);if(h.add(a),n)if(m._key=a,m instanceof P){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 v?m.syncPropertiesFromYjs(t,null):m instanceof i||s(93);x=N,p++}else{if(void 0===_){_=new Set;for(let e=0;e<d;e++){const t=f[e]._key;""!==t&&_.add(t)}}if(null!==N&&void 0!==a&&!_.has(a)){const t=e.$getNodeByKeyOrThrow(a);e.removeFromParent(t),n--,p++;continue}g=o.getWritable();const s=y(t,m,l),i=s.__key;if(u.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===d-1&&(g.__last=i),g.__size++,x=s}}for(let n=0;n<a;n++){const o=c[n];if(!h.has(o)){const n=e.$getNodeByKeyOrThrow(o),s=t.collabNodeMap.get(o);void 0!==s&&s.destroy(t),e.removeFromParent(n)}}}syncPropertiesFromLexical(e,t,n){k(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 P&&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 v&&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,d=a.length-1,u=t.collabNodeMap;let h,g,p=0,y=0;for(;p<=f&&y<=d;){const n=c[p],o=a[y];if(n===o)this._syncChildFromLexical(t,y,o,s,i,l),p++,y++;else{void 0===h&&(h=new Set(c)),void 0===g&&(g=new Set(a));const s=g.has(n),i=h.has(o);if(s){const n=_(t,e.$getNodeByKeyOrThrow(o),this);u.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>d;if(x&&!m)for(;y<=d;++y){const n=a[y],o=_(t,e.$getNodeByKeyOrThrow(n),this);this.append(o),u.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 P)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 v&&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 P)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 v&&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 O(e,t,n){const o=new P(e,t,n);return e._collabNode=o,o}function E(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 P&&"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 M(e,n){return t.createAbsolutePositionFromRelativePosition(e,n.doc)}function F(e,n){if(null==e){if(null!=n)return!0}else if(null==n||!t.compareRelativePositions(e,n))return!0;return!1}function L(e,t){return{color:t,name:e,selection:null}}function A(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 D(e,t){const n=t.selection;null!==n&&A(e,n)}function R(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 j(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(),d=n.selection;if(null===s)return null===d?void 0:(n.selection=null,void A(t,d));n.selection=s;const u=s.caret,h=s.color,_=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 T;if(m===N&&e.$isLineBreakNode(m)){T=[l.getElementByKey(y).getBoundingClientRect()]}else{const e=o.createDOMRange(l,m,g.offset,N,p.offset);if(null===e)return;T=o.createRectsFromDOMRange(l,e)}const k=_.length,C=T.length;for(let e=0;e<C;e++){const t=T[e];let n=_[e];if(void 0===n){n=document.createElement("span"),_[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:${h};opacity:0.3;`,e===C-1&&u.parentNode!==n&&n.appendChild(u)}for(let e=k-1;e>=C;e--){const t=_[e];c.removeChild(t),_.pop()}}function Y(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,i=0,l=null,r=0;if(null!==n&&null!==o){const t=M(n,e),c=M(o,e);null!==t&&null!==c&&([s,i]=B(t.type,t.index),[l,r]=B(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:i,focusCollabNode:l,focusOffset:r}}function z(t,n){const o=n.awareness.getLocalState();if(null===o)return;const{anchorCollabNode:s,anchorOffset:i,focusCollabNode:l,focusOffset:r}=Y(t,o);if(null!==s&&null!==l){const t=s.getKey(),n=l.getKey(),o=e.$getSelection();if(!e.$isRangeSelection(o))return;K(o.anchor,t,i),K(o.focus,n,r)}}function K(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 B(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof P){const{node:e,offset:o}=$(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function I(e,t){return t.awareness.getStates()}function W(e,t,n){const{getAwarenessStates:o=I}=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,a]=n;if(o!==i){c.add(o);const{name:t,color:n,focusing:s}=a;let i=null,f=l.get(o);if(void 0===f&&(f=L(t,n),l.set(o,f)),s){const{anchorCollabNode:t,anchorOffset:n,focusCollabNode:o,focusOffset:s}=Y(e,a);if(null!==t&&null!==o){const e=t.getKey(),l=o.getKey();if(i=f.selection,null===i)i=R(f,e,n,l,s);else{const t=i.anchor,o=i.focus;t.key=e,t.offset=n,o.key=l,o.offset=s}}}j(e,f,i,r)}}const a=Array.from(l.keys());for(let t=0;t<a.length;t++){const n=a[t];if(!c.has(n)){const t=l.get(n);void 0!==t&&(D(e,t),l.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:d,awarenessData:u}=l;let h=null,_=null;(null!==s&&(null===r||s.is(o))||null!==o)&&(e.$isRangeSelection(s)&&(h=E(s.anchor,t),_=E(s.focus,t)),(F(r,h)||F(c,_))&&i.setLocalState({...l,anchorPos:h,awarenessData:u,color:f,focusPos:_,focusing:d,name:a}))}function G(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 P&&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 v&&o instanceof t.YXmlEvent){const{attributesChanged:e}=o;e.size>0&&l.syncPropertiesFromYjs(n,e)}else s(82)}const X=e.createCommand("CONNECTED_COMMAND"),q=e.createCommand("TOGGLE_CONNECT_COMMAND");exports.CONNECTED_COMMAND=X,exports.TOGGLE_CONNECT_COMMAND=q,exports.createBinding=function(e,n,o,i,l,r){null==i&&s(81);const c=O(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=Y,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=W,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(e.COLLABORATION_TAG)||a.has(e.HISTORIC_TAG))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=W){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];G(t,n)}const i=e.$getSelection();if(e.$isRangeSelection(i))if(S(i)){const o=r._selection;if(e.$isRangeSelection(o)&&(z(t,n),S(i))){b(i.anchor.key,r)}U(t,n,o,e.$getSelection())}else z(t,n);s||e.$addUpdateTag(e.SKIP_SCROLL_INTO_VIEW_TAG)}),{onUpdate:()=>{i(t,n),l.update((()=>{0===e.$getRoot().getChildrenSize()&&e.$getRoot().append(e.$createParagraphNode())}))},skipTransforms:!0,tag:s?e.HISTORIC_TAG:e.COLLABORATION_TAG})};