diff --git a/LexicalYjs.dev.js b/LexicalYjs.dev.js index c5d65670f41e4aa31497c8db8cb13648cbd5cef5..3ef350f2c9205c5d3a73c173da9bf9bdf362c3a8 100644 --- a/LexicalYjs.dev.js +++ b/LexicalYjs.dev.js @@ -1061,7 +1061,8 @@ function $syncNodeStateToLexical(sharedType, lexicalNode) { } function syncNodeStateFromLexical(binding, sharedType, prevLexicalNode, nextLexicalNode) { const nextState = nextLexicalNode.__state; - const existingState = sharedTypeGet(sharedType, '__state'); + // Backport facebook/lexical#8347: detached Yjs types cannot contain existing state. + const existingState = sharedType.doc === null ? undefined : sharedTypeGet(sharedType, '__state'); if (!nextState) { return; } diff --git a/LexicalYjs.dev.mjs b/LexicalYjs.dev.mjs index 90ee3d329d301cbf905eea0c3a3c5ceb165d7a0a..1c0732a6ef805deffdce311e5a8dacf0a08d131a 100644 --- a/LexicalYjs.dev.mjs +++ b/LexicalYjs.dev.mjs @@ -1059,7 +1059,8 @@ function $syncNodeStateToLexical(sharedType, lexicalNode) { } function syncNodeStateFromLexical(binding, sharedType, prevLexicalNode, nextLexicalNode) { const nextState = nextLexicalNode.__state; - const existingState = sharedTypeGet(sharedType, '__state'); + // Backport facebook/lexical#8347: detached Yjs types cannot contain existing state. + const existingState = sharedType.doc === null ? undefined : sharedTypeGet(sharedType, '__state'); if (!nextState) { return; } diff --git a/LexicalYjs.prod.js b/LexicalYjs.prod.js index 59778f151ea28a57a16ca3a2a7e7d360f033969c..2ee35349778ce96cf45ccf6bd02756bf9715df0e 100644 --- a/LexicalYjs.prod.js +++ b/LexicalYjs.prod.js @@ -9 +9 @@ -"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.`)}function i(e,t,n){const o=e.length,s=t.length;let i=0,r=0;for(;i0;){const{node:t,nodeIndex:s,offset:i,length:l}=K(this,o,!1);if(t instanceof u||t instanceof c||t instanceof r)n.splice(s,1),e-=1;else{if(!(t instanceof f))break;{const o=Math.min(e,l),r=0!==s?n[s-1]:null,c=t.getSize();if(0===i&&l===c){n.splice(s,1);const e=M(t._text,i,o-1,"");e.length>0&&(r instanceof f?r._text+=e:this._xmlText.delete(i,e.length))}else t._text=M(t._text,i,o,"");e-=o}}}}else{if(null==a)throw new Error("Unexpected delta format");if("string"==typeof a){const{node:e,offset:t}=K(this,o,!0);e instanceof f?e._text=M(e._text,t,0,a):this._xmlText.delete(t,a.length),o+=a.length}else{const t=a,{node:i,nodeIndex:r,length:l}=K(this,o,!1),c=C(e,t,this);if(i instanceof f&&l>0&&lf,m=_>d;if(y&&!m)for(;_<=d;++_){const n=a[_],o=E(t,e.$getNodeByKeyOrThrow(n),this);this.append(o),u.set(n,o)}else if(m&&!y)for(let e=this._children.length-1;e>=_;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 u)t.insertEmbed(s,e._xmlText);else if(e instanceof f){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof c?t.insertEmbed(s,e._map):e instanceof r&&t.insertEmbed(s,e._xmlElem);this._children.push(e)}splice(e,t,n,o){const i=this._children,l=i[t];if(void 0===l)return void 0===o&&s(94),void this.append(o);const a=l.getOffset();-1===a&&s(95);const d=this._xmlText;if(0!==n&&d.delete(a,l.getSize()),o instanceof u)d.insertEmbed(a,o._xmlText);else if(o instanceof f){const e=o._map;null===e.parent&&d.insertEmbed(a,e),d.insert(a+1,o._text)}else o instanceof c?d.insertEmbed(a,o._map):o instanceof r&&d.insertEmbed(a,o._xmlElem);if(0!==n){const o=i.slice(t,t+n);for(let t=0;te.getKey()));for(const e of o)this._nodeMap.set(e.getKey(),e),this._nodeKeyToSharedType.set(e.getKey(),n)}else i&&s(332),e.$isTextNode(o)&&s(333),this._sharedTypeToNodeKeys.set(n,[o.getKey()]),this._nodeMap.set(o.getKey(),o),this._nodeKeyToSharedType.set(o.getKey(),n)}get(e){const n=this._sharedTypeToNodeKeys.get(e);if(void 0!==n){if(e instanceof t.XmlText){const e=Array.from(n.map(e=>this._nodeMap.get(e)));return e.length>0?e:void 0}return this._nodeMap.get(n[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 g(e,t,n,o,i){null==n&&s(81);const r={clientID:n.clientID,cursors:new Map,cursorsContainer:null,doc:n,docMap:o,editor:e,excludedProperties:i||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))N(t,o,e)||(s[t]=n);n.set(o.__type,Object.freeze(s))})})}(r),r}function _(e){return Object.hasOwn(e,"collabNodeMap")}const y=new Set(["__key","__parent","__next","__prev","__state"]),m=new Set(["__first","__last","__size"]),x=new Set(["__cachedText"]),T=new Set(["__text"]);function N(t,n,o){if(y.has(t)||"function"==typeof n[t])return!0;if(e.$isTextNode(n)){if(T.has(t))return!0}else if(e.$isElementNode(n)&&(m.has(t)||e.$isRootNode(n)&&x.has(t)))return!0;const s=n.constructor,i=o.excludedProperties.get(s);return null!=i&&i.has(t)}function S(e,t){const n=e.__type,{nodeProperties:o}=t,i=o.get(n);return void 0===i&&s(330,n),i}function E(n,o,i){const r=o.__type;let c;if(e.$isElementNode(o)){c=h(new t.XmlText,i,r),c.syncPropertiesFromLexical(n,o,null),c.syncChildrenFromLexical(n,o,null,null,null)}else if(e.$isTextNode(o)){c=d(new t.Map,o.__text,i,r),c.syncPropertiesAndTextFromLexical(n,o,null)}else if(e.$isLineBreakNode(o)){const e=new t.Map;e.set("__type","linebreak"),c=a(e,i)}else if(e.$isDecoratorNode(o)){c=l(new t.XmlElement,i,r),c.syncPropertiesFromLexical(n,o,null)}else s(86);return c._key=o.__key,c}function b(e){const t=O(e,"__type");return"string"!=typeof t&&void 0!==t&&s(87),t}function C(e,n,o){const i=n._collabNode;if(void 0===i){const i=e.editor._nodes,r=b(n);"string"!=typeof r&&s(87);void 0===i.get(r)&&s(88,r);const c=n.parent,f=void 0===o&&null!==c?C(e,c):o||null;if(f instanceof u||s(89),n instanceof t.XmlText)return h(n,f,r);if(n instanceof t.Map)return"linebreak"===r?a(n,f):d(n,"",f,r);if(n instanceof t.XmlElement)return l(n,f,r)}return i}function k(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 u){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof f?t.syncPropertiesAndTextFromYjs(e,null):t instanceof r&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(l.__key,t),l}function $(n,o,s,i){const r=null===i?o instanceof t.Map?Array.from(o.keys()):o instanceof t.XmlText||o instanceof t.XmlElement?Object.keys(o.getAttributes()):Object.keys(o):Array.from(i);let l;for(let i=0;i{s.markDirty()})}v(n,c,f)}}}function M(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function K(e,t,n){let o=0,s=0;const i=e._children,r=i.length;for(;s=t:o>t)&&e instanceof f){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 P(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 L(e,t){e.doc.transact(t,e)}function F(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():F(i.__key,n):e.$getRoot().selectStart()}const I=e=>"UNDEFINED"===e.nodeName,R=(n,o,i,r,l,c,a)=>{let f=o.mapping.get(n);if(f&&i&&0===i.size&&!r)return f;const d=I(n)?e.RootNode.getType():n.nodeName,u=o.editor._nodes.get(d);if(void 0===u)throw new Error(`$createOrUpdateNodeFromYElement: Node ${d} is not registered`);if(f||(f=new u.klass,i=null,r=!0),r&&f instanceof e.ElementNode){const e=[],i=n=>{if(n instanceof t.XmlElement){const t=R(n,o,new Set,!1,l,c,a);null!==t&&e.push(t)}else if(n instanceof t.XmlText){const t=X(n,o,l,c,a);null!==t&&t.forEach(t=>{null!==t&&e.push(t)})}else s(329)};void 0===l||void 0===c?n.toArray().forEach(i):t.typeListToArraySnapshot(n,new t.Snapshot(c.ds,l.sv)).filter(e=>!e._item.deleted||D(e._item,l)||D(e._item,c)).forEach(i),j(f,e)}const h=n.getAttributes(l);I(n)||void 0===l||(D(n._item,l)?D(n._item,c)||(h[ee("ychange")]=a?a("added",n._item.id):{type:"added"}):h[ee("ychange")]=a?a("removed",n._item.id):{type:"removed"});const p={...S(f,o)},g={};for(const e in h)e.startsWith(Z)?g[te(e)]=h[e]:p[e]=h[e];if($(o,p,f,i),i){const t=Object.keys(g).filter(e=>i.has(ee(e)));if(t.length>0){const n=e.$getWritableNodeState(f);for(const e of t)n.updateFromUnknown(e,g[e])}}else e.$getWritableNodeState(f).updateFromJSON(g);const _=f.getLatest();return o.mapping.set(n,_),_},j=(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,[])},D=(e,n)=>void 0===n?!e.deleted:n.sv.has(e.id.client)&&n.sv.get(e.id.client)>e.id.clock&&!t.isDeleted(n.ds,e.id),X=(t,n,o,s,i)=>{const r=J(t,o,s,i);let l=n.mapping.get(t)??[];const c=r.map(t=>t.attributes.t??e.TextNode.getType());if(!(l.length===c.length&&l.every((e,t)=>e.getType()===c[t]))){const t=n.editor._nodes;l=c.map(n=>{const o=t.get(n);if(void 0===o)throw new Error(`$createTextNodesFromYText: Node ${n} is not registered`);const s=new o.klass;if(!e.$isTextNode(s))throw new Error(`$createTextNodesFromYText: Node ${n} is not a TextNode`);return s})}for(let t=0;te.startsWith(Z)).map(([e,t])=>[te(e),t]));$(n,a,o,null),e.$getWritableNodeState(o).updateFromJSON(f)}const a=l.map(e=>e.getLatest());return n.mapping.set(t,a),a},z=(n,o)=>n instanceof Array?((e,n)=>{const o=new t.XmlText;return H(o,e,n),o})(n,o):((n,o)=>{const s=new t.XmlElement(n.getType()),i={...Q(n,o),...ne(n)};for(const e in i){const t=i[e];null!==t&&s.setAttribute(e,t)}return n instanceof e.ElementNode?(s.insert(0,U(n).map(e=>z(e,o))),o.mapping.set(s,n),s):s})(n,o),Y=e=>"object"==typeof e&&null!=e,B=(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{if(!(t instanceof e.ElementNode))return[];const n=t.getChildren(),o=[];for(let t=0;t{const s=J(t);return s.length===n.length&&s.every((t,s)=>{const i=n[s],r=t.attributes.t??e.TextNode.getType(),l=t.attributes.p??{},c=Object.fromEntries(Object.entries(t.attributes).filter(([e])=>e.startsWith(Z)));return t.insert===i.getTextContent()&&r===i.getType()&&B(l,Q(i,o))&&B(c,ne(i))})},G=(e,n,o)=>{if(e instanceof t.XmlElement&&!(n instanceof Array)&&se(e,n)){const t=U(n);return e._length===t.length&&B(e.getAttributes(),{...Q(n,o),...ne(n)})&&e.toArray().every((e,n)=>G(e,t[n],o))}return e instanceof t.XmlText&&n instanceof Array&&W(e,n,o)},V=(e,t)=>e===t||e instanceof Array&&t instanceof Array&&e.length===t.length&&e.every((e,n)=>t[n]===e),q=(e,n,o)=>{const s=e.toArray(),i=U(n),r=i.length,l=s.length,c=Math.min(l,r);let a=0,f=0,d=!1;for(;a{s.mapping.set(n,o);const{nAttrs:r,str:l}=(e=>{let n="",o=e._start;const s={};for(;null!==o;)o.deleted||(o.countable&&o.content instanceof t.ContentString?n+=o.content.str:o.content instanceof t.ContentFormat&&(s[o.content.key]=null)),o=o.right;return{nAttrs:s,str:n}})(n),c=o.map((t,n)=>{const o=t.getType();let i=Q(t,s);return 0===Object.keys(i).length&&(i=null),{attributes:Object.assign({},r,{...o!==e.TextNode.getType()&&{t:o},p:i,...ne(t),...n>0&&{i:n}}),insert:t.getTextContent(),nodeKey:t.getKey()}}),a=c.map(e=>e.insert).join(""),f=e.$getSelection();let d;if(e.$isRangeSelection(f)&&f.isCollapsed()){d=0;for(const e of c){if(e.nodeKey===f.anchor.key){d+=f.anchor.offset;break}d+=e.insert.length}}else d=a.length;const{insert:u,remove:h,index:p}=i(l,a,d);n.delete(p,h),n.insert(p,u),n.applyDelta(c.map(e=>({attributes:e.attributes,retain:e.insert.length})))},J=(e,t,n,o)=>e.toDelta(t,n,o).map(e=>{const t=e.attributes??{};return"ychange"in t&&(t[ee("ychange")]=t.ychange,delete t.ychange),{...e,attributes:t}}),Q=(e,t)=>{const n=S(e,t),o={};return Object.entries(n).forEach(([t,n])=>{const s=e[t];s!==n&&(o[t]=s)}),o},Z="s_",ee=e=>`s_${e}`,te=e=>{if(!e.startsWith(Z))throw new Error(`Invalid state key: ${e}`);return e.slice(Z.length)},ne=e=>{const t=e.__state;if(!t)return{};const[n={},o]=t.getInternalState(),s={};for(const[e,t]of Object.entries(n))s[ee(e)]=t;for(const[e,t]of o)s[ee(e.key)]=e.unparse(t);return s},oe=(n,o,s,i,r)=>{if(o instanceof t.XmlElement&&o.nodeName!==s.getType()&&(!I(o)||s.getType()!==e.RootNode.getType()))throw new Error("node name mismatch!");if(i.mapping.set(o,s),o instanceof t.XmlElement){const e=o.getAttributes(),t={...Q(s,i),...ne(s)};for(const n in t)if(null!=t[n]){e[n]===t[n]||Y(e[n])&&Y(t[n])&&B(e[n],t[n])||"ychange"===n||o.setAttribute(n,t[n])}else o.removeAttribute(n);for(const n in e)void 0===t[n]&&o.removeAttribute(n)}const l=U(s),c=l.length,a=o.toArray(),f=a.length,d=Math.min(c,f);let u=0,h=0;for(;u0&&c-u-h>0;){const e=a[u],s=l[u],d=a[f-h-1],p=l[c-h-1];if(e instanceof t.XmlText&&s instanceof Array)W(e,s,i)||H(e,s,i),u+=1;else{let l=e instanceof t.XmlElement&&se(e,s),c=d instanceof t.XmlElement&&se(d,p);if(l&&c){const t=q(e,s,i),n=q(d,p,i);t.foundMappedChild&&!n.foundMappedChild?c=!1:!t.foundMappedChild&&n.foundMappedChild||t.equalityFactor0&&(o.slice(u,u+p).forEach(e=>i.mapping.delete(e)),o.delete(u,p)),u+h!(t instanceof Array)&&e.nodeName===t.getType(),ie=e.createState("ychange",{isEqual:(e,t)=>e===t,parse:e=>e??null});function re(n,o){const i=o.collabNodeMap.get(n.key);if(void 0===i)return null;let r=n.offset,l=i.getSharedType();if(i instanceof f){l=i._parent._xmlText;const e=i.getOffset();if(-1===e)return null;r=e+1+r}else if(i instanceof u&&"element"===n.type){const t=n.getNode();e.$isElementNode(t)||s(184);let o=0,i=0,l=t.getFirstChild();for(;null!==l&&i++=E;e--){const t=p[e];c.removeChild(t),p.pop()}}function ge(t,n){const{anchorPos:o,focusPos:s}=n,i=o?ce(o,t):null,r=s?ce(s,t):null;if(null===i||null===r)return{anchorKey:null,anchorOffset:0,focusKey:null,focusOffset:0};if(_(t)){const[e,t]=me(i.type,i.index),[n,o]=me(r.type,r.index);return{anchorKey:null!==e?e.getKey():null,anchorOffset:t,focusKey:null!==n?n.getKey():null,focusOffset:o}}let[l,c]=xe(t.mapping,i),[a,f]=xe(t.mapping,r);if(a&&l&&(a!==l||f!==c)){const t=a.isBefore(l),n=t?a:l,o=t?f:c;e.$isTextNode(n)&&e.$isTextNode(n.getNextSibling())&&o===n.getTextContentSize()&&(t?(a=n.getNextSibling(),f=0):(l=n.getNextSibling(),c=0))}return{anchorKey:null!==l?l.getKey():null,anchorOffset:c,focusKey:null!==a?a.getKey():null,focusOffset:f}}function _e(t,n){const o=n.awareness.getLocalState();if(null===o)return;const{anchorKey:s,anchorOffset:i,focusKey:r,focusOffset:l}=ge(t,o);if(null!==s&&null!==r){const t=e.$getSelection();if(!e.$isRangeSelection(t))return;ye(t.anchor,s,i),ye(t.focus,r,l)}}function ye(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 me(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof u){const{node:e,offset:o}=K(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function xe(n,o){const s=o.type,i=o.index;if(s instanceof t.XmlElement){const t=n.get(s);if(void 0===t)return[null,0];if(!e.$isElementNode(t))return[t,i];let o=i,r=0;const l=t.getChildren();for(;o>0&&re[t].getTextContentSize()&&t+1ge(e,f));if(null!==t&&null!==o)if(i=d.selection,null===i)i=he(d,t,n,o,s);else{const e=i.anchor,r=i.focus;e.key=t,e.offset=n,r.key=o,r.offset=s}}pe(e,d,i,c)}}const f=Array.from(r.keys());for(let t=0;t0&&l.syncPropertiesFromYjs(n,e),t&&(l.applyChildrenYjsDelta(n,s),l.syncChildrenFromYjs(n))}else if(l instanceof f&&o instanceof t.YMapEvent){const{keysChanged:e}=o;e.size>0&&l.syncPropertiesAndTextFromYjs(n,e)}else if(l instanceof r&&o instanceof t.YXmlEvent){const{attributesChanged:e}=o;e.size>0&&l.syncPropertiesFromYjs(n,e)}else s(82)}function be(t,n,o){const s=e.$getSelection();if(e.$isRangeSelection(s))if(P(s)){const i=t._selection;if(e.$isRangeSelection(i)&&(_e(n,o),P(s))){F(s.anchor.key,t)}Se(n,o,i,e.$getSelection())}else _e(n,o)}function Ce(){0===e.$getRoot().getChildrenSize()&&e.$getRoot().append(e.$createParagraphNode())}function ke(e,n){const{target:o}=n;if(o instanceof t.XmlElement&&n instanceof t.YXmlEvent)R(o,e,n.attributesChanged,n.childListChanged);else if(o instanceof t.XmlText&&n instanceof t.YTextEvent){const n=o.parent;n instanceof t.XmlElement?R(n,e,new Set,!0):s(327)}else s(328)}const $e=e.createCommand("CONNECTED_COMMAND"),Oe=e.createCommand("TOGGLE_CONNECT_COMMAND"),ve=e.createCommand("DIFF_VERSIONS_COMMAND"),Ae=e.createCommand("CLEAR_DIFF_VERSIONS_COMMAND");exports.$getYChangeState=function(t){return e.$getState(t,ie)},exports.CLEAR_DIFF_VERSIONS_COMMAND__EXPERIMENTAL=Ae,exports.CONNECTED_COMMAND=$e,exports.DIFF_VERSIONS_COMMAND__EXPERIMENTAL=ve,exports.TOGGLE_CONNECT_COMMAND=Oe,exports.createBinding=function(e,n,o,i,r,l){null==i&&s(81);const c=h(i.get("root",t.XmlText),null,"root");return c._key="root",{...g(e,o,i,r,l),collabNodeMap:new Map,root:c}},exports.createBindingV2__EXPERIMENTAL=function(e,n,o,i,r={}){null==o&&s(81);const{excludedProperties:l,rootName:c="root-v2"}=r;return{...g(e,n,o,i,l),mapping:new p,root:o.get(c,t.XmlElement)}},exports.createUndoManager=function(e,n){return new t.UndoManager(n,{trackedOrigins:new Set([e,null])})},exports.getAnchorAndFocusCollabNodesForUserState=function(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,i=0,r=null,l=0;if(null!==n&&null!==o){const t=ce(n,e),c=ce(o,e);null!==t&&null!==c&&([s,i]=me(t.type,t.index),[r,l]=me(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:i,focusCollabNode:r,focusOffset:l}},exports.initLocalState=function(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})},exports.renderSnapshot__EXPERIMENTAL=(n,o=t.snapshot(n.doc),i=t.emptySnapshot)=>{const{doc:r}=n;r.gc&&s(325),r.transact(s=>{const l=new t.PermanentUserData(r);l&&l.dss.forEach(e=>{t.iterateDeletedStructs(s,e,e=>{})});const c=(e,t)=>({id:t,type:e,user:("added"===e?l.getUserByClientId(t.client):l.getUserByDeletedId(t))??null});n.mapping.clear(),n.editor.update(()=>{e.$getRoot().clear(),R(n.root,n,null,!0,o,i,c)})},n)},exports.setLocalStateFocus=function(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)},exports.syncCursorPositions=Ne,exports.syncLexicalUpdateToYjs=function(t,n,o,s,i,r,l,c){L(t,()=>{s.read(()=>{if(c.has(e.COLLABORATION_TAG)||c.has(e.HISTORIC_TAG))return void(l.size>0&&function(t,n){const o=Array.from(n),s=t.collabNodeMap,i=[],r=[];for(let t=0;t{t.mapping.deleteNode(e)}),L(t,()=>{s.read(()=>{i.has("root")&&oe(t.doc,t.root,e.$getRoot(),t,new Set(i.keys()));const s=e.$getSelection(),r=o._selection;Se(t,n,r,s)})}))},exports.syncYjsChangesToLexical=function(t,n,o,s,i=Ne){const r=t.editor,l=r._editorState;o.forEach(e=>e.delta),r.update(()=>{for(let e=0;e{i(t,n),r.update(()=>Ce())},skipTransforms:!0,tag:s?e.HISTORIC_TAG:e.COLLABORATION_TAG})},exports.syncYjsChangesToLexicalV2__EXPERIMENTAL=function(n,o,s,i,r){const l=n.editor,c=l._editorState;t.iterateDeletedStructs(i,i.deleteSet,e=>{if(e.constructor===t.Item){const t=e.content.type;t&&n.mapping.delete(t)}}),s.forEach(e=>e.delta),l.update(()=>{for(let e=0;e{Ne(n,o),l.update(()=>Ce())},skipTransforms:!0,tag:r?e.HISTORIC_TAG:e.COLLABORATION_TAG})},exports.syncYjsStateToLexicalV2__EXPERIMENTAL=function(t,n){t.mapping.clear();const o=t.editor;o.update(()=>{e.$getRoot().clear(),R(t.root,t,null,!0),e.$addUpdateTag(e.COLLABORATION_TAG)},{discrete:!0,onUpdate:()=>{Ne(t,n),o.update(()=>Ce())},skipTransforms:!0,tag:e.COLLABORATION_TAG})}; +"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.`)}function i(e,t,n){const o=e.length,s=t.length;let i=0,r=0;for(;i0;){const{node:t,nodeIndex:s,offset:i,length:l}=K(this,o,!1);if(t instanceof u||t instanceof c||t instanceof r)n.splice(s,1),e-=1;else{if(!(t instanceof f))break;{const o=Math.min(e,l),r=0!==s?n[s-1]:null,c=t.getSize();if(0===i&&l===c){n.splice(s,1);const e=M(t._text,i,o-1,"");e.length>0&&(r instanceof f?r._text+=e:this._xmlText.delete(i,e.length))}else t._text=M(t._text,i,o,"");e-=o}}}}else{if(null==a)throw new Error("Unexpected delta format");if("string"==typeof a){const{node:e,offset:t}=K(this,o,!0);e instanceof f?e._text=M(e._text,t,0,a):this._xmlText.delete(t,a.length),o+=a.length}else{const t=a,{node:i,nodeIndex:r,length:l}=K(this,o,!1),c=C(e,t,this);if(i instanceof f&&l>0&&lf,m=_>d;if(y&&!m)for(;_<=d;++_){const n=a[_],o=E(t,e.$getNodeByKeyOrThrow(n),this);this.append(o),u.set(n,o)}else if(m&&!y)for(let e=this._children.length-1;e>=_;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 u)t.insertEmbed(s,e._xmlText);else if(e instanceof f){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof c?t.insertEmbed(s,e._map):e instanceof r&&t.insertEmbed(s,e._xmlElem);this._children.push(e)}splice(e,t,n,o){const i=this._children,l=i[t];if(void 0===l)return void 0===o&&s(94),void this.append(o);const a=l.getOffset();-1===a&&s(95);const d=this._xmlText;if(0!==n&&d.delete(a,l.getSize()),o instanceof u)d.insertEmbed(a,o._xmlText);else if(o instanceof f){const e=o._map;null===e.parent&&d.insertEmbed(a,e),d.insert(a+1,o._text)}else o instanceof c?d.insertEmbed(a,o._map):o instanceof r&&d.insertEmbed(a,o._xmlElem);if(0!==n){const o=i.slice(t,t+n);for(let t=0;te.getKey()));for(const e of o)this._nodeMap.set(e.getKey(),e),this._nodeKeyToSharedType.set(e.getKey(),n)}else i&&s(332),e.$isTextNode(o)&&s(333),this._sharedTypeToNodeKeys.set(n,[o.getKey()]),this._nodeMap.set(o.getKey(),o),this._nodeKeyToSharedType.set(o.getKey(),n)}get(e){const n=this._sharedTypeToNodeKeys.get(e);if(void 0!==n){if(e instanceof t.XmlText){const e=Array.from(n.map(e=>this._nodeMap.get(e)));return e.length>0?e:void 0}return this._nodeMap.get(n[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 g(e,t,n,o,i){null==n&&s(81);const r={clientID:n.clientID,cursors:new Map,cursorsContainer:null,doc:n,docMap:o,editor:e,excludedProperties:i||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))N(t,o,e)||(s[t]=n);n.set(o.__type,Object.freeze(s))})})}(r),r}function _(e){return Object.hasOwn(e,"collabNodeMap")}const y=new Set(["__key","__parent","__next","__prev","__state"]),m=new Set(["__first","__last","__size"]),x=new Set(["__cachedText"]),T=new Set(["__text"]);function N(t,n,o){if(y.has(t)||"function"==typeof n[t])return!0;if(e.$isTextNode(n)){if(T.has(t))return!0}else if(e.$isElementNode(n)&&(m.has(t)||e.$isRootNode(n)&&x.has(t)))return!0;const s=n.constructor,i=o.excludedProperties.get(s);return null!=i&&i.has(t)}function S(e,t){const n=e.__type,{nodeProperties:o}=t,i=o.get(n);return void 0===i&&s(330,n),i}function E(n,o,i){const r=o.__type;let c;if(e.$isElementNode(o)){c=h(new t.XmlText,i,r),c.syncPropertiesFromLexical(n,o,null),c.syncChildrenFromLexical(n,o,null,null,null)}else if(e.$isTextNode(o)){c=d(new t.Map,o.__text,i,r),c.syncPropertiesAndTextFromLexical(n,o,null)}else if(e.$isLineBreakNode(o)){const e=new t.Map;e.set("__type","linebreak"),c=a(e,i)}else if(e.$isDecoratorNode(o)){c=l(new t.XmlElement,i,r),c.syncPropertiesFromLexical(n,o,null)}else s(86);return c._key=o.__key,c}function b(e){const t=O(e,"__type");return"string"!=typeof t&&void 0!==t&&s(87),t}function C(e,n,o){const i=n._collabNode;if(void 0===i){const i=e.editor._nodes,r=b(n);"string"!=typeof r&&s(87);void 0===i.get(r)&&s(88,r);const c=n.parent,f=void 0===o&&null!==c?C(e,c):o||null;if(f instanceof u||s(89),n instanceof t.XmlText)return h(n,f,r);if(n instanceof t.Map)return"linebreak"===r?a(n,f):d(n,"",f,r);if(n instanceof t.XmlElement)return l(n,f,r)}return i}function k(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 u){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof f?t.syncPropertiesAndTextFromYjs(e,null):t instanceof r&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(l.__key,t),l}function $(n,o,s,i){const r=null===i?o instanceof t.Map?Array.from(o.keys()):o instanceof t.XmlText||o instanceof t.XmlElement?Object.keys(o.getAttributes()):Object.keys(o):Array.from(i);let l;for(let i=0;i{s.markDirty()})}v(n,c,f)}}}function M(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function K(e,t,n){let o=0,s=0;const i=e._children,r=i.length;for(;s=t:o>t)&&e instanceof f){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 P(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 L(e,t){e.doc.transact(t,e)}function F(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():F(i.__key,n):e.$getRoot().selectStart()}const I=e=>"UNDEFINED"===e.nodeName,R=(n,o,i,r,l,c,a)=>{let f=o.mapping.get(n);if(f&&i&&0===i.size&&!r)return f;const d=I(n)?e.RootNode.getType():n.nodeName,u=o.editor._nodes.get(d);if(void 0===u)throw new Error(`$createOrUpdateNodeFromYElement: Node ${d} is not registered`);if(f||(f=new u.klass,i=null,r=!0),r&&f instanceof e.ElementNode){const e=[],i=n=>{if(n instanceof t.XmlElement){const t=R(n,o,new Set,!1,l,c,a);null!==t&&e.push(t)}else if(n instanceof t.XmlText){const t=X(n,o,l,c,a);null!==t&&t.forEach(t=>{null!==t&&e.push(t)})}else s(329)};void 0===l||void 0===c?n.toArray().forEach(i):t.typeListToArraySnapshot(n,new t.Snapshot(c.ds,l.sv)).filter(e=>!e._item.deleted||D(e._item,l)||D(e._item,c)).forEach(i),j(f,e)}const h=n.getAttributes(l);I(n)||void 0===l||(D(n._item,l)?D(n._item,c)||(h[ee("ychange")]=a?a("added",n._item.id):{type:"added"}):h[ee("ychange")]=a?a("removed",n._item.id):{type:"removed"});const p={...S(f,o)},g={};for(const e in h)e.startsWith(Z)?g[te(e)]=h[e]:p[e]=h[e];if($(o,p,f,i),i){const t=Object.keys(g).filter(e=>i.has(ee(e)));if(t.length>0){const n=e.$getWritableNodeState(f);for(const e of t)n.updateFromUnknown(e,g[e])}}else e.$getWritableNodeState(f).updateFromJSON(g);const _=f.getLatest();return o.mapping.set(n,_),_},j=(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,[])},D=(e,n)=>void 0===n?!e.deleted:n.sv.has(e.id.client)&&n.sv.get(e.id.client)>e.id.clock&&!t.isDeleted(n.ds,e.id),X=(t,n,o,s,i)=>{const r=J(t,o,s,i);let l=n.mapping.get(t)??[];const c=r.map(t=>t.attributes.t??e.TextNode.getType());if(!(l.length===c.length&&l.every((e,t)=>e.getType()===c[t]))){const t=n.editor._nodes;l=c.map(n=>{const o=t.get(n);if(void 0===o)throw new Error(`$createTextNodesFromYText: Node ${n} is not registered`);const s=new o.klass;if(!e.$isTextNode(s))throw new Error(`$createTextNodesFromYText: Node ${n} is not a TextNode`);return s})}for(let t=0;te.startsWith(Z)).map(([e,t])=>[te(e),t]));$(n,a,o,null),e.$getWritableNodeState(o).updateFromJSON(f)}const a=l.map(e=>e.getLatest());return n.mapping.set(t,a),a},z=(n,o)=>n instanceof Array?((e,n)=>{const o=new t.XmlText;return H(o,e,n),o})(n,o):((n,o)=>{const s=new t.XmlElement(n.getType()),i={...Q(n,o),...ne(n)};for(const e in i){const t=i[e];null!==t&&s.setAttribute(e,t)}return n instanceof e.ElementNode?(s.insert(0,U(n).map(e=>z(e,o))),o.mapping.set(s,n),s):s})(n,o),Y=e=>"object"==typeof e&&null!=e,B=(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{if(!(t instanceof e.ElementNode))return[];const n=t.getChildren(),o=[];for(let t=0;t{const s=J(t);return s.length===n.length&&s.every((t,s)=>{const i=n[s],r=t.attributes.t??e.TextNode.getType(),l=t.attributes.p??{},c=Object.fromEntries(Object.entries(t.attributes).filter(([e])=>e.startsWith(Z)));return t.insert===i.getTextContent()&&r===i.getType()&&B(l,Q(i,o))&&B(c,ne(i))})},G=(e,n,o)=>{if(e instanceof t.XmlElement&&!(n instanceof Array)&&se(e,n)){const t=U(n);return e._length===t.length&&B(e.getAttributes(),{...Q(n,o),...ne(n)})&&e.toArray().every((e,n)=>G(e,t[n],o))}return e instanceof t.XmlText&&n instanceof Array&&W(e,n,o)},V=(e,t)=>e===t||e instanceof Array&&t instanceof Array&&e.length===t.length&&e.every((e,n)=>t[n]===e),q=(e,n,o)=>{const s=e.toArray(),i=U(n),r=i.length,l=s.length,c=Math.min(l,r);let a=0,f=0,d=!1;for(;a{s.mapping.set(n,o);const{nAttrs:r,str:l}=(e=>{let n="",o=e._start;const s={};for(;null!==o;)o.deleted||(o.countable&&o.content instanceof t.ContentString?n+=o.content.str:o.content instanceof t.ContentFormat&&(s[o.content.key]=null)),o=o.right;return{nAttrs:s,str:n}})(n),c=o.map((t,n)=>{const o=t.getType();let i=Q(t,s);return 0===Object.keys(i).length&&(i=null),{attributes:Object.assign({},r,{...o!==e.TextNode.getType()&&{t:o},p:i,...ne(t),...n>0&&{i:n}}),insert:t.getTextContent(),nodeKey:t.getKey()}}),a=c.map(e=>e.insert).join(""),f=e.$getSelection();let d;if(e.$isRangeSelection(f)&&f.isCollapsed()){d=0;for(const e of c){if(e.nodeKey===f.anchor.key){d+=f.anchor.offset;break}d+=e.insert.length}}else d=a.length;const{insert:u,remove:h,index:p}=i(l,a,d);n.delete(p,h),n.insert(p,u),n.applyDelta(c.map(e=>({attributes:e.attributes,retain:e.insert.length})))},J=(e,t,n,o)=>e.toDelta(t,n,o).map(e=>{const t=e.attributes??{};return"ychange"in t&&(t[ee("ychange")]=t.ychange,delete t.ychange),{...e,attributes:t}}),Q=(e,t)=>{const n=S(e,t),o={};return Object.entries(n).forEach(([t,n])=>{const s=e[t];s!==n&&(o[t]=s)}),o},Z="s_",ee=e=>`s_${e}`,te=e=>{if(!e.startsWith(Z))throw new Error(`Invalid state key: ${e}`);return e.slice(Z.length)},ne=e=>{const t=e.__state;if(!t)return{};const[n={},o]=t.getInternalState(),s={};for(const[e,t]of Object.entries(n))s[ee(e)]=t;for(const[e,t]of o)s[ee(e.key)]=e.unparse(t);return s},oe=(n,o,s,i,r)=>{if(o instanceof t.XmlElement&&o.nodeName!==s.getType()&&(!I(o)||s.getType()!==e.RootNode.getType()))throw new Error("node name mismatch!");if(i.mapping.set(o,s),o instanceof t.XmlElement){const e=o.getAttributes(),t={...Q(s,i),...ne(s)};for(const n in t)if(null!=t[n]){e[n]===t[n]||Y(e[n])&&Y(t[n])&&B(e[n],t[n])||"ychange"===n||o.setAttribute(n,t[n])}else o.removeAttribute(n);for(const n in e)void 0===t[n]&&o.removeAttribute(n)}const l=U(s),c=l.length,a=o.toArray(),f=a.length,d=Math.min(c,f);let u=0,h=0;for(;u0&&c-u-h>0;){const e=a[u],s=l[u],d=a[f-h-1],p=l[c-h-1];if(e instanceof t.XmlText&&s instanceof Array)W(e,s,i)||H(e,s,i),u+=1;else{let l=e instanceof t.XmlElement&&se(e,s),c=d instanceof t.XmlElement&&se(d,p);if(l&&c){const t=q(e,s,i),n=q(d,p,i);t.foundMappedChild&&!n.foundMappedChild?c=!1:!t.foundMappedChild&&n.foundMappedChild||t.equalityFactor0&&(o.slice(u,u+p).forEach(e=>i.mapping.delete(e)),o.delete(u,p)),u+h!(t instanceof Array)&&e.nodeName===t.getType(),ie=e.createState("ychange",{isEqual:(e,t)=>e===t,parse:e=>e??null});function re(n,o){const i=o.collabNodeMap.get(n.key);if(void 0===i)return null;let r=n.offset,l=i.getSharedType();if(i instanceof f){l=i._parent._xmlText;const e=i.getOffset();if(-1===e)return null;r=e+1+r}else if(i instanceof u&&"element"===n.type){const t=n.getNode();e.$isElementNode(t)||s(184);let o=0,i=0,l=t.getFirstChild();for(;null!==l&&i++=E;e--){const t=p[e];c.removeChild(t),p.pop()}}function ge(t,n){const{anchorPos:o,focusPos:s}=n,i=o?ce(o,t):null,r=s?ce(s,t):null;if(null===i||null===r)return{anchorKey:null,anchorOffset:0,focusKey:null,focusOffset:0};if(_(t)){const[e,t]=me(i.type,i.index),[n,o]=me(r.type,r.index);return{anchorKey:null!==e?e.getKey():null,anchorOffset:t,focusKey:null!==n?n.getKey():null,focusOffset:o}}let[l,c]=xe(t.mapping,i),[a,f]=xe(t.mapping,r);if(a&&l&&(a!==l||f!==c)){const t=a.isBefore(l),n=t?a:l,o=t?f:c;e.$isTextNode(n)&&e.$isTextNode(n.getNextSibling())&&o===n.getTextContentSize()&&(t?(a=n.getNextSibling(),f=0):(l=n.getNextSibling(),c=0))}return{anchorKey:null!==l?l.getKey():null,anchorOffset:c,focusKey:null!==a?a.getKey():null,focusOffset:f}}function _e(t,n){const o=n.awareness.getLocalState();if(null===o)return;const{anchorKey:s,anchorOffset:i,focusKey:r,focusOffset:l}=ge(t,o);if(null!==s&&null!==r){const t=e.$getSelection();if(!e.$isRangeSelection(t))return;ye(t.anchor,s,i),ye(t.focus,r,l)}}function ye(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 me(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof u){const{node:e,offset:o}=K(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function xe(n,o){const s=o.type,i=o.index;if(s instanceof t.XmlElement){const t=n.get(s);if(void 0===t)return[null,0];if(!e.$isElementNode(t))return[t,i];let o=i,r=0;const l=t.getChildren();for(;o>0&&re[t].getTextContentSize()&&t+1ge(e,f));if(null!==t&&null!==o)if(i=d.selection,null===i)i=he(d,t,n,o,s);else{const e=i.anchor,r=i.focus;e.key=t,e.offset=n,r.key=o,r.offset=s}}pe(e,d,i,c)}}const f=Array.from(r.keys());for(let t=0;t0&&l.syncPropertiesFromYjs(n,e),t&&(l.applyChildrenYjsDelta(n,s),l.syncChildrenFromYjs(n))}else if(l instanceof f&&o instanceof t.YMapEvent){const{keysChanged:e}=o;e.size>0&&l.syncPropertiesAndTextFromYjs(n,e)}else if(l instanceof r&&o instanceof t.YXmlEvent){const{attributesChanged:e}=o;e.size>0&&l.syncPropertiesFromYjs(n,e)}else s(82)}function be(t,n,o){const s=e.$getSelection();if(e.$isRangeSelection(s))if(P(s)){const i=t._selection;if(e.$isRangeSelection(i)&&(_e(n,o),P(s))){F(s.anchor.key,t)}Se(n,o,i,e.$getSelection())}else _e(n,o)}function Ce(){0===e.$getRoot().getChildrenSize()&&e.$getRoot().append(e.$createParagraphNode())}function ke(e,n){const{target:o}=n;if(o instanceof t.XmlElement&&n instanceof t.YXmlEvent)R(o,e,n.attributesChanged,n.childListChanged);else if(o instanceof t.XmlText&&n instanceof t.YTextEvent){const n=o.parent;n instanceof t.XmlElement?R(n,e,new Set,!0):s(327)}else s(328)}const $e=e.createCommand("CONNECTED_COMMAND"),Oe=e.createCommand("TOGGLE_CONNECT_COMMAND"),ve=e.createCommand("DIFF_VERSIONS_COMMAND"),Ae=e.createCommand("CLEAR_DIFF_VERSIONS_COMMAND");exports.$getYChangeState=function(t){return e.$getState(t,ie)},exports.CLEAR_DIFF_VERSIONS_COMMAND__EXPERIMENTAL=Ae,exports.CONNECTED_COMMAND=$e,exports.DIFF_VERSIONS_COMMAND__EXPERIMENTAL=ve,exports.TOGGLE_CONNECT_COMMAND=Oe,exports.createBinding=function(e,n,o,i,r,l){null==i&&s(81);const c=h(i.get("root",t.XmlText),null,"root");return c._key="root",{...g(e,o,i,r,l),collabNodeMap:new Map,root:c}},exports.createBindingV2__EXPERIMENTAL=function(e,n,o,i,r={}){null==o&&s(81);const{excludedProperties:l,rootName:c="root-v2"}=r;return{...g(e,n,o,i,l),mapping:new p,root:o.get(c,t.XmlElement)}},exports.createUndoManager=function(e,n){return new t.UndoManager(n,{trackedOrigins:new Set([e,null])})},exports.getAnchorAndFocusCollabNodesForUserState=function(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,i=0,r=null,l=0;if(null!==n&&null!==o){const t=ce(n,e),c=ce(o,e);null!==t&&null!==c&&([s,i]=me(t.type,t.index),[r,l]=me(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:i,focusCollabNode:r,focusOffset:l}},exports.initLocalState=function(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})},exports.renderSnapshot__EXPERIMENTAL=(n,o=t.snapshot(n.doc),i=t.emptySnapshot)=>{const{doc:r}=n;r.gc&&s(325),r.transact(s=>{const l=new t.PermanentUserData(r);l&&l.dss.forEach(e=>{t.iterateDeletedStructs(s,e,e=>{})});const c=(e,t)=>({id:t,type:e,user:("added"===e?l.getUserByClientId(t.client):l.getUserByDeletedId(t))??null});n.mapping.clear(),n.editor.update(()=>{e.$getRoot().clear(),R(n.root,n,null,!0,o,i,c)})},n)},exports.setLocalStateFocus=function(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)},exports.syncCursorPositions=Ne,exports.syncLexicalUpdateToYjs=function(t,n,o,s,i,r,l,c){L(t,()=>{s.read(()=>{if(c.has(e.COLLABORATION_TAG)||c.has(e.HISTORIC_TAG))return void(l.size>0&&function(t,n){const o=Array.from(n),s=t.collabNodeMap,i=[],r=[];for(let t=0;t{t.mapping.deleteNode(e)}),L(t,()=>{s.read(()=>{i.has("root")&&oe(t.doc,t.root,e.$getRoot(),t,new Set(i.keys()));const s=e.$getSelection(),r=o._selection;Se(t,n,r,s)})}))},exports.syncYjsChangesToLexical=function(t,n,o,s,i=Ne){const r=t.editor,l=r._editorState;o.forEach(e=>e.delta),r.update(()=>{for(let e=0;e{i(t,n),r.update(()=>Ce())},skipTransforms:!0,tag:s?e.HISTORIC_TAG:e.COLLABORATION_TAG})},exports.syncYjsChangesToLexicalV2__EXPERIMENTAL=function(n,o,s,i,r){const l=n.editor,c=l._editorState;t.iterateDeletedStructs(i,i.deleteSet,e=>{if(e.constructor===t.Item){const t=e.content.type;t&&n.mapping.delete(t)}}),s.forEach(e=>e.delta),l.update(()=>{for(let e=0;e{Ne(n,o),l.update(()=>Ce())},skipTransforms:!0,tag:r?e.HISTORIC_TAG:e.COLLABORATION_TAG})},exports.syncYjsStateToLexicalV2__EXPERIMENTAL=function(t,n){t.mapping.clear();const o=t.editor;o.update(()=>{e.$getRoot().clear(),R(t.root,t,null,!0),e.$addUpdateTag(e.COLLABORATION_TAG)},{discrete:!0,onUpdate:()=>{Ne(t,n),o.update(()=>Ce())},skipTransforms:!0,tag:e.COLLABORATION_TAG})}; diff --git a/LexicalYjs.prod.mjs b/LexicalYjs.prod.mjs index fa79f7013f40fa9a2575482ca0aae7d0a51b28df..3142c3b03762b3b714e207d051328d731b6982a5 100644 --- a/LexicalYjs.prod.mjs +++ b/LexicalYjs.prod.mjs @@ -9 +9 @@ -import{$isDecoratorNode as e,$getNodeByKey as t,$isLineBreakNode as n,$isTextNode as o,$getSelection as s,$isRangeSelection as i,$isElementNode as r,$getNodeByKeyOrThrow as l,removeFromParent as c,createEditor as a,$isRootNode as f,$getWritableNodeState as d,$getRoot as u,RootNode as h,ElementNode as p,TextNode as g,$getState as _,createState as y,COLLABORATION_TAG as m,HISTORIC_TAG as x,$addUpdateTag as T,SKIP_SCROLL_INTO_VIEW_TAG as b,$createParagraphNode as k,createCommand as S}from"lexical";import{XmlText as N,XmlElement as v,Map as w,Doc as C,typeListToArraySnapshot as O,Snapshot as K,isDeleted as M,XmlHook as E,ContentString as P,ContentFormat as F,emptySnapshot as A,PermanentUserData as j,iterateDeletedStructs as z,snapshot as L,createAbsolutePositionFromRelativePosition as D,createRelativePositionFromTypeIndex as Y,compareRelativePositions as I,Item as $,YMapEvent as W,YTextEvent as U,YXmlEvent as R,UndoManager as B}from"yjs";import{$createChildrenArray as q}from"@lexical/offset";import{createDOMRange as J,createRectsFromDOMRange as G}from"@lexical/selection";function V(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 H(e,t,n){const o=e.length,s=t.length;let i=0,r=0;for(;i0;){const{node:t,nodeIndex:s,offset:i,length:r}=we(this,o,!1);if(t instanceof oe||t instanceof Z||t instanceof Q)n.splice(s,1),e-=1;else{if(!(t instanceof te))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=ve(t._text,i,o-1,"");e.length>0&&(l instanceof te?l._text+=e:this._xmlText.delete(i,e.length))}else t._text=ve(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}=we(this,o,!0);e instanceof te?e._text=ve(e._text,t,0,l):this._xmlText.delete(t,l.length),o+=l.length}else{const t=l,{node:i,nodeIndex:r,length:c}=we(this,o,!1),a=me(e,t,this);if(i instanceof te&&c>0&&ca,y=g>f;if(_&&!y)for(;g<=f;++g){const t=c[g],n=_e(e,l(t),this);this.append(n),d.set(t,n)}else if(y&&!_)for(let t=this._children.length-1;t>=g;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 oe)t.insertEmbed(s,e._xmlText);else if(e instanceof te){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof Z?t.insertEmbed(s,e._map):e instanceof Q&&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&&V(94),void this.append(o);const r=i.getOffset();-1===r&&V(95);const l=this._xmlText;if(0!==n&&l.delete(r,i.getSize()),o instanceof oe)l.insertEmbed(r,o._xmlText);else if(o instanceof te){const e=o._map;null===e.parent&&l.insertEmbed(r,e),l.insert(r+1,o._text)}else o instanceof Z?l.insertEmbed(r,o._map):o instanceof Q&&l.insertEmbed(r,o._xmlElem);if(0!==n){const o=s.slice(t,t+n);for(let t=0;te.getKey()));for(const n of t)this._nodeMap.set(n.getKey(),n),this._nodeKeyToSharedType.set(n.getKey(),e)}else n&&V(332),o(t)&&V(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 N){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 re(e,t,n,o,s){null==n&&V(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))pe(t,o,e)||(s[t]=n);n.set(o.__type,Object.freeze(s))})})}(i),i}function le(e,t,n,o,s,i){null==o&&V(81);const r=se(o.get("root",N),null,"root");return r._key="root",{...re(e,n,o,s,i),collabNodeMap:new Map,root:r}}function ce(e,t,n,o,s={}){null==n&&V(81);const{excludedProperties:i,rootName:r="root-v2"}=s;return{...re(e,t,n,o,i),mapping:new ie,root:n.get(r,v)}}function ae(e){return Object.hasOwn(e,"collabNodeMap")}const fe=new Set(["__key","__parent","__next","__prev","__state"]),de=new Set(["__first","__last","__size"]),ue=new Set(["__cachedText"]),he=new Set(["__text"]);function pe(e,t,n){if(fe.has(e)||"function"==typeof t[e])return!0;if(o(t)){if(he.has(e))return!0}else if(r(t)&&(de.has(e)||f(t)&&ue.has(e)))return!0;const s=t.constructor,i=n.excludedProperties.get(s);return null!=i&&i.has(e)}function ge(e,t){const n=e.__type,{nodeProperties:o}=t,s=o.get(n);return void 0===s&&V(330,n),s}function _e(t,s,i){const l=s.__type;let c;if(r(s)){c=se(new N,i,l),c.syncPropertiesFromLexical(t,s,null),c.syncChildrenFromLexical(t,s,null,null,null)}else if(o(s)){c=ne(new w,s.__text,i,l),c.syncPropertiesAndTextFromLexical(t,s,null)}else if(n(s)){const e=new w;e.set("__type","linebreak"),c=ee(e,i)}else if(e(s)){c=X(new v,i,l),c.syncPropertiesFromLexical(t,s,null)}else V(86);return c._key=s.__key,c}function ye(e){const t=be(e,"__type");return"string"!=typeof t&&void 0!==t&&V(87),t}function me(e,t,n){const o=t._collabNode;if(void 0===o){const o=e.editor._nodes,s=ye(t);"string"!=typeof s&&V(87);void 0===o.get(s)&&V(88,s);const i=t.parent,r=void 0===n&&null!==i?me(e,i):n||null;if(r instanceof oe||V(89),t instanceof N)return se(t,r,s);if(t instanceof w)return"linebreak"===s?ee(t,r):ne(t,"",r,s);if(t instanceof v)return X(t,r,s)}return o}function xe(e,t,n){const o=t.getType(),s=e.editor._nodes.get(o);void 0===s&&V(88,o);const i=new s.klass;if(i.__parent=n,t._key=i.__key,t instanceof oe){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof te?t.syncPropertiesAndTextFromYjs(e,null):t instanceof Q&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(i.__key,t),i}function Te(e,t,n,o){const s=null===o?t instanceof w?Array.from(t.keys()):t instanceof N||t instanceof v?Object.keys(t.getAttributes()):Object.keys(t):Array.from(o);let i;for(let o=0;o{o.markDirty()})}ke(t,l,a)}}}function ve(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function we(e,t,n){let o=0,s=0;const i=e._children,r=i.length;for(;s=t:o>t)&&e instanceof te){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 Ce(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 Oe(e,t){e.doc.transact(t,e)}function Ke(e,n){const o=n._nodeMap.get(e);if(!o)return void u().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():Ke(i.__key,n):u().selectStart()}const Me=e=>"UNDEFINED"===e.nodeName,Ee=(e,t,n,o,s,i,r)=>{let l=t.mapping.get(e);if(l&&n&&0===n.size&&!o)return l;const c=Me(e)?h.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 p){const n=[],o=e=>{if(e instanceof v){const o=Ee(e,t,new Set,!1,s,i,r);null!==o&&n.push(o)}else if(e instanceof N){const o=Ae(e,t,s,i,r);null!==o&&o.forEach(e=>{null!==e&&n.push(e)})}else V(329)};void 0===s||void 0===i?e.toArray().forEach(o):O(e,new K(i.ds,s.sv)).filter(e=>!e._item.deleted||Fe(e._item,s)||Fe(e._item,i)).forEach(o),Pe(l,n)}const f=e.getAttributes(s);Me(e)||void 0===s||(Fe(e._item,s)?Fe(e._item,i)||(f[Je("ychange")]=r?r("added",e._item.id):{type:"added"}):f[Je("ychange")]=r?r("removed",e._item.id):{type:"removed"});const u={...ge(l,t)},g={};for(const e in f)e.startsWith(qe)?g[Ge(e)]=f[e]:u[e]=f[e];if(Te(t,u,l,n),n){const e=Object.keys(g).filter(e=>n.has(Je(e)));if(e.length>0){const t=d(l);for(const n of e)t.updateFromUnknown(n,g[n])}}else d(l).updateFromJSON(g);const _=l.getLatest();return t.mapping.set(e,_),_},Pe=(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,[])},Fe=(e,t)=>void 0===t?!e.deleted:t.sv.has(e.id.client)&&t.sv.get(e.id.client)>e.id.clock&&!M(t.ds,e.id),Ae=(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??g.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;ee.startsWith(qe)).map(([e,t])=>[Ge(e),t]));Te(t,c,n,null),d(n).updateFromJSON(a)}const a=l.map(e=>e.getLatest());return t.mapping.set(e,a),a},je=(e,t)=>e instanceof Array?((e,t)=>{const n=new N;return Ue(n,e,t),n})(e,t):((e,t)=>{const n=new v(e.getType()),o={...Be(e,t),...Ve(e)};for(const e in o){const t=o[e];null!==t&&n.setAttribute(e,t)}return e instanceof p?(n.insert(0,De(e).map(e=>je(e,t))),t.mapping.set(n,e),n):n})(e,t),ze=e=>"object"==typeof e&&null!=e,Le=(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{if(!(e instanceof p))return[];const t=e.getChildren(),n=[];for(let e=0;e{const o=Re(e);return o.length===t.length&&o.every((e,o)=>{const s=t[o],i=e.attributes.t??g.getType(),r=e.attributes.p??{},l=Object.fromEntries(Object.entries(e.attributes).filter(([e])=>e.startsWith(qe)));return e.insert===s.getTextContent()&&i===s.getType()&&Le(r,Be(s,n))&&Le(l,Ve(s))})},Ie=(e,t,n)=>{if(e instanceof v&&!(t instanceof Array)&&Qe(e,t)){const o=De(t);return e._length===o.length&&Le(e.getAttributes(),{...Be(t,n),...Ve(t)})&&e.toArray().every((e,t)=>Ie(e,o[t],n))}return e instanceof N&&t instanceof Array&&Ye(e,t,n)},$e=(e,t)=>e===t||e instanceof Array&&t instanceof Array&&e.length===t.length&&e.every((e,n)=>t[n]===e),We=(e,t,n)=>{const o=e.toArray(),s=De(t),i=s.length,r=o.length,l=Math.min(r,i);let c=0,a=0,f=!1;for(;c{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 P?t+=n.content.str:n.content instanceof F&&(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=Be(e,n);return 0===Object.keys(i).length&&(i=null),{attributes:Object.assign({},o,{...s!==g.getType()&&{t:s},p:i,...Ve(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}=H(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[Je("ychange")]=t.ychange,delete t.ychange),{...e,attributes:t}}),Be=(e,t)=>{const n=ge(e,t),o={};return Object.entries(n).forEach(([t,n])=>{const s=e[t];s!==n&&(o[t]=s)}),o},qe="s_",Je=e=>`s_${e}`,Ge=e=>{if(!e.startsWith(qe))throw new Error(`Invalid state key: ${e}`);return e.slice(qe.length)},Ve=e=>{const t=e.__state;if(!t)return{};const[n={},o]=t.getInternalState(),s={};for(const[e,t]of Object.entries(n))s[Je(e)]=t;for(const[e,t]of o)s[Je(e.key)]=e.unparse(t);return s},He=(e,t,n,o,s)=>{if(t instanceof v&&t.nodeName!==n.getType()&&(!Me(t)||n.getType()!==h.getType()))throw new Error("node name mismatch!");if(o.mapping.set(t,n),t instanceof v){const e=t.getAttributes(),s={...Be(n,o),...Ve(n)};for(const n in s)if(null!=s[n]){e[n]===s[n]||ze(e[n])&&ze(s[n])&&Le(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=De(n),r=i.length,l=t.toArray(),c=l.length,a=Math.min(r,c);let f=0,d=0;for(;f0&&r-f-d>0;){const n=l[f],a=i[f],u=l[c-d-1],h=i[r-d-1];if(n instanceof N&&a instanceof Array)Ye(n,a,o)||Ue(n,a,o),f+=1;else{let i=n instanceof v&&Qe(n,a),r=u instanceof v&&Qe(u,h);if(i&&r){const e=We(n,a,o),t=We(u,h,o);e.foundMappedChild&&!t.foundMappedChild?r=!1:!e.foundMappedChild&&t.foundMappedChild||e.equalityFactor0&&(t.slice(f,f+u).forEach(e=>o.mapping.delete(e)),t.delete(f,u)),f+d!(t instanceof Array)&&e.nodeName===t.getType(),Xe=y("ychange",{isEqual:(e,t)=>e===t,parse:e=>e??null});function Ze(e){return _(e,Xe)}const et=(e,t=L(e.doc),n=A)=>{const{doc:o}=e;o.gc&&V(325),o.transact(s=>{const i=new j(o);i&&i.dss.forEach(e=>{z(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(()=>{u().clear(),Ee(e.root,e,null,!0,t,n,r)})},e)};function tt(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 te){i=n._parent._xmlText;const e=n.getOffset();if(-1===e)return null;s=e+1+s}else if(n instanceof oe&&"element"===e.type){const t=e.getNode();r(t)||V(184);let n=0,i=0,l=t.getFirstChild();for(;null!==l&&i++=k;e--){const t=h[e];l.removeChild(t),h.pop()}}function ft(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,i=0,r=null,l=0;if(null!==n&&null!==o){const t=ot(n,e),c=ot(o,e);null!==t&&null!==c&&([s,i]=pt(t.type,t.index),[r,l]=pt(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:i,focusCollabNode:r,focusOffset:l}}function dt(e,t){const{anchorPos:n,focusPos:s}=t,i=n?ot(n,e):null,r=s?ot(s,e):null;if(null===i||null===r)return{anchorKey:null,anchorOffset:0,focusKey:null,focusOffset:0};if(ae(e)){const[e,t]=pt(i.type,i.index),[n,o]=pt(r.type,r.index);return{anchorKey:null!==e?e.getKey():null,anchorOffset:t,focusKey:null!==n?n.getKey():null,focusOffset:o}}let[l,c]=gt(e.mapping,i),[a,f]=gt(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 ut(e,t){const n=t.awareness.getLocalState();if(null===n)return;const{anchorKey:o,anchorOffset:r,focusKey:l,focusOffset:c}=dt(e,n);if(null!==o&&null!==l){const e=s();if(!i(e))return;ht(e.anchor,o,r),ht(e.focus,l,c)}}function ht(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 pt(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof oe){const{node:e,offset:o}=we(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function gt(e,t){const n=t.type,s=t.index;if(n instanceof v){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&&lt[o].getTextContentSize()&&o+1dt(e,f));if(null!==t&&null!==o)if(i=d.selection,null===i)i=ct(d,t,n,o,s);else{const e=i.anchor,r=i.focus;e.key=t,e.offset=n,r.key=o,r.offset=s}}at(e,d,i,c)}}const f=Array.from(r.keys());for(let t=0;t0&&o.syncPropertiesFromYjs(e,n),s&&(o.applyChildrenYjsDelta(e,i),o.syncChildrenFromYjs(e))}else if(o instanceof te&&t instanceof W){const{keysChanged:n}=t;n.size>0&&o.syncPropertiesAndTextFromYjs(e,n)}else if(o instanceof Q&&t instanceof R){const{attributesChanged:n}=t;n.size>0&&o.syncPropertiesFromYjs(e,n)}else V(82)}function Tt(e,t,n,o,s=yt){const i=e.editor,r=i._editorState;n.forEach(e=>e.delta),i.update(()=>{for(let t=0;t{s(e,t),i.update(()=>kt())},skipTransforms:!0,tag:o?x:m})}function bt(e,t,n){const o=s();if(i(o))if(Ce(o)){const r=e._selection;if(i(r)&&(ut(t,n),Ce(o))){Ke(o.anchor.key,e)}mt(t,n,r,s())}else ut(t,n)}function kt(){0===u().getChildrenSize()&&u().append(k())}function St(e,n,i,r,l,c,a,f){Oe(e,()=>{r.read(()=>{if(f.has(m)||f.has(x))return void(a.size>0&&function(e,n){const s=Array.from(n),i=e.collabNodeMap,r=[],l=[];for(let e=0;e{if(t.constructor===$){const n=t.content.type;n&&e.mapping.delete(n)}}),n.forEach(e=>e.delta),i.update(()=>{for(let t=0;t{yt(e,t),i.update(()=>kt())},skipTransforms:!0,tag:s?x:m})}function wt(e,t){e.mapping.clear();const n=e.editor;n.update(()=>{u().clear(),Ee(e.root,e,null,!0),T(m)},{discrete:!0,onUpdate:()=>{yt(e,t),n.update(()=>kt())},skipTransforms:!0,tag:m})}function Ct(e,t,n,o,i,r,l){(l.has(m)||l.has(x))&&0===r.size||(r.forEach(t=>{e.mapping.deleteNode(t)}),Oe(e,()=>{o.read(()=>{i.has("root")&&He(e.doc,e.root,u(),e,new Set(i.keys()));const o=s(),r=n._selection;mt(e,t,r,o)})}))}const Ot=S("CONNECTED_COMMAND"),Kt=S("TOGGLE_CONNECT_COMMAND"),Mt=S("DIFF_VERSIONS_COMMAND"),Et=S("CLEAR_DIFF_VERSIONS_COMMAND");function Pt(e,t){return new B(t,{trackedOrigins:new Set([e,null])})}function Ft(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})}function At(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{Ze as $getYChangeState,Et as CLEAR_DIFF_VERSIONS_COMMAND__EXPERIMENTAL,Ot as CONNECTED_COMMAND,Mt as DIFF_VERSIONS_COMMAND__EXPERIMENTAL,Kt as TOGGLE_CONNECT_COMMAND,le as createBinding,ce as createBindingV2__EXPERIMENTAL,Pt as createUndoManager,ft as getAnchorAndFocusCollabNodesForUserState,Ft as initLocalState,et as renderSnapshot__EXPERIMENTAL,At as setLocalStateFocus,yt as syncCursorPositions,St as syncLexicalUpdateToYjs,Ct as syncLexicalUpdateToYjsV2__EXPERIMENTAL,Tt as syncYjsChangesToLexical,vt as syncYjsChangesToLexicalV2__EXPERIMENTAL,wt as syncYjsStateToLexicalV2__EXPERIMENTAL}; +import{$isDecoratorNode as e,$getNodeByKey as t,$isLineBreakNode as n,$isTextNode as o,$getSelection as s,$isRangeSelection as i,$isElementNode as r,$getNodeByKeyOrThrow as l,removeFromParent as c,createEditor as a,$isRootNode as f,$getWritableNodeState as d,$getRoot as u,RootNode as h,ElementNode as p,TextNode as g,$getState as _,createState as y,COLLABORATION_TAG as m,HISTORIC_TAG as x,$addUpdateTag as T,SKIP_SCROLL_INTO_VIEW_TAG as b,$createParagraphNode as k,createCommand as S}from"lexical";import{XmlText as N,XmlElement as v,Map as w,Doc as C,typeListToArraySnapshot as O,Snapshot as K,isDeleted as M,XmlHook as E,ContentString as P,ContentFormat as F,emptySnapshot as A,PermanentUserData as j,iterateDeletedStructs as z,snapshot as L,createAbsolutePositionFromRelativePosition as D,createRelativePositionFromTypeIndex as Y,compareRelativePositions as I,Item as $,YMapEvent as W,YTextEvent as U,YXmlEvent as R,UndoManager as B}from"yjs";import{$createChildrenArray as q}from"@lexical/offset";import{createDOMRange as J,createRectsFromDOMRange as G}from"@lexical/selection";function V(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 H(e,t,n){const o=e.length,s=t.length;let i=0,r=0;for(;i0;){const{node:t,nodeIndex:s,offset:i,length:r}=we(this,o,!1);if(t instanceof oe||t instanceof Z||t instanceof Q)n.splice(s,1),e-=1;else{if(!(t instanceof te))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=ve(t._text,i,o-1,"");e.length>0&&(l instanceof te?l._text+=e:this._xmlText.delete(i,e.length))}else t._text=ve(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}=we(this,o,!0);e instanceof te?e._text=ve(e._text,t,0,l):this._xmlText.delete(t,l.length),o+=l.length}else{const t=l,{node:i,nodeIndex:r,length:c}=we(this,o,!1),a=me(e,t,this);if(i instanceof te&&c>0&&ca,y=g>f;if(_&&!y)for(;g<=f;++g){const t=c[g],n=_e(e,l(t),this);this.append(n),d.set(t,n)}else if(y&&!_)for(let t=this._children.length-1;t>=g;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 oe)t.insertEmbed(s,e._xmlText);else if(e instanceof te){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof Z?t.insertEmbed(s,e._map):e instanceof Q&&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&&V(94),void this.append(o);const r=i.getOffset();-1===r&&V(95);const l=this._xmlText;if(0!==n&&l.delete(r,i.getSize()),o instanceof oe)l.insertEmbed(r,o._xmlText);else if(o instanceof te){const e=o._map;null===e.parent&&l.insertEmbed(r,e),l.insert(r+1,o._text)}else o instanceof Z?l.insertEmbed(r,o._map):o instanceof Q&&l.insertEmbed(r,o._xmlElem);if(0!==n){const o=s.slice(t,t+n);for(let t=0;te.getKey()));for(const n of t)this._nodeMap.set(n.getKey(),n),this._nodeKeyToSharedType.set(n.getKey(),e)}else n&&V(332),o(t)&&V(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 N){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 re(e,t,n,o,s){null==n&&V(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))pe(t,o,e)||(s[t]=n);n.set(o.__type,Object.freeze(s))})})}(i),i}function le(e,t,n,o,s,i){null==o&&V(81);const r=se(o.get("root",N),null,"root");return r._key="root",{...re(e,n,o,s,i),collabNodeMap:new Map,root:r}}function ce(e,t,n,o,s={}){null==n&&V(81);const{excludedProperties:i,rootName:r="root-v2"}=s;return{...re(e,t,n,o,i),mapping:new ie,root:n.get(r,v)}}function ae(e){return Object.hasOwn(e,"collabNodeMap")}const fe=new Set(["__key","__parent","__next","__prev","__state"]),de=new Set(["__first","__last","__size"]),ue=new Set(["__cachedText"]),he=new Set(["__text"]);function pe(e,t,n){if(fe.has(e)||"function"==typeof t[e])return!0;if(o(t)){if(he.has(e))return!0}else if(r(t)&&(de.has(e)||f(t)&&ue.has(e)))return!0;const s=t.constructor,i=n.excludedProperties.get(s);return null!=i&&i.has(e)}function ge(e,t){const n=e.__type,{nodeProperties:o}=t,s=o.get(n);return void 0===s&&V(330,n),s}function _e(t,s,i){const l=s.__type;let c;if(r(s)){c=se(new N,i,l),c.syncPropertiesFromLexical(t,s,null),c.syncChildrenFromLexical(t,s,null,null,null)}else if(o(s)){c=ne(new w,s.__text,i,l),c.syncPropertiesAndTextFromLexical(t,s,null)}else if(n(s)){const e=new w;e.set("__type","linebreak"),c=ee(e,i)}else if(e(s)){c=X(new v,i,l),c.syncPropertiesFromLexical(t,s,null)}else V(86);return c._key=s.__key,c}function ye(e){const t=be(e,"__type");return"string"!=typeof t&&void 0!==t&&V(87),t}function me(e,t,n){const o=t._collabNode;if(void 0===o){const o=e.editor._nodes,s=ye(t);"string"!=typeof s&&V(87);void 0===o.get(s)&&V(88,s);const i=t.parent,r=void 0===n&&null!==i?me(e,i):n||null;if(r instanceof oe||V(89),t instanceof N)return se(t,r,s);if(t instanceof w)return"linebreak"===s?ee(t,r):ne(t,"",r,s);if(t instanceof v)return X(t,r,s)}return o}function xe(e,t,n){const o=t.getType(),s=e.editor._nodes.get(o);void 0===s&&V(88,o);const i=new s.klass;if(i.__parent=n,t._key=i.__key,t instanceof oe){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof te?t.syncPropertiesAndTextFromYjs(e,null):t instanceof Q&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(i.__key,t),i}function Te(e,t,n,o){const s=null===o?t instanceof w?Array.from(t.keys()):t instanceof N||t instanceof v?Object.keys(t.getAttributes()):Object.keys(t):Array.from(o);let i;for(let o=0;o{o.markDirty()})}ke(t,l,a)}}}function ve(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function we(e,t,n){let o=0,s=0;const i=e._children,r=i.length;for(;s=t:o>t)&&e instanceof te){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 Ce(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 Oe(e,t){e.doc.transact(t,e)}function Ke(e,n){const o=n._nodeMap.get(e);if(!o)return void u().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():Ke(i.__key,n):u().selectStart()}const Me=e=>"UNDEFINED"===e.nodeName,Ee=(e,t,n,o,s,i,r)=>{let l=t.mapping.get(e);if(l&&n&&0===n.size&&!o)return l;const c=Me(e)?h.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 p){const n=[],o=e=>{if(e instanceof v){const o=Ee(e,t,new Set,!1,s,i,r);null!==o&&n.push(o)}else if(e instanceof N){const o=Ae(e,t,s,i,r);null!==o&&o.forEach(e=>{null!==e&&n.push(e)})}else V(329)};void 0===s||void 0===i?e.toArray().forEach(o):O(e,new K(i.ds,s.sv)).filter(e=>!e._item.deleted||Fe(e._item,s)||Fe(e._item,i)).forEach(o),Pe(l,n)}const f=e.getAttributes(s);Me(e)||void 0===s||(Fe(e._item,s)?Fe(e._item,i)||(f[Je("ychange")]=r?r("added",e._item.id):{type:"added"}):f[Je("ychange")]=r?r("removed",e._item.id):{type:"removed"});const u={...ge(l,t)},g={};for(const e in f)e.startsWith(qe)?g[Ge(e)]=f[e]:u[e]=f[e];if(Te(t,u,l,n),n){const e=Object.keys(g).filter(e=>n.has(Je(e)));if(e.length>0){const t=d(l);for(const n of e)t.updateFromUnknown(n,g[n])}}else d(l).updateFromJSON(g);const _=l.getLatest();return t.mapping.set(e,_),_},Pe=(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,[])},Fe=(e,t)=>void 0===t?!e.deleted:t.sv.has(e.id.client)&&t.sv.get(e.id.client)>e.id.clock&&!M(t.ds,e.id),Ae=(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??g.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;ee.startsWith(qe)).map(([e,t])=>[Ge(e),t]));Te(t,c,n,null),d(n).updateFromJSON(a)}const a=l.map(e=>e.getLatest());return t.mapping.set(e,a),a},je=(e,t)=>e instanceof Array?((e,t)=>{const n=new N;return Ue(n,e,t),n})(e,t):((e,t)=>{const n=new v(e.getType()),o={...Be(e,t),...Ve(e)};for(const e in o){const t=o[e];null!==t&&n.setAttribute(e,t)}return e instanceof p?(n.insert(0,De(e).map(e=>je(e,t))),t.mapping.set(n,e),n):n})(e,t),ze=e=>"object"==typeof e&&null!=e,Le=(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{if(!(e instanceof p))return[];const t=e.getChildren(),n=[];for(let e=0;e{const o=Re(e);return o.length===t.length&&o.every((e,o)=>{const s=t[o],i=e.attributes.t??g.getType(),r=e.attributes.p??{},l=Object.fromEntries(Object.entries(e.attributes).filter(([e])=>e.startsWith(qe)));return e.insert===s.getTextContent()&&i===s.getType()&&Le(r,Be(s,n))&&Le(l,Ve(s))})},Ie=(e,t,n)=>{if(e instanceof v&&!(t instanceof Array)&&Qe(e,t)){const o=De(t);return e._length===o.length&&Le(e.getAttributes(),{...Be(t,n),...Ve(t)})&&e.toArray().every((e,t)=>Ie(e,o[t],n))}return e instanceof N&&t instanceof Array&&Ye(e,t,n)},$e=(e,t)=>e===t||e instanceof Array&&t instanceof Array&&e.length===t.length&&e.every((e,n)=>t[n]===e),We=(e,t,n)=>{const o=e.toArray(),s=De(t),i=s.length,r=o.length,l=Math.min(r,i);let c=0,a=0,f=!1;for(;c{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 P?t+=n.content.str:n.content instanceof F&&(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=Be(e,n);return 0===Object.keys(i).length&&(i=null),{attributes:Object.assign({},o,{...s!==g.getType()&&{t:s},p:i,...Ve(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}=H(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[Je("ychange")]=t.ychange,delete t.ychange),{...e,attributes:t}}),Be=(e,t)=>{const n=ge(e,t),o={};return Object.entries(n).forEach(([t,n])=>{const s=e[t];s!==n&&(o[t]=s)}),o},qe="s_",Je=e=>`s_${e}`,Ge=e=>{if(!e.startsWith(qe))throw new Error(`Invalid state key: ${e}`);return e.slice(qe.length)},Ve=e=>{const t=e.__state;if(!t)return{};const[n={},o]=t.getInternalState(),s={};for(const[e,t]of Object.entries(n))s[Je(e)]=t;for(const[e,t]of o)s[Je(e.key)]=e.unparse(t);return s},He=(e,t,n,o,s)=>{if(t instanceof v&&t.nodeName!==n.getType()&&(!Me(t)||n.getType()!==h.getType()))throw new Error("node name mismatch!");if(o.mapping.set(t,n),t instanceof v){const e=t.getAttributes(),s={...Be(n,o),...Ve(n)};for(const n in s)if(null!=s[n]){e[n]===s[n]||ze(e[n])&&ze(s[n])&&Le(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=De(n),r=i.length,l=t.toArray(),c=l.length,a=Math.min(r,c);let f=0,d=0;for(;f0&&r-f-d>0;){const n=l[f],a=i[f],u=l[c-d-1],h=i[r-d-1];if(n instanceof N&&a instanceof Array)Ye(n,a,o)||Ue(n,a,o),f+=1;else{let i=n instanceof v&&Qe(n,a),r=u instanceof v&&Qe(u,h);if(i&&r){const e=We(n,a,o),t=We(u,h,o);e.foundMappedChild&&!t.foundMappedChild?r=!1:!e.foundMappedChild&&t.foundMappedChild||e.equalityFactor0&&(t.slice(f,f+u).forEach(e=>o.mapping.delete(e)),t.delete(f,u)),f+d!(t instanceof Array)&&e.nodeName===t.getType(),Xe=y("ychange",{isEqual:(e,t)=>e===t,parse:e=>e??null});function Ze(e){return _(e,Xe)}const et=(e,t=L(e.doc),n=A)=>{const{doc:o}=e;o.gc&&V(325),o.transact(s=>{const i=new j(o);i&&i.dss.forEach(e=>{z(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(()=>{u().clear(),Ee(e.root,e,null,!0,t,n,r)})},e)};function tt(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 te){i=n._parent._xmlText;const e=n.getOffset();if(-1===e)return null;s=e+1+s}else if(n instanceof oe&&"element"===e.type){const t=e.getNode();r(t)||V(184);let n=0,i=0,l=t.getFirstChild();for(;null!==l&&i++=k;e--){const t=h[e];l.removeChild(t),h.pop()}}function ft(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,i=0,r=null,l=0;if(null!==n&&null!==o){const t=ot(n,e),c=ot(o,e);null!==t&&null!==c&&([s,i]=pt(t.type,t.index),[r,l]=pt(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:i,focusCollabNode:r,focusOffset:l}}function dt(e,t){const{anchorPos:n,focusPos:s}=t,i=n?ot(n,e):null,r=s?ot(s,e):null;if(null===i||null===r)return{anchorKey:null,anchorOffset:0,focusKey:null,focusOffset:0};if(ae(e)){const[e,t]=pt(i.type,i.index),[n,o]=pt(r.type,r.index);return{anchorKey:null!==e?e.getKey():null,anchorOffset:t,focusKey:null!==n?n.getKey():null,focusOffset:o}}let[l,c]=gt(e.mapping,i),[a,f]=gt(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 ut(e,t){const n=t.awareness.getLocalState();if(null===n)return;const{anchorKey:o,anchorOffset:r,focusKey:l,focusOffset:c}=dt(e,n);if(null!==o&&null!==l){const e=s();if(!i(e))return;ht(e.anchor,o,r),ht(e.focus,l,c)}}function ht(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 pt(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof oe){const{node:e,offset:o}=we(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function gt(e,t){const n=t.type,s=t.index;if(n instanceof v){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&&lt[o].getTextContentSize()&&o+1dt(e,f));if(null!==t&&null!==o)if(i=d.selection,null===i)i=ct(d,t,n,o,s);else{const e=i.anchor,r=i.focus;e.key=t,e.offset=n,r.key=o,r.offset=s}}at(e,d,i,c)}}const f=Array.from(r.keys());for(let t=0;t0&&o.syncPropertiesFromYjs(e,n),s&&(o.applyChildrenYjsDelta(e,i),o.syncChildrenFromYjs(e))}else if(o instanceof te&&t instanceof W){const{keysChanged:n}=t;n.size>0&&o.syncPropertiesAndTextFromYjs(e,n)}else if(o instanceof Q&&t instanceof R){const{attributesChanged:n}=t;n.size>0&&o.syncPropertiesFromYjs(e,n)}else V(82)}function Tt(e,t,n,o,s=yt){const i=e.editor,r=i._editorState;n.forEach(e=>e.delta),i.update(()=>{for(let t=0;t{s(e,t),i.update(()=>kt())},skipTransforms:!0,tag:o?x:m})}function bt(e,t,n){const o=s();if(i(o))if(Ce(o)){const r=e._selection;if(i(r)&&(ut(t,n),Ce(o))){Ke(o.anchor.key,e)}mt(t,n,r,s())}else ut(t,n)}function kt(){0===u().getChildrenSize()&&u().append(k())}function St(e,n,i,r,l,c,a,f){Oe(e,()=>{r.read(()=>{if(f.has(m)||f.has(x))return void(a.size>0&&function(e,n){const s=Array.from(n),i=e.collabNodeMap,r=[],l=[];for(let e=0;e{if(t.constructor===$){const n=t.content.type;n&&e.mapping.delete(n)}}),n.forEach(e=>e.delta),i.update(()=>{for(let t=0;t{yt(e,t),i.update(()=>kt())},skipTransforms:!0,tag:s?x:m})}function wt(e,t){e.mapping.clear();const n=e.editor;n.update(()=>{u().clear(),Ee(e.root,e,null,!0),T(m)},{discrete:!0,onUpdate:()=>{yt(e,t),n.update(()=>kt())},skipTransforms:!0,tag:m})}function Ct(e,t,n,o,i,r,l){(l.has(m)||l.has(x))&&0===r.size||(r.forEach(t=>{e.mapping.deleteNode(t)}),Oe(e,()=>{o.read(()=>{i.has("root")&&He(e.doc,e.root,u(),e,new Set(i.keys()));const o=s(),r=n._selection;mt(e,t,r,o)})}))}const Ot=S("CONNECTED_COMMAND"),Kt=S("TOGGLE_CONNECT_COMMAND"),Mt=S("DIFF_VERSIONS_COMMAND"),Et=S("CLEAR_DIFF_VERSIONS_COMMAND");function Pt(e,t){return new B(t,{trackedOrigins:new Set([e,null])})}function Ft(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})}function At(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{Ze as $getYChangeState,Et as CLEAR_DIFF_VERSIONS_COMMAND__EXPERIMENTAL,Ot as CONNECTED_COMMAND,Mt as DIFF_VERSIONS_COMMAND__EXPERIMENTAL,Kt as TOGGLE_CONNECT_COMMAND,le as createBinding,ce as createBindingV2__EXPERIMENTAL,Pt as createUndoManager,ft as getAnchorAndFocusCollabNodesForUserState,Ft as initLocalState,et as renderSnapshot__EXPERIMENTAL,At as setLocalStateFocus,yt as syncCursorPositions,St as syncLexicalUpdateToYjs,Ct as syncLexicalUpdateToYjsV2__EXPERIMENTAL,Tt as syncYjsChangesToLexical,vt as syncYjsChangesToLexicalV2__EXPERIMENTAL,wt as syncYjsStateToLexicalV2__EXPERIMENTAL};