UNPKG

@lexical/yjs

Version:

The library provides Yjs editor bindings for Lexical.

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