@lexical/yjs 0.25.1-nightly.20250227.0 → 0.26.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 +106 -27
- package/LexicalYjs.dev.mjs +103 -24
- package/LexicalYjs.prod.js +1 -1
- package/LexicalYjs.prod.mjs +1 -1
- package/Utils.d.ts +3 -4
- package/package.json +4 -4
package/LexicalYjs.dev.js
CHANGED
|
@@ -177,7 +177,7 @@ class CollabTextNode {
|
|
|
177
177
|
if (!(lexicalNode !== null)) {
|
|
178
178
|
throw Error(`syncPropertiesAndTextFromYjs: could not find decorator node`);
|
|
179
179
|
}
|
|
180
|
-
syncPropertiesFromYjs(binding, this._map, lexicalNode, keysChanged);
|
|
180
|
+
$syncPropertiesFromYjs(binding, this._map, lexicalNode, keysChanged);
|
|
181
181
|
const collabText = this._text;
|
|
182
182
|
if (lexicalNode.__text !== collabText) {
|
|
183
183
|
const writable = lexicalNode.getWritable();
|
|
@@ -203,12 +203,12 @@ function $createCollabTextNode(map, text, parent, type) {
|
|
|
203
203
|
*
|
|
204
204
|
*/
|
|
205
205
|
|
|
206
|
-
const baseExcludedProperties = new Set(['__key', '__parent', '__next', '__prev']);
|
|
206
|
+
const baseExcludedProperties = new Set(['__key', '__parent', '__next', '__prev', '__state']);
|
|
207
207
|
const elementExcludedProperties = new Set(['__first', '__last', '__size']);
|
|
208
208
|
const rootExcludedProperties = new Set(['__cachedText']);
|
|
209
209
|
const textExcludedProperties = new Set(['__text']);
|
|
210
210
|
function isExcludedProperty(name, node, binding) {
|
|
211
|
-
if (baseExcludedProperties.has(name)) {
|
|
211
|
+
if (baseExcludedProperties.has(name) || typeof node[name] === 'function') {
|
|
212
212
|
return true;
|
|
213
213
|
}
|
|
214
214
|
if (lexical.$isTextNode(node)) {
|
|
@@ -224,13 +224,6 @@ function isExcludedProperty(name, node, binding) {
|
|
|
224
224
|
const excludedProperties = binding.excludedProperties.get(nodeKlass);
|
|
225
225
|
return excludedProperties != null && excludedProperties.has(name);
|
|
226
226
|
}
|
|
227
|
-
function $getNodeByKeyOrThrow(key) {
|
|
228
|
-
const node = lexical.$getNodeByKey(key);
|
|
229
|
-
if (!(node !== null)) {
|
|
230
|
-
throw Error(`could not find node by key`);
|
|
231
|
-
}
|
|
232
|
-
return node;
|
|
233
|
-
}
|
|
234
227
|
function $createCollabNodeFromLexicalNode(binding, lexicalNode, parent) {
|
|
235
228
|
const nodeType = lexicalNode.__type;
|
|
236
229
|
let collabNode;
|
|
@@ -261,8 +254,8 @@ function $createCollabNodeFromLexicalNode(binding, lexicalNode, parent) {
|
|
|
261
254
|
return collabNode;
|
|
262
255
|
}
|
|
263
256
|
function getNodeTypeFromSharedType(sharedType) {
|
|
264
|
-
const type = sharedType
|
|
265
|
-
if (!(type
|
|
257
|
+
const type = sharedTypeGet(sharedType, '__type');
|
|
258
|
+
if (!(typeof type === 'string' || typeof type === 'undefined')) {
|
|
266
259
|
throw Error(`Expected shared type to include type attribute`);
|
|
267
260
|
}
|
|
268
261
|
return type;
|
|
@@ -272,6 +265,9 @@ function $getOrInitCollabNodeFromSharedType(binding, sharedType, parent) {
|
|
|
272
265
|
if (collabNode === undefined) {
|
|
273
266
|
const registeredNodes = binding.editor._nodes;
|
|
274
267
|
const type = getNodeTypeFromSharedType(sharedType);
|
|
268
|
+
if (!(typeof type === 'string')) {
|
|
269
|
+
throw Error(`Expected shared type to include type attribute`);
|
|
270
|
+
}
|
|
275
271
|
const nodeInfo = registeredNodes.get(type);
|
|
276
272
|
if (!(nodeInfo !== undefined)) {
|
|
277
273
|
throw Error(`Node ${type} is not registered`);
|
|
@@ -317,17 +313,23 @@ function createLexicalNodeFromCollabNode(binding, collabNode, parentKey) {
|
|
|
317
313
|
binding.collabNodeMap.set(lexicalNode.__key, collabNode);
|
|
318
314
|
return lexicalNode;
|
|
319
315
|
}
|
|
320
|
-
function syncPropertiesFromYjs(binding, sharedType, lexicalNode, keysChanged) {
|
|
316
|
+
function $syncPropertiesFromYjs(binding, sharedType, lexicalNode, keysChanged) {
|
|
321
317
|
const properties = keysChanged === null ? sharedType instanceof yjs.Map ? Array.from(sharedType.keys()) : Object.keys(sharedType.getAttributes()) : Array.from(keysChanged);
|
|
322
318
|
let writableNode;
|
|
323
319
|
for (let i = 0; i < properties.length; i++) {
|
|
324
320
|
const property = properties[i];
|
|
325
321
|
if (isExcludedProperty(property, lexicalNode, binding)) {
|
|
322
|
+
if (property === '__state') {
|
|
323
|
+
if (!writableNode) {
|
|
324
|
+
writableNode = lexicalNode.getWritable();
|
|
325
|
+
}
|
|
326
|
+
$syncNodeStateToLexical(binding, sharedType, writableNode);
|
|
327
|
+
}
|
|
326
328
|
continue;
|
|
327
329
|
}
|
|
328
330
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
329
331
|
const prevValue = lexicalNode[property];
|
|
330
|
-
let nextValue = sharedType
|
|
332
|
+
let nextValue = sharedTypeGet(sharedType, property);
|
|
331
333
|
if (prevValue !== nextValue) {
|
|
332
334
|
if (nextValue instanceof yjs.Doc) {
|
|
333
335
|
const yjsDocMap = binding.docMap;
|
|
@@ -343,10 +345,65 @@ function syncPropertiesFromYjs(binding, sharedType, lexicalNode, keysChanged) {
|
|
|
343
345
|
if (writableNode === undefined) {
|
|
344
346
|
writableNode = lexicalNode.getWritable();
|
|
345
347
|
}
|
|
348
|
+
|
|
349
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
346
350
|
writableNode[property] = nextValue;
|
|
347
351
|
}
|
|
348
352
|
}
|
|
349
353
|
}
|
|
354
|
+
function sharedTypeGet(sharedType, property) {
|
|
355
|
+
if (sharedType instanceof yjs.Map) {
|
|
356
|
+
return sharedType.get(property);
|
|
357
|
+
} else {
|
|
358
|
+
return sharedType.getAttribute(property);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
function sharedTypeSet(sharedType, property, nextValue) {
|
|
362
|
+
if (sharedType instanceof yjs.Map) {
|
|
363
|
+
sharedType.set(property, nextValue);
|
|
364
|
+
} else {
|
|
365
|
+
sharedType.setAttribute(property, nextValue);
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
function $syncNodeStateToLexical(binding, sharedType, lexicalNode) {
|
|
369
|
+
const existingState = sharedTypeGet(sharedType, '__state');
|
|
370
|
+
if (!(existingState instanceof yjs.Map)) {
|
|
371
|
+
return;
|
|
372
|
+
}
|
|
373
|
+
// This should only called when creating the node initially,
|
|
374
|
+
// incremental updates to state come in through YMapEvent
|
|
375
|
+
// with the __state as the target.
|
|
376
|
+
lexical.$getWritableNodeState(lexicalNode).updateFromJSON(existingState.toJSON());
|
|
377
|
+
}
|
|
378
|
+
function syncNodeStateFromLexical(binding, sharedType, prevLexicalNode, nextLexicalNode) {
|
|
379
|
+
const nextState = nextLexicalNode.__state;
|
|
380
|
+
const existingState = sharedTypeGet(sharedType, '__state');
|
|
381
|
+
if (!nextState) {
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
const [unknown, known] = nextState.getInternalState();
|
|
385
|
+
const prevState = prevLexicalNode && prevLexicalNode.__state;
|
|
386
|
+
const stateMap = existingState instanceof yjs.Map ? existingState : new yjs.Map();
|
|
387
|
+
if (prevState === nextState) {
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
const [prevUnknown, prevKnown] = prevState && stateMap.doc ? prevState.getInternalState() : [undefined, new Map()];
|
|
391
|
+
if (unknown) {
|
|
392
|
+
for (const [k, v] of Object.entries(unknown)) {
|
|
393
|
+
if (prevUnknown && v !== prevUnknown[k]) {
|
|
394
|
+
stateMap.set(k, v);
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
for (const [stateConfig, v] of known) {
|
|
399
|
+
if (prevKnown.get(stateConfig) !== v) {
|
|
400
|
+
stateMap.set(stateConfig.key, stateConfig.unparse(v));
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
if (!existingState) {
|
|
404
|
+
sharedTypeSet(sharedType, '__state', stateMap);
|
|
405
|
+
}
|
|
406
|
+
}
|
|
350
407
|
function syncPropertiesFromLexical(binding, sharedType, prevLexicalNode, nextLexicalNode) {
|
|
351
408
|
const type = nextLexicalNode.__type;
|
|
352
409
|
const nodeProperties = binding.nodeProperties;
|
|
@@ -358,6 +415,7 @@ function syncPropertiesFromLexical(binding, sharedType, prevLexicalNode, nextLex
|
|
|
358
415
|
nodeProperties.set(type, properties);
|
|
359
416
|
}
|
|
360
417
|
const EditorClass = binding.editor.constructor;
|
|
418
|
+
syncNodeStateFromLexical(binding, sharedType, prevLexicalNode, nextLexicalNode);
|
|
361
419
|
for (let i = 0; i < properties.length; i++) {
|
|
362
420
|
const property = properties[i];
|
|
363
421
|
const prevValue =
|
|
@@ -386,11 +444,7 @@ function syncPropertiesFromLexical(binding, sharedType, prevLexicalNode, nextLex
|
|
|
386
444
|
nextLexicalNode.markDirty();
|
|
387
445
|
});
|
|
388
446
|
}
|
|
389
|
-
|
|
390
|
-
sharedType.set(property, nextValue);
|
|
391
|
-
} else {
|
|
392
|
-
sharedType.setAttribute(property, nextValue);
|
|
393
|
-
}
|
|
447
|
+
sharedTypeSet(sharedType, property, nextValue);
|
|
394
448
|
}
|
|
395
449
|
}
|
|
396
450
|
}
|
|
@@ -601,7 +655,7 @@ class CollabDecoratorNode {
|
|
|
601
655
|
throw Error(`syncPropertiesFromYjs: could not find decorator node`);
|
|
602
656
|
}
|
|
603
657
|
const xmlElem = this._xmlElem;
|
|
604
|
-
syncPropertiesFromYjs(binding, xmlElem, lexicalNode, keysChanged);
|
|
658
|
+
$syncPropertiesFromYjs(binding, xmlElem, lexicalNode, keysChanged);
|
|
605
659
|
}
|
|
606
660
|
destroy(binding) {
|
|
607
661
|
const collabNodeMap = binding.collabNodeMap;
|
|
@@ -668,7 +722,7 @@ class CollabElementNode {
|
|
|
668
722
|
if (!(lexicalNode !== null)) {
|
|
669
723
|
throw Error(`syncPropertiesFromYjs: could not find element node`);
|
|
670
724
|
}
|
|
671
|
-
syncPropertiesFromYjs(binding, this._xmlText, lexicalNode, keysChanged);
|
|
725
|
+
$syncPropertiesFromYjs(binding, this._xmlText, lexicalNode, keysChanged);
|
|
672
726
|
}
|
|
673
727
|
applyChildrenYjsDelta(binding, deltas) {
|
|
674
728
|
const children = this._children;
|
|
@@ -831,7 +885,7 @@ class CollabElementNode {
|
|
|
831
885
|
}
|
|
832
886
|
}
|
|
833
887
|
if (collabLexicalChildNode !== null && lexicalChildKey !== undefined && !collabKeys.has(lexicalChildKey)) {
|
|
834
|
-
const nodeToRemove =
|
|
888
|
+
const nodeToRemove = lexical.$getNodeByKeyOrThrow(lexicalChildKey);
|
|
835
889
|
removeFromParent(nodeToRemove);
|
|
836
890
|
i--;
|
|
837
891
|
prevIndex++;
|
|
@@ -872,7 +926,7 @@ class CollabElementNode {
|
|
|
872
926
|
const lexicalChildKey = prevLexicalChildrenKeys[i];
|
|
873
927
|
if (!visitedKeys.has(lexicalChildKey)) {
|
|
874
928
|
// Remove
|
|
875
|
-
const lexicalChildNode =
|
|
929
|
+
const lexicalChildNode = lexical.$getNodeByKeyOrThrow(lexicalChildKey);
|
|
876
930
|
const collabNode = binding.collabNodeMap.get(lexicalChildKey);
|
|
877
931
|
if (collabNode !== undefined) {
|
|
878
932
|
collabNode.destroy(binding);
|
|
@@ -887,7 +941,7 @@ class CollabElementNode {
|
|
|
887
941
|
_syncChildFromLexical(binding, index, key, prevNodeMap, dirtyElements, dirtyLeaves) {
|
|
888
942
|
const childCollabNode = this._children[index];
|
|
889
943
|
// Update
|
|
890
|
-
const nextChildNode =
|
|
944
|
+
const nextChildNode = lexical.$getNodeByKeyOrThrow(key);
|
|
891
945
|
if (childCollabNode instanceof CollabElementNode && lexical.$isElementNode(nextChildNode)) {
|
|
892
946
|
childCollabNode.syncPropertiesFromLexical(binding, nextChildNode, prevNodeMap);
|
|
893
947
|
childCollabNode.syncChildrenFromLexical(binding, nextChildNode, prevNodeMap, dirtyElements, dirtyLeaves);
|
|
@@ -931,7 +985,7 @@ class CollabElementNode {
|
|
|
931
985
|
prevIndex++;
|
|
932
986
|
} else {
|
|
933
987
|
// Create or replace
|
|
934
|
-
const nextChildNode =
|
|
988
|
+
const nextChildNode = lexical.$getNodeByKeyOrThrow(nextKey);
|
|
935
989
|
const collabNode = $createCollabNodeFromLexicalNode(binding, nextChildNode, this);
|
|
936
990
|
collabNodeMap.set(nextKey, collabNode);
|
|
937
991
|
if (prevHasNextKey) {
|
|
@@ -950,7 +1004,7 @@ class CollabElementNode {
|
|
|
950
1004
|
if (appendNewChildren && !removeOldChildren) {
|
|
951
1005
|
for (; nextIndex <= nextEndIndex; ++nextIndex) {
|
|
952
1006
|
const key = nextChildren[nextIndex];
|
|
953
|
-
const nextChildNode =
|
|
1007
|
+
const nextChildNode = lexical.$getNodeByKeyOrThrow(key);
|
|
954
1008
|
const collabNode = $createCollabNodeFromLexicalNode(binding, nextChildNode, this);
|
|
955
1009
|
this.append(collabNode);
|
|
956
1010
|
collabNodeMap.set(key, collabNode);
|
|
@@ -1454,14 +1508,39 @@ function syncLexicalSelectionToYjs(binding, provider, prevSelection, nextSelecti
|
|
|
1454
1508
|
*/
|
|
1455
1509
|
|
|
1456
1510
|
|
|
1511
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1512
|
+
function $syncStateEvent(binding, event) {
|
|
1513
|
+
const {
|
|
1514
|
+
target
|
|
1515
|
+
} = event;
|
|
1516
|
+
if (!(target._item && target._item.parentSub === '__state' && getNodeTypeFromSharedType(target) === undefined && (target.parent instanceof yjs.XmlText || target.parent instanceof yjs.XmlElement || target.parent instanceof yjs.Map))) {
|
|
1517
|
+
// TODO there might be a case to handle in here when a YMap
|
|
1518
|
+
// is used as a value of __state? It would probably be desirable
|
|
1519
|
+
// to mark the node as dirty when that happens.
|
|
1520
|
+
return false;
|
|
1521
|
+
}
|
|
1522
|
+
const collabNode = $getOrInitCollabNodeFromSharedType(binding, target.parent);
|
|
1523
|
+
const node = collabNode.getNode();
|
|
1524
|
+
if (node) {
|
|
1525
|
+
const state = lexical.$getWritableNodeState(node.getWritable());
|
|
1526
|
+
for (const k of event.keysChanged) {
|
|
1527
|
+
state.updateFromUnknown(k, target.get(k));
|
|
1528
|
+
}
|
|
1529
|
+
}
|
|
1530
|
+
return true;
|
|
1531
|
+
}
|
|
1532
|
+
|
|
1457
1533
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1458
1534
|
function $syncEvent(binding, event) {
|
|
1535
|
+
if (event instanceof yjs.YMapEvent && $syncStateEvent(binding, event)) {
|
|
1536
|
+
return;
|
|
1537
|
+
}
|
|
1459
1538
|
const {
|
|
1460
1539
|
target
|
|
1461
1540
|
} = event;
|
|
1462
1541
|
const collabNode = $getOrInitCollabNodeFromSharedType(binding, target);
|
|
1463
1542
|
if (collabNode instanceof CollabElementNode && event instanceof yjs.YTextEvent) {
|
|
1464
|
-
// @ts-expect-error We need to access the private property of the class
|
|
1543
|
+
// @ts-expect-error We need to access the private childListChanged property of the class
|
|
1465
1544
|
const {
|
|
1466
1545
|
keysChanged,
|
|
1467
1546
|
childListChanged,
|
package/LexicalYjs.dev.mjs
CHANGED
|
@@ -6,8 +6,8 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import { $getNodeByKey, $isLineBreakNode, $isTextNode, $getSelection, $isRangeSelection, $isElementNode, $isDecoratorNode, createEditor, $getRoot, $isRootNode, $createParagraphNode, createCommand } from 'lexical';
|
|
10
|
-
import { XmlText, Map as Map$1, XmlElement, Doc, createAbsolutePositionFromRelativePosition, createRelativePositionFromTypeIndex, compareRelativePositions,
|
|
9
|
+
import { $getNodeByKey, $isLineBreakNode, $isTextNode, $getSelection, $isRangeSelection, $isElementNode, $isDecoratorNode, createEditor, $getWritableNodeState, $getRoot, $isRootNode, $getNodeByKeyOrThrow, $createParagraphNode, createCommand } from 'lexical';
|
|
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';
|
|
13
13
|
|
|
@@ -175,7 +175,7 @@ class CollabTextNode {
|
|
|
175
175
|
if (!(lexicalNode !== null)) {
|
|
176
176
|
throw Error(`syncPropertiesAndTextFromYjs: could not find decorator node`);
|
|
177
177
|
}
|
|
178
|
-
syncPropertiesFromYjs(binding, this._map, lexicalNode, keysChanged);
|
|
178
|
+
$syncPropertiesFromYjs(binding, this._map, lexicalNode, keysChanged);
|
|
179
179
|
const collabText = this._text;
|
|
180
180
|
if (lexicalNode.__text !== collabText) {
|
|
181
181
|
const writable = lexicalNode.getWritable();
|
|
@@ -201,12 +201,12 @@ function $createCollabTextNode(map, text, parent, type) {
|
|
|
201
201
|
*
|
|
202
202
|
*/
|
|
203
203
|
|
|
204
|
-
const baseExcludedProperties = new Set(['__key', '__parent', '__next', '__prev']);
|
|
204
|
+
const baseExcludedProperties = new Set(['__key', '__parent', '__next', '__prev', '__state']);
|
|
205
205
|
const elementExcludedProperties = new Set(['__first', '__last', '__size']);
|
|
206
206
|
const rootExcludedProperties = new Set(['__cachedText']);
|
|
207
207
|
const textExcludedProperties = new Set(['__text']);
|
|
208
208
|
function isExcludedProperty(name, node, binding) {
|
|
209
|
-
if (baseExcludedProperties.has(name)) {
|
|
209
|
+
if (baseExcludedProperties.has(name) || typeof node[name] === 'function') {
|
|
210
210
|
return true;
|
|
211
211
|
}
|
|
212
212
|
if ($isTextNode(node)) {
|
|
@@ -222,13 +222,6 @@ function isExcludedProperty(name, node, binding) {
|
|
|
222
222
|
const excludedProperties = binding.excludedProperties.get(nodeKlass);
|
|
223
223
|
return excludedProperties != null && excludedProperties.has(name);
|
|
224
224
|
}
|
|
225
|
-
function $getNodeByKeyOrThrow(key) {
|
|
226
|
-
const node = $getNodeByKey(key);
|
|
227
|
-
if (!(node !== null)) {
|
|
228
|
-
throw Error(`could not find node by key`);
|
|
229
|
-
}
|
|
230
|
-
return node;
|
|
231
|
-
}
|
|
232
225
|
function $createCollabNodeFromLexicalNode(binding, lexicalNode, parent) {
|
|
233
226
|
const nodeType = lexicalNode.__type;
|
|
234
227
|
let collabNode;
|
|
@@ -259,8 +252,8 @@ function $createCollabNodeFromLexicalNode(binding, lexicalNode, parent) {
|
|
|
259
252
|
return collabNode;
|
|
260
253
|
}
|
|
261
254
|
function getNodeTypeFromSharedType(sharedType) {
|
|
262
|
-
const type = sharedType
|
|
263
|
-
if (!(type
|
|
255
|
+
const type = sharedTypeGet(sharedType, '__type');
|
|
256
|
+
if (!(typeof type === 'string' || typeof type === 'undefined')) {
|
|
264
257
|
throw Error(`Expected shared type to include type attribute`);
|
|
265
258
|
}
|
|
266
259
|
return type;
|
|
@@ -270,6 +263,9 @@ function $getOrInitCollabNodeFromSharedType(binding, sharedType, parent) {
|
|
|
270
263
|
if (collabNode === undefined) {
|
|
271
264
|
const registeredNodes = binding.editor._nodes;
|
|
272
265
|
const type = getNodeTypeFromSharedType(sharedType);
|
|
266
|
+
if (!(typeof type === 'string')) {
|
|
267
|
+
throw Error(`Expected shared type to include type attribute`);
|
|
268
|
+
}
|
|
273
269
|
const nodeInfo = registeredNodes.get(type);
|
|
274
270
|
if (!(nodeInfo !== undefined)) {
|
|
275
271
|
throw Error(`Node ${type} is not registered`);
|
|
@@ -315,17 +311,23 @@ function createLexicalNodeFromCollabNode(binding, collabNode, parentKey) {
|
|
|
315
311
|
binding.collabNodeMap.set(lexicalNode.__key, collabNode);
|
|
316
312
|
return lexicalNode;
|
|
317
313
|
}
|
|
318
|
-
function syncPropertiesFromYjs(binding, sharedType, lexicalNode, keysChanged) {
|
|
314
|
+
function $syncPropertiesFromYjs(binding, sharedType, lexicalNode, keysChanged) {
|
|
319
315
|
const properties = keysChanged === null ? sharedType instanceof Map$1 ? Array.from(sharedType.keys()) : Object.keys(sharedType.getAttributes()) : Array.from(keysChanged);
|
|
320
316
|
let writableNode;
|
|
321
317
|
for (let i = 0; i < properties.length; i++) {
|
|
322
318
|
const property = properties[i];
|
|
323
319
|
if (isExcludedProperty(property, lexicalNode, binding)) {
|
|
320
|
+
if (property === '__state') {
|
|
321
|
+
if (!writableNode) {
|
|
322
|
+
writableNode = lexicalNode.getWritable();
|
|
323
|
+
}
|
|
324
|
+
$syncNodeStateToLexical(binding, sharedType, writableNode);
|
|
325
|
+
}
|
|
324
326
|
continue;
|
|
325
327
|
}
|
|
326
328
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
327
329
|
const prevValue = lexicalNode[property];
|
|
328
|
-
let nextValue = sharedType
|
|
330
|
+
let nextValue = sharedTypeGet(sharedType, property);
|
|
329
331
|
if (prevValue !== nextValue) {
|
|
330
332
|
if (nextValue instanceof Doc) {
|
|
331
333
|
const yjsDocMap = binding.docMap;
|
|
@@ -341,10 +343,65 @@ function syncPropertiesFromYjs(binding, sharedType, lexicalNode, keysChanged) {
|
|
|
341
343
|
if (writableNode === undefined) {
|
|
342
344
|
writableNode = lexicalNode.getWritable();
|
|
343
345
|
}
|
|
346
|
+
|
|
347
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
344
348
|
writableNode[property] = nextValue;
|
|
345
349
|
}
|
|
346
350
|
}
|
|
347
351
|
}
|
|
352
|
+
function sharedTypeGet(sharedType, property) {
|
|
353
|
+
if (sharedType instanceof Map$1) {
|
|
354
|
+
return sharedType.get(property);
|
|
355
|
+
} else {
|
|
356
|
+
return sharedType.getAttribute(property);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
function sharedTypeSet(sharedType, property, nextValue) {
|
|
360
|
+
if (sharedType instanceof Map$1) {
|
|
361
|
+
sharedType.set(property, nextValue);
|
|
362
|
+
} else {
|
|
363
|
+
sharedType.setAttribute(property, nextValue);
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
function $syncNodeStateToLexical(binding, sharedType, lexicalNode) {
|
|
367
|
+
const existingState = sharedTypeGet(sharedType, '__state');
|
|
368
|
+
if (!(existingState instanceof Map$1)) {
|
|
369
|
+
return;
|
|
370
|
+
}
|
|
371
|
+
// This should only called when creating the node initially,
|
|
372
|
+
// incremental updates to state come in through YMapEvent
|
|
373
|
+
// with the __state as the target.
|
|
374
|
+
$getWritableNodeState(lexicalNode).updateFromJSON(existingState.toJSON());
|
|
375
|
+
}
|
|
376
|
+
function syncNodeStateFromLexical(binding, sharedType, prevLexicalNode, nextLexicalNode) {
|
|
377
|
+
const nextState = nextLexicalNode.__state;
|
|
378
|
+
const existingState = sharedTypeGet(sharedType, '__state');
|
|
379
|
+
if (!nextState) {
|
|
380
|
+
return;
|
|
381
|
+
}
|
|
382
|
+
const [unknown, known] = nextState.getInternalState();
|
|
383
|
+
const prevState = prevLexicalNode && prevLexicalNode.__state;
|
|
384
|
+
const stateMap = existingState instanceof Map$1 ? existingState : new Map$1();
|
|
385
|
+
if (prevState === nextState) {
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
const [prevUnknown, prevKnown] = prevState && stateMap.doc ? prevState.getInternalState() : [undefined, new Map()];
|
|
389
|
+
if (unknown) {
|
|
390
|
+
for (const [k, v] of Object.entries(unknown)) {
|
|
391
|
+
if (prevUnknown && v !== prevUnknown[k]) {
|
|
392
|
+
stateMap.set(k, v);
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
for (const [stateConfig, v] of known) {
|
|
397
|
+
if (prevKnown.get(stateConfig) !== v) {
|
|
398
|
+
stateMap.set(stateConfig.key, stateConfig.unparse(v));
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
if (!existingState) {
|
|
402
|
+
sharedTypeSet(sharedType, '__state', stateMap);
|
|
403
|
+
}
|
|
404
|
+
}
|
|
348
405
|
function syncPropertiesFromLexical(binding, sharedType, prevLexicalNode, nextLexicalNode) {
|
|
349
406
|
const type = nextLexicalNode.__type;
|
|
350
407
|
const nodeProperties = binding.nodeProperties;
|
|
@@ -356,6 +413,7 @@ function syncPropertiesFromLexical(binding, sharedType, prevLexicalNode, nextLex
|
|
|
356
413
|
nodeProperties.set(type, properties);
|
|
357
414
|
}
|
|
358
415
|
const EditorClass = binding.editor.constructor;
|
|
416
|
+
syncNodeStateFromLexical(binding, sharedType, prevLexicalNode, nextLexicalNode);
|
|
359
417
|
for (let i = 0; i < properties.length; i++) {
|
|
360
418
|
const property = properties[i];
|
|
361
419
|
const prevValue =
|
|
@@ -384,11 +442,7 @@ function syncPropertiesFromLexical(binding, sharedType, prevLexicalNode, nextLex
|
|
|
384
442
|
nextLexicalNode.markDirty();
|
|
385
443
|
});
|
|
386
444
|
}
|
|
387
|
-
|
|
388
|
-
sharedType.set(property, nextValue);
|
|
389
|
-
} else {
|
|
390
|
-
sharedType.setAttribute(property, nextValue);
|
|
391
|
-
}
|
|
445
|
+
sharedTypeSet(sharedType, property, nextValue);
|
|
392
446
|
}
|
|
393
447
|
}
|
|
394
448
|
}
|
|
@@ -599,7 +653,7 @@ class CollabDecoratorNode {
|
|
|
599
653
|
throw Error(`syncPropertiesFromYjs: could not find decorator node`);
|
|
600
654
|
}
|
|
601
655
|
const xmlElem = this._xmlElem;
|
|
602
|
-
syncPropertiesFromYjs(binding, xmlElem, lexicalNode, keysChanged);
|
|
656
|
+
$syncPropertiesFromYjs(binding, xmlElem, lexicalNode, keysChanged);
|
|
603
657
|
}
|
|
604
658
|
destroy(binding) {
|
|
605
659
|
const collabNodeMap = binding.collabNodeMap;
|
|
@@ -666,7 +720,7 @@ class CollabElementNode {
|
|
|
666
720
|
if (!(lexicalNode !== null)) {
|
|
667
721
|
throw Error(`syncPropertiesFromYjs: could not find element node`);
|
|
668
722
|
}
|
|
669
|
-
syncPropertiesFromYjs(binding, this._xmlText, lexicalNode, keysChanged);
|
|
723
|
+
$syncPropertiesFromYjs(binding, this._xmlText, lexicalNode, keysChanged);
|
|
670
724
|
}
|
|
671
725
|
applyChildrenYjsDelta(binding, deltas) {
|
|
672
726
|
const children = this._children;
|
|
@@ -1452,14 +1506,39 @@ function syncLexicalSelectionToYjs(binding, provider, prevSelection, nextSelecti
|
|
|
1452
1506
|
*/
|
|
1453
1507
|
|
|
1454
1508
|
|
|
1509
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1510
|
+
function $syncStateEvent(binding, event) {
|
|
1511
|
+
const {
|
|
1512
|
+
target
|
|
1513
|
+
} = event;
|
|
1514
|
+
if (!(target._item && target._item.parentSub === '__state' && getNodeTypeFromSharedType(target) === undefined && (target.parent instanceof XmlText || target.parent instanceof XmlElement || target.parent instanceof Map$1))) {
|
|
1515
|
+
// TODO there might be a case to handle in here when a YMap
|
|
1516
|
+
// is used as a value of __state? It would probably be desirable
|
|
1517
|
+
// to mark the node as dirty when that happens.
|
|
1518
|
+
return false;
|
|
1519
|
+
}
|
|
1520
|
+
const collabNode = $getOrInitCollabNodeFromSharedType(binding, target.parent);
|
|
1521
|
+
const node = collabNode.getNode();
|
|
1522
|
+
if (node) {
|
|
1523
|
+
const state = $getWritableNodeState(node.getWritable());
|
|
1524
|
+
for (const k of event.keysChanged) {
|
|
1525
|
+
state.updateFromUnknown(k, target.get(k));
|
|
1526
|
+
}
|
|
1527
|
+
}
|
|
1528
|
+
return true;
|
|
1529
|
+
}
|
|
1530
|
+
|
|
1455
1531
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1456
1532
|
function $syncEvent(binding, event) {
|
|
1533
|
+
if (event instanceof YMapEvent && $syncStateEvent(binding, event)) {
|
|
1534
|
+
return;
|
|
1535
|
+
}
|
|
1457
1536
|
const {
|
|
1458
1537
|
target
|
|
1459
1538
|
} = event;
|
|
1460
1539
|
const collabNode = $getOrInitCollabNodeFromSharedType(binding, target);
|
|
1461
1540
|
if (collabNode instanceof CollabElementNode && event instanceof YTextEvent) {
|
|
1462
|
-
// @ts-expect-error We need to access the private property of the class
|
|
1541
|
+
// @ts-expect-error We need to access the private childListChanged property of the class
|
|
1463
1542
|
const {
|
|
1464
1543
|
keysChanged,
|
|
1465
1544
|
childListChanged,
|
package/LexicalYjs.prod.js
CHANGED
|
@@ -6,4 +6,4 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
"use strict";var e=require("lexical"),t=require("yjs"),n=require("@lexical/offset"),o=require("@lexical/selection");function s(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var l=s((function(e){const t=new URL("https://lexical.dev/docs/error"),n=new URLSearchParams;n.append("code",e);for(let e=1;e<arguments.length;e++)n.append("v",arguments[e]);throw t.search=n.toString(),Error(`Minified Lexical error #${e}; visit ${t.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}));class i{constructor(e,t){this._key="",this._map=e,this._parent=t,this._type="linebreak"}getNode(){const t=e.$getNodeByKey(this._key);return e.$isLineBreakNode(t)?t:null}getKey(){return this._key}getSharedType(){return this._map}getType(){return this._type}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}destroy(e){e.collabNodeMap.delete(this._key)}}function r(e,t){const n=new i(e,t);return e._collabNode=n,n}class c{constructor(e,t,n,o){this._key="",this._map=e,this._parent=n,this._text=t,this._type=o,this._normalized=!1}getPrevNode(t){if(null===t)return null;const n=t.get(this._key);return e.$isTextNode(n)?n:null}getNode(){const t=e.$getNodeByKey(this._key);return e.$isTextNode(t)?t:null}getSharedType(){return this._map}getType(){return this._type}getKey(){return this._key}getSize(){return this._text.length+(this._normalized?0:1)}getOffset(){return this._parent.getChildOffset(this)}spliceText(e,t,n){const o=this._parent._xmlText,s=this.getOffset()+1+e;0!==t&&o.delete(s,t),""!==n&&o.insert(s,n)}syncPropertiesAndTextFromLexical(t,n,o){const s=this.getPrevNode(o),l=n.__text;if(N(t,this._map,s,n),null!==s){const t=s.__text;if(t!==l){!function(t,n,o,s){const l=e.$getSelection();let i=s.length;if(e.$isRangeSelection(l)&&l.isCollapsed()){const e=l.anchor;e.key===n&&(i=e.offset)}const r=function(e,t,n){const o=e.length,s=t.length;let l=0,i=0;for(;l<o&&l<s&&e[l]===t[l]&&l<n;)l++;for(;i+l<o&&i+l<s&&e[o-i-1]===t[s-i-1];)i++;for(;i+l<o&&i+l<s&&e[l]===t[l];)l++;return{index:l,insert:t.slice(l,s-i),remove:o-l-i}}(o,s,i);t.spliceText(r.index,r.remove,r.insert)}(this,n.__key,t,l),this._text=l}}}syncPropertiesAndTextFromYjs(e,t){const n=this.getNode();null===n&&l(84),m(e,this._map,n,t);const o=this._text;if(n.__text!==o){n.getWritable().__text=o}}destroy(e){e.collabNodeMap.delete(this._key)}}function a(e,t,n,o){const s=new c(e,t,n,o);return e._collabNode=s,s}const f=new Set(["__key","__parent","__next","__prev"]),u=new Set(["__first","__last","__size"]),d=new Set(["__cachedText"]),_=new Set(["__text"]);function h(t,n,o){if(f.has(t))return!0;if(e.$isTextNode(n)){if(_.has(t))return!0}else if(e.$isElementNode(n)&&(u.has(t)||e.$isRootNode(n)&&d.has(t)))return!0;const s=n.constructor,l=o.excludedProperties.get(s);return null!=l&&l.has(t)}function g(t){const n=e.$getNodeByKey(t);return null===n&&l(85),n}function p(n,o,s){const i=o.__type;let c;if(e.$isElementNode(o)){c=w(new t.XmlText,s,i),c.syncPropertiesFromLexical(n,o,null),c.syncChildrenFromLexical(n,o,null,null,null)}else if(e.$isTextNode(o)){c=a(new t.Map,o.__text,s,i),c.syncPropertiesAndTextFromLexical(n,o,null)}else if(e.$isLineBreakNode(o)){const e=new t.Map;e.set("__type","linebreak"),c=r(e,s)}else if(e.$isDecoratorNode(o)){c=S(new t.XmlElement,s,i),c.syncPropertiesFromLexical(n,o,null)}else l(86);return c._key=o.__key,c}function y(e,n,o){const s=n._collabNode;if(void 0===s){const s=e.editor._nodes,i=function(e){const n=e instanceof t.Map?e.get("__type"):e.getAttribute("__type");return null==n&&l(87),n}(n);void 0===s.get(i)&&l(88,i);const c=n.parent,f=void 0===o&&null!==c?y(e,c):o||null;if(f instanceof P||l(89),n instanceof t.XmlText)return w(n,f,i);if(n instanceof t.Map)return"linebreak"===i?r(n,f):a(n,"",f,i);if(n instanceof t.XmlElement)return S(n,f,i)}return s}function x(e,t,n){const o=t.getType(),s=e.editor._nodes.get(o);void 0===s&&l(88,o);const i=new s.klass;if(i.__parent=n,t._key=i.__key,t instanceof P){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof c?t.syncPropertiesAndTextFromYjs(e,null):t instanceof v&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(i.__key,t),i}function m(n,o,s,l){const i=null===l?o instanceof t.Map?Array.from(o.keys()):Object.keys(o.getAttributes()):Array.from(l);let r;for(let l=0;l<i.length;l++){const c=i[l];if(h(c,s,n))continue;const a=s[c];let f=o instanceof t.Map?o.get(c):o.getAttribute(c);if(a!==f){if(f instanceof t.Doc){const o=n.docMap;a instanceof t.Doc&&o.delete(a.guid);const s=e.createEditor(),l=f.guid;s._key=l,o.set(l,f),f=s}void 0===r&&(r=s.getWritable()),r[c]=f}}}function N(e,n,o,s){const l=s.__type,i=e.nodeProperties;let r=i.get(l);void 0===r&&(r=Object.keys(s).filter((t=>!h(t,s,e))),i.set(l,r));const c=e.editor.constructor;for(let l=0;l<r.length;l++){const i=r[l],a=null===o?void 0:o[i];let f=s[i];if(a!==f){if(f instanceof c){const n=e.docMap;let o;if(a instanceof c){const e=a._key;o=n.get(e),n.delete(e)}const l=o||new t.Doc,i=l.guid;f._key=i,n.set(i,l),f=l,e.editor.update((()=>{s.markDirty()}))}n instanceof t.Map?n.set(i,f):n.setAttribute(i,f)}}}function b(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function k(e,t,n){let o=0,s=0;const l=e._children,i=l.length;for(;s<i;s++){const e=l[s],r=o;o+=e.getSize();if((n?o>=t:o>t)&&e instanceof c){let n=t-r-1;n<0&&(n=0);return{length:o-t,node:e,nodeIndex:s,offset:n}}if(o>t)return{length:0,node:e,nodeIndex:s,offset:r};if(s===i-1)return{length:0,node:null,nodeIndex:s+1,offset:r+1}}return{length:0,node:null,nodeIndex:0,offset:0}}function C(t){const n=t.anchor,o=t.focus;let s=!1;try{const t=n.getNode(),l=o.getNode();(!t.isAttached()||!l.isAttached()||e.$isTextNode(t)&&n.offset>t.getTextContentSize()||e.$isTextNode(l)&&o.offset>l.getTextContentSize())&&(s=!0)}catch(e){s=!0}return s}function T(e){const t=e.getParent();if(null!==t){const n=e.getWritable(),o=t.getWritable(),s=e.getPreviousSibling(),l=e.getNextSibling();if(null===s)if(null!==l){const e=l.getWritable();o.__first=l.__key,e.__prev=null}else o.__first=null;else{const e=s.getWritable();if(null!==l){const t=l.getWritable();t.__prev=e.__key,e.__next=t.__key}else e.__next=null;n.__prev=null}if(null===l)if(null!==s){const e=s.getWritable();o.__last=s.__key,e.__next=null}else o.__last=null;else{const e=l.getWritable();if(null!==s){const t=s.getWritable();t.__next=e.__key,e.__prev=t.__key}else e.__prev=null;n.__next=null}o.__size--,n.__parent=null}}function $(t,n){const o=n._nodeMap.get(t);if(!o)return void e.$getRoot().selectStart();const s=o.__prev;let l=null;s&&(l=e.$getNodeByKey(s)),null===l&&null!==o.__parent&&(l=e.$getNodeByKey(o.__parent)),null!==l?null!==l&&l.isAttached()?l.selectEnd():$(l.__key,n):e.$getRoot().selectStart()}class v{constructor(e,t,n){this._key="",this._xmlElem=e,this._parent=t,this._type=n}getPrevNode(t){if(null===t)return null;const n=t.get(this._key);return e.$isDecoratorNode(n)?n:null}getNode(){const t=e.$getNodeByKey(this._key);return e.$isDecoratorNode(t)?t:null}getSharedType(){return this._xmlElem}getType(){return this._type}getKey(){return this._key}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}syncPropertiesFromLexical(e,t,n){const o=this.getPrevNode(n);N(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&l(83);m(e,this._xmlElem,n,t)}destroy(e){e.collabNodeMap.delete(this._key)}}function S(e,t,n){const o=new v(e,t,n);return e._collabNode=o,o}class P{constructor(e,t,n){this._key="",this._children=[],this._xmlText=e,this._type=n,this._parent=t}getPrevNode(t){if(null===t)return null;const n=t.get(this._key);return e.$isElementNode(n)?n:null}getNode(){const t=e.$getNodeByKey(this._key);return e.$isElementNode(t)?t:null}getSharedType(){return this._xmlText}getType(){return this._type}getKey(){return this._key}isEmpty(){return 0===this._children.length}getSize(){return 1}getOffset(){const e=this._parent;return null===e&&l(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&l(91),m(e,this._xmlText,n,t)}applyChildrenYjsDelta(e,t){const n=this._children;let o=0,s=null;for(let l=0;l<t.length;l++){const r=t[l],a=r.insert,f=r.delete;if(null!=r.retain)o+=r.retain;else if("number"==typeof f){let e=f;for(;e>0;){const{node:t,nodeIndex:s,offset:l,length:r}=k(this,o,!1);if(t instanceof P||t instanceof i||t instanceof v)n.splice(s,1),e-=1;else{if(!(t instanceof c))break;{const o=Math.min(e,r),i=0!==s?n[s-1]:null,a=t.getSize();if(0===l&&r===a){n.splice(s,1);const e=b(t._text,l,o-1,"");e.length>0&&(i instanceof c?i._text+=e:this._xmlText.delete(l,e.length))}else t._text=b(t._text,l,o,"");e-=o}}}}else{if(null==a)throw new Error("Unexpected delta format");if("string"==typeof a){const{node:e,offset:t}=k(this,o,!0);e instanceof c?e._text=b(e._text,t,0,a):this._xmlText.delete(t,a.length),o+=a.length}else{const t=a,{node:l,nodeIndex:i,length:r}=k(this,o,!1),f=y(e,t,this);if(l instanceof c&&r>0&&r<l._text.length){const e=l._text,t=e.length-r;l._text=b(e,t,r,""),n.splice(i+1,0,f),s=b(e,0,t,"")}else n.splice(i,0,f);null!==s&&f instanceof c&&(f._text=s+f._text,s=null),o+=1}}}}syncChildrenFromYjs(t){const o=this.getNode();null===o&&l(92);const s=o.__key,r=n.$createChildrenArray(o,null),a=r.length,f=this._children,u=f.length,d=t.collabNodeMap,_=new Set;let h,p,y=0,m=null;u!==a&&(p=o.getWritable());for(let n=0;n<u;n++){const a=r[y],N=f[n],b=N.getNode(),k=N._key;if(null!==b&&a===k){const n=e.$isTextNode(b);if(_.add(a),n)if(N._key=a,N instanceof P){const e=N._xmlText;N.syncPropertiesFromYjs(t,null),N.applyChildrenYjsDelta(t,e.toDelta()),N.syncChildrenFromYjs(t)}else N instanceof c?N.syncPropertiesAndTextFromYjs(t,null):N instanceof v?N.syncPropertiesFromYjs(t,null):N instanceof i||l(93);m=b,y++}else{if(void 0===h){h=new Set;for(let e=0;e<u;e++){const t=f[e]._key;""!==t&&h.add(t)}}if(null!==b&&void 0!==a&&!h.has(a)){T(g(a)),n--,y++;continue}p=o.getWritable();const e=x(t,N,s),l=e.__key;if(d.set(l,N),null===m){const t=p.getFirstChild();if(p.__first=l,null!==t){const n=t.getWritable();n.__prev=l,e.__next=n.__key}}else{const t=m.getWritable(),n=m.getNextSibling();if(t.__next=l,e.__prev=m.__key,null!==n){const t=n.getWritable();t.__prev=l,e.__next=t.__key}}n===u-1&&(p.__last=l),p.__size++,m=e}}for(let e=0;e<a;e++){const n=r[e];if(!_.has(n)){const e=g(n),o=t.collabNodeMap.get(n);void 0!==o&&o.destroy(t),T(e)}}}syncPropertiesFromLexical(e,t,n){N(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(t,n,o,s,l,i){const r=this._children[n],a=g(o);r instanceof P&&e.$isElementNode(a)?(r.syncPropertiesFromLexical(t,a,s),r.syncChildrenFromLexical(t,a,s,l,i)):r instanceof c&&e.$isTextNode(a)?r.syncPropertiesAndTextFromLexical(t,a,s):r instanceof v&&e.$isDecoratorNode(a)&&r.syncPropertiesFromLexical(t,a,s)}syncChildrenFromLexical(e,t,o,s,l){const i=this.getPrevNode(o),r=null===i?[]:n.$createChildrenArray(i,o),c=n.$createChildrenArray(t,null),a=r.length-1,f=c.length-1,u=e.collabNodeMap;let d,_,h=0,y=0;for(;h<=a&&y<=f;){const t=r[h],n=c[y];if(t===n)this._syncChildFromLexical(e,y,n,o,s,l),h++,y++;else{void 0===d&&(d=new Set(r)),void 0===_&&(_=new Set(c));const o=_.has(t),s=d.has(n);if(o){const t=p(e,g(n),this);u.set(n,t),s?(this.splice(e,y,1,t),h++,y++):(this.splice(e,y,0,t),y++)}else this.splice(e,y,1),h++}}const x=h>a,m=y>f;if(x&&!m)for(;y<=f;++y){const t=c[y],n=p(e,g(t),this);this.append(n),u.set(t,n)}else if(m&&!x)for(let t=this._children.length-1;t>=y;t--)this.splice(e,t,1)}append(e){const t=this._xmlText,n=this._children,o=n[n.length-1],s=void 0!==o?o.getOffset()+o.getSize():0;if(e instanceof P)t.insertEmbed(s,e._xmlText);else if(e instanceof c){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof i?t.insertEmbed(s,e._map):e instanceof v&&t.insertEmbed(s,e._xmlElem);this._children.push(e)}splice(e,t,n,o){const s=this._children,r=s[t];if(void 0===r)return void 0===o&&l(94),void this.append(o);const a=r.getOffset();-1===a&&l(95);const f=this._xmlText;if(0!==n&&f.delete(a,r.getSize()),o instanceof P)f.insertEmbed(a,o._xmlText);else if(o instanceof c){const e=o._map;null===e.parent&&f.insertEmbed(a,e),f.insert(a+1,o._text)}else o instanceof i?f.insertEmbed(a,o._map):o instanceof v&&f.insertEmbed(a,o._xmlElem);if(0!==n){const o=s.slice(t,t+n);for(let t=0;t<o.length;t++)o[t].destroy(e)}void 0!==o?s.splice(t,n,o):s.splice(t,n)}getChildOffset(e){let t=0;const n=this._children;for(let o=0;o<n.length;o++){const s=n[o];if(s===e)return t;t+=s.getSize()}return-1}destroy(e){const t=e.collabNodeMap,n=this._children;for(let t=0;t<n.length;t++)n[t].destroy(e);t.delete(this._key)}}function w(e,t,n){const o=new P(e,t,n);return e._collabNode=o,o}function E(n,o){const s=o.collabNodeMap.get(n.key);if(void 0===s)return null;let i=n.offset,r=s.getSharedType();if(s instanceof c){r=s._parent._xmlText;const e=s.getOffset();if(-1===e)return null;i=e+1+i}else if(s instanceof P&&"element"===n.type){const t=n.getNode();e.$isElementNode(t)||l(184);let o=0,s=0,r=t.getFirstChild();for(;null!==r&&s++<i;)e.$isTextNode(r)?o+=r.getTextContentSize()+1:o++,r=r.getNextSibling();i=o}return t.createRelativePositionFromTypeIndex(r,i)}function M(e,n){return t.createAbsolutePositionFromRelativePosition(e,n.doc)}function F(e,n){if(null==e){if(null!=n)return!0}else if(null==n||!t.compareRelativePositions(e,n))return!0;return!1}function O(e,t){return{color:t,name:e,selection:null}}function L(e,t){const n=e.cursorsContainer;if(null!==n){const e=t.selections,o=e.length;for(let t=0;t<o;t++)n.removeChild(e[t])}}function A(e,t){const n=t.selection;null!==n&&L(e,n)}function D(e,t,n,o,s){const l=e.color,i=document.createElement("span");i.style.cssText=`position:absolute;top:0;bottom:0;right:-1px;width:1px;background-color:${l};z-index:10;`;const r=document.createElement("span");return r.textContent=e.name,r.style.cssText=`position:absolute;left:-2px;top:-16px;background-color:${l};color:#fff;line-height:12px;font-size:12px;padding:2px;font-family:Arial;font-weight:bold;white-space:nowrap;`,i.appendChild(r),{anchor:{key:t,offset:n},caret:i,color:l,focus:{key:o,offset:s},name:r,selections:[]}}function j(t,n,s,l){const i=t.editor,r=i.getRootElement(),c=t.cursorsContainer;if(null===c||null===r)return;const a=c.offsetParent;if(null===a)return;const f=a.getBoundingClientRect(),u=n.selection;if(null===s)return null===u?void 0:(n.selection=null,void L(t,u));n.selection=s;const d=s.caret,_=s.color,h=s.selections,g=s.anchor,p=s.focus,y=g.key,x=p.key,m=l.get(y),N=l.get(x);if(null==m||null==N)return;let b;if(m===N&&e.$isLineBreakNode(m)){b=[i.getElementByKey(y).getBoundingClientRect()]}else{const e=o.createDOMRange(i,m,g.offset,N,p.offset);if(null===e)return;b=o.createRectsFromDOMRange(i,e)}const k=h.length,C=b.length;for(let e=0;e<C;e++){const t=b[e];let n=h[e];if(void 0===n){n=document.createElement("span"),h[e]=n;const t=document.createElement("span");n.appendChild(t),c.appendChild(n)}const o=`position:absolute;top:${t.top-f.top}px;left:${t.left-f.left}px;height:${t.height}px;width:${t.width}px;pointer-events:none;z-index:5;`;n.style.cssText=o,n.firstChild.style.cssText=`${o}left:0;top:0;background-color:${_};opacity:0.3;`,e===C-1&&d.parentNode!==n&&n.appendChild(d)}for(let e=k-1;e>=C;e--){const t=h[e];c.removeChild(t),h.pop()}}function z(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,l=0,i=null,r=0;if(null!==n&&null!==o){const t=M(n,e),c=M(o,e);null!==t&&null!==c&&([s,l]=K(t.type,t.index),[i,r]=K(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:l,focusCollabNode:i,focusOffset:r}}function Y(t,n){const o=n.awareness.getLocalState();if(null===o)return;const{anchorCollabNode:s,anchorOffset:l,focusCollabNode:i,focusOffset:r}=z(t,o);if(null!==s&&null!==i){const t=s.getKey(),n=i.getKey(),o=e.$getSelection();if(!e.$isRangeSelection(o))return;R(o.anchor,t,l),R(o.focus,n,r)}}function R(t,n,o){if(t.key!==n||t.offset!==o){let s=e.$getNodeByKey(n);if(null!==s&&!e.$isElementNode(s)&&!e.$isTextNode(s)){const e=s.getParentOrThrow();n=e.getKey(),o=s.getIndexWithinParent(),s=e}t.set(n,o,e.$isElementNode(s)?"element":"text")}}function K(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof P){const{node:e,offset:o}=k(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function B(e,t){const n=Array.from(t.awareness.getStates()),o=e.clientID,s=e.cursors,l=e.editor._editorState._nodeMap,i=new Set;for(let t=0;t<n.length;t++){const r=n[t],[c,a]=r;if(c!==o){i.add(c);const{name:t,color:n,focusing:o}=a;let r=null,f=s.get(c);if(void 0===f&&(f=O(t,n),s.set(c,f)),o){const{anchorCollabNode:t,anchorOffset:n,focusCollabNode:o,focusOffset:s}=z(e,a);if(null!==t&&null!==o){const e=t.getKey(),l=o.getKey();if(r=f.selection,null===r)r=D(f,e,n,l,s);else{const t=r.anchor,o=r.focus;t.key=e,t.offset=n,o.key=l,o.offset=s}}}j(e,f,r,l)}}const r=Array.from(s.keys());for(let t=0;t<r.length;t++){const n=r[t];if(!i.has(n)){const t=s.get(n);void 0!==t&&(A(e,t),s.delete(n))}}}function W(t,n,o,s){const l=n.awareness,i=l.getLocalState();if(null===i)return;const{anchorPos:r,focusPos:c,name:a,color:f,focusing:u,awarenessData:d}=i;let _=null,h=null;(null!==s&&(null===r||s.is(o))||null!==o)&&(e.$isRangeSelection(s)&&(_=E(s.anchor,t),h=E(s.focus,t)),(F(r,_)||F(c,h))&&l.setLocalState({...i,anchorPos:_,awarenessData:d,color:f,focusPos:h,focusing:u,name:a}))}function I(e,n){const{target:o}=n,s=y(e,o);if(s instanceof P&&n instanceof t.YTextEvent){const{keysChanged:t,childListChanged:o,delta:l}=n;t.size>0&&s.syncPropertiesFromYjs(e,t),o&&(s.applyChildrenYjsDelta(e,l),s.syncChildrenFromYjs(e))}else if(s instanceof c&&n instanceof t.YMapEvent){const{keysChanged:t}=n;t.size>0&&s.syncPropertiesAndTextFromYjs(e,t)}else if(s instanceof v&&n instanceof t.YXmlEvent){const{attributesChanged:t}=n;t.size>0&&s.syncPropertiesFromYjs(e,t)}else l(82)}const U=e.createCommand("CONNECTED_COMMAND"),X=e.createCommand("TOGGLE_CONNECT_COMMAND");exports.CONNECTED_COMMAND=U,exports.TOGGLE_CONNECT_COMMAND=X,exports.createBinding=function(e,n,o,s,i,r){null==s&&l(81);const c=w(s.get("root",t.XmlText),null,"root");return c._key="root",{clientID:s.clientID,collabNodeMap:new Map,cursors:new Map,cursorsContainer:null,doc:s,docMap:i,editor:e,excludedProperties:r||new Map,id:o,nodeProperties:new Map,root:c}},exports.createUndoManager=function(e,n){return new t.UndoManager(n,{trackedOrigins:new Set([e,null])})},exports.getAnchorAndFocusCollabNodesForUserState=z,exports.initLocalState=function(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})},exports.setLocalStateFocus=function(e,t,n,o,s){const{awareness:l}=e;let i=l.getLocalState();null===i&&(i={anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t}),i.focusing=o,l.setLocalState(i)},exports.syncCursorPositions=B,exports.syncLexicalUpdateToYjs=function(t,n,o,s,l,i,r,a){!function(e,t){e.doc.transact(t,e)}(t,(()=>{s.read((()=>{if(a.has("collaboration")||a.has("historic"))return void(r.size>0&&function(t,n){const o=Array.from(n),s=t.collabNodeMap,l=[],i=[];for(let t=0;t<o.length;t++){const n=o[t],r=e.$getNodeByKey(n),a=s.get(n);if(a instanceof c)if(e.$isTextNode(r))l.push([a,r.__text]);else{const e=a.getOffset();if(-1===e)continue;const t=a._parent;a._normalized=!0,t._xmlText.delete(e,1),i.push(a)}}for(let e=0;e<i.length;e++){const t=i[e],n=t.getKey();s.delete(n);const o=t._parent._children,l=o.indexOf(t);o.splice(l,1)}for(let e=0;e<l.length;e++){const[t,n]=l[e];t._text=n}}(t,r));if(l.has("root")){const n=o._nodeMap,s=e.$getRoot(),r=t.root;r.syncPropertiesFromLexical(t,s,n),r.syncChildrenFromLexical(t,s,n,l,i)}const s=e.$getSelection(),f=o._selection;W(t,n,f,s)}))}))},exports.syncYjsChangesToLexical=function(t,n,o,s,l=B){const i=t.editor,r=i._editorState;o.forEach((e=>e.delta)),i.update((()=>{for(let e=0;e<o.length;e++){const n=o[e];I(t,n)}const s=e.$getSelection();if(e.$isRangeSelection(s))if(C(s)){const o=r._selection;if(e.$isRangeSelection(o)&&(Y(t,n),C(s))){$(s.anchor.key,r)}W(t,n,o,e.$getSelection())}else Y(t,n)}),{onUpdate:()=>{l(t,n),i.update((()=>{0===e.$getRoot().getChildrenSize()&&e.$getRoot().append(e.$createParagraphNode())}))},skipTransforms:!0,tag:s?"historic":"collaboration"})};
|
|
9
|
+
"use strict";var e=require("lexical"),t=require("yjs"),n=require("@lexical/offset"),o=require("@lexical/selection");function s(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var l=s((function(e){const t=new URL("https://lexical.dev/docs/error"),n=new URLSearchParams;n.append("code",e);for(let e=1;e<arguments.length;e++)n.append("v",arguments[e]);throw t.search=n.toString(),Error(`Minified Lexical error #${e}; visit ${t.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}));class i{constructor(e,t){this._key="",this._map=e,this._parent=t,this._type="linebreak"}getNode(){const t=e.$getNodeByKey(this._key);return e.$isLineBreakNode(t)?t:null}getKey(){return this._key}getSharedType(){return this._map}getType(){return this._type}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}destroy(e){e.collabNodeMap.delete(this._key)}}function r(e,t){const n=new i(e,t);return e._collabNode=n,n}class c{constructor(e,t,n,o){this._key="",this._map=e,this._parent=n,this._text=t,this._type=o,this._normalized=!1}getPrevNode(t){if(null===t)return null;const n=t.get(this._key);return e.$isTextNode(n)?n:null}getNode(){const t=e.$getNodeByKey(this._key);return e.$isTextNode(t)?t:null}getSharedType(){return this._map}getType(){return this._type}getKey(){return this._key}getSize(){return this._text.length+(this._normalized?0:1)}getOffset(){return this._parent.getChildOffset(this)}spliceText(e,t,n){const o=this._parent._xmlText,s=this.getOffset()+1+e;0!==t&&o.delete(s,t),""!==n&&o.insert(s,n)}syncPropertiesAndTextFromLexical(t,n,o){const s=this.getPrevNode(o),l=n.__text;if(T(t,this._map,s,n),null!==s){const t=s.__text;if(t!==l){!function(t,n,o,s){const l=e.$getSelection();let i=s.length;if(e.$isRangeSelection(l)&&l.isCollapsed()){const e=l.anchor;e.key===n&&(i=e.offset)}const r=function(e,t,n){const o=e.length,s=t.length;let l=0,i=0;for(;l<o&&l<s&&e[l]===t[l]&&l<n;)l++;for(;i+l<o&&i+l<s&&e[o-i-1]===t[s-i-1];)i++;for(;i+l<o&&i+l<s&&e[l]===t[l];)l++;return{index:l,insert:t.slice(l,s-i),remove:o-l-i}}(o,s,i);t.spliceText(r.index,r.remove,r.insert)}(this,n.__key,t,l),this._text=l}}}syncPropertiesAndTextFromYjs(e,t){const n=this.getNode();null===n&&l(84),m(e,this._map,n,t);const o=this._text;if(n.__text!==o){n.getWritable().__text=o}}destroy(e){e.collabNodeMap.delete(this._key)}}function a(e,t,n,o){const s=new c(e,t,n,o);return e._collabNode=s,s}const f=new Set(["__key","__parent","__next","__prev","__state"]),u=new Set(["__first","__last","__size"]),d=new Set(["__cachedText"]),_=new Set(["__text"]);function h(t,n,o){if(f.has(t)||"function"==typeof n[t])return!0;if(e.$isTextNode(n)){if(_.has(t))return!0}else if(e.$isElementNode(n)&&(u.has(t)||e.$isRootNode(n)&&d.has(t)))return!0;const s=n.constructor,l=o.excludedProperties.get(s);return null!=l&&l.has(t)}function g(n,o,s){const i=o.__type;let c;if(e.$isElementNode(o)){c=O(new t.XmlText,s,i),c.syncPropertiesFromLexical(n,o,null),c.syncChildrenFromLexical(n,o,null,null,null)}else if(e.$isTextNode(o)){c=a(new t.Map,o.__text,s,i),c.syncPropertiesAndTextFromLexical(n,o,null)}else if(e.$isLineBreakNode(o)){const e=new t.Map;e.set("__type","linebreak"),c=r(e,s)}else if(e.$isDecoratorNode(o)){c=E(new t.XmlElement,s,i),c.syncPropertiesFromLexical(n,o,null)}else l(86);return c._key=o.__key,c}function p(e){const t=N(e,"__type");return"string"!=typeof t&&void 0!==t&&l(87),t}function y(e,n,o){const s=n._collabNode;if(void 0===s){const s=e.editor._nodes,i=p(n);"string"!=typeof i&&l(87);void 0===s.get(i)&&l(88,i);const c=n.parent,f=void 0===o&&null!==c?y(e,c):o||null;if(f instanceof M||l(89),n instanceof t.XmlText)return O(n,f,i);if(n instanceof t.Map)return"linebreak"===i?r(n,f):a(n,"",f,i);if(n instanceof t.XmlElement)return E(n,f,i)}return s}function x(e,t,n){const o=t.getType(),s=e.editor._nodes.get(o);void 0===s&&l(88,o);const i=new s.klass;if(i.__parent=n,t._key=i.__key,t instanceof M){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof c?t.syncPropertiesAndTextFromYjs(e,null):t instanceof P&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(i.__key,t),i}function m(n,o,s,l){const i=null===l?o instanceof t.Map?Array.from(o.keys()):Object.keys(o.getAttributes()):Array.from(l);let r;for(let l=0;l<i.length;l++){const c=i[l];if(h(c,s,n)){"__state"===c&&(r||(r=s.getWritable()),k(n,o,r));continue}const a=s[c];let f=N(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(),l=f.guid;s._key=l,o.set(l,f),f=s}void 0===r&&(r=s.getWritable()),r[c]=f}}}function N(e,n){return e instanceof t.Map?e.get(n):e.getAttribute(n)}function b(e,n,o){e instanceof t.Map?e.set(n,o):e.setAttribute(n,o)}function k(n,o,s){const l=N(o,"__state");l instanceof t.Map&&e.$getWritableNodeState(s).updateFromJSON(l.toJSON())}function T(e,n,o,s){const l=s.__type,i=e.nodeProperties;let r=i.get(l);void 0===r&&(r=Object.keys(s).filter((t=>!h(t,s,e))),i.set(l,r));const c=e.editor.constructor;!function(e,n,o,s){const l=s.__state,i=N(n,"__state");if(!l)return;const[r,c]=l.getInternalState(),a=o&&o.__state,f=i instanceof t.Map?i:new t.Map;if(a===l)return;const[u,d]=a&&f.doc?a.getInternalState():[void 0,new Map];if(r)for(const[e,t]of Object.entries(r))u&&t!==u[e]&&f.set(e,t);for(const[e,t]of c)d.get(e)!==t&&f.set(e.key,e.unparse(t));i||b(n,"__state",f)}(0,n,o,s);for(let l=0;l<r.length;l++){const i=r[l],a=null===o?void 0:o[i];let f=s[i];if(a!==f){if(f instanceof c){const n=e.docMap;let o;if(a instanceof c){const e=a._key;o=n.get(e),n.delete(e)}const l=o||new t.Doc,i=l.guid;f._key=i,n.set(i,l),f=l,e.editor.update((()=>{s.markDirty()}))}b(n,i,f)}}}function $(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function C(e,t,n){let o=0,s=0;const l=e._children,i=l.length;for(;s<i;s++){const e=l[s],r=o;o+=e.getSize();if((n?o>=t:o>t)&&e instanceof c){let n=t-r-1;n<0&&(n=0);return{length:o-t,node:e,nodeIndex:s,offset:n}}if(o>t)return{length:0,node:e,nodeIndex:s,offset:r};if(s===i-1)return{length:0,node:null,nodeIndex:s+1,offset:r+1}}return{length:0,node:null,nodeIndex:0,offset:0}}function v(t){const n=t.anchor,o=t.focus;let s=!1;try{const t=n.getNode(),l=o.getNode();(!t.isAttached()||!l.isAttached()||e.$isTextNode(t)&&n.offset>t.getTextContentSize()||e.$isTextNode(l)&&o.offset>l.getTextContentSize())&&(s=!0)}catch(e){s=!0}return s}function S(e){const t=e.getParent();if(null!==t){const n=e.getWritable(),o=t.getWritable(),s=e.getPreviousSibling(),l=e.getNextSibling();if(null===s)if(null!==l){const e=l.getWritable();o.__first=l.__key,e.__prev=null}else o.__first=null;else{const e=s.getWritable();if(null!==l){const t=l.getWritable();t.__prev=e.__key,e.__next=t.__key}else e.__next=null;n.__prev=null}if(null===l)if(null!==s){const e=s.getWritable();o.__last=s.__key,e.__next=null}else o.__last=null;else{const e=l.getWritable();if(null!==s){const t=s.getWritable();t.__next=e.__key,e.__prev=t.__key}else e.__prev=null;n.__next=null}o.__size--,n.__parent=null}}function w(t,n){const o=n._nodeMap.get(t);if(!o)return void e.$getRoot().selectStart();const s=o.__prev;let l=null;s&&(l=e.$getNodeByKey(s)),null===l&&null!==o.__parent&&(l=e.$getNodeByKey(o.__parent)),null!==l?null!==l&&l.isAttached()?l.selectEnd():w(l.__key,n):e.$getRoot().selectStart()}class P{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);T(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&l(83);m(e,this._xmlElem,n,t)}destroy(e){e.collabNodeMap.delete(this._key)}}function E(e,t,n){const o=new P(e,t,n);return e._collabNode=o,o}class M{constructor(e,t,n){this._key="",this._children=[],this._xmlText=e,this._type=n,this._parent=t}getPrevNode(t){if(null===t)return null;const n=t.get(this._key);return e.$isElementNode(n)?n:null}getNode(){const t=e.$getNodeByKey(this._key);return e.$isElementNode(t)?t:null}getSharedType(){return this._xmlText}getType(){return this._type}getKey(){return this._key}isEmpty(){return 0===this._children.length}getSize(){return 1}getOffset(){const e=this._parent;return null===e&&l(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&l(91),m(e,this._xmlText,n,t)}applyChildrenYjsDelta(e,t){const n=this._children;let o=0,s=null;for(let l=0;l<t.length;l++){const r=t[l],a=r.insert,f=r.delete;if(null!=r.retain)o+=r.retain;else if("number"==typeof f){let e=f;for(;e>0;){const{node:t,nodeIndex:s,offset:l,length:r}=C(this,o,!1);if(t instanceof M||t instanceof i||t instanceof P)n.splice(s,1),e-=1;else{if(!(t instanceof c))break;{const o=Math.min(e,r),i=0!==s?n[s-1]:null,a=t.getSize();if(0===l&&r===a){n.splice(s,1);const e=$(t._text,l,o-1,"");e.length>0&&(i instanceof c?i._text+=e:this._xmlText.delete(l,e.length))}else t._text=$(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}=C(this,o,!0);e instanceof c?e._text=$(e._text,t,0,a):this._xmlText.delete(t,a.length),o+=a.length}else{const t=a,{node:l,nodeIndex:i,length:r}=C(this,o,!1),f=y(e,t,this);if(l instanceof c&&r>0&&r<l._text.length){const e=l._text,t=e.length-r;l._text=$(e,t,r,""),n.splice(i+1,0,f),s=$(e,0,t,"")}else n.splice(i,0,f);null!==s&&f instanceof c&&(f._text=s+f._text,s=null),o+=1}}}}syncChildrenFromYjs(t){const o=this.getNode();null===o&&l(92);const s=o.__key,r=n.$createChildrenArray(o,null),a=r.length,f=this._children,u=f.length,d=t.collabNodeMap,_=new Set;let h,g,p=0,y=null;u!==a&&(g=o.getWritable());for(let n=0;n<u;n++){const a=r[p],m=f[n],N=m.getNode(),b=m._key;if(null!==N&&a===b){const n=e.$isTextNode(N);if(_.add(a),n)if(m._key=a,m instanceof M){const e=m._xmlText;m.syncPropertiesFromYjs(t,null),m.applyChildrenYjsDelta(t,e.toDelta()),m.syncChildrenFromYjs(t)}else m instanceof c?m.syncPropertiesAndTextFromYjs(t,null):m instanceof P?m.syncPropertiesFromYjs(t,null):m instanceof i||l(93);y=N,p++}else{if(void 0===h){h=new Set;for(let e=0;e<u;e++){const t=f[e]._key;""!==t&&h.add(t)}}if(null!==N&&void 0!==a&&!h.has(a)){S(e.$getNodeByKeyOrThrow(a)),n--,p++;continue}g=o.getWritable();const l=x(t,m,s),i=l.__key;if(d.set(i,m),null===y){const e=g.getFirstChild();if(g.__first=i,null!==e){const t=e.getWritable();t.__prev=i,l.__next=t.__key}}else{const e=y.getWritable(),t=y.getNextSibling();if(e.__next=i,l.__prev=y.__key,null!==t){const e=t.getWritable();e.__prev=i,l.__next=e.__key}}n===u-1&&(g.__last=i),g.__size++,y=l}}for(let n=0;n<a;n++){const o=r[n];if(!_.has(o)){const n=e.$getNodeByKeyOrThrow(o),s=t.collabNodeMap.get(o);void 0!==s&&s.destroy(t),S(n)}}}syncPropertiesFromLexical(e,t,n){T(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(t,n,o,s,l,i){const r=this._children[n],a=e.$getNodeByKeyOrThrow(o);r instanceof M&&e.$isElementNode(a)?(r.syncPropertiesFromLexical(t,a,s),r.syncChildrenFromLexical(t,a,s,l,i)):r instanceof c&&e.$isTextNode(a)?r.syncPropertiesAndTextFromLexical(t,a,s):r instanceof P&&e.$isDecoratorNode(a)&&r.syncPropertiesFromLexical(t,a,s)}syncChildrenFromLexical(t,o,s,l,i){const r=this.getPrevNode(s),c=null===r?[]:n.$createChildrenArray(r,s),a=n.$createChildrenArray(o,null),f=c.length-1,u=a.length-1,d=t.collabNodeMap;let _,h,p=0,y=0;for(;p<=f&&y<=u;){const n=c[p],o=a[y];if(n===o)this._syncChildFromLexical(t,y,o,s,l,i),p++,y++;else{void 0===_&&(_=new Set(c)),void 0===h&&(h=new Set(a));const s=h.has(n),l=_.has(o);if(s){const n=g(t,e.$getNodeByKeyOrThrow(o),this);d.set(o,n),l?(this.splice(t,y,1,n),p++,y++):(this.splice(t,y,0,n),y++)}else this.splice(t,y,1),p++}}const x=p>f,m=y>u;if(x&&!m)for(;y<=u;++y){const n=a[y],o=g(t,e.$getNodeByKeyOrThrow(n),this);this.append(o),d.set(n,o)}else if(m&&!x)for(let e=this._children.length-1;e>=y;e--)this.splice(t,e,1)}append(e){const t=this._xmlText,n=this._children,o=n[n.length-1],s=void 0!==o?o.getOffset()+o.getSize():0;if(e instanceof M)t.insertEmbed(s,e._xmlText);else if(e instanceof c){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof i?t.insertEmbed(s,e._map):e instanceof P&&t.insertEmbed(s,e._xmlElem);this._children.push(e)}splice(e,t,n,o){const s=this._children,r=s[t];if(void 0===r)return void 0===o&&l(94),void this.append(o);const a=r.getOffset();-1===a&&l(95);const f=this._xmlText;if(0!==n&&f.delete(a,r.getSize()),o instanceof M)f.insertEmbed(a,o._xmlText);else if(o instanceof c){const e=o._map;null===e.parent&&f.insertEmbed(a,e),f.insert(a+1,o._text)}else o instanceof i?f.insertEmbed(a,o._map):o instanceof P&&f.insertEmbed(a,o._xmlElem);if(0!==n){const o=s.slice(t,t+n);for(let t=0;t<o.length;t++)o[t].destroy(e)}void 0!==o?s.splice(t,n,o):s.splice(t,n)}getChildOffset(e){let t=0;const n=this._children;for(let o=0;o<n.length;o++){const s=n[o];if(s===e)return t;t+=s.getSize()}return-1}destroy(e){const t=e.collabNodeMap,n=this._children;for(let t=0;t<n.length;t++)n[t].destroy(e);t.delete(this._key)}}function O(e,t,n){const o=new M(e,t,n);return e._collabNode=o,o}function F(n,o){const s=o.collabNodeMap.get(n.key);if(void 0===s)return null;let i=n.offset,r=s.getSharedType();if(s instanceof c){r=s._parent._xmlText;const e=s.getOffset();if(-1===e)return null;i=e+1+i}else if(s instanceof M&&"element"===n.type){const t=n.getNode();e.$isElementNode(t)||l(184);let o=0,s=0,r=t.getFirstChild();for(;null!==r&&s++<i;)e.$isTextNode(r)?o+=r.getTextContentSize()+1:o++,r=r.getNextSibling();i=o}return t.createRelativePositionFromTypeIndex(r,i)}function L(e,n){return t.createAbsolutePositionFromRelativePosition(e,n.doc)}function A(e,n){if(null==e){if(null!=n)return!0}else if(null==n||!t.compareRelativePositions(e,n))return!0;return!1}function D(e,t){return{color:t,name:e,selection:null}}function j(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 z(e,t){const n=t.selection;null!==n&&j(e,n)}function Y(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 K(t,n,s,l){const i=t.editor,r=i.getRootElement(),c=t.cursorsContainer;if(null===c||null===r)return;const a=c.offsetParent;if(null===a)return;const f=a.getBoundingClientRect(),u=n.selection;if(null===s)return null===u?void 0:(n.selection=null,void j(t,u));n.selection=s;const d=s.caret,_=s.color,h=s.selections,g=s.anchor,p=s.focus,y=g.key,x=p.key,m=l.get(y),N=l.get(x);if(null==m||null==N)return;let b;if(m===N&&e.$isLineBreakNode(m)){b=[i.getElementByKey(y).getBoundingClientRect()]}else{const e=o.createDOMRange(i,m,g.offset,N,p.offset);if(null===e)return;b=o.createRectsFromDOMRange(i,e)}const k=h.length,T=b.length;for(let e=0;e<T;e++){const t=b[e];let n=h[e];if(void 0===n){n=document.createElement("span"),h[e]=n;const t=document.createElement("span");n.appendChild(t),c.appendChild(n)}const o=`position:absolute;top:${t.top-f.top}px;left:${t.left-f.left}px;height:${t.height}px;width:${t.width}px;pointer-events:none;z-index:5;`;n.style.cssText=o,n.firstChild.style.cssText=`${o}left:0;top:0;background-color:${_};opacity:0.3;`,e===T-1&&d.parentNode!==n&&n.appendChild(d)}for(let e=k-1;e>=T;e--){const t=h[e];c.removeChild(t),h.pop()}}function R(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,l=0,i=null,r=0;if(null!==n&&null!==o){const t=L(n,e),c=L(o,e);null!==t&&null!==c&&([s,l]=I(t.type,t.index),[i,r]=I(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:l,focusCollabNode:i,focusOffset:r}}function B(t,n){const o=n.awareness.getLocalState();if(null===o)return;const{anchorCollabNode:s,anchorOffset:l,focusCollabNode:i,focusOffset:r}=R(t,o);if(null!==s&&null!==i){const t=s.getKey(),n=i.getKey(),o=e.$getSelection();if(!e.$isRangeSelection(o))return;W(o.anchor,t,l),W(o.focus,n,r)}}function W(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 I(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof M){const{node:e,offset:o}=C(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function U(e,t){const n=Array.from(t.awareness.getStates()),o=e.clientID,s=e.cursors,l=e.editor._editorState._nodeMap,i=new Set;for(let t=0;t<n.length;t++){const r=n[t],[c,a]=r;if(c!==o){i.add(c);const{name:t,color:n,focusing:o}=a;let r=null,f=s.get(c);if(void 0===f&&(f=D(t,n),s.set(c,f)),o){const{anchorCollabNode:t,anchorOffset:n,focusCollabNode:o,focusOffset:s}=R(e,a);if(null!==t&&null!==o){const e=t.getKey(),l=o.getKey();if(r=f.selection,null===r)r=Y(f,e,n,l,s);else{const t=r.anchor,o=r.focus;t.key=e,t.offset=n,o.key=l,o.offset=s}}}K(e,f,r,l)}}const r=Array.from(s.keys());for(let t=0;t<r.length;t++){const n=r[t];if(!i.has(n)){const t=s.get(n);void 0!==t&&(z(e,t),s.delete(n))}}}function X(t,n,o,s){const l=n.awareness,i=l.getLocalState();if(null===i)return;const{anchorPos:r,focusPos:c,name:a,color:f,focusing:u,awarenessData:d}=i;let _=null,h=null;(null!==s&&(null===r||s.is(o))||null!==o)&&(e.$isRangeSelection(s)&&(_=F(s.anchor,t),h=F(s.focus,t)),(A(r,_)||A(c,h))&&l.setLocalState({...i,anchorPos:_,awarenessData:d,color:f,focusPos:h,focusing:u,name:a}))}function q(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 l=y(n,s.parent).getNode();if(l){const t=e.$getWritableNodeState(l.getWritable());for(const e of o.keysChanged)t.updateFromUnknown(e,s.get(e))}return!0}(n,o))return;const{target:s}=o,i=y(n,s);if(i instanceof M&&o instanceof t.YTextEvent){const{keysChanged:e,childListChanged:t,delta:s}=o;e.size>0&&i.syncPropertiesFromYjs(n,e),t&&(i.applyChildrenYjsDelta(n,s),i.syncChildrenFromYjs(n))}else if(i instanceof c&&o instanceof t.YMapEvent){const{keysChanged:e}=o;e.size>0&&i.syncPropertiesAndTextFromYjs(n,e)}else if(i instanceof P&&o instanceof t.YXmlEvent){const{attributesChanged:e}=o;e.size>0&&i.syncPropertiesFromYjs(n,e)}else l(82)}const G=e.createCommand("CONNECTED_COMMAND"),J=e.createCommand("TOGGLE_CONNECT_COMMAND");exports.CONNECTED_COMMAND=G,exports.TOGGLE_CONNECT_COMMAND=J,exports.createBinding=function(e,n,o,s,i,r){null==s&&l(81);const c=O(s.get("root",t.XmlText),null,"root");return c._key="root",{clientID:s.clientID,collabNodeMap:new Map,cursors:new Map,cursorsContainer:null,doc:s,docMap:i,editor:e,excludedProperties:r||new Map,id:o,nodeProperties:new Map,root:c}},exports.createUndoManager=function(e,n){return new t.UndoManager(n,{trackedOrigins:new Set([e,null])})},exports.getAnchorAndFocusCollabNodesForUserState=R,exports.initLocalState=function(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})},exports.setLocalStateFocus=function(e,t,n,o,s){const{awareness:l}=e;let i=l.getLocalState();null===i&&(i={anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t}),i.focusing=o,l.setLocalState(i)},exports.syncCursorPositions=U,exports.syncLexicalUpdateToYjs=function(t,n,o,s,l,i,r,a){!function(e,t){e.doc.transact(t,e)}(t,(()=>{s.read((()=>{if(a.has("collaboration")||a.has("historic"))return void(r.size>0&&function(t,n){const o=Array.from(n),s=t.collabNodeMap,l=[],i=[];for(let t=0;t<o.length;t++){const n=o[t],r=e.$getNodeByKey(n),a=s.get(n);if(a instanceof c)if(e.$isTextNode(r))l.push([a,r.__text]);else{const e=a.getOffset();if(-1===e)continue;const t=a._parent;a._normalized=!0,t._xmlText.delete(e,1),i.push(a)}}for(let e=0;e<i.length;e++){const t=i[e],n=t.getKey();s.delete(n);const o=t._parent._children,l=o.indexOf(t);o.splice(l,1)}for(let e=0;e<l.length;e++){const[t,n]=l[e];t._text=n}}(t,r));if(l.has("root")){const n=o._nodeMap,s=e.$getRoot(),r=t.root;r.syncPropertiesFromLexical(t,s,n),r.syncChildrenFromLexical(t,s,n,l,i)}const s=e.$getSelection(),f=o._selection;X(t,n,f,s)}))}))},exports.syncYjsChangesToLexical=function(t,n,o,s,l=U){const i=t.editor,r=i._editorState;o.forEach((e=>e.delta)),i.update((()=>{for(let e=0;e<o.length;e++){const n=o[e];q(t,n)}const s=e.$getSelection();if(e.$isRangeSelection(s))if(v(s)){const o=r._selection;if(e.$isRangeSelection(o)&&(B(t,n),v(s))){w(s.anchor.key,r)}X(t,n,o,e.$getSelection())}else B(t,n)}),{onUpdate:()=>{l(t,n),i.update((()=>{0===e.$getRoot().getChildrenSize()&&e.$getRoot().append(e.$createParagraphNode())}))},skipTransforms:!0,tag:s?"historic":"collaboration"})};
|
package/LexicalYjs.prod.mjs
CHANGED
|
@@ -6,4 +6,4 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import{$getNodeByKey as e,$isLineBreakNode as t,$isTextNode as n,$getSelection as o,$isRangeSelection as s,$isElementNode as l,$isDecoratorNode as i,createEditor as r,$getRoot as c,$isRootNode as a,$createParagraphNode as f,createCommand as u}from"lexical";import{XmlText as d,Map as _,XmlElement as h,Doc as p,createAbsolutePositionFromRelativePosition as g,createRelativePositionFromTypeIndex as y,compareRelativePositions as x,YTextEvent as m,YMapEvent as b,YXmlEvent as k,UndoManager as v}from"yjs";import{$createChildrenArray as C}from"@lexical/offset";import{createDOMRange as N,createRectsFromDOMRange as P}from"@lexical/selection";function w(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var T=w((function(e){const t=new URL("https://lexical.dev/docs/error"),n=new URLSearchParams;n.append("code",e);for(let e=1;e<arguments.length;e++)n.append("v",arguments[e]);throw t.search=n.toString(),Error(`Minified Lexical error #${e}; visit ${t.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}));class S{constructor(e,t){this._key="",this._map=e,this._parent=t,this._type="linebreak"}getNode(){const n=e(this._key);return t(n)?n:null}getKey(){return this._key}getSharedType(){return this._map}getType(){return this._type}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}destroy(e){e.collabNodeMap.delete(this._key)}}function F(e,t){const n=new S(e,t);return e._collabNode=n,n}class O{constructor(e,t,n,o){this._key="",this._map=e,this._parent=n,this._text=t,this._type=o,this._normalized=!1}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return n(t)?t:null}getNode(){const t=e(this._key);return n(t)?t:null}getSharedType(){return this._map}getType(){return this._type}getKey(){return this._key}getSize(){return this._text.length+(this._normalized?0:1)}getOffset(){return this._parent.getChildOffset(this)}spliceText(e,t,n){const o=this._parent._xmlText,s=this.getOffset()+1+e;0!==t&&o.delete(s,t),""!==n&&o.insert(s,n)}syncPropertiesAndTextFromLexical(e,t,n){const l=this.getPrevNode(n),i=t.__text;if($(e,this._map,l,t),null!==l){const e=l.__text;if(e!==i){!function(e,t,n,l){const i=o();let r=l.length;if(s(i)&&i.isCollapsed()){const e=i.anchor;e.key===t&&(r=e.offset)}const c=function(e,t,n){const o=e.length,s=t.length;let l=0,i=0;for(;l<o&&l<s&&e[l]===t[l]&&l<n;)l++;for(;i+l<o&&i+l<s&&e[o-i-1]===t[s-i-1];)i++;for(;i+l<o&&i+l<s&&e[l]===t[l];)l++;return{index:l,insert:t.slice(l,s-i),remove:o-l-i}}(n,l,r);e.spliceText(c.index,c.remove,c.insert)}(this,t.__key,e,i),this._text=i}}}syncPropertiesAndTextFromYjs(e,t){const n=this.getNode();null===n&&T(84),I(e,this._map,n,t);const o=this._text;if(n.__text!==o){n.getWritable().__text=o}}destroy(e){e.collabNodeMap.delete(this._key)}}function E(e,t,n,o){const s=new O(e,t,n,o);return e._collabNode=s,s}const M=new Set(["__key","__parent","__next","__prev"]),L=new Set(["__first","__last","__size"]),z=new Set(["__cachedText"]),j=new Set(["__text"]);function A(e,t,o){if(M.has(e))return!0;if(n(t)){if(j.has(e))return!0}else if(l(t)&&(L.has(e)||a(t)&&z.has(e)))return!0;const s=t.constructor,i=o.excludedProperties.get(s);return null!=i&&i.has(e)}function Y(t){const n=e(t);return null===n&&T(85),n}function D(e,o,s){const r=o.__type;let c;if(l(o)){c=V(new d,s,r),c.syncPropertiesFromLexical(e,o,null),c.syncChildrenFromLexical(e,o,null,null,null)}else if(n(o)){c=E(new _,o.__text,s,r),c.syncPropertiesAndTextFromLexical(e,o,null)}else if(t(o)){const e=new _;e.set("__type","linebreak"),c=F(e,s)}else if(i(o)){c=J(new h,s,r),c.syncPropertiesFromLexical(e,o,null)}else T(86);return c._key=o.__key,c}function W(e,t,n){const o=t._collabNode;if(void 0===o){const o=e.editor._nodes,s=function(e){const t=e instanceof _?e.get("__type"):e.getAttribute("__type");return null==t&&T(87),t}(t);void 0===o.get(s)&&T(88,s);const l=t.parent,i=void 0===n&&null!==l?W(e,l):n||null;if(i instanceof Q||T(89),t instanceof d)return V(t,i,s);if(t instanceof _)return"linebreak"===s?F(t,i):E(t,"",i,s);if(t instanceof h)return J(t,i,s)}return o}function K(e,t,n){const o=t.getType(),s=e.editor._nodes.get(o);void 0===s&&T(88,o);const l=new s.klass;if(l.__parent=n,t._key=l.__key,t instanceof Q){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof O?t.syncPropertiesAndTextFromYjs(e,null):t instanceof H&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(l.__key,t),l}function I(e,t,n,o){const s=null===o?t instanceof _?Array.from(t.keys()):Object.keys(t.getAttributes()):Array.from(o);let l;for(let o=0;o<s.length;o++){const i=s[o];if(A(i,n,e))continue;const c=n[i];let a=t instanceof _?t.get(i):t.getAttribute(i);if(c!==a){if(a instanceof p){const t=e.docMap;c instanceof p&&t.delete(c.guid);const n=r(),o=a.guid;n._key=o,t.set(o,a),a=n}void 0===l&&(l=n.getWritable()),l[i]=a}}}function $(e,t,n,o){const s=o.__type,l=e.nodeProperties;let i=l.get(s);void 0===i&&(i=Object.keys(o).filter((t=>!A(t,o,e))),l.set(s,i));const r=e.editor.constructor;for(let s=0;s<i.length;s++){const l=i[s],c=null===n?void 0:n[l];let a=o[l];if(c!==a){if(a instanceof r){const t=e.docMap;let n;if(c instanceof r){const e=c._key;n=t.get(e),t.delete(e)}const s=n||new p,l=s.guid;a._key=l,t.set(l,s),a=s,e.editor.update((()=>{o.markDirty()}))}t instanceof _?t.set(l,a):t.setAttribute(l,a)}}}function R(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function U(e,t,n){let o=0,s=0;const l=e._children,i=l.length;for(;s<i;s++){const e=l[s],r=o;o+=e.getSize();if((n?o>=t:o>t)&&e instanceof O){let n=t-r-1;n<0&&(n=0);return{length:o-t,node:e,nodeIndex:s,offset:n}}if(o>t)return{length:0,node:e,nodeIndex:s,offset:r};if(s===i-1)return{length:0,node:null,nodeIndex:s+1,offset:r+1}}return{length:0,node:null,nodeIndex:0,offset:0}}function B(e){const t=e.anchor,o=e.focus;let s=!1;try{const e=t.getNode(),l=o.getNode();(!e.isAttached()||!l.isAttached()||n(e)&&t.offset>e.getTextContentSize()||n(l)&&o.offset>l.getTextContentSize())&&(s=!0)}catch(e){s=!0}return s}function G(e){const t=e.getParent();if(null!==t){const n=e.getWritable(),o=t.getWritable(),s=e.getPreviousSibling(),l=e.getNextSibling();if(null===s)if(null!==l){const e=l.getWritable();o.__first=l.__key,e.__prev=null}else o.__first=null;else{const e=s.getWritable();if(null!==l){const t=l.getWritable();t.__prev=e.__key,e.__next=t.__key}else e.__next=null;n.__prev=null}if(null===l)if(null!==s){const e=s.getWritable();o.__last=s.__key,e.__next=null}else o.__last=null;else{const e=l.getWritable();if(null!==s){const t=s.getWritable();t.__next=e.__key,e.__prev=t.__key}else e.__prev=null;n.__next=null}o.__size--,n.__parent=null}}function q(t,n){const o=n._nodeMap.get(t);if(!o)return void c().selectStart();const s=o.__prev;let l=null;s&&(l=e(s)),null===l&&null!==o.__parent&&(l=e(o.__parent)),null!==l?null!==l&&l.isAttached()?l.selectEnd():q(l.__key,n):c().selectStart()}class H{constructor(e,t,n){this._key="",this._xmlElem=e,this._parent=t,this._type=n}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return i(t)?t:null}getNode(){const t=e(this._key);return i(t)?t:null}getSharedType(){return this._xmlElem}getType(){return this._type}getKey(){return this._key}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}syncPropertiesFromLexical(e,t,n){const o=this.getPrevNode(n);$(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&T(83);I(e,this._xmlElem,n,t)}destroy(e){e.collabNodeMap.delete(this._key)}}function J(e,t,n){const o=new H(e,t,n);return e._collabNode=o,o}class Q{constructor(e,t,n){this._key="",this._children=[],this._xmlText=e,this._type=n,this._parent=t}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return l(t)?t:null}getNode(){const t=e(this._key);return l(t)?t:null}getSharedType(){return this._xmlText}getType(){return this._type}getKey(){return this._key}isEmpty(){return 0===this._children.length}getSize(){return 1}getOffset(){const e=this._parent;return null===e&&T(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&T(91),I(e,this._xmlText,n,t)}applyChildrenYjsDelta(e,t){const n=this._children;let o=0,s=null;for(let l=0;l<t.length;l++){const i=t[l],r=i.insert,c=i.delete;if(null!=i.retain)o+=i.retain;else if("number"==typeof c){let e=c;for(;e>0;){const{node:t,nodeIndex:s,offset:l,length:i}=U(this,o,!1);if(t instanceof Q||t instanceof S||t instanceof H)n.splice(s,1),e-=1;else{if(!(t instanceof O))break;{const o=Math.min(e,i),r=0!==s?n[s-1]:null,c=t.getSize();if(0===l&&i===c){n.splice(s,1);const e=R(t._text,l,o-1,"");e.length>0&&(r instanceof O?r._text+=e:this._xmlText.delete(l,e.length))}else t._text=R(t._text,l,o,"");e-=o}}}}else{if(null==r)throw new Error("Unexpected delta format");if("string"==typeof r){const{node:e,offset:t}=U(this,o,!0);e instanceof O?e._text=R(e._text,t,0,r):this._xmlText.delete(t,r.length),o+=r.length}else{const t=r,{node:l,nodeIndex:i,length:c}=U(this,o,!1),a=W(e,t,this);if(l instanceof O&&c>0&&c<l._text.length){const e=l._text,t=e.length-c;l._text=R(e,t,c,""),n.splice(i+1,0,a),s=R(e,0,t,"")}else n.splice(i,0,a);null!==s&&a instanceof O&&(a._text=s+a._text,s=null),o+=1}}}}syncChildrenFromYjs(e){const t=this.getNode();null===t&&T(92);const o=t.__key,s=C(t,null),l=s.length,i=this._children,r=i.length,c=e.collabNodeMap,a=new Set;let f,u,d=0,_=null;r!==l&&(u=t.getWritable());for(let l=0;l<r;l++){const h=s[d],p=i[l],g=p.getNode(),y=p._key;if(null!==g&&h===y){const t=n(g);if(a.add(h),t)if(p._key=h,p instanceof Q){const t=p._xmlText;p.syncPropertiesFromYjs(e,null),p.applyChildrenYjsDelta(e,t.toDelta()),p.syncChildrenFromYjs(e)}else p instanceof O?p.syncPropertiesAndTextFromYjs(e,null):p instanceof H?p.syncPropertiesFromYjs(e,null):p instanceof S||T(93);_=g,d++}else{if(void 0===f){f=new Set;for(let e=0;e<r;e++){const t=i[e]._key;""!==t&&f.add(t)}}if(null!==g&&void 0!==h&&!f.has(h)){G(Y(h)),l--,d++;continue}u=t.getWritable();const n=K(e,p,o),s=n.__key;if(c.set(s,p),null===_){const e=u.getFirstChild();if(u.__first=s,null!==e){const t=e.getWritable();t.__prev=s,n.__next=t.__key}}else{const e=_.getWritable(),t=_.getNextSibling();if(e.__next=s,n.__prev=_.__key,null!==t){const e=t.getWritable();e.__prev=s,n.__next=e.__key}}l===r-1&&(u.__last=s),u.__size++,_=n}}for(let t=0;t<l;t++){const n=s[t];if(!a.has(n)){const t=Y(n),o=e.collabNodeMap.get(n);void 0!==o&&o.destroy(e),G(t)}}}syncPropertiesFromLexical(e,t,n){$(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(e,t,o,s,r,c){const a=this._children[t],f=Y(o);a instanceof Q&&l(f)?(a.syncPropertiesFromLexical(e,f,s),a.syncChildrenFromLexical(e,f,s,r,c)):a instanceof O&&n(f)?a.syncPropertiesAndTextFromLexical(e,f,s):a instanceof H&&i(f)&&a.syncPropertiesFromLexical(e,f,s)}syncChildrenFromLexical(e,t,n,o,s){const l=this.getPrevNode(n),i=null===l?[]:C(l,n),r=C(t,null),c=i.length-1,a=r.length-1,f=e.collabNodeMap;let u,d,_=0,h=0;for(;_<=c&&h<=a;){const t=i[_],l=r[h];if(t===l)this._syncChildFromLexical(e,h,l,n,o,s),_++,h++;else{void 0===u&&(u=new Set(i)),void 0===d&&(d=new Set(r));const n=d.has(t),o=u.has(l);if(n){const t=D(e,Y(l),this);f.set(l,t),o?(this.splice(e,h,1,t),_++,h++):(this.splice(e,h,0,t),h++)}else this.splice(e,h,1),_++}}const p=_>c,g=h>a;if(p&&!g)for(;h<=a;++h){const t=r[h],n=D(e,Y(t),this);this.append(n),f.set(t,n)}else if(g&&!p)for(let t=this._children.length-1;t>=h;t--)this.splice(e,t,1)}append(e){const t=this._xmlText,n=this._children,o=n[n.length-1],s=void 0!==o?o.getOffset()+o.getSize():0;if(e instanceof Q)t.insertEmbed(s,e._xmlText);else if(e instanceof O){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof S?t.insertEmbed(s,e._map):e instanceof H&&t.insertEmbed(s,e._xmlElem);this._children.push(e)}splice(e,t,n,o){const s=this._children,l=s[t];if(void 0===l)return void 0===o&&T(94),void this.append(o);const i=l.getOffset();-1===i&&T(95);const r=this._xmlText;if(0!==n&&r.delete(i,l.getSize()),o instanceof Q)r.insertEmbed(i,o._xmlText);else if(o instanceof O){const e=o._map;null===e.parent&&r.insertEmbed(i,e),r.insert(i+1,o._text)}else o instanceof S?r.insertEmbed(i,o._map):o instanceof H&&r.insertEmbed(i,o._xmlElem);if(0!==n){const o=s.slice(t,t+n);for(let t=0;t<o.length;t++)o[t].destroy(e)}void 0!==o?s.splice(t,n,o):s.splice(t,n)}getChildOffset(e){let t=0;const n=this._children;for(let o=0;o<n.length;o++){const s=n[o];if(s===e)return t;t+=s.getSize()}return-1}destroy(e){const t=e.collabNodeMap,n=this._children;for(let t=0;t<n.length;t++)n[t].destroy(e);t.delete(this._key)}}function V(e,t,n){const o=new Q(e,t,n);return e._collabNode=o,o}function X(e,t,n,o,s,l){null==o&&T(81);const i=V(o.get("root",d),null,"root");return i._key="root",{clientID:o.clientID,collabNodeMap:new Map,cursors:new Map,cursorsContainer:null,doc:o,docMap:s,editor:e,excludedProperties:l||new Map,id:n,nodeProperties:new Map,root:i}}function Z(e,t){const o=t.collabNodeMap.get(e.key);if(void 0===o)return null;let s=e.offset,i=o.getSharedType();if(o instanceof O){i=o._parent._xmlText;const e=o.getOffset();if(-1===e)return null;s=e+1+s}else if(o instanceof Q&&"element"===e.type){const t=e.getNode();l(t)||T(184);let o=0,i=0,r=t.getFirstChild();for(;null!==r&&i++<s;)n(r)?o+=r.getTextContentSize()+1:o++,r=r.getNextSibling();s=o}return y(i,s)}function ee(e,t){return g(e,t.doc)}function te(e,t){if(null==e){if(null!=t)return!0}else if(null==t||!x(e,t))return!0;return!1}function ne(e,t){return{color:t,name:e,selection:null}}function oe(e,t){const n=e.cursorsContainer;if(null!==n){const e=t.selections,o=e.length;for(let t=0;t<o;t++)n.removeChild(e[t])}}function se(e,t){const n=t.selection;null!==n&&oe(e,n)}function le(e,t,n,o,s){const l=e.color,i=document.createElement("span");i.style.cssText=`position:absolute;top:0;bottom:0;right:-1px;width:1px;background-color:${l};z-index:10;`;const r=document.createElement("span");return r.textContent=e.name,r.style.cssText=`position:absolute;left:-2px;top:-16px;background-color:${l};color:#fff;line-height:12px;font-size:12px;padding:2px;font-family:Arial;font-weight:bold;white-space:nowrap;`,i.appendChild(r),{anchor:{key:t,offset:n},caret:i,color:l,focus:{key:o,offset:s},name:r,selections:[]}}function ie(e,n,o,s){const l=e.editor,i=l.getRootElement(),r=e.cursorsContainer;if(null===r||null===i)return;const c=r.offsetParent;if(null===c)return;const a=c.getBoundingClientRect(),f=n.selection;if(null===o)return null===f?void 0:(n.selection=null,void oe(e,f));n.selection=o;const u=o.caret,d=o.color,_=o.selections,h=o.anchor,p=o.focus,g=h.key,y=p.key,x=s.get(g),m=s.get(y);if(null==x||null==m)return;let b;if(x===m&&t(x)){b=[l.getElementByKey(g).getBoundingClientRect()]}else{const e=N(l,x,h.offset,m,p.offset);if(null===e)return;b=P(l,e)}const k=_.length,v=b.length;for(let e=0;e<v;e++){const t=b[e];let n=_[e];if(void 0===n){n=document.createElement("span"),_[e]=n;const t=document.createElement("span");n.appendChild(t),r.appendChild(n)}const o=`position:absolute;top:${t.top-a.top}px;left:${t.left-a.left}px;height:${t.height}px;width:${t.width}px;pointer-events:none;z-index:5;`;n.style.cssText=o,n.firstChild.style.cssText=`${o}left:0;top:0;background-color:${d};opacity:0.3;`,e===v-1&&u.parentNode!==n&&n.appendChild(u)}for(let e=k-1;e>=v;e--){const t=_[e];r.removeChild(t),_.pop()}}function re(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,l=0,i=null,r=0;if(null!==n&&null!==o){const t=ee(n,e),c=ee(o,e);null!==t&&null!==c&&([s,l]=fe(t.type,t.index),[i,r]=fe(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:l,focusCollabNode:i,focusOffset:r}}function ce(e,t){const n=t.awareness.getLocalState();if(null===n)return;const{anchorCollabNode:l,anchorOffset:i,focusCollabNode:r,focusOffset:c}=re(e,n);if(null!==l&&null!==r){const e=l.getKey(),t=r.getKey(),n=o();if(!s(n))return;ae(n.anchor,e,i),ae(n.focus,t,c)}}function ae(t,o,s){if(t.key!==o||t.offset!==s){let i=e(o);if(null!==i&&!l(i)&&!n(i)){const e=i.getParentOrThrow();o=e.getKey(),s=i.getIndexWithinParent(),i=e}t.set(o,s,l(i)?"element":"text")}}function fe(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof Q){const{node:e,offset:o}=U(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function ue(e,t){const n=Array.from(t.awareness.getStates()),o=e.clientID,s=e.cursors,l=e.editor._editorState._nodeMap,i=new Set;for(let t=0;t<n.length;t++){const r=n[t],[c,a]=r;if(c!==o){i.add(c);const{name:t,color:n,focusing:o}=a;let r=null,f=s.get(c);if(void 0===f&&(f=ne(t,n),s.set(c,f)),o){const{anchorCollabNode:t,anchorOffset:n,focusCollabNode:o,focusOffset:s}=re(e,a);if(null!==t&&null!==o){const e=t.getKey(),l=o.getKey();if(r=f.selection,null===r)r=le(f,e,n,l,s);else{const t=r.anchor,o=r.focus;t.key=e,t.offset=n,o.key=l,o.offset=s}}}ie(e,f,r,l)}}const r=Array.from(s.keys());for(let t=0;t<r.length;t++){const n=r[t];if(!i.has(n)){const t=s.get(n);void 0!==t&&(se(e,t),s.delete(n))}}}function de(e,t,n,o){const l=t.awareness,i=l.getLocalState();if(null===i)return;const{anchorPos:r,focusPos:c,name:a,color:f,focusing:u,awarenessData:d}=i;let _=null,h=null;(null!==o&&(null===r||o.is(n))||null!==n)&&(s(o)&&(_=Z(o.anchor,e),h=Z(o.focus,e)),(te(r,_)||te(c,h))&&l.setLocalState({...i,anchorPos:_,awarenessData:d,color:f,focusPos:h,focusing:u,name:a}))}function _e(e,t){const{target:n}=t,o=W(e,n);if(o instanceof Q&&t instanceof m){const{keysChanged:n,childListChanged:s,delta:l}=t;n.size>0&&o.syncPropertiesFromYjs(e,n),s&&(o.applyChildrenYjsDelta(e,l),o.syncChildrenFromYjs(e))}else if(o instanceof O&&t instanceof b){const{keysChanged:n}=t;n.size>0&&o.syncPropertiesAndTextFromYjs(e,n)}else if(o instanceof H&&t instanceof k){const{attributesChanged:n}=t;n.size>0&&o.syncPropertiesFromYjs(e,n)}else T(82)}function he(e,t,n,l,i=ue){const r=e.editor,a=r._editorState;n.forEach((e=>e.delta)),r.update((()=>{for(let t=0;t<n.length;t++){const o=n[t];_e(e,o)}const l=o();if(s(l))if(B(l)){const n=a._selection;if(s(n)&&(ce(e,t),B(l))){q(l.anchor.key,a)}de(e,t,n,o())}else ce(e,t)}),{onUpdate:()=>{i(e,t),r.update((()=>{0===c().getChildrenSize()&&c().append(f())}))},skipTransforms:!0,tag:l?"historic":"collaboration"})}function pe(t,s,l,i,r,a,f,u){!function(e,t){e.doc.transact(t,e)}(t,(()=>{i.read((()=>{if(u.has("collaboration")||u.has("historic"))return void(f.size>0&&function(t,o){const s=Array.from(o),l=t.collabNodeMap,i=[],r=[];for(let t=0;t<s.length;t++){const o=s[t],c=e(o),a=l.get(o);if(a instanceof O)if(n(c))i.push([a,c.__text]);else{const e=a.getOffset();if(-1===e)continue;const t=a._parent;a._normalized=!0,t._xmlText.delete(e,1),r.push(a)}}for(let e=0;e<r.length;e++){const t=r[e],n=t.getKey();l.delete(n);const o=t._parent._children,s=o.indexOf(t);o.splice(s,1)}for(let e=0;e<i.length;e++){const[t,n]=i[e];t._text=n}}(t,f));if(r.has("root")){const e=l._nodeMap,n=c(),o=t.root;o.syncPropertiesFromLexical(t,n,e),o.syncChildrenFromLexical(t,n,e,r,a)}const i=o(),d=l._selection;de(t,s,d,i)}))}))}const ge=u("CONNECTED_COMMAND"),ye=u("TOGGLE_CONNECT_COMMAND");function xe(e,t){return new v(t,{trackedOrigins:new Set([e,null])})}function me(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})}function be(e,t,n,o,s){const{awareness:l}=e;let i=l.getLocalState();null===i&&(i={anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t}),i.focusing=o,l.setLocalState(i)}export{ge as CONNECTED_COMMAND,ye as TOGGLE_CONNECT_COMMAND,X as createBinding,xe as createUndoManager,re as getAnchorAndFocusCollabNodesForUserState,me as initLocalState,be as setLocalStateFocus,ue as syncCursorPositions,pe as syncLexicalUpdateToYjs,he as syncYjsChangesToLexical};
|
|
9
|
+
import{$getNodeByKey as e,$isLineBreakNode as t,$isTextNode as n,$getSelection as o,$isRangeSelection as s,$isElementNode as l,$isDecoratorNode as i,createEditor as r,$getWritableNodeState as c,$getRoot as a,$isRootNode as f,$getNodeByKeyOrThrow as u,$createParagraphNode as d,createCommand as _}from"lexical";import{XmlText as h,Map as p,XmlElement as g,Doc as y,createAbsolutePositionFromRelativePosition as x,createRelativePositionFromTypeIndex as m,compareRelativePositions as b,YMapEvent as k,YTextEvent as v,YXmlEvent as C,UndoManager as N}from"yjs";import{$createChildrenArray as w}from"@lexical/offset";import{createDOMRange as S,createRectsFromDOMRange as P}from"@lexical/selection";function T(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var F=T((function(e){const t=new URL("https://lexical.dev/docs/error"),n=new URLSearchParams;n.append("code",e);for(let e=1;e<arguments.length;e++)n.append("v",arguments[e]);throw t.search=n.toString(),Error(`Minified Lexical error #${e}; visit ${t.toString()} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}));class O{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 M(e,t){const n=new O(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(J(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&&F(84),R(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 L(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","__state"]),j=new Set(["__first","__last","__size"]),A=new Set(["__cachedText"]),Y=new Set(["__text"]);function W(e,t,o){if(z.has(e)||"function"==typeof t[e])return!0;if(n(t)){if(Y.has(e))return!0}else if(l(t)&&(j.has(e)||f(t)&&A.has(e)))return!0;const s=t.constructor,i=o.excludedProperties.get(s);return null!=i&&i.has(e)}function D(e,o,s){const r=o.__type;let c;if(l(o)){c=ne(new h,s,r),c.syncPropertiesFromLexical(e,o,null),c.syncChildrenFromLexical(e,o,null,null,null)}else if(n(o)){c=L(new p,o.__text,s,r),c.syncPropertiesAndTextFromLexical(e,o,null)}else if(t(o)){const e=new p;e.set("__type","linebreak"),c=M(e,s)}else if(i(o)){c=ee(new g,s,r),c.syncPropertiesFromLexical(e,o,null)}else F(86);return c._key=o.__key,c}function I(e){const t=U(e,"__type");return"string"!=typeof t&&void 0!==t&&F(87),t}function K(e,t,n){const o=t._collabNode;if(void 0===o){const o=e.editor._nodes,s=I(t);"string"!=typeof s&&F(87);void 0===o.get(s)&&F(88,s);const l=t.parent,i=void 0===n&&null!==l?K(e,l):n||null;if(i instanceof te||F(89),t instanceof h)return ne(t,i,s);if(t instanceof p)return"linebreak"===s?M(t,i):L(t,"",i,s);if(t instanceof g)return ee(t,i,s)}return o}function $(e,t,n){const o=t.getType(),s=e.editor._nodes.get(o);void 0===s&&F(88,o);const l=new s.klass;if(l.__parent=n,t._key=l.__key,t instanceof te){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 Z&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(l.__key,t),l}function R(e,t,n,o){const s=null===o?t instanceof p?Array.from(t.keys()):Object.keys(t.getAttributes()):Array.from(o);let l;for(let o=0;o<s.length;o++){const i=s[o];if(W(i,n,e)){"__state"===i&&(l||(l=n.getWritable()),G(e,t,l));continue}const c=n[i];let a=U(t,i);if(c!==a){if(a instanceof y){const t=e.docMap;c instanceof y&&t.delete(c.guid);const n=r(),o=a.guid;n._key=o,t.set(o,a),a=n}void 0===l&&(l=n.getWritable()),l[i]=a}}}function U(e,t){return e instanceof p?e.get(t):e.getAttribute(t)}function B(e,t,n){e instanceof p?e.set(t,n):e.setAttribute(t,n)}function G(e,t,n){const o=U(t,"__state");o instanceof p&&c(n).updateFromJSON(o.toJSON())}function J(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=U(t,"__state");if(!s)return;const[i,r]=s.getInternalState(),c=n&&n.__state,a=l instanceof p?l:new p;if(c===s)return;const[f,u]=c&&a.doc?c.getInternalState():[void 0,new Map];if(i)for(const[e,t]of Object.entries(i))f&&t!==f[e]&&a.set(e,t);for(const[e,t]of r)u.get(e)!==t&&a.set(e.key,e.unparse(t));l||B(t,"__state",a)}(0,t,n,o);for(let s=0;s<i.length;s++){const l=i[s],c=null===n?void 0:n[l];let a=o[l];if(c!==a){if(a instanceof r){const t=e.docMap;let n;if(c instanceof r){const e=c._key;n=t.get(e),t.delete(e)}const s=n||new y,l=s.guid;a._key=l,t.set(l,s),a=s,e.editor.update((()=>{o.markDirty()}))}B(t,l,a)}}}function q(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function H(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 Q(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 V(e){const t=e.getParent();if(null!==t){const n=e.getWritable(),o=t.getWritable(),s=e.getPreviousSibling(),l=e.getNextSibling();if(null===s)if(null!==l){const e=l.getWritable();o.__first=l.__key,e.__prev=null}else o.__first=null;else{const e=s.getWritable();if(null!==l){const t=l.getWritable();t.__prev=e.__key,e.__next=t.__key}else e.__next=null;n.__prev=null}if(null===l)if(null!==s){const e=s.getWritable();o.__last=s.__key,e.__next=null}else o.__last=null;else{const e=l.getWritable();if(null!==s){const t=s.getWritable();t.__next=e.__key,e.__prev=t.__key}else e.__prev=null;n.__next=null}o.__size--,n.__parent=null}}function X(t,n){const o=n._nodeMap.get(t);if(!o)return void a().selectStart();const s=o.__prev;let l=null;s&&(l=e(s)),null===l&&null!==o.__parent&&(l=e(o.__parent)),null!==l?null!==l&&l.isAttached()?l.selectEnd():X(l.__key,n):a().selectStart()}class Z{constructor(e,t,n){this._key="",this._xmlElem=e,this._parent=t,this._type=n}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return i(t)?t:null}getNode(){const t=e(this._key);return i(t)?t:null}getSharedType(){return this._xmlElem}getType(){return this._type}getKey(){return this._key}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}syncPropertiesFromLexical(e,t,n){const o=this.getPrevNode(n);J(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&F(83);R(e,this._xmlElem,n,t)}destroy(e){e.collabNodeMap.delete(this._key)}}function ee(e,t,n){const o=new Z(e,t,n);return e._collabNode=o,o}class te{constructor(e,t,n){this._key="",this._children=[],this._xmlText=e,this._type=n,this._parent=t}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return l(t)?t:null}getNode(){const t=e(this._key);return l(t)?t:null}getSharedType(){return this._xmlText}getType(){return this._type}getKey(){return this._key}isEmpty(){return 0===this._children.length}getSize(){return 1}getOffset(){const e=this._parent;return null===e&&F(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&F(91),R(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}=H(this,o,!1);if(t instanceof te||t instanceof O||t instanceof Z)n.splice(s,1),e-=1;else{if(!(t instanceof E))break;{const o=Math.min(e,i),r=0!==s?n[s-1]:null,c=t.getSize();if(0===l&&i===c){n.splice(s,1);const e=q(t._text,l,o-1,"");e.length>0&&(r instanceof E?r._text+=e:this._xmlText.delete(l,e.length))}else t._text=q(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}=H(this,o,!0);e instanceof E?e._text=q(e._text,t,0,r):this._xmlText.delete(t,r.length),o+=r.length}else{const t=r,{node:l,nodeIndex:i,length:c}=H(this,o,!1),a=K(e,t,this);if(l instanceof E&&c>0&&c<l._text.length){const e=l._text,t=e.length-c;l._text=q(e,t,c,""),n.splice(i+1,0,a),s=q(e,0,t,"")}else n.splice(i,0,a);null!==s&&a instanceof E&&(a._text=s+a._text,s=null),o+=1}}}}syncChildrenFromYjs(e){const t=this.getNode();null===t&&F(92);const o=t.__key,s=w(t,null),l=s.length,i=this._children,r=i.length,c=e.collabNodeMap,a=new Set;let f,d,_=0,h=null;r!==l&&(d=t.getWritable());for(let l=0;l<r;l++){const p=s[_],g=i[l],y=g.getNode(),x=g._key;if(null!==y&&p===x){const t=n(y);if(a.add(p),t)if(g._key=p,g instanceof te){const t=g._xmlText;g.syncPropertiesFromYjs(e,null),g.applyChildrenYjsDelta(e,t.toDelta()),g.syncChildrenFromYjs(e)}else g instanceof E?g.syncPropertiesAndTextFromYjs(e,null):g instanceof Z?g.syncPropertiesFromYjs(e,null):g instanceof O||F(93);h=y,_++}else{if(void 0===f){f=new Set;for(let e=0;e<r;e++){const t=i[e]._key;""!==t&&f.add(t)}}if(null!==y&&void 0!==p&&!f.has(p)){V(u(p)),l--,_++;continue}d=t.getWritable();const n=$(e,g,o),s=n.__key;if(c.set(s,g),null===h){const e=d.getFirstChild();if(d.__first=s,null!==e){const t=e.getWritable();t.__prev=s,n.__next=t.__key}}else{const e=h.getWritable(),t=h.getNextSibling();if(e.__next=s,n.__prev=h.__key,null!==t){const e=t.getWritable();e.__prev=s,n.__next=e.__key}}l===r-1&&(d.__last=s),d.__size++,h=n}}for(let t=0;t<l;t++){const n=s[t];if(!a.has(n)){const t=u(n),o=e.collabNodeMap.get(n);void 0!==o&&o.destroy(e),V(t)}}}syncPropertiesFromLexical(e,t,n){J(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(e,t,o,s,r,c){const a=this._children[t],f=u(o);a instanceof te&&l(f)?(a.syncPropertiesFromLexical(e,f,s),a.syncChildrenFromLexical(e,f,s,r,c)):a instanceof E&&n(f)?a.syncPropertiesAndTextFromLexical(e,f,s):a instanceof Z&&i(f)&&a.syncPropertiesFromLexical(e,f,s)}syncChildrenFromLexical(e,t,n,o,s){const l=this.getPrevNode(n),i=null===l?[]:w(l,n),r=w(t,null),c=i.length-1,a=r.length-1,f=e.collabNodeMap;let d,_,h=0,p=0;for(;h<=c&&p<=a;){const t=i[h],l=r[p];if(t===l)this._syncChildFromLexical(e,p,l,n,o,s),h++,p++;else{void 0===d&&(d=new Set(i)),void 0===_&&(_=new Set(r));const n=_.has(t),o=d.has(l);if(n){const t=D(e,u(l),this);f.set(l,t),o?(this.splice(e,p,1,t),h++,p++):(this.splice(e,p,0,t),p++)}else this.splice(e,p,1),h++}}const g=h>c,y=p>a;if(g&&!y)for(;p<=a;++p){const t=r[p],n=D(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 te)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 O?t.insertEmbed(s,e._map):e instanceof Z&&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&&F(94),void this.append(o);const i=l.getOffset();-1===i&&F(95);const r=this._xmlText;if(0!==n&&r.delete(i,l.getSize()),o instanceof te)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 O?r.insertEmbed(i,o._map):o instanceof Z&&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 ne(e,t,n){const o=new te(e,t,n);return e._collabNode=o,o}function oe(e,t,n,o,s,l){null==o&&F(81);const i=ne(o.get("root",h),null,"root");return i._key="root",{clientID:o.clientID,collabNodeMap:new Map,cursors:new Map,cursorsContainer:null,doc:o,docMap:s,editor:e,excludedProperties:l||new Map,id:n,nodeProperties:new Map,root:i}}function se(e,t){const o=t.collabNodeMap.get(e.key);if(void 0===o)return null;let s=e.offset,i=o.getSharedType();if(o instanceof E){i=o._parent._xmlText;const e=o.getOffset();if(-1===e)return null;s=e+1+s}else if(o instanceof te&&"element"===e.type){const t=e.getNode();l(t)||F(184);let o=0,i=0,r=t.getFirstChild();for(;null!==r&&i++<s;)n(r)?o+=r.getTextContentSize()+1:o++,r=r.getNextSibling();s=o}return m(i,s)}function le(e,t){return x(e,t.doc)}function ie(e,t){if(null==e){if(null!=t)return!0}else if(null==t||!b(e,t))return!0;return!1}function re(e,t){return{color:t,name:e,selection:null}}function ce(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 ae(e,t){const n=t.selection;null!==n&&ce(e,n)}function fe(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 ue(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 ce(e,f));n.selection=o;const u=o.caret,d=o.color,_=o.selections,h=o.anchor,p=o.focus,g=h.key,y=p.key,x=s.get(g),m=s.get(y);if(null==x||null==m)return;let b;if(x===m&&t(x)){b=[l.getElementByKey(g).getBoundingClientRect()]}else{const e=S(l,x,h.offset,m,p.offset);if(null===e)return;b=P(l,e)}const k=_.length,v=b.length;for(let e=0;e<v;e++){const t=b[e];let n=_[e];if(void 0===n){n=document.createElement("span"),_[e]=n;const t=document.createElement("span");n.appendChild(t),r.appendChild(n)}const o=`position:absolute;top:${t.top-a.top}px;left:${t.left-a.left}px;height:${t.height}px;width:${t.width}px;pointer-events:none;z-index:5;`;n.style.cssText=o,n.firstChild.style.cssText=`${o}left:0;top:0;background-color:${d};opacity:0.3;`,e===v-1&&u.parentNode!==n&&n.appendChild(u)}for(let e=k-1;e>=v;e--){const t=_[e];r.removeChild(t),_.pop()}}function de(e,t){const{anchorPos:n,focusPos:o}=t;let s=null,l=0,i=null,r=0;if(null!==n&&null!==o){const t=le(n,e),c=le(o,e);null!==t&&null!==c&&([s,l]=pe(t.type,t.index),[i,r]=pe(c.type,c.index))}return{anchorCollabNode:s,anchorOffset:l,focusCollabNode:i,focusOffset:r}}function _e(e,t){const n=t.awareness.getLocalState();if(null===n)return;const{anchorCollabNode:l,anchorOffset:i,focusCollabNode:r,focusOffset:c}=de(e,n);if(null!==l&&null!==r){const e=l.getKey(),t=r.getKey(),n=o();if(!s(n))return;he(n.anchor,e,i),he(n.focus,t,c)}}function he(t,o,s){if(t.key!==o||t.offset!==s){let i=e(o);if(null!==i&&!l(i)&&!n(i)){const e=i.getParentOrThrow();o=e.getKey(),s=i.getIndexWithinParent(),i=e}t.set(o,s,l(i)?"element":"text")}}function pe(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof te){const{node:e,offset:o}=H(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function ge(e,t){const n=Array.from(t.awareness.getStates()),o=e.clientID,s=e.cursors,l=e.editor._editorState._nodeMap,i=new Set;for(let t=0;t<n.length;t++){const r=n[t],[c,a]=r;if(c!==o){i.add(c);const{name:t,color:n,focusing:o}=a;let r=null,f=s.get(c);if(void 0===f&&(f=re(t,n),s.set(c,f)),o){const{anchorCollabNode:t,anchorOffset:n,focusCollabNode:o,focusOffset:s}=de(e,a);if(null!==t&&null!==o){const e=t.getKey(),l=o.getKey();if(r=f.selection,null===r)r=fe(f,e,n,l,s);else{const t=r.anchor,o=r.focus;t.key=e,t.offset=n,o.key=l,o.offset=s}}}ue(e,f,r,l)}}const r=Array.from(s.keys());for(let t=0;t<r.length;t++){const n=r[t];if(!i.has(n)){const t=s.get(n);void 0!==t&&(ae(e,t),s.delete(n))}}}function ye(e,t,n,o){const l=t.awareness,i=l.getLocalState();if(null===i)return;const{anchorPos:r,focusPos:c,name:a,color:f,focusing:u,awarenessData:d}=i;let _=null,h=null;(null!==o&&(null===r||o.is(n))||null!==n)&&(s(o)&&(_=se(o.anchor,e),h=se(o.focus,e)),(ie(r,_)||ie(c,h))&&l.setLocalState({...i,anchorPos:_,awarenessData:d,color:f,focusPos:h,focusing:u,name:a}))}function xe(e,t){if(t instanceof k&&function(e,t){const{target:n}=t;if(!n._item||"__state"!==n._item.parentSub||void 0!==I(n)||!(n.parent instanceof h||n.parent instanceof g||n.parent instanceof p))return!1;const o=K(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=K(e,n);if(o instanceof te&&t instanceof v){const{keysChanged:n,childListChanged:s,delta:l}=t;n.size>0&&o.syncPropertiesFromYjs(e,n),s&&(o.applyChildrenYjsDelta(e,l),o.syncChildrenFromYjs(e))}else if(o instanceof E&&t instanceof k){const{keysChanged:n}=t;n.size>0&&o.syncPropertiesAndTextFromYjs(e,n)}else if(o instanceof Z&&t instanceof C){const{attributesChanged:n}=t;n.size>0&&o.syncPropertiesFromYjs(e,n)}else F(82)}function me(e,t,n,l,i=ge){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];xe(e,o)}const l=o();if(s(l))if(Q(l)){const n=c._selection;if(s(n)&&(_e(e,t),Q(l))){X(l.anchor.key,c)}ye(e,t,n,o())}else _e(e,t)}),{onUpdate:()=>{i(e,t),r.update((()=>{0===a().getChildrenSize()&&a().append(d())}))},skipTransforms:!0,tag:l?"historic":"collaboration"})}function be(t,s,l,i,r,c,f,u){!function(e,t){e.doc.transact(t,e)}(t,(()=>{i.read((()=>{if(u.has("collaboration")||u.has("historic"))return void(f.size>0&&function(t,o){const s=Array.from(o),l=t.collabNodeMap,i=[],r=[];for(let t=0;t<s.length;t++){const o=s[t],c=e(o),a=l.get(o);if(a instanceof E)if(n(c))i.push([a,c.__text]);else{const e=a.getOffset();if(-1===e)continue;const t=a._parent;a._normalized=!0,t._xmlText.delete(e,1),r.push(a)}}for(let e=0;e<r.length;e++){const t=r[e],n=t.getKey();l.delete(n);const o=t._parent._children,s=o.indexOf(t);o.splice(s,1)}for(let e=0;e<i.length;e++){const[t,n]=i[e];t._text=n}}(t,f));if(r.has("root")){const e=l._nodeMap,n=a(),o=t.root;o.syncPropertiesFromLexical(t,n,e),o.syncChildrenFromLexical(t,n,e,r,c)}const i=o(),d=l._selection;ye(t,s,d,i)}))}))}const ke=_("CONNECTED_COMMAND"),ve=_("TOGGLE_CONNECT_COMMAND");function Ce(e,t){return new N(t,{trackedOrigins:new Set([e,null])})}function Ne(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})}function we(e,t,n,o,s){const{awareness:l}=e;let i=l.getLocalState();null===i&&(i={anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t}),i.focusing=o,l.setLocalState(i)}export{ke as CONNECTED_COMMAND,ve as TOGGLE_CONNECT_COMMAND,oe as createBinding,Ce as createUndoManager,de as getAnchorAndFocusCollabNodesForUserState,Ne as initLocalState,we as setLocalStateFocus,ge as syncCursorPositions,be as syncLexicalUpdateToYjs,me as syncYjsChangesToLexical};
|
package/Utils.d.ts
CHANGED
|
@@ -6,19 +6,18 @@
|
|
|
6
6
|
*
|
|
7
7
|
*/
|
|
8
8
|
import type { Binding, YjsNode } from '.';
|
|
9
|
-
import
|
|
10
|
-
import { NodeKey } from 'lexical';
|
|
9
|
+
import { EditorState, LexicalNode, NodeKey, RangeSelection } from 'lexical';
|
|
11
10
|
import { Map as YMap, XmlElement, XmlText } from 'yjs';
|
|
12
11
|
import { CollabDecoratorNode } from './CollabDecoratorNode';
|
|
13
12
|
import { CollabElementNode } from './CollabElementNode';
|
|
14
13
|
import { CollabLineBreakNode } from './CollabLineBreakNode';
|
|
15
14
|
import { CollabTextNode } from './CollabTextNode';
|
|
16
15
|
export declare function getIndexOfYjsNode(yjsParentNode: YjsNode, yjsNode: YjsNode): number;
|
|
17
|
-
export declare function $getNodeByKeyOrThrow(key: NodeKey): LexicalNode;
|
|
18
16
|
export declare function $createCollabNodeFromLexicalNode(binding: Binding, lexicalNode: LexicalNode, parent: CollabElementNode): CollabElementNode | CollabTextNode | CollabLineBreakNode | CollabDecoratorNode;
|
|
17
|
+
export declare function getNodeTypeFromSharedType(sharedType: XmlText | YMap<unknown> | XmlElement): string | undefined;
|
|
19
18
|
export declare function $getOrInitCollabNodeFromSharedType(binding: Binding, sharedType: XmlText | YMap<unknown> | XmlElement, parent?: CollabElementNode): CollabElementNode | CollabTextNode | CollabLineBreakNode | CollabDecoratorNode;
|
|
20
19
|
export declare function createLexicalNodeFromCollabNode(binding: Binding, collabNode: CollabElementNode | CollabTextNode | CollabDecoratorNode | CollabLineBreakNode, parentKey: NodeKey): LexicalNode;
|
|
21
|
-
export declare function syncPropertiesFromYjs(binding: Binding, sharedType: XmlText | YMap<unknown> | XmlElement, lexicalNode: LexicalNode, keysChanged: null | Set<string>): void;
|
|
20
|
+
export declare function $syncPropertiesFromYjs(binding: Binding, sharedType: XmlText | YMap<unknown> | XmlElement, lexicalNode: LexicalNode, keysChanged: null | Set<string>): void;
|
|
22
21
|
export declare function syncPropertiesFromLexical(binding: Binding, sharedType: XmlText | YMap<unknown> | XmlElement, prevLexicalNode: null | LexicalNode, nextLexicalNode: LexicalNode): void;
|
|
23
22
|
export declare function spliceString(str: string, index: number, delCount: number, newText: string): string;
|
|
24
23
|
export declare function getPositionFromElementAndOffset(node: CollabElementNode, offset: number, boundaryIsEdge: boolean): {
|
package/package.json
CHANGED
|
@@ -11,13 +11,13 @@
|
|
|
11
11
|
"crdt"
|
|
12
12
|
],
|
|
13
13
|
"license": "MIT",
|
|
14
|
-
"version": "0.
|
|
14
|
+
"version": "0.26.0",
|
|
15
15
|
"main": "LexicalYjs.js",
|
|
16
16
|
"types": "index.d.ts",
|
|
17
17
|
"dependencies": {
|
|
18
|
-
"@lexical/offset": "0.
|
|
19
|
-
"@lexical/selection": "0.
|
|
20
|
-
"lexical": "0.
|
|
18
|
+
"@lexical/offset": "0.26.0",
|
|
19
|
+
"@lexical/selection": "0.26.0",
|
|
20
|
+
"lexical": "0.26.0"
|
|
21
21
|
},
|
|
22
22
|
"peerDependencies": {
|
|
23
23
|
"yjs": ">=13.5.22"
|