@lexical/yjs
Version:
The library provides Yjs editor bindings for Lexical.
10 lines (8 loc) • 34.2 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{$isDecoratorNode as e,$getNodeByKey as t,$isLineBreakNode as n,$isTextNode as o,$getSelection as s,$isRangeSelection as i,$isElementNode as r,$createChildrenArray as l,$getNodeByKeyOrThrow as c,removeFromParent as a,createEditor as f,$isRootNode as d,$getWritableNodeState as u,$getRoot as h,RootNode as p,ElementNode as g,TextNode as _,$getState as y,createState as m,setDOMStyleObject as x,COLLABORATION_TAG as b,HISTORIC_TAG as T,$addUpdateTag as k,SKIP_SCROLL_INTO_VIEW_TAG as N,$createParagraphNode as S,createCommand as v}from"lexical";import{XmlText as w,XmlElement as C,Map as O,Doc as K,typeListToArraySnapshot as M,Snapshot as E,isDeleted as P,XmlHook as F,ContentString as A,ContentFormat as j,emptySnapshot as z,PermanentUserData as L,iterateDeletedStructs as D,snapshot as Y,createAbsolutePositionFromRelativePosition as I,createRelativePositionFromTypeIndex as $,compareRelativePositions as W,Item as U,YMapEvent as B,YTextEvent as R,YXmlEvent as q,UndoManager as J}from"yjs";import{createDOMRange as G,createRectsFromDOMRange as V}from"@lexical/selection";function H(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.`)}function Q(e,t,n){const o=e.length,s=t.length;let i=0,r=0;for(;i<o&&i<s&&e[i]===t[i]&&i<n;)i++;for(;r+i<o&&r+i<s&&e[o-r-1]===t[s-r-1];)r++;for(;r+i<o&&r+i<s&&e[i]===t[i];)i++;return{index:i,insert:t.slice(i,s-r),remove:o-i-r}}class X{_xmlElem;_key;_parent;_type;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(n)?n:null}getNode(){const n=t(this._key);return e(n)?n: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);ve(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&H(83);Te(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{_map;_key;_parent;_type;constructor(e,t){this._key="",this._map=e,this._parent=t,this._type="linebreak"}getNode(){const e=t(this._key);return n(e)?e: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 te(e,t){const n=new ee(e,t);return e._collabNode=n,n}class ne{_map;_key;_parent;_text;_type;_normalized;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 o(t)?t:null}getNode(){const e=t(this._key);return o(e)?e: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 o=this.getPrevNode(n),r=t.__text;if(ve(e,this._map,o,t),null!==o){const e=o.__text;if(e!==r){!function(e,t,n,o){const r=s();let l=o.length;if(i(r)&&r.isCollapsed()){const e=r.anchor;e.key===t&&(l=e.offset)}const c=Q(n,o,l);e.spliceText(c.index,c.remove,c.insert)}(this,t.__key,e,r),this._text=r}}}syncPropertiesAndTextFromYjs(e,t){const n=this.getNode();null===n&&H(84),Te(e,this._map,n,t);const o=this._text;n.__text!==o&&n.setTextContent(o)}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function oe(e,t,n,o){const s=new ne(e,t,n,o);return e._collabNode=s,s}class se{_key;_children;_xmlText;_type;_parent;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 r(t)?t:null}getNode(){const e=t(this._key);return r(e)?e: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&&H(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&H(91),Te(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 r=t[i],l=r.insert,c=r.delete;if(null!=r.retain)o+=r.retain;else if("number"==typeof c){let e=c;for(;e>0;){const{node:t,nodeIndex:s,offset:i,length:r}=Ce(this,o,!1);if(t instanceof se||t instanceof ee||t instanceof X)n.splice(s,1),e-=1;else{if(!(t instanceof ne))break;{const o=Math.min(e,r),l=0!==s?n[s-1]:null,c=t.getSize();if(0===i&&r===c){n.splice(s,1);const e=we(t._text,i,o-1,"");e.length>0&&(l instanceof ne?l._text+=e:this._xmlText.delete(i,e.length))}else t._text=we(t._text,i,o,"");e-=o}}}}else{if(null==l)throw new Error("Unexpected delta format");if("string"==typeof l){const{node:e,offset:t}=Ce(this,o,!0);e instanceof ne?e._text=we(e._text,t,0,l):this._xmlText.delete(t,l.length),o+=l.length}else{const t=l,{node:i,nodeIndex:r,length:c}=Ce(this,o,!1),a=xe(e,t,this);if(i instanceof ne&&c>0&&c<i._text.length){const e=i._text,t=e.length-c;i._text=we(e,t,c,""),n.splice(r+1,0,a),s=we(e,0,t,"")}else n.splice(r,0,a);null!==s&&a instanceof ne&&(a._text=s+a._text,s=null),o+=1}}}}syncChildrenFromYjs(e){const t=this.getNode();null===t&&H(92);const n=t.__key,s=l(t,null),i=s.length,r=this._children,f=r.length,d=e.collabNodeMap,u=new Set;let h,p,g=0,_=null;f!==i&&(p=t.getWritable());for(let i=0;i<f;i++){const l=s[g],y=r[i],m=y.getNode(),x=y._key;if(null!==m&&l===x){const t=o(m);if(u.add(l),t)if(y._key=l,y instanceof se){const t=y._xmlText;y.syncPropertiesFromYjs(e,null),y.applyChildrenYjsDelta(e,t.toDelta()),y.syncChildrenFromYjs(e)}else y instanceof ne?y.syncPropertiesAndTextFromYjs(e,null):y instanceof X?y.syncPropertiesFromYjs(e,null):y instanceof ee||H(93);_=m,g++}else{if(void 0===h){h=new Set;for(let e=0;e<f;e++){const t=r[e]._key;""!==t&&h.add(t)}}if(null!==m&&void 0!==l&&!h.has(l)){const e=c(l);a(e),i--,g++;continue}p=t.getWritable();const o=be(e,y,n),s=o.__key;if(d.set(s,y),null===_){const e=p.getFirstChild();if(p.__first=s,null!==e){const t=e.getWritable();t.__prev=s,o.__next=t.__key}}else{const e=_.getWritable(),t=_.getNextSibling();if(e.__next=s,o.__prev=_.__key,null!==t){const e=t.getWritable();e.__prev=s,o.__next=e.__key}}i===f-1&&(p.__last=s),p.__size++,_=o}}for(let t=0;t<i;t++){const n=s[t];if(!u.has(n)){const t=c(n),o=e.collabNodeMap.get(n);void 0!==o&&o.destroy(e),a(t)}}}syncPropertiesFromLexical(e,t,n){ve(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(t,n,s,i,l,a){const f=this._children[n],d=c(s);f instanceof se&&r(d)?(f.syncPropertiesFromLexical(t,d,i),f.syncChildrenFromLexical(t,d,i,l,a)):f instanceof ne&&o(d)?f.syncPropertiesAndTextFromLexical(t,d,i):f instanceof X&&e(d)&&f.syncPropertiesFromLexical(t,d,i)}syncChildrenFromLexical(e,t,n,o,s){const i=this.getPrevNode(n),r=null===i?[]:l(i,n),a=l(t,null),f=r.length-1,d=a.length-1,u=e.collabNodeMap;let h,p,g=0,_=0;for(;g<=f&&_<=d;){const t=r[g],i=a[_];if(t===i)this._syncChildFromLexical(e,_,i,n,o,s),g++,_++;else{void 0===h&&(h=new Set(r)),void 0===p&&(p=new Set(a));const n=p.has(t),o=h.has(i);if(n){const t=ye(e,c(i),this);u.set(i,t),o?(this.splice(e,_,1,t),g++,_++):(this.splice(e,_,0,t),_++)}else this.splice(e,_,1),g++}}const y=g>f,m=_>d;if(y&&!m)for(;_<=d;++_){const t=a[_],n=ye(e,c(t),this);this.append(n),u.set(t,n)}else if(m&&!y)for(let t=this._children.length-1;t>=_;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 ne){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof ee?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,i=s[t];if(void 0===i)return void 0===o&&H(94),void this.append(o);const r=i.getOffset();-1===r&&H(95);const l=this._xmlText;if(0!==n&&l.delete(r,i.getSize()),o instanceof se)l.insertEmbed(r,o._xmlText);else if(o instanceof ne){const e=o._map;null===e.parent&&l.insertEmbed(r,e),l.insert(r+1,o._text)}else o instanceof ee?l.insertEmbed(r,o._map):o instanceof X&&l.insertEmbed(r,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}class re{_nodeMap=new Map;_sharedTypeToNodeKeys=new Map;_nodeKeyToSharedType=new Map;set(e,t){const n=t instanceof Array;this.delete(e);const s=n?t:[t];for(const e of s){const t=e.getKey();if(this._nodeKeyToSharedType.has(t)){const e=this._nodeKeyToSharedType.get(t),n=this._sharedTypeToNodeKeys.get(e).indexOf(t);-1!==n&&this._sharedTypeToNodeKeys.get(e).splice(n,1),this._nodeKeyToSharedType.delete(t),this._nodeMap.delete(t)}}if(e instanceof w){if(n||H(331),0===t.length)return;this._sharedTypeToNodeKeys.set(e,t.map(e=>e.getKey()));for(const n of t)this._nodeMap.set(n.getKey(),n),this._nodeKeyToSharedType.set(n.getKey(),e)}else n&&H(332),o(t)&&H(333),this._sharedTypeToNodeKeys.set(e,[t.getKey()]),this._nodeMap.set(t.getKey(),t),this._nodeKeyToSharedType.set(t.getKey(),e)}get(e){const t=this._sharedTypeToNodeKeys.get(e);if(void 0!==t){if(e instanceof w){const e=Array.from(t.map(e=>this._nodeMap.get(e)));return e.length>0?e:void 0}return this._nodeMap.get(t[0])}}getSharedType(e){return this._nodeKeyToSharedType.get(e.getKey())}delete(e){const t=this._sharedTypeToNodeKeys.get(e);if(void 0!==t){for(const e of t)this._nodeMap.delete(e),this._nodeKeyToSharedType.delete(e);this._sharedTypeToNodeKeys.delete(e)}}deleteNode(e){const t=this._nodeKeyToSharedType.get(e);t&&this.delete(t),this._nodeMap.delete(e)}has(e){return this._sharedTypeToNodeKeys.has(e)}clear(){this._nodeMap.clear(),this._sharedTypeToNodeKeys.clear(),this._nodeKeyToSharedType.clear()}}function le(e,t,n,o,s){null==n&&H(81);const i={clientID:n.clientID,cursors:new Map,cursorsContainer:null,doc:n,docMap:o,editor:e,excludedProperties:s||new Map,id:t,nodeProperties:new Map};return function(e){const{editor:t,nodeProperties:n}=e;t.update(()=>{t._nodes.forEach(t=>{const o=new t.klass,s={};for(const[t,n]of Object.entries(o))ge(t,o,e)||(s[t]=n);n.set(o.__type,Object.freeze(s))})})}(i),i}function ce(e,t,n,o,s,i){null==o&&H(81);const r=ie(o.get("root",w),null,"root");return r._key="root",{...le(e,n,o,s,i),collabNodeMap:new Map,root:r}}function ae(e,t,n,o,s={}){null==n&&H(81);const{excludedProperties:i,rootName:r="root-v2"}=s;return{...le(e,t,n,o,i),mapping:new re,root:n.get(r,C)}}function fe(e){return Object.hasOwn(e,"collabNodeMap")}const de=new Set(["__key","__parent","__next","__prev","__state"]),ue=new Set(["__first","__last","__size"]),he=new Set(["__cachedText"]),pe=new Set(["__text"]);function ge(e,t,n){if(de.has(e)||"function"==typeof t[e])return!0;if(o(t)){if(pe.has(e))return!0}else if(r(t)&&(ue.has(e)||d(t)&&he.has(e)))return!0;const s=t.constructor,i=n.excludedProperties.get(s);return null!=i&&i.has(e)}function _e(e,t){const n=e.__type,{nodeProperties:o}=t,s=o.get(n);return void 0===s&&H(330,n),s}function ye(t,s,i){const l=s.__type;let c;if(r(s)){c=ie(new w,i,l),c.syncPropertiesFromLexical(t,s,null),c.syncChildrenFromLexical(t,s,null,null,null)}else if(o(s)){c=oe(new O,s.__text,i,l),c.syncPropertiesAndTextFromLexical(t,s,null)}else if(n(s)){const e=new O;e.set("__type","linebreak"),c=te(e,i)}else if(e(s)){c=Z(new C,i,l),c.syncPropertiesFromLexical(t,s,null)}else H(86);return c._key=s.__key,c}function me(e){const t=ke(e,"__type");return"string"!=typeof t&&void 0!==t&&H(87),t}function xe(e,t,n){const o=t._collabNode;if(void 0===o){const o=e.editor._nodes,s=me(t);"string"!=typeof s&&H(87);void 0===o.get(s)&&H(88,s);const i=t.parent,r=void 0===n&&null!==i?xe(e,i):n||null;if(r instanceof se||H(89),t instanceof w)return ie(t,r,s);if(t instanceof O)return"linebreak"===s?te(t,r):oe(t,"",r,s);if(t instanceof C)return Z(t,r,s)}return o}function be(e,t,n){const o=t.getType(),s=e.editor._nodes.get(o);void 0===s&&H(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 ne?t.syncPropertiesAndTextFromYjs(e,null):t instanceof X&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(i.__key,t),i}function Te(e,t,n,o){const s=null===o?t instanceof O?Array.from(t.keys()):t instanceof w||t instanceof C?Object.keys(t.getAttributes()):Object.keys(t):Array.from(o);let i;for(let o=0;o<s.length;o++){const r=s[o];if(ge(r,n,e)){"__state"===r&&fe(e)&&(i||(i=n.getWritable()),Se(t,i));continue}const l=n[r];let c=ke(t,r);if(l!==c){if(c instanceof K){const t=e.docMap;l instanceof K&&t.delete(l.guid);const n=f(),o=c.guid;n._key=o,t.set(o,c),c=n}void 0===i&&(i=n.getWritable()),i[r]=c}}}function ke(e,t){return e instanceof O?e.get(t):e instanceof w||e instanceof C?e.getAttribute(t):e[t]}function Ne(e,t,n){e instanceof O?e.set(t,n):e.setAttribute(t,n)}function Se(e,t){const n=ke(e,"__state");n instanceof O&&u(t).updateFromJSON(n.toJSON())}function ve(e,t,n,o){const s=Object.keys(_e(o,e)),i=e.editor.constructor;!function(e,t,n,o){const s=o.__state,i=null===t.doc?void 0:ke(t,"__state");if(!s)return;const[r,l]=s.getInternalState(),c=n&&n.__state,a=i instanceof O?i:new O;if(c===s)return;const[f,d]=c&&a.doc?c.getInternalState():[void 0,new Map];if(r)for(const[e,t]of Object.entries(r))f&&t!==f[e]&&a.set(e,t);for(const[e,t]of l)d.get(e)!==t&&a.set(e.key,e.unparse(t));i||Ne(t,"__state",a)}(0,t,n,o);for(let r=0;r<s.length;r++){const l=s[r],c=null===n?void 0:n[l];let a=o[l];if(c!==a){if(a instanceof i){const t=e.docMap;let n;if(c instanceof i){const e=c._key;n=t.get(e),t.delete(e)}const s=n||new K,r=s.guid;a._key=r,t.set(r,s),a=s,e.editor.update(()=>{o.markDirty()})}Ne(t,l,a)}}}function we(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function Ce(e,t,n){let o=0,s=0;const i=e._children,r=i.length;for(;s<r;s++){const e=i[s],l=o;o+=e.getSize();if((n?o>=t:o>t)&&e instanceof ne){let n=t-l-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:l};if(s===r-1)return{length:0,node:null,nodeIndex:s+1,offset:l+1}}return{length:0,node:null,nodeIndex:0,offset:0}}function Oe(e){const t=e.anchor,n=e.focus;let s=!1;try{const e=t.getNode(),i=n.getNode();(!e.isAttached()||!i.isAttached()||o(e)&&t.offset>e.getTextContentSize()||o(i)&&n.offset>i.getTextContentSize())&&(s=!0)}catch(e){s=!0}return s}function Ke(e,t){e.doc.transact(t,e)}function Me(e,n){const o=n._nodeMap.get(e);if(!o)return void h().selectStart();const s=o.__prev;let i=null;s&&(i=t(s)),null===i&&null!==o.__parent&&(i=t(o.__parent)),null!==i?null!==i&&i.isAttached()?i.selectEnd():Me(i.__key,n):h().selectStart()}const Ee=e=>"UNDEFINED"===e.nodeName,Pe=(e,t,n,o,s,i,r)=>{let l=t.mapping.get(e);if(l&&n&&0===n.size&&!o)return l;const c=Ee(e)?p.getType():e.nodeName,a=t.editor._nodes.get(c);if(void 0===a)throw new Error(`$createOrUpdateNodeFromYElement: Node ${c} is not registered`);if(l||(l=new a.klass,n=null,o=!0),o&&l instanceof g){const n=[],o=e=>{if(e instanceof C){const o=Pe(e,t,new Set,!1,s,i,r);null!==o&&n.push(o)}else if(e instanceof w){const o=je(e,t,s,i,r);null!==o&&o.forEach(e=>{null!==e&&n.push(e)})}else H(329)};void 0===s||void 0===i?e.toArray().forEach(o):M(e,new E(i.ds,s.sv)).filter(e=>!e._item.deleted||Ae(e._item,s)||Ae(e._item,i)).forEach(o),Fe(l,n)}const f=e.getAttributes(s);Ee(e)||void 0===s||(Ae(e._item,s)?Ae(e._item,i)||(f[Ge("ychange")]=r?r("added",e._item.id):{type:"added"}):f[Ge("ychange")]=r?r("removed",e._item.id):{type:"removed"});const d={..._e(l,t)},h={};for(const e in f)e.startsWith(Je)?h[Ve(e)]=f[e]:d[e]=f[e];if(Te(t,d,l,n),n){const e=Object.keys(h).filter(e=>n.has(Ge(e)));if(e.length>0){const t=u(l);for(const n of e)t.updateFromUnknown(n,h[n])}}else u(l).updateFromJSON(h);const _=l.getLatest();return t.mapping.set(e,_),_},Fe=(e,t)=>{const n=e.getChildren(),o=new Set(n.map(e=>e.getKey())),s=new Set(t.map(e=>e.getKey())),i=n.length-1,r=t.length-1;let l=0,c=0;for(;l<=i&&c<=r;){const i=n[l].getKey(),r=t[c].getKey();if(i===r){l++,c++;continue}const a=s.has(i),f=o.has(r);if(!a){if(0===c&&1===e.getChildrenSize())return void e.splice(c,1,t.slice(c));e.splice(c,1,[]),l++;continue}const d=t[c];f?(e.splice(c,1,[d]),l++,c++):(e.splice(c,0,[d]),c++)}const a=l>i,f=c>r;a&&!f?e.append(...t.slice(c)):f&&!a&&e.splice(t.length,e.getChildrenSize()-t.length,[])},Ae=(e,t)=>void 0===t?!e.deleted:t.sv.has(e.id.client)&&t.sv.get(e.id.client)>e.id.clock&&!P(t.ds,e.id),je=(e,t,n,s,i)=>{const r=Re(e,n,s,i);let l=t.mapping.get(e)??[];const c=r.map(e=>e.attributes.t??_.getType());if(!(l.length===c.length&&l.every((e,t)=>e.getType()===c[t]))){const e=t.editor._nodes;l=c.map(t=>{const n=e.get(t);if(void 0===n)throw new Error(`$createTextNodesFromYText: Node ${t} is not registered`);const s=new n.klass;if(!o(s))throw new Error(`$createTextNodesFromYText: Node ${t} is not a TextNode`);return s})}for(let e=0;e<r.length;e++){const n=l[e],o=r[e],{attributes:s,insert:i}=o;n.__text!==i&&n.setTextContent(i);const c={..._e(n,t),...s.p},a=Object.fromEntries(Object.entries(s).filter(([e])=>e.startsWith(Je)).map(([e,t])=>[Ve(e),t]));Te(t,c,n,null),u(n).updateFromJSON(a)}const a=l.map(e=>e.getLatest());return t.mapping.set(e,a),a},ze=(e,t)=>e instanceof Array?((e,t)=>{const n=new w;return Be(n,e,t),n})(e,t):((e,t)=>{const n=new C(e.getType()),o={...qe(e,t),...He(e)};for(const e in o){const t=o[e];null!==t&&n.setAttribute(e,t)}return e instanceof g?(n.insert(0,Ye(e).map(e=>ze(e,t))),t.mapping.set(n,e),n):n})(e,t),Le=e=>"object"==typeof e&&null!=e,De=(e,t)=>{const n=Object.keys(e).filter(t=>null!==e[t]);if(null==t)return 0===n.length;let o=n.length===Object.keys(t).filter(e=>null!==t[e]).length;for(let s=0;s<n.length&&o;s++){const i=n[s],r=e[i],l=t[i];o="ychange"===i||r===l||Le(r)&&Le(l)&&De(r,l)}return o},Ye=e=>{if(!(e instanceof g))return[];const t=e.getChildren(),n=[];for(let e=0;e<t.length;e++){const s=t[e];if(o(s)){const s=[];for(let n=t[e];e<t.length&&o(n);n=t[++e])s.push(n);e--,n.push(s)}else n.push(s)}return n},Ie=(e,t,n)=>{const o=Re(e);return o.length===t.length&&o.every((e,o)=>{const s=t[o],i=e.attributes.t??_.getType(),r=e.attributes.p??{},l=Object.fromEntries(Object.entries(e.attributes).filter(([e])=>e.startsWith(Je)));return e.insert===s.getTextContent()&&i===s.getType()&&De(r,qe(s,n))&&De(l,He(s))})},$e=(e,t,n)=>{if(e instanceof C&&!(t instanceof Array)&&Xe(e,t)){const o=Ye(t);return e._length===o.length&&De(e.getAttributes(),{...qe(t,n),...He(t)})&&e.toArray().every((e,t)=>$e(e,o[t],n))}return e instanceof w&&t instanceof Array&&Ie(e,t,n)},We=(e,t)=>e===t||e instanceof Array&&t instanceof Array&&e.length===t.length&&e.every((e,n)=>t[n]===e),Ue=(e,t,n)=>{const o=e.toArray(),s=Ye(t),i=s.length,r=o.length,l=Math.min(r,i);let c=0,a=0,f=!1;for(;c<l;c++){const e=o[c],t=s[c];if(e instanceof F)break;if(We(n.mapping.get(e),t))f=!0;else if(!$e(e,t,n))break}for(;c+a<l;a++){const e=o[r-a-1],t=s[i-a-1];if(e instanceof F)break;if(We(n.mapping.get(e),t))f=!0;else if(!$e(e,t,n))break}return{equalityFactor:c+a,foundMappedChild:f}},Be=(e,t,n)=>{n.mapping.set(e,t);const{nAttrs:o,str:r}=(e=>{let t="",n=e._start;const o={};for(;null!==n;)n.deleted||(n.countable&&n.content instanceof A?t+=n.content.str:n.content instanceof j&&(o[n.content.key]=null)),n=n.right;return{nAttrs:o,str:t}})(e),l=t.map((e,t)=>{const s=e.getType();let i=qe(e,n);return 0===Object.keys(i).length&&(i=null),{attributes:Object.assign({},o,{...s!==_.getType()&&{t:s},p:i,...He(e),...t>0&&{i:t}}),insert:e.getTextContent(),nodeKey:e.getKey()}}),c=l.map(e=>e.insert).join(""),a=s();let f;if(i(a)&&a.isCollapsed()){f=0;for(const e of l){if(e.nodeKey===a.anchor.key){f+=a.anchor.offset;break}f+=e.insert.length}}else f=c.length;const{insert:d,remove:u,index:h}=Q(r,c,f);e.delete(h,u),e.insert(h,d),e.applyDelta(l.map(e=>({attributes:e.attributes,retain:e.insert.length})))},Re=(e,t,n,o)=>e.toDelta(t,n,o).map(e=>{const t=e.attributes??{};return"ychange"in t&&(t[Ge("ychange")]=t.ychange,delete t.ychange),{...e,attributes:t}}),qe=(e,t)=>{const n=_e(e,t),o={};return Object.entries(n).forEach(([t,n])=>{const s=e[t];s!==n&&(o[t]=s)}),o},Je="s_",Ge=e=>`s_${e}`,Ve=e=>{if(!e.startsWith(Je))throw new Error(`Invalid state key: ${e}`);return e.slice(Je.length)},He=e=>{const t=e.__state;if(!t)return{};const[n={},o]=t.getInternalState(),s={};for(const[e,t]of Object.entries(n))s[Ge(e)]=t;for(const[e,t]of o)s[Ge(e.key)]=e.unparse(t);return s},Qe=(e,t,n,o,s)=>{if(t instanceof C&&t.nodeName!==n.getType()&&(!Ee(t)||n.getType()!==p.getType()))throw new Error("node name mismatch!");if(o.mapping.set(t,n),t instanceof C){const e=t.getAttributes(),s={...qe(n,o),...He(n)};for(const n in s)if(null!=s[n]){e[n]===s[n]||Le(e[n])&&Le(s[n])&&De(e[n],s[n])||"ychange"===n||t.setAttribute(n,s[n])}else t.removeAttribute(n);for(const n in e)void 0===s[n]&&t.removeAttribute(n)}const i=Ye(n),r=i.length,l=t.toArray(),c=l.length,a=Math.min(r,c);let f=0,d=0;for(;f<a;f++){const t=l[f],n=i[f];if(t instanceof F)break;if(We(o.mapping.get(t),n))n instanceof g&&s.has(n.getKey())&&Qe(e,t,n,o,s);else{if(!$e(t,n,o))break;o.mapping.set(t,n)}}for(;d+f<a;d++){const t=l[c-d-1],n=i[r-d-1];if(t instanceof F)break;if(We(o.mapping.get(t),n))n instanceof g&&s.has(n.getKey())&&Qe(e,t,n,o,s);else{if(!$e(t,n,o))break;o.mapping.set(t,n)}}for(;c-f-d>0&&r-f-d>0;){const n=l[f],a=i[f],u=l[c-d-1],h=i[r-d-1];if(n instanceof w&&a instanceof Array)Ie(n,a,o)||Be(n,a,o),f+=1;else{let i=n instanceof C&&Xe(n,a),r=u instanceof C&&Xe(u,h);if(i&&r){const e=Ue(n,a,o),t=Ue(u,h,o);e.foundMappedChild&&!t.foundMappedChild?r=!1:!e.foundMappedChild&&t.foundMappedChild||e.equalityFactor<t.equalityFactor?i=!1:r=!1}i?(Qe(e,n,a,o,s),f+=1):r?(Qe(e,u,h,o,s),d+=1):(o.mapping.delete(t.get(f)),t.delete(f,1),t.insert(f,[ze(a,o)]),f+=1)}}const u=c-f-d;if(1===c&&0===r&&l[0]instanceof w?(o.mapping.delete(l[0]),l[0].delete(0,l[0].length)):u>0&&(t.slice(f,f+u).forEach(e=>o.mapping.delete(e)),t.delete(f,u)),f+d<r){const e=[];for(let t=f;t<r-d;t++)e.push(ze(i[t],o));t.insert(f,e)}},Xe=(e,t)=>!(t instanceof Array)&&e.nodeName===t.getType(),Ze=m("ychange",{isEqual:(e,t)=>e===t,parse:e=>e??null});function et(e){return y(e,Ze)}const tt=(e,t=Y(e.doc),n=z)=>{const{doc:o}=e;o.gc&&H(325),o.transact(s=>{const i=new L(o);i&&i.dss.forEach(e=>{D(s,e,e=>{})});const r=(e,t)=>({id:t,type:e,user:("added"===e?i.getUserByClientId(t.client):i.getUserByDeletedId(t))??null});e.mapping.clear(),e.editor.update(()=>{h().clear(),Pe(e.root,e,null,!0,t,n,r)})},e)};function nt(e,t){const n=t.collabNodeMap.get(e.key);if(void 0===n)return null;let s=e.offset,i=n.getSharedType();if(n instanceof ne){i=n._parent._xmlText;const e=n.getOffset();if(-1===e)return null;s=e+1+s}else if(n instanceof se&&"element"===e.type){const t=e.getNode();r(t)||H(184);let n=0,i=0,l=t.getFirstChild();for(;null!==l&&i++<s;)o(l)?n+=l.getTextContentSize()+1:n++,l=l.getNextSibling();s=n}return $(i,s)}function ot(e,t){const{mapping:n}=t,{offset:s}=e,i=e.getNode(),l=n.getSharedType(i);if(void 0===l)return null;if("text"===e.type){o(i)||H(326);let e=i.getPreviousSibling(),t=s;for(;o(e);)t+=e.getTextContentSize(),e=e.getPreviousSibling();return $(l,t)}if("element"===e.type){r(i)||H(184);let e=0,t=i.getFirstChild();for(;null!==t&&e<s;){if(o(t)){let e=t.getNextSibling();for(;o(e);)e=e.getNextSibling()}e++,t=t.getNextSibling()}return $(l,e)}return null}function st(e,t){return I(e,t.doc)}function it(e,t){if(null==e){if(null!=t)return!0}else if(null==t||!W(e,t))return!0;return!1}function rt(e,t){return{color:t,name:e,selection:null}}function lt(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 ct(e,t){const n=t.selection;null!==n&<(e,n)}function at(e,t,n,o,s,i={}){const r=e.color,l=document.createElement("span");i.cursor?(l.className=i.cursor,x(l.style,{"--lexical-cursor-color":r,bottom:"0",position:"absolute",right:"-1px",top:"0"})):x(l.style,{"background-color":r,bottom:"0",position:"absolute",right:"-1px",top:"0",width:"1px","z-index":"10"});const c=document.createElement("span");return c.textContent=e.name,i.cursorName?c.className=i.cursorName:x(c.style,{"background-color":r,color:"#fff","font-family":"Arial","font-size":"12px","font-weight":"bold",left:"-2px","line-height":"12px",padding:"2px",position:"absolute",top:"-16px","white-space":"nowrap"}),l.appendChild(c),{anchor:{key:t,offset:n},caret:l,color:r,focus:{key:o,offset:s},name:c,selections:[]}}function ft(e,t,o,s,i={}){const r=e.editor,l=r.getRootElement(),c=e.cursorsContainer;if(null===c||null===l)return;const a=c.offsetParent;if(null===a)return;const f=a.getBoundingClientRect(),d=t.selection;if(null===o)return null===d?void 0:(t.selection=null,void lt(e,d));t.selection=o;const u=o.caret,h=o.color,p=o.selections,g=o.anchor,_=o.focus,y=g.key,m=_.key,b=s.get(y),T=s.get(m);if(null==b||null==T)return;let k;if(b===T&&n(b)){k=[r.getElementByKey(y).getBoundingClientRect()]}else{const e=G(r,b,g.offset,T,_.offset);if(null===e)return;k=V(r,e)}const N=p.length,S=k.length;for(let e=0;e<S;e++){const t=k[e];let n=p[e];if(void 0===n){n=document.createElement("span"),p[e]=n;const t=document.createElement("span");i.selectionBg&&(t.className=i.selectionBg),n.appendChild(t),c.appendChild(n)}const o=t.top-f.top,s=t.left-f.left,r={height:`${t.height}px`,left:`${s}px`,"pointer-events":"none",position:"absolute",top:`${o}px`,width:`${t.width}px`};i.selection?(n.className=i.selection,x(n.style,{...r,"--lexical-cursor-color":h}),x(n.firstChild.style,{height:"100%",left:"0",position:"absolute",top:"0",width:"100%"})):(x(n.style,r),x(n.firstChild.style,{...r,"background-color":h,left:"0",opacity:"0.3",top:"0","z-index":"5"})),e===S-1&&u.parentNode!==n&&n.appendChild(u)}for(let e=N-1;e>=S;e--){const t=p[e];c.removeChild(t),p.pop()}}function dt(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,i=0,r=null,l=0;if(null!==n&&null!==o){const t=st(n,e),c=st(o,e);null!==t&&null!==c&&([s,i]=gt(t.type,t.index),[r,l]=gt(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:i,focusCollabNode:r,focusOffset:l}}function ut(e,t){const{anchorPos:n,focusPos:s}=t,i=n?st(n,e):null,r=s?st(s,e):null;if(null===i||null===r)return{anchorKey:null,anchorOffset:0,focusKey:null,focusOffset:0};if(fe(e)){const[e,t]=gt(i.type,i.index),[n,o]=gt(r.type,r.index);return{anchorKey:null!==e?e.getKey():null,anchorOffset:t,focusKey:null!==n?n.getKey():null,focusOffset:o}}let[l,c]=_t(e.mapping,i),[a,f]=_t(e.mapping,r);if(a&&l&&(a!==l||f!==c)){const e=a.isBefore(l),t=e?a:l,n=e?f:c;o(t)&&o(t.getNextSibling())&&n===t.getTextContentSize()&&(e?(a=t.getNextSibling(),f=0):(l=t.getNextSibling(),c=0))}return{anchorKey:null!==l?l.getKey():null,anchorOffset:c,focusKey:null!==a?a.getKey():null,focusOffset:f}}function ht(e,t){const n=t.awareness.getLocalState();if(null===n)return;const{anchorKey:o,anchorOffset:r,focusKey:l,focusOffset:c}=ut(e,n);if(null!==o&&null!==l){const e=s();if(!i(e))return;pt(e.anchor,o,r),pt(e.focus,l,c)}}function pt(e,n,s){if(e.key!==n||e.offset!==s){let i=t(n);if(null!==i&&!r(i)&&!o(i)){const e=i.getParentOrThrow();n=e.getKey(),s=i.getIndexWithinParent(),i=e}e.set(n,s,r(i)?"element":"text")}}function gt(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof se){const{node:e,offset:o}=Ce(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function _t(e,t){const n=t.type,s=t.index;if(n instanceof C){const t=e.get(n);if(void 0===t)return[null,0];if(!r(t))return[t,s];let i=s,l=0;const c=t.getChildren();for(;i>0&&l<c.length;){const e=c[l];if(i-=1,l+=1,o(e))for(;l<c.length&&o(c[l]);)l+=1}return[t,l]}{const t=e.get(n);if(void 0===t)return[null,0];let o=0,i=s;for(;i>t[o].getTextContentSize()&&o+1<t.length;)i-=t[o].getTextContentSize(),o++;const r=t[o];return[r,Math.min(i,r.getTextContentSize())]}}function yt(e,t){return t.awareness.getStates()}function mt(e,t,n){const{getAwarenessStates:o=yt}=n??{},s=Array.from(o(e,t)),i=e.clientID,r=e.cursors,l=e.editor,c=l._config.theme.collaboration,a=l._editorState._nodeMap,f=new Set;for(let t=0;t<s.length;t++){const n=s[t],[o,d]=n;if(0!==o&&o!==i){f.add(o);const{name:t,color:n,focusing:s}=d;let i=null,u=r.get(o);if(void 0===u&&(u=rt(t,n),r.set(o,u)),s){const{anchorKey:t,anchorOffset:n,focusKey:o,focusOffset:s}=l.read(()=>ut(e,d));if(null!==t&&null!==o)if(i=u.selection,null===i)i=at(u,t,n,o,s,c);else{const e=i.anchor,r=i.focus;e.key=t,e.offset=n,r.key=o,r.offset=s}}ft(e,u,i,a,c)}}const d=Array.from(r.keys());for(let t=0;t<d.length;t++){const n=d[t];if(!f.has(n)){const t=r.get(n);void 0!==t&&(ct(e,t),r.delete(n))}}}function xt(e,t,n,o){const s=t.awareness,r=s.getLocalState();if(null===r)return;const{anchorPos:l,focusPos:c,name:a,color:f,focusing:d,awarenessData:u}=r;let h=null,p=null;(null!==o&&(null===l||o.is(n))||null!==n)&&(i(o)&&(fe(e)?(h=nt(o.anchor,e),p=nt(o.focus,e)):(h=ot(o.anchor,e),p=ot(o.focus,e))),(it(l,h)||it(c,p))&&s.setLocalState({...r,anchorPos:h,awarenessData:u,color:f,focusPos:p,focusing:d,name:a}))}function bt(e,t){if(t instanceof B&&function(e,t){const{target:n}=t;if(!n._item||"__state"!==n._item.parentSub||void 0!==me(n)||!(n.parent instanceof w||n.parent instanceof C||n.parent instanceof O))return!1;const o=xe(e,n.parent).getNode();if(o){const e=u(o.getWritable());for(const o of t.keysChanged)e.updateFromUnknown(o,n.get(o))}return!0}(e,t))return;const{target:n}=t,o=xe(e,n);if(o instanceof se&&t instanceof R){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 ne&&t instanceof B){const{keysChanged:n}=t;n.size>0&&o.syncPropertiesAndTextFromYjs(e,n)}else if(o instanceof X&&t instanceof q){const{attributesChanged:n}=t;n.size>0&&o.syncPropertiesFromYjs(e,n)}else H(82)}function Tt(e,t,n,o,s=mt){const i=e.editor,r=i._editorState;n.forEach(e=>e.delta),i.update(()=>{for(let t=0;t<n.length;t++){const o=n[t];bt(e,o)}kt(r,e,t),o||k(N)},{onUpdate:()=>{s(e,t),i.update(()=>Nt())},skipTransforms:!0,tag:o?T:b})}function kt(e,t,n){const o=s();if(i(o))if(Oe(o)){const r=e._selection;if(i(r)&&(ht(t,n),Oe(o))){Me(o.anchor.key,e)}xt(t,n,r,s())}else ht(t,n)}function Nt(){0===h().getChildrenSize()&&h().append(S())}function St(e,n,i,r,l,c,a,f){Ke(e,()=>{r.read(()=>{if(f.has(b)||f.has(T))return void(a.size>0&&function(e,n){const s=Array.from(n),i=e.collabNodeMap,r=[],l=[];for(let e=0;e<s.length;e++){const n=s[e],c=t(n),a=i.get(n);if(a instanceof ne)if(o(c))r.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();i.delete(n);const o=t._parent._children,s=o.indexOf(t);o.splice(s,1)}for(let e=0;e<r.length;e++){const[t,n]=r[e];t._text=n}}(e,a));if(l.has("root")){const t=i._nodeMap,n=h(),o=e.root;o.syncPropertiesFromLexical(e,n,t),o.syncChildrenFromLexical(e,n,t,l,c)}const r=s(),d=i._selection;xt(e,n,d,r)})})}function vt(e,t){const{target:n}=t;if(n instanceof C&&t instanceof q)Pe(n,e,t.attributesChanged,t.childListChanged);else if(n instanceof w&&t instanceof R){const t=n.parent;t instanceof C?Pe(t,e,new Set,!0):H(327)}else H(328)}function wt(e,t,n,o,s){const i=e.editor,r=i._editorState;D(o,o.deleteSet,t=>{if(t.constructor===U){const n=t.content.type;n&&e.mapping.delete(n)}}),n.forEach(e=>e.delta),i.update(()=>{for(let t=0;t<n.length;t++){const o=n[t];vt(e,o)}kt(r,e,t),s||k(N)},{discrete:!0,onUpdate:()=>{mt(e,t),i.update(()=>Nt())},skipTransforms:!0,tag:s?T:b})}function Ct(e,t){e.mapping.clear();const n=e.editor;n.update(()=>{h().clear(),Pe(e.root,e,null,!0),k(b)},{discrete:!0,onUpdate:()=>{mt(e,t),n.update(()=>Nt())},skipTransforms:!0,tag:b})}function Ot(e,t,n,o,i,r,l){(l.has(b)||l.has(T))&&0===r.size||(r.forEach(t=>{e.mapping.deleteNode(t)}),Ke(e,()=>{o.read(()=>{i.has("root")&&Qe(e.doc,e.root,h(),e,new Set(i.keys()));const o=s(),r=n._selection;xt(e,t,r,o)})}))}const Kt=v("CONNECTED_COMMAND"),Mt=v("TOGGLE_CONNECT_COMMAND"),Et=v("DIFF_VERSIONS_COMMAND"),Pt=v("CLEAR_DIFF_VERSIONS_COMMAND");function Ft(e,t){return new J(t,{trackedOrigins:new Set([e,null])})}function At(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})}function jt(e,t,n,o,s){const{awareness:i}=e;let r=i.getLocalState();null===r&&(r={anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t}),r.focusing=o,i.setLocalState(r)}export{et as $getYChangeState,Pt as CLEAR_DIFF_VERSIONS_COMMAND__EXPERIMENTAL,Kt as CONNECTED_COMMAND,Et as DIFF_VERSIONS_COMMAND__EXPERIMENTAL,Mt as TOGGLE_CONNECT_COMMAND,ce as createBinding,ae as createBindingV2__EXPERIMENTAL,Ft as createUndoManager,dt as getAnchorAndFocusCollabNodesForUserState,At as initLocalState,tt as renderSnapshot__EXPERIMENTAL,jt as setLocalStateFocus,mt as syncCursorPositions,St as syncLexicalUpdateToYjs,Ot as syncLexicalUpdateToYjsV2__EXPERIMENTAL,Tt as syncYjsChangesToLexical,wt as syncYjsChangesToLexicalV2__EXPERIMENTAL,Ct as syncYjsStateToLexicalV2__EXPERIMENTAL};