@lexical/yjs 0.51.1-nightly.20260925.0 → 0.52.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2885,31 +2885,39 @@ function updateCursor(binding, cursor, nextSelection, nodeMap, theme = {}) {
2885
2885
  if (anchorNode == null || focusNode == null) {
2886
2886
  return;
2887
2887
  }
2888
- if (highlight !== null) {
2889
- // modern path: CSS Custom Highlight API
2890
- const range = createDOMRange(editor, anchorNode, anchor.offset, focusNode, focus.offset);
2891
- if (range === null) {
2892
- return;
2893
- }
2894
-
2895
- // The browser handles line wrapping, RTL, and font metrics — no rect math.
2896
- highlight.clear();
2897
- if (!range.collapsed) {
2898
- highlight.add(range);
2899
- }
2900
-
2901
- // Caret stays as a positioned element; anchor it to the focus end.
2902
- const caretRange = range.cloneRange();
2903
- caretRange.collapse(false);
2904
- let caretRect = caretRange.getBoundingClientRect();
2905
- if (caretRect.height === 0 && $isLineBreakNode(focusNode)) {
2906
- // Bare <br>: collapsed range reports zero size. Fall back to the
2907
- // line break's own box so the caret still renders.
2888
+ const positionCaretAtFocus = () => {
2889
+ const focusRange = createDOMRange(editor, focusNode, focus.offset, focusNode, focus.offset);
2890
+ let caretRect = focusRange === null ? undefined : focusRange.getBoundingClientRect();
2891
+ if ((!caretRect || caretRect.height === 0) && $isLineBreakNode(focusNode)) {
2908
2892
  const focusEl = editor.getElementByKey(focusKey);
2909
2893
  if (focusEl !== null) {
2910
2894
  caretRect = focusEl.getBoundingClientRect();
2911
2895
  }
2912
2896
  }
2897
+ if (caretRect !== undefined && caretRect.width === 0 && caretRect.height === 0 && $isElementNode(focusNode)) {
2898
+ // A collapsed range at an element boundary can have no geometry. Use
2899
+ // the adjacent text boundary instead of treating (0, 0) as a caret.
2900
+ const adjacentRect = editor.read('latest', () => {
2901
+ const previous = focusNode.getChildAtIndex(focus.offset - 1);
2902
+ const next = focusNode.getChildAtIndex(focus.offset);
2903
+ for (const [node, offset] of [[previous, $isTextNode(previous) ? previous.getTextContentSize() : 0], [next, 0]]) {
2904
+ if ($isTextNode(node)) {
2905
+ const range = createDOMRange(editor, node, offset, node, offset);
2906
+ const rect = range === null ? undefined : range.getBoundingClientRect();
2907
+ if (rect && rect.height > 0) {
2908
+ return rect;
2909
+ }
2910
+ }
2911
+ }
2912
+ return null;
2913
+ });
2914
+ if (adjacentRect !== null) {
2915
+ caretRect = adjacentRect;
2916
+ }
2917
+ }
2918
+ if (!caretRect || caretRect.width === 0 && caretRect.height === 0) {
2919
+ return false;
2920
+ }
2913
2921
  setDOMStyleObject(caret.style, {
2914
2922
  'background-color': theme.cursor ? '' : color,
2915
2923
  bottom: '',
@@ -2925,6 +2933,21 @@ function updateCursor(binding, cursor, nextSelection, nodeMap, theme = {}) {
2925
2933
  if (caret.parentNode !== cursorsContainer) {
2926
2934
  cursorsContainer.appendChild(caret);
2927
2935
  }
2936
+ return true;
2937
+ };
2938
+ if (highlight !== null) {
2939
+ // modern path: CSS Custom Highlight API
2940
+ const range = createDOMRange(editor, anchorNode, anchor.offset, focusNode, focus.offset);
2941
+ if (range === null) {
2942
+ return;
2943
+ }
2944
+
2945
+ // The browser handles line wrapping, RTL, and font metrics — no rect math.
2946
+ highlight.clear();
2947
+ if (!range.collapsed) {
2948
+ highlight.add(range);
2949
+ }
2950
+ positionCaretAtFocus();
2928
2951
  return;
2929
2952
  }
2930
2953
 
@@ -2996,10 +3019,20 @@ function updateCursor(binding, cursor, nextSelection, nodeMap, theme = {}) {
2996
3019
  'z-index': '5'
2997
3020
  });
2998
3021
  }
2999
- if (i === selectionRectsLength - 1) {
3000
- if (caret.parentNode !== selection) {
3001
- selection.appendChild(caret);
3002
- }
3022
+ }
3023
+ if (!positionCaretAtFocus() && selectionRectsLength > 0) {
3024
+ const lastSelection = selections[selectionRectsLength - 1];
3025
+ // A reused caret may still have container-relative coordinates. Restore
3026
+ // rectangle-relative positioning before moving it into the fallback span.
3027
+ setDOMStyleObject(caret.style, {
3028
+ bottom: '0',
3029
+ height: '',
3030
+ left: '',
3031
+ right: '-1px',
3032
+ top: '0'
3033
+ });
3034
+ if (caret.parentNode !== lastSelection) {
3035
+ lastSelection.appendChild(caret);
3003
3036
  }
3004
3037
  }
3005
3038
  for (let i = selectionsLength - 1; i >= selectionRectsLength; i--) {
@@ -6,4 +6,4 @@
6
6
  *
7
7
  */
8
8
 
9
- 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,isLexicalEditor as f,createEditor as u,$getSlotNames as d,$getSlot as h,$removeSlot as p,$setSlot as g,$isRootNode as y,$getWritableNodeState as _,getDeclaredSlots as m,$getRoot as x,RootNode as S,ElementNode as b,TextNode as T,$getState as N,createState as k,getRootOwnerDocument as v,setDOMStyleObject as w,isDOMShadowRoot as C,COLLABORATION_TAG as F,HISTORIC_TAG as M,$addUpdateTag as A,SKIP_SCROLL_INTO_VIEW_TAG as O,$createParagraphNode as E,createCommand as K}from"lexical";import{XmlText as P,XmlElement as j,Map as L,Doc as z,typeListToArraySnapshot as Y,Snapshot as D,typeMapGetAllSnapshot as I,isDeleted as R,XmlHook as $,ContentString as B,ContentFormat as W,emptySnapshot as U,PermanentUserData as H,iterateDeletedStructs as q,snapshot as J,createAbsolutePositionFromRelativePosition as V,createRelativePositionFromTypeIndex as G,compareRelativePositions as X,Item as Q,YMapEvent as Z,YTextEvent as ee,YXmlEvent as te,UndoManager as ne}from"yjs";import{createDOMRange as oe,createRectsFromDOMRange as se}from"@lexical/selection";function ie(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 re(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 le{_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 instanceof le&&ie(394),this._parent.getChildOffset(this)}syncPropertiesFromLexical(e,t,n){const o=this.getPrevNode(n);$e(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null!==n&&Ye(e,this._xmlElem,n,t)}syncSlotsFromYjs(e,t){Je(e,this._xmlElem,t,this)}syncSlotsFromLexical(e,t,n,o,s){Ve(e,this._xmlElem,t,n,o,s,this)}destroy(e){const t=e.collabNodeMap;Ge(e,this._xmlElem),t.get(this._key)===this&&t.delete(this._key)}}function ce(e,t,n){const o=new le(e,t,n);return e._collabNode=o,o}class ae{_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 fe(e,t){const n=new ae(e,t);return e._collabNode=n,n}class ue{_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($e(e,this._map,o,t),null!==o){const e=o.__text;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=re(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();if(null===n)return;Ye(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 de(e,t,n,o){const s=new ue(e,t,n,o);return e._collabNode=s,s}class he{_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 e instanceof he||ie(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null!==n&&Ye(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}=We(this,o,!1);if(t instanceof he||t instanceof ae||t instanceof le)n.splice(s,1),e-=1;else{if(!(t instanceof ue))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=Be(t._text,i,o-1,"");e.length>0&&(l instanceof ue?l._text+=e:this._xmlText.delete(i,e.length))}else t._text=Be(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 ue?e._text=Be(e._text,t,0,l):this._xmlText.delete(t,l.length),o+=l.length}else{const t=l;if(void 0===je(t))continue;const{node:i,nodeIndex:r,length:c}=We(this,o,!1),a=Le(e,t,this);if(i instanceof ue&&c>0&&c<i._text.length){const e=i._text,t=e.length-c;i._text=Be(e,t,c,""),n.splice(r+1,0,a),s=Be(e,0,t,"")}else n.splice(r,0,a);null!==s&&a instanceof ue&&(a._text=s+a._text,s=null),o+=1}}}}syncChildrenFromYjs(e){const t=this.getNode();if(null===t)return;const n=t.__key,s=l(t,null),i=s.length,r=this._children,f=r.length,u=e.collabNodeMap,d=new Set;let h,p,g=0,y=null;f!==i&&(p=t.getWritable());for(let i=0;i<f;i++){const l=s[g],_=r[i],m=_.getNode(),x=_._key;if(null!==m&&l===x){const t=o(m);if(d.add(l),t)if(_._key=l,_ instanceof he){const t=_._xmlText;_.syncPropertiesFromYjs(e,null),_.applyChildrenYjsDelta(e,t.toDelta()),_.syncChildrenFromYjs(e)}else _ instanceof ue?_.syncPropertiesAndTextFromYjs(e,null):_ instanceof le?_.syncPropertiesFromYjs(e,null):_ instanceof ae||ie(93);y=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=ze(e,_,n),s=o.__key;if(u.set(s,_),null===y){const e=p.getFirstChild();if(p.__first=s,null!==e){const t=e.getWritable();t.__prev=s,o.__next=t.__key}}else{const e=y.getWritable(),t=y.getNextSibling();if(e.__next=s,o.__prev=y.__key,null!==t){const e=t.getWritable();e.__prev=s,o.__next=e.__key}}i===f-1&&(p.__last=s),p.__size++,y=o}}for(let t=0;t<i;t++){const n=s[t];if(!d.has(n)){const t=c(n),o=e.collabNodeMap.get(n);void 0!==o&&o.destroy(e),a(t)}}this.syncSlotsFromYjs(e,t)}syncSlotsFromYjs(e,t){Je(e,this._xmlText,t,this)}syncPropertiesFromLexical(e,t,n){$e(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(t,n,s,i,l,a){const f=this._children[n],u=c(s);f instanceof he&&r(u)?(f.syncPropertiesFromLexical(t,u,i),f.syncChildrenFromLexical(t,u,i,l,a),f.syncSlotsFromLexical(t,u,i,l,a)):f instanceof ue&&o(u)?f.syncPropertiesAndTextFromLexical(t,u,i):f instanceof le&&e(u)&&(f.syncPropertiesFromLexical(t,u,i),f.syncSlotsFromLexical(t,u,i,l,a))}syncSlotsFromLexical(e,t,n,o,s){Ve(e,this._xmlText,t,n,o,s,this)}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,u=a.length-1,d=e.collabNodeMap;let h,p,g=0,y=0;for(;g<=f&&y<=u;){const t=r[g],i=a[y];if(t===i)this._syncChildFromLexical(e,y,i,n,o,s),g++,y++;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=Pe(e,c(i),this);d.set(i,t),o?(this.splice(e,y,1,t),g++,y++):(this.splice(e,y,0,t),y++)}else this.splice(e,y,1),g++}}const _=g>f,m=y>u;if(_&&!m)for(;y<=u;++y){const t=a[y],n=Pe(e,c(t),this);this.append(n),d.set(t,n)}else if(m&&!_)for(let t=this._children.length-1;t>=y;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 he)t.insertEmbed(s,e._xmlText);else if(e instanceof ue){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof ae?t.insertEmbed(s,e._map):e instanceof le&&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(void 0!==o&&this.append(o));const r=i.getOffset();-1===r&&ie(95);const l=this._xmlText;if(0!==n){const o=s.slice(t,t+n);for(let t=0;t<o.length;t++)o[t].destroy(e);l.delete(r,i.getSize())}if(o instanceof he)l.insertEmbed(r,o._xmlText);else if(o instanceof ue){const e=o._map;null===e.parent&&l.insertEmbed(r,e),l.insert(r+1,o._text)}else o instanceof ae?l.insertEmbed(r,o._map):o instanceof le&&l.insertEmbed(r,o._xmlElem);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);Ge(e,this._xmlText),t.get(this._key)===this&&t.delete(this._key)}}function pe(e,t,n){const o=new he(e,t,n);return e._collabNode=o,o}class ge{_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 P){if(n||ie(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&&ie(332),o(t)&&ie(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 P){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 ye(e,t,n,o,s){const i={clientID:n.clientID,cursorHighlightSheet:null,cursors:new Map,cursorsContainer:null,doc:n,docMap:o,editor:e,excludedProperties:s||new Map,id:t,isBootstrapping:!1,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))Ae(t,o,e)||(s[t]=n);n.set(o.__type,Object.freeze(s))})})}(i),i}function _e({editor:e,id:t,doc:n,docMap:o,excludedProperties:s,rootName:i="root",getXmlText:r}){const l=r?r(n):n.get(i,P);l instanceof P&&l.doc===n||ie(401);const c=pe(l,null,"root");return c._key="root",{...ye(e,t,n,o,s),collabNodeMap:new Map,root:c}}function me(e,t,n,o,s,i){return null==o&&ie(81),_e({doc:o,docMap:s,editor:e,excludedProperties:i,id:n})}function xe(e,t,n,o,s={}){null==n&&ie(81);const{excludedProperties:i,rootName:r="root-v2",getXmlElement:l}=s,c=l?l(n):n.get(r,j);return c instanceof j&&c.doc===n||ie(402),c.nodeName!==Te&&ie(403,c.nodeName,Te),{...ye(e,t,n,o,i),mapping:new ge,root:c}}function Se(e){return Object.prototype.hasOwnProperty.call(e,"collabNodeMap")}const be="__slots",Te="UNDEFINED",Ne=new Set(["__key","__parent","__next","__prev","__state","__slotHost","__slots"]),ke=new Set(["__first","__last","__size"]),ve=new Set(["__cachedText","__textFormat","__textStyle"]),we=new Set(["__text"]);function Ce(e,t){e.setAttribute(be,t)}function Fe(e){return"__proto__"===e||"constructor"===e||"prototype"===e}function Me(t){return(r(t)||e(t))&&!t.isInline()}function Ae(e,t,n){if(Ne.has(e)||"function"==typeof t[e])return!0;if(o(t)){if(we.has(e))return!0}else if(r(t)&&(ke.has(e)||y(t)&&ve.has(e)))return!0;const s=t.constructor,i=n.excludedProperties.get(s);return null!=i&&i.has(e)}function Oe(e,t){const n=e.__type,{nodeProperties:o}=t,s=o.get(n);return void 0===s&&ie(330,n),s}function Ee(t,n,s,i,l,c){n instanceof he&&r(s)?(n.syncPropertiesFromLexical(t,s,i),n.syncChildrenFromLexical(t,s,i,l,c),n.syncSlotsFromLexical(t,s,i,l,c)):n instanceof ue&&o(s)?n.syncPropertiesAndTextFromLexical(t,s,i):n instanceof le&&e(s)&&(n.syncPropertiesFromLexical(t,s,i),n.syncSlotsFromLexical(t,s,i,l,c))}function Ke(e,t,n,o){const s=d(t),i=m(t.constructor).length>0;if(0===s.length&&!i)return;const r=new L;let l=!1;for(const o of s){const s=h(t,o);if(null===s)continue;const i=Pe(e,s,n);e.collabNodeMap.set(s.__key,i),r.set(o,i.getSharedType()),l=!0}(l||i)&&Ce(o,r)}function Pe(t,s,i){const l=s.__type;let c;if(r(s)){const e=new P;c=pe(e,i,l),c.syncPropertiesFromLexical(t,s,null),c.syncChildrenFromLexical(t,s,null,null,null),Ke(t,s,c,e)}else if(o(s))i instanceof he||ie(389),c=de(new L,s.__text,i,l),c.syncPropertiesAndTextFromLexical(t,s,null);else if(n(s)){i instanceof he||ie(390);const e=new L;e.set("__type","linebreak"),c=fe(e,i)}else if(e(s)){const e=new j;c=ce(e,i,l),c.syncPropertiesFromLexical(t,s,null),Ke(t,s,c,e)}else ie(86);return c._key=s.__key,c}function je(e){const t=De(e,"__type");return"string"!=typeof t&&void 0!==t&&ie(87),t}function Le(e,t,n){const o=t._collabNode;if(void 0===o){const o=e.editor._nodes,s=je(t);"string"!=typeof s&&ie(87),void 0===o.get(s)&&ie(88,s);const i=t.parent,r=void 0===n&&null!==i?Le(e,i):n||null;if(r instanceof he||r instanceof le&&function(e,t){const n=e._xmlElem.getAttribute(be);if(!(n instanceof L))return!1;for(const e of n.values())if(e===t)return!0;return!1}(r,t)||ie(391),t instanceof P)return pe(t,r,s);if(t instanceof L)return r instanceof he||ie(392),"linebreak"===s?fe(t,r):de(t,"",r,s);if(t instanceof j)return ce(t,r,s)}return o}function ze(t,n,o){const s=n.getType(),i=t.editor._nodes.get(s);void 0===i&&ie(88,s);const r=new i.klass;if(r.__parent=o,n._key=r.__key,n instanceof he){const e=n._xmlText;n.syncPropertiesFromYjs(t,null),n.applyChildrenYjsDelta(t,e.toDelta()),n.syncChildrenFromYjs(t)}else n instanceof ue?n.syncPropertiesAndTextFromYjs(t,null):n instanceof le&&(n.syncPropertiesFromYjs(t,null),e(r)||ie(393),n.syncSlotsFromYjs(t,r));return t.collabNodeMap.set(r.__key,n),r}function Ye(e,t,n,o){const s=null===o?t instanceof L?Array.from(t.keys()):t instanceof P||t instanceof j?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(Ae(r,n,e)){"__state"===r&&Se(e)&&(i||(i=n.getWritable()),Re(t,i));continue}const l=n[r];let c=De(t,r);if(l!==c){if(c instanceof z){const t=e.docMap;l instanceof z&&t.delete(l.guid);const n=c.guid;if(t.set(n,c),f(l))l._key=n,c=l;else{const e=u();e._key=n,c=e}}void 0===i&&(i=n.getWritable()),i[r]=c}}}function De(e,t){return e instanceof L?e.get(t):e instanceof P||e instanceof j?e.getAttribute(t):e[t]}function Ie(e,t,n){e instanceof L?e.set(t,n):e.setAttribute(t,n)}function Re(e,t){const n=De(e,"__state");n instanceof L&&_(t).updateFromJSON(n.toJSON())}function $e(e,t,n,o){const s=Object.keys(Oe(o,e)),i=e.editor.constructor;!function(e,t,n,o){const s=o.__state,i=null===t.doc?void 0:De(t,"__state");if(!s)return;const[r,l]=s.getInternalState(),c=n&&n.__state,a=i instanceof L?i:new L;if(c===s)return;const[f,u]=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)u.get(e)!==t&&a.set(e.key,e.unparse(t));i||Ie(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 z,r=s.guid;a._key=r,t.set(r,s),a=s,e.editor.update(()=>{o.markDirty()})}Ie(t,l,a)}}}function Be(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<r;s++){const e=i[s],l=o;if(o+=e.getSize(),(n?o>=t:o>t)&&e instanceof ue){let n=t-l-1;return n<0&&(n=0),{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 Ue(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 He(e,t){e.doc.transact(t,e)}function qe(e,n){const o=n._nodeMap.get(e);if(!o)return void x().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():qe(i.__key,n):x().selectStart()}function Je(n,o,s,i){const l=o.getAttribute(be),c=l instanceof L?new Set(l.keys()):new Set;for(const e of d(s))if(!c.has(e)){const t=h(s,e);if(null!==t){const e=n.collabNodeMap.get(t.__key);void 0!==e&&e.destroy(n)}p(s,e)}if(l instanceof L)for(const[o,c]of l.entries()){if(Fe(o))continue;if(!(c instanceof P||c instanceof j||c instanceof L))continue;const l=h(s,o),a=null===l?void 0:n.collabNodeMap.get(l.__key);if(void 0!==a&&a.getSharedType()===c)continue;if(void 0===c._collabNode&&"string"!=typeof De(c,"__type"))continue;if(c instanceof L&&i instanceof le)continue;const f=c._collabNode;if(void 0!==f){const n=t(f._key);if(null!==n&&(n.isAttached()||(r(n)||e(n))&&null!==n.getLatest().__slotHost))continue}const u=Le(n,c,i),d=ze(n,u,null);Me(d)?(void 0!==a&&a.destroy(n),g(s,o,d)):n.collabNodeMap.get(d.__key)===u&&n.collabNodeMap.delete(d.__key)}}function Ve(e,t,n,o,s,i,r){const l=d(n),c=t.getAttribute(be);if(0===l.length&&!(c instanceof L))return;let a;c instanceof L?a=c:(a=new L,Ce(t,a));const f=new Set(l);for(const t of Array.from(a.keys()))if(!f.has(t)){const n=a.get(t),o=null==n?void 0:n._collabNode;void 0!==o&&o.destroy(e),a.delete(t)}const u=e.collabNodeMap;for(const t of l){const l=h(n,t);if(null===l)continue;const c=u.get(l.__key);if(void 0!==c&&a.get(t)===c.getSharedType())Ee(e,c,l,o,s,i);else{const n=a.get(t),o=null==n?void 0:n._collabNode;void 0!==o&&o.destroy(e);const s=Pe(e,l,r);u.set(l.__key,s),a.set(t,s.getSharedType())}}}function Ge(e,t){const n=t.getAttribute(be);if(n instanceof L)for(const t of n.keys()){const o=n.get(t),s=null==o?void 0:o._collabNode;void 0!==s&&s.destroy(e)}}const Xe=e=>e.nodeName===Te,Qe=(t,n,o,s,i,r,l)=>{let c=n.mapping.get(t);if(c&&o&&0===o.size&&!s)return c;const a=Xe(t)?S.getType():t.nodeName,f=n.editor._nodes.get(a);if(void 0===f)throw new Error(`$createOrUpdateNodeFromYElement: Node ${a} is not registered`);if(c||(c=new f.klass,o=null,s=!0),s&&c instanceof b){const e=[],o=t=>{if(t instanceof j){const o=Qe(t,n,new Set,!1,i,r,l);null!==o&&e.push(o)}else if(t instanceof P){const o=tt(t,n,i,r,l);null!==o&&o.forEach(t=>{null!==t&&e.push(t)})}else ie(329)};void 0===i||void 0===r?t.toArray().forEach(o):Y(t,new D(r.ds,i.sv)).filter(e=>!e._item.deleted||et(e._item,i)||et(e._item,r)).forEach(o),Ze(c,e)}const u=t.getAttributes(i);Xe(t)||void 0===i||(et(t._item,i)?et(t._item,r)||(u[mt("ychange")]=l?l("added",t._item.id):{type:"added"}):u[mt("ychange")]=l?l("removed",t._item.id):{type:"removed"});const y={...Oe(c,n)},m={};for(const e in u)e.startsWith(_t)?m[xt(e)]=u[e]:e!==be&&"__proto__"!==e&&"constructor"!==e&&"prototype"!==e&&(y[e]=u[e]);if(Ye(n,y,c,o),o){if(o.size>0){const e=_(c);for(const t of o)if(t.startsWith(_t)){const n=xt(t);e.updateFromUnknown(n,m[n])}}}else _(c).updateFromJSON(m);if(c instanceof b||e(c)){const e=u[be],t=new Set;if(e instanceof L){const o=void 0===i?Array.from(e.entries()):Object.entries(I(e,i));for(const[e,s]of o){if(t.add(e),Fe(e))continue;let o=null;if(s instanceof j&&(o=Qe(s,n,new Set,!1,i,r,l)),null===o)continue;const a=h(c,e);null!==a&&a.getKey()===o.getKey()||(Me(o)?null===o.getLatest().__slotHost&&g(c,e,o):o.isAttached()||ot(s,n))}}for(const e of d(c))t.has(e)||p(c,e)}const x=c.getLatest();return n.mapping.set(t,x),x},Ze=(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 u=t[c];f?(e.splice(c,1,[u]),l++,c++):(e.splice(c,0,[u]),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,[])},et=(e,t)=>void 0===t?!e.deleted:t.sv.has(e.id.client)&&t.sv.get(e.id.client)>e.id.clock&&!R(t.ds,e.id),tt=(e,t,n,s,i)=>{const r=gt(e,n,s,i);let l=t.mapping.get(e)??[];const c=r.map(e=>e.attributes.t??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={...Oe(n,t),...s.p},a=Object.fromEntries(Object.entries(s).filter(([e])=>e.startsWith(_t)).map(([e,t])=>[xt(e),t]));Ye(t,c,n,null),_(n).updateFromJSON(a)}const a=l.map(e=>e.getLatest());return t.mapping.set(e,a),a},nt=(e,t)=>{const n=d(e);if(0===n.length&&0===m(e.constructor).length)return;const o=new L;for(const s of n){const n=h(e,s);null!=n&&o.set(s,it(n,t))}return o},ot=(e,t)=>{if((e instanceof j||e instanceof P)&&t.mapping.delete(e),e instanceof j){const n=e.getAttribute(be);if(n instanceof L)for(const e of n.values())ot(e,t);for(const n of e.toArray())ot(n,t)}},st=(e,t)=>{const n=new j(e.getType()),o={...yt(e,t),...St(e)};for(const e in o){const t=o[e];null!==t&&n.setAttribute(e,t)}if(!(e instanceof b)){const o=nt(e,t);return void 0!==o&&(Ce(n,o),t.mapping.set(n,e)),n}const s=nt(e,t);return void 0!==s&&Ce(n,s),n.insert(0,at(e).map(e=>rt(e,t))),t.mapping.set(n,e),n},it=(t,n)=>{const o=st(t,n);return e(t)&&void 0===n.mapping.getSharedType(t)&&n.mapping.set(o,t),o},rt=(e,t)=>e instanceof Array?((e,t)=>{const n=new P;return pt(n,e,t),n})(e,t):st(e,t),lt=e=>"object"==typeof e&&null!=e,ct=(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||lt(r)&&lt(l)&&ct(r,l)}return o},at=e=>{if(!(e instanceof b))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},ft=(e,t,n)=>{const o=gt(e);return o.length===t.length&&o.every((e,o)=>{const s=t[o],i=e.attributes.t??T.getType(),r=e.attributes.p??{},l=Object.fromEntries(Object.entries(e.attributes).filter(([e])=>e.startsWith(_t)));return e.insert===s.getTextContent()&&i===s.getType()&&ct(r,yt(s,n))&&ct(l,St(s))})},ut=(e,t,n)=>{if(e instanceof j&&!(t instanceof Array)&&Tt(e,t)){const o=at(t),s=e.getAttributes();return delete s[be],e._length===o.length&&ct(s,{...yt(t,n),...St(t)})&&((e,t,n)=>{const o=d(t),s=e.getAttribute(be);if(!(s instanceof L))return 0===o.length;if(s.size!==o.length)return!1;for(const e of o){const o=h(t,e),i=s.get(e);if(null===o||!(i instanceof j)||n.mapping.get(i)!==o)return!1}return!0})(e,t,n)&&e.toArray().every((e,t)=>ut(e,o[t],n))}return e instanceof P&&t instanceof Array&&ft(e,t,n)},dt=(e,t)=>e===t||e instanceof Array&&t instanceof Array&&e.length===t.length&&e.every((e,n)=>t[n]===e),ht=(e,t,n)=>{const o=e.toArray(),s=at(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 $)break;if(dt(n.mapping.get(e),t))f=!0;else if(!ut(e,t,n))break}for(;c+a<l;a++){const e=o[r-a-1],t=s[i-a-1];if(e instanceof $)break;if(dt(n.mapping.get(e),t))f=!0;else if(!ut(e,t,n))break}return{equalityFactor:c+a,foundMappedChild:f}},pt=(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 B?t+=n.content.str:n.content instanceof W&&(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=yt(e,n);return 0===Object.keys(i).length&&(i=null),{attributes:Object.assign({},o,{...s!==T.getType()&&{t:s},p:i,...St(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:u,remove:d,index:h}=re(r,c,f);e.delete(h,d),e.insert(h,u),e.applyDelta(l.map(e=>({attributes:e.attributes,retain:e.insert.length})))},gt=(e,t,n,o)=>e.toDelta(t,n,o).map(e=>{const t=e.attributes??{};return"ychange"in t&&(t[mt("ychange")]=t.ychange,delete t.ychange),{...e,attributes:t}}),yt=(e,t)=>{const n=Oe(e,t),o={};return Object.entries(n).forEach(([t,n])=>{const s=e[t];s!==n&&(o[t]=s)}),o},_t="s_",mt=e=>`s_${e}`,xt=e=>{if(!e.startsWith(_t))throw new Error(`Invalid state key: ${e}`);return e.slice(_t.length)},St=e=>{const t=e.__state;if(!t)return{};const[n={},o]=t.getInternalState(),s={};for(const[e,t]of Object.entries(n))s[mt(e)]=t;for(const[e,t]of o)s[mt(e.key)]=e.unparse(t);return s},bt=(t,n,o,s,i)=>{if(n instanceof j&&n.nodeName!==o.getType()&&(!Xe(n)||o.getType()!==S.getType()))throw new Error("node name mismatch!");if(s.mapping.set(n,o),n instanceof j){const r=n.getAttributes(),l={...yt(o,s),...St(o)};for(const e in l)null!=l[e]?r[e]===l[e]||lt(r[e])&&lt(l[e])&&ct(r[e],l[e])||"ychange"===e||n.setAttribute(e,l[e]):n.removeAttribute(e);for(const e in r)e!==be&&void 0===l[e]&&n.removeAttribute(e);(o instanceof b||e(o))&&((t,n,o,s,i)=>{const r=d(n),l=t.getAttribute(be);if(0===r.length&&!(l instanceof L)&&0===m(n.constructor).length)return;let c;l instanceof L?c=l:(c=new L,Ce(t,c));const a=new Set(r);for(const e of Array.from(c.keys()))if(!a.has(e)){const t=c.get(e);void 0!==t&&ot(t,o),c.delete(e)}for(const t of r){const r=h(n,t);if(null==r)continue;const l=c.get(t);l instanceof j&&(r instanceof b||e(r))&&l===o.mapping.getSharedType(r)?s.has(r.getKey())&&bt(i,l,r,o,s):(l instanceof j&&ot(l,o),c.set(t,it(r,o)))}})(n,o,s,i,t)}const r=at(o),l=r.length,c=n.toArray(),a=c.length,f=Math.min(l,a);let u=0,p=0;for(;u<f;u++){const n=c[u],o=r[u];if(n instanceof $)break;if(dt(s.mapping.get(n),o))o instanceof Array||!(o instanceof b||e(o))||!i.has(o.getKey())||bt(t,n,o,s,i);else{if(!ut(n,o,s))break;s.mapping.set(n,o)}}for(;p+u<f;p++){const n=c[a-p-1],o=r[l-p-1];if(n instanceof $)break;if(dt(s.mapping.get(n),o))o instanceof Array||!(o instanceof b||e(o))||!i.has(o.getKey())||bt(t,n,o,s,i);else{if(!ut(n,o,s))break;s.mapping.set(n,o)}}for(;a-u-p>0&&l-u-p>0;){const e=c[u],o=r[u],f=c[a-p-1],d=r[l-p-1];if(e instanceof P&&o instanceof Array)ft(e,o,s)||pt(e,o,s),u+=1;else{let r=e instanceof j&&Tt(e,o),l=f instanceof j&&Tt(f,d);if(r&&l){const t=ht(e,o,s),n=ht(f,d,s);t.foundMappedChild&&!n.foundMappedChild?l=!1:!t.foundMappedChild&&n.foundMappedChild||t.equalityFactor<n.equalityFactor?r=!1:l=!1}r?(bt(t,e,o,s,i),u+=1):l?(bt(t,f,d,s,i),p+=1):(s.mapping.delete(n.get(u)),n.delete(u,1),n.insert(u,[rt(o,s)]),u+=1)}}const g=a-u-p;if(1===a&&0===l&&c[0]instanceof P?(s.mapping.delete(c[0]),c[0].delete(0,c[0].length)):g>0&&(n.slice(u,u+g).forEach(e=>s.mapping.delete(e)),n.delete(u,g)),u+p<l){const e=[];for(let t=u;t<l-p;t++)e.push(rt(r[t],s));n.insert(u,e)}},Tt=(e,t)=>!(t instanceof Array)&&e.nodeName===t.getType(),Nt=/* @__PURE__ */k("ychange",{isEqual:(e,t)=>e===t,parse:e=>e??null});function kt(e){return N(e,Nt)}const vt=(e,t=J(e.doc),n=U)=>{const{doc:o}=e;o.gc&&ie(325),o.transact(s=>{const i=new H(o);i&&i.dss.forEach(e=>{q(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(()=>{x().clear(),Qe(e.root,e,null,!0,t,n,r)})},e)};function wt(){return"undefined"!=typeof Highlight&&"undefined"!=typeof CSS&&"highlights"in CSS}const Ct=/* @__PURE__ */wt();function Ft(e,t){const n=e.cursorHighlightSheet;if(null===n)return;const o=`::highlight(${t})`;for(let e=n.cssRules.length-1;e>=0;e--){const t=n.cssRules[e];if(null!=t&&t.selectorText===o)return void n.deleteRule(e)}}function Mt(e,t,n=0){const s=t.collabNodeMap.get(e.key);if(void 0===s)return null;let i=e.offset,l=s.getSharedType();if(s instanceof ue){l=s._parent._xmlText;const e=s.getOffset();if(-1===e)return null;i=e+1+i}else if(s instanceof he&&"element"===e.type){const t=e.getNode();r(t)||ie(184);let n=0,s=0,l=t.getFirstChild();for(;null!==l&&s++<i;)o(l)?n+=l.getTextContentSize()+1:n++,l=l.getNextSibling();i=n}return G(l,i,n)}function At(e,t,n=0){const{mapping:s}=t,{offset:i}=e,l=e.getNode(),c=s.getSharedType(l);if(void 0===c)return null;if("text"===e.type){o(l)||ie(326);let e=l.getPreviousSibling(),t=i;for(;o(e);)t+=e.getTextContentSize(),e=e.getPreviousSibling();return G(c,t,n)}if("element"===e.type){r(l)||ie(184);let e=0,t=0,s=l.getFirstChild();for(;null!==s&&t<i;){let n=s.getNextSibling();if(t++,o(s))for(;o(n);)n=n.getNextSibling(),t++;e++,s=n}return G(c,e,n)}return null}function Ot(e,t){return V(e,t.doc)}function Et(e,t){if(null==e){if(null!=t)return!0}else if(null==t||!X(e,t))return!0;return!1}function Kt(e,t){return{color:t,name:e,selection:null}}function Pt(e,t){null!==t.highlight&&(CSS.highlights.delete(t.highlightName),Ft(e,t.highlightName));const n=e.cursorsContainer;if(null===n)return;t.caret.parentNode===n&&n.removeChild(t.caret);const o=t.selections;for(let e=0;e<o.length;e++)o[e].parentNode===n&&n.removeChild(o[e])}function jt(e,t){const n=t.selection;null!==n&&Pt(e,n)}function Lt(e,t,n,o,s,i,r,l,c={}){const a=e.color,f=v(t.editor.getRootElement()),u=f.createElement("span");c.cursor?(u.className=c.cursor,w(u.style,{"--lexical-cursor-color":a,bottom:"0",position:"absolute",right:"-1px",top:"0"})):w(u.style,{"background-color":a,bottom:"0",position:"absolute",right:"-1px",top:"0",width:"1px","z-index":"10"});const d=f.createElement("span");d.textContent=e.name,c.cursorName?d.className=c.cursorName:w(d.style,{"background-color":a,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"}),u.appendChild(d);const h=`lexical-cursor-${t.id}-${n}`;let p=null;return l&&Ct&&(p=new Highlight,CSS.highlights.set(h,p),function(e,t,n){if(!CSS.supports("color",n))return;const o=function(e){const t=e.editor.getRootElement(),n=v(t);let o=e.cursorHighlightSheet;null===o&&(o=new(n.defaultView||window).CSSStyleSheet,e.cursorHighlightSheet=o);const s=null!==t?t.getRootNode():null,i=C(s)?s:n;return i.adoptedStyleSheets.includes(o)||(i.adoptedStyleSheets=[...i.adoptedStyleSheets,o]),o}(e),s=o.insertRule(`::highlight(${t}) { }`,o.cssRules.length),i=o.cssRules[s];i.style.setProperty("background-color",`color-mix(in srgb, ${n} 30%, transparent)`),i.style.setProperty("color","inherit")}(t,h,a)),{anchor:{key:o,offset:s},caret:u,color:a,focus:{key:i,offset:r},highlight:p,highlightName:h,name:d,selections:[]}}function zt(e,t,o,s,i={}){const r=e.editor,l=r.getRootElement(),c=e.cursorsContainer;if(null===c||null===l)return;const a=v(l),f=c.offsetParent;if(null===f)return;const u=f.getBoundingClientRect(),d=t.selection;if(null===o)return null===d?void 0:(t.selection=null,void Pt(e,d));t.selection=o;const h=o.caret,p=o.color,g=o.highlight,y=o.anchor,_=o.focus,m=y.key,x=_.key,S=s.get(m),b=s.get(x);if(null==S||null==b)return;if(null!==g){const e=oe(r,S,y.offset,b,_.offset);if(null===e)return;g.clear(),e.collapsed||g.add(e);const t=e.cloneRange();t.collapse(!1);let o=t.getBoundingClientRect();if(0===o.height&&n(b)){const e=r.getElementByKey(x);null!==e&&(o=e.getBoundingClientRect())}return w(h.style,{"background-color":i.cursor?"":p,bottom:"",height:`${o.height||16}px`,left:o.left-u.left+"px","pointer-events":"none",position:"absolute",right:"",top:o.top-u.top+"px",width:"1px","z-index":"10"}),void(h.parentNode!==c&&c.appendChild(h))}const T=o.selections;let N;if(S===b&&n(S))N=[r.getElementByKey(m).getBoundingClientRect()];else{const e=oe(r,S,y.offset,b,_.offset);if(null===e)return;N=se(r,e)}const k=T.length,C=N.length;for(let e=0;e<C;e++){const t=N[e];let n=T[e];if(void 0===n){n=a.createElement("span"),T[e]=n;const t=a.createElement("span");i.selectionBg&&(t.className=i.selectionBg),n.appendChild(t),c.appendChild(n)}const o=t.top-u.top,s=t.left-u.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,w(n.style,{...r,"--lexical-cursor-color":p}),w(n.firstChild.style,{height:"100%",left:"0",position:"absolute",top:"0",width:"100%"})):(w(n.style,r),w(n.firstChild.style,{...r,"background-color":p,left:"0",opacity:"0.3",top:"0","z-index":"5"})),e===C-1&&h.parentNode!==n&&n.appendChild(h)}for(let e=k-1;e>=C;e--){const t=T[e];c.removeChild(t),T.pop()}}function Yt(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]=$t(e,t.type,t.index),[r,l]=$t(e,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(Se(e)){const[t,n]=$t(e,i.type,i.index),[o,s]=$t(e,r.type,r.index);return{anchorKey:null!==t?t.getKey():null,anchorOffset:n,focusKey:null!==o?o.getKey():null,focusOffset:s}}let[l,c]=Bt(e.mapping,i),[a,f]=Bt(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 It(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;Rt(e.anchor,o,r),Rt(e.focus,l,c)}}function Rt(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 $t(e,t,n){const o=t._collabNode;if(void 0===o||!function(e,t){const n=e.root.getSharedType();let o=t;for(;null!=o;){if(o===n)return!0;o=o.parent}return!1}(e,t))return[null,0];if(o instanceof he){const{node:e,offset:t}=We(o,n,!0);return null===e?[o,o._children.length]:[e,t]}return[null,0]}function Bt(e,t){const n=t.type,s=t.index;if(n instanceof j){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 Wt(e,t){return t.awareness.getStates()}function Ut(e,t,n){const{getAwarenessStates:o=Wt,selectionHighlight:s=!1}=n??{},i=Array.from(o(e,t)),r=e.clientID,l=e.cursors,c=e.editor,a=c._config.theme.collaboration,f=c._editorState._nodeMap,u=new Set;for(let t=0;t<i.length;t++){const n=i[t],[o,d]=n;if(0!==o&&o!==r){u.add(o);const{name:t,color:n,focusing:i}=d;let r=null,h=l.get(o);if(void 0===h?(h=Kt(t,n),l.set(o,h)):h.name===t&&h.color===n||(jt(e,h),h.name=t,h.color=n,h.selection=null),i){const{anchorKey:t,anchorOffset:n,focusKey:i,focusOffset:l}=c.read(()=>Dt(e,d));if(null!==t&&null!==i)if(r=h.selection,null===r)r=Lt(h,e,o,t,n,i,l,s,a);else{const e=r.anchor,o=r.focus;e.key=t,e.offset=n,o.key=i,o.offset=l}}zt(e,h,r,f,a)}}const d=Array.from(l.keys());for(let t=0;t<d.length;t++){const n=d[t];if(!u.has(n)){const t=l.get(n);void 0!==t&&(jt(e,t),l.delete(n))}}}function Ht(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:u,awarenessData:d}=r;let h=null,p=null;if(null!==o&&(null===l||o.is(n))||null!==n){if(i(o)){const t=o.isCollapsed(),n=!t&&o.isBackward(),s=n?-1:0,i=t||n?0:-1;Se(e)?(h=Mt(o.anchor,e,s),p=Mt(o.focus,e,i)):(h=At(o.anchor,e,s),p=At(o.focus,e,i))}(Et(l,h)||Et(c,p))&&s.setLocalState({...r,anchorPos:h,awarenessData:d,color:f,focusPos:p,focusing:u,name:a})}}function qt(e,t){if(t instanceof Z&&function(e,t){const{target:n}=t;if(!n._item||"__state"!==n._item.parentSub||void 0!==je(n)||!(n.parent instanceof P||n.parent instanceof j||n.parent instanceof L))return!1;const o=Le(e,n.parent).getNode();if(o){const e=_(o.getWritable());for(const o of t.keysChanged)e.updateFromUnknown(o,n.get(o))}return!0}(e,t))return;const{target:n}=t;if(t instanceof Z&&n instanceof L&&null!=n._item&&n._item.parentSub===be&&(n.parent instanceof P||n.parent instanceof j)){const t=Le(e,n.parent);if(t instanceof he){const n=t.getNode();null!==n&&t.syncSlotsFromYjs(e,n)}else if(t instanceof le){const n=t.getNode();null!==n&&t.syncSlotsFromYjs(e,n)}return}const o=Le(e,n);if(o instanceof he&&t instanceof ee){const{keysChanged:n,childListChanged:s,delta:i}=t;if(n.size>0&&o.syncPropertiesFromYjs(e,n),n.has(be)){const t=o.getNode();null!==t&&o.syncSlotsFromYjs(e,t)}s&&(o.applyChildrenYjsDelta(e,i),o.syncChildrenFromYjs(e))}else if(o instanceof ue&&t instanceof Z){const{keysChanged:n}=t;n.size>0&&o.syncPropertiesAndTextFromYjs(e,n)}else if(o instanceof le&&t instanceof te){const{attributesChanged:n}=t;if(n.size>0&&o.syncPropertiesFromYjs(e,n),n.has(be)){const t=o.getNode();null!==t&&o.syncSlotsFromYjs(e,t)}}else ie(82)}function Jt(e,t,n,o,s=Ut){const i=e.editor,r=i._editorState;if(n.forEach(e=>e.delta),i.update(()=>{for(let t=0;t<n.length;t++){const o=n[t];qt(e,o)}Vt(r,e,t),o||A(O)},{onUpdate:()=>{s(e,t),e.root.isEmpty()&&i.update(()=>Gt())},skipTransforms:!0,tag:o?M:F}),n.length>0){const t=n[0].transaction;q(t,t.deleteSet,t=>{if(t.constructor===Q){const n=t.content.type;if(n){const t=n._collabNode;void 0!==t&&e.collabNodeMap.get(t._key)===t&&e.collabNodeMap.delete(t._key)}}})}}function Vt(e,t,n){const o=s();if(i(o))if(Ue(o)){const r=e._selection;i(r)&&(It(t,n),Ue(o))&&qe(o.anchor.key,e),Ht(t,n,r,s())}else It(t,n)}function Gt(){0===x().getChildrenSize()&&x().append(E())}function Xt(e,n,i,r,l,c,a,f){He(e,()=>{r.read(()=>{if(f.has(F)||f.has(M))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 ue)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=x(),o=e.root;o.syncPropertiesFromLexical(e,n,t),o.syncChildrenFromLexical(e,n,t,l,c),o.syncSlotsFromLexical(e,n,t,l,c),0===n.getChildrenSize()&&i._nodeMap.size>1&&e.editor.update(()=>Gt())}const r=s(),u=i._selection;Ht(e,n,u,r)})})}function Qt(e,t){const{target:n}=t;if(n instanceof j&&t instanceof te)Qe(n,e,t.attributesChanged,t.childListChanged);else if(n instanceof P&&t instanceof ee){const t=n.parent;t instanceof j?Qe(t,e,new Set,!0):ie(327)}else if(n instanceof L&&t instanceof Z&&null!=n._item&&n._item.parentSub===be){const t=n.parent;t instanceof j?Qe(t,e,new Set([be]),!1):ie(388)}else ie(328)}function Zt(e,t,n,o,s){const i=e.editor,r=i._editorState;q(o,o.deleteSet,t=>{if(t.constructor===Q){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];Qt(e,o)}Vt(r,e,t),s||A(O)},{discrete:!0,onUpdate:()=>{Ut(e,t),0===e.root.length&&i.update(()=>Gt())},skipTransforms:!0,tag:s?M:F})}function en(e,t){e.mapping.clear();const n=e.editor;n.update(()=>{x().clear(),Qe(e.root,e,null,!0),A(F)},{discrete:!0,onUpdate:()=>{Ut(e,t),0===e.root.length&&n.update(()=>Gt())},skipTransforms:!0,tag:F})}function tn(e,t,n,o,i,r,l,c){const a=c.has(F)||c.has(M);a&&0===l.size||(l.forEach(t=>{e.mapping.deleteNode(t)}),He(e,()=>{o.read(()=>{if(i.has("root")){const t=x();bt(e.doc,e.root,t,e,new Set([...i.keys(),...r])),!a&&0===t.getChildrenSize()&&n._nodeMap.size>1&&e.editor.update(()=>Gt())}const o=s(),l=n._selection;Ht(e,t,l,o)})}))}const nn=/* @__PURE__ */K("CONNECTED_COMMAND"),on=/* @__PURE__ */K("TOGGLE_CONNECT_COMMAND"),sn=/* @__PURE__ */K("DIFF_VERSIONS_COMMAND"),rn=/* @__PURE__ */K("CLEAR_DIFF_VERSIONS_COMMAND");function ln(e,t){return new ne(t,{captureTransaction:()=>!e.isBootstrapping,trackedOrigins:new Set([e,null])})}function cn(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})}function an(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{Dt as $getAnchorAndFocusForUserState,kt as $getYChangeState,rn as CLEAR_DIFF_VERSIONS_COMMAND__EXPERIMENTAL,nn as CONNECTED_COMMAND,sn as DIFF_VERSIONS_COMMAND__EXPERIMENTAL,on as TOGGLE_CONNECT_COMMAND,me as createBinding,xe as createBindingV2__EXPERIMENTAL,ln as createUndoManager,_e as createYjsBinding,Yt as getAnchorAndFocusCollabNodesForUserState,cn as initLocalState,Ft as removeCursorHighlightRule,vt as renderSnapshot__EXPERIMENTAL,an as setLocalStateFocus,Ut as syncCursorPositions,Xt as syncLexicalUpdateToYjs,tn as syncLexicalUpdateToYjsV2__EXPERIMENTAL,Jt as syncYjsChangesToLexical,Zt as syncYjsChangesToLexicalV2__EXPERIMENTAL,en as syncYjsStateToLexicalV2__EXPERIMENTAL};
9
+ 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,isLexicalEditor as f,createEditor as u,$getSlotNames as d,$getSlot as h,$removeSlot as g,$setSlot as p,$isRootNode as y,$getWritableNodeState as _,getDeclaredSlots as m,$getRoot as x,RootNode as S,ElementNode as b,TextNode as T,$getState as N,createState as k,getRootOwnerDocument as v,setDOMStyleObject as w,isDOMShadowRoot as C,COLLABORATION_TAG as F,HISTORIC_TAG as A,$addUpdateTag as M,SKIP_SCROLL_INTO_VIEW_TAG as O,$createParagraphNode as E,createCommand as K}from"lexical";import{XmlText as P,XmlElement as j,Map as z,Doc as L,typeListToArraySnapshot as Y,Snapshot as D,typeMapGetAllSnapshot as I,isDeleted as R,XmlHook as $,ContentString as B,ContentFormat as W,emptySnapshot as U,PermanentUserData as H,iterateDeletedStructs as q,snapshot as J,createAbsolutePositionFromRelativePosition as V,createRelativePositionFromTypeIndex as G,compareRelativePositions as X,Item as Q,YMapEvent as Z,YTextEvent as ee,YXmlEvent as te,UndoManager as ne}from"yjs";import{createDOMRange as oe,createRectsFromDOMRange as se}from"@lexical/selection";function ie(e,...t){throw function(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);return n.search=o.toString(),new 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.`)}(e,...t)}function re(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 le{_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 instanceof le&&ie(394),this._parent.getChildOffset(this)}syncPropertiesFromLexical(e,t,n){const o=this.getPrevNode(n);$e(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null!==n&&Ye(e,this._xmlElem,n,t)}syncSlotsFromYjs(e,t){Je(e,this._xmlElem,t,this)}syncSlotsFromLexical(e,t,n,o,s){Ve(e,this._xmlElem,t,n,o,s,this)}destroy(e){const t=e.collabNodeMap;Ge(e,this._xmlElem),t.get(this._key)===this&&t.delete(this._key)}}function ce(e,t,n){const o=new le(e,t,n);return e._collabNode=o,o}class ae{_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 fe(e,t){const n=new ae(e,t);return e._collabNode=n,n}class ue{_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($e(e,this._map,o,t),null!==o){const e=o.__text;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=re(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();if(null===n)return;Ye(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 de(e,t,n,o){const s=new ue(e,t,n,o);return e._collabNode=s,s}class he{_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 e instanceof he||ie(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null!==n&&Ye(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}=We(this,o,!1);if(t instanceof he||t instanceof ae||t instanceof le)n.splice(s,1),e-=1;else{if(!(t instanceof ue))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=Be(t._text,i,o-1,"");e.length>0&&(l instanceof ue?l._text+=e:this._xmlText.delete(i,e.length))}else t._text=Be(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 ue?e._text=Be(e._text,t,0,l):this._xmlText.delete(t,l.length),o+=l.length}else{const t=l;if(void 0===je(t))continue;const{node:i,nodeIndex:r,length:c}=We(this,o,!1),a=ze(e,t,this);if(i instanceof ue&&c>0&&c<i._text.length){const e=i._text,t=e.length-c;i._text=Be(e,t,c,""),n.splice(r+1,0,a),s=Be(e,0,t,"")}else n.splice(r,0,a);null!==s&&a instanceof ue&&(a._text=s+a._text,s=null),o+=1}}}}syncChildrenFromYjs(e){const t=this.getNode();if(null===t)return;const n=t.__key,s=l(t,null),i=s.length,r=this._children,f=r.length,u=e.collabNodeMap,d=new Set;let h,g,p=0,y=null;f!==i&&(g=t.getWritable());for(let i=0;i<f;i++){const l=s[p],_=r[i],m=_.getNode(),x=_._key;if(null!==m&&l===x){const t=o(m);if(d.add(l),t)if(_._key=l,_ instanceof he){const t=_._xmlText;_.syncPropertiesFromYjs(e,null),_.applyChildrenYjsDelta(e,t.toDelta()),_.syncChildrenFromYjs(e)}else _ instanceof ue?_.syncPropertiesAndTextFromYjs(e,null):_ instanceof le?_.syncPropertiesFromYjs(e,null):_ instanceof ae||ie(93);y=m,p++}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--,p++;continue}g=t.getWritable();const o=Le(e,_,n),s=o.__key;if(u.set(s,_),null===y){const e=g.getFirstChild();if(g.__first=s,null!==e){const t=e.getWritable();t.__prev=s,o.__next=t.__key}}else{const e=y.getWritable(),t=y.getNextSibling();if(e.__next=s,o.__prev=y.__key,null!==t){const e=t.getWritable();e.__prev=s,o.__next=e.__key}}i===f-1&&(g.__last=s),g.__size++,y=o}}for(let t=0;t<i;t++){const n=s[t];if(!d.has(n)){const t=c(n),o=e.collabNodeMap.get(n);void 0!==o&&o.destroy(e),a(t)}}this.syncSlotsFromYjs(e,t)}syncSlotsFromYjs(e,t){Je(e,this._xmlText,t,this)}syncPropertiesFromLexical(e,t,n){$e(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(t,n,s,i,l,a){const f=this._children[n],u=c(s);f instanceof he&&r(u)?(f.syncPropertiesFromLexical(t,u,i),f.syncChildrenFromLexical(t,u,i,l,a),f.syncSlotsFromLexical(t,u,i,l,a)):f instanceof ue&&o(u)?f.syncPropertiesAndTextFromLexical(t,u,i):f instanceof le&&e(u)&&(f.syncPropertiesFromLexical(t,u,i),f.syncSlotsFromLexical(t,u,i,l,a))}syncSlotsFromLexical(e,t,n,o,s){Ve(e,this._xmlText,t,n,o,s,this)}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,u=a.length-1,d=e.collabNodeMap;let h,g,p=0,y=0;for(;p<=f&&y<=u;){const t=r[p],i=a[y];if(t===i)this._syncChildFromLexical(e,y,i,n,o,s),p++,y++;else{void 0===h&&(h=new Set(r)),void 0===g&&(g=new Set(a));const n=g.has(t),o=h.has(i);if(n){const t=Pe(e,c(i),this);d.set(i,t),o?(this.splice(e,y,1,t),p++,y++):(this.splice(e,y,0,t),y++)}else this.splice(e,y,1),p++}}const _=p>f,m=y>u;if(_&&!m)for(;y<=u;++y){const t=a[y],n=Pe(e,c(t),this);this.append(n),d.set(t,n)}else if(m&&!_)for(let t=this._children.length-1;t>=y;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 he)t.insertEmbed(s,e._xmlText);else if(e instanceof ue){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof ae?t.insertEmbed(s,e._map):e instanceof le&&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(void 0!==o&&this.append(o));const r=i.getOffset();-1===r&&ie(95);const l=this._xmlText;if(0!==n){const o=s.slice(t,t+n);for(let t=0;t<o.length;t++)o[t].destroy(e);l.delete(r,i.getSize())}if(o instanceof he)l.insertEmbed(r,o._xmlText);else if(o instanceof ue){const e=o._map;null===e.parent&&l.insertEmbed(r,e),l.insert(r+1,o._text)}else o instanceof ae?l.insertEmbed(r,o._map):o instanceof le&&l.insertEmbed(r,o._xmlElem);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);Ge(e,this._xmlText),t.get(this._key)===this&&t.delete(this._key)}}function ge(e,t,n){const o=new he(e,t,n);return e._collabNode=o,o}class pe{_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 P){if(n||ie(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&&ie(332),o(t)&&ie(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 P){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 ye(e,t,n,o,s){const i={clientID:n.clientID,cursorHighlightSheet:null,cursors:new Map,cursorsContainer:null,doc:n,docMap:o,editor:e,excludedProperties:s||new Map,id:t,isBootstrapping:!1,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))Me(t,o,e)||(s[t]=n);n.set(o.__type,Object.freeze(s))})})}(i),i}function _e({editor:e,id:t,doc:n,docMap:o,excludedProperties:s,rootName:i="root",getXmlText:r}){const l=r?r(n):n.get(i,P);l instanceof P&&l.doc===n||ie(401);const c=ge(l,null,"root");return c._key="root",{...ye(e,t,n,o,s),collabNodeMap:new Map,root:c}}function me(e,t,n,o,s,i){return null==o&&ie(81),_e({doc:o,docMap:s,editor:e,excludedProperties:i,id:n})}function xe(e,t,n,o,s={}){null==n&&ie(81);const{excludedProperties:i,rootName:r="root-v2",getXmlElement:l}=s,c=l?l(n):n.get(r,j);return c instanceof j&&c.doc===n||ie(402),c.nodeName!==Te&&ie(403,c.nodeName,Te),{...ye(e,t,n,o,i),mapping:new pe,root:c}}function Se(e){return Object.prototype.hasOwnProperty.call(e,"collabNodeMap")}const be="__slots",Te="UNDEFINED",Ne=new Set(["__key","__parent","__next","__prev","__state","__slotHost","__slots"]),ke=new Set(["__first","__last","__size"]),ve=new Set(["__cachedText","__textFormat","__textStyle"]),we=new Set(["__text"]);function Ce(e,t){e.setAttribute(be,t)}function Fe(e){return"__proto__"===e||"constructor"===e||"prototype"===e}function Ae(t){return(r(t)||e(t))&&!t.isInline()}function Me(e,t,n){if(Ne.has(e)||"function"==typeof t[e])return!0;if(o(t)){if(we.has(e))return!0}else if(r(t)&&(ke.has(e)||y(t)&&ve.has(e)))return!0;const s=t.constructor,i=n.excludedProperties.get(s);return null!=i&&i.has(e)}function Oe(e,t){const n=e.__type,{nodeProperties:o}=t,s=o.get(n);return void 0===s&&ie(330,n),s}function Ee(t,n,s,i,l,c){n instanceof he&&r(s)?(n.syncPropertiesFromLexical(t,s,i),n.syncChildrenFromLexical(t,s,i,l,c),n.syncSlotsFromLexical(t,s,i,l,c)):n instanceof ue&&o(s)?n.syncPropertiesAndTextFromLexical(t,s,i):n instanceof le&&e(s)&&(n.syncPropertiesFromLexical(t,s,i),n.syncSlotsFromLexical(t,s,i,l,c))}function Ke(e,t,n,o){const s=d(t),i=m(t.constructor).length>0;if(0===s.length&&!i)return;const r=new z;let l=!1;for(const o of s){const s=h(t,o);if(null===s)continue;const i=Pe(e,s,n);e.collabNodeMap.set(s.__key,i),r.set(o,i.getSharedType()),l=!0}(l||i)&&Ce(o,r)}function Pe(t,s,i){const l=s.__type;let c;if(r(s)){const e=new P;c=ge(e,i,l),c.syncPropertiesFromLexical(t,s,null),c.syncChildrenFromLexical(t,s,null,null,null),Ke(t,s,c,e)}else if(o(s))i instanceof he||ie(389),c=de(new z,s.__text,i,l),c.syncPropertiesAndTextFromLexical(t,s,null);else if(n(s)){i instanceof he||ie(390);const e=new z;e.set("__type","linebreak"),c=fe(e,i)}else if(e(s)){const e=new j;c=ce(e,i,l),c.syncPropertiesFromLexical(t,s,null),Ke(t,s,c,e)}else ie(86);return c._key=s.__key,c}function je(e){const t=De(e,"__type");return"string"!=typeof t&&void 0!==t&&ie(87),t}function ze(e,t,n){const o=t._collabNode;if(void 0===o){const o=e.editor._nodes,s=je(t);"string"!=typeof s&&ie(87),void 0===o.get(s)&&ie(88,s);const i=t.parent,r=void 0===n&&null!==i?ze(e,i):n||null;if(r instanceof he||r instanceof le&&function(e,t){const n=e._xmlElem.getAttribute(be);if(!(n instanceof z))return!1;for(const e of n.values())if(e===t)return!0;return!1}(r,t)||ie(391),t instanceof P)return ge(t,r,s);if(t instanceof z)return r instanceof he||ie(392),"linebreak"===s?fe(t,r):de(t,"",r,s);if(t instanceof j)return ce(t,r,s)}return o}function Le(t,n,o){const s=n.getType(),i=t.editor._nodes.get(s);void 0===i&&ie(88,s);const r=new i.klass;if(r.__parent=o,n._key=r.__key,n instanceof he){const e=n._xmlText;n.syncPropertiesFromYjs(t,null),n.applyChildrenYjsDelta(t,e.toDelta()),n.syncChildrenFromYjs(t)}else n instanceof ue?n.syncPropertiesAndTextFromYjs(t,null):n instanceof le&&(n.syncPropertiesFromYjs(t,null),e(r)||ie(393),n.syncSlotsFromYjs(t,r));return t.collabNodeMap.set(r.__key,n),r}function Ye(e,t,n,o){const s=null===o?t instanceof z?Array.from(t.keys()):t instanceof P||t instanceof j?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(Me(r,n,e)){"__state"===r&&Se(e)&&(i||(i=n.getWritable()),Re(t,i));continue}const l=n[r];let c=De(t,r);if(l!==c){if(c instanceof L){const t=e.docMap;l instanceof L&&t.delete(l.guid);const n=c.guid;if(t.set(n,c),f(l))l._key=n,c=l;else{const e=u();e._key=n,c=e}}void 0===i&&(i=n.getWritable()),i[r]=c}}}function De(e,t){return e instanceof z?e.get(t):e instanceof P||e instanceof j?e.getAttribute(t):e[t]}function Ie(e,t,n){e instanceof z?e.set(t,n):e.setAttribute(t,n)}function Re(e,t){const n=De(e,"__state");n instanceof z&&_(t).updateFromJSON(n.toJSON())}function $e(e,t,n,o){const s=Object.keys(Oe(o,e)),i=e.editor.constructor;!function(e,t,n,o){const s=o.__state,i=null===t.doc?void 0:De(t,"__state");if(!s)return;const[r,l]=s.getInternalState(),c=n&&n.__state,a=i instanceof z?i:new z;if(c===s)return;const[f,u]=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)u.get(e)!==t&&a.set(e.key,e.unparse(t));i||Ie(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 L,r=s.guid;a._key=r,t.set(r,s),a=s,e.editor.update(()=>{o.markDirty()})}Ie(t,l,a)}}}function Be(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<r;s++){const e=i[s],l=o;if(o+=e.getSize(),(n?o>=t:o>t)&&e instanceof ue){let n=t-l-1;return n<0&&(n=0),{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 Ue(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 He(e,t){e.doc.transact(t,e)}function qe(e,n){const o=n._nodeMap.get(e);if(!o)return void x().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():qe(i.__key,n):x().selectStart()}function Je(n,o,s,i){const l=o.getAttribute(be),c=l instanceof z?new Set(l.keys()):new Set;for(const e of d(s))if(!c.has(e)){const t=h(s,e);if(null!==t){const e=n.collabNodeMap.get(t.__key);void 0!==e&&e.destroy(n)}g(s,e)}if(l instanceof z)for(const[o,c]of l.entries()){if(Fe(o))continue;if(!(c instanceof P||c instanceof j||c instanceof z))continue;const l=h(s,o),a=null===l?void 0:n.collabNodeMap.get(l.__key);if(void 0!==a&&a.getSharedType()===c)continue;if(void 0===c._collabNode&&"string"!=typeof De(c,"__type"))continue;if(c instanceof z&&i instanceof le)continue;const f=c._collabNode;if(void 0!==f){const n=t(f._key);if(null!==n&&(n.isAttached()||(r(n)||e(n))&&null!==n.getLatest().__slotHost))continue}const u=ze(n,c,i),d=Le(n,u,null);Ae(d)?(void 0!==a&&a.destroy(n),p(s,o,d)):n.collabNodeMap.get(d.__key)===u&&n.collabNodeMap.delete(d.__key)}}function Ve(e,t,n,o,s,i,r){const l=d(n),c=t.getAttribute(be);if(0===l.length&&!(c instanceof z))return;let a;c instanceof z?a=c:(a=new z,Ce(t,a));const f=new Set(l);for(const t of Array.from(a.keys()))if(!f.has(t)){const n=a.get(t),o=null==n?void 0:n._collabNode;void 0!==o&&o.destroy(e),a.delete(t)}const u=e.collabNodeMap;for(const t of l){const l=h(n,t);if(null===l)continue;const c=u.get(l.__key);if(void 0!==c&&a.get(t)===c.getSharedType())Ee(e,c,l,o,s,i);else{const n=a.get(t),o=null==n?void 0:n._collabNode;void 0!==o&&o.destroy(e);const s=Pe(e,l,r);u.set(l.__key,s),a.set(t,s.getSharedType())}}}function Ge(e,t){const n=t.getAttribute(be);if(n instanceof z)for(const t of n.keys()){const o=n.get(t),s=null==o?void 0:o._collabNode;void 0!==s&&s.destroy(e)}}const Xe=e=>e.nodeName===Te,Qe=(t,n,o,s,i,r,l)=>{let c=n.mapping.get(t);if(c&&o&&0===o.size&&!s)return c;const a=Xe(t)?S.getType():t.nodeName,f=n.editor._nodes.get(a);if(void 0===f)throw new Error(`$createOrUpdateNodeFromYElement: Node ${a} is not registered`);if(c||(c=new f.klass,o=null,s=!0),s&&c instanceof b){const e=[],o=t=>{if(t instanceof j){const o=Qe(t,n,new Set,!1,i,r,l);null!==o&&e.push(o)}else if(t instanceof P){const o=tt(t,n,i,r,l);null!==o&&o.forEach(t=>{null!==t&&e.push(t)})}else ie(329)};void 0===i||void 0===r?t.toArray().forEach(o):Y(t,new D(r.ds,i.sv)).filter(e=>!e._item.deleted||et(e._item,i)||et(e._item,r)).forEach(o),Ze(c,e)}const u=t.getAttributes(i);Xe(t)||void 0===i||(et(t._item,i)?et(t._item,r)||(u[mt("ychange")]=l?l("added",t._item.id):{type:"added"}):u[mt("ychange")]=l?l("removed",t._item.id):{type:"removed"});const y={...Oe(c,n)},m={};for(const e in u)e.startsWith(_t)?m[xt(e)]=u[e]:e!==be&&"__proto__"!==e&&"constructor"!==e&&"prototype"!==e&&(y[e]=u[e]);if(Ye(n,y,c,o),o){if(o.size>0){const e=_(c);for(const t of o)if(t.startsWith(_t)){const n=xt(t);e.updateFromUnknown(n,m[n])}}}else _(c).updateFromJSON(m);if(c instanceof b||e(c)){const e=u[be],t=new Set;if(e instanceof z){const o=void 0===i?Array.from(e.entries()):Object.entries(I(e,i));for(const[e,s]of o){if(t.add(e),Fe(e))continue;let o=null;if(s instanceof j&&(o=Qe(s,n,new Set,!1,i,r,l)),null===o)continue;const a=h(c,e);null!==a&&a.getKey()===o.getKey()||(Ae(o)?null===o.getLatest().__slotHost&&p(c,e,o):o.isAttached()||ot(s,n))}}for(const e of d(c))t.has(e)||g(c,e)}const x=c.getLatest();return n.mapping.set(t,x),x},Ze=(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 u=t[c];f?(e.splice(c,1,[u]),l++,c++):(e.splice(c,0,[u]),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,[])},et=(e,t)=>void 0===t?!e.deleted:t.sv.has(e.id.client)&&t.sv.get(e.id.client)>e.id.clock&&!R(t.ds,e.id),tt=(e,t,n,s,i)=>{const r=pt(e,n,s,i);let l=t.mapping.get(e)??[];const c=r.map(e=>e.attributes.t??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={...Oe(n,t),...s.p},a=Object.fromEntries(Object.entries(s).filter(([e])=>e.startsWith(_t)).map(([e,t])=>[xt(e),t]));Ye(t,c,n,null),_(n).updateFromJSON(a)}const a=l.map(e=>e.getLatest());return t.mapping.set(e,a),a},nt=(e,t)=>{const n=d(e);if(0===n.length&&0===m(e.constructor).length)return;const o=new z;for(const s of n){const n=h(e,s);null!=n&&o.set(s,it(n,t))}return o},ot=(e,t)=>{if((e instanceof j||e instanceof P)&&t.mapping.delete(e),e instanceof j){const n=e.getAttribute(be);if(n instanceof z)for(const e of n.values())ot(e,t);for(const n of e.toArray())ot(n,t)}},st=(e,t)=>{const n=new j(e.getType()),o={...yt(e,t),...St(e)};for(const e in o){const t=o[e];null!==t&&n.setAttribute(e,t)}if(!(e instanceof b)){const o=nt(e,t);return void 0!==o&&(Ce(n,o),t.mapping.set(n,e)),n}const s=nt(e,t);return void 0!==s&&Ce(n,s),n.insert(0,at(e).map(e=>rt(e,t))),t.mapping.set(n,e),n},it=(t,n)=>{const o=st(t,n);return e(t)&&void 0===n.mapping.getSharedType(t)&&n.mapping.set(o,t),o},rt=(e,t)=>e instanceof Array?((e,t)=>{const n=new P;return gt(n,e,t),n})(e,t):st(e,t),lt=e=>"object"==typeof e&&null!=e,ct=(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||lt(r)&&lt(l)&&ct(r,l)}return o},at=e=>{if(!(e instanceof b))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},ft=(e,t,n)=>{const o=pt(e);return o.length===t.length&&o.every((e,o)=>{const s=t[o],i=e.attributes.t??T.getType(),r=e.attributes.p??{},l=Object.fromEntries(Object.entries(e.attributes).filter(([e])=>e.startsWith(_t)));return e.insert===s.getTextContent()&&i===s.getType()&&ct(r,yt(s,n))&&ct(l,St(s))})},ut=(e,t,n)=>{if(e instanceof j&&!(t instanceof Array)&&Tt(e,t)){const o=at(t),s=e.getAttributes();return delete s[be],e._length===o.length&&ct(s,{...yt(t,n),...St(t)})&&((e,t,n)=>{const o=d(t),s=e.getAttribute(be);if(!(s instanceof z))return 0===o.length;if(s.size!==o.length)return!1;for(const e of o){const o=h(t,e),i=s.get(e);if(null===o||!(i instanceof j)||n.mapping.get(i)!==o)return!1}return!0})(e,t,n)&&e.toArray().every((e,t)=>ut(e,o[t],n))}return e instanceof P&&t instanceof Array&&ft(e,t,n)},dt=(e,t)=>e===t||e instanceof Array&&t instanceof Array&&e.length===t.length&&e.every((e,n)=>t[n]===e),ht=(e,t,n)=>{const o=e.toArray(),s=at(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 $)break;if(dt(n.mapping.get(e),t))f=!0;else if(!ut(e,t,n))break}for(;c+a<l;a++){const e=o[r-a-1],t=s[i-a-1];if(e instanceof $)break;if(dt(n.mapping.get(e),t))f=!0;else if(!ut(e,t,n))break}return{equalityFactor:c+a,foundMappedChild:f}},gt=(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 B?t+=n.content.str:n.content instanceof W&&(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=yt(e,n);return 0===Object.keys(i).length&&(i=null),{attributes:Object.assign({},o,{...s!==T.getType()&&{t:s},p:i,...St(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:u,remove:d,index:h}=re(r,c,f);e.delete(h,d),e.insert(h,u),e.applyDelta(l.map(e=>({attributes:e.attributes,retain:e.insert.length})))},pt=(e,t,n,o)=>e.toDelta(t,n,o).map(e=>{const t=e.attributes??{};return"ychange"in t&&(t[mt("ychange")]=t.ychange,delete t.ychange),{...e,attributes:t}}),yt=(e,t)=>{const n=Oe(e,t),o={};return Object.entries(n).forEach(([t,n])=>{const s=e[t];s!==n&&(o[t]=s)}),o},_t="s_",mt=e=>`s_${e}`,xt=e=>{if(!e.startsWith(_t))throw new Error(`Invalid state key: ${e}`);return e.slice(_t.length)},St=e=>{const t=e.__state;if(!t)return{};const[n={},o]=t.getInternalState(),s={};for(const[e,t]of Object.entries(n))s[mt(e)]=t;for(const[e,t]of o)s[mt(e.key)]=e.unparse(t);return s},bt=(t,n,o,s,i)=>{if(n instanceof j&&n.nodeName!==o.getType()&&(!Xe(n)||o.getType()!==S.getType()))throw new Error("node name mismatch!");if(s.mapping.set(n,o),n instanceof j){const r=n.getAttributes(),l={...yt(o,s),...St(o)};for(const e in l)null!=l[e]?r[e]===l[e]||lt(r[e])&&lt(l[e])&&ct(r[e],l[e])||"ychange"===e||n.setAttribute(e,l[e]):n.removeAttribute(e);for(const e in r)e!==be&&void 0===l[e]&&n.removeAttribute(e);(o instanceof b||e(o))&&((t,n,o,s,i)=>{const r=d(n),l=t.getAttribute(be);if(0===r.length&&!(l instanceof z)&&0===m(n.constructor).length)return;let c;l instanceof z?c=l:(c=new z,Ce(t,c));const a=new Set(r);for(const e of Array.from(c.keys()))if(!a.has(e)){const t=c.get(e);void 0!==t&&ot(t,o),c.delete(e)}for(const t of r){const r=h(n,t);if(null==r)continue;const l=c.get(t);l instanceof j&&(r instanceof b||e(r))&&l===o.mapping.getSharedType(r)?s.has(r.getKey())&&bt(i,l,r,o,s):(l instanceof j&&ot(l,o),c.set(t,it(r,o)))}})(n,o,s,i,t)}const r=at(o),l=r.length,c=n.toArray(),a=c.length,f=Math.min(l,a);let u=0,g=0;for(;u<f;u++){const n=c[u],o=r[u];if(n instanceof $)break;if(dt(s.mapping.get(n),o))o instanceof Array||!(o instanceof b||e(o))||!i.has(o.getKey())||bt(t,n,o,s,i);else{if(!ut(n,o,s))break;s.mapping.set(n,o)}}for(;g+u<f;g++){const n=c[a-g-1],o=r[l-g-1];if(n instanceof $)break;if(dt(s.mapping.get(n),o))o instanceof Array||!(o instanceof b||e(o))||!i.has(o.getKey())||bt(t,n,o,s,i);else{if(!ut(n,o,s))break;s.mapping.set(n,o)}}for(;a-u-g>0&&l-u-g>0;){const e=c[u],o=r[u],f=c[a-g-1],d=r[l-g-1];if(e instanceof P&&o instanceof Array)ft(e,o,s)||gt(e,o,s),u+=1;else{let r=e instanceof j&&Tt(e,o),l=f instanceof j&&Tt(f,d);if(r&&l){const t=ht(e,o,s),n=ht(f,d,s);t.foundMappedChild&&!n.foundMappedChild?l=!1:!t.foundMappedChild&&n.foundMappedChild||t.equalityFactor<n.equalityFactor?r=!1:l=!1}r?(bt(t,e,o,s,i),u+=1):l?(bt(t,f,d,s,i),g+=1):(s.mapping.delete(n.get(u)),n.delete(u,1),n.insert(u,[rt(o,s)]),u+=1)}}const p=a-u-g;if(1===a&&0===l&&c[0]instanceof P?(s.mapping.delete(c[0]),c[0].delete(0,c[0].length)):p>0&&(n.slice(u,u+p).forEach(e=>s.mapping.delete(e)),n.delete(u,p)),u+g<l){const e=[];for(let t=u;t<l-g;t++)e.push(rt(r[t],s));n.insert(u,e)}},Tt=(e,t)=>!(t instanceof Array)&&e.nodeName===t.getType(),Nt=/* @__PURE__ */k("ychange",{isEqual:(e,t)=>e===t,parse:e=>e??null});function kt(e){return N(e,Nt)}const vt=(e,t=J(e.doc),n=U)=>{const{doc:o}=e;o.gc&&ie(325),o.transact(s=>{const i=new H(o);i&&i.dss.forEach(e=>{q(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(()=>{x().clear(),Qe(e.root,e,null,!0,t,n,r)})},e)};function wt(){return"undefined"!=typeof Highlight&&"undefined"!=typeof CSS&&"highlights"in CSS}const Ct=/* @__PURE__ */wt();function Ft(e,t){const n=e.cursorHighlightSheet;if(null===n)return;const o=`::highlight(${t})`;for(let e=n.cssRules.length-1;e>=0;e--){const t=n.cssRules[e];if(null!=t&&t.selectorText===o)return void n.deleteRule(e)}}function At(e,t,n=0){const s=t.collabNodeMap.get(e.key);if(void 0===s)return null;let i=e.offset,l=s.getSharedType();if(s instanceof ue){l=s._parent._xmlText;const e=s.getOffset();if(-1===e)return null;i=e+1+i}else if(s instanceof he&&"element"===e.type){const t=e.getNode();r(t)||ie(184);let n=0,s=0,l=t.getFirstChild();for(;null!==l&&s++<i;)o(l)?n+=l.getTextContentSize()+1:n++,l=l.getNextSibling();i=n}return G(l,i,n)}function Mt(e,t,n=0){const{mapping:s}=t,{offset:i}=e,l=e.getNode(),c=s.getSharedType(l);if(void 0===c)return null;if("text"===e.type){o(l)||ie(326);let e=l.getPreviousSibling(),t=i;for(;o(e);)t+=e.getTextContentSize(),e=e.getPreviousSibling();return G(c,t,n)}if("element"===e.type){r(l)||ie(184);let e=0,t=0,s=l.getFirstChild();for(;null!==s&&t<i;){let n=s.getNextSibling();if(t++,o(s))for(;o(n);)n=n.getNextSibling(),t++;e++,s=n}return G(c,e,n)}return null}function Ot(e,t){return V(e,t.doc)}function Et(e,t){if(null==e){if(null!=t)return!0}else if(null==t||!X(e,t))return!0;return!1}function Kt(e,t){return{color:t,name:e,selection:null}}function Pt(e,t){null!==t.highlight&&(CSS.highlights.delete(t.highlightName),Ft(e,t.highlightName));const n=e.cursorsContainer;if(null===n)return;t.caret.parentNode===n&&n.removeChild(t.caret);const o=t.selections;for(let e=0;e<o.length;e++)o[e].parentNode===n&&n.removeChild(o[e])}function jt(e,t){const n=t.selection;null!==n&&Pt(e,n)}function zt(e,t,n,o,s,i,r,l,c={}){const a=e.color,f=v(t.editor.getRootElement()),u=f.createElement("span");c.cursor?(u.className=c.cursor,w(u.style,{"--lexical-cursor-color":a,bottom:"0",position:"absolute",right:"-1px",top:"0"})):w(u.style,{"background-color":a,bottom:"0",position:"absolute",right:"-1px",top:"0",width:"1px","z-index":"10"});const d=f.createElement("span");d.textContent=e.name,c.cursorName?d.className=c.cursorName:w(d.style,{"background-color":a,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"}),u.appendChild(d);const h=`lexical-cursor-${t.id}-${n}`;let g=null;return l&&Ct&&(g=new Highlight,CSS.highlights.set(h,g),function(e,t,n){if(!CSS.supports("color",n))return;const o=function(e){const t=e.editor.getRootElement(),n=v(t);let o=e.cursorHighlightSheet;null===o&&(o=new(n.defaultView||window).CSSStyleSheet,e.cursorHighlightSheet=o);const s=null!==t?t.getRootNode():null,i=C(s)?s:n;return i.adoptedStyleSheets.includes(o)||(i.adoptedStyleSheets=[...i.adoptedStyleSheets,o]),o}(e),s=o.insertRule(`::highlight(${t}) { }`,o.cssRules.length),i=o.cssRules[s];i.style.setProperty("background-color",`color-mix(in srgb, ${n} 30%, transparent)`),i.style.setProperty("color","inherit")}(t,h,a)),{anchor:{key:o,offset:s},caret:u,color:a,focus:{key:i,offset:r},highlight:g,highlightName:h,name:d,selections:[]}}function Lt(e,t,s,i,l={}){const c=e.editor,a=c.getRootElement(),f=e.cursorsContainer;if(null===f||null===a)return;const u=v(a),d=f.offsetParent;if(null===d)return;const h=d.getBoundingClientRect(),g=t.selection;if(null===s)return null===g?void 0:(t.selection=null,void Pt(e,g));t.selection=s;const p=s.caret,y=s.color,_=s.highlight,m=s.anchor,x=s.focus,S=m.key,b=x.key,T=i.get(S),N=i.get(b);if(null==T||null==N)return;const k=()=>{const e=oe(c,N,x.offset,N,x.offset);let t=null===e?void 0:e.getBoundingClientRect();if((!t||0===t.height)&&n(N)){const e=c.getElementByKey(b);null!==e&&(t=e.getBoundingClientRect())}if(void 0!==t&&0===t.width&&0===t.height&&r(N)){const e=c.read("latest",()=>{const e=N.getChildAtIndex(x.offset-1),t=N.getChildAtIndex(x.offset);for(const[n,s]of[[e,o(e)?e.getTextContentSize():0],[t,0]])if(o(n)){const e=oe(c,n,s,n,s),t=null===e?void 0:e.getBoundingClientRect();if(t&&t.height>0)return t}return null});null!==e&&(t=e)}return!(!t||0===t.width&&0===t.height||(w(p.style,{"background-color":l.cursor?"":y,bottom:"",height:`${t.height||16}px`,left:t.left-h.left+"px","pointer-events":"none",position:"absolute",right:"",top:t.top-h.top+"px",width:"1px","z-index":"10"}),p.parentNode!==f&&f.appendChild(p),0))};if(null!==_){const e=oe(c,T,m.offset,N,x.offset);if(null===e)return;return _.clear(),e.collapsed||_.add(e),void k()}const C=s.selections;let F;if(T===N&&n(T))F=[c.getElementByKey(S).getBoundingClientRect()];else{const e=oe(c,T,m.offset,N,x.offset);if(null===e)return;F=se(c,e)}const A=C.length,M=F.length;for(let e=0;e<M;e++){const t=F[e];let n=C[e];if(void 0===n){n=u.createElement("span"),C[e]=n;const t=u.createElement("span");l.selectionBg&&(t.className=l.selectionBg),n.appendChild(t),f.appendChild(n)}const o=t.top-h.top,s=t.left-h.left,i={height:`${t.height}px`,left:`${s}px`,"pointer-events":"none",position:"absolute",top:`${o}px`,width:`${t.width}px`};l.selection?(n.className=l.selection,w(n.style,{...i,"--lexical-cursor-color":y}),w(n.firstChild.style,{height:"100%",left:"0",position:"absolute",top:"0",width:"100%"})):(w(n.style,i),w(n.firstChild.style,{...i,"background-color":y,left:"0",opacity:"0.3",top:"0","z-index":"5"}))}if(!k()&&M>0){const e=C[M-1];w(p.style,{bottom:"0",height:"",left:"",right:"-1px",top:"0"}),p.parentNode!==e&&e.appendChild(p)}for(let e=A-1;e>=M;e--){const t=C[e];f.removeChild(t),C.pop()}}function Yt(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]=$t(e,t.type,t.index),[r,l]=$t(e,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(Se(e)){const[t,n]=$t(e,i.type,i.index),[o,s]=$t(e,r.type,r.index);return{anchorKey:null!==t?t.getKey():null,anchorOffset:n,focusKey:null!==o?o.getKey():null,focusOffset:s}}let[l,c]=Bt(e.mapping,i),[a,f]=Bt(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 It(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;Rt(e.anchor,o,r),Rt(e.focus,l,c)}}function Rt(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 $t(e,t,n){const o=t._collabNode;if(void 0===o||!function(e,t){const n=e.root.getSharedType();let o=t;for(;null!=o;){if(o===n)return!0;o=o.parent}return!1}(e,t))return[null,0];if(o instanceof he){const{node:e,offset:t}=We(o,n,!0);return null===e?[o,o._children.length]:[e,t]}return[null,0]}function Bt(e,t){const n=t.type,s=t.index;if(n instanceof j){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 Wt(e,t){return t.awareness.getStates()}function Ut(e,t,n){const{getAwarenessStates:o=Wt,selectionHighlight:s=!1}=n??{},i=Array.from(o(e,t)),r=e.clientID,l=e.cursors,c=e.editor,a=c._config.theme.collaboration,f=c._editorState._nodeMap,u=new Set;for(let t=0;t<i.length;t++){const n=i[t],[o,d]=n;if(0!==o&&o!==r){u.add(o);const{name:t,color:n,focusing:i}=d;let r=null,h=l.get(o);if(void 0===h?(h=Kt(t,n),l.set(o,h)):h.name===t&&h.color===n||(jt(e,h),h.name=t,h.color=n,h.selection=null),i){const{anchorKey:t,anchorOffset:n,focusKey:i,focusOffset:l}=c.read(()=>Dt(e,d));if(null!==t&&null!==i)if(r=h.selection,null===r)r=zt(h,e,o,t,n,i,l,s,a);else{const e=r.anchor,o=r.focus;e.key=t,e.offset=n,o.key=i,o.offset=l}}Lt(e,h,r,f,a)}}const d=Array.from(l.keys());for(let t=0;t<d.length;t++){const n=d[t];if(!u.has(n)){const t=l.get(n);void 0!==t&&(jt(e,t),l.delete(n))}}}function Ht(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:u,awarenessData:d}=r;let h=null,g=null;if(null!==o&&(null===l||o.is(n))||null!==n){if(i(o)){const t=o.isCollapsed(),n=!t&&o.isBackward(),s=n?-1:0,i=t||n?0:-1;Se(e)?(h=At(o.anchor,e,s),g=At(o.focus,e,i)):(h=Mt(o.anchor,e,s),g=Mt(o.focus,e,i))}(Et(l,h)||Et(c,g))&&s.setLocalState({...r,anchorPos:h,awarenessData:d,color:f,focusPos:g,focusing:u,name:a})}}function qt(e,t){if(t instanceof Z&&function(e,t){const{target:n}=t;if(!n._item||"__state"!==n._item.parentSub||void 0!==je(n)||!(n.parent instanceof P||n.parent instanceof j||n.parent instanceof z))return!1;const o=ze(e,n.parent).getNode();if(o){const e=_(o.getWritable());for(const o of t.keysChanged)e.updateFromUnknown(o,n.get(o))}return!0}(e,t))return;const{target:n}=t;if(t instanceof Z&&n instanceof z&&null!=n._item&&n._item.parentSub===be&&(n.parent instanceof P||n.parent instanceof j)){const t=ze(e,n.parent);if(t instanceof he){const n=t.getNode();null!==n&&t.syncSlotsFromYjs(e,n)}else if(t instanceof le){const n=t.getNode();null!==n&&t.syncSlotsFromYjs(e,n)}return}const o=ze(e,n);if(o instanceof he&&t instanceof ee){const{keysChanged:n,childListChanged:s,delta:i}=t;if(n.size>0&&o.syncPropertiesFromYjs(e,n),n.has(be)){const t=o.getNode();null!==t&&o.syncSlotsFromYjs(e,t)}s&&(o.applyChildrenYjsDelta(e,i),o.syncChildrenFromYjs(e))}else if(o instanceof ue&&t instanceof Z){const{keysChanged:n}=t;n.size>0&&o.syncPropertiesAndTextFromYjs(e,n)}else if(o instanceof le&&t instanceof te){const{attributesChanged:n}=t;if(n.size>0&&o.syncPropertiesFromYjs(e,n),n.has(be)){const t=o.getNode();null!==t&&o.syncSlotsFromYjs(e,t)}}else ie(82)}function Jt(e,t,n,o,s=Ut){const i=e.editor,r=i._editorState;if(n.forEach(e=>e.delta),i.update(()=>{for(let t=0;t<n.length;t++){const o=n[t];qt(e,o)}Vt(r,e,t),o||M(O)},{onUpdate:()=>{s(e,t),e.root.isEmpty()&&i.update(()=>Gt())},skipTransforms:!0,tag:o?A:F}),n.length>0){const t=n[0].transaction;q(t,t.deleteSet,t=>{if(t.constructor===Q){const n=t.content.type;if(n){const t=n._collabNode;void 0!==t&&e.collabNodeMap.get(t._key)===t&&e.collabNodeMap.delete(t._key)}}})}}function Vt(e,t,n){const o=s();if(i(o))if(Ue(o)){const r=e._selection;i(r)&&(It(t,n),Ue(o))&&qe(o.anchor.key,e),Ht(t,n,r,s())}else It(t,n)}function Gt(){0===x().getChildrenSize()&&x().append(E())}function Xt(e,n,i,r,l,c,a,f){He(e,()=>{r.read(()=>{if(f.has(F)||f.has(A))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 ue)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=x(),o=e.root;o.syncPropertiesFromLexical(e,n,t),o.syncChildrenFromLexical(e,n,t,l,c),o.syncSlotsFromLexical(e,n,t,l,c),0===n.getChildrenSize()&&i._nodeMap.size>1&&e.editor.update(()=>Gt())}const r=s(),u=i._selection;Ht(e,n,u,r)})})}function Qt(e,t){const{target:n}=t;if(n instanceof j&&t instanceof te)Qe(n,e,t.attributesChanged,t.childListChanged);else if(n instanceof P&&t instanceof ee){const t=n.parent;t instanceof j?Qe(t,e,new Set,!0):ie(327)}else if(n instanceof z&&t instanceof Z&&null!=n._item&&n._item.parentSub===be){const t=n.parent;t instanceof j?Qe(t,e,new Set([be]),!1):ie(388)}else ie(328)}function Zt(e,t,n,o,s){const i=e.editor,r=i._editorState;q(o,o.deleteSet,t=>{if(t.constructor===Q){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];Qt(e,o)}Vt(r,e,t),s||M(O)},{discrete:!0,onUpdate:()=>{Ut(e,t),0===e.root.length&&i.update(()=>Gt())},skipTransforms:!0,tag:s?A:F})}function en(e,t){e.mapping.clear();const n=e.editor;n.update(()=>{x().clear(),Qe(e.root,e,null,!0),M(F)},{discrete:!0,onUpdate:()=>{Ut(e,t),0===e.root.length&&n.update(()=>Gt())},skipTransforms:!0,tag:F})}function tn(e,t,n,o,i,r,l,c){const a=c.has(F)||c.has(A);a&&0===l.size||(l.forEach(t=>{e.mapping.deleteNode(t)}),He(e,()=>{o.read(()=>{if(i.has("root")){const t=x();bt(e.doc,e.root,t,e,new Set([...i.keys(),...r])),!a&&0===t.getChildrenSize()&&n._nodeMap.size>1&&e.editor.update(()=>Gt())}const o=s(),l=n._selection;Ht(e,t,l,o)})}))}const nn=/* @__PURE__ */K("CONNECTED_COMMAND"),on=/* @__PURE__ */K("TOGGLE_CONNECT_COMMAND"),sn=/* @__PURE__ */K("DIFF_VERSIONS_COMMAND"),rn=/* @__PURE__ */K("CLEAR_DIFF_VERSIONS_COMMAND");function ln(e,t){return new ne(t,{captureTransaction:()=>!e.isBootstrapping,trackedOrigins:new Set([e,null])})}function cn(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})}function an(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{Dt as $getAnchorAndFocusForUserState,kt as $getYChangeState,rn as CLEAR_DIFF_VERSIONS_COMMAND__EXPERIMENTAL,nn as CONNECTED_COMMAND,sn as DIFF_VERSIONS_COMMAND__EXPERIMENTAL,on as TOGGLE_CONNECT_COMMAND,me as createBinding,xe as createBindingV2__EXPERIMENTAL,ln as createUndoManager,_e as createYjsBinding,Yt as getAnchorAndFocusCollabNodesForUserState,cn as initLocalState,Ft as removeCursorHighlightRule,vt as renderSnapshot__EXPERIMENTAL,an as setLocalStateFocus,Ut as syncCursorPositions,Xt as syncLexicalUpdateToYjs,tn as syncLexicalUpdateToYjsV2__EXPERIMENTAL,Jt as syncYjsChangesToLexical,Zt as syncYjsChangesToLexicalV2__EXPERIMENTAL,en as syncYjsStateToLexicalV2__EXPERIMENTAL};
package/package.json CHANGED
@@ -11,13 +11,13 @@
11
11
  "crdt"
12
12
  ],
13
13
  "license": "MIT",
14
- "version": "0.51.1-nightly.20260925.0",
14
+ "version": "0.52.0",
15
15
  "main": "./dist/LexicalYjs.js",
16
16
  "types": "./dist/typescript-too-old.d.ts",
17
17
  "dependencies": {
18
- "@lexical/internal": "0.51.1-nightly.20260925.0",
19
- "@lexical/selection": "0.51.1-nightly.20260925.0",
20
- "lexical": "0.51.1-nightly.20260925.0"
18
+ "@lexical/internal": "0.52.0",
19
+ "@lexical/selection": "0.52.0",
20
+ "lexical": "0.52.0"
21
21
  },
22
22
  "devDependencies": {
23
23
  "yjs": "^13.6.31"
@@ -487,37 +487,55 @@ function updateCursor(
487
487
  return;
488
488
  }
489
489
 
490
- if (highlight !== null) {
491
- // modern path: CSS Custom Highlight API
492
- const range = createDOMRange(
490
+ const positionCaretAtFocus = (): boolean => {
491
+ const focusRange = createDOMRange(
493
492
  editor,
494
- anchorNode,
495
- anchor.offset,
493
+ focusNode,
494
+ focus.offset,
496
495
  focusNode,
497
496
  focus.offset,
498
497
  );
499
- if (range === null) {
500
- return;
501
- }
502
-
503
- // The browser handles line wrapping, RTL, and font metrics — no rect math.
504
- highlight.clear();
505
- if (!range.collapsed) {
506
- highlight.add(range);
507
- }
508
-
509
- // Caret stays as a positioned element; anchor it to the focus end.
510
- const caretRange = range.cloneRange();
511
- caretRange.collapse(false);
512
- let caretRect: DOMRect = caretRange.getBoundingClientRect();
513
- if (caretRect.height === 0 && $isLineBreakNode(focusNode)) {
514
- // Bare <br>: collapsed range reports zero size. Fall back to the
515
- // line break's own box so the caret still renders.
498
+ let caretRect =
499
+ focusRange === null ? undefined : focusRange.getBoundingClientRect();
500
+ if ((!caretRect || caretRect.height === 0) && $isLineBreakNode(focusNode)) {
516
501
  const focusEl = editor.getElementByKey(focusKey) as HTMLElement | null;
517
502
  if (focusEl !== null) {
518
503
  caretRect = focusEl.getBoundingClientRect();
519
504
  }
520
505
  }
506
+ if (
507
+ caretRect !== undefined &&
508
+ caretRect.width === 0 &&
509
+ caretRect.height === 0 &&
510
+ $isElementNode(focusNode)
511
+ ) {
512
+ // A collapsed range at an element boundary can have no geometry. Use
513
+ // the adjacent text boundary instead of treating (0, 0) as a caret.
514
+ const adjacentRect = editor.read('latest', () => {
515
+ const previous = focusNode.getChildAtIndex(focus.offset - 1);
516
+ const next = focusNode.getChildAtIndex(focus.offset);
517
+ for (const [node, offset] of [
518
+ [previous, $isTextNode(previous) ? previous.getTextContentSize() : 0],
519
+ [next, 0],
520
+ ] as const) {
521
+ if ($isTextNode(node)) {
522
+ const range = createDOMRange(editor, node, offset, node, offset);
523
+ const rect =
524
+ range === null ? undefined : range.getBoundingClientRect();
525
+ if (rect && rect.height > 0) {
526
+ return rect;
527
+ }
528
+ }
529
+ }
530
+ return null;
531
+ });
532
+ if (adjacentRect !== null) {
533
+ caretRect = adjacentRect;
534
+ }
535
+ }
536
+ if (!caretRect || (caretRect.width === 0 && caretRect.height === 0)) {
537
+ return false;
538
+ }
521
539
 
522
540
  setDOMStyleObject(caret.style, {
523
541
  'background-color': theme.cursor ? '' : color,
@@ -535,6 +553,29 @@ function updateCursor(
535
553
  if (caret.parentNode !== cursorsContainer) {
536
554
  cursorsContainer.appendChild(caret);
537
555
  }
556
+ return true;
557
+ };
558
+
559
+ if (highlight !== null) {
560
+ // modern path: CSS Custom Highlight API
561
+ const range = createDOMRange(
562
+ editor,
563
+ anchorNode,
564
+ anchor.offset,
565
+ focusNode,
566
+ focus.offset,
567
+ );
568
+ if (range === null) {
569
+ return;
570
+ }
571
+
572
+ // The browser handles line wrapping, RTL, and font metrics — no rect math.
573
+ highlight.clear();
574
+ if (!range.collapsed) {
575
+ highlight.add(range);
576
+ }
577
+
578
+ positionCaretAtFocus();
538
579
  return;
539
580
  }
540
581
 
@@ -620,11 +661,21 @@ function updateCursor(
620
661
  'z-index': '5',
621
662
  });
622
663
  }
664
+ }
623
665
 
624
- if (i === selectionRectsLength - 1) {
625
- if (caret.parentNode !== selection) {
626
- selection.appendChild(caret);
627
- }
666
+ if (!positionCaretAtFocus() && selectionRectsLength > 0) {
667
+ const lastSelection = selections[selectionRectsLength - 1];
668
+ // A reused caret may still have container-relative coordinates. Restore
669
+ // rectangle-relative positioning before moving it into the fallback span.
670
+ setDOMStyleObject(caret.style, {
671
+ bottom: '0',
672
+ height: '',
673
+ left: '',
674
+ right: '-1px',
675
+ top: '0',
676
+ });
677
+ if (caret.parentNode !== lastSelection) {
678
+ lastSelection.appendChild(caret);
628
679
  }
629
680
  }
630
681