@lexical/yjs 0.29.1-nightly.20250326.0 → 0.29.1-nightly.20250328.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.
- package/LexicalYjs.dev.js +1 -1
- package/LexicalYjs.dev.mjs +1 -1
- package/LexicalYjs.prod.js +1 -1
- package/LexicalYjs.prod.mjs +1 -1
- package/package.json +4 -4
package/LexicalYjs.dev.js
CHANGED
|
@@ -206,7 +206,7 @@ function $createCollabTextNode(map, text, parent, type) {
|
|
|
206
206
|
}
|
|
207
207
|
|
|
208
208
|
const baseExcludedProperties = new Set(['__key', '__parent', '__next', '__prev', '__state']);
|
|
209
|
-
const elementExcludedProperties = new Set(['__first', '__last', '__size']);
|
|
209
|
+
const elementExcludedProperties = new Set(['__first', '__last', '__size', '__dir']);
|
|
210
210
|
const rootExcludedProperties = new Set(['__cachedText']);
|
|
211
211
|
const textExcludedProperties = new Set(['__text']);
|
|
212
212
|
function isExcludedProperty(name, node, binding) {
|
package/LexicalYjs.dev.mjs
CHANGED
|
@@ -204,7 +204,7 @@ function $createCollabTextNode(map, text, parent, type) {
|
|
|
204
204
|
}
|
|
205
205
|
|
|
206
206
|
const baseExcludedProperties = new Set(['__key', '__parent', '__next', '__prev', '__state']);
|
|
207
|
-
const elementExcludedProperties = new Set(['__first', '__last', '__size']);
|
|
207
|
+
const elementExcludedProperties = new Set(['__first', '__last', '__size', '__dir']);
|
|
208
208
|
const rootExcludedProperties = new Set(['__cachedText']);
|
|
209
209
|
const textExcludedProperties = new Set(['__text']);
|
|
210
210
|
function isExcludedProperty(name, node, binding) {
|
package/LexicalYjs.prod.js
CHANGED
|
@@ -6,4 +6,4 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
"use strict";var e=require("lexical"),t=require("yjs"),n=require("@lexical/offset"),o=require("@lexical/selection");function s(e,...t){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",e);for(const e of t)o.append("v",e);throw n.search=o.toString(),Error(`Minified Lexical error #${e}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}class i{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 l(e,t){const n=new i(e,t);return e._collabNode=n,n}class r{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 s=this.getPrevNode(o),i=n.__text;if(b(t,this._map,s,n),null!==s){const t=s.__text;if(t!==i){!function(t,n,o,s){const i=e.$getSelection();let l=s.length;if(e.$isRangeSelection(i)&&i.isCollapsed()){const e=i.anchor;e.key===n&&(l=e.offset)}const r=function(e,t,n){const o=e.length,s=t.length;let i=0,l=0;for(;i<o&&i<s&&e[i]===t[i]&&i<n;)i++;for(;l+i<o&&l+i<s&&e[o-l-1]===t[s-l-1];)l++;for(;l+i<o&&l+i<s&&e[i]===t[i];)i++;return{index:i,insert:t.slice(i,s-l),remove:o-i-l}}(o,s,l);t.spliceText(r.index,r.remove,r.insert)}(this,n.__key,t,i),this._text=i}}}syncPropertiesAndTextFromYjs(e,t){const n=this.getNode();null===n&&s(84),x(e,this._map,n,t);const o=this._text;if(n.__text!==o){n.getWritable().__text=o}}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function c(e,t,n,o){const s=new r(e,t,n,o);return e._collabNode=s,s}const a=new Set(["__key","__parent","__next","__prev","__state"]),f=new Set(["__first","__last","__size"]),u=new Set(["__cachedText"]),d=new Set(["__text"]);function _(t,n,o){if(a.has(t)||"function"==typeof n[t])return!0;if(e.$isTextNode(n)){if(d.has(t))return!0}else if(e.$isElementNode(n)&&(f.has(t)||e.$isRootNode(n)&&u.has(t)))return!0;const s=n.constructor,i=o.excludedProperties.get(s);return null!=i&&i.has(t)}function h(n,o,i){const r=o.__type;let a;if(e.$isElementNode(o)){a=M(new t.XmlText,i,r),a.syncPropertiesFromLexical(n,o,null),a.syncChildrenFromLexical(n,o,null,null,null)}else if(e.$isTextNode(o)){a=c(new t.Map,o.__text,i,r),a.syncPropertiesAndTextFromLexical(n,o,null)}else if(e.$isLineBreakNode(o)){const e=new t.Map;e.set("__type","linebreak"),a=l(e,i)}else if(e.$isDecoratorNode(o)){a=P(new t.XmlElement,i,r),a.syncPropertiesFromLexical(n,o,null)}else s(86);return a._key=o.__key,a}function g(e){const t=m(e,"__type");return"string"!=typeof t&&void 0!==t&&s(87),t}function p(e,n,o){const i=n._collabNode;if(void 0===i){const i=e.editor._nodes,r=g(n);"string"!=typeof r&&s(87);void 0===i.get(r)&&s(88,r);const a=n.parent,f=void 0===o&&null!==a?p(e,a):o||null;if(f instanceof E||s(89),n instanceof t.XmlText)return M(n,f,r);if(n instanceof t.Map)return"linebreak"===r?l(n,f):c(n,"",f,r);if(n instanceof t.XmlElement)return P(n,f,r)}return i}function y(e,t,n){const o=t.getType(),i=e.editor._nodes.get(o);void 0===i&&s(88,o);const l=new i.klass;if(l.__parent=n,t._key=l.__key,t instanceof E){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof r?t.syncPropertiesAndTextFromYjs(e,null):t instanceof w&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(l.__key,t),l}function x(n,o,s,i){const l=null===i?o instanceof t.Map?Array.from(o.keys()):Object.keys(o.getAttributes()):Array.from(i);let r;for(let i=0;i<l.length;i++){const c=l[i];if(_(c,s,n)){"__state"===c&&(r||(r=s.getWritable()),k(n,o,r));continue}const a=s[c];let f=m(o,c);if(a!==f){if(f instanceof t.Doc){const o=n.docMap;a instanceof t.Doc&&o.delete(a.guid);const s=e.createEditor(),i=f.guid;s._key=i,o.set(i,f),f=s}void 0===r&&(r=s.getWritable()),r[c]=f}}}function m(e,n){return e instanceof t.Map?e.get(n):e.getAttribute(n)}function N(e,n,o){e instanceof t.Map?e.set(n,o):e.setAttribute(n,o)}function k(n,o,s){const i=m(o,"__state");i instanceof t.Map&&e.$getWritableNodeState(s).updateFromJSON(i.toJSON())}function b(e,n,o,s){const i=s.__type,l=e.nodeProperties;let r=l.get(i);void 0===r&&(r=Object.keys(s).filter((t=>!_(t,s,e))),l.set(i,r));const c=e.editor.constructor;!function(e,n,o,s){const i=s.__state,l=m(n,"__state");if(!i)return;const[r,c]=i.getInternalState(),a=o&&o.__state,f=l instanceof t.Map?l:new t.Map;if(a===i)return;const[u,d]=a&&f.doc?a.getInternalState():[void 0,new Map];if(r)for(const[e,t]of Object.entries(r))u&&t!==u[e]&&f.set(e,t);for(const[e,t]of c)d.get(e)!==t&&f.set(e.key,e.unparse(t));l||N(n,"__state",f)}(0,n,o,s);for(let i=0;i<r.length;i++){const l=r[i],a=null===o?void 0:o[l];let f=s[l];if(a!==f){if(f instanceof c){const n=e.docMap;let o;if(a instanceof c){const e=a._key;o=n.get(e),n.delete(e)}const i=o||new t.Doc,l=i.guid;f._key=l,n.set(l,i),f=i,e.editor.update((()=>{s.markDirty()}))}N(n,l,f)}}}function T(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function $(e,t,n){let o=0,s=0;const i=e._children,l=i.length;for(;s<l;s++){const e=i[s],c=o;o+=e.getSize();if((n?o>=t:o>t)&&e instanceof r){let n=t-c-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:c};if(s===l-1)return{length:0,node:null,nodeIndex:s+1,offset:c+1}}return{length:0,node:null,nodeIndex:0,offset:0}}function C(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 S(e){const t=e.getParent();if(null!==t){const n=e.getWritable(),o=t.getWritable(),s=e.getPreviousSibling(),i=e.getNextSibling();if(null===s)if(null!==i){const e=i.getWritable();o.__first=i.__key,e.__prev=null}else o.__first=null;else{const e=s.getWritable();if(null!==i){const t=i.getWritable();t.__prev=e.__key,e.__next=t.__key}else e.__next=null;n.__prev=null}if(null===i)if(null!==s){const e=s.getWritable();o.__last=s.__key,e.__next=null}else o.__last=null;else{const e=i.getWritable();if(null!==s){const t=s.getWritable();t.__next=e.__key,e.__prev=t.__key}else e.__prev=null;n.__next=null}o.__size--,n.__parent=null}}function v(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():v(i.__key,n):e.$getRoot().selectStart()}class w{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);b(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&s(83);x(e,this._xmlElem,n,t)}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function P(e,t,n){const o=new w(e,t,n);return e._collabNode=o,o}class E{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&&s(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&s(91),x(e,this._xmlText,n,t)}applyChildrenYjsDelta(e,t){const n=this._children;let o=0,s=null;for(let l=0;l<t.length;l++){const c=t[l],a=c.insert,f=c.delete;if(null!=c.retain)o+=c.retain;else if("number"==typeof f){let e=f;for(;e>0;){const{node:t,nodeIndex:s,offset:l,length:c}=$(this,o,!1);if(t instanceof E||t instanceof i||t instanceof w)n.splice(s,1),e-=1;else{if(!(t instanceof r))break;{const o=Math.min(e,c),i=0!==s?n[s-1]:null,a=t.getSize();if(0===l&&c===a){n.splice(s,1);const e=T(t._text,l,o-1,"");e.length>0&&(i instanceof r?i._text+=e:this._xmlText.delete(l,e.length))}else t._text=T(t._text,l,o,"");e-=o}}}}else{if(null==a)throw new Error("Unexpected delta format");if("string"==typeof a){const{node:e,offset:t}=$(this,o,!0);e instanceof r?e._text=T(e._text,t,0,a):this._xmlText.delete(t,a.length),o+=a.length}else{const t=a,{node:i,nodeIndex:l,length:c}=$(this,o,!1),f=p(e,t,this);if(i instanceof r&&c>0&&c<i._text.length){const e=i._text,t=e.length-c;i._text=T(e,t,c,""),n.splice(l+1,0,f),s=T(e,0,t,"")}else n.splice(l,0,f);null!==s&&f instanceof r&&(f._text=s+f._text,s=null),o+=1}}}}syncChildrenFromYjs(t){const o=this.getNode();null===o&&s(92);const l=o.__key,c=n.$createChildrenArray(o,null),a=c.length,f=this._children,u=f.length,d=t.collabNodeMap,_=new Set;let h,g,p=0,x=null;u!==a&&(g=o.getWritable());for(let n=0;n<u;n++){const a=c[p],m=f[n],N=m.getNode(),k=m._key;if(null!==N&&a===k){const n=e.$isTextNode(N);if(_.add(a),n)if(m._key=a,m instanceof E){const e=m._xmlText;m.syncPropertiesFromYjs(t,null),m.applyChildrenYjsDelta(t,e.toDelta()),m.syncChildrenFromYjs(t)}else m instanceof r?m.syncPropertiesAndTextFromYjs(t,null):m instanceof w?m.syncPropertiesFromYjs(t,null):m instanceof i||s(93);x=N,p++}else{if(void 0===h){h=new Set;for(let e=0;e<u;e++){const t=f[e]._key;""!==t&&h.add(t)}}if(null!==N&&void 0!==a&&!h.has(a)){S(e.$getNodeByKeyOrThrow(a)),n--,p++;continue}g=o.getWritable();const s=y(t,m,l),i=s.__key;if(d.set(i,m),null===x){const e=g.getFirstChild();if(g.__first=i,null!==e){const t=e.getWritable();t.__prev=i,s.__next=t.__key}}else{const e=x.getWritable(),t=x.getNextSibling();if(e.__next=i,s.__prev=x.__key,null!==t){const e=t.getWritable();e.__prev=i,s.__next=e.__key}}n===u-1&&(g.__last=i),g.__size++,x=s}}for(let n=0;n<a;n++){const o=c[n];if(!_.has(o)){const n=e.$getNodeByKeyOrThrow(o),s=t.collabNodeMap.get(o);void 0!==s&&s.destroy(t),S(n)}}}syncPropertiesFromLexical(e,t,n){b(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(t,n,o,s,i,l){const c=this._children[n],a=e.$getNodeByKeyOrThrow(o);c instanceof E&&e.$isElementNode(a)?(c.syncPropertiesFromLexical(t,a,s),c.syncChildrenFromLexical(t,a,s,i,l)):c instanceof r&&e.$isTextNode(a)?c.syncPropertiesAndTextFromLexical(t,a,s):c instanceof w&&e.$isDecoratorNode(a)&&c.syncPropertiesFromLexical(t,a,s)}syncChildrenFromLexical(t,o,s,i,l){const r=this.getPrevNode(s),c=null===r?[]:n.$createChildrenArray(r,s),a=n.$createChildrenArray(o,null),f=c.length-1,u=a.length-1,d=t.collabNodeMap;let _,g,p=0,y=0;for(;p<=f&&y<=u;){const n=c[p],o=a[y];if(n===o)this._syncChildFromLexical(t,y,o,s,i,l),p++,y++;else{void 0===_&&(_=new Set(c)),void 0===g&&(g=new Set(a));const s=g.has(n),i=_.has(o);if(s){const n=h(t,e.$getNodeByKeyOrThrow(o),this);d.set(o,n),i?(this.splice(t,y,1,n),p++,y++):(this.splice(t,y,0,n),y++)}else this.splice(t,y,1),p++}}const x=p>f,m=y>u;if(x&&!m)for(;y<=u;++y){const n=a[y],o=h(t,e.$getNodeByKeyOrThrow(n),this);this.append(o),d.set(n,o)}else if(m&&!x)for(let e=this._children.length-1;e>=y;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 E)t.insertEmbed(s,e._xmlText);else if(e instanceof r){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof i?t.insertEmbed(s,e._map):e instanceof w&&t.insertEmbed(s,e._xmlElem);this._children.push(e)}splice(e,t,n,o){const l=this._children,c=l[t];if(void 0===c)return void 0===o&&s(94),void this.append(o);const a=c.getOffset();-1===a&&s(95);const f=this._xmlText;if(0!==n&&f.delete(a,c.getSize()),o instanceof E)f.insertEmbed(a,o._xmlText);else if(o instanceof r){const e=o._map;null===e.parent&&f.insertEmbed(a,e),f.insert(a+1,o._text)}else o instanceof i?f.insertEmbed(a,o._map):o instanceof w&&f.insertEmbed(a,o._xmlElem);if(0!==n){const o=l.slice(t,t+n);for(let t=0;t<o.length;t++)o[t].destroy(e)}void 0!==o?l.splice(t,n,o):l.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 M(e,t,n){const o=new E(e,t,n);return e._collabNode=o,o}function O(n,o){const i=o.collabNodeMap.get(n.key);if(void 0===i)return null;let l=n.offset,c=i.getSharedType();if(i instanceof r){c=i._parent._xmlText;const e=i.getOffset();if(-1===e)return null;l=e+1+l}else if(i instanceof E&&"element"===n.type){const t=n.getNode();e.$isElementNode(t)||s(184);let o=0,i=0,r=t.getFirstChild();for(;null!==r&&i++<l;)e.$isTextNode(r)?o+=r.getTextContentSize()+1:o++,r=r.getNextSibling();l=o}return t.createRelativePositionFromTypeIndex(c,l)}function F(e,n){return t.createAbsolutePositionFromRelativePosition(e,n.doc)}function L(e,n){if(null==e){if(null!=n)return!0}else if(null==n||!t.compareRelativePositions(e,n))return!0;return!1}function A(e,t){return{color:t,name:e,selection:null}}function D(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 j(e,t){const n=t.selection;null!==n&&D(e,n)}function z(e,t,n,o,s){const i=e.color,l=document.createElement("span");l.style.cssText=`position:absolute;top:0;bottom:0;right:-1px;width:1px;background-color:${i};z-index:10;`;const r=document.createElement("span");return r.textContent=e.name,r.style.cssText=`position:absolute;left:-2px;top:-16px;background-color:${i};color:#fff;line-height:12px;font-size:12px;padding:2px;font-family:Arial;font-weight:bold;white-space:nowrap;`,l.appendChild(r),{anchor:{key:t,offset:n},caret:l,color:i,focus:{key:o,offset:s},name:r,selections:[]}}function Y(t,n,s,i){const l=t.editor,r=l.getRootElement(),c=t.cursorsContainer;if(null===c||null===r)return;const a=c.offsetParent;if(null===a)return;const f=a.getBoundingClientRect(),u=n.selection;if(null===s)return null===u?void 0:(n.selection=null,void D(t,u));n.selection=s;const d=s.caret,_=s.color,h=s.selections,g=s.anchor,p=s.focus,y=g.key,x=p.key,m=i.get(y),N=i.get(x);if(null==m||null==N)return;let k;if(m===N&&e.$isLineBreakNode(m)){k=[l.getElementByKey(y).getBoundingClientRect()]}else{const e=o.createDOMRange(l,m,g.offset,N,p.offset);if(null===e)return;k=o.createRectsFromDOMRange(l,e)}const b=h.length,T=k.length;for(let e=0;e<T;e++){const t=k[e];let n=h[e];if(void 0===n){n=document.createElement("span"),h[e]=n;const t=document.createElement("span");n.appendChild(t),c.appendChild(n)}const o=`position:absolute;top:${t.top-f.top}px;left:${t.left-f.left}px;height:${t.height}px;width:${t.width}px;pointer-events:none;z-index:5;`;n.style.cssText=o,n.firstChild.style.cssText=`${o}left:0;top:0;background-color:${_};opacity:0.3;`,e===T-1&&d.parentNode!==n&&n.appendChild(d)}for(let e=b-1;e>=T;e--){const t=h[e];c.removeChild(t),h.pop()}}function K(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,i=0,l=null,r=0;if(null!==n&&null!==o){const t=F(n,e),c=F(o,e);null!==t&&null!==c&&([s,i]=W(t.type,t.index),[l,r]=W(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:i,focusCollabNode:l,focusOffset:r}}function R(t,n){const o=n.awareness.getLocalState();if(null===o)return;const{anchorCollabNode:s,anchorOffset:i,focusCollabNode:l,focusOffset:r}=K(t,o);if(null!==s&&null!==l){const t=s.getKey(),n=l.getKey(),o=e.$getSelection();if(!e.$isRangeSelection(o))return;B(o.anchor,t,i),B(o.focus,n,r)}}function B(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 W(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof E){const{node:e,offset:o}=$(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function I(e,t){const n=Array.from(t.awareness.getStates()),o=e.clientID,s=e.cursors,i=e.editor._editorState._nodeMap,l=new Set;for(let t=0;t<n.length;t++){const r=n[t],[c,a]=r;if(c!==o){l.add(c);const{name:t,color:n,focusing:o}=a;let r=null,f=s.get(c);if(void 0===f&&(f=A(t,n),s.set(c,f)),o){const{anchorCollabNode:t,anchorOffset:n,focusCollabNode:o,focusOffset:s}=K(e,a);if(null!==t&&null!==o){const e=t.getKey(),i=o.getKey();if(r=f.selection,null===r)r=z(f,e,n,i,s);else{const t=r.anchor,o=r.focus;t.key=e,t.offset=n,o.key=i,o.offset=s}}}Y(e,f,r,i)}}const r=Array.from(s.keys());for(let t=0;t<r.length;t++){const n=r[t];if(!l.has(n)){const t=s.get(n);void 0!==t&&(j(e,t),s.delete(n))}}}function U(t,n,o,s){const i=n.awareness,l=i.getLocalState();if(null===l)return;const{anchorPos:r,focusPos:c,name:a,color:f,focusing:u,awarenessData:d}=l;let _=null,h=null;(null!==s&&(null===r||s.is(o))||null!==o)&&(e.$isRangeSelection(s)&&(_=O(s.anchor,t),h=O(s.focus,t)),(L(r,_)||L(c,h))&&i.setLocalState({...l,anchorPos:_,awarenessData:d,color:f,focusPos:h,focusing:u,name:a}))}function X(n,o){if(o instanceof t.YMapEvent&&function(n,o){const{target:s}=o;if(!s._item||"__state"!==s._item.parentSub||void 0!==g(s)||!(s.parent instanceof t.XmlText||s.parent instanceof t.XmlElement||s.parent instanceof t.Map))return!1;const i=p(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,o))return;const{target:i}=o,l=p(n,i);if(l instanceof E&&o instanceof t.YTextEvent){const{keysChanged:e,childListChanged:t,delta:s}=o;e.size>0&&l.syncPropertiesFromYjs(n,e),t&&(l.applyChildrenYjsDelta(n,s),l.syncChildrenFromYjs(n))}else if(l instanceof r&&o instanceof t.YMapEvent){const{keysChanged:e}=o;e.size>0&&l.syncPropertiesAndTextFromYjs(n,e)}else if(l instanceof w&&o instanceof t.YXmlEvent){const{attributesChanged:e}=o;e.size>0&&l.syncPropertiesFromYjs(n,e)}else s(82)}const q=e.createCommand("CONNECTED_COMMAND"),G=e.createCommand("TOGGLE_CONNECT_COMMAND");exports.CONNECTED_COMMAND=q,exports.TOGGLE_CONNECT_COMMAND=G,exports.createBinding=function(e,n,o,i,l,r){null==i&&s(81);const c=M(i.get("root",t.XmlText),null,"root");return c._key="root",{clientID:i.clientID,collabNodeMap:new Map,cursors:new Map,cursorsContainer:null,doc:i,docMap:l,editor:e,excludedProperties:r||new Map,id:o,nodeProperties:new Map,root:c}},exports.createUndoManager=function(e,n){return new t.UndoManager(n,{trackedOrigins:new Set([e,null])})},exports.getAnchorAndFocusCollabNodesForUserState=K,exports.initLocalState=function(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})},exports.setLocalStateFocus=function(e,t,n,o,s){const{awareness:i}=e;let l=i.getLocalState();null===l&&(l={anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t}),l.focusing=o,i.setLocalState(l)},exports.syncCursorPositions=I,exports.syncLexicalUpdateToYjs=function(t,n,o,s,i,l,c,a){!function(e,t){e.doc.transact(t,e)}(t,(()=>{s.read((()=>{if(a.has("collaboration")||a.has("historic"))return void(c.size>0&&function(t,n){const o=Array.from(n),s=t.collabNodeMap,i=[],l=[];for(let t=0;t<o.length;t++){const n=o[t],c=e.$getNodeByKey(n),a=s.get(n);if(a instanceof r)if(e.$isTextNode(c))i.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();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,c));if(i.has("root")){const n=o._nodeMap,s=e.$getRoot(),r=t.root;r.syncPropertiesFromLexical(t,s,n),r.syncChildrenFromLexical(t,s,n,i,l)}const s=e.$getSelection(),f=o._selection;U(t,n,f,s)}))}))},exports.syncYjsChangesToLexical=function(t,n,o,s,i=I){const l=t.editor,r=l._editorState;o.forEach((e=>e.delta)),l.update((()=>{for(let e=0;e<o.length;e++){const n=o[e];X(t,n)}const s=e.$getSelection();if(e.$isRangeSelection(s))if(C(s)){const o=r._selection;if(e.$isRangeSelection(o)&&(R(t,n),C(s))){v(s.anchor.key,r)}U(t,n,o,e.$getSelection())}else R(t,n)}),{onUpdate:()=>{i(t,n),l.update((()=>{0===e.$getRoot().getChildrenSize()&&e.$getRoot().append(e.$createParagraphNode())}))},skipTransforms:!0,tag:s?"historic":"collaboration"})};
|
|
9
|
+
"use strict";var e=require("lexical"),t=require("yjs"),n=require("@lexical/offset"),o=require("@lexical/selection");function s(e,...t){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",e);for(const e of t)o.append("v",e);throw n.search=o.toString(),Error(`Minified Lexical error #${e}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}class i{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 l(e,t){const n=new i(e,t);return e._collabNode=n,n}class r{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 s=this.getPrevNode(o),i=n.__text;if(b(t,this._map,s,n),null!==s){const t=s.__text;if(t!==i){!function(t,n,o,s){const i=e.$getSelection();let l=s.length;if(e.$isRangeSelection(i)&&i.isCollapsed()){const e=i.anchor;e.key===n&&(l=e.offset)}const r=function(e,t,n){const o=e.length,s=t.length;let i=0,l=0;for(;i<o&&i<s&&e[i]===t[i]&&i<n;)i++;for(;l+i<o&&l+i<s&&e[o-l-1]===t[s-l-1];)l++;for(;l+i<o&&l+i<s&&e[i]===t[i];)i++;return{index:i,insert:t.slice(i,s-l),remove:o-i-l}}(o,s,l);t.spliceText(r.index,r.remove,r.insert)}(this,n.__key,t,i),this._text=i}}}syncPropertiesAndTextFromYjs(e,t){const n=this.getNode();null===n&&s(84),x(e,this._map,n,t);const o=this._text;if(n.__text!==o){n.getWritable().__text=o}}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function c(e,t,n,o){const s=new r(e,t,n,o);return e._collabNode=s,s}const a=new Set(["__key","__parent","__next","__prev","__state"]),f=new Set(["__first","__last","__size","__dir"]),u=new Set(["__cachedText"]),d=new Set(["__text"]);function _(t,n,o){if(a.has(t)||"function"==typeof n[t])return!0;if(e.$isTextNode(n)){if(d.has(t))return!0}else if(e.$isElementNode(n)&&(f.has(t)||e.$isRootNode(n)&&u.has(t)))return!0;const s=n.constructor,i=o.excludedProperties.get(s);return null!=i&&i.has(t)}function h(n,o,i){const r=o.__type;let a;if(e.$isElementNode(o)){a=M(new t.XmlText,i,r),a.syncPropertiesFromLexical(n,o,null),a.syncChildrenFromLexical(n,o,null,null,null)}else if(e.$isTextNode(o)){a=c(new t.Map,o.__text,i,r),a.syncPropertiesAndTextFromLexical(n,o,null)}else if(e.$isLineBreakNode(o)){const e=new t.Map;e.set("__type","linebreak"),a=l(e,i)}else if(e.$isDecoratorNode(o)){a=P(new t.XmlElement,i,r),a.syncPropertiesFromLexical(n,o,null)}else s(86);return a._key=o.__key,a}function g(e){const t=m(e,"__type");return"string"!=typeof t&&void 0!==t&&s(87),t}function p(e,n,o){const i=n._collabNode;if(void 0===i){const i=e.editor._nodes,r=g(n);"string"!=typeof r&&s(87);void 0===i.get(r)&&s(88,r);const a=n.parent,f=void 0===o&&null!==a?p(e,a):o||null;if(f instanceof E||s(89),n instanceof t.XmlText)return M(n,f,r);if(n instanceof t.Map)return"linebreak"===r?l(n,f):c(n,"",f,r);if(n instanceof t.XmlElement)return P(n,f,r)}return i}function y(e,t,n){const o=t.getType(),i=e.editor._nodes.get(o);void 0===i&&s(88,o);const l=new i.klass;if(l.__parent=n,t._key=l.__key,t instanceof E){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof r?t.syncPropertiesAndTextFromYjs(e,null):t instanceof w&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(l.__key,t),l}function x(n,o,s,i){const l=null===i?o instanceof t.Map?Array.from(o.keys()):Object.keys(o.getAttributes()):Array.from(i);let r;for(let i=0;i<l.length;i++){const c=l[i];if(_(c,s,n)){"__state"===c&&(r||(r=s.getWritable()),k(n,o,r));continue}const a=s[c];let f=m(o,c);if(a!==f){if(f instanceof t.Doc){const o=n.docMap;a instanceof t.Doc&&o.delete(a.guid);const s=e.createEditor(),i=f.guid;s._key=i,o.set(i,f),f=s}void 0===r&&(r=s.getWritable()),r[c]=f}}}function m(e,n){return e instanceof t.Map?e.get(n):e.getAttribute(n)}function N(e,n,o){e instanceof t.Map?e.set(n,o):e.setAttribute(n,o)}function k(n,o,s){const i=m(o,"__state");i instanceof t.Map&&e.$getWritableNodeState(s).updateFromJSON(i.toJSON())}function b(e,n,o,s){const i=s.__type,l=e.nodeProperties;let r=l.get(i);void 0===r&&(r=Object.keys(s).filter((t=>!_(t,s,e))),l.set(i,r));const c=e.editor.constructor;!function(e,n,o,s){const i=s.__state,l=m(n,"__state");if(!i)return;const[r,c]=i.getInternalState(),a=o&&o.__state,f=l instanceof t.Map?l:new t.Map;if(a===i)return;const[u,d]=a&&f.doc?a.getInternalState():[void 0,new Map];if(r)for(const[e,t]of Object.entries(r))u&&t!==u[e]&&f.set(e,t);for(const[e,t]of c)d.get(e)!==t&&f.set(e.key,e.unparse(t));l||N(n,"__state",f)}(0,n,o,s);for(let i=0;i<r.length;i++){const l=r[i],a=null===o?void 0:o[l];let f=s[l];if(a!==f){if(f instanceof c){const n=e.docMap;let o;if(a instanceof c){const e=a._key;o=n.get(e),n.delete(e)}const i=o||new t.Doc,l=i.guid;f._key=l,n.set(l,i),f=i,e.editor.update((()=>{s.markDirty()}))}N(n,l,f)}}}function T(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function $(e,t,n){let o=0,s=0;const i=e._children,l=i.length;for(;s<l;s++){const e=i[s],c=o;o+=e.getSize();if((n?o>=t:o>t)&&e instanceof r){let n=t-c-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:c};if(s===l-1)return{length:0,node:null,nodeIndex:s+1,offset:c+1}}return{length:0,node:null,nodeIndex:0,offset:0}}function C(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 S(e){const t=e.getParent();if(null!==t){const n=e.getWritable(),o=t.getWritable(),s=e.getPreviousSibling(),i=e.getNextSibling();if(null===s)if(null!==i){const e=i.getWritable();o.__first=i.__key,e.__prev=null}else o.__first=null;else{const e=s.getWritable();if(null!==i){const t=i.getWritable();t.__prev=e.__key,e.__next=t.__key}else e.__next=null;n.__prev=null}if(null===i)if(null!==s){const e=s.getWritable();o.__last=s.__key,e.__next=null}else o.__last=null;else{const e=i.getWritable();if(null!==s){const t=s.getWritable();t.__next=e.__key,e.__prev=t.__key}else e.__prev=null;n.__next=null}o.__size--,n.__parent=null}}function v(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():v(i.__key,n):e.$getRoot().selectStart()}class w{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);b(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&s(83);x(e,this._xmlElem,n,t)}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function P(e,t,n){const o=new w(e,t,n);return e._collabNode=o,o}class E{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&&s(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&s(91),x(e,this._xmlText,n,t)}applyChildrenYjsDelta(e,t){const n=this._children;let o=0,s=null;for(let l=0;l<t.length;l++){const c=t[l],a=c.insert,f=c.delete;if(null!=c.retain)o+=c.retain;else if("number"==typeof f){let e=f;for(;e>0;){const{node:t,nodeIndex:s,offset:l,length:c}=$(this,o,!1);if(t instanceof E||t instanceof i||t instanceof w)n.splice(s,1),e-=1;else{if(!(t instanceof r))break;{const o=Math.min(e,c),i=0!==s?n[s-1]:null,a=t.getSize();if(0===l&&c===a){n.splice(s,1);const e=T(t._text,l,o-1,"");e.length>0&&(i instanceof r?i._text+=e:this._xmlText.delete(l,e.length))}else t._text=T(t._text,l,o,"");e-=o}}}}else{if(null==a)throw new Error("Unexpected delta format");if("string"==typeof a){const{node:e,offset:t}=$(this,o,!0);e instanceof r?e._text=T(e._text,t,0,a):this._xmlText.delete(t,a.length),o+=a.length}else{const t=a,{node:i,nodeIndex:l,length:c}=$(this,o,!1),f=p(e,t,this);if(i instanceof r&&c>0&&c<i._text.length){const e=i._text,t=e.length-c;i._text=T(e,t,c,""),n.splice(l+1,0,f),s=T(e,0,t,"")}else n.splice(l,0,f);null!==s&&f instanceof r&&(f._text=s+f._text,s=null),o+=1}}}}syncChildrenFromYjs(t){const o=this.getNode();null===o&&s(92);const l=o.__key,c=n.$createChildrenArray(o,null),a=c.length,f=this._children,u=f.length,d=t.collabNodeMap,_=new Set;let h,g,p=0,x=null;u!==a&&(g=o.getWritable());for(let n=0;n<u;n++){const a=c[p],m=f[n],N=m.getNode(),k=m._key;if(null!==N&&a===k){const n=e.$isTextNode(N);if(_.add(a),n)if(m._key=a,m instanceof E){const e=m._xmlText;m.syncPropertiesFromYjs(t,null),m.applyChildrenYjsDelta(t,e.toDelta()),m.syncChildrenFromYjs(t)}else m instanceof r?m.syncPropertiesAndTextFromYjs(t,null):m instanceof w?m.syncPropertiesFromYjs(t,null):m instanceof i||s(93);x=N,p++}else{if(void 0===h){h=new Set;for(let e=0;e<u;e++){const t=f[e]._key;""!==t&&h.add(t)}}if(null!==N&&void 0!==a&&!h.has(a)){S(e.$getNodeByKeyOrThrow(a)),n--,p++;continue}g=o.getWritable();const s=y(t,m,l),i=s.__key;if(d.set(i,m),null===x){const e=g.getFirstChild();if(g.__first=i,null!==e){const t=e.getWritable();t.__prev=i,s.__next=t.__key}}else{const e=x.getWritable(),t=x.getNextSibling();if(e.__next=i,s.__prev=x.__key,null!==t){const e=t.getWritable();e.__prev=i,s.__next=e.__key}}n===u-1&&(g.__last=i),g.__size++,x=s}}for(let n=0;n<a;n++){const o=c[n];if(!_.has(o)){const n=e.$getNodeByKeyOrThrow(o),s=t.collabNodeMap.get(o);void 0!==s&&s.destroy(t),S(n)}}}syncPropertiesFromLexical(e,t,n){b(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(t,n,o,s,i,l){const c=this._children[n],a=e.$getNodeByKeyOrThrow(o);c instanceof E&&e.$isElementNode(a)?(c.syncPropertiesFromLexical(t,a,s),c.syncChildrenFromLexical(t,a,s,i,l)):c instanceof r&&e.$isTextNode(a)?c.syncPropertiesAndTextFromLexical(t,a,s):c instanceof w&&e.$isDecoratorNode(a)&&c.syncPropertiesFromLexical(t,a,s)}syncChildrenFromLexical(t,o,s,i,l){const r=this.getPrevNode(s),c=null===r?[]:n.$createChildrenArray(r,s),a=n.$createChildrenArray(o,null),f=c.length-1,u=a.length-1,d=t.collabNodeMap;let _,g,p=0,y=0;for(;p<=f&&y<=u;){const n=c[p],o=a[y];if(n===o)this._syncChildFromLexical(t,y,o,s,i,l),p++,y++;else{void 0===_&&(_=new Set(c)),void 0===g&&(g=new Set(a));const s=g.has(n),i=_.has(o);if(s){const n=h(t,e.$getNodeByKeyOrThrow(o),this);d.set(o,n),i?(this.splice(t,y,1,n),p++,y++):(this.splice(t,y,0,n),y++)}else this.splice(t,y,1),p++}}const x=p>f,m=y>u;if(x&&!m)for(;y<=u;++y){const n=a[y],o=h(t,e.$getNodeByKeyOrThrow(n),this);this.append(o),d.set(n,o)}else if(m&&!x)for(let e=this._children.length-1;e>=y;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 E)t.insertEmbed(s,e._xmlText);else if(e instanceof r){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof i?t.insertEmbed(s,e._map):e instanceof w&&t.insertEmbed(s,e._xmlElem);this._children.push(e)}splice(e,t,n,o){const l=this._children,c=l[t];if(void 0===c)return void 0===o&&s(94),void this.append(o);const a=c.getOffset();-1===a&&s(95);const f=this._xmlText;if(0!==n&&f.delete(a,c.getSize()),o instanceof E)f.insertEmbed(a,o._xmlText);else if(o instanceof r){const e=o._map;null===e.parent&&f.insertEmbed(a,e),f.insert(a+1,o._text)}else o instanceof i?f.insertEmbed(a,o._map):o instanceof w&&f.insertEmbed(a,o._xmlElem);if(0!==n){const o=l.slice(t,t+n);for(let t=0;t<o.length;t++)o[t].destroy(e)}void 0!==o?l.splice(t,n,o):l.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 M(e,t,n){const o=new E(e,t,n);return e._collabNode=o,o}function O(n,o){const i=o.collabNodeMap.get(n.key);if(void 0===i)return null;let l=n.offset,c=i.getSharedType();if(i instanceof r){c=i._parent._xmlText;const e=i.getOffset();if(-1===e)return null;l=e+1+l}else if(i instanceof E&&"element"===n.type){const t=n.getNode();e.$isElementNode(t)||s(184);let o=0,i=0,r=t.getFirstChild();for(;null!==r&&i++<l;)e.$isTextNode(r)?o+=r.getTextContentSize()+1:o++,r=r.getNextSibling();l=o}return t.createRelativePositionFromTypeIndex(c,l)}function F(e,n){return t.createAbsolutePositionFromRelativePosition(e,n.doc)}function L(e,n){if(null==e){if(null!=n)return!0}else if(null==n||!t.compareRelativePositions(e,n))return!0;return!1}function A(e,t){return{color:t,name:e,selection:null}}function D(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 j(e,t){const n=t.selection;null!==n&&D(e,n)}function z(e,t,n,o,s){const i=e.color,l=document.createElement("span");l.style.cssText=`position:absolute;top:0;bottom:0;right:-1px;width:1px;background-color:${i};z-index:10;`;const r=document.createElement("span");return r.textContent=e.name,r.style.cssText=`position:absolute;left:-2px;top:-16px;background-color:${i};color:#fff;line-height:12px;font-size:12px;padding:2px;font-family:Arial;font-weight:bold;white-space:nowrap;`,l.appendChild(r),{anchor:{key:t,offset:n},caret:l,color:i,focus:{key:o,offset:s},name:r,selections:[]}}function Y(t,n,s,i){const l=t.editor,r=l.getRootElement(),c=t.cursorsContainer;if(null===c||null===r)return;const a=c.offsetParent;if(null===a)return;const f=a.getBoundingClientRect(),u=n.selection;if(null===s)return null===u?void 0:(n.selection=null,void D(t,u));n.selection=s;const d=s.caret,_=s.color,h=s.selections,g=s.anchor,p=s.focus,y=g.key,x=p.key,m=i.get(y),N=i.get(x);if(null==m||null==N)return;let k;if(m===N&&e.$isLineBreakNode(m)){k=[l.getElementByKey(y).getBoundingClientRect()]}else{const e=o.createDOMRange(l,m,g.offset,N,p.offset);if(null===e)return;k=o.createRectsFromDOMRange(l,e)}const b=h.length,T=k.length;for(let e=0;e<T;e++){const t=k[e];let n=h[e];if(void 0===n){n=document.createElement("span"),h[e]=n;const t=document.createElement("span");n.appendChild(t),c.appendChild(n)}const o=`position:absolute;top:${t.top-f.top}px;left:${t.left-f.left}px;height:${t.height}px;width:${t.width}px;pointer-events:none;z-index:5;`;n.style.cssText=o,n.firstChild.style.cssText=`${o}left:0;top:0;background-color:${_};opacity:0.3;`,e===T-1&&d.parentNode!==n&&n.appendChild(d)}for(let e=b-1;e>=T;e--){const t=h[e];c.removeChild(t),h.pop()}}function K(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,i=0,l=null,r=0;if(null!==n&&null!==o){const t=F(n,e),c=F(o,e);null!==t&&null!==c&&([s,i]=W(t.type,t.index),[l,r]=W(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:i,focusCollabNode:l,focusOffset:r}}function R(t,n){const o=n.awareness.getLocalState();if(null===o)return;const{anchorCollabNode:s,anchorOffset:i,focusCollabNode:l,focusOffset:r}=K(t,o);if(null!==s&&null!==l){const t=s.getKey(),n=l.getKey(),o=e.$getSelection();if(!e.$isRangeSelection(o))return;B(o.anchor,t,i),B(o.focus,n,r)}}function B(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 W(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof E){const{node:e,offset:o}=$(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function I(e,t){const n=Array.from(t.awareness.getStates()),o=e.clientID,s=e.cursors,i=e.editor._editorState._nodeMap,l=new Set;for(let t=0;t<n.length;t++){const r=n[t],[c,a]=r;if(c!==o){l.add(c);const{name:t,color:n,focusing:o}=a;let r=null,f=s.get(c);if(void 0===f&&(f=A(t,n),s.set(c,f)),o){const{anchorCollabNode:t,anchorOffset:n,focusCollabNode:o,focusOffset:s}=K(e,a);if(null!==t&&null!==o){const e=t.getKey(),i=o.getKey();if(r=f.selection,null===r)r=z(f,e,n,i,s);else{const t=r.anchor,o=r.focus;t.key=e,t.offset=n,o.key=i,o.offset=s}}}Y(e,f,r,i)}}const r=Array.from(s.keys());for(let t=0;t<r.length;t++){const n=r[t];if(!l.has(n)){const t=s.get(n);void 0!==t&&(j(e,t),s.delete(n))}}}function U(t,n,o,s){const i=n.awareness,l=i.getLocalState();if(null===l)return;const{anchorPos:r,focusPos:c,name:a,color:f,focusing:u,awarenessData:d}=l;let _=null,h=null;(null!==s&&(null===r||s.is(o))||null!==o)&&(e.$isRangeSelection(s)&&(_=O(s.anchor,t),h=O(s.focus,t)),(L(r,_)||L(c,h))&&i.setLocalState({...l,anchorPos:_,awarenessData:d,color:f,focusPos:h,focusing:u,name:a}))}function X(n,o){if(o instanceof t.YMapEvent&&function(n,o){const{target:s}=o;if(!s._item||"__state"!==s._item.parentSub||void 0!==g(s)||!(s.parent instanceof t.XmlText||s.parent instanceof t.XmlElement||s.parent instanceof t.Map))return!1;const i=p(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,o))return;const{target:i}=o,l=p(n,i);if(l instanceof E&&o instanceof t.YTextEvent){const{keysChanged:e,childListChanged:t,delta:s}=o;e.size>0&&l.syncPropertiesFromYjs(n,e),t&&(l.applyChildrenYjsDelta(n,s),l.syncChildrenFromYjs(n))}else if(l instanceof r&&o instanceof t.YMapEvent){const{keysChanged:e}=o;e.size>0&&l.syncPropertiesAndTextFromYjs(n,e)}else if(l instanceof w&&o instanceof t.YXmlEvent){const{attributesChanged:e}=o;e.size>0&&l.syncPropertiesFromYjs(n,e)}else s(82)}const q=e.createCommand("CONNECTED_COMMAND"),G=e.createCommand("TOGGLE_CONNECT_COMMAND");exports.CONNECTED_COMMAND=q,exports.TOGGLE_CONNECT_COMMAND=G,exports.createBinding=function(e,n,o,i,l,r){null==i&&s(81);const c=M(i.get("root",t.XmlText),null,"root");return c._key="root",{clientID:i.clientID,collabNodeMap:new Map,cursors:new Map,cursorsContainer:null,doc:i,docMap:l,editor:e,excludedProperties:r||new Map,id:o,nodeProperties:new Map,root:c}},exports.createUndoManager=function(e,n){return new t.UndoManager(n,{trackedOrigins:new Set([e,null])})},exports.getAnchorAndFocusCollabNodesForUserState=K,exports.initLocalState=function(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})},exports.setLocalStateFocus=function(e,t,n,o,s){const{awareness:i}=e;let l=i.getLocalState();null===l&&(l={anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t}),l.focusing=o,i.setLocalState(l)},exports.syncCursorPositions=I,exports.syncLexicalUpdateToYjs=function(t,n,o,s,i,l,c,a){!function(e,t){e.doc.transact(t,e)}(t,(()=>{s.read((()=>{if(a.has("collaboration")||a.has("historic"))return void(c.size>0&&function(t,n){const o=Array.from(n),s=t.collabNodeMap,i=[],l=[];for(let t=0;t<o.length;t++){const n=o[t],c=e.$getNodeByKey(n),a=s.get(n);if(a instanceof r)if(e.$isTextNode(c))i.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();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,c));if(i.has("root")){const n=o._nodeMap,s=e.$getRoot(),r=t.root;r.syncPropertiesFromLexical(t,s,n),r.syncChildrenFromLexical(t,s,n,i,l)}const s=e.$getSelection(),f=o._selection;U(t,n,f,s)}))}))},exports.syncYjsChangesToLexical=function(t,n,o,s,i=I){const l=t.editor,r=l._editorState;o.forEach((e=>e.delta)),l.update((()=>{for(let e=0;e<o.length;e++){const n=o[e];X(t,n)}const s=e.$getSelection();if(e.$isRangeSelection(s))if(C(s)){const o=r._selection;if(e.$isRangeSelection(o)&&(R(t,n),C(s))){v(s.anchor.key,r)}U(t,n,o,e.$getSelection())}else R(t,n)}),{onUpdate:()=>{i(t,n),l.update((()=>{0===e.$getRoot().getChildrenSize()&&e.$getRoot().append(e.$createParagraphNode())}))},skipTransforms:!0,tag:s?"historic":"collaboration"})};
|
package/LexicalYjs.prod.mjs
CHANGED
|
@@ -6,4 +6,4 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import{$getNodeByKey as e,$isLineBreakNode as t,$isTextNode as n,$getSelection as o,$isRangeSelection as s,$isElementNode as l,$isDecoratorNode as i,createEditor as r,$getWritableNodeState as c,$getRoot as a,$isRootNode as f,$getNodeByKeyOrThrow as u,$createParagraphNode as d,createCommand as _}from"lexical";import{XmlText as h,Map as p,XmlElement as g,Doc as y,createAbsolutePositionFromRelativePosition as x,createRelativePositionFromTypeIndex as m,compareRelativePositions as k,YMapEvent as b,YTextEvent as v,YXmlEvent as C,UndoManager as N}from"yjs";import{$createChildrenArray as S}from"@lexical/offset";import{createDOMRange as w,createRectsFromDOMRange as P}from"@lexical/selection";function T(e,...t){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",e);for(const e of t)o.append("v",e);throw n.search=o.toString(),Error(`Minified Lexical error #${e}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}class F{constructor(e,t){this._key="",this._map=e,this._parent=t,this._type="linebreak"}getNode(){const n=e(this._key);return t(n)?n:null}getKey(){return this._key}getSharedType(){return this._map}getType(){return this._type}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function O(e,t){const n=new F(e,t);return e._collabNode=n,n}class E{constructor(e,t,n,o){this._key="",this._map=e,this._parent=n,this._text=t,this._type=o,this._normalized=!1}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return n(t)?t:null}getNode(){const t=e(this._key);return n(t)?t:null}getSharedType(){return this._map}getType(){return this._type}getKey(){return this._key}getSize(){return this._text.length+(this._normalized?0:1)}getOffset(){return this._parent.getChildOffset(this)}spliceText(e,t,n){const o=this._parent._xmlText,s=this.getOffset()+1+e;0!==t&&o.delete(s,t),""!==n&&o.insert(s,n)}syncPropertiesAndTextFromLexical(e,t,n){const l=this.getPrevNode(n),i=t.__text;if(G(e,this._map,l,t),null!==l){const e=l.__text;if(e!==i){!function(e,t,n,l){const i=o();let r=l.length;if(s(i)&&i.isCollapsed()){const e=i.anchor;e.key===t&&(r=e.offset)}const c=function(e,t,n){const o=e.length,s=t.length;let l=0,i=0;for(;l<o&&l<s&&e[l]===t[l]&&l<n;)l++;for(;i+l<o&&i+l<s&&e[o-i-1]===t[s-i-1];)i++;for(;i+l<o&&i+l<s&&e[l]===t[l];)l++;return{index:l,insert:t.slice(l,s-i),remove:o-l-i}}(n,l,r);e.spliceText(c.index,c.remove,c.insert)}(this,t.__key,e,i),this._text=i}}}syncPropertiesAndTextFromYjs(e,t){const n=this.getNode();null===n&&T(84),$(e,this._map,n,t);const o=this._text;if(n.__text!==o){n.getWritable().__text=o}}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function M(e,t,n,o){const s=new E(e,t,n,o);return e._collabNode=s,s}const L=new Set(["__key","__parent","__next","__prev","__state"]),z=new Set(["__first","__last","__size"]),j=new Set(["__cachedText"]),A=new Set(["__text"]);function Y(e,t,o){if(L.has(e)||"function"==typeof t[e])return!0;if(n(t)){if(A.has(e))return!0}else if(l(t)&&(z.has(e)||f(t)&&j.has(e)))return!0;const s=t.constructor,i=o.excludedProperties.get(s);return null!=i&&i.has(e)}function W(e,o,s){const r=o.__type;let c;if(l(o)){c=te(new h,s,r),c.syncPropertiesFromLexical(e,o,null),c.syncChildrenFromLexical(e,o,null,null,null)}else if(n(o)){c=M(new p,o.__text,s,r),c.syncPropertiesAndTextFromLexical(e,o,null)}else if(t(o)){const e=new p;e.set("__type","linebreak"),c=O(e,s)}else if(i(o)){c=Z(new g,s,r),c.syncPropertiesFromLexical(e,o,null)}else T(86);return c._key=o.__key,c}function D(e){const t=R(e,"__type");return"string"!=typeof t&&void 0!==t&&T(87),t}function I(e,t,n){const o=t._collabNode;if(void 0===o){const o=e.editor._nodes,s=D(t);"string"!=typeof s&&T(87);void 0===o.get(s)&&T(88,s);const l=t.parent,i=void 0===n&&null!==l?I(e,l):n||null;if(i instanceof ee||T(89),t instanceof h)return te(t,i,s);if(t instanceof p)return"linebreak"===s?O(t,i):M(t,"",i,s);if(t instanceof g)return Z(t,i,s)}return o}function K(e,t,n){const o=t.getType(),s=e.editor._nodes.get(o);void 0===s&&T(88,o);const l=new s.klass;if(l.__parent=n,t._key=l.__key,t instanceof ee){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof E?t.syncPropertiesAndTextFromYjs(e,null):t instanceof X&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(l.__key,t),l}function $(e,t,n,o){const s=null===o?t instanceof p?Array.from(t.keys()):Object.keys(t.getAttributes()):Array.from(o);let l;for(let o=0;o<s.length;o++){const i=s[o];if(Y(i,n,e)){"__state"===i&&(l||(l=n.getWritable()),B(e,t,l));continue}const c=n[i];let a=R(t,i);if(c!==a){if(a instanceof y){const t=e.docMap;c instanceof y&&t.delete(c.guid);const n=r(),o=a.guid;n._key=o,t.set(o,a),a=n}void 0===l&&(l=n.getWritable()),l[i]=a}}}function R(e,t){return e instanceof p?e.get(t):e.getAttribute(t)}function U(e,t,n){e instanceof p?e.set(t,n):e.setAttribute(t,n)}function B(e,t,n){const o=R(t,"__state");o instanceof p&&c(n).updateFromJSON(o.toJSON())}function G(e,t,n,o){const s=o.__type,l=e.nodeProperties;let i=l.get(s);void 0===i&&(i=Object.keys(o).filter((t=>!Y(t,o,e))),l.set(s,i));const r=e.editor.constructor;!function(e,t,n,o){const s=o.__state,l=R(t,"__state");if(!s)return;const[i,r]=s.getInternalState(),c=n&&n.__state,a=l instanceof p?l:new p;if(c===s)return;const[f,u]=c&&a.doc?c.getInternalState():[void 0,new Map];if(i)for(const[e,t]of Object.entries(i))f&&t!==f[e]&&a.set(e,t);for(const[e,t]of r)u.get(e)!==t&&a.set(e.key,e.unparse(t));l||U(t,"__state",a)}(0,t,n,o);for(let s=0;s<i.length;s++){const l=i[s],c=null===n?void 0:n[l];let a=o[l];if(c!==a){if(a instanceof r){const t=e.docMap;let n;if(c instanceof r){const e=c._key;n=t.get(e),t.delete(e)}const s=n||new y,l=s.guid;a._key=l,t.set(l,s),a=s,e.editor.update((()=>{o.markDirty()}))}U(t,l,a)}}}function J(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function q(e,t,n){let o=0,s=0;const l=e._children,i=l.length;for(;s<i;s++){const e=l[s],r=o;o+=e.getSize();if((n?o>=t:o>t)&&e instanceof E){let n=t-r-1;n<0&&(n=0);return{length:o-t,node:e,nodeIndex:s,offset:n}}if(o>t)return{length:0,node:e,nodeIndex:s,offset:r};if(s===i-1)return{length:0,node:null,nodeIndex:s+1,offset:r+1}}return{length:0,node:null,nodeIndex:0,offset:0}}function H(e){const t=e.anchor,o=e.focus;let s=!1;try{const e=t.getNode(),l=o.getNode();(!e.isAttached()||!l.isAttached()||n(e)&&t.offset>e.getTextContentSize()||n(l)&&o.offset>l.getTextContentSize())&&(s=!0)}catch(e){s=!0}return s}function Q(e){const t=e.getParent();if(null!==t){const n=e.getWritable(),o=t.getWritable(),s=e.getPreviousSibling(),l=e.getNextSibling();if(null===s)if(null!==l){const e=l.getWritable();o.__first=l.__key,e.__prev=null}else o.__first=null;else{const e=s.getWritable();if(null!==l){const t=l.getWritable();t.__prev=e.__key,e.__next=t.__key}else e.__next=null;n.__prev=null}if(null===l)if(null!==s){const e=s.getWritable();o.__last=s.__key,e.__next=null}else o.__last=null;else{const e=l.getWritable();if(null!==s){const t=s.getWritable();t.__next=e.__key,e.__prev=t.__key}else e.__prev=null;n.__next=null}o.__size--,n.__parent=null}}function V(t,n){const o=n._nodeMap.get(t);if(!o)return void a().selectStart();const s=o.__prev;let l=null;s&&(l=e(s)),null===l&&null!==o.__parent&&(l=e(o.__parent)),null!==l?null!==l&&l.isAttached()?l.selectEnd():V(l.__key,n):a().selectStart()}class X{constructor(e,t,n){this._key="",this._xmlElem=e,this._parent=t,this._type=n}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return i(t)?t:null}getNode(){const t=e(this._key);return i(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);G(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&T(83);$(e,this._xmlElem,n,t)}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function Z(e,t,n){const o=new X(e,t,n);return e._collabNode=o,o}class ee{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 l(t)?t:null}getNode(){const t=e(this._key);return l(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&&T(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&T(91),$(e,this._xmlText,n,t)}applyChildrenYjsDelta(e,t){const n=this._children;let o=0,s=null;for(let l=0;l<t.length;l++){const i=t[l],r=i.insert,c=i.delete;if(null!=i.retain)o+=i.retain;else if("number"==typeof c){let e=c;for(;e>0;){const{node:t,nodeIndex:s,offset:l,length:i}=q(this,o,!1);if(t instanceof ee||t instanceof F||t instanceof X)n.splice(s,1),e-=1;else{if(!(t instanceof E))break;{const o=Math.min(e,i),r=0!==s?n[s-1]:null,c=t.getSize();if(0===l&&i===c){n.splice(s,1);const e=J(t._text,l,o-1,"");e.length>0&&(r instanceof E?r._text+=e:this._xmlText.delete(l,e.length))}else t._text=J(t._text,l,o,"");e-=o}}}}else{if(null==r)throw new Error("Unexpected delta format");if("string"==typeof r){const{node:e,offset:t}=q(this,o,!0);e instanceof E?e._text=J(e._text,t,0,r):this._xmlText.delete(t,r.length),o+=r.length}else{const t=r,{node:l,nodeIndex:i,length:c}=q(this,o,!1),a=I(e,t,this);if(l instanceof E&&c>0&&c<l._text.length){const e=l._text,t=e.length-c;l._text=J(e,t,c,""),n.splice(i+1,0,a),s=J(e,0,t,"")}else n.splice(i,0,a);null!==s&&a instanceof E&&(a._text=s+a._text,s=null),o+=1}}}}syncChildrenFromYjs(e){const t=this.getNode();null===t&&T(92);const o=t.__key,s=S(t,null),l=s.length,i=this._children,r=i.length,c=e.collabNodeMap,a=new Set;let f,d,_=0,h=null;r!==l&&(d=t.getWritable());for(let l=0;l<r;l++){const p=s[_],g=i[l],y=g.getNode(),x=g._key;if(null!==y&&p===x){const t=n(y);if(a.add(p),t)if(g._key=p,g instanceof ee){const t=g._xmlText;g.syncPropertiesFromYjs(e,null),g.applyChildrenYjsDelta(e,t.toDelta()),g.syncChildrenFromYjs(e)}else g instanceof E?g.syncPropertiesAndTextFromYjs(e,null):g instanceof X?g.syncPropertiesFromYjs(e,null):g instanceof F||T(93);h=y,_++}else{if(void 0===f){f=new Set;for(let e=0;e<r;e++){const t=i[e]._key;""!==t&&f.add(t)}}if(null!==y&&void 0!==p&&!f.has(p)){Q(u(p)),l--,_++;continue}d=t.getWritable();const n=K(e,g,o),s=n.__key;if(c.set(s,g),null===h){const e=d.getFirstChild();if(d.__first=s,null!==e){const t=e.getWritable();t.__prev=s,n.__next=t.__key}}else{const e=h.getWritable(),t=h.getNextSibling();if(e.__next=s,n.__prev=h.__key,null!==t){const e=t.getWritable();e.__prev=s,n.__next=e.__key}}l===r-1&&(d.__last=s),d.__size++,h=n}}for(let t=0;t<l;t++){const n=s[t];if(!a.has(n)){const t=u(n),o=e.collabNodeMap.get(n);void 0!==o&&o.destroy(e),Q(t)}}}syncPropertiesFromLexical(e,t,n){G(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(e,t,o,s,r,c){const a=this._children[t],f=u(o);a instanceof ee&&l(f)?(a.syncPropertiesFromLexical(e,f,s),a.syncChildrenFromLexical(e,f,s,r,c)):a instanceof E&&n(f)?a.syncPropertiesAndTextFromLexical(e,f,s):a instanceof X&&i(f)&&a.syncPropertiesFromLexical(e,f,s)}syncChildrenFromLexical(e,t,n,o,s){const l=this.getPrevNode(n),i=null===l?[]:S(l,n),r=S(t,null),c=i.length-1,a=r.length-1,f=e.collabNodeMap;let d,_,h=0,p=0;for(;h<=c&&p<=a;){const t=i[h],l=r[p];if(t===l)this._syncChildFromLexical(e,p,l,n,o,s),h++,p++;else{void 0===d&&(d=new Set(i)),void 0===_&&(_=new Set(r));const n=_.has(t),o=d.has(l);if(n){const t=W(e,u(l),this);f.set(l,t),o?(this.splice(e,p,1,t),h++,p++):(this.splice(e,p,0,t),p++)}else this.splice(e,p,1),h++}}const g=h>c,y=p>a;if(g&&!y)for(;p<=a;++p){const t=r[p],n=W(e,u(t),this);this.append(n),f.set(t,n)}else if(y&&!g)for(let t=this._children.length-1;t>=p;t--)this.splice(e,t,1)}append(e){const t=this._xmlText,n=this._children,o=n[n.length-1],s=void 0!==o?o.getOffset()+o.getSize():0;if(e instanceof ee)t.insertEmbed(s,e._xmlText);else if(e instanceof E){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof F?t.insertEmbed(s,e._map):e instanceof X&&t.insertEmbed(s,e._xmlElem);this._children.push(e)}splice(e,t,n,o){const s=this._children,l=s[t];if(void 0===l)return void 0===o&&T(94),void this.append(o);const i=l.getOffset();-1===i&&T(95);const r=this._xmlText;if(0!==n&&r.delete(i,l.getSize()),o instanceof ee)r.insertEmbed(i,o._xmlText);else if(o instanceof E){const e=o._map;null===e.parent&&r.insertEmbed(i,e),r.insert(i+1,o._text)}else o instanceof F?r.insertEmbed(i,o._map):o instanceof X&&r.insertEmbed(i,o._xmlElem);if(0!==n){const o=s.slice(t,t+n);for(let t=0;t<o.length;t++)o[t].destroy(e)}void 0!==o?s.splice(t,n,o):s.splice(t,n)}getChildOffset(e){let t=0;const n=this._children;for(let o=0;o<n.length;o++){const s=n[o];if(s===e)return t;t+=s.getSize()}return-1}destroy(e){const t=e.collabNodeMap,n=this._children;for(let t=0;t<n.length;t++)n[t].destroy(e);t.get(this._key)===this&&t.delete(this._key)}}function te(e,t,n){const o=new ee(e,t,n);return e._collabNode=o,o}function ne(e,t,n,o,s,l){null==o&&T(81);const i=te(o.get("root",h),null,"root");return i._key="root",{clientID:o.clientID,collabNodeMap:new Map,cursors:new Map,cursorsContainer:null,doc:o,docMap:s,editor:e,excludedProperties:l||new Map,id:n,nodeProperties:new Map,root:i}}function oe(e,t){const o=t.collabNodeMap.get(e.key);if(void 0===o)return null;let s=e.offset,i=o.getSharedType();if(o instanceof E){i=o._parent._xmlText;const e=o.getOffset();if(-1===e)return null;s=e+1+s}else if(o instanceof ee&&"element"===e.type){const t=e.getNode();l(t)||T(184);let o=0,i=0,r=t.getFirstChild();for(;null!==r&&i++<s;)n(r)?o+=r.getTextContentSize()+1:o++,r=r.getNextSibling();s=o}return m(i,s)}function se(e,t){return x(e,t.doc)}function le(e,t){if(null==e){if(null!=t)return!0}else if(null==t||!k(e,t))return!0;return!1}function ie(e,t){return{color:t,name:e,selection:null}}function re(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 ce(e,t){const n=t.selection;null!==n&&re(e,n)}function ae(e,t,n,o,s){const l=e.color,i=document.createElement("span");i.style.cssText=`position:absolute;top:0;bottom:0;right:-1px;width:1px;background-color:${l};z-index:10;`;const r=document.createElement("span");return r.textContent=e.name,r.style.cssText=`position:absolute;left:-2px;top:-16px;background-color:${l};color:#fff;line-height:12px;font-size:12px;padding:2px;font-family:Arial;font-weight:bold;white-space:nowrap;`,i.appendChild(r),{anchor:{key:t,offset:n},caret:i,color:l,focus:{key:o,offset:s},name:r,selections:[]}}function fe(e,n,o,s){const l=e.editor,i=l.getRootElement(),r=e.cursorsContainer;if(null===r||null===i)return;const c=r.offsetParent;if(null===c)return;const a=c.getBoundingClientRect(),f=n.selection;if(null===o)return null===f?void 0:(n.selection=null,void re(e,f));n.selection=o;const u=o.caret,d=o.color,_=o.selections,h=o.anchor,p=o.focus,g=h.key,y=p.key,x=s.get(g),m=s.get(y);if(null==x||null==m)return;let k;if(x===m&&t(x)){k=[l.getElementByKey(g).getBoundingClientRect()]}else{const e=w(l,x,h.offset,m,p.offset);if(null===e)return;k=P(l,e)}const b=_.length,v=k.length;for(let e=0;e<v;e++){const t=k[e];let n=_[e];if(void 0===n){n=document.createElement("span"),_[e]=n;const t=document.createElement("span");n.appendChild(t),r.appendChild(n)}const o=`position:absolute;top:${t.top-a.top}px;left:${t.left-a.left}px;height:${t.height}px;width:${t.width}px;pointer-events:none;z-index:5;`;n.style.cssText=o,n.firstChild.style.cssText=`${o}left:0;top:0;background-color:${d};opacity:0.3;`,e===v-1&&u.parentNode!==n&&n.appendChild(u)}for(let e=b-1;e>=v;e--){const t=_[e];r.removeChild(t),_.pop()}}function ue(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,l=0,i=null,r=0;if(null!==n&&null!==o){const t=se(n,e),c=se(o,e);null!==t&&null!==c&&([s,l]=he(t.type,t.index),[i,r]=he(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:l,focusCollabNode:i,focusOffset:r}}function de(e,t){const n=t.awareness.getLocalState();if(null===n)return;const{anchorCollabNode:l,anchorOffset:i,focusCollabNode:r,focusOffset:c}=ue(e,n);if(null!==l&&null!==r){const e=l.getKey(),t=r.getKey(),n=o();if(!s(n))return;_e(n.anchor,e,i),_e(n.focus,t,c)}}function _e(t,o,s){if(t.key!==o||t.offset!==s){let i=e(o);if(null!==i&&!l(i)&&!n(i)){const e=i.getParentOrThrow();o=e.getKey(),s=i.getIndexWithinParent(),i=e}t.set(o,s,l(i)?"element":"text")}}function he(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof ee){const{node:e,offset:o}=q(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function pe(e,t){const n=Array.from(t.awareness.getStates()),o=e.clientID,s=e.cursors,l=e.editor._editorState._nodeMap,i=new Set;for(let t=0;t<n.length;t++){const r=n[t],[c,a]=r;if(c!==o){i.add(c);const{name:t,color:n,focusing:o}=a;let r=null,f=s.get(c);if(void 0===f&&(f=ie(t,n),s.set(c,f)),o){const{anchorCollabNode:t,anchorOffset:n,focusCollabNode:o,focusOffset:s}=ue(e,a);if(null!==t&&null!==o){const e=t.getKey(),l=o.getKey();if(r=f.selection,null===r)r=ae(f,e,n,l,s);else{const t=r.anchor,o=r.focus;t.key=e,t.offset=n,o.key=l,o.offset=s}}}fe(e,f,r,l)}}const r=Array.from(s.keys());for(let t=0;t<r.length;t++){const n=r[t];if(!i.has(n)){const t=s.get(n);void 0!==t&&(ce(e,t),s.delete(n))}}}function ge(e,t,n,o){const l=t.awareness,i=l.getLocalState();if(null===i)return;const{anchorPos:r,focusPos:c,name:a,color:f,focusing:u,awarenessData:d}=i;let _=null,h=null;(null!==o&&(null===r||o.is(n))||null!==n)&&(s(o)&&(_=oe(o.anchor,e),h=oe(o.focus,e)),(le(r,_)||le(c,h))&&l.setLocalState({...i,anchorPos:_,awarenessData:d,color:f,focusPos:h,focusing:u,name:a}))}function ye(e,t){if(t instanceof b&&function(e,t){const{target:n}=t;if(!n._item||"__state"!==n._item.parentSub||void 0!==D(n)||!(n.parent instanceof h||n.parent instanceof g||n.parent instanceof p))return!1;const o=I(e,n.parent).getNode();if(o){const e=c(o.getWritable());for(const o of t.keysChanged)e.updateFromUnknown(o,n.get(o))}return!0}(e,t))return;const{target:n}=t,o=I(e,n);if(o instanceof ee&&t instanceof v){const{keysChanged:n,childListChanged:s,delta:l}=t;n.size>0&&o.syncPropertiesFromYjs(e,n),s&&(o.applyChildrenYjsDelta(e,l),o.syncChildrenFromYjs(e))}else if(o instanceof E&&t instanceof b){const{keysChanged:n}=t;n.size>0&&o.syncPropertiesAndTextFromYjs(e,n)}else if(o instanceof X&&t instanceof C){const{attributesChanged:n}=t;n.size>0&&o.syncPropertiesFromYjs(e,n)}else T(82)}function xe(e,t,n,l,i=pe){const r=e.editor,c=r._editorState;n.forEach((e=>e.delta)),r.update((()=>{for(let t=0;t<n.length;t++){const o=n[t];ye(e,o)}const l=o();if(s(l))if(H(l)){const n=c._selection;if(s(n)&&(de(e,t),H(l))){V(l.anchor.key,c)}ge(e,t,n,o())}else de(e,t)}),{onUpdate:()=>{i(e,t),r.update((()=>{0===a().getChildrenSize()&&a().append(d())}))},skipTransforms:!0,tag:l?"historic":"collaboration"})}function me(t,s,l,i,r,c,f,u){!function(e,t){e.doc.transact(t,e)}(t,(()=>{i.read((()=>{if(u.has("collaboration")||u.has("historic"))return void(f.size>0&&function(t,o){const s=Array.from(o),l=t.collabNodeMap,i=[],r=[];for(let t=0;t<s.length;t++){const o=s[t],c=e(o),a=l.get(o);if(a instanceof E)if(n(c))i.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),r.push(a)}}for(let e=0;e<r.length;e++){const t=r[e],n=t.getKey();l.delete(n);const o=t._parent._children,s=o.indexOf(t);o.splice(s,1)}for(let e=0;e<i.length;e++){const[t,n]=i[e];t._text=n}}(t,f));if(r.has("root")){const e=l._nodeMap,n=a(),o=t.root;o.syncPropertiesFromLexical(t,n,e),o.syncChildrenFromLexical(t,n,e,r,c)}const i=o(),d=l._selection;ge(t,s,d,i)}))}))}const ke=_("CONNECTED_COMMAND"),be=_("TOGGLE_CONNECT_COMMAND");function ve(e,t){return new N(t,{trackedOrigins:new Set([e,null])})}function Ce(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})}function Ne(e,t,n,o,s){const{awareness:l}=e;let i=l.getLocalState();null===i&&(i={anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t}),i.focusing=o,l.setLocalState(i)}export{ke as CONNECTED_COMMAND,be as TOGGLE_CONNECT_COMMAND,ne as createBinding,ve as createUndoManager,ue as getAnchorAndFocusCollabNodesForUserState,Ce as initLocalState,Ne as setLocalStateFocus,pe as syncCursorPositions,me as syncLexicalUpdateToYjs,xe as syncYjsChangesToLexical};
|
|
9
|
+
import{$getNodeByKey as e,$isLineBreakNode as t,$isTextNode as n,$getSelection as o,$isRangeSelection as s,$isElementNode as l,$isDecoratorNode as i,createEditor as r,$getWritableNodeState as c,$getRoot as a,$isRootNode as f,$getNodeByKeyOrThrow as u,$createParagraphNode as _,createCommand as d}from"lexical";import{XmlText as h,Map as p,XmlElement as g,Doc as y,createAbsolutePositionFromRelativePosition as x,createRelativePositionFromTypeIndex as m,compareRelativePositions as k,YMapEvent as b,YTextEvent as v,YXmlEvent as C,UndoManager as N}from"yjs";import{$createChildrenArray as S}from"@lexical/offset";import{createDOMRange as w,createRectsFromDOMRange as P}from"@lexical/selection";function T(e,...t){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",e);for(const e of t)o.append("v",e);throw n.search=o.toString(),Error(`Minified Lexical error #${e}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}class F{constructor(e,t){this._key="",this._map=e,this._parent=t,this._type="linebreak"}getNode(){const n=e(this._key);return t(n)?n:null}getKey(){return this._key}getSharedType(){return this._map}getType(){return this._type}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function O(e,t){const n=new F(e,t);return e._collabNode=n,n}class E{constructor(e,t,n,o){this._key="",this._map=e,this._parent=n,this._text=t,this._type=o,this._normalized=!1}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return n(t)?t:null}getNode(){const t=e(this._key);return n(t)?t:null}getSharedType(){return this._map}getType(){return this._type}getKey(){return this._key}getSize(){return this._text.length+(this._normalized?0:1)}getOffset(){return this._parent.getChildOffset(this)}spliceText(e,t,n){const o=this._parent._xmlText,s=this.getOffset()+1+e;0!==t&&o.delete(s,t),""!==n&&o.insert(s,n)}syncPropertiesAndTextFromLexical(e,t,n){const l=this.getPrevNode(n),i=t.__text;if(G(e,this._map,l,t),null!==l){const e=l.__text;if(e!==i){!function(e,t,n,l){const i=o();let r=l.length;if(s(i)&&i.isCollapsed()){const e=i.anchor;e.key===t&&(r=e.offset)}const c=function(e,t,n){const o=e.length,s=t.length;let l=0,i=0;for(;l<o&&l<s&&e[l]===t[l]&&l<n;)l++;for(;i+l<o&&i+l<s&&e[o-i-1]===t[s-i-1];)i++;for(;i+l<o&&i+l<s&&e[l]===t[l];)l++;return{index:l,insert:t.slice(l,s-i),remove:o-l-i}}(n,l,r);e.spliceText(c.index,c.remove,c.insert)}(this,t.__key,e,i),this._text=i}}}syncPropertiesAndTextFromYjs(e,t){const n=this.getNode();null===n&&T(84),$(e,this._map,n,t);const o=this._text;if(n.__text!==o){n.getWritable().__text=o}}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function M(e,t,n,o){const s=new E(e,t,n,o);return e._collabNode=s,s}const L=new Set(["__key","__parent","__next","__prev","__state"]),z=new Set(["__first","__last","__size","__dir"]),j=new Set(["__cachedText"]),A=new Set(["__text"]);function Y(e,t,o){if(L.has(e)||"function"==typeof t[e])return!0;if(n(t)){if(A.has(e))return!0}else if(l(t)&&(z.has(e)||f(t)&&j.has(e)))return!0;const s=t.constructor,i=o.excludedProperties.get(s);return null!=i&&i.has(e)}function W(e,o,s){const r=o.__type;let c;if(l(o)){c=te(new h,s,r),c.syncPropertiesFromLexical(e,o,null),c.syncChildrenFromLexical(e,o,null,null,null)}else if(n(o)){c=M(new p,o.__text,s,r),c.syncPropertiesAndTextFromLexical(e,o,null)}else if(t(o)){const e=new p;e.set("__type","linebreak"),c=O(e,s)}else if(i(o)){c=Z(new g,s,r),c.syncPropertiesFromLexical(e,o,null)}else T(86);return c._key=o.__key,c}function D(e){const t=R(e,"__type");return"string"!=typeof t&&void 0!==t&&T(87),t}function I(e,t,n){const o=t._collabNode;if(void 0===o){const o=e.editor._nodes,s=D(t);"string"!=typeof s&&T(87);void 0===o.get(s)&&T(88,s);const l=t.parent,i=void 0===n&&null!==l?I(e,l):n||null;if(i instanceof ee||T(89),t instanceof h)return te(t,i,s);if(t instanceof p)return"linebreak"===s?O(t,i):M(t,"",i,s);if(t instanceof g)return Z(t,i,s)}return o}function K(e,t,n){const o=t.getType(),s=e.editor._nodes.get(o);void 0===s&&T(88,o);const l=new s.klass;if(l.__parent=n,t._key=l.__key,t instanceof ee){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof E?t.syncPropertiesAndTextFromYjs(e,null):t instanceof X&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(l.__key,t),l}function $(e,t,n,o){const s=null===o?t instanceof p?Array.from(t.keys()):Object.keys(t.getAttributes()):Array.from(o);let l;for(let o=0;o<s.length;o++){const i=s[o];if(Y(i,n,e)){"__state"===i&&(l||(l=n.getWritable()),B(e,t,l));continue}const c=n[i];let a=R(t,i);if(c!==a){if(a instanceof y){const t=e.docMap;c instanceof y&&t.delete(c.guid);const n=r(),o=a.guid;n._key=o,t.set(o,a),a=n}void 0===l&&(l=n.getWritable()),l[i]=a}}}function R(e,t){return e instanceof p?e.get(t):e.getAttribute(t)}function U(e,t,n){e instanceof p?e.set(t,n):e.setAttribute(t,n)}function B(e,t,n){const o=R(t,"__state");o instanceof p&&c(n).updateFromJSON(o.toJSON())}function G(e,t,n,o){const s=o.__type,l=e.nodeProperties;let i=l.get(s);void 0===i&&(i=Object.keys(o).filter((t=>!Y(t,o,e))),l.set(s,i));const r=e.editor.constructor;!function(e,t,n,o){const s=o.__state,l=R(t,"__state");if(!s)return;const[i,r]=s.getInternalState(),c=n&&n.__state,a=l instanceof p?l:new p;if(c===s)return;const[f,u]=c&&a.doc?c.getInternalState():[void 0,new Map];if(i)for(const[e,t]of Object.entries(i))f&&t!==f[e]&&a.set(e,t);for(const[e,t]of r)u.get(e)!==t&&a.set(e.key,e.unparse(t));l||U(t,"__state",a)}(0,t,n,o);for(let s=0;s<i.length;s++){const l=i[s],c=null===n?void 0:n[l];let a=o[l];if(c!==a){if(a instanceof r){const t=e.docMap;let n;if(c instanceof r){const e=c._key;n=t.get(e),t.delete(e)}const s=n||new y,l=s.guid;a._key=l,t.set(l,s),a=s,e.editor.update((()=>{o.markDirty()}))}U(t,l,a)}}}function J(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function q(e,t,n){let o=0,s=0;const l=e._children,i=l.length;for(;s<i;s++){const e=l[s],r=o;o+=e.getSize();if((n?o>=t:o>t)&&e instanceof E){let n=t-r-1;n<0&&(n=0);return{length:o-t,node:e,nodeIndex:s,offset:n}}if(o>t)return{length:0,node:e,nodeIndex:s,offset:r};if(s===i-1)return{length:0,node:null,nodeIndex:s+1,offset:r+1}}return{length:0,node:null,nodeIndex:0,offset:0}}function H(e){const t=e.anchor,o=e.focus;let s=!1;try{const e=t.getNode(),l=o.getNode();(!e.isAttached()||!l.isAttached()||n(e)&&t.offset>e.getTextContentSize()||n(l)&&o.offset>l.getTextContentSize())&&(s=!0)}catch(e){s=!0}return s}function Q(e){const t=e.getParent();if(null!==t){const n=e.getWritable(),o=t.getWritable(),s=e.getPreviousSibling(),l=e.getNextSibling();if(null===s)if(null!==l){const e=l.getWritable();o.__first=l.__key,e.__prev=null}else o.__first=null;else{const e=s.getWritable();if(null!==l){const t=l.getWritable();t.__prev=e.__key,e.__next=t.__key}else e.__next=null;n.__prev=null}if(null===l)if(null!==s){const e=s.getWritable();o.__last=s.__key,e.__next=null}else o.__last=null;else{const e=l.getWritable();if(null!==s){const t=s.getWritable();t.__next=e.__key,e.__prev=t.__key}else e.__prev=null;n.__next=null}o.__size--,n.__parent=null}}function V(t,n){const o=n._nodeMap.get(t);if(!o)return void a().selectStart();const s=o.__prev;let l=null;s&&(l=e(s)),null===l&&null!==o.__parent&&(l=e(o.__parent)),null!==l?null!==l&&l.isAttached()?l.selectEnd():V(l.__key,n):a().selectStart()}class X{constructor(e,t,n){this._key="",this._xmlElem=e,this._parent=t,this._type=n}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return i(t)?t:null}getNode(){const t=e(this._key);return i(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);G(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&T(83);$(e,this._xmlElem,n,t)}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function Z(e,t,n){const o=new X(e,t,n);return e._collabNode=o,o}class ee{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 l(t)?t:null}getNode(){const t=e(this._key);return l(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&&T(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&T(91),$(e,this._xmlText,n,t)}applyChildrenYjsDelta(e,t){const n=this._children;let o=0,s=null;for(let l=0;l<t.length;l++){const i=t[l],r=i.insert,c=i.delete;if(null!=i.retain)o+=i.retain;else if("number"==typeof c){let e=c;for(;e>0;){const{node:t,nodeIndex:s,offset:l,length:i}=q(this,o,!1);if(t instanceof ee||t instanceof F||t instanceof X)n.splice(s,1),e-=1;else{if(!(t instanceof E))break;{const o=Math.min(e,i),r=0!==s?n[s-1]:null,c=t.getSize();if(0===l&&i===c){n.splice(s,1);const e=J(t._text,l,o-1,"");e.length>0&&(r instanceof E?r._text+=e:this._xmlText.delete(l,e.length))}else t._text=J(t._text,l,o,"");e-=o}}}}else{if(null==r)throw new Error("Unexpected delta format");if("string"==typeof r){const{node:e,offset:t}=q(this,o,!0);e instanceof E?e._text=J(e._text,t,0,r):this._xmlText.delete(t,r.length),o+=r.length}else{const t=r,{node:l,nodeIndex:i,length:c}=q(this,o,!1),a=I(e,t,this);if(l instanceof E&&c>0&&c<l._text.length){const e=l._text,t=e.length-c;l._text=J(e,t,c,""),n.splice(i+1,0,a),s=J(e,0,t,"")}else n.splice(i,0,a);null!==s&&a instanceof E&&(a._text=s+a._text,s=null),o+=1}}}}syncChildrenFromYjs(e){const t=this.getNode();null===t&&T(92);const o=t.__key,s=S(t,null),l=s.length,i=this._children,r=i.length,c=e.collabNodeMap,a=new Set;let f,_,d=0,h=null;r!==l&&(_=t.getWritable());for(let l=0;l<r;l++){const p=s[d],g=i[l],y=g.getNode(),x=g._key;if(null!==y&&p===x){const t=n(y);if(a.add(p),t)if(g._key=p,g instanceof ee){const t=g._xmlText;g.syncPropertiesFromYjs(e,null),g.applyChildrenYjsDelta(e,t.toDelta()),g.syncChildrenFromYjs(e)}else g instanceof E?g.syncPropertiesAndTextFromYjs(e,null):g instanceof X?g.syncPropertiesFromYjs(e,null):g instanceof F||T(93);h=y,d++}else{if(void 0===f){f=new Set;for(let e=0;e<r;e++){const t=i[e]._key;""!==t&&f.add(t)}}if(null!==y&&void 0!==p&&!f.has(p)){Q(u(p)),l--,d++;continue}_=t.getWritable();const n=K(e,g,o),s=n.__key;if(c.set(s,g),null===h){const e=_.getFirstChild();if(_.__first=s,null!==e){const t=e.getWritable();t.__prev=s,n.__next=t.__key}}else{const e=h.getWritable(),t=h.getNextSibling();if(e.__next=s,n.__prev=h.__key,null!==t){const e=t.getWritable();e.__prev=s,n.__next=e.__key}}l===r-1&&(_.__last=s),_.__size++,h=n}}for(let t=0;t<l;t++){const n=s[t];if(!a.has(n)){const t=u(n),o=e.collabNodeMap.get(n);void 0!==o&&o.destroy(e),Q(t)}}}syncPropertiesFromLexical(e,t,n){G(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(e,t,o,s,r,c){const a=this._children[t],f=u(o);a instanceof ee&&l(f)?(a.syncPropertiesFromLexical(e,f,s),a.syncChildrenFromLexical(e,f,s,r,c)):a instanceof E&&n(f)?a.syncPropertiesAndTextFromLexical(e,f,s):a instanceof X&&i(f)&&a.syncPropertiesFromLexical(e,f,s)}syncChildrenFromLexical(e,t,n,o,s){const l=this.getPrevNode(n),i=null===l?[]:S(l,n),r=S(t,null),c=i.length-1,a=r.length-1,f=e.collabNodeMap;let _,d,h=0,p=0;for(;h<=c&&p<=a;){const t=i[h],l=r[p];if(t===l)this._syncChildFromLexical(e,p,l,n,o,s),h++,p++;else{void 0===_&&(_=new Set(i)),void 0===d&&(d=new Set(r));const n=d.has(t),o=_.has(l);if(n){const t=W(e,u(l),this);f.set(l,t),o?(this.splice(e,p,1,t),h++,p++):(this.splice(e,p,0,t),p++)}else this.splice(e,p,1),h++}}const g=h>c,y=p>a;if(g&&!y)for(;p<=a;++p){const t=r[p],n=W(e,u(t),this);this.append(n),f.set(t,n)}else if(y&&!g)for(let t=this._children.length-1;t>=p;t--)this.splice(e,t,1)}append(e){const t=this._xmlText,n=this._children,o=n[n.length-1],s=void 0!==o?o.getOffset()+o.getSize():0;if(e instanceof ee)t.insertEmbed(s,e._xmlText);else if(e instanceof E){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof F?t.insertEmbed(s,e._map):e instanceof X&&t.insertEmbed(s,e._xmlElem);this._children.push(e)}splice(e,t,n,o){const s=this._children,l=s[t];if(void 0===l)return void 0===o&&T(94),void this.append(o);const i=l.getOffset();-1===i&&T(95);const r=this._xmlText;if(0!==n&&r.delete(i,l.getSize()),o instanceof ee)r.insertEmbed(i,o._xmlText);else if(o instanceof E){const e=o._map;null===e.parent&&r.insertEmbed(i,e),r.insert(i+1,o._text)}else o instanceof F?r.insertEmbed(i,o._map):o instanceof X&&r.insertEmbed(i,o._xmlElem);if(0!==n){const o=s.slice(t,t+n);for(let t=0;t<o.length;t++)o[t].destroy(e)}void 0!==o?s.splice(t,n,o):s.splice(t,n)}getChildOffset(e){let t=0;const n=this._children;for(let o=0;o<n.length;o++){const s=n[o];if(s===e)return t;t+=s.getSize()}return-1}destroy(e){const t=e.collabNodeMap,n=this._children;for(let t=0;t<n.length;t++)n[t].destroy(e);t.get(this._key)===this&&t.delete(this._key)}}function te(e,t,n){const o=new ee(e,t,n);return e._collabNode=o,o}function ne(e,t,n,o,s,l){null==o&&T(81);const i=te(o.get("root",h),null,"root");return i._key="root",{clientID:o.clientID,collabNodeMap:new Map,cursors:new Map,cursorsContainer:null,doc:o,docMap:s,editor:e,excludedProperties:l||new Map,id:n,nodeProperties:new Map,root:i}}function oe(e,t){const o=t.collabNodeMap.get(e.key);if(void 0===o)return null;let s=e.offset,i=o.getSharedType();if(o instanceof E){i=o._parent._xmlText;const e=o.getOffset();if(-1===e)return null;s=e+1+s}else if(o instanceof ee&&"element"===e.type){const t=e.getNode();l(t)||T(184);let o=0,i=0,r=t.getFirstChild();for(;null!==r&&i++<s;)n(r)?o+=r.getTextContentSize()+1:o++,r=r.getNextSibling();s=o}return m(i,s)}function se(e,t){return x(e,t.doc)}function le(e,t){if(null==e){if(null!=t)return!0}else if(null==t||!k(e,t))return!0;return!1}function ie(e,t){return{color:t,name:e,selection:null}}function re(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 ce(e,t){const n=t.selection;null!==n&&re(e,n)}function ae(e,t,n,o,s){const l=e.color,i=document.createElement("span");i.style.cssText=`position:absolute;top:0;bottom:0;right:-1px;width:1px;background-color:${l};z-index:10;`;const r=document.createElement("span");return r.textContent=e.name,r.style.cssText=`position:absolute;left:-2px;top:-16px;background-color:${l};color:#fff;line-height:12px;font-size:12px;padding:2px;font-family:Arial;font-weight:bold;white-space:nowrap;`,i.appendChild(r),{anchor:{key:t,offset:n},caret:i,color:l,focus:{key:o,offset:s},name:r,selections:[]}}function fe(e,n,o,s){const l=e.editor,i=l.getRootElement(),r=e.cursorsContainer;if(null===r||null===i)return;const c=r.offsetParent;if(null===c)return;const a=c.getBoundingClientRect(),f=n.selection;if(null===o)return null===f?void 0:(n.selection=null,void re(e,f));n.selection=o;const u=o.caret,_=o.color,d=o.selections,h=o.anchor,p=o.focus,g=h.key,y=p.key,x=s.get(g),m=s.get(y);if(null==x||null==m)return;let k;if(x===m&&t(x)){k=[l.getElementByKey(g).getBoundingClientRect()]}else{const e=w(l,x,h.offset,m,p.offset);if(null===e)return;k=P(l,e)}const b=d.length,v=k.length;for(let e=0;e<v;e++){const t=k[e];let n=d[e];if(void 0===n){n=document.createElement("span"),d[e]=n;const t=document.createElement("span");n.appendChild(t),r.appendChild(n)}const o=`position:absolute;top:${t.top-a.top}px;left:${t.left-a.left}px;height:${t.height}px;width:${t.width}px;pointer-events:none;z-index:5;`;n.style.cssText=o,n.firstChild.style.cssText=`${o}left:0;top:0;background-color:${_};opacity:0.3;`,e===v-1&&u.parentNode!==n&&n.appendChild(u)}for(let e=b-1;e>=v;e--){const t=d[e];r.removeChild(t),d.pop()}}function ue(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,l=0,i=null,r=0;if(null!==n&&null!==o){const t=se(n,e),c=se(o,e);null!==t&&null!==c&&([s,l]=he(t.type,t.index),[i,r]=he(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:l,focusCollabNode:i,focusOffset:r}}function _e(e,t){const n=t.awareness.getLocalState();if(null===n)return;const{anchorCollabNode:l,anchorOffset:i,focusCollabNode:r,focusOffset:c}=ue(e,n);if(null!==l&&null!==r){const e=l.getKey(),t=r.getKey(),n=o();if(!s(n))return;de(n.anchor,e,i),de(n.focus,t,c)}}function de(t,o,s){if(t.key!==o||t.offset!==s){let i=e(o);if(null!==i&&!l(i)&&!n(i)){const e=i.getParentOrThrow();o=e.getKey(),s=i.getIndexWithinParent(),i=e}t.set(o,s,l(i)?"element":"text")}}function he(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof ee){const{node:e,offset:o}=q(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function pe(e,t){const n=Array.from(t.awareness.getStates()),o=e.clientID,s=e.cursors,l=e.editor._editorState._nodeMap,i=new Set;for(let t=0;t<n.length;t++){const r=n[t],[c,a]=r;if(c!==o){i.add(c);const{name:t,color:n,focusing:o}=a;let r=null,f=s.get(c);if(void 0===f&&(f=ie(t,n),s.set(c,f)),o){const{anchorCollabNode:t,anchorOffset:n,focusCollabNode:o,focusOffset:s}=ue(e,a);if(null!==t&&null!==o){const e=t.getKey(),l=o.getKey();if(r=f.selection,null===r)r=ae(f,e,n,l,s);else{const t=r.anchor,o=r.focus;t.key=e,t.offset=n,o.key=l,o.offset=s}}}fe(e,f,r,l)}}const r=Array.from(s.keys());for(let t=0;t<r.length;t++){const n=r[t];if(!i.has(n)){const t=s.get(n);void 0!==t&&(ce(e,t),s.delete(n))}}}function ge(e,t,n,o){const l=t.awareness,i=l.getLocalState();if(null===i)return;const{anchorPos:r,focusPos:c,name:a,color:f,focusing:u,awarenessData:_}=i;let d=null,h=null;(null!==o&&(null===r||o.is(n))||null!==n)&&(s(o)&&(d=oe(o.anchor,e),h=oe(o.focus,e)),(le(r,d)||le(c,h))&&l.setLocalState({...i,anchorPos:d,awarenessData:_,color:f,focusPos:h,focusing:u,name:a}))}function ye(e,t){if(t instanceof b&&function(e,t){const{target:n}=t;if(!n._item||"__state"!==n._item.parentSub||void 0!==D(n)||!(n.parent instanceof h||n.parent instanceof g||n.parent instanceof p))return!1;const o=I(e,n.parent).getNode();if(o){const e=c(o.getWritable());for(const o of t.keysChanged)e.updateFromUnknown(o,n.get(o))}return!0}(e,t))return;const{target:n}=t,o=I(e,n);if(o instanceof ee&&t instanceof v){const{keysChanged:n,childListChanged:s,delta:l}=t;n.size>0&&o.syncPropertiesFromYjs(e,n),s&&(o.applyChildrenYjsDelta(e,l),o.syncChildrenFromYjs(e))}else if(o instanceof E&&t instanceof b){const{keysChanged:n}=t;n.size>0&&o.syncPropertiesAndTextFromYjs(e,n)}else if(o instanceof X&&t instanceof C){const{attributesChanged:n}=t;n.size>0&&o.syncPropertiesFromYjs(e,n)}else T(82)}function xe(e,t,n,l,i=pe){const r=e.editor,c=r._editorState;n.forEach((e=>e.delta)),r.update((()=>{for(let t=0;t<n.length;t++){const o=n[t];ye(e,o)}const l=o();if(s(l))if(H(l)){const n=c._selection;if(s(n)&&(_e(e,t),H(l))){V(l.anchor.key,c)}ge(e,t,n,o())}else _e(e,t)}),{onUpdate:()=>{i(e,t),r.update((()=>{0===a().getChildrenSize()&&a().append(_())}))},skipTransforms:!0,tag:l?"historic":"collaboration"})}function me(t,s,l,i,r,c,f,u){!function(e,t){e.doc.transact(t,e)}(t,(()=>{i.read((()=>{if(u.has("collaboration")||u.has("historic"))return void(f.size>0&&function(t,o){const s=Array.from(o),l=t.collabNodeMap,i=[],r=[];for(let t=0;t<s.length;t++){const o=s[t],c=e(o),a=l.get(o);if(a instanceof E)if(n(c))i.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),r.push(a)}}for(let e=0;e<r.length;e++){const t=r[e],n=t.getKey();l.delete(n);const o=t._parent._children,s=o.indexOf(t);o.splice(s,1)}for(let e=0;e<i.length;e++){const[t,n]=i[e];t._text=n}}(t,f));if(r.has("root")){const e=l._nodeMap,n=a(),o=t.root;o.syncPropertiesFromLexical(t,n,e),o.syncChildrenFromLexical(t,n,e,r,c)}const i=o(),_=l._selection;ge(t,s,_,i)}))}))}const ke=d("CONNECTED_COMMAND"),be=d("TOGGLE_CONNECT_COMMAND");function ve(e,t){return new N(t,{trackedOrigins:new Set([e,null])})}function Ce(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})}function Ne(e,t,n,o,s){const{awareness:l}=e;let i=l.getLocalState();null===i&&(i={anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t}),i.focusing=o,l.setLocalState(i)}export{ke as CONNECTED_COMMAND,be as TOGGLE_CONNECT_COMMAND,ne as createBinding,ve as createUndoManager,ue as getAnchorAndFocusCollabNodesForUserState,Ce as initLocalState,Ne as setLocalStateFocus,pe as syncCursorPositions,me as syncLexicalUpdateToYjs,xe as syncYjsChangesToLexical};
|
package/package.json
CHANGED
|
@@ -11,13 +11,13 @@
|
|
|
11
11
|
"crdt"
|
|
12
12
|
],
|
|
13
13
|
"license": "MIT",
|
|
14
|
-
"version": "0.29.1-nightly.
|
|
14
|
+
"version": "0.29.1-nightly.20250328.0",
|
|
15
15
|
"main": "LexicalYjs.js",
|
|
16
16
|
"types": "index.d.ts",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@lexical/offset": "0.29.1-nightly.
|
|
19
|
-
"@lexical/selection": "0.29.1-nightly.
|
|
20
|
-
"lexical": "0.29.1-nightly.
|
|
18
|
+
"@lexical/offset": "0.29.1-nightly.20250328.0",
|
|
19
|
+
"@lexical/selection": "0.29.1-nightly.20250328.0",
|
|
20
|
+
"lexical": "0.29.1-nightly.20250328.0"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"yjs": ">=13.5.22"
|