@lexical/yjs 0.24.1-nightly.20250214.0 → 0.24.1-nightly.20250217.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.prod.js +1 -1
- package/LexicalYjs.prod.mjs +1 -1
- package/package.json +4 -4
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){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var l=s((function(e){const t=new URLSearchParams;t.append("code",e);for(let e=1;e<arguments.length;e++)t.append("v",arguments[e]);throw Error(`Minified Lexical error #${e}; visit https://lexical.dev/docs/error?${t} 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){e.collabNodeMap.delete(this._key)}}function r(e,t){const n=new i(e,t);return e._collabNode=n,n}class c{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),l=n.__text;if(N(t,this._map,s,n),null!==s){const t=s.__text;if(t!==l){!function(t,n,o,s){const l=e.$getSelection();let i=s.length;if(e.$isRangeSelection(l)&&l.isCollapsed()){const e=l.anchor;e.key===n&&(i=e.offset)}const r=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}}(o,s,i);t.spliceText(r.index,r.remove,r.insert)}(this,n.__key,t,l),this._text=l}}}syncPropertiesAndTextFromYjs(e,t){const n=this.getNode();null===n&&l(84),m(e,this._map,n,t);const o=this._text;if(n.__text!==o){n.getWritable().__text=o}}destroy(e){e.collabNodeMap.delete(this._key)}}function a(e,t,n,o){const s=new c(e,t,n,o);return e._collabNode=s,s}const f=new Set(["__key","__parent","__next","__prev"]),u=new Set(["__first","__last","__size"]),d=new Set(["__cachedText"]),_=new Set(["__text"]);function h(t,n,o){if(f.has(t))return!0;if(e.$isTextNode(n)){if(_.has(t))return!0}else if(e.$isElementNode(n)&&(u.has(t)||e.$isRootNode(n)&&d.has(t)))return!0;const s=n.constructor,l=o.excludedProperties.get(s);return null!=l&&l.has(t)}function p(t){const n=e.$getNodeByKey(t);return null===n&&l(85),n}function g(n,o,s){const i=o.__type;let c;if(e.$isElementNode(o)){c=w(new t.XmlText,s,i),c.syncPropertiesFromLexical(n,o,null),c.syncChildrenFromLexical(n,o,null,null,null)}else if(e.$isTextNode(o)){c=a(new t.Map,o.__text,s,i),c.syncPropertiesAndTextFromLexical(n,o,null)}else if(e.$isLineBreakNode(o)){const e=new t.Map;e.set("__type","linebreak"),c=r(e,s)}else if(e.$isDecoratorNode(o)){c=S(new t.XmlElement,s,i),c.syncPropertiesFromLexical(n,o,null)}else l(86);return c._key=o.__key,c}function y(e,n,o){const s=n._collabNode;if(void 0===s){const s=e.editor._nodes,i=function(e){const n=e instanceof t.Map?e.get("__type"):e.getAttribute("__type");return null==n&&l(87),n}(n);void 0===s.get(i)&&l(88,i);const c=n.parent,f=void 0===o&&null!==c?y(e,c):o||null;if(f instanceof P||l(89),n instanceof t.XmlText)return w(n,f,i);if(n instanceof t.Map)return"linebreak"===i?r(n,f):a(n,"",f,i);if(n instanceof t.XmlElement)return S(n,f,i)}return s}function x(e,t,n){const o=t.getType(),s=e.editor._nodes.get(o);void 0===s&&l(88,o);const i=new s.klass;if(i.__parent=n,t._key=i.__key,t instanceof P){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof c?t.syncPropertiesAndTextFromYjs(e,null):t instanceof v&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(i.__key,t),i}function m(n,o,s,l){const i=null===l?o instanceof t.Map?Array.from(o.keys()):Object.keys(o.getAttributes()):Array.from(l);let r;for(let l=0;l<i.length;l++){const c=i[l];if(h(c,s,n))continue;const a=s[c];let f=o instanceof t.Map?o.get(c):o.getAttribute(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(),l=f.guid;s._key=l,o.set(l,f),f=s}void 0===r&&(r=s.getWritable()),r[c]=f}}}function N(e,n,o,s){const l=s.__type,i=e.nodeProperties;let r=i.get(l);void 0===r&&(r=Object.keys(s).filter((t=>!h(t,s,e))),i.set(l,r));const c=e.editor.constructor;for(let l=0;l<r.length;l++){const i=r[l],a=null===o?void 0:o[i];let f=s[i];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 l=o||new t.Doc,i=l.guid;f._key=i,n.set(i,l),f=l,e.editor.update((()=>{s.markDirty()}))}n instanceof t.Map?n.set(i,f):n.setAttribute(i,f)}}}function b(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function k(e,t,n){let o=0,s=0;const 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 c){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 C(t){const n=t.anchor,o=t.focus;let s=!1;try{const t=n.getNode(),l=o.getNode();(!t.isAttached()||!l.isAttached()||e.$isTextNode(t)&&n.offset>t.getTextContentSize()||e.$isTextNode(l)&&o.offset>l.getTextContentSize())&&(s=!0)}catch(e){s=!0}return s}function T(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 $(t,n){const o=n._nodeMap.get(t);if(!o)return void e.$getRoot().selectStart();const s=o.__prev;let l=null;s&&(l=e.$getNodeByKey(s)),null===l&&null!==o.__parent&&(l=e.$getNodeByKey(o.__parent)),null!==l?null!==l&&l.isAttached()?l.selectEnd():$(l.__key,n):e.$getRoot().selectStart()}class v{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);N(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&l(83);m(e,this._xmlElem,n,t)}destroy(e){e.collabNodeMap.delete(this._key)}}function S(e,t,n){const o=new v(e,t,n);return e._collabNode=o,o}class P{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&&l(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&l(91),m(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 r=t[l],a=r.insert,f=r.delete;if(null!=r.retain)o+=r.retain;else if("number"==typeof f){let e=f;for(;e>0;){const{node:t,nodeIndex:s,offset:l,length:r}=k(this,o,!1);if(t instanceof P||t instanceof i||t instanceof v)n.splice(s,1),e-=1;else{if(!(t instanceof c))break;{const o=Math.min(e,r),i=0!==s?n[s-1]:null,a=t.getSize();if(0===l&&r===a){n.splice(s,1);const e=b(t._text,l,o-1,"");e.length>0&&(i instanceof c?i._text+=e:this._xmlText.delete(l,e.length))}else t._text=b(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}=k(this,o,!0);e instanceof c?e._text=b(e._text,t,0,a):this._xmlText.delete(t,a.length),o+=a.length}else{const t=a,{node:l,nodeIndex:i,length:r}=k(this,o,!1),f=y(e,t,this);if(l instanceof c&&r>0&&r<l._text.length){const e=l._text,t=e.length-r;l._text=b(e,t,r,""),n.splice(i+1,0,f),s=b(e,0,t,"")}else n.splice(i,0,f);null!==s&&f instanceof c&&(f._text=s+f._text,s=null),o+=1}}}}syncChildrenFromYjs(t){const o=this.getNode();null===o&&l(92);const s=o.__key,r=n.$createChildrenArray(o,null),a=r.length,f=this._children,u=f.length,d=t.collabNodeMap,_=new Set;let h,g,y=0,m=null;u!==a&&(g=o.getWritable());for(let n=0;n<u;n++){const a=r[y],N=f[n],b=N.getNode(),k=N._key;if(null!==b&&a===k){const n=e.$isTextNode(b);if(_.add(a),n)if(N._key=a,N instanceof P){const e=N._xmlText;N.syncPropertiesFromYjs(t,null),N.applyChildrenYjsDelta(t,e.toDelta()),N.syncChildrenFromYjs(t)}else N instanceof c?N.syncPropertiesAndTextFromYjs(t,null):N instanceof v?N.syncPropertiesFromYjs(t,null):N instanceof i||l(93);m=b,y++}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!==b&&void 0!==a&&!h.has(a)){T(p(a)),n--,y++;continue}g=o.getWritable();const e=x(t,N,s),l=e.__key;if(d.set(l,N),null===m){const t=g.getFirstChild();if(g.__first=l,null!==t){const n=t.getWritable();n.__prev=l,e.__next=n.__key}}else{const t=m.getWritable(),n=m.getNextSibling();if(t.__next=l,e.__prev=m.__key,null!==n){const t=n.getWritable();t.__prev=l,e.__next=t.__key}}n===u-1&&(g.__last=l),g.__size++,m=e}}for(let e=0;e<a;e++){const n=r[e];if(!_.has(n)){const e=p(n),o=t.collabNodeMap.get(n);void 0!==o&&o.destroy(t),T(e)}}}syncPropertiesFromLexical(e,t,n){N(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(t,n,o,s,l,i){const r=this._children[n],a=p(o);r instanceof P&&e.$isElementNode(a)?(r.syncPropertiesFromLexical(t,a,s),r.syncChildrenFromLexical(t,a,s,l,i)):r instanceof c&&e.$isTextNode(a)?r.syncPropertiesAndTextFromLexical(t,a,s):r instanceof v&&e.$isDecoratorNode(a)&&r.syncPropertiesFromLexical(t,a,s)}syncChildrenFromLexical(e,t,o,s,l){const i=this.getPrevNode(o),r=null===i?[]:n.$createChildrenArray(i,o),c=n.$createChildrenArray(t,null),a=r.length-1,f=c.length-1,u=e.collabNodeMap;let d,_,h=0,y=0;for(;h<=a&&y<=f;){const t=r[h],n=c[y];if(t===n)this._syncChildFromLexical(e,y,n,o,s,l),h++,y++;else{void 0===d&&(d=new Set(r)),void 0===_&&(_=new Set(c));const o=_.has(t),s=d.has(n);if(o){const t=g(e,p(n),this);u.set(n,t),s?(this.splice(e,y,1,t),h++,y++):(this.splice(e,y,0,t),y++)}else this.splice(e,y,1),h++}}const x=h>a,m=y>f;if(x&&!m)for(;y<=f;++y){const t=c[y],n=g(e,p(t),this);this.append(n),u.set(t,n)}else if(m&&!x)for(let t=this._children.length-1;t>=y;t--)this.splice(e,t,1)}append(e){const t=this._xmlText,n=this._children,o=n[n.length-1],s=void 0!==o?o.getOffset()+o.getSize():0;if(e instanceof P)t.insertEmbed(s,e._xmlText);else if(e instanceof c){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 v&&t.insertEmbed(s,e._xmlElem);this._children.push(e)}splice(e,t,n,o){const s=this._children,r=s[t];if(void 0===r)return void 0===o&&l(94),void this.append(o);const a=r.getOffset();-1===a&&l(95);const f=this._xmlText;if(0!==n&&f.delete(a,r.getSize()),o instanceof P)f.insertEmbed(a,o._xmlText);else if(o instanceof c){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 v&&f.insertEmbed(a,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.delete(this._key)}}function w(e,t,n){const o=new P(e,t,n);return e._collabNode=o,o}function E(n,o){const s=o.collabNodeMap.get(n.key);if(void 0===s)return null;let i=n.offset,r=s.getSharedType();if(s instanceof c){r=s._parent._xmlText;const e=s.getOffset();if(-1===e)return null;i=e+1+i}else if(s instanceof P&&"element"===n.type){const t=n.getNode();e.$isElementNode(t)||l(184);let o=0,s=0,r=t.getFirstChild();for(;null!==r&&s++<i;)e.$isTextNode(r)?o+=r.getTextContentSize()+1:o++,r=r.getNextSibling();i=o}return t.createRelativePositionFromTypeIndex(r,i)}function M(e,n){return t.createAbsolutePositionFromRelativePosition(e,n.doc)}function F(e,n){if(null==e){if(null!=n)return!0}else if(null==n||!t.compareRelativePositions(e,n))return!0;return!1}function O(e,t){return{color:t,name:e,selection:null}}function L(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 A(e,t){const n=t.selection;null!==n&&L(e,n)}function D(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 j(t,n,s,l){const i=t.editor,r=i.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 L(t,u));n.selection=s;const d=s.caret,_=s.color,h=s.selections,p=s.anchor,g=s.focus,y=p.key,x=g.key,m=l.get(y),N=l.get(x);if(null==m||null==N)return;let b;if(m===N&&e.$isLineBreakNode(m)){b=[i.getElementByKey(y).getBoundingClientRect()]}else{const e=o.createDOMRange(i,m,p.offset,N,g.offset);if(null===e)return;b=o.createRectsFromDOMRange(i,e)}const k=h.length,C=b.length;for(let e=0;e<C;e++){const t=b[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===C-1&&d.parentNode!==n&&n.appendChild(d)}for(let e=k-1;e>=C;e--){const t=h[e];c.removeChild(t),h.pop()}}function z(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,l=0,i=null,r=0;if(null!==n&&null!==o){const t=M(n,e),c=M(o,e);null!==t&&null!==c&&([s,l]=K(t.type,t.index),[i,r]=K(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:l,focusCollabNode:i,focusOffset:r}}function Y(t,n){const o=n.awareness.getLocalState();if(null===o)return;const{anchorCollabNode:s,anchorOffset:l,focusCollabNode:i,focusOffset:r}=z(t,o);if(null!==s&&null!==i){const t=s.getKey(),n=i.getKey(),o=e.$getSelection();if(!e.$isRangeSelection(o))return;R(o.anchor,t,l),R(o.focus,n,r)}}function R(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 K(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof P){const{node:e,offset:o}=k(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function B(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=O(t,n),s.set(c,f)),o){const{anchorCollabNode:t,anchorOffset:n,focusCollabNode:o,focusOffset:s}=z(e,a);if(null!==t&&null!==o){const e=t.getKey(),l=o.getKey();if(r=f.selection,null===r)r=D(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}}}j(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&&(A(e,t),s.delete(n))}}}function W(t,n,o,s){const l=n.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!==s&&(null===r||s.is(o))||null!==o)&&(e.$isRangeSelection(s)&&(_=E(s.anchor,t),h=E(s.focus,t)),(F(r,_)||F(c,h))&&l.setLocalState({...i,anchorPos:_,awarenessData:d,color:f,focusPos:h,focusing:u,name:a}))}function I(e,n){const{target:o}=n,s=y(e,o);if(s instanceof P&&n instanceof t.YTextEvent){const{keysChanged:t,childListChanged:o,delta:l}=n;t.size>0&&s.syncPropertiesFromYjs(e,t),o&&(s.applyChildrenYjsDelta(e,l),s.syncChildrenFromYjs(e))}else if(s instanceof c&&n instanceof t.YMapEvent){const{keysChanged:t}=n;t.size>0&&s.syncPropertiesAndTextFromYjs(e,t)}else if(s instanceof v&&n instanceof t.YXmlEvent){const{attributesChanged:t}=n;t.size>0&&s.syncPropertiesFromYjs(e,t)}else l(82)}const U=e.createCommand("CONNECTED_COMMAND"),X=e.createCommand("TOGGLE_CONNECT_COMMAND");exports.CONNECTED_COMMAND=U,exports.TOGGLE_CONNECT_COMMAND=X,exports.createBinding=function(e,n,o,s,i,r){null==s&&l(81);const c=w(s.get("root",t.XmlText),null,"root");return c._key="root",{clientID:s.clientID,collabNodeMap:new Map,cursors:new Map,cursorsContainer:null,doc:s,docMap:i,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=z,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: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)},exports.syncCursorPositions=B,exports.syncLexicalUpdateToYjs=function(t,n,o,s,l,i,r,a){!function(e,t){e.doc.transact(t,e)}(t,(()=>{s.read((()=>{if(a.has("collaboration")||a.has("historic"))return void(r.size>0&&function(t,n){const o=Array.from(n),s=t.collabNodeMap,l=[];for(let t=0;t<o.length;t++){const n=o[t],i=e.$getNodeByKey(n),r=s.get(n);if(r instanceof c)if(e.$isTextNode(i))l.push([r,i.__text]);else{const e=r.getOffset();if(-1===e)continue;const t=r._parent;r._normalized=!0,t._xmlText.delete(e,1),s.delete(n);const o=t._children,l=o.indexOf(r);o.splice(l,1)}}for(let e=0;e<l.length;e++){const[t,n]=l[e];t instanceof c&&"string"==typeof n&&(t._text=n)}}(t,r));if(l.has("root")){const n=o._nodeMap,s=e.$getRoot(),r=t.root;r.syncPropertiesFromLexical(t,s,n),r.syncChildrenFromLexical(t,s,n,l,i)}const s=e.$getSelection(),f=o._selection;W(t,n,f,s)}))}))},exports.syncYjsChangesToLexical=function(t,n,o,s,l=B){const i=t.editor,r=i._editorState;o.forEach((e=>e.delta)),i.update((()=>{for(let e=0;e<o.length;e++){const n=o[e];I(t,n)}const s=e.$getSelection();if(e.$isRangeSelection(s))if(C(s)){const o=r._selection;if(e.$isRangeSelection(o)&&(Y(t,n),C(s))){$(s.anchor.key,r)}W(t,n,o,e.$getSelection())}else Y(t,n)}),{onUpdate:()=>{l(t,n),i.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){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var l=s((function(e){const t=new URL("https://lexical.dev/docs/error"),n=new URLSearchParams;n.append("code",e);for(let e=1;e<arguments.length;e++)n.append("v",arguments[e]);throw t.search=n.toString(),Error(`Minified Lexical error #${e}; visit ${t.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){e.collabNodeMap.delete(this._key)}}function r(e,t){const n=new i(e,t);return e._collabNode=n,n}class c{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),l=n.__text;if(N(t,this._map,s,n),null!==s){const t=s.__text;if(t!==l){!function(t,n,o,s){const l=e.$getSelection();let i=s.length;if(e.$isRangeSelection(l)&&l.isCollapsed()){const e=l.anchor;e.key===n&&(i=e.offset)}const r=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}}(o,s,i);t.spliceText(r.index,r.remove,r.insert)}(this,n.__key,t,l),this._text=l}}}syncPropertiesAndTextFromYjs(e,t){const n=this.getNode();null===n&&l(84),m(e,this._map,n,t);const o=this._text;if(n.__text!==o){n.getWritable().__text=o}}destroy(e){e.collabNodeMap.delete(this._key)}}function a(e,t,n,o){const s=new c(e,t,n,o);return e._collabNode=s,s}const f=new Set(["__key","__parent","__next","__prev"]),u=new Set(["__first","__last","__size"]),d=new Set(["__cachedText"]),_=new Set(["__text"]);function h(t,n,o){if(f.has(t))return!0;if(e.$isTextNode(n)){if(_.has(t))return!0}else if(e.$isElementNode(n)&&(u.has(t)||e.$isRootNode(n)&&d.has(t)))return!0;const s=n.constructor,l=o.excludedProperties.get(s);return null!=l&&l.has(t)}function g(t){const n=e.$getNodeByKey(t);return null===n&&l(85),n}function p(n,o,s){const i=o.__type;let c;if(e.$isElementNode(o)){c=w(new t.XmlText,s,i),c.syncPropertiesFromLexical(n,o,null),c.syncChildrenFromLexical(n,o,null,null,null)}else if(e.$isTextNode(o)){c=a(new t.Map,o.__text,s,i),c.syncPropertiesAndTextFromLexical(n,o,null)}else if(e.$isLineBreakNode(o)){const e=new t.Map;e.set("__type","linebreak"),c=r(e,s)}else if(e.$isDecoratorNode(o)){c=S(new t.XmlElement,s,i),c.syncPropertiesFromLexical(n,o,null)}else l(86);return c._key=o.__key,c}function y(e,n,o){const s=n._collabNode;if(void 0===s){const s=e.editor._nodes,i=function(e){const n=e instanceof t.Map?e.get("__type"):e.getAttribute("__type");return null==n&&l(87),n}(n);void 0===s.get(i)&&l(88,i);const c=n.parent,f=void 0===o&&null!==c?y(e,c):o||null;if(f instanceof P||l(89),n instanceof t.XmlText)return w(n,f,i);if(n instanceof t.Map)return"linebreak"===i?r(n,f):a(n,"",f,i);if(n instanceof t.XmlElement)return S(n,f,i)}return s}function x(e,t,n){const o=t.getType(),s=e.editor._nodes.get(o);void 0===s&&l(88,o);const i=new s.klass;if(i.__parent=n,t._key=i.__key,t instanceof P){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof c?t.syncPropertiesAndTextFromYjs(e,null):t instanceof v&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(i.__key,t),i}function m(n,o,s,l){const i=null===l?o instanceof t.Map?Array.from(o.keys()):Object.keys(o.getAttributes()):Array.from(l);let r;for(let l=0;l<i.length;l++){const c=i[l];if(h(c,s,n))continue;const a=s[c];let f=o instanceof t.Map?o.get(c):o.getAttribute(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(),l=f.guid;s._key=l,o.set(l,f),f=s}void 0===r&&(r=s.getWritable()),r[c]=f}}}function N(e,n,o,s){const l=s.__type,i=e.nodeProperties;let r=i.get(l);void 0===r&&(r=Object.keys(s).filter((t=>!h(t,s,e))),i.set(l,r));const c=e.editor.constructor;for(let l=0;l<r.length;l++){const i=r[l],a=null===o?void 0:o[i];let f=s[i];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 l=o||new t.Doc,i=l.guid;f._key=i,n.set(i,l),f=l,e.editor.update((()=>{s.markDirty()}))}n instanceof t.Map?n.set(i,f):n.setAttribute(i,f)}}}function b(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function k(e,t,n){let o=0,s=0;const 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 c){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 C(t){const n=t.anchor,o=t.focus;let s=!1;try{const t=n.getNode(),l=o.getNode();(!t.isAttached()||!l.isAttached()||e.$isTextNode(t)&&n.offset>t.getTextContentSize()||e.$isTextNode(l)&&o.offset>l.getTextContentSize())&&(s=!0)}catch(e){s=!0}return s}function T(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 $(t,n){const o=n._nodeMap.get(t);if(!o)return void e.$getRoot().selectStart();const s=o.__prev;let l=null;s&&(l=e.$getNodeByKey(s)),null===l&&null!==o.__parent&&(l=e.$getNodeByKey(o.__parent)),null!==l?null!==l&&l.isAttached()?l.selectEnd():$(l.__key,n):e.$getRoot().selectStart()}class v{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);N(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&l(83);m(e,this._xmlElem,n,t)}destroy(e){e.collabNodeMap.delete(this._key)}}function S(e,t,n){const o=new v(e,t,n);return e._collabNode=o,o}class P{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&&l(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&l(91),m(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 r=t[l],a=r.insert,f=r.delete;if(null!=r.retain)o+=r.retain;else if("number"==typeof f){let e=f;for(;e>0;){const{node:t,nodeIndex:s,offset:l,length:r}=k(this,o,!1);if(t instanceof P||t instanceof i||t instanceof v)n.splice(s,1),e-=1;else{if(!(t instanceof c))break;{const o=Math.min(e,r),i=0!==s?n[s-1]:null,a=t.getSize();if(0===l&&r===a){n.splice(s,1);const e=b(t._text,l,o-1,"");e.length>0&&(i instanceof c?i._text+=e:this._xmlText.delete(l,e.length))}else t._text=b(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}=k(this,o,!0);e instanceof c?e._text=b(e._text,t,0,a):this._xmlText.delete(t,a.length),o+=a.length}else{const t=a,{node:l,nodeIndex:i,length:r}=k(this,o,!1),f=y(e,t,this);if(l instanceof c&&r>0&&r<l._text.length){const e=l._text,t=e.length-r;l._text=b(e,t,r,""),n.splice(i+1,0,f),s=b(e,0,t,"")}else n.splice(i,0,f);null!==s&&f instanceof c&&(f._text=s+f._text,s=null),o+=1}}}}syncChildrenFromYjs(t){const o=this.getNode();null===o&&l(92);const s=o.__key,r=n.$createChildrenArray(o,null),a=r.length,f=this._children,u=f.length,d=t.collabNodeMap,_=new Set;let h,p,y=0,m=null;u!==a&&(p=o.getWritable());for(let n=0;n<u;n++){const a=r[y],N=f[n],b=N.getNode(),k=N._key;if(null!==b&&a===k){const n=e.$isTextNode(b);if(_.add(a),n)if(N._key=a,N instanceof P){const e=N._xmlText;N.syncPropertiesFromYjs(t,null),N.applyChildrenYjsDelta(t,e.toDelta()),N.syncChildrenFromYjs(t)}else N instanceof c?N.syncPropertiesAndTextFromYjs(t,null):N instanceof v?N.syncPropertiesFromYjs(t,null):N instanceof i||l(93);m=b,y++}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!==b&&void 0!==a&&!h.has(a)){T(g(a)),n--,y++;continue}p=o.getWritable();const e=x(t,N,s),l=e.__key;if(d.set(l,N),null===m){const t=p.getFirstChild();if(p.__first=l,null!==t){const n=t.getWritable();n.__prev=l,e.__next=n.__key}}else{const t=m.getWritable(),n=m.getNextSibling();if(t.__next=l,e.__prev=m.__key,null!==n){const t=n.getWritable();t.__prev=l,e.__next=t.__key}}n===u-1&&(p.__last=l),p.__size++,m=e}}for(let e=0;e<a;e++){const n=r[e];if(!_.has(n)){const e=g(n),o=t.collabNodeMap.get(n);void 0!==o&&o.destroy(t),T(e)}}}syncPropertiesFromLexical(e,t,n){N(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(t,n,o,s,l,i){const r=this._children[n],a=g(o);r instanceof P&&e.$isElementNode(a)?(r.syncPropertiesFromLexical(t,a,s),r.syncChildrenFromLexical(t,a,s,l,i)):r instanceof c&&e.$isTextNode(a)?r.syncPropertiesAndTextFromLexical(t,a,s):r instanceof v&&e.$isDecoratorNode(a)&&r.syncPropertiesFromLexical(t,a,s)}syncChildrenFromLexical(e,t,o,s,l){const i=this.getPrevNode(o),r=null===i?[]:n.$createChildrenArray(i,o),c=n.$createChildrenArray(t,null),a=r.length-1,f=c.length-1,u=e.collabNodeMap;let d,_,h=0,y=0;for(;h<=a&&y<=f;){const t=r[h],n=c[y];if(t===n)this._syncChildFromLexical(e,y,n,o,s,l),h++,y++;else{void 0===d&&(d=new Set(r)),void 0===_&&(_=new Set(c));const o=_.has(t),s=d.has(n);if(o){const t=p(e,g(n),this);u.set(n,t),s?(this.splice(e,y,1,t),h++,y++):(this.splice(e,y,0,t),y++)}else this.splice(e,y,1),h++}}const x=h>a,m=y>f;if(x&&!m)for(;y<=f;++y){const t=c[y],n=p(e,g(t),this);this.append(n),u.set(t,n)}else if(m&&!x)for(let t=this._children.length-1;t>=y;t--)this.splice(e,t,1)}append(e){const t=this._xmlText,n=this._children,o=n[n.length-1],s=void 0!==o?o.getOffset()+o.getSize():0;if(e instanceof P)t.insertEmbed(s,e._xmlText);else if(e instanceof c){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 v&&t.insertEmbed(s,e._xmlElem);this._children.push(e)}splice(e,t,n,o){const s=this._children,r=s[t];if(void 0===r)return void 0===o&&l(94),void this.append(o);const a=r.getOffset();-1===a&&l(95);const f=this._xmlText;if(0!==n&&f.delete(a,r.getSize()),o instanceof P)f.insertEmbed(a,o._xmlText);else if(o instanceof c){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 v&&f.insertEmbed(a,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.delete(this._key)}}function w(e,t,n){const o=new P(e,t,n);return e._collabNode=o,o}function E(n,o){const s=o.collabNodeMap.get(n.key);if(void 0===s)return null;let i=n.offset,r=s.getSharedType();if(s instanceof c){r=s._parent._xmlText;const e=s.getOffset();if(-1===e)return null;i=e+1+i}else if(s instanceof P&&"element"===n.type){const t=n.getNode();e.$isElementNode(t)||l(184);let o=0,s=0,r=t.getFirstChild();for(;null!==r&&s++<i;)e.$isTextNode(r)?o+=r.getTextContentSize()+1:o++,r=r.getNextSibling();i=o}return t.createRelativePositionFromTypeIndex(r,i)}function M(e,n){return t.createAbsolutePositionFromRelativePosition(e,n.doc)}function F(e,n){if(null==e){if(null!=n)return!0}else if(null==n||!t.compareRelativePositions(e,n))return!0;return!1}function O(e,t){return{color:t,name:e,selection:null}}function L(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 A(e,t){const n=t.selection;null!==n&&L(e,n)}function D(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 j(t,n,s,l){const i=t.editor,r=i.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 L(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=l.get(y),N=l.get(x);if(null==m||null==N)return;let b;if(m===N&&e.$isLineBreakNode(m)){b=[i.getElementByKey(y).getBoundingClientRect()]}else{const e=o.createDOMRange(i,m,g.offset,N,p.offset);if(null===e)return;b=o.createRectsFromDOMRange(i,e)}const k=h.length,C=b.length;for(let e=0;e<C;e++){const t=b[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===C-1&&d.parentNode!==n&&n.appendChild(d)}for(let e=k-1;e>=C;e--){const t=h[e];c.removeChild(t),h.pop()}}function z(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,l=0,i=null,r=0;if(null!==n&&null!==o){const t=M(n,e),c=M(o,e);null!==t&&null!==c&&([s,l]=K(t.type,t.index),[i,r]=K(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:l,focusCollabNode:i,focusOffset:r}}function Y(t,n){const o=n.awareness.getLocalState();if(null===o)return;const{anchorCollabNode:s,anchorOffset:l,focusCollabNode:i,focusOffset:r}=z(t,o);if(null!==s&&null!==i){const t=s.getKey(),n=i.getKey(),o=e.$getSelection();if(!e.$isRangeSelection(o))return;R(o.anchor,t,l),R(o.focus,n,r)}}function R(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 K(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof P){const{node:e,offset:o}=k(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function B(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=O(t,n),s.set(c,f)),o){const{anchorCollabNode:t,anchorOffset:n,focusCollabNode:o,focusOffset:s}=z(e,a);if(null!==t&&null!==o){const e=t.getKey(),l=o.getKey();if(r=f.selection,null===r)r=D(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}}}j(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&&(A(e,t),s.delete(n))}}}function W(t,n,o,s){const l=n.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!==s&&(null===r||s.is(o))||null!==o)&&(e.$isRangeSelection(s)&&(_=E(s.anchor,t),h=E(s.focus,t)),(F(r,_)||F(c,h))&&l.setLocalState({...i,anchorPos:_,awarenessData:d,color:f,focusPos:h,focusing:u,name:a}))}function I(e,n){const{target:o}=n,s=y(e,o);if(s instanceof P&&n instanceof t.YTextEvent){const{keysChanged:t,childListChanged:o,delta:l}=n;t.size>0&&s.syncPropertiesFromYjs(e,t),o&&(s.applyChildrenYjsDelta(e,l),s.syncChildrenFromYjs(e))}else if(s instanceof c&&n instanceof t.YMapEvent){const{keysChanged:t}=n;t.size>0&&s.syncPropertiesAndTextFromYjs(e,t)}else if(s instanceof v&&n instanceof t.YXmlEvent){const{attributesChanged:t}=n;t.size>0&&s.syncPropertiesFromYjs(e,t)}else l(82)}const U=e.createCommand("CONNECTED_COMMAND"),X=e.createCommand("TOGGLE_CONNECT_COMMAND");exports.CONNECTED_COMMAND=U,exports.TOGGLE_CONNECT_COMMAND=X,exports.createBinding=function(e,n,o,s,i,r){null==s&&l(81);const c=w(s.get("root",t.XmlText),null,"root");return c._key="root",{clientID:s.clientID,collabNodeMap:new Map,cursors:new Map,cursorsContainer:null,doc:s,docMap:i,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=z,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: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)},exports.syncCursorPositions=B,exports.syncLexicalUpdateToYjs=function(t,n,o,s,l,i,r,a){!function(e,t){e.doc.transact(t,e)}(t,(()=>{s.read((()=>{if(a.has("collaboration")||a.has("historic"))return void(r.size>0&&function(t,n){const o=Array.from(n),s=t.collabNodeMap,l=[];for(let t=0;t<o.length;t++){const n=o[t],i=e.$getNodeByKey(n),r=s.get(n);if(r instanceof c)if(e.$isTextNode(i))l.push([r,i.__text]);else{const e=r.getOffset();if(-1===e)continue;const t=r._parent;r._normalized=!0,t._xmlText.delete(e,1),s.delete(n);const o=t._children,l=o.indexOf(r);o.splice(l,1)}}for(let e=0;e<l.length;e++){const[t,n]=l[e];t instanceof c&&"string"==typeof n&&(t._text=n)}}(t,r));if(l.has("root")){const n=o._nodeMap,s=e.$getRoot(),r=t.root;r.syncPropertiesFromLexical(t,s,n),r.syncChildrenFromLexical(t,s,n,l,i)}const s=e.$getSelection(),f=o._selection;W(t,n,f,s)}))}))},exports.syncYjsChangesToLexical=function(t,n,o,s,l=B){const i=t.editor,r=i._editorState;o.forEach((e=>e.delta)),i.update((()=>{for(let e=0;e<o.length;e++){const n=o[e];I(t,n)}const s=e.$getSelection();if(e.$isRangeSelection(s))if(C(s)){const o=r._selection;if(e.$isRangeSelection(o)&&(Y(t,n),C(s))){$(s.anchor.key,r)}W(t,n,o,e.$getSelection())}else Y(t,n)}),{onUpdate:()=>{l(t,n),i.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,$getRoot as c,$isRootNode as f,$createParagraphNode as a,createCommand as u}from"lexical";import{XmlText as d,Map as _,XmlElement as h,Doc as p,createAbsolutePositionFromRelativePosition as g,createRelativePositionFromTypeIndex as y,compareRelativePositions as x,YTextEvent as m,YMapEvent as b,YXmlEvent as k,UndoManager as v}from"yjs";import{$createChildrenArray as C}from"@lexical/offset";import{createDOMRange as N,createRectsFromDOMRange as P}from"@lexical/selection";function w(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var T=w((function(e){const t=new URLSearchParams;t.append("code",e);for(let e=1;e<arguments.length;e++)t.append("v",arguments[e]);throw Error(`Minified Lexical error #${e}; visit https://lexical.dev/docs/error?${t} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}));class S{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){e.collabNodeMap.delete(this._key)}}function F(e,t){const n=new S(e,t);return e._collabNode=n,n}class O{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($(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),K(e,this._map,n,t);const o=this._text;if(n.__text!==o){n.getWritable().__text=o}}destroy(e){e.collabNodeMap.delete(this._key)}}function E(e,t,n,o){const s=new O(e,t,n,o);return e._collabNode=s,s}const M=new Set(["__key","__parent","__next","__prev"]),z=new Set(["__first","__last","__size"]),L=new Set(["__cachedText"]),j=new Set(["__text"]);function A(e,t,o){if(M.has(e))return!0;if(n(t)){if(j.has(e))return!0}else if(l(t)&&(z.has(e)||f(t)&&L.has(e)))return!0;const s=t.constructor,i=o.excludedProperties.get(s);return null!=i&&i.has(e)}function Y(t){const n=e(t);return null===n&&T(85),n}function D(e,o,s){const r=o.__type;let c;if(l(o)){c=V(new d,s,r),c.syncPropertiesFromLexical(e,o,null),c.syncChildrenFromLexical(e,o,null,null,null)}else if(n(o)){c=E(new _,o.__text,s,r),c.syncPropertiesAndTextFromLexical(e,o,null)}else if(t(o)){const e=new _;e.set("__type","linebreak"),c=F(e,s)}else if(i(o)){c=J(new h,s,r),c.syncPropertiesFromLexical(e,o,null)}else T(86);return c._key=o.__key,c}function W(e,t,n){const o=t._collabNode;if(void 0===o){const o=e.editor._nodes,s=function(e){const t=e instanceof _?e.get("__type"):e.getAttribute("__type");return null==t&&T(87),t}(t);void 0===o.get(s)&&T(88,s);const l=t.parent,i=void 0===n&&null!==l?W(e,l):n||null;if(i instanceof Q||T(89),t instanceof d)return V(t,i,s);if(t instanceof _)return"linebreak"===s?F(t,i):E(t,"",i,s);if(t instanceof h)return J(t,i,s)}return o}function I(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 Q){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof O?t.syncPropertiesAndTextFromYjs(e,null):t instanceof H&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(l.__key,t),l}function K(e,t,n,o){const s=null===o?t instanceof _?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(A(i,n,e))continue;const c=n[i];let f=t instanceof _?t.get(i):t.getAttribute(i);if(c!==f){if(f instanceof p){const t=e.docMap;c instanceof p&&t.delete(c.guid);const n=r(),o=f.guid;n._key=o,t.set(o,f),f=n}void 0===l&&(l=n.getWritable()),l[i]=f}}}function $(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=>!A(t,o,e))),l.set(s,i));const r=e.editor.constructor;for(let s=0;s<i.length;s++){const l=i[s],c=null===n?void 0:n[l];let f=o[l];if(c!==f){if(f 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 p,l=s.guid;f._key=l,t.set(l,s),f=s,e.editor.update((()=>{o.markDirty()}))}t instanceof _?t.set(l,f):t.setAttribute(l,f)}}}function R(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function B(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 O){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 U(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 G(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 q(t,n){const o=n._nodeMap.get(t);if(!o)return void c().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():q(l.__key,n):c().selectStart()}class H{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);$(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&T(83);K(e,this._xmlElem,n,t)}destroy(e){e.collabNodeMap.delete(this._key)}}function J(e,t,n){const o=new H(e,t,n);return e._collabNode=o,o}class Q{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),K(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}=B(this,o,!1);if(t instanceof Q||t instanceof S||t instanceof H)n.splice(s,1),e-=1;else{if(!(t instanceof O))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=R(t._text,l,o-1,"");e.length>0&&(r instanceof O?r._text+=e:this._xmlText.delete(l,e.length))}else t._text=R(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}=B(this,o,!0);e instanceof O?e._text=R(e._text,t,0,r):this._xmlText.delete(t,r.length),o+=r.length}else{const t=r,{node:l,nodeIndex:i,length:c}=B(this,o,!1),f=W(e,t,this);if(l instanceof O&&c>0&&c<l._text.length){const e=l._text,t=e.length-c;l._text=R(e,t,c,""),n.splice(i+1,0,f),s=R(e,0,t,"")}else n.splice(i,0,f);null!==s&&f instanceof O&&(f._text=s+f._text,s=null),o+=1}}}}syncChildrenFromYjs(e){const t=this.getNode();null===t&&T(92);const o=t.__key,s=C(t,null),l=s.length,i=this._children,r=i.length,c=e.collabNodeMap,f=new Set;let a,u,d=0,_=null;r!==l&&(u=t.getWritable());for(let l=0;l<r;l++){const h=s[d],p=i[l],g=p.getNode(),y=p._key;if(null!==g&&h===y){const t=n(g);if(f.add(h),t)if(p._key=h,p instanceof Q){const t=p._xmlText;p.syncPropertiesFromYjs(e,null),p.applyChildrenYjsDelta(e,t.toDelta()),p.syncChildrenFromYjs(e)}else p instanceof O?p.syncPropertiesAndTextFromYjs(e,null):p instanceof H?p.syncPropertiesFromYjs(e,null):p instanceof S||T(93);_=g,d++}else{if(void 0===a){a=new Set;for(let e=0;e<r;e++){const t=i[e]._key;""!==t&&a.add(t)}}if(null!==g&&void 0!==h&&!a.has(h)){G(Y(h)),l--,d++;continue}u=t.getWritable();const n=I(e,p,o),s=n.__key;if(c.set(s,p),null===_){const e=u.getFirstChild();if(u.__first=s,null!==e){const t=e.getWritable();t.__prev=s,n.__next=t.__key}}else{const e=_.getWritable(),t=_.getNextSibling();if(e.__next=s,n.__prev=_.__key,null!==t){const e=t.getWritable();e.__prev=s,n.__next=e.__key}}l===r-1&&(u.__last=s),u.__size++,_=n}}for(let t=0;t<l;t++){const n=s[t];if(!f.has(n)){const t=Y(n),o=e.collabNodeMap.get(n);void 0!==o&&o.destroy(e),G(t)}}}syncPropertiesFromLexical(e,t,n){$(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(e,t,o,s,r,c){const f=this._children[t],a=Y(o);f instanceof Q&&l(a)?(f.syncPropertiesFromLexical(e,a,s),f.syncChildrenFromLexical(e,a,s,r,c)):f instanceof O&&n(a)?f.syncPropertiesAndTextFromLexical(e,a,s):f instanceof H&&i(a)&&f.syncPropertiesFromLexical(e,a,s)}syncChildrenFromLexical(e,t,n,o,s){const l=this.getPrevNode(n),i=null===l?[]:C(l,n),r=C(t,null),c=i.length-1,f=r.length-1,a=e.collabNodeMap;let u,d,_=0,h=0;for(;_<=c&&h<=f;){const t=i[_],l=r[h];if(t===l)this._syncChildFromLexical(e,h,l,n,o,s),_++,h++;else{void 0===u&&(u=new Set(i)),void 0===d&&(d=new Set(r));const n=d.has(t),o=u.has(l);if(n){const t=D(e,Y(l),this);a.set(l,t),o?(this.splice(e,h,1,t),_++,h++):(this.splice(e,h,0,t),h++)}else this.splice(e,h,1),_++}}const p=_>c,g=h>f;if(p&&!g)for(;h<=f;++h){const t=r[h],n=D(e,Y(t),this);this.append(n),a.set(t,n)}else if(g&&!p)for(let t=this._children.length-1;t>=h;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 Q)t.insertEmbed(s,e._xmlText);else if(e instanceof O){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof S?t.insertEmbed(s,e._map):e instanceof H&&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 Q)r.insertEmbed(i,o._xmlText);else if(o instanceof O){const e=o._map;null===e.parent&&r.insertEmbed(i,e),r.insert(i+1,o._text)}else o instanceof S?r.insertEmbed(i,o._map):o instanceof H&&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.delete(this._key)}}function V(e,t,n){const o=new Q(e,t,n);return e._collabNode=o,o}function X(e,t,n,o,s,l){null==o&&T(81);const i=V(o.get("root",d),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 Z(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 O){i=o._parent._xmlText;const e=o.getOffset();if(-1===e)return null;s=e+1+s}else if(o instanceof Q&&"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 y(i,s)}function ee(e,t){return g(e,t.doc)}function te(e,t){if(null==e){if(null!=t)return!0}else if(null==t||!x(e,t))return!0;return!1}function ne(e,t){return{color:t,name:e,selection:null}}function oe(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 se(e,t){const n=t.selection;null!==n&&oe(e,n)}function le(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 ie(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 f=c.getBoundingClientRect(),a=n.selection;if(null===o)return null===a?void 0:(n.selection=null,void oe(e,a));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 b;if(x===m&&t(x)){b=[l.getElementByKey(g).getBoundingClientRect()]}else{const e=N(l,x,h.offset,m,p.offset);if(null===e)return;b=P(l,e)}const k=_.length,v=b.length;for(let e=0;e<v;e++){const t=b[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-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:${d};opacity:0.3;`,e===v-1&&u.parentNode!==n&&n.appendChild(u)}for(let e=k-1;e>=v;e--){const t=_[e];r.removeChild(t),_.pop()}}function re(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,l=0,i=null,r=0;if(null!==n&&null!==o){const t=ee(n,e),c=ee(o,e);null!==t&&null!==c&&([s,l]=ae(t.type,t.index),[i,r]=ae(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:l,focusCollabNode:i,focusOffset:r}}function ce(e,t){const n=t.awareness.getLocalState();if(null===n)return;const{anchorCollabNode:l,anchorOffset:i,focusCollabNode:r,focusOffset:c}=re(e,n);if(null!==l&&null!==r){const e=l.getKey(),t=r.getKey(),n=o();if(!s(n))return;fe(n.anchor,e,i),fe(n.focus,t,c)}}function fe(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 ae(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof Q){const{node:e,offset:o}=B(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function ue(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,f]=r;if(c!==o){i.add(c);const{name:t,color:n,focusing:o}=f;let r=null,a=s.get(c);if(void 0===a&&(a=ne(t,n),s.set(c,a)),o){const{anchorCollabNode:t,anchorOffset:n,focusCollabNode:o,focusOffset:s}=re(e,f);if(null!==t&&null!==o){const e=t.getKey(),l=o.getKey();if(r=a.selection,null===r)r=le(a,e,n,l,s);else{const t=r.anchor,o=r.focus;t.key=e,t.offset=n,o.key=l,o.offset=s}}}ie(e,a,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&&(se(e,t),s.delete(n))}}}function de(e,t,n,o){const l=t.awareness,i=l.getLocalState();if(null===i)return;const{anchorPos:r,focusPos:c,name:f,color:a,focusing:u,awarenessData:d}=i;let _=null,h=null;(null!==o&&(null===r||o.is(n))||null!==n)&&(s(o)&&(_=Z(o.anchor,e),h=Z(o.focus,e)),(te(r,_)||te(c,h))&&l.setLocalState({...i,anchorPos:_,awarenessData:d,color:a,focusPos:h,focusing:u,name:f}))}function _e(e,t){const{target:n}=t,o=W(e,n);if(o instanceof Q&&t instanceof m){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 O&&t instanceof b){const{keysChanged:n}=t;n.size>0&&o.syncPropertiesAndTextFromYjs(e,n)}else if(o instanceof H&&t instanceof k){const{attributesChanged:n}=t;n.size>0&&o.syncPropertiesFromYjs(e,n)}else T(82)}function he(e,t,n,l,i=ue){const r=e.editor,f=r._editorState;n.forEach((e=>e.delta)),r.update((()=>{for(let t=0;t<n.length;t++){const o=n[t];_e(e,o)}const l=o();if(s(l))if(U(l)){const n=f._selection;if(s(n)&&(ce(e,t),U(l))){q(l.anchor.key,f)}de(e,t,n,o())}else ce(e,t)}),{onUpdate:()=>{i(e,t),r.update((()=>{0===c().getChildrenSize()&&c().append(a())}))},skipTransforms:!0,tag:l?"historic":"collaboration"})}function pe(t,s,l,i,r,f,a,u){!function(e,t){e.doc.transact(t,e)}(t,(()=>{i.read((()=>{if(u.has("collaboration")||u.has("historic"))return void(a.size>0&&function(t,o){const s=Array.from(o),l=t.collabNodeMap,i=[];for(let t=0;t<s.length;t++){const o=s[t],r=e(o),c=l.get(o);if(c instanceof O)if(n(r))i.push([c,r.__text]);else{const e=c.getOffset();if(-1===e)continue;const t=c._parent;c._normalized=!0,t._xmlText.delete(e,1),l.delete(o);const n=t._children,s=n.indexOf(c);n.splice(s,1)}}for(let e=0;e<i.length;e++){const[t,n]=i[e];t instanceof O&&"string"==typeof n&&(t._text=n)}}(t,a));if(r.has("root")){const e=l._nodeMap,n=c(),o=t.root;o.syncPropertiesFromLexical(t,n,e),o.syncChildrenFromLexical(t,n,e,r,f)}const i=o(),d=l._selection;de(t,s,d,i)}))}))}const ge=u("CONNECTED_COMMAND"),ye=u("TOGGLE_CONNECT_COMMAND");function xe(e,t){return new v(t,{trackedOrigins:new Set([e,null])})}function me(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})}function be(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{ge as CONNECTED_COMMAND,ye as TOGGLE_CONNECT_COMMAND,X as createBinding,xe as createUndoManager,re as getAnchorAndFocusCollabNodesForUserState,me as initLocalState,be as setLocalStateFocus,ue as syncCursorPositions,pe as syncLexicalUpdateToYjs,he 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,$getRoot as c,$isRootNode as a,$createParagraphNode as f,createCommand as u}from"lexical";import{XmlText as d,Map as _,XmlElement as h,Doc as p,createAbsolutePositionFromRelativePosition as g,createRelativePositionFromTypeIndex as y,compareRelativePositions as x,YTextEvent as m,YMapEvent as b,YXmlEvent as k,UndoManager as v}from"yjs";import{$createChildrenArray as C}from"@lexical/offset";import{createDOMRange as N,createRectsFromDOMRange as P}from"@lexical/selection";function w(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var T=w((function(e){const t=new URL("https://lexical.dev/docs/error"),n=new URLSearchParams;n.append("code",e);for(let e=1;e<arguments.length;e++)n.append("v",arguments[e]);throw t.search=n.toString(),Error(`Minified Lexical error #${e}; visit ${t.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}));class S{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){e.collabNodeMap.delete(this._key)}}function F(e,t){const n=new S(e,t);return e._collabNode=n,n}class O{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($(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),K(e,this._map,n,t);const o=this._text;if(n.__text!==o){n.getWritable().__text=o}}destroy(e){e.collabNodeMap.delete(this._key)}}function E(e,t,n,o){const s=new O(e,t,n,o);return e._collabNode=s,s}const M=new Set(["__key","__parent","__next","__prev"]),L=new Set(["__first","__last","__size"]),z=new Set(["__cachedText"]),j=new Set(["__text"]);function A(e,t,o){if(M.has(e))return!0;if(n(t)){if(j.has(e))return!0}else if(l(t)&&(L.has(e)||a(t)&&z.has(e)))return!0;const s=t.constructor,i=o.excludedProperties.get(s);return null!=i&&i.has(e)}function Y(t){const n=e(t);return null===n&&T(85),n}function D(e,o,s){const r=o.__type;let c;if(l(o)){c=V(new d,s,r),c.syncPropertiesFromLexical(e,o,null),c.syncChildrenFromLexical(e,o,null,null,null)}else if(n(o)){c=E(new _,o.__text,s,r),c.syncPropertiesAndTextFromLexical(e,o,null)}else if(t(o)){const e=new _;e.set("__type","linebreak"),c=F(e,s)}else if(i(o)){c=J(new h,s,r),c.syncPropertiesFromLexical(e,o,null)}else T(86);return c._key=o.__key,c}function W(e,t,n){const o=t._collabNode;if(void 0===o){const o=e.editor._nodes,s=function(e){const t=e instanceof _?e.get("__type"):e.getAttribute("__type");return null==t&&T(87),t}(t);void 0===o.get(s)&&T(88,s);const l=t.parent,i=void 0===n&&null!==l?W(e,l):n||null;if(i instanceof Q||T(89),t instanceof d)return V(t,i,s);if(t instanceof _)return"linebreak"===s?F(t,i):E(t,"",i,s);if(t instanceof h)return J(t,i,s)}return o}function I(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 Q){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof O?t.syncPropertiesAndTextFromYjs(e,null):t instanceof H&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(l.__key,t),l}function K(e,t,n,o){const s=null===o?t instanceof _?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(A(i,n,e))continue;const c=n[i];let a=t instanceof _?t.get(i):t.getAttribute(i);if(c!==a){if(a instanceof p){const t=e.docMap;c instanceof p&&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 $(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=>!A(t,o,e))),l.set(s,i));const r=e.editor.constructor;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 p,l=s.guid;a._key=l,t.set(l,s),a=s,e.editor.update((()=>{o.markDirty()}))}t instanceof _?t.set(l,a):t.setAttribute(l,a)}}}function R(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function U(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 O){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 B(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 G(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 q(t,n){const o=n._nodeMap.get(t);if(!o)return void c().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():q(l.__key,n):c().selectStart()}class H{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);$(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&T(83);K(e,this._xmlElem,n,t)}destroy(e){e.collabNodeMap.delete(this._key)}}function J(e,t,n){const o=new H(e,t,n);return e._collabNode=o,o}class Q{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),K(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}=U(this,o,!1);if(t instanceof Q||t instanceof S||t instanceof H)n.splice(s,1),e-=1;else{if(!(t instanceof O))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=R(t._text,l,o-1,"");e.length>0&&(r instanceof O?r._text+=e:this._xmlText.delete(l,e.length))}else t._text=R(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}=U(this,o,!0);e instanceof O?e._text=R(e._text,t,0,r):this._xmlText.delete(t,r.length),o+=r.length}else{const t=r,{node:l,nodeIndex:i,length:c}=U(this,o,!1),a=W(e,t,this);if(l instanceof O&&c>0&&c<l._text.length){const e=l._text,t=e.length-c;l._text=R(e,t,c,""),n.splice(i+1,0,a),s=R(e,0,t,"")}else n.splice(i,0,a);null!==s&&a instanceof O&&(a._text=s+a._text,s=null),o+=1}}}}syncChildrenFromYjs(e){const t=this.getNode();null===t&&T(92);const o=t.__key,s=C(t,null),l=s.length,i=this._children,r=i.length,c=e.collabNodeMap,a=new Set;let f,u,d=0,_=null;r!==l&&(u=t.getWritable());for(let l=0;l<r;l++){const h=s[d],p=i[l],g=p.getNode(),y=p._key;if(null!==g&&h===y){const t=n(g);if(a.add(h),t)if(p._key=h,p instanceof Q){const t=p._xmlText;p.syncPropertiesFromYjs(e,null),p.applyChildrenYjsDelta(e,t.toDelta()),p.syncChildrenFromYjs(e)}else p instanceof O?p.syncPropertiesAndTextFromYjs(e,null):p instanceof H?p.syncPropertiesFromYjs(e,null):p instanceof S||T(93);_=g,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!==g&&void 0!==h&&!f.has(h)){G(Y(h)),l--,d++;continue}u=t.getWritable();const n=I(e,p,o),s=n.__key;if(c.set(s,p),null===_){const e=u.getFirstChild();if(u.__first=s,null!==e){const t=e.getWritable();t.__prev=s,n.__next=t.__key}}else{const e=_.getWritable(),t=_.getNextSibling();if(e.__next=s,n.__prev=_.__key,null!==t){const e=t.getWritable();e.__prev=s,n.__next=e.__key}}l===r-1&&(u.__last=s),u.__size++,_=n}}for(let t=0;t<l;t++){const n=s[t];if(!a.has(n)){const t=Y(n),o=e.collabNodeMap.get(n);void 0!==o&&o.destroy(e),G(t)}}}syncPropertiesFromLexical(e,t,n){$(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(e,t,o,s,r,c){const a=this._children[t],f=Y(o);a instanceof Q&&l(f)?(a.syncPropertiesFromLexical(e,f,s),a.syncChildrenFromLexical(e,f,s,r,c)):a instanceof O&&n(f)?a.syncPropertiesAndTextFromLexical(e,f,s):a instanceof H&&i(f)&&a.syncPropertiesFromLexical(e,f,s)}syncChildrenFromLexical(e,t,n,o,s){const l=this.getPrevNode(n),i=null===l?[]:C(l,n),r=C(t,null),c=i.length-1,a=r.length-1,f=e.collabNodeMap;let u,d,_=0,h=0;for(;_<=c&&h<=a;){const t=i[_],l=r[h];if(t===l)this._syncChildFromLexical(e,h,l,n,o,s),_++,h++;else{void 0===u&&(u=new Set(i)),void 0===d&&(d=new Set(r));const n=d.has(t),o=u.has(l);if(n){const t=D(e,Y(l),this);f.set(l,t),o?(this.splice(e,h,1,t),_++,h++):(this.splice(e,h,0,t),h++)}else this.splice(e,h,1),_++}}const p=_>c,g=h>a;if(p&&!g)for(;h<=a;++h){const t=r[h],n=D(e,Y(t),this);this.append(n),f.set(t,n)}else if(g&&!p)for(let t=this._children.length-1;t>=h;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 Q)t.insertEmbed(s,e._xmlText);else if(e instanceof O){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof S?t.insertEmbed(s,e._map):e instanceof H&&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 Q)r.insertEmbed(i,o._xmlText);else if(o instanceof O){const e=o._map;null===e.parent&&r.insertEmbed(i,e),r.insert(i+1,o._text)}else o instanceof S?r.insertEmbed(i,o._map):o instanceof H&&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.delete(this._key)}}function V(e,t,n){const o=new Q(e,t,n);return e._collabNode=o,o}function X(e,t,n,o,s,l){null==o&&T(81);const i=V(o.get("root",d),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 Z(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 O){i=o._parent._xmlText;const e=o.getOffset();if(-1===e)return null;s=e+1+s}else if(o instanceof Q&&"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 y(i,s)}function ee(e,t){return g(e,t.doc)}function te(e,t){if(null==e){if(null!=t)return!0}else if(null==t||!x(e,t))return!0;return!1}function ne(e,t){return{color:t,name:e,selection:null}}function oe(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 se(e,t){const n=t.selection;null!==n&&oe(e,n)}function le(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 ie(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 oe(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 b;if(x===m&&t(x)){b=[l.getElementByKey(g).getBoundingClientRect()]}else{const e=N(l,x,h.offset,m,p.offset);if(null===e)return;b=P(l,e)}const k=_.length,v=b.length;for(let e=0;e<v;e++){const t=b[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=k-1;e>=v;e--){const t=_[e];r.removeChild(t),_.pop()}}function re(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,l=0,i=null,r=0;if(null!==n&&null!==o){const t=ee(n,e),c=ee(o,e);null!==t&&null!==c&&([s,l]=fe(t.type,t.index),[i,r]=fe(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:l,focusCollabNode:i,focusOffset:r}}function ce(e,t){const n=t.awareness.getLocalState();if(null===n)return;const{anchorCollabNode:l,anchorOffset:i,focusCollabNode:r,focusOffset:c}=re(e,n);if(null!==l&&null!==r){const e=l.getKey(),t=r.getKey(),n=o();if(!s(n))return;ae(n.anchor,e,i),ae(n.focus,t,c)}}function ae(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 fe(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof Q){const{node:e,offset:o}=U(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function ue(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=ne(t,n),s.set(c,f)),o){const{anchorCollabNode:t,anchorOffset:n,focusCollabNode:o,focusOffset:s}=re(e,a);if(null!==t&&null!==o){const e=t.getKey(),l=o.getKey();if(r=f.selection,null===r)r=le(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}}}ie(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&&(se(e,t),s.delete(n))}}}function de(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)&&(_=Z(o.anchor,e),h=Z(o.focus,e)),(te(r,_)||te(c,h))&&l.setLocalState({...i,anchorPos:_,awarenessData:d,color:f,focusPos:h,focusing:u,name:a}))}function _e(e,t){const{target:n}=t,o=W(e,n);if(o instanceof Q&&t instanceof m){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 O&&t instanceof b){const{keysChanged:n}=t;n.size>0&&o.syncPropertiesAndTextFromYjs(e,n)}else if(o instanceof H&&t instanceof k){const{attributesChanged:n}=t;n.size>0&&o.syncPropertiesFromYjs(e,n)}else T(82)}function he(e,t,n,l,i=ue){const r=e.editor,a=r._editorState;n.forEach((e=>e.delta)),r.update((()=>{for(let t=0;t<n.length;t++){const o=n[t];_e(e,o)}const l=o();if(s(l))if(B(l)){const n=a._selection;if(s(n)&&(ce(e,t),B(l))){q(l.anchor.key,a)}de(e,t,n,o())}else ce(e,t)}),{onUpdate:()=>{i(e,t),r.update((()=>{0===c().getChildrenSize()&&c().append(f())}))},skipTransforms:!0,tag:l?"historic":"collaboration"})}function pe(t,s,l,i,r,a,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=[];for(let t=0;t<s.length;t++){const o=s[t],r=e(o),c=l.get(o);if(c instanceof O)if(n(r))i.push([c,r.__text]);else{const e=c.getOffset();if(-1===e)continue;const t=c._parent;c._normalized=!0,t._xmlText.delete(e,1),l.delete(o);const n=t._children,s=n.indexOf(c);n.splice(s,1)}}for(let e=0;e<i.length;e++){const[t,n]=i[e];t instanceof O&&"string"==typeof n&&(t._text=n)}}(t,f));if(r.has("root")){const e=l._nodeMap,n=c(),o=t.root;o.syncPropertiesFromLexical(t,n,e),o.syncChildrenFromLexical(t,n,e,r,a)}const i=o(),d=l._selection;de(t,s,d,i)}))}))}const ge=u("CONNECTED_COMMAND"),ye=u("TOGGLE_CONNECT_COMMAND");function xe(e,t){return new v(t,{trackedOrigins:new Set([e,null])})}function me(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})}function be(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{ge as CONNECTED_COMMAND,ye as TOGGLE_CONNECT_COMMAND,X as createBinding,xe as createUndoManager,re as getAnchorAndFocusCollabNodesForUserState,me as initLocalState,be as setLocalStateFocus,ue as syncCursorPositions,pe as syncLexicalUpdateToYjs,he as syncYjsChangesToLexical};
|
package/package.json
CHANGED
|
@@ -11,13 +11,13 @@
|
|
|
11
11
|
"crdt"
|
|
12
12
|
],
|
|
13
13
|
"license": "MIT",
|
|
14
|
-
"version": "0.24.1-nightly.
|
|
14
|
+
"version": "0.24.1-nightly.20250217.0",
|
|
15
15
|
"main": "LexicalYjs.js",
|
|
16
16
|
"types": "index.d.ts",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@lexical/offset": "0.24.1-nightly.
|
|
19
|
-
"@lexical/selection": "0.24.1-nightly.
|
|
20
|
-
"lexical": "0.24.1-nightly.
|
|
18
|
+
"@lexical/offset": "0.24.1-nightly.20250217.0",
|
|
19
|
+
"@lexical/selection": "0.24.1-nightly.20250217.0",
|
|
20
|
+
"lexical": "0.24.1-nightly.20250217.0"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"yjs": ">=13.5.22"
|