@lexical/yjs 0.35.1-nightly.20250924.0 → 0.35.1-nightly.20250925.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 +19 -0
- package/LexicalYjs.dev.mjs +20 -1
- package/LexicalYjs.prod.js +1 -1
- package/LexicalYjs.prod.mjs +1 -1
- package/index.d.ts +1 -0
- package/package.json +4 -4
- package/types.d.ts +22 -0
package/LexicalYjs.dev.js
CHANGED
|
@@ -36,6 +36,10 @@ function formatDevErrorMessage(message) {
|
|
|
36
36
|
*/
|
|
37
37
|
|
|
38
38
|
class CollabLineBreakNode {
|
|
39
|
+
_map;
|
|
40
|
+
_key;
|
|
41
|
+
_parent;
|
|
42
|
+
_type;
|
|
39
43
|
constructor(map, parent) {
|
|
40
44
|
this._key = '';
|
|
41
45
|
this._map = map;
|
|
@@ -121,6 +125,12 @@ function $diffTextContentAndApplyDelta(collabNode, key, prevText, nextText) {
|
|
|
121
125
|
collabNode.spliceText(diff.index, diff.remove, diff.insert);
|
|
122
126
|
}
|
|
123
127
|
class CollabTextNode {
|
|
128
|
+
_map;
|
|
129
|
+
_key;
|
|
130
|
+
_parent;
|
|
131
|
+
_text;
|
|
132
|
+
_type;
|
|
133
|
+
_normalized;
|
|
124
134
|
constructor(map, text, parent, type) {
|
|
125
135
|
this._key = '';
|
|
126
136
|
this._map = map;
|
|
@@ -554,6 +564,10 @@ function $moveSelectionToPreviousNode(anchorNodeKey, currentEditorState) {
|
|
|
554
564
|
}
|
|
555
565
|
|
|
556
566
|
class CollabDecoratorNode {
|
|
567
|
+
_xmlElem;
|
|
568
|
+
_key;
|
|
569
|
+
_parent;
|
|
570
|
+
_type;
|
|
557
571
|
constructor(xmlElem, parent, type) {
|
|
558
572
|
this._key = '';
|
|
559
573
|
this._xmlElem = xmlElem;
|
|
@@ -614,6 +628,11 @@ function $createCollabDecoratorNode(xmlElem, parent, type) {
|
|
|
614
628
|
}
|
|
615
629
|
|
|
616
630
|
class CollabElementNode {
|
|
631
|
+
_key;
|
|
632
|
+
_children;
|
|
633
|
+
_xmlText;
|
|
634
|
+
_type;
|
|
635
|
+
_parent;
|
|
617
636
|
constructor(xmlText, parent, type) {
|
|
618
637
|
this._key = '';
|
|
619
638
|
this._children = [];
|
package/LexicalYjs.dev.mjs
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import { $getNodeByKey, $isLineBreakNode, $isTextNode, $getSelection, $isRangeSelection, $isElementNode, $isDecoratorNode,
|
|
9
|
+
import { $getNodeByKey, $isLineBreakNode, $isTextNode, $getSelection, $isRangeSelection, createEditor, $isElementNode, $isDecoratorNode, $isRootNode, $getWritableNodeState, $getRoot, $getNodeByKeyOrThrow, removeFromParent, $addUpdateTag, SKIP_SCROLL_INTO_VIEW_TAG, HISTORIC_TAG, COLLABORATION_TAG, $createParagraphNode, createCommand } from 'lexical';
|
|
10
10
|
import { XmlText, Map as Map$1, XmlElement, Doc, createAbsolutePositionFromRelativePosition, createRelativePositionFromTypeIndex, compareRelativePositions, YMapEvent, YTextEvent, YXmlEvent, UndoManager } from 'yjs';
|
|
11
11
|
import { $createChildrenArray } from '@lexical/offset';
|
|
12
12
|
import { createDOMRange, createRectsFromDOMRange } from '@lexical/selection';
|
|
@@ -34,6 +34,10 @@ function formatDevErrorMessage(message) {
|
|
|
34
34
|
*/
|
|
35
35
|
|
|
36
36
|
class CollabLineBreakNode {
|
|
37
|
+
_map;
|
|
38
|
+
_key;
|
|
39
|
+
_parent;
|
|
40
|
+
_type;
|
|
37
41
|
constructor(map, parent) {
|
|
38
42
|
this._key = '';
|
|
39
43
|
this._map = map;
|
|
@@ -119,6 +123,12 @@ function $diffTextContentAndApplyDelta(collabNode, key, prevText, nextText) {
|
|
|
119
123
|
collabNode.spliceText(diff.index, diff.remove, diff.insert);
|
|
120
124
|
}
|
|
121
125
|
class CollabTextNode {
|
|
126
|
+
_map;
|
|
127
|
+
_key;
|
|
128
|
+
_parent;
|
|
129
|
+
_text;
|
|
130
|
+
_type;
|
|
131
|
+
_normalized;
|
|
122
132
|
constructor(map, text, parent, type) {
|
|
123
133
|
this._key = '';
|
|
124
134
|
this._map = map;
|
|
@@ -552,6 +562,10 @@ function $moveSelectionToPreviousNode(anchorNodeKey, currentEditorState) {
|
|
|
552
562
|
}
|
|
553
563
|
|
|
554
564
|
class CollabDecoratorNode {
|
|
565
|
+
_xmlElem;
|
|
566
|
+
_key;
|
|
567
|
+
_parent;
|
|
568
|
+
_type;
|
|
555
569
|
constructor(xmlElem, parent, type) {
|
|
556
570
|
this._key = '';
|
|
557
571
|
this._xmlElem = xmlElem;
|
|
@@ -612,6 +626,11 @@ function $createCollabDecoratorNode(xmlElem, parent, type) {
|
|
|
612
626
|
}
|
|
613
627
|
|
|
614
628
|
class CollabElementNode {
|
|
629
|
+
_key;
|
|
630
|
+
_children;
|
|
631
|
+
_xmlText;
|
|
632
|
+
_type;
|
|
633
|
+
_parent;
|
|
615
634
|
constructor(xmlText, parent, type) {
|
|
616
635
|
this._key = '';
|
|
617
636
|
this._children = [];
|
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(k(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;n.__text!==o&&n.setTextContent(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"]),d=new Set(["__cachedText"]),u=new Set(["__text"]);function h(t,n,o){if(a.has(t)||"function"==typeof n[t])return!0;if(e.$isTextNode(n)){if(u.has(t))return!0}else if(e.$isElementNode(n)&&(f.has(t)||e.$isRootNode(n)&&d.has(t)))return!0;const s=n.constructor,i=o.excludedProperties.get(s);return null!=i&&i.has(t)}function _(n,o,i){const r=o.__type;let a;if(e.$isElementNode(o)){a=O(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=w(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 P||s(89),n instanceof t.XmlText)return O(n,f,r);if(n instanceof t.Map)return"linebreak"===r?l(n,f):c(n,"",f,r);if(n instanceof t.XmlElement)return w(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 P){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 v&&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(h(c,s,n)){"__state"===c&&(r||(r=s.getWritable()),T(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 T(n,o,s){const i=m(o,"__state");i instanceof t.Map&&e.$getWritableNodeState(s).updateFromJSON(i.toJSON())}function k(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=>!h(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[d,u]=a&&f.doc?a.getInternalState():[void 0,new Map];if(r)for(const[e,t]of Object.entries(r))d&&t!==d[e]&&f.set(e,t);for(const[e,t]of c)u.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 C(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 S(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 b(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():b(i.__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);k(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 w(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&&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 P||t instanceof i||t instanceof v)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=C(t._text,l,o-1,"");e.length>0&&(i instanceof r?i._text+=e:this._xmlText.delete(l,e.length))}else t._text=C(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=C(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=C(e,t,c,""),n.splice(l+1,0,f),s=C(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,d=f.length,u=t.collabNodeMap,h=new Set;let _,g,p=0,x=null;d!==a&&(g=o.getWritable());for(let n=0;n<d;n++){const a=c[p],m=f[n],N=m.getNode(),T=m._key;if(null!==N&&a===T){const n=e.$isTextNode(N);if(h.add(a),n)if(m._key=a,m instanceof P){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 v?m.syncPropertiesFromYjs(t,null):m instanceof i||s(93);x=N,p++}else{if(void 0===_){_=new Set;for(let e=0;e<d;e++){const t=f[e]._key;""!==t&&_.add(t)}}if(null!==N&&void 0!==a&&!_.has(a)){const t=e.$getNodeByKeyOrThrow(a);e.removeFromParent(t),n--,p++;continue}g=o.getWritable();const s=y(t,m,l),i=s.__key;if(u.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===d-1&&(g.__last=i),g.__size++,x=s}}for(let n=0;n<a;n++){const o=c[n];if(!h.has(o)){const n=e.$getNodeByKeyOrThrow(o),s=t.collabNodeMap.get(o);void 0!==s&&s.destroy(t),e.removeFromParent(n)}}}syncPropertiesFromLexical(e,t,n){k(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 P&&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 v&&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,d=a.length-1,u=t.collabNodeMap;let h,g,p=0,y=0;for(;p<=f&&y<=d;){const n=c[p],o=a[y];if(n===o)this._syncChildFromLexical(t,y,o,s,i,l),p++,y++;else{void 0===h&&(h=new Set(c)),void 0===g&&(g=new Set(a));const s=g.has(n),i=h.has(o);if(s){const n=_(t,e.$getNodeByKeyOrThrow(o),this);u.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>d;if(x&&!m)for(;y<=d;++y){const n=a[y],o=_(t,e.$getNodeByKeyOrThrow(n),this);this.append(o),u.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 P)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 v&&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 P)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 v&&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 O(e,t,n){const o=new P(e,t,n);return e._collabNode=o,o}function E(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 P&&"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 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 L(e,t){return{color:t,name:e,selection:null}}function A(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 D(e,t){const n=t.selection;null!==n&&A(e,n)}function R(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 j(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(),d=n.selection;if(null===s)return null===d?void 0:(n.selection=null,void A(t,d));n.selection=s;const u=s.caret,h=s.color,_=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 T;if(m===N&&e.$isLineBreakNode(m)){T=[l.getElementByKey(y).getBoundingClientRect()]}else{const e=o.createDOMRange(l,m,g.offset,N,p.offset);if(null===e)return;T=o.createRectsFromDOMRange(l,e)}const k=_.length,C=T.length;for(let e=0;e<C;e++){const t=T[e];let n=_[e];if(void 0===n){n=document.createElement("span"),_[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:${h};opacity:0.3;`,e===C-1&&u.parentNode!==n&&n.appendChild(u)}for(let e=k-1;e>=C;e--){const t=_[e];c.removeChild(t),_.pop()}}function Y(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,i=0,l=null,r=0;if(null!==n&&null!==o){const t=M(n,e),c=M(o,e);null!==t&&null!==c&&([s,i]=B(t.type,t.index),[l,r]=B(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:i,focusCollabNode:l,focusOffset:r}}function z(t,n){const o=n.awareness.getLocalState();if(null===o)return;const{anchorCollabNode:s,anchorOffset:i,focusCollabNode:l,focusOffset:r}=Y(t,o);if(null!==s&&null!==l){const t=s.getKey(),n=l.getKey(),o=e.$getSelection();if(!e.$isRangeSelection(o))return;K(o.anchor,t,i),K(o.focus,n,r)}}function K(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 B(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof P){const{node:e,offset:o}=$(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function I(e,t){return t.awareness.getStates()}function W(e,t,n){const{getAwarenessStates:o=I}=n??{},s=Array.from(o(e,t)),i=e.clientID,l=e.cursors,r=e.editor._editorState._nodeMap,c=new Set;for(let t=0;t<s.length;t++){const n=s[t],[o,a]=n;if(o!==i){c.add(o);const{name:t,color:n,focusing:s}=a;let i=null,f=l.get(o);if(void 0===f&&(f=L(t,n),l.set(o,f)),s){const{anchorCollabNode:t,anchorOffset:n,focusCollabNode:o,focusOffset:s}=Y(e,a);if(null!==t&&null!==o){const e=t.getKey(),l=o.getKey();if(i=f.selection,null===i)i=R(f,e,n,l,s);else{const t=i.anchor,o=i.focus;t.key=e,t.offset=n,o.key=l,o.offset=s}}}j(e,f,i,r)}}const a=Array.from(l.keys());for(let t=0;t<a.length;t++){const n=a[t];if(!c.has(n)){const t=l.get(n);void 0!==t&&(D(e,t),l.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:d,awarenessData:u}=l;let h=null,_=null;(null!==s&&(null===r||s.is(o))||null!==o)&&(e.$isRangeSelection(s)&&(h=E(s.anchor,t),_=E(s.focus,t)),(F(r,h)||F(c,_))&&i.setLocalState({...l,anchorPos:h,awarenessData:u,color:f,focusPos:_,focusing:d,name:a}))}function G(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 P&&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 v&&o instanceof t.YXmlEvent){const{attributesChanged:e}=o;e.size>0&&l.syncPropertiesFromYjs(n,e)}else s(82)}const X=e.createCommand("CONNECTED_COMMAND"),q=e.createCommand("TOGGLE_CONNECT_COMMAND");exports.CONNECTED_COMMAND=X,exports.TOGGLE_CONNECT_COMMAND=q,exports.createBinding=function(e,n,o,i,l,r){null==i&&s(81);const c=O(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=Y,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=W,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(e.COLLABORATION_TAG)||a.has(e.HISTORIC_TAG))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=W){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];G(t,n)}const i=e.$getSelection();if(e.$isRangeSelection(i))if(S(i)){const o=r._selection;if(e.$isRangeSelection(o)&&(z(t,n),S(i))){b(i.anchor.key,r)}U(t,n,o,e.$getSelection())}else z(t,n);s||e.$addUpdateTag(e.SKIP_SCROLL_INTO_VIEW_TAG)}),{onUpdate:()=>{i(t,n),l.update((()=>{0===e.$getRoot().getChildrenSize()&&e.$getRoot().append(e.$createParagraphNode())}))},skipTransforms:!0,tag:s?e.HISTORIC_TAG:e.COLLABORATION_TAG})};
|
|
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{_map;_key;_parent;_type;constructor(e,t){this._key="",this._map=e,this._parent=t,this._type="linebreak"}getNode(){const t=e.$getNodeByKey(this._key);return e.$isLineBreakNode(t)?t:null}getKey(){return this._key}getSharedType(){return this._map}getType(){return this._type}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function l(e,t){const n=new i(e,t);return e._collabNode=n,n}class r{_map;_key;_parent;_text;_type;_normalized;constructor(e,t,n,o){this._key="",this._map=e,this._parent=n,this._text=t,this._type=o,this._normalized=!1}getPrevNode(t){if(null===t)return null;const n=t.get(this._key);return e.$isTextNode(n)?n:null}getNode(){const t=e.$getNodeByKey(this._key);return e.$isTextNode(t)?t:null}getSharedType(){return this._map}getType(){return this._type}getKey(){return this._key}getSize(){return this._text.length+(this._normalized?0:1)}getOffset(){return this._parent.getChildOffset(this)}spliceText(e,t,n){const o=this._parent._xmlText,s=this.getOffset()+1+e;0!==t&&o.delete(s,t),""!==n&&o.insert(s,n)}syncPropertiesAndTextFromLexical(t,n,o){const s=this.getPrevNode(o),i=n.__text;if(k(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;n.__text!==o&&n.setTextContent(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"]),d=new Set(["__cachedText"]),u=new Set(["__text"]);function h(t,n,o){if(a.has(t)||"function"==typeof n[t])return!0;if(e.$isTextNode(n)){if(u.has(t))return!0}else if(e.$isElementNode(n)&&(f.has(t)||e.$isRootNode(n)&&d.has(t)))return!0;const s=n.constructor,i=o.excludedProperties.get(s);return null!=i&&i.has(t)}function _(n,o,i){const r=o.__type;let a;if(e.$isElementNode(o)){a=O(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=w(new t.XmlElement,i,r),a.syncPropertiesFromLexical(n,o,null)}else s(86);return a._key=o.__key,a}function p(e){const t=m(e,"__type");return"string"!=typeof t&&void 0!==t&&s(87),t}function g(e,n,o){const i=n._collabNode;if(void 0===i){const i=e.editor._nodes,r=p(n);"string"!=typeof r&&s(87);void 0===i.get(r)&&s(88,r);const a=n.parent,f=void 0===o&&null!==a?g(e,a):o||null;if(f instanceof P||s(89),n instanceof t.XmlText)return O(n,f,r);if(n instanceof t.Map)return"linebreak"===r?l(n,f):c(n,"",f,r);if(n instanceof t.XmlElement)return w(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 P){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 v&&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(h(c,s,n)){"__state"===c&&(r||(r=s.getWritable()),T(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 T(n,o,s){const i=m(o,"__state");i instanceof t.Map&&e.$getWritableNodeState(s).updateFromJSON(i.toJSON())}function k(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=>!h(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[d,u]=a&&f.doc?a.getInternalState():[void 0,new Map];if(r)for(const[e,t]of Object.entries(r))d&&t!==d[e]&&f.set(e,t);for(const[e,t]of c)u.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 C(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 S(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 b(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():b(i.__key,n):e.$getRoot().selectStart()}class v{_xmlElem;_key;_parent;_type;constructor(e,t,n){this._key="",this._xmlElem=e,this._parent=t,this._type=n}getPrevNode(t){if(null===t)return null;const n=t.get(this._key);return e.$isDecoratorNode(n)?n:null}getNode(){const t=e.$getNodeByKey(this._key);return e.$isDecoratorNode(t)?t:null}getSharedType(){return this._xmlElem}getType(){return this._type}getKey(){return this._key}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}syncPropertiesFromLexical(e,t,n){const o=this.getPrevNode(n);k(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 w(e,t,n){const o=new v(e,t,n);return e._collabNode=o,o}class P{_key;_children;_xmlText;_type;_parent;constructor(e,t,n){this._key="",this._children=[],this._xmlText=e,this._type=n,this._parent=t}getPrevNode(t){if(null===t)return null;const n=t.get(this._key);return e.$isElementNode(n)?n:null}getNode(){const t=e.$getNodeByKey(this._key);return e.$isElementNode(t)?t:null}getSharedType(){return this._xmlText}getType(){return this._type}getKey(){return this._key}isEmpty(){return 0===this._children.length}getSize(){return 1}getOffset(){const e=this._parent;return null===e&&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 P||t instanceof i||t instanceof v)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=C(t._text,l,o-1,"");e.length>0&&(i instanceof r?i._text+=e:this._xmlText.delete(l,e.length))}else t._text=C(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=C(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=g(e,t,this);if(i instanceof r&&c>0&&c<i._text.length){const e=i._text,t=e.length-c;i._text=C(e,t,c,""),n.splice(l+1,0,f),s=C(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,d=f.length,u=t.collabNodeMap,h=new Set;let _,p,g=0,x=null;d!==a&&(p=o.getWritable());for(let n=0;n<d;n++){const a=c[g],m=f[n],N=m.getNode(),T=m._key;if(null!==N&&a===T){const n=e.$isTextNode(N);if(h.add(a),n)if(m._key=a,m instanceof P){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 v?m.syncPropertiesFromYjs(t,null):m instanceof i||s(93);x=N,g++}else{if(void 0===_){_=new Set;for(let e=0;e<d;e++){const t=f[e]._key;""!==t&&_.add(t)}}if(null!==N&&void 0!==a&&!_.has(a)){const t=e.$getNodeByKeyOrThrow(a);e.removeFromParent(t),n--,g++;continue}p=o.getWritable();const s=y(t,m,l),i=s.__key;if(u.set(i,m),null===x){const e=p.getFirstChild();if(p.__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===d-1&&(p.__last=i),p.__size++,x=s}}for(let n=0;n<a;n++){const o=c[n];if(!h.has(o)){const n=e.$getNodeByKeyOrThrow(o),s=t.collabNodeMap.get(o);void 0!==s&&s.destroy(t),e.removeFromParent(n)}}}syncPropertiesFromLexical(e,t,n){k(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 P&&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 v&&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,d=a.length-1,u=t.collabNodeMap;let h,p,g=0,y=0;for(;g<=f&&y<=d;){const n=c[g],o=a[y];if(n===o)this._syncChildFromLexical(t,y,o,s,i,l),g++,y++;else{void 0===h&&(h=new Set(c)),void 0===p&&(p=new Set(a));const s=p.has(n),i=h.has(o);if(s){const n=_(t,e.$getNodeByKeyOrThrow(o),this);u.set(o,n),i?(this.splice(t,y,1,n),g++,y++):(this.splice(t,y,0,n),y++)}else this.splice(t,y,1),g++}}const x=g>f,m=y>d;if(x&&!m)for(;y<=d;++y){const n=a[y],o=_(t,e.$getNodeByKeyOrThrow(n),this);this.append(o),u.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 P)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 v&&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 P)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 v&&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 O(e,t,n){const o=new P(e,t,n);return e._collabNode=o,o}function E(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 P&&"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 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 L(e,t){return{color:t,name:e,selection:null}}function A(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 D(e,t){const n=t.selection;null!==n&&A(e,n)}function R(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 j(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(),d=n.selection;if(null===s)return null===d?void 0:(n.selection=null,void A(t,d));n.selection=s;const u=s.caret,h=s.color,_=s.selections,p=s.anchor,g=s.focus,y=p.key,x=g.key,m=i.get(y),N=i.get(x);if(null==m||null==N)return;let T;if(m===N&&e.$isLineBreakNode(m)){T=[l.getElementByKey(y).getBoundingClientRect()]}else{const e=o.createDOMRange(l,m,p.offset,N,g.offset);if(null===e)return;T=o.createRectsFromDOMRange(l,e)}const k=_.length,C=T.length;for(let e=0;e<C;e++){const t=T[e];let n=_[e];if(void 0===n){n=document.createElement("span"),_[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:${h};opacity:0.3;`,e===C-1&&u.parentNode!==n&&n.appendChild(u)}for(let e=k-1;e>=C;e--){const t=_[e];c.removeChild(t),_.pop()}}function z(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,i=0,l=null,r=0;if(null!==n&&null!==o){const t=M(n,e),c=M(o,e);null!==t&&null!==c&&([s,i]=B(t.type,t.index),[l,r]=B(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:i,focusCollabNode:l,focusOffset:r}}function Y(t,n){const o=n.awareness.getLocalState();if(null===o)return;const{anchorCollabNode:s,anchorOffset:i,focusCollabNode:l,focusOffset:r}=z(t,o);if(null!==s&&null!==l){const t=s.getKey(),n=l.getKey(),o=e.$getSelection();if(!e.$isRangeSelection(o))return;K(o.anchor,t,i),K(o.focus,n,r)}}function K(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 B(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof P){const{node:e,offset:o}=$(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function I(e,t){return t.awareness.getStates()}function W(e,t,n){const{getAwarenessStates:o=I}=n??{},s=Array.from(o(e,t)),i=e.clientID,l=e.cursors,r=e.editor._editorState._nodeMap,c=new Set;for(let t=0;t<s.length;t++){const n=s[t],[o,a]=n;if(o!==i){c.add(o);const{name:t,color:n,focusing:s}=a;let i=null,f=l.get(o);if(void 0===f&&(f=L(t,n),l.set(o,f)),s){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(i=f.selection,null===i)i=R(f,e,n,l,s);else{const t=i.anchor,o=i.focus;t.key=e,t.offset=n,o.key=l,o.offset=s}}}j(e,f,i,r)}}const a=Array.from(l.keys());for(let t=0;t<a.length;t++){const n=a[t];if(!c.has(n)){const t=l.get(n);void 0!==t&&(D(e,t),l.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:d,awarenessData:u}=l;let h=null,_=null;(null!==s&&(null===r||s.is(o))||null!==o)&&(e.$isRangeSelection(s)&&(h=E(s.anchor,t),_=E(s.focus,t)),(F(r,h)||F(c,_))&&i.setLocalState({...l,anchorPos:h,awarenessData:u,color:f,focusPos:_,focusing:d,name:a}))}function G(n,o){if(o instanceof t.YMapEvent&&function(n,o){const{target:s}=o;if(!s._item||"__state"!==s._item.parentSub||void 0!==p(s)||!(s.parent instanceof t.XmlText||s.parent instanceof t.XmlElement||s.parent instanceof t.Map))return!1;const i=g(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=g(n,i);if(l instanceof P&&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 v&&o instanceof t.YXmlEvent){const{attributesChanged:e}=o;e.size>0&&l.syncPropertiesFromYjs(n,e)}else s(82)}const X=e.createCommand("CONNECTED_COMMAND"),q=e.createCommand("TOGGLE_CONNECT_COMMAND");exports.CONNECTED_COMMAND=X,exports.TOGGLE_CONNECT_COMMAND=q,exports.createBinding=function(e,n,o,i,l,r){null==i&&s(81);const c=O(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=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: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=W,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(e.COLLABORATION_TAG)||a.has(e.HISTORIC_TAG))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=W){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];G(t,n)}const i=e.$getSelection();if(e.$isRangeSelection(i))if(S(i)){const o=r._selection;if(e.$isRangeSelection(o)&&(Y(t,n),S(i))){b(i.anchor.key,r)}U(t,n,o,e.$getSelection())}else Y(t,n);s||e.$addUpdateTag(e.SKIP_SCROLL_INTO_VIEW_TAG)}),{onUpdate:()=>{i(t,n),l.update((()=>{0===e.$getRoot().getChildrenSize()&&e.$getRoot().append(e.$createParagraphNode())}))},skipTransforms:!0,tag:s?e.HISTORIC_TAG:e.COLLABORATION_TAG})};
|
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 i,$isDecoratorNode as l,createEditor as r,$getWritableNodeState as c,$getRoot as f,$isRootNode as a,$getNodeByKeyOrThrow as u,removeFromParent as d,$addUpdateTag as h,SKIP_SCROLL_INTO_VIEW_TAG as _,$createParagraphNode as p,HISTORIC_TAG as g,COLLABORATION_TAG as y,createCommand as x}from"lexical";import{XmlText as m,Map as k,XmlElement as b,Doc as C,createAbsolutePositionFromRelativePosition as N,createRelativePositionFromTypeIndex as v,compareRelativePositions as w,YMapEvent as S,YTextEvent as T,YXmlEvent as P,UndoManager as F}from"yjs";import{$createChildrenArray as O}from"@lexical/offset";import{createDOMRange as E,createRectsFromDOMRange as M}from"@lexical/selection";function L(e,...t){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",e);for(const e of t)o.append("v",e);throw n.search=o.toString(),Error(`Minified Lexical error #${e}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}class z{constructor(e,t){this._key="",this._map=e,this._parent=t,this._type="linebreak"}getNode(){const n=e(this._key);return t(n)?n:null}getKey(){return this._key}getSharedType(){return this._map}getType(){return this._type}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function j(e,t){const n=new z(e,t);return e._collabNode=n,n}class A{constructor(e,t,n,o){this._key="",this._map=e,this._parent=n,this._text=t,this._type=o,this._normalized=!1}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return n(t)?t:null}getNode(){const t=e(this._key);return n(t)?t:null}getSharedType(){return this._map}getType(){return this._type}getKey(){return this._key}getSize(){return this._text.length+(this._normalized?0:1)}getOffset(){return this._parent.getChildOffset(this)}spliceText(e,t,n){const o=this._parent._xmlText,s=this.getOffset()+1+e;0!==t&&o.delete(s,t),""!==n&&o.insert(s,n)}syncPropertiesAndTextFromLexical(e,t,n){const i=this.getPrevNode(n),l=t.__text;if(V(e,this._map,i,t),null!==i){const e=i.__text;if(e!==l){!function(e,t,n,i){const l=o();let r=i.length;if(s(l)&&l.isCollapsed()){const e=l.anchor;e.key===t&&(r=e.offset)}const c=function(e,t,n){const o=e.length,s=t.length;let i=0,l=0;for(;i<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}}(n,i,r);e.spliceText(c.index,c.remove,c.insert)}(this,t.__key,e,l),this._text=l}}}syncPropertiesAndTextFromYjs(e,t){const n=this.getNode();null===n&&L(84),J(e,this._map,n,t);const o=this._text;n.__text!==o&&n.setTextContent(o)}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function Y(e,t,n,o){const s=new A(e,t,n,o);return e._collabNode=s,s}const D=new Set(["__key","__parent","__next","__prev","__state"]),I=new Set(["__first","__last","__size"]),K=new Set(["__cachedText"]),$=new Set(["__text"]);function W(e,t,o){if(D.has(e)||"function"==typeof t[e])return!0;if(n(t)){if($.has(e))return!0}else if(i(t)&&(I.has(e)||a(t)&&K.has(e)))return!0;const s=t.constructor,l=o.excludedProperties.get(s);return null!=l&&l.has(e)}function R(e,o,s){const r=o.__type;let c;if(i(o)){c=ie(new m,s,r),c.syncPropertiesFromLexical(e,o,null),c.syncChildrenFromLexical(e,o,null,null,null)}else if(n(o)){c=Y(new k,o.__text,s,r),c.syncPropertiesAndTextFromLexical(e,o,null)}else if(t(o)){const e=new k;e.set("__type","linebreak"),c=j(e,s)}else if(l(o)){c=oe(new b,s,r),c.syncPropertiesFromLexical(e,o,null)}else L(86);return c._key=o.__key,c}function U(e){const t=q(e,"__type");return"string"!=typeof t&&void 0!==t&&L(87),t}function B(e,t,n){const o=t._collabNode;if(void 0===o){const o=e.editor._nodes,s=U(t);"string"!=typeof s&&L(87);void 0===o.get(s)&&L(88,s);const i=t.parent,l=void 0===n&&null!==i?B(e,i):n||null;if(l instanceof se||L(89),t instanceof m)return ie(t,l,s);if(t instanceof k)return"linebreak"===s?j(t,l):Y(t,"",l,s);if(t instanceof b)return oe(t,l,s)}return o}function G(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 se){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof A?t.syncPropertiesAndTextFromYjs(e,null):t instanceof ne&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(i.__key,t),i}function J(e,t,n,o){const s=null===o?t instanceof k?Array.from(t.keys()):Object.keys(t.getAttributes()):Array.from(o);let i;for(let o=0;o<s.length;o++){const l=s[o];if(W(l,n,e)){"__state"===l&&(i||(i=n.getWritable()),Q(e,t,i));continue}const c=n[l];let f=q(t,l);if(c!==f){if(f instanceof C){const t=e.docMap;c instanceof C&&t.delete(c.guid);const n=r(),o=f.guid;n._key=o,t.set(o,f),f=n}void 0===i&&(i=n.getWritable()),i[l]=f}}}function q(e,t){return e instanceof k?e.get(t):e.getAttribute(t)}function H(e,t,n){e instanceof k?e.set(t,n):e.setAttribute(t,n)}function Q(e,t,n){const o=q(t,"__state");o instanceof k&&c(n).updateFromJSON(o.toJSON())}function V(e,t,n,o){const s=o.__type,i=e.nodeProperties;let l=i.get(s);void 0===l&&(l=Object.keys(o).filter((t=>!W(t,o,e))),i.set(s,l));const r=e.editor.constructor;!function(e,t,n,o){const s=o.__state,i=q(t,"__state");if(!s)return;const[l,r]=s.getInternalState(),c=n&&n.__state,f=i instanceof k?i:new k;if(c===s)return;const[a,u]=c&&f.doc?c.getInternalState():[void 0,new Map];if(l)for(const[e,t]of Object.entries(l))a&&t!==a[e]&&f.set(e,t);for(const[e,t]of r)u.get(e)!==t&&f.set(e.key,e.unparse(t));i||H(t,"__state",f)}(0,t,n,o);for(let s=0;s<l.length;s++){const i=l[s],c=null===n?void 0:n[i];let f=o[i];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 C,i=s.guid;f._key=i,t.set(i,s),f=s,e.editor.update((()=>{o.markDirty()}))}H(t,i,f)}}}function X(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function Z(e,t,n){let o=0,s=0;const i=e._children,l=i.length;for(;s<l;s++){const e=i[s],r=o;o+=e.getSize();if((n?o>=t:o>t)&&e instanceof A){let n=t-r-1;n<0&&(n=0);return{length:o-t,node:e,nodeIndex:s,offset:n}}if(o>t)return{length:0,node:e,nodeIndex:s,offset:r};if(s===l-1)return{length:0,node:null,nodeIndex:s+1,offset:r+1}}return{length:0,node:null,nodeIndex:0,offset:0}}function ee(e){const t=e.anchor,o=e.focus;let s=!1;try{const e=t.getNode(),i=o.getNode();(!e.isAttached()||!i.isAttached()||n(e)&&t.offset>e.getTextContentSize()||n(i)&&o.offset>i.getTextContentSize())&&(s=!0)}catch(e){s=!0}return s}function te(t,n){const o=n._nodeMap.get(t);if(!o)return void f().selectStart();const s=o.__prev;let i=null;s&&(i=e(s)),null===i&&null!==o.__parent&&(i=e(o.__parent)),null!==i?null!==i&&i.isAttached()?i.selectEnd():te(i.__key,n):f().selectStart()}class ne{constructor(e,t,n){this._key="",this._xmlElem=e,this._parent=t,this._type=n}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return l(t)?t:null}getNode(){const t=e(this._key);return l(t)?t:null}getSharedType(){return this._xmlElem}getType(){return this._type}getKey(){return this._key}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}syncPropertiesFromLexical(e,t,n){const o=this.getPrevNode(n);V(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&L(83);J(e,this._xmlElem,n,t)}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function oe(e,t,n){const o=new ne(e,t,n);return e._collabNode=o,o}class se{constructor(e,t,n){this._key="",this._children=[],this._xmlText=e,this._type=n,this._parent=t}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return i(t)?t:null}getNode(){const t=e(this._key);return i(t)?t:null}getSharedType(){return this._xmlText}getType(){return this._type}getKey(){return this._key}isEmpty(){return 0===this._children.length}getSize(){return 1}getOffset(){const e=this._parent;return null===e&&L(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&L(91),J(e,this._xmlText,n,t)}applyChildrenYjsDelta(e,t){const n=this._children;let o=0,s=null;for(let i=0;i<t.length;i++){const l=t[i],r=l.insert,c=l.delete;if(null!=l.retain)o+=l.retain;else if("number"==typeof c){let e=c;for(;e>0;){const{node:t,nodeIndex:s,offset:i,length:l}=Z(this,o,!1);if(t instanceof se||t instanceof z||t instanceof ne)n.splice(s,1),e-=1;else{if(!(t instanceof A))break;{const o=Math.min(e,l),r=0!==s?n[s-1]:null,c=t.getSize();if(0===i&&l===c){n.splice(s,1);const e=X(t._text,i,o-1,"");e.length>0&&(r instanceof A?r._text+=e:this._xmlText.delete(i,e.length))}else t._text=X(t._text,i,o,"");e-=o}}}}else{if(null==r)throw new Error("Unexpected delta format");if("string"==typeof r){const{node:e,offset:t}=Z(this,o,!0);e instanceof A?e._text=X(e._text,t,0,r):this._xmlText.delete(t,r.length),o+=r.length}else{const t=r,{node:i,nodeIndex:l,length:c}=Z(this,o,!1),f=B(e,t,this);if(i instanceof A&&c>0&&c<i._text.length){const e=i._text,t=e.length-c;i._text=X(e,t,c,""),n.splice(l+1,0,f),s=X(e,0,t,"")}else n.splice(l,0,f);null!==s&&f instanceof A&&(f._text=s+f._text,s=null),o+=1}}}}syncChildrenFromYjs(e){const t=this.getNode();null===t&&L(92);const o=t.__key,s=O(t,null),i=s.length,l=this._children,r=l.length,c=e.collabNodeMap,f=new Set;let a,h,_=0,p=null;r!==i&&(h=t.getWritable());for(let i=0;i<r;i++){const g=s[_],y=l[i],x=y.getNode(),m=y._key;if(null!==x&&g===m){const t=n(x);if(f.add(g),t)if(y._key=g,y instanceof se){const t=y._xmlText;y.syncPropertiesFromYjs(e,null),y.applyChildrenYjsDelta(e,t.toDelta()),y.syncChildrenFromYjs(e)}else y instanceof A?y.syncPropertiesAndTextFromYjs(e,null):y instanceof ne?y.syncPropertiesFromYjs(e,null):y instanceof z||L(93);p=x,_++}else{if(void 0===a){a=new Set;for(let e=0;e<r;e++){const t=l[e]._key;""!==t&&a.add(t)}}if(null!==x&&void 0!==g&&!a.has(g)){const e=u(g);d(e),i--,_++;continue}h=t.getWritable();const n=G(e,y,o),s=n.__key;if(c.set(s,y),null===p){const e=h.getFirstChild();if(h.__first=s,null!==e){const t=e.getWritable();t.__prev=s,n.__next=t.__key}}else{const e=p.getWritable(),t=p.getNextSibling();if(e.__next=s,n.__prev=p.__key,null!==t){const e=t.getWritable();e.__prev=s,n.__next=e.__key}}i===r-1&&(h.__last=s),h.__size++,p=n}}for(let t=0;t<i;t++){const n=s[t];if(!f.has(n)){const t=u(n),o=e.collabNodeMap.get(n);void 0!==o&&o.destroy(e),d(t)}}}syncPropertiesFromLexical(e,t,n){V(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(e,t,o,s,r,c){const f=this._children[t],a=u(o);f instanceof se&&i(a)?(f.syncPropertiesFromLexical(e,a,s),f.syncChildrenFromLexical(e,a,s,r,c)):f instanceof A&&n(a)?f.syncPropertiesAndTextFromLexical(e,a,s):f instanceof ne&&l(a)&&f.syncPropertiesFromLexical(e,a,s)}syncChildrenFromLexical(e,t,n,o,s){const i=this.getPrevNode(n),l=null===i?[]:O(i,n),r=O(t,null),c=l.length-1,f=r.length-1,a=e.collabNodeMap;let d,h,_=0,p=0;for(;_<=c&&p<=f;){const t=l[_],i=r[p];if(t===i)this._syncChildFromLexical(e,p,i,n,o,s),_++,p++;else{void 0===d&&(d=new Set(l)),void 0===h&&(h=new Set(r));const n=h.has(t),o=d.has(i);if(n){const t=R(e,u(i),this);a.set(i,t),o?(this.splice(e,p,1,t),_++,p++):(this.splice(e,p,0,t),p++)}else this.splice(e,p,1),_++}}const g=_>c,y=p>f;if(g&&!y)for(;p<=f;++p){const t=r[p],n=R(e,u(t),this);this.append(n),a.set(t,n)}else if(y&&!g)for(let t=this._children.length-1;t>=p;t--)this.splice(e,t,1)}append(e){const t=this._xmlText,n=this._children,o=n[n.length-1],s=void 0!==o?o.getOffset()+o.getSize():0;if(e instanceof se)t.insertEmbed(s,e._xmlText);else if(e instanceof A){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof z?t.insertEmbed(s,e._map):e instanceof ne&&t.insertEmbed(s,e._xmlElem);this._children.push(e)}splice(e,t,n,o){const s=this._children,i=s[t];if(void 0===i)return void 0===o&&L(94),void this.append(o);const l=i.getOffset();-1===l&&L(95);const r=this._xmlText;if(0!==n&&r.delete(l,i.getSize()),o instanceof se)r.insertEmbed(l,o._xmlText);else if(o instanceof A){const e=o._map;null===e.parent&&r.insertEmbed(l,e),r.insert(l+1,o._text)}else o instanceof z?r.insertEmbed(l,o._map):o instanceof ne&&r.insertEmbed(l,o._xmlElem);if(0!==n){const o=s.slice(t,t+n);for(let t=0;t<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 ie(e,t,n){const o=new se(e,t,n);return e._collabNode=o,o}function le(e,t,n,o,s,i){null==o&&L(81);const l=ie(o.get("root",m),null,"root");return l._key="root",{clientID:o.clientID,collabNodeMap:new Map,cursors:new Map,cursorsContainer:null,doc:o,docMap:s,editor:e,excludedProperties:i||new Map,id:n,nodeProperties:new Map,root:l}}function re(e,t){const o=t.collabNodeMap.get(e.key);if(void 0===o)return null;let s=e.offset,l=o.getSharedType();if(o instanceof A){l=o._parent._xmlText;const e=o.getOffset();if(-1===e)return null;s=e+1+s}else if(o instanceof se&&"element"===e.type){const t=e.getNode();i(t)||L(184);let o=0,l=0,r=t.getFirstChild();for(;null!==r&&l++<s;)n(r)?o+=r.getTextContentSize()+1:o++,r=r.getNextSibling();s=o}return v(l,s)}function ce(e,t){return N(e,t.doc)}function fe(e,t){if(null==e){if(null!=t)return!0}else if(null==t||!w(e,t))return!0;return!1}function ae(e,t){return{color:t,name:e,selection:null}}function ue(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 de(e,t){const n=t.selection;null!==n&&ue(e,n)}function he(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 _e(e,n,o,s){const i=e.editor,l=i.getRootElement(),r=e.cursorsContainer;if(null===r||null===l)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 ue(e,a));n.selection=o;const u=o.caret,d=o.color,h=o.selections,_=o.anchor,p=o.focus,g=_.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=[i.getElementByKey(g).getBoundingClientRect()]}else{const e=E(i,x,_.offset,m,p.offset);if(null===e)return;k=M(i,e)}const b=h.length,C=k.length;for(let e=0;e<C;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),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===C-1&&u.parentNode!==n&&n.appendChild(u)}for(let e=b-1;e>=C;e--){const t=h[e];r.removeChild(t),h.pop()}}function pe(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,i=0,l=null,r=0;if(null!==n&&null!==o){const t=ce(n,e),c=ce(o,e);null!==t&&null!==c&&([s,i]=xe(t.type,t.index),[l,r]=xe(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:i,focusCollabNode:l,focusOffset:r}}function ge(e,t){const n=t.awareness.getLocalState();if(null===n)return;const{anchorCollabNode:i,anchorOffset:l,focusCollabNode:r,focusOffset:c}=pe(e,n);if(null!==i&&null!==r){const e=i.getKey(),t=r.getKey(),n=o();if(!s(n))return;ye(n.anchor,e,l),ye(n.focus,t,c)}}function ye(t,o,s){if(t.key!==o||t.offset!==s){let l=e(o);if(null!==l&&!i(l)&&!n(l)){const e=l.getParentOrThrow();o=e.getKey(),s=l.getIndexWithinParent(),l=e}t.set(o,s,i(l)?"element":"text")}}function xe(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof se){const{node:e,offset:o}=Z(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function me(e,t){return t.awareness.getStates()}function ke(e,t,n){const{getAwarenessStates:o=me}=n??{},s=Array.from(o(e,t)),i=e.clientID,l=e.cursors,r=e.editor._editorState._nodeMap,c=new Set;for(let t=0;t<s.length;t++){const n=s[t],[o,f]=n;if(o!==i){c.add(o);const{name:t,color:n,focusing:s}=f;let i=null,a=l.get(o);if(void 0===a&&(a=ae(t,n),l.set(o,a)),s){const{anchorCollabNode:t,anchorOffset:n,focusCollabNode:o,focusOffset:s}=pe(e,f);if(null!==t&&null!==o){const e=t.getKey(),l=o.getKey();if(i=a.selection,null===i)i=he(a,e,n,l,s);else{const t=i.anchor,o=i.focus;t.key=e,t.offset=n,o.key=l,o.offset=s}}}_e(e,a,i,r)}}const f=Array.from(l.keys());for(let t=0;t<f.length;t++){const n=f[t];if(!c.has(n)){const t=l.get(n);void 0!==t&&(de(e,t),l.delete(n))}}}function be(e,t,n,o){const i=t.awareness,l=i.getLocalState();if(null===l)return;const{anchorPos:r,focusPos:c,name:f,color:a,focusing:u,awarenessData:d}=l;let h=null,_=null;(null!==o&&(null===r||o.is(n))||null!==n)&&(s(o)&&(h=re(o.anchor,e),_=re(o.focus,e)),(fe(r,h)||fe(c,_))&&i.setLocalState({...l,anchorPos:h,awarenessData:d,color:a,focusPos:_,focusing:u,name:f}))}function Ce(e,t){if(t instanceof S&&function(e,t){const{target:n}=t;if(!n._item||"__state"!==n._item.parentSub||void 0!==U(n)||!(n.parent instanceof m||n.parent instanceof b||n.parent instanceof k))return!1;const o=B(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=B(e,n);if(o instanceof se&&t instanceof T){const{keysChanged:n,childListChanged:s,delta:i}=t;n.size>0&&o.syncPropertiesFromYjs(e,n),s&&(o.applyChildrenYjsDelta(e,i),o.syncChildrenFromYjs(e))}else if(o instanceof A&&t instanceof S){const{keysChanged:n}=t;n.size>0&&o.syncPropertiesAndTextFromYjs(e,n)}else if(o instanceof ne&&t instanceof P){const{attributesChanged:n}=t;n.size>0&&o.syncPropertiesFromYjs(e,n)}else L(82)}function Ne(e,t,n,i,l=ke){const r=e.editor,c=r._editorState;n.forEach((e=>e.delta)),r.update((()=>{for(let t=0;t<n.length;t++){const o=n[t];Ce(e,o)}const l=o();if(s(l))if(ee(l)){const n=c._selection;if(s(n)&&(ge(e,t),ee(l))){te(l.anchor.key,c)}be(e,t,n,o())}else ge(e,t);i||h(_)}),{onUpdate:()=>{l(e,t),r.update((()=>{0===f().getChildrenSize()&&f().append(p())}))},skipTransforms:!0,tag:i?g:y})}function ve(t,s,i,l,r,c,a,u){!function(e,t){e.doc.transact(t,e)}(t,(()=>{l.read((()=>{if(u.has(y)||u.has(g))return void(a.size>0&&function(t,o){const s=Array.from(o),i=t.collabNodeMap,l=[],r=[];for(let t=0;t<s.length;t++){const o=s[t],c=e(o),f=i.get(o);if(f instanceof A)if(n(c))l.push([f,c.__text]);else{const e=f.getOffset();if(-1===e)continue;const t=f._parent;f._normalized=!0,t._xmlText.delete(e,1),r.push(f)}}for(let e=0;e<r.length;e++){const t=r[e],n=t.getKey();i.delete(n);const o=t._parent._children,s=o.indexOf(t);o.splice(s,1)}for(let e=0;e<l.length;e++){const[t,n]=l[e];t._text=n}}(t,a));if(r.has("root")){const e=i._nodeMap,n=f(),o=t.root;o.syncPropertiesFromLexical(t,n,e),o.syncChildrenFromLexical(t,n,e,r,c)}const l=o(),d=i._selection;be(t,s,d,l)}))}))}const we=x("CONNECTED_COMMAND"),Se=x("TOGGLE_CONNECT_COMMAND");function Te(e,t){return new F(t,{trackedOrigins:new Set([e,null])})}function Pe(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})}function Fe(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)}export{we as CONNECTED_COMMAND,Se as TOGGLE_CONNECT_COMMAND,le as createBinding,Te as createUndoManager,pe as getAnchorAndFocusCollabNodesForUserState,Pe as initLocalState,Fe as setLocalStateFocus,ke as syncCursorPositions,ve as syncLexicalUpdateToYjs,Ne as syncYjsChangesToLexical};
|
|
9
|
+
import{$getNodeByKey as e,$isLineBreakNode as t,$isTextNode as n,$getSelection as o,$isRangeSelection as s,createEditor as l,$isElementNode as i,$isDecoratorNode as r,$isRootNode as c,$getWritableNodeState as a,$getRoot as f,$getNodeByKeyOrThrow as u,removeFromParent as d,$addUpdateTag as h,SKIP_SCROLL_INTO_VIEW_TAG as _,HISTORIC_TAG as p,COLLABORATION_TAG as g,$createParagraphNode as y,createCommand as x}from"lexical";import{XmlText as m,Map as k,XmlElement as b,Doc as C,createAbsolutePositionFromRelativePosition as N,createRelativePositionFromTypeIndex as v,compareRelativePositions as w,YMapEvent as S,YTextEvent as T,YXmlEvent as P,UndoManager as F}from"yjs";import{$createChildrenArray as O}from"@lexical/offset";import{createDOMRange as E,createRectsFromDOMRange as M}from"@lexical/selection";function L(e,...t){const n=new URL("https://lexical.dev/docs/error"),o=new URLSearchParams;o.append("code",e);for(const e of t)o.append("v",e);throw n.search=o.toString(),Error(`Minified Lexical error #${e}; visit ${n.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}class z{_map;_key;_parent;_type;constructor(e,t){this._key="",this._map=e,this._parent=t,this._type="linebreak"}getNode(){const n=e(this._key);return t(n)?n:null}getKey(){return this._key}getSharedType(){return this._map}getType(){return this._type}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function j(e,t){const n=new z(e,t);return e._collabNode=n,n}class A{_map;_key;_parent;_text;_type;_normalized;constructor(e,t,n,o){this._key="",this._map=e,this._parent=n,this._text=t,this._type=o,this._normalized=!1}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return 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(V(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&&L(84),J(e,this._map,n,t);const o=this._text;n.__text!==o&&n.setTextContent(o)}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function Y(e,t,n,o){const s=new A(e,t,n,o);return e._collabNode=s,s}const D=new Set(["__key","__parent","__next","__prev","__state"]),I=new Set(["__first","__last","__size"]),K=new Set(["__cachedText"]),$=new Set(["__text"]);function W(e,t,o){if(D.has(e)||"function"==typeof t[e])return!0;if(n(t)){if($.has(e))return!0}else if(i(t)&&(I.has(e)||c(t)&&K.has(e)))return!0;const s=t.constructor,l=o.excludedProperties.get(s);return null!=l&&l.has(e)}function R(e,o,s){const l=o.__type;let c;if(i(o)){c=le(new m,s,l),c.syncPropertiesFromLexical(e,o,null),c.syncChildrenFromLexical(e,o,null,null,null)}else if(n(o)){c=Y(new k,o.__text,s,l),c.syncPropertiesAndTextFromLexical(e,o,null)}else if(t(o)){const e=new k;e.set("__type","linebreak"),c=j(e,s)}else if(r(o)){c=oe(new b,s,l),c.syncPropertiesFromLexical(e,o,null)}else L(86);return c._key=o.__key,c}function U(e){const t=q(e,"__type");return"string"!=typeof t&&void 0!==t&&L(87),t}function B(e,t,n){const o=t._collabNode;if(void 0===o){const o=e.editor._nodes,s=U(t);"string"!=typeof s&&L(87);void 0===o.get(s)&&L(88,s);const l=t.parent,i=void 0===n&&null!==l?B(e,l):n||null;if(i instanceof se||L(89),t instanceof m)return le(t,i,s);if(t instanceof k)return"linebreak"===s?j(t,i):Y(t,"",i,s);if(t instanceof b)return oe(t,i,s)}return o}function G(e,t,n){const o=t.getType(),s=e.editor._nodes.get(o);void 0===s&&L(88,o);const l=new s.klass;if(l.__parent=n,t._key=l.__key,t instanceof se){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof A?t.syncPropertiesAndTextFromYjs(e,null):t instanceof ne&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(l.__key,t),l}function J(e,t,n,o){const s=null===o?t instanceof k?Array.from(t.keys()):Object.keys(t.getAttributes()):Array.from(o);let i;for(let o=0;o<s.length;o++){const r=s[o];if(W(r,n,e)){"__state"===r&&(i||(i=n.getWritable()),Q(e,t,i));continue}const c=n[r];let a=q(t,r);if(c!==a){if(a instanceof C){const t=e.docMap;c instanceof C&&t.delete(c.guid);const n=l(),o=a.guid;n._key=o,t.set(o,a),a=n}void 0===i&&(i=n.getWritable()),i[r]=a}}}function q(e,t){return e instanceof k?e.get(t):e.getAttribute(t)}function H(e,t,n){e instanceof k?e.set(t,n):e.setAttribute(t,n)}function Q(e,t,n){const o=q(t,"__state");o instanceof k&&a(n).updateFromJSON(o.toJSON())}function V(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=>!W(t,o,e))),l.set(s,i));const r=e.editor.constructor;!function(e,t,n,o){const s=o.__state,l=q(t,"__state");if(!s)return;const[i,r]=s.getInternalState(),c=n&&n.__state,a=l instanceof k?l:new k;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||H(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 C,l=s.guid;a._key=l,t.set(l,s),a=s,e.editor.update((()=>{o.markDirty()}))}H(t,l,a)}}}function X(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function Z(e,t,n){let o=0,s=0;const 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 A){let n=t-r-1;n<0&&(n=0);return{length:o-t,node:e,nodeIndex:s,offset:n}}if(o>t)return{length:0,node:e,nodeIndex:s,offset:r};if(s===i-1)return{length:0,node:null,nodeIndex:s+1,offset:r+1}}return{length:0,node:null,nodeIndex:0,offset:0}}function ee(e){const t=e.anchor,o=e.focus;let s=!1;try{const e=t.getNode(),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 te(t,n){const o=n._nodeMap.get(t);if(!o)return void f().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():te(l.__key,n):f().selectStart()}class ne{_xmlElem;_key;_parent;_type;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 r(t)?t:null}getNode(){const t=e(this._key);return r(t)?t:null}getSharedType(){return this._xmlElem}getType(){return this._type}getKey(){return this._key}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}syncPropertiesFromLexical(e,t,n){const o=this.getPrevNode(n);V(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&L(83);J(e,this._xmlElem,n,t)}destroy(e){const t=e.collabNodeMap;t.get(this._key)===this&&t.delete(this._key)}}function oe(e,t,n){const o=new ne(e,t,n);return e._collabNode=o,o}class se{_key;_children;_xmlText;_type;_parent;constructor(e,t,n){this._key="",this._children=[],this._xmlText=e,this._type=n,this._parent=t}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return i(t)?t:null}getNode(){const t=e(this._key);return i(t)?t:null}getSharedType(){return this._xmlText}getType(){return this._type}getKey(){return this._key}isEmpty(){return 0===this._children.length}getSize(){return 1}getOffset(){const e=this._parent;return null===e&&L(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&L(91),J(e,this._xmlText,n,t)}applyChildrenYjsDelta(e,t){const n=this._children;let o=0,s=null;for(let 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}=Z(this,o,!1);if(t instanceof se||t instanceof z||t instanceof ne)n.splice(s,1),e-=1;else{if(!(t instanceof A))break;{const o=Math.min(e,i),r=0!==s?n[s-1]:null,c=t.getSize();if(0===l&&i===c){n.splice(s,1);const e=X(t._text,l,o-1,"");e.length>0&&(r instanceof A?r._text+=e:this._xmlText.delete(l,e.length))}else t._text=X(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}=Z(this,o,!0);e instanceof A?e._text=X(e._text,t,0,r):this._xmlText.delete(t,r.length),o+=r.length}else{const t=r,{node:l,nodeIndex:i,length:c}=Z(this,o,!1),a=B(e,t,this);if(l instanceof A&&c>0&&c<l._text.length){const e=l._text,t=e.length-c;l._text=X(e,t,c,""),n.splice(i+1,0,a),s=X(e,0,t,"")}else n.splice(i,0,a);null!==s&&a instanceof A&&(a._text=s+a._text,s=null),o+=1}}}}syncChildrenFromYjs(e){const t=this.getNode();null===t&&L(92);const o=t.__key,s=O(t,null),l=s.length,i=this._children,r=i.length,c=e.collabNodeMap,a=new Set;let f,h,_=0,p=null;r!==l&&(h=t.getWritable());for(let l=0;l<r;l++){const g=s[_],y=i[l],x=y.getNode(),m=y._key;if(null!==x&&g===m){const t=n(x);if(a.add(g),t)if(y._key=g,y instanceof se){const t=y._xmlText;y.syncPropertiesFromYjs(e,null),y.applyChildrenYjsDelta(e,t.toDelta()),y.syncChildrenFromYjs(e)}else y instanceof A?y.syncPropertiesAndTextFromYjs(e,null):y instanceof ne?y.syncPropertiesFromYjs(e,null):y instanceof z||L(93);p=x,_++}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!==x&&void 0!==g&&!f.has(g)){const e=u(g);d(e),l--,_++;continue}h=t.getWritable();const n=G(e,y,o),s=n.__key;if(c.set(s,y),null===p){const e=h.getFirstChild();if(h.__first=s,null!==e){const t=e.getWritable();t.__prev=s,n.__next=t.__key}}else{const e=p.getWritable(),t=p.getNextSibling();if(e.__next=s,n.__prev=p.__key,null!==t){const e=t.getWritable();e.__prev=s,n.__next=e.__key}}l===r-1&&(h.__last=s),h.__size++,p=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),d(t)}}}syncPropertiesFromLexical(e,t,n){V(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(e,t,o,s,l,c){const a=this._children[t],f=u(o);a instanceof se&&i(f)?(a.syncPropertiesFromLexical(e,f,s),a.syncChildrenFromLexical(e,f,s,l,c)):a instanceof A&&n(f)?a.syncPropertiesAndTextFromLexical(e,f,s):a instanceof ne&&r(f)&&a.syncPropertiesFromLexical(e,f,s)}syncChildrenFromLexical(e,t,n,o,s){const l=this.getPrevNode(n),i=null===l?[]:O(l,n),r=O(t,null),c=i.length-1,a=r.length-1,f=e.collabNodeMap;let d,h,_=0,p=0;for(;_<=c&&p<=a;){const t=i[_],l=r[p];if(t===l)this._syncChildFromLexical(e,p,l,n,o,s),_++,p++;else{void 0===d&&(d=new Set(i)),void 0===h&&(h=new Set(r));const n=h.has(t),o=d.has(l);if(n){const t=R(e,u(l),this);f.set(l,t),o?(this.splice(e,p,1,t),_++,p++):(this.splice(e,p,0,t),p++)}else this.splice(e,p,1),_++}}const g=_>c,y=p>a;if(g&&!y)for(;p<=a;++p){const t=r[p],n=R(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 se)t.insertEmbed(s,e._xmlText);else if(e instanceof A){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof z?t.insertEmbed(s,e._map):e instanceof ne&&t.insertEmbed(s,e._xmlElem);this._children.push(e)}splice(e,t,n,o){const s=this._children,l=s[t];if(void 0===l)return void 0===o&&L(94),void this.append(o);const i=l.getOffset();-1===i&&L(95);const r=this._xmlText;if(0!==n&&r.delete(i,l.getSize()),o instanceof se)r.insertEmbed(i,o._xmlText);else if(o instanceof A){const e=o._map;null===e.parent&&r.insertEmbed(i,e),r.insert(i+1,o._text)}else o instanceof z?r.insertEmbed(i,o._map):o instanceof ne&&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 le(e,t,n){const o=new se(e,t,n);return e._collabNode=o,o}function ie(e,t,n,o,s,l){null==o&&L(81);const i=le(o.get("root",m),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 re(e,t){const o=t.collabNodeMap.get(e.key);if(void 0===o)return null;let s=e.offset,l=o.getSharedType();if(o instanceof A){l=o._parent._xmlText;const e=o.getOffset();if(-1===e)return null;s=e+1+s}else if(o instanceof se&&"element"===e.type){const t=e.getNode();i(t)||L(184);let o=0,l=0,r=t.getFirstChild();for(;null!==r&&l++<s;)n(r)?o+=r.getTextContentSize()+1:o++,r=r.getNextSibling();s=o}return v(l,s)}function ce(e,t){return N(e,t.doc)}function ae(e,t){if(null==e){if(null!=t)return!0}else if(null==t||!w(e,t))return!0;return!1}function fe(e,t){return{color:t,name:e,selection:null}}function ue(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 de(e,t){const n=t.selection;null!==n&&ue(e,n)}function he(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 _e(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 ue(e,f));n.selection=o;const u=o.caret,d=o.color,h=o.selections,_=o.anchor,p=o.focus,g=_.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=E(l,x,_.offset,m,p.offset);if(null===e)return;k=M(l,e)}const b=h.length,C=k.length;for(let e=0;e<C;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),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===C-1&&u.parentNode!==n&&n.appendChild(u)}for(let e=b-1;e>=C;e--){const t=h[e];r.removeChild(t),h.pop()}}function pe(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,l=0,i=null,r=0;if(null!==n&&null!==o){const t=ce(n,e),c=ce(o,e);null!==t&&null!==c&&([s,l]=xe(t.type,t.index),[i,r]=xe(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:l,focusCollabNode:i,focusOffset:r}}function ge(e,t){const n=t.awareness.getLocalState();if(null===n)return;const{anchorCollabNode:l,anchorOffset:i,focusCollabNode:r,focusOffset:c}=pe(e,n);if(null!==l&&null!==r){const e=l.getKey(),t=r.getKey(),n=o();if(!s(n))return;ye(n.anchor,e,i),ye(n.focus,t,c)}}function ye(t,o,s){if(t.key!==o||t.offset!==s){let l=e(o);if(null!==l&&!i(l)&&!n(l)){const e=l.getParentOrThrow();o=e.getKey(),s=l.getIndexWithinParent(),l=e}t.set(o,s,i(l)?"element":"text")}}function xe(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof se){const{node:e,offset:o}=Z(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function me(e,t){return t.awareness.getStates()}function ke(e,t,n){const{getAwarenessStates:o=me}=n??{},s=Array.from(o(e,t)),l=e.clientID,i=e.cursors,r=e.editor._editorState._nodeMap,c=new Set;for(let t=0;t<s.length;t++){const n=s[t],[o,a]=n;if(o!==l){c.add(o);const{name:t,color:n,focusing:s}=a;let l=null,f=i.get(o);if(void 0===f&&(f=fe(t,n),i.set(o,f)),s){const{anchorCollabNode:t,anchorOffset:n,focusCollabNode:o,focusOffset:s}=pe(e,a);if(null!==t&&null!==o){const e=t.getKey(),i=o.getKey();if(l=f.selection,null===l)l=he(f,e,n,i,s);else{const t=l.anchor,o=l.focus;t.key=e,t.offset=n,o.key=i,o.offset=s}}}_e(e,f,l,r)}}const a=Array.from(i.keys());for(let t=0;t<a.length;t++){const n=a[t];if(!c.has(n)){const t=i.get(n);void 0!==t&&(de(e,t),i.delete(n))}}}function be(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 h=null,_=null;(null!==o&&(null===r||o.is(n))||null!==n)&&(s(o)&&(h=re(o.anchor,e),_=re(o.focus,e)),(ae(r,h)||ae(c,_))&&l.setLocalState({...i,anchorPos:h,awarenessData:d,color:f,focusPos:_,focusing:u,name:a}))}function Ce(e,t){if(t instanceof S&&function(e,t){const{target:n}=t;if(!n._item||"__state"!==n._item.parentSub||void 0!==U(n)||!(n.parent instanceof m||n.parent instanceof b||n.parent instanceof k))return!1;const o=B(e,n.parent).getNode();if(o){const e=a(o.getWritable());for(const o of t.keysChanged)e.updateFromUnknown(o,n.get(o))}return!0}(e,t))return;const{target:n}=t,o=B(e,n);if(o instanceof se&&t instanceof T){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 A&&t instanceof S){const{keysChanged:n}=t;n.size>0&&o.syncPropertiesAndTextFromYjs(e,n)}else if(o instanceof ne&&t instanceof P){const{attributesChanged:n}=t;n.size>0&&o.syncPropertiesFromYjs(e,n)}else L(82)}function Ne(e,t,n,l,i=ke){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];Ce(e,o)}const i=o();if(s(i))if(ee(i)){const n=c._selection;if(s(n)&&(ge(e,t),ee(i))){te(i.anchor.key,c)}be(e,t,n,o())}else ge(e,t);l||h(_)}),{onUpdate:()=>{i(e,t),r.update((()=>{0===f().getChildrenSize()&&f().append(y())}))},skipTransforms:!0,tag:l?p:g})}function ve(t,s,l,i,r,c,a,u){!function(e,t){e.doc.transact(t,e)}(t,(()=>{i.read((()=>{if(u.has(g)||u.has(p))return void(a.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 A)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,a));if(r.has("root")){const e=l._nodeMap,n=f(),o=t.root;o.syncPropertiesFromLexical(t,n,e),o.syncChildrenFromLexical(t,n,e,r,c)}const i=o(),d=l._selection;be(t,s,d,i)}))}))}const we=x("CONNECTED_COMMAND"),Se=x("TOGGLE_CONNECT_COMMAND");function Te(e,t){return new F(t,{trackedOrigins:new Set([e,null])})}function Pe(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})}function Fe(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{we as CONNECTED_COMMAND,Se as TOGGLE_CONNECT_COMMAND,ie as createBinding,Te as createUndoManager,pe as getAnchorAndFocusCollabNodesForUserState,Pe as initLocalState,Fe as setLocalStateFocus,ke as syncCursorPositions,ve as syncLexicalUpdateToYjs,Ne as syncYjsChangesToLexical};
|
package/index.d.ts
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
import type { Binding } from './Bindings';
|
|
9
9
|
import type { LexicalCommand } from 'lexical';
|
|
10
10
|
import type { Doc, RelativePosition, UndoManager, XmlText } from 'yjs';
|
|
11
|
+
import './types';
|
|
11
12
|
export type UserState = {
|
|
12
13
|
anchorPos: null | RelativePosition;
|
|
13
14
|
color: string;
|
package/package.json
CHANGED
|
@@ -11,13 +11,13 @@
|
|
|
11
11
|
"crdt"
|
|
12
12
|
],
|
|
13
13
|
"license": "MIT",
|
|
14
|
-
"version": "0.35.1-nightly.
|
|
14
|
+
"version": "0.35.1-nightly.20250925.0",
|
|
15
15
|
"main": "LexicalYjs.js",
|
|
16
16
|
"types": "index.d.ts",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@lexical/offset": "0.35.1-nightly.
|
|
19
|
-
"@lexical/selection": "0.35.1-nightly.
|
|
20
|
-
"lexical": "0.35.1-nightly.
|
|
18
|
+
"@lexical/offset": "0.35.1-nightly.20250925.0",
|
|
19
|
+
"@lexical/selection": "0.35.1-nightly.20250925.0",
|
|
20
|
+
"lexical": "0.35.1-nightly.20250925.0"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"yjs": ">=13.5.22"
|
package/types.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
3
|
+
*
|
|
4
|
+
* This source code is licensed under the MIT license found in the
|
|
5
|
+
* LICENSE file in the root directory of this source tree.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
import { CollabDecoratorNode } from './CollabDecoratorNode';
|
|
9
|
+
import { CollabElementNode } from './CollabElementNode';
|
|
10
|
+
import { CollabLineBreakNode } from './CollabLineBreakNode';
|
|
11
|
+
import { CollabTextNode } from './CollabTextNode';
|
|
12
|
+
declare module 'yjs' {
|
|
13
|
+
interface XmlElement {
|
|
14
|
+
_collabNode: CollabDecoratorNode;
|
|
15
|
+
}
|
|
16
|
+
interface XmlText {
|
|
17
|
+
_collabNode: CollabElementNode;
|
|
18
|
+
}
|
|
19
|
+
interface Map<MapType> {
|
|
20
|
+
_collabNode: CollabLineBreakNode | CollabTextNode;
|
|
21
|
+
}
|
|
22
|
+
}
|