@lexical/yjs 0.1.9 → 0.1.10

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 CHANGED
@@ -107,7 +107,7 @@ function diffTextContentAndApplyDelta(collabNode, key, prevText, nextText) {
107
107
  const selection = lexical.$getSelection();
108
108
  let cursorOffset = nextText.length;
109
109
 
110
- if (selection !== null && selection.isCollapsed()) {
110
+ if (lexical.$isRangeSelection(selection) && selection.isCollapsed()) {
111
111
  const anchor = selection.anchor;
112
112
 
113
113
  if (anchor.key === key) {
@@ -1697,8 +1697,8 @@ function syncLocalCursorPosition(binding, provider) {
1697
1697
  const focusKey = focusCollabNode.getKey();
1698
1698
  const selection = lexical.$getSelection();
1699
1699
 
1700
- if (selection === null) {
1701
- throw new Error('TODO: syncLocalCursorPosition');
1700
+ if (!lexical.$isRangeSelection(selection)) {
1701
+ return;
1702
1702
  }
1703
1703
 
1704
1704
  const anchor = selection.anchor;
@@ -1841,7 +1841,7 @@ function syncLexicalSelectionToYjs(binding, provider, prevSelection, nextSelecti
1841
1841
  }
1842
1842
  }
1843
1843
 
1844
- if (nextSelection !== null) {
1844
+ if (lexical.$isRangeSelection(nextSelection)) {
1845
1845
  anchorPos = createRelativePosition(nextSelection.anchor, binding);
1846
1846
  focusPos = createRelativePosition(nextSelection.focus, binding);
1847
1847
  }
@@ -2325,14 +2325,14 @@ function syncYjsChangesToLexical(binding, provider, events) {
2325
2325
 
2326
2326
  const selection = lexical.$getSelection();
2327
2327
 
2328
- if (selection !== null) {
2328
+ if (lexical.$isRangeSelection(selection)) {
2329
2329
  // We can't use Yjs's cursor position here, as it doesn't always
2330
2330
  // handle selection recovery correctly – especially on elements that
2331
2331
  // get moved around or split. So instead, we roll our own solution.
2332
2332
  if (doesSelectionNeedRecovering(selection)) {
2333
2333
  const prevSelection = currentEditorState._selection;
2334
2334
 
2335
- if (prevSelection !== null) {
2335
+ if (lexical.$isRangeSelection(prevSelection)) {
2336
2336
  const prevOffsetView = $createOffsetView(editor, 0, currentEditorState);
2337
2337
  const nextOffsetView = $createOffsetView(editor, 0, pendingEditorState);
2338
2338
  const [start, end] = prevOffsetView.getOffsetsFromSelection(prevSelection);
@@ -7,8 +7,8 @@
7
7
  var m=require("yjs"),v=require("lexical");function z(a){throw Error(`Minified Lexical error #${a}; see codes.json for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}
8
8
  class B{constructor(a,b){this._key="";this._map=a;this._parent=b;this._type="linebreak"}getNode(){const a=v.$getNodeByKey(this._key);return v.$isLineBreakNode(a)?a:null}getKey(){return this._key}getSharedType(){return this._map}getType(){return this._type}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}destroy(a){a.collabNodeMap.delete(this._key)}}function C(a,b){b=new B(a,b);return a._collabNode=b}
9
9
  class D{constructor(a,b,c,d){this._key="";this._map=a;this._parent=c;this._text=b;this._type=d;this._normalized=!1}getPrevNode(a){if(null===a)return null;a=a.get(this._key);return v.$isTextNode(a)?a:null}getNode(){const a=v.$getNodeByKey(this._key);return v.$isTextNode(a)?a: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(a,b,c){const d=
10
- this._parent._xmlText;a=this.getOffset()+1+a;0!==b&&d.delete(a,b);""!==c&&d.insert(a,c)}syncPropertiesAndTextFromLexical(a,b,c){var d=this.getPrevNode(c);c=b.__text;E(a,this._map,d,b);if(null!==d&&(a=d.__text,a!==c)){d=b.__key;b=a;var e=v.$getSelection();a=c.length;null!==e&&e.isCollapsed()&&(e=e.anchor,e.key===d&&(a=e.offset));d=b.length;const f=c.length;let g=e=0;for(;e<d&&e<f&&b[e]===c[e]&&e<a;)e++;for(;g+e<d&&g+e<f&&b[d-g-1]===c[f-g-1];)g++;for(;g+e<d&&g+e<f&&b[e]===c[e];)e++;b=e;a=c.slice(e,
11
- f-g);d=d-e-g;this.spliceText(b,d,a);this._text=c}}syncPropertiesAndTextFromYjs(a,b){const c=this.getNode();if(null===c)throw Error("Should never happen");F(a,this._map,c,b);a=this._text;c.__text!==a&&(c.getWritable().__text=a)}destroy(a){a.collabNodeMap.delete(this._key)}}function G(a,b,c,d){b=new D(a,b,c,d);return a._collabNode=b}const I=new Set("__key __children __parent __cachedText __text __state".split(" "));
10
+ this._parent._xmlText;a=this.getOffset()+1+a;0!==b&&d.delete(a,b);""!==c&&d.insert(a,c)}syncPropertiesAndTextFromLexical(a,b,c){var d=this.getPrevNode(c);c=b.__text;E(a,this._map,d,b);if(null!==d&&(a=d.__text,a!==c)){d=b.__key;b=a;var e=v.$getSelection();a=c.length;v.$isRangeSelection(e)&&e.isCollapsed()&&(e=e.anchor,e.key===d&&(a=e.offset));d=b.length;const f=c.length;let g=e=0;for(;e<d&&e<f&&b[e]===c[e]&&e<a;)e++;for(;g+e<d&&g+e<f&&b[d-g-1]===c[f-g-1];)g++;for(;g+e<d&&g+e<f&&b[e]===c[e];)e++;b=
11
+ e;a=c.slice(e,f-g);d=d-e-g;this.spliceText(b,d,a);this._text=c}}syncPropertiesAndTextFromYjs(a,b){const c=this.getNode();if(null===c)throw Error("Should never happen");F(a,this._map,c,b);a=this._text;c.__text!==a&&(c.getWritable().__text=a)}destroy(a){a.collabNodeMap.delete(this._key)}}function G(a,b,c,d){b=new D(a,b,c,d);return a._collabNode=b}const I=new Set("__key __children __parent __cachedText __text __state".split(" "));
12
12
  function J(a){a=v.$getNodeByKey(a);if(null===a)throw Error("Should never happen");return a}
13
13
  function aa(a,b,c){const d=b.__type;if(v.$isElementNode(b)){var e=new m.XmlText;c=K(e,c,d);c.syncPropertiesFromLexical(a,b,null);c.syncChildrenFromLexical(a,b,null,null,null)}else if(v.$isTextNode(b))e=new m.Map,c=G(e,b.__text,c,d),c.syncPropertiesAndTextFromLexical(a,b,null);else if(v.$isLineBreakNode(b))a=new m.Map,a.set("__type","linebreak"),c=C(a,c);else if(v.$isDecoratorNode(b))e=new m.XmlElement,c=ba(e,c,d),c.syncPropertiesFromLexical(a,b,null);else throw Error("Should never happen");c._key=
14
14
  b.__key;return c}
@@ -42,14 +42,14 @@ if(-1===g)throw Error("Should never happen");var h=this._xmlText;0!==c&&h.delete
42
42
  if(e===a)return b;b+=e.getSize()}return-1}destroy(a){const b=a.collabNodeMap,c=this._children;for(let d=0;d<c.length;d++)c[d].destroy(a);b.delete(this._key)}}function ka(a,b,c){return void 0===b?(a=a.getWritable(),a.__children=c,a):b}function K(a,b,c){b=new M(a,b,c);return a._collabNode=b}
43
43
  function la(a,b){b=b.collabNodeMap.get(a.key);if(void 0===b)return null;a=a.offset;let c=b.getSharedType();if(b instanceof D){c=b._parent._xmlText;b=b.getOffset();if(-1===b)return null;a=b+1+a}return m.createRelativePositionFromTypeIndex(c,a)}function ma(a,b){if(null==a){if(null!=b)return!0}else if(null==b||!m.compareRelativePositions(a,b))return!0;return!1}function na(a,b){a=a.cursorsContainer;if(null!==a){b=b.selections;const c=b.length;for(let d=0;d<c;d++)a.removeChild(b[d])}}
44
44
  function oa(a){for(;null!=a;){if(3===a.nodeType)return a;a=a.firstChild}return null}function pa(a){const b=a.parentNode;if(null==b)throw Error("Should never happen");return[b,Array.from(b.childNodes).indexOf(a)]}
45
- function qa(a,b){var c=b.awareness.getLocalState();if(null!==c&&(b=c.anchorPos,c=c.focusPos,null!==b&&null!==c&&(b=m.createAbsolutePositionFromRelativePosition(b,a.doc),a=m.createAbsolutePositionFromRelativePosition(c,a.doc),null!==b&&null!==a))){const [e,f]=Y(b.type,b.index),[g,h]=Y(a.type,a.index);if(null!==e&&null!==g){c=e.getKey();a=g.getKey();b=v.$getSelection();if(null===b)throw Error("TODO: syncLocalCursorPosition");var d=b.anchor;const n=b.focus;if(d.key!==c||d.offset!==f)d=v.$getNodeByKey(c),
46
- b.anchor.set(c,f,v.$isElementNode(d)?"element":"text");if(n.key!==a||n.offset!==h)c=v.$getNodeByKey(a),b.focus.set(a,h,v.$isElementNode(c)?"element":"text")}}}function Y(a,b){a=a._collabNode;if(void 0===a)return[null,0];if(a instanceof M){const {node:c,offset:d}=N(a,b,!0);return null===c?[a,0]:[c,d]}return[null,0]}
45
+ function qa(a,b){var c=b.awareness.getLocalState();if(null!==c&&(b=c.anchorPos,c=c.focusPos,null!==b&&null!==c&&(b=m.createAbsolutePositionFromRelativePosition(b,a.doc),a=m.createAbsolutePositionFromRelativePosition(c,a.doc),null!==b&&null!==a))){const [e,f]=Y(b.type,b.index),[g,h]=Y(a.type,a.index);if(null!==e&&null!==g){const n=e.getKey();a=g.getKey();b=v.$getSelection();if(v.$isRangeSelection(b)){var d=b.anchor;c=b.focus;if(d.key!==n||d.offset!==f)d=v.$getNodeByKey(n),b.anchor.set(n,f,v.$isElementNode(d)?
46
+ "element":"text");if(c.key!==a||c.offset!==h)c=v.$getNodeByKey(a),b.focus.set(a,h,v.$isElementNode(c)?"element":"text")}}}}function Y(a,b){a=a._collabNode;if(void 0===a)return[null,0];if(a instanceof M){const {node:c,offset:d}=N(a,b,!0);return null===c?[a,0]:[c,d]}return[null,0]}
47
47
  function va(a,b){var c=Array.from(b.awareness.getStates()),d=a.clientID;b=a.cursors;var e=a.editor._editorState._nodeMap;const f=new Set;for(var g=0;g<c.length;g++){const [O,Aa]=c[g];if(O!==d){f.add(O);const {anchorPos:ra,focusPos:sa,name:Ba,color:Ca,focusing:Da}=Aa;var h=null,n=b.get(O);void 0===n&&(n={color:Ca,name:Ba,selection:null},b.set(O,n));if(null!==ra&&null!==sa&&Da){var l=m.createAbsolutePositionFromRelativePosition(ra,a.doc),k=m.createAbsolutePositionFromRelativePosition(sa,a.doc);if(null!==
48
48
  l&&null!==k){const [H,W]=Y(l.type,l.index),[ta,ua]=Y(k.type,k.index);if(null!==H&&null!==ta){k=H.getKey();var r=ta.getKey();h=n.selection;if(null===h){l=n;h=k;k=W;var t=ua,q=l.color,p=document.createElement("span");p.style.cssText=`position:absolute;top:0;bottom:0;right:-1px;width:1px;background-color:rgb(${q});z-index:10;`;var u=document.createElement("span");u.textContent=l.name;u.style.cssText=`position:absolute;left:-2px;top:-16px;background-color:rgb(${q});color:#fff;line-height:12px;height:12px;font-size:12px;padding:2px;font-family:Arial;font-weight:bold`;
49
49
  p.appendChild(u);h={anchor:{key:h,offset:k},caret:p,color:q,focus:{key:r,offset:t},name:u,range:document.createRange(),selections:[]}}else l=h.anchor,t=h.focus,l.key=k,l.offset=W,t.key=r,t.offset=ua}}}a:{l=a;k=n;p=h;var w=e;q=l.editor;r=q.getRootElement();n=l.cursorsContainer;if(null===n||null===r)break a;h=k.selection;if(null===p){null!==h&&(k.selection=null,na(l,h));break a}else k.selection=p;t=p.range;h=p.caret;k=p.color;l=p.selections;var y=p.anchor,x=p.focus;p=y.key;u=x.key;var A=w.get(p);const H=
50
50
  w.get(u);w=q.getElementByKey(p);q=q.getElementByKey(u);y=y.offset;x=x.offset;v.$isTextNode(A)&&(w=oa(w));v.$isTextNode(H)&&(q=oa(q));if(void 0!==A&&void 0!==H&&null!==w&&null!==q){"BR"===w.nodeName&&([w,y]=pa(w));"BR"===q.nodeName&&([q,x]=pa(q));A=w.firstChild;w===q&&null!=A&&"BR"===A.nodeName&&0===y&&0===x&&(x=1);try{t.setStart(w,y),t.setEnd(q,x)}catch(W){break a}!t.collapsed||y===x&&p===u||(t.setStart(q,x),t.setEnd(w,y));q=r.getBoundingClientRect();r=getComputedStyle(r);r=parseFloat(r.paddingLeft)+
51
51
  parseFloat(r.paddingRight);p=Array.from(t.getClientRects());t=p.length;u=l.length;for(A=0;A<t;A++)w=p[A],w.width+r===q.width?(p.splice(A--,1),t--):(y=l[A],void 0===y&&(y=document.createElement("span"),l[A]=y,n.appendChild(y)),y.style.cssText=`position:absolute;top:${w.top}px;left:${w.left}px;height:${w.height}px;width:${w.width}px;background-color:rgba(${k}, 0.3);pointer-events:none;z-index:10;`,A===t-1&&h.parentNode!==y&&y.appendChild(h));for(h=u-1;h>=t;h--)n.removeChild(l[h]),l.pop()}}}}c=Array.from(b.keys());
52
- for(d=0;d<c.length;d++)e=c[d],f.has(e)||(g=b.get(e),void 0!==g&&(g=g.selection,null!==g&&na(a,g),b.delete(e)))}function wa(a,b,c,d){b=b.awareness;var e=b.getLocalState();if(null!==e){var {anchorPos:f,focusPos:g,name:h,color:n,focusing:l}=e,k=e=null;if(null!==d&&(null===f||d.is(c))||null!==c)null!==d&&(e=la(d.anchor,a),k=la(d.focus,a)),(ma(f,e)||ma(g,k))&&b.setLocalState({anchorPos:e,color:n,focusPos:k,focusing:l,name:h})}}
52
+ for(d=0;d<c.length;d++)e=c[d],f.has(e)||(g=b.get(e),void 0!==g&&(g=g.selection,null!==g&&na(a,g),b.delete(e)))}function wa(a,b,c,d){b=b.awareness;var e=b.getLocalState();if(null!==e){var {anchorPos:f,focusPos:g,name:h,color:n,focusing:l}=e,k=e=null;if(null!==d&&(null===f||d.is(c))||null!==c)v.$isRangeSelection(d)&&(e=la(d.anchor,a),k=la(d.focus,a)),(ma(f,e)||ma(g,k))&&b.setLocalState({anchorPos:e,color:n,focusPos:k,focusing:l,name:h})}}
53
53
  class xa{constructor(a,b,c=1){this._offsetMap=a;this._firstNode=b;this._blockOffsetSize=c}createSelectionFromOffsets(a,b,c){var d=this._firstNode;if(null===d)return null;var e=Z(d,a,this._blockOffsetSize);let f=Z(d,b,this._blockOffsetSize);void 0!==c&&(a=ya(a,e,c,this,this._blockOffsetSize),e=Z(d,a,this._blockOffsetSize),b=ya(b,f,c,this,this._blockOffsetSize),f=Z(d,b,this._blockOffsetSize));if(null===e||null===f)return null;c=e.key;d=f.key;const g=v.$getNodeByKey(c),h=v.$getNodeByKey(d);if(null===
54
54
  g||null===h)return null;let n=0,l=0,k="element",r="element";"text"===e.type?(n=a-e.start,k="text",e=g.getNextSibling(),a!==b&&n===g.getTextContentSize()&&v.$isTextNode(e)&&(n=0,c=e.__key)):"inline"===e.type&&(c=g.getParentOrThrow().getKey(),n=b>e.start?e.end:e.start);"text"===f.type?(l=b-f.start,r="text"):"inline"===f.type&&(d=h.getParentOrThrow().getKey(),l=b>f.start?f.end:f.start);a=v.$createRangeSelection();if(null===a)return null;a.anchor.set(c,n,k);a.focus.set(d,l,r);return a}getOffsetsFromSelection(a){var b=
55
55
  a.anchor,c=a.focus,d=this._offsetMap;const e=b.offset;var f=c.offset;let g=a=-1;if("text"===b.type)b=d.get(b.key),void 0!==b&&(a=b.start+e);else{b=b.getNode().getDescendantByIndex(e);const h=d.get(b.getKey());void 0!==h&&(a=b.getIndexWithinParent()!==e?h.end:h.start)}"text"===c.type?(f=d.get(c.key),void 0!==f&&(g=f.start+c.offset)):(c=c.getNode().getDescendantByIndex(f),d=d.get(c.getKey()),void 0!==d&&(g=c.getIndexWithinParent()!==f?d.end:d.start));return[a,g]}}
@@ -63,5 +63,5 @@ exports.createBinding=function(a,b,c,d){b=d.get(c);if(void 0===b)throw Error("Sh
63
63
  exports.initLocalState=function(a,b,c,d){a.awareness.setLocalState({anchorPos:null,color:c,focusPos:null,focusing:d,name:b})};exports.setLocalStateFocus=function(a,b){({awareness:a}=a);a.setLocalState({...a.getLocalState(),focusing:b})};exports.syncCursorPositions=va;
64
64
  exports.syncLexicalUpdateToYjs=function(a,b,c,d,e,f,g,h){P(a,()=>{d.read(()=>{if(h.has("collaboration")){if(0<g.size){var n=Array.from(g),l=a.collabNodeMap,k=[];for(let p=0;p<n.length;p++){var r=n[p],t=v.$getNodeByKey(r),q=l.get(r);if(q instanceof D)if(v.$isTextNode(t))k.push([q,t.__text]);else{t=q.getOffset();if(-1===t)continue;const u=q._parent;q._normalized=!0;u._xmlText.delete(t,1);l.delete(r);r=u._children;q=r.indexOf(q);r.splice(q,1)}}for(n=0;n<k.length;n++){const [p,u]=k[n];p._text=u}}}else e.has("root")&&
65
65
  (k=c._nodeMap,n=v.$getRoot(),l=a.root,l.syncPropertiesFromLexical(a,n,k),l.syncChildrenFromLexical(a,n,k,e,f)),k=v.$getSelection(),wa(a,b,c._selection,k)})})};
66
- exports.syncYjsChangesToLexical=function(a,b,c){const d=a.editor,e=d._editorState;d.update(()=>{var f=d._pendingEditorState;for(var g=0;g<c.length;g++)Fa(a,c[g]);var h=v.$getSelection();if(null!==h)if(ca(h)){g=e._selection;if(null!==g){const n=Ea(d,0,e);f=Ea(d,0,f);const [l,k]=n.getOffsetsFromSelection(g);f=f.createSelectionFromOffsets(l,k,n);null!==f?v.$setSelection(f):(qa(a,b),ca(h)&&(h=v.$getRoot(),0===h.getChildrenSize()&&h.append(v.$createParagraphNode()),v.$getRoot().selectEnd()))}wa(a,b,g,
67
- v.$getSelection())}else qa(a,b)},{onUpdate:()=>{va(a,b)},skipTransforms:!0,tag:"collaboration"})};
66
+ exports.syncYjsChangesToLexical=function(a,b,c){const d=a.editor,e=d._editorState;d.update(()=>{var f=d._pendingEditorState;for(var g=0;g<c.length;g++)Fa(a,c[g]);var h=v.$getSelection();if(v.$isRangeSelection(h))if(ca(h)){g=e._selection;if(v.$isRangeSelection(g)){const n=Ea(d,0,e);f=Ea(d,0,f);const [l,k]=n.getOffsetsFromSelection(g);f=f.createSelectionFromOffsets(l,k,n);null!==f?v.$setSelection(f):(qa(a,b),ca(h)&&(h=v.$getRoot(),0===h.getChildrenSize()&&h.append(v.$createParagraphNode()),v.$getRoot().selectEnd()))}wa(a,
67
+ b,g,v.$getSelection())}else qa(a,b)},{onUpdate:()=>{va(a,b)},skipTransforms:!0,tag:"collaboration"})};
package/package.json CHANGED
@@ -15,10 +15,10 @@
15
15
  "crdt"
16
16
  ],
17
17
  "license": "MIT",
18
- "version": "0.1.9",
18
+ "version": "0.1.10",
19
19
  "main": "LexicalYjs.js",
20
20
  "peerDependencies": {
21
- "lexical": "0.1.9",
21
+ "lexical": "0.1.10",
22
22
  "yjs": ">=13.5.22"
23
23
  },
24
24
  "repository": {