/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * */ import{$getNodeByKey as e,$isLineBreakNode as t,$isTextNode as n,$getSelection as o,$isRangeSelection as s,$isElementNode as i,$isDecoratorNode as l,createEditor as r,$getWritableNodeState as c,$getRoot as f,$isRootNode as a,$getNodeByKeyOrThrow as u,removeFromParent as d,$addUpdateTag as h,SKIP_SCROLL_INTO_VIEW_TAG as _,$createParagraphNode as p,HISTORIC_TAG as g,COLLABORATION_TAG as y,createCommand as x}from"lexical";import{XmlText as m,Map as k,XmlElement as b,Doc as C,createAbsolutePositionFromRelativePosition as N,createRelativePositionFromTypeIndex as v,compareRelativePositions as w,YMapEvent as S,YTextEvent as T,YXmlEvent as P,UndoManager as F}from"yjs";import{$createChildrenArray as O}from"@lexical/offset";import{createDOMRange as E,createRectsFromDOMRange as M}from"@lexical/selection";function L(e,...t){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",e);for(const e of t)o.append("v",e);throw n.search=o.toString(),Error(`Minified Lexical error #${e}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}class z{constructor(e,t){this._key="",this._map=e,this._parent=t,this._type="linebreak"}getNode(){const n=e(this._key);return t(n)?n:null}getKey(){return this._key}getSharedType(){return this._map}getType(){return this._type}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function j(e,t){const n=new z(e,t);return e._collabNode=n,n}class A{constructor(e,t,n,o){this._key="",this._map=e,this._parent=n,this._text=t,this._type=o,this._normalized=!1}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return n(t)?t:null}getNode(){const t=e(this._key);return n(t)?t:null}getSharedType(){return this._map}getType(){return this._type}getKey(){return this._key}getSize(){return this._text.length+(this._normalized?0:1)}getOffset(){return this._parent.getChildOffset(this)}spliceText(e,t,n){const o=this._parent._xmlText,s=this.getOffset()+1+e;0!==t&&o.delete(s,t),""!==n&&o.insert(s,n)}syncPropertiesAndTextFromLexical(e,t,n){const i=this.getPrevNode(n),l=t.__text;if(V(e,this._map,i,t),null!==i){const e=i.__text;if(e!==l){!function(e,t,n,i){const l=o();let r=i.length;if(s(l)&&l.isCollapsed()){const e=l.anchor;e.key===t&&(r=e.offset)}const c=function(e,t,n){const o=e.length,s=t.length;let i=0,l=0;for(;i!W(t,o,e))),i.set(s,l));const r=e.editor.constructor;!function(e,t,n,o){const s=o.__state,i=q(t,"__state");if(!s)return;const[l,r]=s.getInternalState(),c=n&&n.__state,f=i instanceof k?i:new k;if(c===s)return;const[a,u]=c&&f.doc?c.getInternalState():[void 0,new Map];if(l)for(const[e,t]of Object.entries(l))a&&t!==a[e]&&f.set(e,t);for(const[e,t]of r)u.get(e)!==t&&f.set(e.key,e.unparse(t));i||H(t,"__state",f)}(0,t,n,o);for(let s=0;s{o.markDirty()}))}H(t,i,f)}}}function X(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function Z(e,t,n){let o=0,s=0;const i=e._children,l=i.length;for(;s=t:o>t)&&e instanceof A){let n=t-r-1;n<0&&(n=0);return{length:o-t,node:e,nodeIndex:s,offset:n}}if(o>t)return{length:0,node:e,nodeIndex:s,offset:r};if(s===l-1)return{length:0,node:null,nodeIndex:s+1,offset:r+1}}return{length:0,node:null,nodeIndex:0,offset:0}}function ee(e){const t=e.anchor,o=e.focus;let s=!1;try{const e=t.getNode(),i=o.getNode();(!e.isAttached()||!i.isAttached()||n(e)&&t.offset>e.getTextContentSize()||n(i)&&o.offset>i.getTextContentSize())&&(s=!0)}catch(e){s=!0}return s}function te(t,n){const o=n._nodeMap.get(t);if(!o)return void f().selectStart();const s=o.__prev;let i=null;s&&(i=e(s)),null===i&&null!==o.__parent&&(i=e(o.__parent)),null!==i?null!==i&&i.isAttached()?i.selectEnd():te(i.__key,n):f().selectStart()}class ne{constructor(e,t,n){this._key="",this._xmlElem=e,this._parent=t,this._type=n}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return l(t)?t:null}getNode(){const t=e(this._key);return l(t)?t:null}getSharedType(){return this._xmlElem}getType(){return this._type}getKey(){return this._key}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}syncPropertiesFromLexical(e,t,n){const o=this.getPrevNode(n);V(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&L(83);J(e,this._xmlElem,n,t)}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function oe(e,t,n){const o=new ne(e,t,n);return e._collabNode=o,o}class se{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 i(t)?t:null}getNode(){const t=e(this._key);return i(t)?t:null}getSharedType(){return this._xmlText}getType(){return this._type}getKey(){return this._key}isEmpty(){return 0===this._children.length}getSize(){return 1}getOffset(){const e=this._parent;return null===e&&L(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&L(91),J(e,this._xmlText,n,t)}applyChildrenYjsDelta(e,t){const n=this._children;let o=0,s=null;for(let i=0;i0;){const{node:t,nodeIndex:s,offset:i,length:l}=Z(this,o,!1);if(t instanceof se||t instanceof z||t instanceof ne)n.splice(s,1),e-=1;else{if(!(t instanceof A))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=X(t._text,i,o-1,"");e.length>0&&(r instanceof A?r._text+=e:this._xmlText.delete(i,e.length))}else t._text=X(t._text,i,o,"");e-=o}}}}else{if(null==r)throw new Error("Unexpected delta format");if("string"==typeof r){const{node:e,offset:t}=Z(this,o,!0);e instanceof A?e._text=X(e._text,t,0,r):this._xmlText.delete(t,r.length),o+=r.length}else{const t=r,{node:i,nodeIndex:l,length:c}=Z(this,o,!1),f=B(e,t,this);if(i instanceof A&&c>0&&cc,y=p>f;if(g&&!y)for(;p<=f;++p){const t=r[p],n=R(e,u(t),this);this.append(n),a.set(t,n)}else if(y&&!g)for(let t=this._children.length-1;t>=p;t--)this.splice(e,t,1)}append(e){const t=this._xmlText,n=this._children,o=n[n.length-1],s=void 0!==o?o.getOffset()+o.getSize():0;if(e instanceof se)t.insertEmbed(s,e._xmlText);else if(e instanceof A){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 ne&&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&&L(94),void this.append(o);const l=i.getOffset();-1===l&&L(95);const r=this._xmlText;if(0!==n&&r.delete(l,i.getSize()),o instanceof se)r.insertEmbed(l,o._xmlText);else if(o instanceof A){const e=o._map;null===e.parent&&r.insertEmbed(l,e),r.insert(l+1,o._text)}else o instanceof z?r.insertEmbed(l,o._map):o instanceof ne&&r.insertEmbed(l,o._xmlElem);if(0!==n){const o=s.slice(t,t+n);for(let t=0;t=C;e--){const t=h[e];r.removeChild(t),h.pop()}}function pe(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,i=0,l=null,r=0;if(null!==n&&null!==o){const t=ce(n,e),c=ce(o,e);null!==t&&null!==c&&([s,i]=xe(t.type,t.index),[l,r]=xe(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:i,focusCollabNode:l,focusOffset:r}}function ge(e,t){const n=t.awareness.getLocalState();if(null===n)return;const{anchorCollabNode:i,anchorOffset:l,focusCollabNode:r,focusOffset:c}=pe(e,n);if(null!==i&&null!==r){const e=i.getKey(),t=r.getKey(),n=o();if(!s(n))return;ye(n.anchor,e,l),ye(n.focus,t,c)}}function ye(t,o,s){if(t.key!==o||t.offset!==s){let l=e(o);if(null!==l&&!i(l)&&!n(l)){const e=l.getParentOrThrow();o=e.getKey(),s=l.getIndexWithinParent(),l=e}t.set(o,s,i(l)?"element":"text")}}function xe(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof se){const{node:e,offset:o}=Z(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function me(e,t){return t.awareness.getStates()}function ke(e,t,n){const{getAwarenessStates:o=me}=n??{},s=Array.from(o(e,t)),i=e.clientID,l=e.cursors,r=e.editor._editorState._nodeMap,c=new Set;for(let t=0;t0&&o.syncPropertiesFromYjs(e,n),s&&(o.applyChildrenYjsDelta(e,i),o.syncChildrenFromYjs(e))}else if(o instanceof A&&t instanceof S){const{keysChanged:n}=t;n.size>0&&o.syncPropertiesAndTextFromYjs(e,n)}else if(o instanceof ne&&t instanceof P){const{attributesChanged:n}=t;n.size>0&&o.syncPropertiesFromYjs(e,n)}else L(82)}function Ne(e,t,n,i,l=ke){const r=e.editor,c=r._editorState;n.forEach((e=>e.delta)),r.update((()=>{for(let t=0;t{l(e,t),r.update((()=>{0===f().getChildrenSize()&&f().append(p())}))},skipTransforms:!0,tag:i?g:y})}function ve(t,s,i,l,r,c,a,u){!function(e,t){e.doc.transact(t,e)}(t,(()=>{l.read((()=>{if(u.has(y)||u.has(g))return void(a.size>0&&function(t,o){const s=Array.from(o),i=t.collabNodeMap,l=[],r=[];for(let t=0;t