@lexical/yjs 0.14.2 → 0.14.4
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 +12 -9
- package/{LexicalYjs.dev.esm.js → LexicalYjs.dev.mjs} +13 -10
- package/{LexicalYjs.esm.js → LexicalYjs.mjs} +2 -2
- package/LexicalYjs.node.mjs +16 -0
- package/LexicalYjs.prod.js +2 -2
- package/{LexicalYjs.prod.esm.js → LexicalYjs.prod.mjs} +1 -1
- package/index.d.ts +0 -1
- package/package.json +23 -5
package/LexicalYjs.dev.js
CHANGED
|
@@ -1451,6 +1451,11 @@ function syncYjsChangesToLexical(binding, provider, events, isFromUndoManger) {
|
|
|
1451
1451
|
const event = events[i];
|
|
1452
1452
|
syncEvent(binding, event);
|
|
1453
1453
|
}
|
|
1454
|
+
// If there was a collision on the top level paragraph
|
|
1455
|
+
// we need to re-add a paragraph
|
|
1456
|
+
if (lexical.$getRoot().getChildrenSize() === 0) {
|
|
1457
|
+
lexical.$getRoot().append(lexical.$createParagraphNode());
|
|
1458
|
+
}
|
|
1454
1459
|
const selection = lexical.$getSelection();
|
|
1455
1460
|
if (lexical.$isRangeSelection(selection)) {
|
|
1456
1461
|
// We can't use Yjs's cursor position here, as it doesn't always
|
|
@@ -1469,14 +1474,6 @@ function syncYjsChangesToLexical(binding, provider, events, isFromUndoManger) {
|
|
|
1469
1474
|
// Fallback is to use the Yjs cursor position
|
|
1470
1475
|
syncLocalCursorPosition(binding, provider);
|
|
1471
1476
|
if (doesSelectionNeedRecovering(selection)) {
|
|
1472
|
-
const root = lexical.$getRoot();
|
|
1473
|
-
|
|
1474
|
-
// If there was a collision on the top level paragraph
|
|
1475
|
-
// we need to re-add a paragraph
|
|
1476
|
-
if (root.getChildrenSize() === 0) {
|
|
1477
|
-
root.append(lexical.$createParagraphNode());
|
|
1478
|
-
}
|
|
1479
|
-
|
|
1480
1477
|
// Fallback
|
|
1481
1478
|
lexical.$getRoot().selectEnd();
|
|
1482
1479
|
}
|
|
@@ -1561,7 +1558,13 @@ function syncLexicalUpdateToYjs(binding, provider, prevEditorState, currEditorSt
|
|
|
1561
1558
|
});
|
|
1562
1559
|
}
|
|
1563
1560
|
|
|
1564
|
-
/**
|
|
1561
|
+
/**
|
|
1562
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1563
|
+
*
|
|
1564
|
+
* This source code is licensed under the MIT license found in the
|
|
1565
|
+
* LICENSE file in the root directory of this source tree.
|
|
1566
|
+
*
|
|
1567
|
+
*/
|
|
1565
1568
|
const CONNECTED_COMMAND = lexical.createCommand('CONNECTED_COMMAND');
|
|
1566
1569
|
const TOGGLE_CONNECT_COMMAND = lexical.createCommand('TOGGLE_CONNECT_COMMAND');
|
|
1567
1570
|
function createUndoManager(binding, root) {
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
import { $getNodeByKey, $isLineBreakNode, $isTextNode, $getSelection, $isRangeSelection, createEditor, $isElementNode, $isRootNode, $isDecoratorNode, $
|
|
7
|
+
import { $getNodeByKey, $isLineBreakNode, $isTextNode, $getSelection, $isRangeSelection, createEditor, $isElementNode, $isRootNode, $isDecoratorNode, $getRoot, $createParagraphNode, $setSelection, createCommand } from 'lexical';
|
|
8
8
|
import { XmlText, Map as Map$1, XmlElement, Doc, createAbsolutePositionFromRelativePosition, createRelativePositionFromTypeIndex, compareRelativePositions, YTextEvent, YMapEvent, YXmlEvent, UndoManager } from 'yjs';
|
|
9
9
|
import { createDOMRange, createRectsFromDOMRange } from '@lexical/selection';
|
|
10
10
|
import { $createOffsetView } from '@lexical/offset';
|
|
@@ -1449,6 +1449,11 @@ function syncYjsChangesToLexical(binding, provider, events, isFromUndoManger) {
|
|
|
1449
1449
|
const event = events[i];
|
|
1450
1450
|
syncEvent(binding, event);
|
|
1451
1451
|
}
|
|
1452
|
+
// If there was a collision on the top level paragraph
|
|
1453
|
+
// we need to re-add a paragraph
|
|
1454
|
+
if ($getRoot().getChildrenSize() === 0) {
|
|
1455
|
+
$getRoot().append($createParagraphNode());
|
|
1456
|
+
}
|
|
1452
1457
|
const selection = $getSelection();
|
|
1453
1458
|
if ($isRangeSelection(selection)) {
|
|
1454
1459
|
// We can't use Yjs's cursor position here, as it doesn't always
|
|
@@ -1467,14 +1472,6 @@ function syncYjsChangesToLexical(binding, provider, events, isFromUndoManger) {
|
|
|
1467
1472
|
// Fallback is to use the Yjs cursor position
|
|
1468
1473
|
syncLocalCursorPosition(binding, provider);
|
|
1469
1474
|
if (doesSelectionNeedRecovering(selection)) {
|
|
1470
|
-
const root = $getRoot();
|
|
1471
|
-
|
|
1472
|
-
// If there was a collision on the top level paragraph
|
|
1473
|
-
// we need to re-add a paragraph
|
|
1474
|
-
if (root.getChildrenSize() === 0) {
|
|
1475
|
-
root.append($createParagraphNode());
|
|
1476
|
-
}
|
|
1477
|
-
|
|
1478
1475
|
// Fallback
|
|
1479
1476
|
$getRoot().selectEnd();
|
|
1480
1477
|
}
|
|
@@ -1559,7 +1556,13 @@ function syncLexicalUpdateToYjs(binding, provider, prevEditorState, currEditorSt
|
|
|
1559
1556
|
});
|
|
1560
1557
|
}
|
|
1561
1558
|
|
|
1562
|
-
/**
|
|
1559
|
+
/**
|
|
1560
|
+
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
1561
|
+
*
|
|
1562
|
+
* This source code is licensed under the MIT license found in the
|
|
1563
|
+
* LICENSE file in the root directory of this source tree.
|
|
1564
|
+
*
|
|
1565
|
+
*/
|
|
1563
1566
|
const CONNECTED_COMMAND = createCommand('CONNECTED_COMMAND');
|
|
1564
1567
|
const TOGGLE_CONNECT_COMMAND = createCommand('TOGGLE_CONNECT_COMMAND');
|
|
1565
1568
|
function createUndoManager(binding, root) {
|
|
@@ -4,8 +4,8 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
import * as modDev from './LexicalYjs.dev.
|
|
8
|
-
import * as modProd from './LexicalYjs.prod.
|
|
7
|
+
import * as modDev from './LexicalYjs.dev.mjs';
|
|
8
|
+
import * as modProd from './LexicalYjs.prod.mjs';
|
|
9
9
|
const mod = process.env.NODE_ENV === 'development' ? modDev : modProd;
|
|
10
10
|
export const CONNECTED_COMMAND = mod.CONNECTED_COMMAND;
|
|
11
11
|
export const TOGGLE_CONNECT_COMMAND = mod.TOGGLE_CONNECT_COMMAND;
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
const mod = await (process.env.NODE_ENV === 'development' ? import('./LexicalYjs.dev.mjs') : import('./LexicalYjs.prod.mjs'));
|
|
8
|
+
export const CONNECTED_COMMAND = mod.CONNECTED_COMMAND;
|
|
9
|
+
export const TOGGLE_CONNECT_COMMAND = mod.TOGGLE_CONNECT_COMMAND;
|
|
10
|
+
export const createBinding = mod.createBinding;
|
|
11
|
+
export const createUndoManager = mod.createUndoManager;
|
|
12
|
+
export const initLocalState = mod.initLocalState;
|
|
13
|
+
export const setLocalStateFocus = mod.setLocalStateFocus;
|
|
14
|
+
export const syncCursorPositions = mod.syncCursorPositions;
|
|
15
|
+
export const syncLexicalUpdateToYjs = mod.syncLexicalUpdateToYjs;
|
|
16
|
+
export const syncYjsChangesToLexical = mod.syncYjsChangesToLexical;
|
package/LexicalYjs.prod.js
CHANGED
|
@@ -46,5 +46,5 @@ exports.initLocalState=function(a,b,c,d,e){a.awareness.setLocalState({anchorPos:
|
|
|
46
46
|
exports.syncLexicalUpdateToYjs=function(a,b,c,d,e,f,h,g){la(a,()=>{d.read(()=>{if(g.has("collaboration")||g.has("historic")){if(0<h.size){var m=Array.from(h),p=a.collabNodeMap,n=[];for(let u=0;u<m.length;u++){var k=m[u],l=r.$getNodeByKey(k),q=p.get(k);if(q instanceof F)if(r.$isTextNode(l))n.push([q,l.__text]);else{l=q.getOffset();if(-1===l)continue;let t=q._parent;q._normalized=!0;t._xmlText.delete(l,1);p.delete(k);k=t._children;q=k.indexOf(q);k.splice(q,1)}}for(m=0;m<n.length;m++){let [u,t]=n[m];
|
|
47
47
|
u instanceof F&&"string"===typeof t&&(u._text=t)}}}else e.has("root")&&(n=c._nodeMap,m=r.$getRoot(),p=a.root,p.syncPropertiesFromLexical(a,m,n),p.syncChildrenFromLexical(a,m,n,e,f)),n=r.$getSelection(),oa(a,b,c._selection,n)})})};
|
|
48
48
|
exports.syncYjsChangesToLexical=function(a,b,c,d){let e=a.editor,f=e._editorState;c.forEach(h=>h.delta);e.update(()=>{var h=e._pendingEditorState;for(var g=0;g<c.length;g++){var m=a,p=c[g],{target:n}=p;n=O(m,n);if(n instanceof P&&p instanceof v.YTextEvent){let {keysChanged:k,childListChanged:l,delta:q}=p;0<k.size&&n.syncPropertiesFromYjs(m,k);l&&(n.applyChildrenYjsDelta(m,q),n.syncChildrenFromYjs(m))}else n instanceof F&&p instanceof v.YMapEvent?({keysChanged:p}=p,0<p.size&&n.syncPropertiesAndTextFromYjs(m,
|
|
49
|
-
p)):n instanceof U&&p instanceof v.YXmlEvent?({attributesChanged:p}=p,0<p.size&&n.syncPropertiesFromYjs(m,p)):A(82)}
|
|
50
|
-
b,
|
|
49
|
+
p)):n instanceof U&&p instanceof v.YXmlEvent?({attributesChanged:p}=p,0<p.size&&n.syncPropertiesFromYjs(m,p)):A(82)}0===r.$getRoot().getChildrenSize()&&r.$getRoot().append(r.$createParagraphNode());m=r.$getSelection();if(r.$isRangeSelection(m))if(R(m)){g=f._selection;if(r.$isRangeSelection(g)){n=z.$createOffsetView(e,0,f);h=z.$createOffsetView(e,0,h);let [k,l]=n.getOffsetsFromSelection(g);h=0<=k&&0<=l?h.createSelectionFromOffsets(k,l,n):null;null!==h?r.$setSelection(h):(Y(a,b),R(m)&&r.$getRoot().selectEnd())}oa(a,
|
|
50
|
+
b,g,r.$getSelection())}else Y(a,b)},{onUpdate:()=>{na(a,b)},skipTransforms:!0,tag:d?"historic":"collaboration"})}
|
|
@@ -4,4 +4,4 @@
|
|
|
4
4
|
* This source code is licensed under the MIT license found in the
|
|
5
5
|
* LICENSE file in the root directory of this source tree.
|
|
6
6
|
*/
|
|
7
|
-
import{$getNodeByKey as e,$isLineBreakNode as t,$isTextNode as n,$getSelection as o,$isRangeSelection as s,createEditor as l,$isElementNode as i,$isRootNode as r,$isDecoratorNode as c,$setSelection as f,$getRoot as a,$createParagraphNode as u,createCommand as d}from"lexical";import{XmlText as _,Map as h,XmlElement as p,Doc as g,createAbsolutePositionFromRelativePosition as y,createRelativePositionFromTypeIndex as x,compareRelativePositions as m,YTextEvent as k,YMapEvent as b,YXmlEvent as v,UndoManager as P}from"yjs";import{createDOMRange as C,createRectsFromDOMRange as w}from"@lexical/selection";import{$createOffsetView as N}from"@lexical/offset";var T=function(e){const t=new URLSearchParams;t.append("code",e);for(let e=1;e<arguments.length;e++)t.append("v",arguments[e]);throw Error(`Minified Lexical error #${e}; visit https://lexical.dev/docs/error?${t} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)};class S{constructor(e,t){this._key="",this._map=e,this._parent=t,this._type="linebreak"}getNode(){const n=e(this._key);return t(n)?n:null}getKey(){return this._key}getSharedType(){return this._map}getType(){return this._type}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}destroy(e){e.collabNodeMap.delete(this._key)}}function F(e,t){const n=new S(e,t);return e._collabNode=n,n}class E{constructor(e,t,n,o){this._key="",this._map=e,this._parent=n,this._text=t,this._type=o,this._normalized=!1}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return n(t)?t:null}getNode(){const t=e(this._key);return n(t)?t:null}getSharedType(){return this._map}getType(){return this._type}getKey(){return this._key}getSize(){return this._text.length+(this._normalized?0:1)}getOffset(){return this._parent.getChildOffset(this)}spliceText(e,t,n){const o=this._parent._xmlText,s=this.getOffset()+1+e;0!==t&&o.delete(s,t),""!==n&&o.insert(s,n)}syncPropertiesAndTextFromLexical(e,t,n){const l=this.getPrevNode(n),i=t.__text;if($(e,this._map,l,t),null!==l){const e=l.__text;if(e!==i){!function(e,t,n,l){const i=o();let r=l.length;if(s(i)&&i.isCollapsed()){const e=i.anchor;e.key===t&&(r=e.offset)}const c=function(e,t,n){const o=e.length,s=t.length;let l=0,i=0;for(;l<o&&l<s&&e[l]===t[l]&&l<n;)l++;for(;i+l<o&&i+l<s&&e[o-i-1]===t[s-i-1];)i++;for(;i+l<o&&i+l<s&&e[l]===t[l];)l++;return{index:l,insert:t.slice(l,s-i),remove:o-l-i}}(n,l,r);e.spliceText(c.index,c.remove,c.insert)}(this,t.__key,e,i),this._text=i}}}syncPropertiesAndTextFromYjs(e,t){const n=this.getNode();null===n&&T(84),K(e,this._map,n,t);const o=this._text;if(n.__text!==o){n.getWritable().__text=o}}destroy(e){e.collabNodeMap.delete(this._key)}}function M(e,t,n,o){const s=new E(e,t,n,o);return e._collabNode=s,s}const z=new Set(["__key","__parent","__next","__prev"]),L=new Set(["__first","__last","__size"]),O=new Set(["__cachedText"]),j=new Set(["__text"]);function A(e,t,o){if(z.has(e))return!0;if(n(t)){if(j.has(e))return!0}else if(i(t)&&(L.has(e)||r(t)&&O.has(e)))return!0;const s=t.constructor,l=o.excludedProperties.get(s);return null!=l&&l.has(e)}function Y(t){const n=e(t);return null===n&&T(85),n}function D(e,o,s){const l=o.__type;let r;if(i(o)){r=V(new _,s,l),r.syncPropertiesFromLexical(e,o,null),r.syncChildrenFromLexical(e,o,null,null,null)}else if(n(o)){r=M(new h,o.__text,s,l),r.syncPropertiesAndTextFromLexical(e,o,null)}else if(t(o)){const e=new h;e.set("__type","linebreak"),r=F(e,s)}else if(c(o)){r=J(new p,s,l),r.syncPropertiesFromLexical(e,o,null)}else T(86);return r._key=o.__key,r}function W(e,t,n){const o=t._collabNode;if(void 0===o){const o=e.editor._nodes,s=function(e){const t=e instanceof h?e.get("__type"):e.getAttribute("__type");return null==t&&T(87),t}(t);void 0===o.get(s)&&T(88,s);const l=t.parent,i=void 0===n&&null!==l?W(e,l):n||null;if(i instanceof Q||T(89),t instanceof _)return V(t,i,s);if(t instanceof h)return"linebreak"===s?F(t,i):M(t,"",i,s);if(t instanceof p)return J(t,i,s)}return o}function I(e,t,n){const o=t.getType(),s=e.editor._nodes.get(o);void 0===s&&T(88,o);const l=new s.klass;if(l.__parent=n,t._key=l.__key,t instanceof Q){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof E?t.syncPropertiesAndTextFromYjs(e,null):t instanceof H&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(l.__key,t),l}function K(e,t,n,o){const s=null===o?t instanceof h?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(A(r,n,e))continue;const c=n[r];let f=t instanceof h?t.get(r):t.getAttribute(r);if(c!==f){if(f instanceof g){const t=e.docMap;c instanceof g&&t.delete(c.guid);const n=l(),o=f.guid;n._key=o,t.set(o,f),f=n}void 0===i&&(i=n.getWritable()),i[r]=f}}}function $(e,t,n,o){const s=o.__type,l=e.nodeProperties;let i=l.get(s);void 0===i&&(i=Object.keys(o).filter((t=>!A(t,o,e))),l.set(s,i));const r=e.editor.constructor;for(let s=0;s<i.length;s++){const l=i[s],c=null===n?void 0:n[l];let f=o[l];if(c!==f){if(f instanceof r){const t=e.docMap;let n;if(c instanceof r){const e=c._key;n=t.get(e),t.delete(e)}const s=n||new g,l=s.guid;f._key=l,t.set(l,s),f=s,e.editor.update((()=>{o.markDirty()}))}t instanceof h?t.set(l,f):t.setAttribute(l,f)}}}function R(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function B(e,t,n){let o=0,s=0;const l=e._children,i=l.length;for(;s<i;s++){const e=l[s],r=o;o+=e.getSize();if((n?o>=t:o>t)&&e instanceof E){let n=t-r-1;n<0&&(n=0);return{length:o-t,node:e,nodeIndex:s,offset:n}}if(o>t)return{length:0,node:e,nodeIndex:s,offset:r};if(s===i-1)return{length:0,node:null,nodeIndex:s+1,offset:r+1}}return{length:0,node:null,nodeIndex:0,offset:0}}function U(e){const t=e.anchor,o=e.focus;let s=!1;try{const e=t.getNode(),l=o.getNode();(!e.isAttached()||!l.isAttached()||n(e)&&t.offset>e.getTextContentSize()||n(l)&&o.offset>l.getTextContentSize())&&(s=!0)}catch(e){s=!0}return s}function G(t,n){const o=[];let s=t.__first;for(;null!==s;){const t=null===n?e(s):n.get(s);null==t&&T(101),o.push(s),s=t.__next}return o}function q(e){const t=e.getParent();if(null!==t){const n=e.getWritable(),o=t.getWritable(),s=e.getPreviousSibling(),l=e.getNextSibling();if(null===s)if(null!==l){const e=l.getWritable();o.__first=l.__key,e.__prev=null}else o.__first=null;else{const e=s.getWritable();if(null!==l){const t=l.getWritable();t.__prev=e.__key,e.__next=t.__key}else e.__next=null;n.__prev=null}if(null===l)if(null!==s){const e=s.getWritable();o.__last=s.__key,e.__next=null}else o.__last=null;else{const e=l.getWritable();if(null!==s){const t=s.getWritable();t.__next=e.__key,e.__prev=t.__key}else e.__prev=null;n.__next=null}o.__size--,n.__parent=null}}class H{constructor(e,t,n){this._key="",this._xmlElem=e,this._parent=t,this._type=n}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return c(t)?t:null}getNode(){const t=e(this._key);return c(t)?t:null}getSharedType(){return this._xmlElem}getType(){return this._type}getKey(){return this._key}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}syncPropertiesFromLexical(e,t,n){const o=this.getPrevNode(n);$(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&T(83);K(e,this._xmlElem,n,t)}destroy(e){e.collabNodeMap.delete(this._key)}}function J(e,t,n){const o=new H(e,t,n);return e._collabNode=o,o}class Q{constructor(e,t,n){this._key="",this._children=[],this._xmlText=e,this._type=n,this._parent=t}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return 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&&T(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&T(91),K(e,this._xmlText,n,t)}applyChildrenYjsDelta(e,t){const n=this._children;let o=0;for(let s=0;s<t.length;s++){const l=t[s],i=l.insert,r=l.delete;if(null!=l.retain)o+=l.retain;else if("number"==typeof r){let e=r;for(;e>0;){const{node:t,nodeIndex:s,offset:l,length:i}=B(this,o,!1);if(t instanceof Q||t instanceof S||t instanceof H)n.splice(s,1),e-=1;else{if(!(t instanceof E))break;{const o=Math.min(e,i),r=0!==s?n[s-1]:null,c=t.getSize();0===l&&1===o&&s>0&&r instanceof E&&i===c&&0===Array.from(t._map.keys()).length?(r._text+=t._text,n.splice(s,1)):0===l&&o===c?n.splice(s,1):t._text=R(t._text,l,o,""),e-=o}}}}else{if(null==i)throw new Error("Unexpected delta format");if("string"==typeof i){const{node:e,offset:t}=B(this,o,!0);e instanceof E?e._text=R(e._text,t,0,i):this._xmlText.delete(t,i.length),o+=i.length}else{const t=i,{nodeIndex:s}=B(this,o,!1),l=W(e,t,this);n.splice(s,0,l),o+=1}}}}syncChildrenFromYjs(e){const t=this.getNode();null===t&&T(92);const o=t.__key,s=G(t,null),l=s.length,i=this._children,r=i.length,c=e.collabNodeMap,f=new Set;let a,u,d=0,_=null;r!==l&&(u=t.getWritable());for(let l=0;l<r;l++){const h=s[d],p=i[l],g=p.getNode(),y=p._key;if(null!==g&&h===y){const t=n(g);if(f.add(h),t)if(p._key=h,p instanceof Q){const t=p._xmlText;p.syncPropertiesFromYjs(e,null),p.applyChildrenYjsDelta(e,t.toDelta()),p.syncChildrenFromYjs(e)}else p instanceof E?p.syncPropertiesAndTextFromYjs(e,null):p instanceof H?p.syncPropertiesFromYjs(e,null):p instanceof S||T(93);_=g,d++}else{if(void 0===a){a=new Set;for(let e=0;e<r;e++){const t=i[e]._key;""!==t&&a.add(t)}}if(null!==g&&void 0!==h&&!a.has(h)){q(Y(h)),l--,d++;continue}u=t.getWritable();const n=I(e,p,o),s=n.__key;if(c.set(s,p),null===_){const e=u.getFirstChild();if(u.__first=s,null!==e){const t=e.getWritable();t.__prev=s,n.__next=t.__key}}else{const e=_.getWritable(),t=_.getNextSibling();if(e.__next=s,n.__prev=_.__key,null!==t){const e=t.getWritable();e.__prev=s,n.__next=e.__key}}l===r-1&&(u.__last=s),u.__size++,_=n}}for(let t=0;t<l;t++){const n=s[t];if(!f.has(n)){const t=Y(n),o=e.collabNodeMap.get(n);void 0!==o&&o.destroy(e),q(t)}}}syncPropertiesFromLexical(e,t,n){$(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(e,t,o,s,l,r){const f=this._children[t],a=Y(o);f instanceof Q&&i(a)?(f.syncPropertiesFromLexical(e,a,s),f.syncChildrenFromLexical(e,a,s,l,r)):f instanceof E&&n(a)?f.syncPropertiesAndTextFromLexical(e,a,s):f instanceof H&&c(a)&&f.syncPropertiesFromLexical(e,a,s)}syncChildrenFromLexical(e,t,n,o,s){const l=this.getPrevNode(n),i=null===l?[]:G(l,n),r=G(t,null),c=i.length-1,f=r.length-1,a=e.collabNodeMap;let u,d,_=0,h=0;for(;_<=c&&h<=f;){const t=i[_],l=r[h];if(t===l)this._syncChildFromLexical(e,h,l,n,o,s),_++,h++;else{void 0===u&&(u=new Set(i)),void 0===d&&(d=new Set(r));const n=d.has(t),o=u.has(l);if(n){const t=D(e,Y(l),this);a.set(l,t),o?(this.splice(e,h,1,t),_++,h++):(this.splice(e,h,0,t),h++)}else this.splice(e,h,1),_++}}const p=_>c,g=h>f;if(p&&!g)for(;h<=f;++h){const t=r[h],n=D(e,Y(t),this);this.append(n),a.set(t,n)}else if(g&&!p)for(let t=this._children.length-1;t>=h;t--)this.splice(e,t,1)}append(e){const t=this._xmlText,n=this._children,o=n[n.length-1],s=void 0!==o?o.getOffset()+o.getSize():0;if(e instanceof Q)t.insertEmbed(s,e._xmlText);else if(e instanceof E){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof S?t.insertEmbed(s,e._map):e instanceof H&&t.insertEmbed(s,e._xmlElem);this._children.push(e)}splice(e,t,n,o){const s=this._children,l=s[t];if(void 0===l)return void 0===o&&T(94),void this.append(o);const i=l.getOffset();-1===i&&T(95);const r=this._xmlText;if(0!==n&&r.delete(i,l.getSize()),o instanceof Q)r.insertEmbed(i,o._xmlText);else if(o instanceof E){const e=o._map;null===e.parent&&r.insertEmbed(i,e),r.insert(i+1,o._text)}else o instanceof S?r.insertEmbed(i,o._map):o instanceof H&&r.insertEmbed(i,o._xmlElem);if(0!==n){const o=s.slice(t,t+n);for(let t=0;t<o.length;t++)o[t].destroy(e)}void 0!==o?s.splice(t,n,o):s.splice(t,n)}getChildOffset(e){let t=0;const n=this._children;for(let o=0;o<n.length;o++){const s=n[o];if(s===e)return t;t+=s.getSize()}return-1}destroy(e){const t=e.collabNodeMap,n=this._children;for(let t=0;t<n.length;t++)n[t].destroy(e);t.delete(this._key)}}function V(e,t,n){const o=new Q(e,t,n);return e._collabNode=o,o}function X(e,t,n,o,s,l){null==o&&T(81);const i=V(o.get("root",_),null,"root");return i._key="root",{clientID:o.clientID,collabNodeMap:new Map,cursors:new Map,cursorsContainer:null,doc:o,docMap:s,editor:e,excludedProperties:l||new Map,id:n,nodeProperties:new Map,root:i}}function Z(e,t){const o=t.collabNodeMap.get(e.key);if(void 0===o)return null;let s=e.offset,l=o.getSharedType();if(o instanceof E){l=o._parent._xmlText;const e=o.getOffset();if(-1===e)return null;s=e+1+s}else if(o instanceof Q&&"element"===e.type){const t=e.getNode();if(!i(t))throw Error("Element point must be an element node");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 x(l,s)}function ee(e,t){return y(e,t.doc)}function te(e,t){if(null==e){if(null!=t)return!0}else if(null==t||!m(e,t))return!0;return!1}function ne(e,t){return{color:t,name:e,selection:null}}function oe(e,t){const n=e.cursorsContainer;if(null!==n){const e=t.selections,o=e.length;for(let t=0;t<o;t++)n.removeChild(e[t])}}function se(e,t){const n=t.selection;null!==n&&oe(e,n)}function le(e,t,n,o,s){const l=e.color,i=document.createElement("span");i.style.cssText=`position:absolute;top:0;bottom:0;right:-1px;width:1px;background-color:${l};z-index:10;`;const r=document.createElement("span");return r.textContent=e.name,r.style.cssText=`position:absolute;left:-2px;top:-16px;background-color:${l};color:#fff;line-height:12px;font-size:12px;padding:2px;font-family:Arial;font-weight:bold;white-space:nowrap;`,i.appendChild(r),{anchor:{key:t,offset:n},caret:i,color:l,focus:{key:o,offset:s},name:r,selections:[]}}function ie(e,n,o,s){const l=e.editor,i=l.getRootElement(),r=e.cursorsContainer;if(null===r||null===i)return;const c=r.offsetParent;if(null===c)return;const f=c.getBoundingClientRect(),a=n.selection;if(null===o)return null===a?void 0:(n.selection=null,void oe(e,a));n.selection=o;const u=o.caret,d=o.color,_=o.selections,h=o.anchor,p=o.focus,g=h.key,y=p.key,x=s.get(g),m=s.get(y);if(null==x||null==m)return;let k;if(x===m&&t(x)){k=[l.getElementByKey(g).getBoundingClientRect()]}else{const e=C(l,x,h.offset,m,p.offset);if(null===e)return;k=w(l,e)}const b=_.length,v=k.length;for(let e=0;e<v;e++){const t=k[e];let n=_[e];if(void 0===n){n=document.createElement("span"),_[e]=n;const t=document.createElement("span");n.appendChild(t),r.appendChild(n)}const o=`position:absolute;top:${t.top-f.top}px;left:${t.left-f.left}px;height:${t.height}px;width:${t.width}px;pointer-events:none;z-index:5;`;n.style.cssText=o,n.firstChild.style.cssText=`${o}left:0;top:0;background-color:${d};opacity:0.3;`,e===v-1&&u.parentNode!==n&&n.appendChild(u)}for(let e=b-1;e>=v;e--){const t=_[e];r.removeChild(t),_.pop()}}function re(e,t){const n=t.awareness.getLocalState();if(null===n)return;const l=n.anchorPos,i=n.focusPos;if(null!==l&&null!==i){const t=ee(l,e),n=ee(i,e);if(null!==t&&null!==n){const[e,l]=fe(t.type,t.index),[i,r]=fe(n.type,n.index);if(null!==e&&null!==i){const t=e.getKey(),n=i.getKey(),c=o();if(!s(c))return;const f=c.anchor,a=c.focus;ce(f,t,l),ce(a,n,r)}}}}function ce(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 fe(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof Q){const{node:e,offset:o}=B(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function ae(e,t){const n=Array.from(t.awareness.getStates()),o=e.clientID,s=e.cursors,l=e.editor._editorState._nodeMap,i=new Set;for(let t=0;t<n.length;t++){const r=n[t],[c,f]=r;if(c!==o){i.add(c);const{anchorPos:t,focusPos:n,name:o,color:r,focusing:a}=f;let u=null,d=s.get(c);if(void 0===d&&(d=ne(o,r),s.set(c,d)),null!==t&&null!==n&&a){const o=ee(t,e),s=ee(n,e);if(null!==o&&null!==s){const[e,t]=fe(o.type,o.index),[n,l]=fe(s.type,s.index);if(null!==e&&null!==n){const o=e.getKey(),s=n.getKey();if(u=d.selection,null===u)u=le(d,o,t,s,l);else{const e=u.anchor,n=u.focus;e.key=o,e.offset=t,n.key=s,n.offset=l}}}}ie(e,d,u,l)}}const r=Array.from(s.keys());for(let t=0;t<r.length;t++){const n=r[t];if(!i.has(n)){const t=s.get(n);void 0!==t&&(se(e,t),s.delete(n))}}}function ue(e,t,n,o){const l=t.awareness,i=l.getLocalState();if(null===i)return;const{anchorPos:r,focusPos:c,name:f,color:a,focusing:u,awarenessData:d}=i;let _=null,h=null;(null!==o&&(null===r||o.is(n))||null!==n)&&(s(o)&&(_=Z(o.anchor,e),h=Z(o.focus,e)),(te(r,_)||te(c,h))&&l.setLocalState({anchorPos:_,awarenessData:d,color:a,focusPos:h,focusing:u,name:f}))}function de(e,t){const{target:n}=t,o=W(e,n);if(o instanceof Q&&t instanceof k){const{keysChanged:n,childListChanged:s,delta:l}=t;n.size>0&&o.syncPropertiesFromYjs(e,n),s&&(o.applyChildrenYjsDelta(e,l),o.syncChildrenFromYjs(e))}else if(o instanceof E&&t instanceof b){const{keysChanged:n}=t;n.size>0&&o.syncPropertiesAndTextFromYjs(e,n)}else if(o instanceof H&&t instanceof v){const{attributesChanged:n}=t;n.size>0&&o.syncPropertiesFromYjs(e,n)}else T(82)}function _e(e,t,n,l){const i=e.editor,r=i._editorState;n.forEach((e=>e.delta)),i.update((()=>{const l=i._pendingEditorState;for(let t=0;t<n.length;t++){const o=n[t];de(e,o)}const c=o();if(s(c))if(U(c)){const n=r._selection;if(s(n)){const o=N(i,0,r),s=N(i,0,l),[d,_]=o.getOffsetsFromSelection(n),h=d>=0&&_>=0?s.createSelectionFromOffsets(d,_,o):null;if(null!==h)f(h);else if(re(e,t),U(c)){const e=a();0===e.getChildrenSize()&&e.append(u()),a().selectEnd()}}ue(e,t,n,o())}else re(e,t)}),{onUpdate:()=>{ae(e,t)},skipTransforms:!0,tag:l?"historic":"collaboration"})}function he(t,s,l,i,r,c,f,u){!function(e,t){e.doc.transact(t,e)}(t,(()=>{i.read((()=>{if(u.has("collaboration")||u.has("historic"))return void(f.size>0&&function(t,o){const s=Array.from(o),l=t.collabNodeMap,i=[];for(let t=0;t<s.length;t++){const o=s[t],r=e(o),c=l.get(o);if(c instanceof E)if(n(r))i.push([c,r.__text]);else{const e=c.getOffset();if(-1===e)continue;const t=c._parent;c._normalized=!0,t._xmlText.delete(e,1),l.delete(o);const n=t._children,s=n.indexOf(c);n.splice(s,1)}}for(let e=0;e<i.length;e++){const[t,n]=i[e];t instanceof E&&"string"==typeof n&&(t._text=n)}}(t,f));if(r.has("root")){const e=l._nodeMap,n=a(),o=t.root;o.syncPropertiesFromLexical(t,n,e),o.syncChildrenFromLexical(t,n,e,r,c)}const i=o(),d=l._selection;ue(t,s,d,i)}))}))}const pe=d("CONNECTED_COMMAND"),ge=d("TOGGLE_CONNECT_COMMAND");function ye(e,t){return new P(t,{trackedOrigins:new Set([e,null])})}function xe(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})}function me(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{pe as CONNECTED_COMMAND,ge as TOGGLE_CONNECT_COMMAND,X as createBinding,ye as createUndoManager,xe as initLocalState,me as setLocalStateFocus,ae as syncCursorPositions,he as syncLexicalUpdateToYjs,_e as syncYjsChangesToLexical};
|
|
7
|
+
import{$getNodeByKey as e,$isLineBreakNode as t,$isTextNode as n,$getSelection as o,$isRangeSelection as s,createEditor as l,$isElementNode as i,$isRootNode as r,$isDecoratorNode as c,$getRoot as f,$createParagraphNode as a,$setSelection as u,createCommand as d}from"lexical";import{XmlText as _,Map as h,XmlElement as p,Doc as g,createAbsolutePositionFromRelativePosition as y,createRelativePositionFromTypeIndex as x,compareRelativePositions as m,YTextEvent as k,YMapEvent as b,YXmlEvent as v,UndoManager as P}from"yjs";import{createDOMRange as C,createRectsFromDOMRange as w}from"@lexical/selection";import{$createOffsetView as N}from"@lexical/offset";var T=function(e){const t=new URLSearchParams;t.append("code",e);for(let e=1;e<arguments.length;e++)t.append("v",arguments[e]);throw Error(`Minified Lexical error #${e}; visit https://lexical.dev/docs/error?${t} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)};class S{constructor(e,t){this._key="",this._map=e,this._parent=t,this._type="linebreak"}getNode(){const n=e(this._key);return t(n)?n:null}getKey(){return this._key}getSharedType(){return this._map}getType(){return this._type}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}destroy(e){e.collabNodeMap.delete(this._key)}}function F(e,t){const n=new S(e,t);return e._collabNode=n,n}class E{constructor(e,t,n,o){this._key="",this._map=e,this._parent=n,this._text=t,this._type=o,this._normalized=!1}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return n(t)?t:null}getNode(){const t=e(this._key);return n(t)?t:null}getSharedType(){return this._map}getType(){return this._type}getKey(){return this._key}getSize(){return this._text.length+(this._normalized?0:1)}getOffset(){return this._parent.getChildOffset(this)}spliceText(e,t,n){const o=this._parent._xmlText,s=this.getOffset()+1+e;0!==t&&o.delete(s,t),""!==n&&o.insert(s,n)}syncPropertiesAndTextFromLexical(e,t,n){const l=this.getPrevNode(n),i=t.__text;if($(e,this._map,l,t),null!==l){const e=l.__text;if(e!==i){!function(e,t,n,l){const i=o();let r=l.length;if(s(i)&&i.isCollapsed()){const e=i.anchor;e.key===t&&(r=e.offset)}const c=function(e,t,n){const o=e.length,s=t.length;let l=0,i=0;for(;l<o&&l<s&&e[l]===t[l]&&l<n;)l++;for(;i+l<o&&i+l<s&&e[o-i-1]===t[s-i-1];)i++;for(;i+l<o&&i+l<s&&e[l]===t[l];)l++;return{index:l,insert:t.slice(l,s-i),remove:o-l-i}}(n,l,r);e.spliceText(c.index,c.remove,c.insert)}(this,t.__key,e,i),this._text=i}}}syncPropertiesAndTextFromYjs(e,t){const n=this.getNode();null===n&&T(84),K(e,this._map,n,t);const o=this._text;if(n.__text!==o){n.getWritable().__text=o}}destroy(e){e.collabNodeMap.delete(this._key)}}function M(e,t,n,o){const s=new E(e,t,n,o);return e._collabNode=s,s}const z=new Set(["__key","__parent","__next","__prev"]),L=new Set(["__first","__last","__size"]),O=new Set(["__cachedText"]),j=new Set(["__text"]);function A(e,t,o){if(z.has(e))return!0;if(n(t)){if(j.has(e))return!0}else if(i(t)&&(L.has(e)||r(t)&&O.has(e)))return!0;const s=t.constructor,l=o.excludedProperties.get(s);return null!=l&&l.has(e)}function Y(t){const n=e(t);return null===n&&T(85),n}function D(e,o,s){const l=o.__type;let r;if(i(o)){r=V(new _,s,l),r.syncPropertiesFromLexical(e,o,null),r.syncChildrenFromLexical(e,o,null,null,null)}else if(n(o)){r=M(new h,o.__text,s,l),r.syncPropertiesAndTextFromLexical(e,o,null)}else if(t(o)){const e=new h;e.set("__type","linebreak"),r=F(e,s)}else if(c(o)){r=J(new p,s,l),r.syncPropertiesFromLexical(e,o,null)}else T(86);return r._key=o.__key,r}function W(e,t,n){const o=t._collabNode;if(void 0===o){const o=e.editor._nodes,s=function(e){const t=e instanceof h?e.get("__type"):e.getAttribute("__type");return null==t&&T(87),t}(t);void 0===o.get(s)&&T(88,s);const l=t.parent,i=void 0===n&&null!==l?W(e,l):n||null;if(i instanceof Q||T(89),t instanceof _)return V(t,i,s);if(t instanceof h)return"linebreak"===s?F(t,i):M(t,"",i,s);if(t instanceof p)return J(t,i,s)}return o}function I(e,t,n){const o=t.getType(),s=e.editor._nodes.get(o);void 0===s&&T(88,o);const l=new s.klass;if(l.__parent=n,t._key=l.__key,t instanceof Q){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof E?t.syncPropertiesAndTextFromYjs(e,null):t instanceof H&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(l.__key,t),l}function K(e,t,n,o){const s=null===o?t instanceof h?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(A(r,n,e))continue;const c=n[r];let f=t instanceof h?t.get(r):t.getAttribute(r);if(c!==f){if(f instanceof g){const t=e.docMap;c instanceof g&&t.delete(c.guid);const n=l(),o=f.guid;n._key=o,t.set(o,f),f=n}void 0===i&&(i=n.getWritable()),i[r]=f}}}function $(e,t,n,o){const s=o.__type,l=e.nodeProperties;let i=l.get(s);void 0===i&&(i=Object.keys(o).filter((t=>!A(t,o,e))),l.set(s,i));const r=e.editor.constructor;for(let s=0;s<i.length;s++){const l=i[s],c=null===n?void 0:n[l];let f=o[l];if(c!==f){if(f instanceof r){const t=e.docMap;let n;if(c instanceof r){const e=c._key;n=t.get(e),t.delete(e)}const s=n||new g,l=s.guid;f._key=l,t.set(l,s),f=s,e.editor.update((()=>{o.markDirty()}))}t instanceof h?t.set(l,f):t.setAttribute(l,f)}}}function R(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function B(e,t,n){let o=0,s=0;const l=e._children,i=l.length;for(;s<i;s++){const e=l[s],r=o;o+=e.getSize();if((n?o>=t:o>t)&&e instanceof E){let n=t-r-1;n<0&&(n=0);return{length:o-t,node:e,nodeIndex:s,offset:n}}if(o>t)return{length:0,node:e,nodeIndex:s,offset:r};if(s===i-1)return{length:0,node:null,nodeIndex:s+1,offset:r+1}}return{length:0,node:null,nodeIndex:0,offset:0}}function U(e){const t=e.anchor,o=e.focus;let s=!1;try{const e=t.getNode(),l=o.getNode();(!e.isAttached()||!l.isAttached()||n(e)&&t.offset>e.getTextContentSize()||n(l)&&o.offset>l.getTextContentSize())&&(s=!0)}catch(e){s=!0}return s}function G(t,n){const o=[];let s=t.__first;for(;null!==s;){const t=null===n?e(s):n.get(s);null==t&&T(101),o.push(s),s=t.__next}return o}function q(e){const t=e.getParent();if(null!==t){const n=e.getWritable(),o=t.getWritable(),s=e.getPreviousSibling(),l=e.getNextSibling();if(null===s)if(null!==l){const e=l.getWritable();o.__first=l.__key,e.__prev=null}else o.__first=null;else{const e=s.getWritable();if(null!==l){const t=l.getWritable();t.__prev=e.__key,e.__next=t.__key}else e.__next=null;n.__prev=null}if(null===l)if(null!==s){const e=s.getWritable();o.__last=s.__key,e.__next=null}else o.__last=null;else{const e=l.getWritable();if(null!==s){const t=s.getWritable();t.__next=e.__key,e.__prev=t.__key}else e.__prev=null;n.__next=null}o.__size--,n.__parent=null}}class H{constructor(e,t,n){this._key="",this._xmlElem=e,this._parent=t,this._type=n}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return c(t)?t:null}getNode(){const t=e(this._key);return c(t)?t:null}getSharedType(){return this._xmlElem}getType(){return this._type}getKey(){return this._key}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}syncPropertiesFromLexical(e,t,n){const o=this.getPrevNode(n);$(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&T(83);K(e,this._xmlElem,n,t)}destroy(e){e.collabNodeMap.delete(this._key)}}function J(e,t,n){const o=new H(e,t,n);return e._collabNode=o,o}class Q{constructor(e,t,n){this._key="",this._children=[],this._xmlText=e,this._type=n,this._parent=t}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return 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&&T(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&T(91),K(e,this._xmlText,n,t)}applyChildrenYjsDelta(e,t){const n=this._children;let o=0;for(let s=0;s<t.length;s++){const l=t[s],i=l.insert,r=l.delete;if(null!=l.retain)o+=l.retain;else if("number"==typeof r){let e=r;for(;e>0;){const{node:t,nodeIndex:s,offset:l,length:i}=B(this,o,!1);if(t instanceof Q||t instanceof S||t instanceof H)n.splice(s,1),e-=1;else{if(!(t instanceof E))break;{const o=Math.min(e,i),r=0!==s?n[s-1]:null,c=t.getSize();0===l&&1===o&&s>0&&r instanceof E&&i===c&&0===Array.from(t._map.keys()).length?(r._text+=t._text,n.splice(s,1)):0===l&&o===c?n.splice(s,1):t._text=R(t._text,l,o,""),e-=o}}}}else{if(null==i)throw new Error("Unexpected delta format");if("string"==typeof i){const{node:e,offset:t}=B(this,o,!0);e instanceof E?e._text=R(e._text,t,0,i):this._xmlText.delete(t,i.length),o+=i.length}else{const t=i,{nodeIndex:s}=B(this,o,!1),l=W(e,t,this);n.splice(s,0,l),o+=1}}}}syncChildrenFromYjs(e){const t=this.getNode();null===t&&T(92);const o=t.__key,s=G(t,null),l=s.length,i=this._children,r=i.length,c=e.collabNodeMap,f=new Set;let a,u,d=0,_=null;r!==l&&(u=t.getWritable());for(let l=0;l<r;l++){const h=s[d],p=i[l],g=p.getNode(),y=p._key;if(null!==g&&h===y){const t=n(g);if(f.add(h),t)if(p._key=h,p instanceof Q){const t=p._xmlText;p.syncPropertiesFromYjs(e,null),p.applyChildrenYjsDelta(e,t.toDelta()),p.syncChildrenFromYjs(e)}else p instanceof E?p.syncPropertiesAndTextFromYjs(e,null):p instanceof H?p.syncPropertiesFromYjs(e,null):p instanceof S||T(93);_=g,d++}else{if(void 0===a){a=new Set;for(let e=0;e<r;e++){const t=i[e]._key;""!==t&&a.add(t)}}if(null!==g&&void 0!==h&&!a.has(h)){q(Y(h)),l--,d++;continue}u=t.getWritable();const n=I(e,p,o),s=n.__key;if(c.set(s,p),null===_){const e=u.getFirstChild();if(u.__first=s,null!==e){const t=e.getWritable();t.__prev=s,n.__next=t.__key}}else{const e=_.getWritable(),t=_.getNextSibling();if(e.__next=s,n.__prev=_.__key,null!==t){const e=t.getWritable();e.__prev=s,n.__next=e.__key}}l===r-1&&(u.__last=s),u.__size++,_=n}}for(let t=0;t<l;t++){const n=s[t];if(!f.has(n)){const t=Y(n),o=e.collabNodeMap.get(n);void 0!==o&&o.destroy(e),q(t)}}}syncPropertiesFromLexical(e,t,n){$(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(e,t,o,s,l,r){const f=this._children[t],a=Y(o);f instanceof Q&&i(a)?(f.syncPropertiesFromLexical(e,a,s),f.syncChildrenFromLexical(e,a,s,l,r)):f instanceof E&&n(a)?f.syncPropertiesAndTextFromLexical(e,a,s):f instanceof H&&c(a)&&f.syncPropertiesFromLexical(e,a,s)}syncChildrenFromLexical(e,t,n,o,s){const l=this.getPrevNode(n),i=null===l?[]:G(l,n),r=G(t,null),c=i.length-1,f=r.length-1,a=e.collabNodeMap;let u,d,_=0,h=0;for(;_<=c&&h<=f;){const t=i[_],l=r[h];if(t===l)this._syncChildFromLexical(e,h,l,n,o,s),_++,h++;else{void 0===u&&(u=new Set(i)),void 0===d&&(d=new Set(r));const n=d.has(t),o=u.has(l);if(n){const t=D(e,Y(l),this);a.set(l,t),o?(this.splice(e,h,1,t),_++,h++):(this.splice(e,h,0,t),h++)}else this.splice(e,h,1),_++}}const p=_>c,g=h>f;if(p&&!g)for(;h<=f;++h){const t=r[h],n=D(e,Y(t),this);this.append(n),a.set(t,n)}else if(g&&!p)for(let t=this._children.length-1;t>=h;t--)this.splice(e,t,1)}append(e){const t=this._xmlText,n=this._children,o=n[n.length-1],s=void 0!==o?o.getOffset()+o.getSize():0;if(e instanceof Q)t.insertEmbed(s,e._xmlText);else if(e instanceof E){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof S?t.insertEmbed(s,e._map):e instanceof H&&t.insertEmbed(s,e._xmlElem);this._children.push(e)}splice(e,t,n,o){const s=this._children,l=s[t];if(void 0===l)return void 0===o&&T(94),void this.append(o);const i=l.getOffset();-1===i&&T(95);const r=this._xmlText;if(0!==n&&r.delete(i,l.getSize()),o instanceof Q)r.insertEmbed(i,o._xmlText);else if(o instanceof E){const e=o._map;null===e.parent&&r.insertEmbed(i,e),r.insert(i+1,o._text)}else o instanceof S?r.insertEmbed(i,o._map):o instanceof H&&r.insertEmbed(i,o._xmlElem);if(0!==n){const o=s.slice(t,t+n);for(let t=0;t<o.length;t++)o[t].destroy(e)}void 0!==o?s.splice(t,n,o):s.splice(t,n)}getChildOffset(e){let t=0;const n=this._children;for(let o=0;o<n.length;o++){const s=n[o];if(s===e)return t;t+=s.getSize()}return-1}destroy(e){const t=e.collabNodeMap,n=this._children;for(let t=0;t<n.length;t++)n[t].destroy(e);t.delete(this._key)}}function V(e,t,n){const o=new Q(e,t,n);return e._collabNode=o,o}function X(e,t,n,o,s,l){null==o&&T(81);const i=V(o.get("root",_),null,"root");return i._key="root",{clientID:o.clientID,collabNodeMap:new Map,cursors:new Map,cursorsContainer:null,doc:o,docMap:s,editor:e,excludedProperties:l||new Map,id:n,nodeProperties:new Map,root:i}}function Z(e,t){const o=t.collabNodeMap.get(e.key);if(void 0===o)return null;let s=e.offset,l=o.getSharedType();if(o instanceof E){l=o._parent._xmlText;const e=o.getOffset();if(-1===e)return null;s=e+1+s}else if(o instanceof Q&&"element"===e.type){const t=e.getNode();if(!i(t))throw Error("Element point must be an element node");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 x(l,s)}function ee(e,t){return y(e,t.doc)}function te(e,t){if(null==e){if(null!=t)return!0}else if(null==t||!m(e,t))return!0;return!1}function ne(e,t){return{color:t,name:e,selection:null}}function oe(e,t){const n=e.cursorsContainer;if(null!==n){const e=t.selections,o=e.length;for(let t=0;t<o;t++)n.removeChild(e[t])}}function se(e,t){const n=t.selection;null!==n&&oe(e,n)}function le(e,t,n,o,s){const l=e.color,i=document.createElement("span");i.style.cssText=`position:absolute;top:0;bottom:0;right:-1px;width:1px;background-color:${l};z-index:10;`;const r=document.createElement("span");return r.textContent=e.name,r.style.cssText=`position:absolute;left:-2px;top:-16px;background-color:${l};color:#fff;line-height:12px;font-size:12px;padding:2px;font-family:Arial;font-weight:bold;white-space:nowrap;`,i.appendChild(r),{anchor:{key:t,offset:n},caret:i,color:l,focus:{key:o,offset:s},name:r,selections:[]}}function ie(e,n,o,s){const l=e.editor,i=l.getRootElement(),r=e.cursorsContainer;if(null===r||null===i)return;const c=r.offsetParent;if(null===c)return;const f=c.getBoundingClientRect(),a=n.selection;if(null===o)return null===a?void 0:(n.selection=null,void oe(e,a));n.selection=o;const u=o.caret,d=o.color,_=o.selections,h=o.anchor,p=o.focus,g=h.key,y=p.key,x=s.get(g),m=s.get(y);if(null==x||null==m)return;let k;if(x===m&&t(x)){k=[l.getElementByKey(g).getBoundingClientRect()]}else{const e=C(l,x,h.offset,m,p.offset);if(null===e)return;k=w(l,e)}const b=_.length,v=k.length;for(let e=0;e<v;e++){const t=k[e];let n=_[e];if(void 0===n){n=document.createElement("span"),_[e]=n;const t=document.createElement("span");n.appendChild(t),r.appendChild(n)}const o=`position:absolute;top:${t.top-f.top}px;left:${t.left-f.left}px;height:${t.height}px;width:${t.width}px;pointer-events:none;z-index:5;`;n.style.cssText=o,n.firstChild.style.cssText=`${o}left:0;top:0;background-color:${d};opacity:0.3;`,e===v-1&&u.parentNode!==n&&n.appendChild(u)}for(let e=b-1;e>=v;e--){const t=_[e];r.removeChild(t),_.pop()}}function re(e,t){const n=t.awareness.getLocalState();if(null===n)return;const l=n.anchorPos,i=n.focusPos;if(null!==l&&null!==i){const t=ee(l,e),n=ee(i,e);if(null!==t&&null!==n){const[e,l]=fe(t.type,t.index),[i,r]=fe(n.type,n.index);if(null!==e&&null!==i){const t=e.getKey(),n=i.getKey(),c=o();if(!s(c))return;const f=c.anchor,a=c.focus;ce(f,t,l),ce(a,n,r)}}}}function ce(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 fe(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof Q){const{node:e,offset:o}=B(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function ae(e,t){const n=Array.from(t.awareness.getStates()),o=e.clientID,s=e.cursors,l=e.editor._editorState._nodeMap,i=new Set;for(let t=0;t<n.length;t++){const r=n[t],[c,f]=r;if(c!==o){i.add(c);const{anchorPos:t,focusPos:n,name:o,color:r,focusing:a}=f;let u=null,d=s.get(c);if(void 0===d&&(d=ne(o,r),s.set(c,d)),null!==t&&null!==n&&a){const o=ee(t,e),s=ee(n,e);if(null!==o&&null!==s){const[e,t]=fe(o.type,o.index),[n,l]=fe(s.type,s.index);if(null!==e&&null!==n){const o=e.getKey(),s=n.getKey();if(u=d.selection,null===u)u=le(d,o,t,s,l);else{const e=u.anchor,n=u.focus;e.key=o,e.offset=t,n.key=s,n.offset=l}}}}ie(e,d,u,l)}}const r=Array.from(s.keys());for(let t=0;t<r.length;t++){const n=r[t];if(!i.has(n)){const t=s.get(n);void 0!==t&&(se(e,t),s.delete(n))}}}function ue(e,t,n,o){const l=t.awareness,i=l.getLocalState();if(null===i)return;const{anchorPos:r,focusPos:c,name:f,color:a,focusing:u,awarenessData:d}=i;let _=null,h=null;(null!==o&&(null===r||o.is(n))||null!==n)&&(s(o)&&(_=Z(o.anchor,e),h=Z(o.focus,e)),(te(r,_)||te(c,h))&&l.setLocalState({anchorPos:_,awarenessData:d,color:a,focusPos:h,focusing:u,name:f}))}function de(e,t){const{target:n}=t,o=W(e,n);if(o instanceof Q&&t instanceof k){const{keysChanged:n,childListChanged:s,delta:l}=t;n.size>0&&o.syncPropertiesFromYjs(e,n),s&&(o.applyChildrenYjsDelta(e,l),o.syncChildrenFromYjs(e))}else if(o instanceof E&&t instanceof b){const{keysChanged:n}=t;n.size>0&&o.syncPropertiesAndTextFromYjs(e,n)}else if(o instanceof H&&t instanceof v){const{attributesChanged:n}=t;n.size>0&&o.syncPropertiesFromYjs(e,n)}else T(82)}function _e(e,t,n,l){const i=e.editor,r=i._editorState;n.forEach((e=>e.delta)),i.update((()=>{const l=i._pendingEditorState;for(let t=0;t<n.length;t++){const o=n[t];de(e,o)}0===f().getChildrenSize()&&f().append(a());const c=o();if(s(c))if(U(c)){const n=r._selection;if(s(n)){const o=N(i,0,r),s=N(i,0,l),[a,d]=o.getOffsetsFromSelection(n),_=a>=0&&d>=0?s.createSelectionFromOffsets(a,d,o):null;null!==_?u(_):(re(e,t),U(c)&&f().selectEnd())}ue(e,t,n,o())}else re(e,t)}),{onUpdate:()=>{ae(e,t)},skipTransforms:!0,tag:l?"historic":"collaboration"})}function he(t,s,l,i,r,c,a,u){!function(e,t){e.doc.transact(t,e)}(t,(()=>{i.read((()=>{if(u.has("collaboration")||u.has("historic"))return void(a.size>0&&function(t,o){const s=Array.from(o),l=t.collabNodeMap,i=[];for(let t=0;t<s.length;t++){const o=s[t],r=e(o),c=l.get(o);if(c instanceof E)if(n(r))i.push([c,r.__text]);else{const e=c.getOffset();if(-1===e)continue;const t=c._parent;c._normalized=!0,t._xmlText.delete(e,1),l.delete(o);const n=t._children,s=n.indexOf(c);n.splice(s,1)}}for(let e=0;e<i.length;e++){const[t,n]=i[e];t instanceof E&&"string"==typeof n&&(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;ue(t,s,d,i)}))}))}const pe=d("CONNECTED_COMMAND"),ge=d("TOGGLE_CONNECT_COMMAND");function ye(e,t){return new P(t,{trackedOrigins:new Set([e,null])})}function xe(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})}function me(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{pe as CONNECTED_COMMAND,ge as TOGGLE_CONNECT_COMMAND,X as createBinding,ye as createUndoManager,xe as initLocalState,me as setLocalStateFocus,ae as syncCursorPositions,he as syncLexicalUpdateToYjs,_e as syncYjsChangesToLexical};
|
package/index.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -11,13 +11,14 @@
|
|
|
11
11
|
"crdt"
|
|
12
12
|
],
|
|
13
13
|
"license": "MIT",
|
|
14
|
-
"version": "0.14.
|
|
14
|
+
"version": "0.14.4",
|
|
15
15
|
"main": "LexicalYjs.js",
|
|
16
|
+
"types": "index.d.ts",
|
|
16
17
|
"dependencies": {
|
|
17
|
-
"@lexical/offset": "0.14.
|
|
18
|
+
"@lexical/offset": "0.14.4",
|
|
19
|
+
"lexical": "0.14.4"
|
|
18
20
|
},
|
|
19
21
|
"peerDependencies": {
|
|
20
|
-
"lexical": "0.14.2",
|
|
21
22
|
"yjs": ">=13.5.22"
|
|
22
23
|
},
|
|
23
24
|
"repository": {
|
|
@@ -25,6 +26,23 @@
|
|
|
25
26
|
"url": "https://github.com/facebook/lexical",
|
|
26
27
|
"directory": "packages/lexical-yjs"
|
|
27
28
|
},
|
|
28
|
-
"module": "LexicalYjs.
|
|
29
|
-
"sideEffects": false
|
|
29
|
+
"module": "LexicalYjs.mjs",
|
|
30
|
+
"sideEffects": false,
|
|
31
|
+
"exports": {
|
|
32
|
+
".": {
|
|
33
|
+
"import": {
|
|
34
|
+
"types": "./index.d.ts",
|
|
35
|
+
"development": "./LexicalYjs.dev.mjs",
|
|
36
|
+
"production": "./LexicalYjs.prod.mjs",
|
|
37
|
+
"node": "./LexicalYjs.node.mjs",
|
|
38
|
+
"default": "./LexicalYjs.mjs"
|
|
39
|
+
},
|
|
40
|
+
"require": {
|
|
41
|
+
"types": "./index.d.ts",
|
|
42
|
+
"development": "./LexicalYjs.dev.js",
|
|
43
|
+
"production": "./LexicalYjs.prod.js",
|
|
44
|
+
"default": "./LexicalYjs.js"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
30
48
|
}
|