@lexical/yjs 0.14.4 → 0.15.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 CHANGED
@@ -3,13 +3,15 @@
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
+ *
6
7
  */
8
+
7
9
  'use strict';
8
10
 
9
11
  var lexical = require('lexical');
10
12
  var yjs = require('yjs');
11
- var selection = require('@lexical/selection');
12
13
  var offset = require('@lexical/offset');
14
+ var selection = require('@lexical/selection');
13
15
 
14
16
  /**
15
17
  * Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -18,6 +20,7 @@ var offset = require('@lexical/offset');
18
20
  * LICENSE file in the root directory of this source tree.
19
21
  *
20
22
  */
23
+
21
24
  class CollabLineBreakNode {
22
25
  constructor(map, parent) {
23
26
  this._key = '';
@@ -96,7 +99,8 @@ function simpleDiffWithCursor(a, b, cursor) {
96
99
  * LICENSE file in the root directory of this source tree.
97
100
  *
98
101
  */
99
- function diffTextContentAndApplyDelta(collabNode, key, prevText, nextText) {
102
+
103
+ function $diffTextContentAndApplyDelta(collabNode, key, prevText, nextText) {
100
104
  const selection = lexical.$getSelection();
101
105
  let cursorOffset = nextText.length;
102
106
  if (lexical.$isRangeSelection(selection) && selection.isCollapsed()) {
@@ -163,7 +167,7 @@ class CollabTextNode {
163
167
  const prevText = prevLexicalNode.__text;
164
168
  if (prevText !== nextText) {
165
169
  const key = nextLexicalNode.__key;
166
- diffTextContentAndApplyDelta(this, key, prevText, nextText);
170
+ $diffTextContentAndApplyDelta(this, key, prevText, nextText);
167
171
  this._text = nextText;
168
172
  }
169
173
  }
@@ -198,6 +202,7 @@ function $createCollabTextNode(map, text, parent, type) {
198
202
  * LICENSE file in the root directory of this source tree.
199
203
  *
200
204
  */
205
+
201
206
  const baseExcludedProperties = new Set(['__key', '__parent', '__next', '__prev']);
202
207
  const elementExcludedProperties = new Set(['__first', '__last', '__size']);
203
208
  const rootExcludedProperties = new Set(['__cachedText']);
@@ -262,7 +267,7 @@ function getNodeTypeFromSharedType(sharedType) {
262
267
  }
263
268
  return type;
264
269
  }
265
- function getOrInitCollabNodeFromSharedType(binding, sharedType, parent) {
270
+ function $getOrInitCollabNodeFromSharedType(binding, sharedType, parent) {
266
271
  const collabNode = sharedType._collabNode;
267
272
  if (collabNode === undefined) {
268
273
  const registeredNodes = binding.editor._nodes;
@@ -272,7 +277,7 @@ function getOrInitCollabNodeFromSharedType(binding, sharedType, parent) {
272
277
  throw Error(`Node ${type} is not registered`);
273
278
  }
274
279
  const sharedParent = sharedType.parent;
275
- const targetParent = parent === undefined && sharedParent !== null ? getOrInitCollabNodeFromSharedType(binding, sharedParent) : parent || null;
280
+ const targetParent = parent === undefined && sharedParent !== null ? $getOrInitCollabNodeFromSharedType(binding, sharedParent) : parent || null;
276
281
  if (!(targetParent instanceof CollabElementNode)) {
277
282
  throw Error(`Expected parent to be a collab element node`);
278
283
  }
@@ -463,21 +468,6 @@ function doesSelectionNeedRecovering(selection) {
463
468
  function syncWithTransaction(binding, fn) {
464
469
  binding.doc.transact(fn, binding);
465
470
  }
466
- function createChildrenArray(element, nodeMap) {
467
- const children = [];
468
- let nodeKey = element.__first;
469
- while (nodeKey !== null) {
470
- const node = nodeMap === null ? lexical.$getNodeByKey(nodeKey) : nodeMap.get(nodeKey);
471
- if (node === null || node === undefined) {
472
- {
473
- throw Error(`createChildrenArray: node does not exist in nodeMap`);
474
- }
475
- }
476
- children.push(nodeKey);
477
- nodeKey = node.__next;
478
- }
479
- return children;
480
- }
481
471
  function removeFromParent(node) {
482
472
  const oldParent = node.getParent();
483
473
  if (oldParent !== null) {
@@ -528,6 +518,35 @@ function removeFromParent(node) {
528
518
  writableNode.__parent = null;
529
519
  }
530
520
  }
521
+ function $moveSelectionToPreviousNode(anchorNodeKey, currentEditorState) {
522
+ const anchorNode = currentEditorState._nodeMap.get(anchorNodeKey);
523
+ if (!anchorNode) {
524
+ lexical.$getRoot().selectStart();
525
+ return;
526
+ }
527
+ // Get previous node
528
+ const prevNodeKey = anchorNode.__prev;
529
+ let prevNode = null;
530
+ if (prevNodeKey) {
531
+ prevNode = lexical.$getNodeByKey(prevNodeKey);
532
+ }
533
+
534
+ // If previous node not found, get parent node
535
+ if (prevNode === null && anchorNode.__parent !== null) {
536
+ prevNode = lexical.$getNodeByKey(anchorNode.__parent);
537
+ }
538
+ if (prevNode === null) {
539
+ lexical.$getRoot().selectStart();
540
+ return;
541
+ }
542
+ if (prevNode !== null && prevNode.isAttached()) {
543
+ prevNode.selectEnd();
544
+ return;
545
+ } else {
546
+ // If the found node is also deleted, select the next one
547
+ $moveSelectionToPreviousNode(prevNode.__key, currentEditorState);
548
+ }
549
+ }
531
550
 
532
551
  /**
533
552
  * Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -536,6 +555,7 @@ function removeFromParent(node) {
536
555
  * LICENSE file in the root directory of this source tree.
537
556
  *
538
557
  */
558
+
539
559
  class CollabDecoratorNode {
540
560
  constructor(xmlElem, parent, type) {
541
561
  this._key = '';
@@ -601,6 +621,7 @@ function $createCollabDecoratorNode(xmlElem, parent, type) {
601
621
  * LICENSE file in the root directory of this source tree.
602
622
  *
603
623
  */
624
+
604
625
  class CollabElementNode {
605
626
  constructor(xmlText, parent, type) {
606
627
  this._key = '';
@@ -717,7 +738,7 @@ class CollabElementNode {
717
738
  const {
718
739
  nodeIndex
719
740
  } = getPositionFromElementAndOffset(this, currIndex, false);
720
- const collabNode = getOrInitCollabNodeFromSharedType(binding, sharedType, this);
741
+ const collabNode = $getOrInitCollabNodeFromSharedType(binding, sharedType, this);
721
742
  children.splice(nodeIndex, 0, collabNode);
722
743
  currIndex += 1;
723
744
  }
@@ -733,7 +754,7 @@ class CollabElementNode {
733
754
  throw Error(`syncChildrenFromYjs: could not find element node`);
734
755
  }
735
756
  const key = lexicalNode.__key;
736
- const prevLexicalChildrenKeys = createChildrenArray(lexicalNode, null);
757
+ const prevLexicalChildrenKeys = offset.$createChildrenArray(lexicalNode, null);
737
758
  const lexicalChildrenKeysLength = prevLexicalChildrenKeys.length;
738
759
  const collabChildren = this._children;
739
760
  const collabChildrenLength = collabChildren.length;
@@ -854,8 +875,8 @@ class CollabElementNode {
854
875
  }
855
876
  syncChildrenFromLexical(binding, nextLexicalNode, prevNodeMap, dirtyElements, dirtyLeaves) {
856
877
  const prevLexicalNode = this.getPrevNode(prevNodeMap);
857
- const prevChildren = prevLexicalNode === null ? [] : createChildrenArray(prevLexicalNode, prevNodeMap);
858
- const nextChildren = createChildrenArray(nextLexicalNode, null);
878
+ const prevChildren = prevLexicalNode === null ? [] : offset.$createChildrenArray(prevLexicalNode, prevNodeMap);
879
+ const nextChildren = offset.$createChildrenArray(nextLexicalNode, null);
859
880
  const prevEndIndex = prevChildren.length - 1;
860
881
  const nextEndIndex = nextChildren.length - 1;
861
882
  const collabNodeMap = binding.collabNodeMap;
@@ -1015,6 +1036,7 @@ function $createCollabElementNode(xmlText, parent, type) {
1015
1036
  * LICENSE file in the root directory of this source tree.
1016
1037
  *
1017
1038
  */
1039
+
1018
1040
  function createBinding(editor, provider, id, doc, docMap, excludedProperties) {
1019
1041
  if (!(doc !== undefined && doc !== null)) {
1020
1042
  throw Error(`createBinding: doc is null or undefined`);
@@ -1044,6 +1066,7 @@ function createBinding(editor, provider, id, doc, docMap, excludedProperties) {
1044
1066
  * LICENSE file in the root directory of this source tree.
1045
1067
  *
1046
1068
  */
1069
+
1047
1070
  function createRelativePosition(point, binding) {
1048
1071
  const collabNodeMap = binding.collabNodeMap;
1049
1072
  const collabNode = collabNodeMap.get(point.key);
@@ -1220,7 +1243,7 @@ function updateCursor(binding, cursor, nextSelection, nodeMap) {
1220
1243
  selections.pop();
1221
1244
  }
1222
1245
  }
1223
- function syncLocalCursorPosition(binding, provider) {
1246
+ function $syncLocalCursorPosition(binding, provider) {
1224
1247
  const awareness = provider.awareness;
1225
1248
  const localState = awareness.getLocalState();
1226
1249
  if (localState === null) {
@@ -1243,13 +1266,13 @@ function syncLocalCursorPosition(binding, provider) {
1243
1266
  }
1244
1267
  const anchor = selection.anchor;
1245
1268
  const focus = selection.focus;
1246
- setPoint(anchor, anchorKey, anchorOffset);
1247
- setPoint(focus, focusKey, focusOffset);
1269
+ $setPoint(anchor, anchorKey, anchorOffset);
1270
+ $setPoint(focus, focusKey, focusOffset);
1248
1271
  }
1249
1272
  }
1250
1273
  }
1251
1274
  }
1252
- function setPoint(point, key, offset) {
1275
+ function $setPoint(point, key, offset) {
1253
1276
  if (point.key !== key || point.offset !== offset) {
1254
1277
  let anchorNode = lexical.$getNodeByKey(key);
1255
1278
  if (anchorNode !== null && !lexical.$isElementNode(anchorNode) && !lexical.$isTextNode(anchorNode)) {
@@ -1389,12 +1412,13 @@ function syncLexicalSelectionToYjs(binding, provider, prevSelection, nextSelecti
1389
1412
  *
1390
1413
  */
1391
1414
 
1415
+
1392
1416
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1393
- function syncEvent(binding, event) {
1417
+ function $syncEvent(binding, event) {
1394
1418
  const {
1395
1419
  target
1396
1420
  } = event;
1397
- const collabNode = getOrInitCollabNodeFromSharedType(binding, target);
1421
+ const collabNode = $getOrInitCollabNodeFromSharedType(binding, target);
1398
1422
  if (collabNode instanceof CollabElementNode && event instanceof yjs.YTextEvent) {
1399
1423
  // @ts-expect-error We need to access the private property of the class
1400
1424
  const {
@@ -1446,10 +1470,9 @@ function syncYjsChangesToLexical(binding, provider, events, isFromUndoManger) {
1446
1470
  // https://github.com/yjs/yjs/blob/00ef472d68545cb260abd35c2de4b3b78719c9e4/src/utils/YEvent.js#L132
1447
1471
  events.forEach(event => event.delta);
1448
1472
  editor.update(() => {
1449
- const pendingEditorState = editor._pendingEditorState;
1450
1473
  for (let i = 0; i < events.length; i++) {
1451
1474
  const event = events[i];
1452
- syncEvent(binding, event);
1475
+ $syncEvent(binding, event);
1453
1476
  }
1454
1477
  // If there was a collision on the top level paragraph
1455
1478
  // we need to re-add a paragraph
@@ -1458,30 +1481,19 @@ function syncYjsChangesToLexical(binding, provider, events, isFromUndoManger) {
1458
1481
  }
1459
1482
  const selection = lexical.$getSelection();
1460
1483
  if (lexical.$isRangeSelection(selection)) {
1461
- // We can't use Yjs's cursor position here, as it doesn't always
1462
- // handle selection recovery correctly – especially on elements that
1463
- // get moved around or split. So instead, we roll our own solution.
1464
1484
  if (doesSelectionNeedRecovering(selection)) {
1465
1485
  const prevSelection = currentEditorState._selection;
1466
1486
  if (lexical.$isRangeSelection(prevSelection)) {
1467
- const prevOffsetView = offset.$createOffsetView(editor, 0, currentEditorState);
1468
- const nextOffsetView = offset.$createOffsetView(editor, 0, pendingEditorState);
1469
- const [start, end] = prevOffsetView.getOffsetsFromSelection(prevSelection);
1470
- const nextSelection = start >= 0 && end >= 0 ? nextOffsetView.createSelectionFromOffsets(start, end, prevOffsetView) : null;
1471
- if (nextSelection !== null) {
1472
- lexical.$setSelection(nextSelection);
1473
- } else {
1474
- // Fallback is to use the Yjs cursor position
1475
- syncLocalCursorPosition(binding, provider);
1476
- if (doesSelectionNeedRecovering(selection)) {
1477
- // Fallback
1478
- lexical.$getRoot().selectEnd();
1479
- }
1487
+ $syncLocalCursorPosition(binding, provider);
1488
+ if (doesSelectionNeedRecovering(selection)) {
1489
+ // If the selected node is deleted, move the selection to the previous or parent node.
1490
+ const anchorNodeKey = selection.anchor.key;
1491
+ $moveSelectionToPreviousNode(anchorNodeKey, currentEditorState);
1480
1492
  }
1481
1493
  }
1482
1494
  syncLexicalSelectionToYjs(binding, provider, prevSelection, lexical.$getSelection());
1483
1495
  } else {
1484
- syncLocalCursorPosition(binding, provider);
1496
+ $syncLocalCursorPosition(binding, provider);
1485
1497
  }
1486
1498
  }
1487
1499
  }, {
@@ -1492,7 +1504,7 @@ function syncYjsChangesToLexical(binding, provider, events, isFromUndoManger) {
1492
1504
  tag: isFromUndoManger ? 'historic' : 'collaboration'
1493
1505
  });
1494
1506
  }
1495
- function handleNormalizationMergeConflicts(binding, normalizedNodes) {
1507
+ function $handleNormalizationMergeConflicts(binding, normalizedNodes) {
1496
1508
  // We handle the merge operations here
1497
1509
  const normalizedNodesKeys = Array.from(normalizedNodes);
1498
1510
  const collabNodeMap = binding.collabNodeMap;
@@ -1540,7 +1552,7 @@ function syncLexicalUpdateToYjs(binding, provider, prevEditorState, currEditorSt
1540
1552
  // when we need to handle normalization merge conflicts.
1541
1553
  if (tags.has('collaboration') || tags.has('historic')) {
1542
1554
  if (normalizedNodes.size > 0) {
1543
- handleNormalizationMergeConflicts(binding, normalizedNodes);
1555
+ $handleNormalizationMergeConflicts(binding, normalizedNodes);
1544
1556
  }
1545
1557
  return;
1546
1558
  }
@@ -1565,6 +1577,7 @@ function syncLexicalUpdateToYjs(binding, provider, prevEditorState, currEditorSt
1565
1577
  * LICENSE file in the root directory of this source tree.
1566
1578
  *
1567
1579
  */
1580
+
1568
1581
  const CONNECTED_COMMAND = lexical.createCommand('CONNECTED_COMMAND');
1569
1582
  const TOGGLE_CONNECT_COMMAND = lexical.createCommand('TOGGLE_CONNECT_COMMAND');
1570
1583
  function createUndoManager(binding, root) {
@@ -3,11 +3,13 @@
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
+ *
6
7
  */
7
- import { $getNodeByKey, $isLineBreakNode, $isTextNode, $getSelection, $isRangeSelection, createEditor, $isElementNode, $isRootNode, $isDecoratorNode, $getRoot, $createParagraphNode, $setSelection, createCommand } from 'lexical';
8
+
9
+ import { $getNodeByKey, $isLineBreakNode, $isTextNode, $getSelection, $isRangeSelection, createEditor, $getRoot, $isElementNode, $isRootNode, $isDecoratorNode, $createParagraphNode, createCommand } from 'lexical';
8
10
  import { XmlText, Map as Map$1, XmlElement, Doc, createAbsolutePositionFromRelativePosition, createRelativePositionFromTypeIndex, compareRelativePositions, YTextEvent, YMapEvent, YXmlEvent, UndoManager } from 'yjs';
11
+ import { $createChildrenArray } from '@lexical/offset';
9
12
  import { createDOMRange, createRectsFromDOMRange } from '@lexical/selection';
10
- import { $createOffsetView } from '@lexical/offset';
11
13
 
12
14
  /**
13
15
  * Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -16,6 +18,7 @@ import { $createOffsetView } from '@lexical/offset';
16
18
  * LICENSE file in the root directory of this source tree.
17
19
  *
18
20
  */
21
+
19
22
  class CollabLineBreakNode {
20
23
  constructor(map, parent) {
21
24
  this._key = '';
@@ -94,7 +97,8 @@ function simpleDiffWithCursor(a, b, cursor) {
94
97
  * LICENSE file in the root directory of this source tree.
95
98
  *
96
99
  */
97
- function diffTextContentAndApplyDelta(collabNode, key, prevText, nextText) {
100
+
101
+ function $diffTextContentAndApplyDelta(collabNode, key, prevText, nextText) {
98
102
  const selection = $getSelection();
99
103
  let cursorOffset = nextText.length;
100
104
  if ($isRangeSelection(selection) && selection.isCollapsed()) {
@@ -161,7 +165,7 @@ class CollabTextNode {
161
165
  const prevText = prevLexicalNode.__text;
162
166
  if (prevText !== nextText) {
163
167
  const key = nextLexicalNode.__key;
164
- diffTextContentAndApplyDelta(this, key, prevText, nextText);
168
+ $diffTextContentAndApplyDelta(this, key, prevText, nextText);
165
169
  this._text = nextText;
166
170
  }
167
171
  }
@@ -196,6 +200,7 @@ function $createCollabTextNode(map, text, parent, type) {
196
200
  * LICENSE file in the root directory of this source tree.
197
201
  *
198
202
  */
203
+
199
204
  const baseExcludedProperties = new Set(['__key', '__parent', '__next', '__prev']);
200
205
  const elementExcludedProperties = new Set(['__first', '__last', '__size']);
201
206
  const rootExcludedProperties = new Set(['__cachedText']);
@@ -260,7 +265,7 @@ function getNodeTypeFromSharedType(sharedType) {
260
265
  }
261
266
  return type;
262
267
  }
263
- function getOrInitCollabNodeFromSharedType(binding, sharedType, parent) {
268
+ function $getOrInitCollabNodeFromSharedType(binding, sharedType, parent) {
264
269
  const collabNode = sharedType._collabNode;
265
270
  if (collabNode === undefined) {
266
271
  const registeredNodes = binding.editor._nodes;
@@ -270,7 +275,7 @@ function getOrInitCollabNodeFromSharedType(binding, sharedType, parent) {
270
275
  throw Error(`Node ${type} is not registered`);
271
276
  }
272
277
  const sharedParent = sharedType.parent;
273
- const targetParent = parent === undefined && sharedParent !== null ? getOrInitCollabNodeFromSharedType(binding, sharedParent) : parent || null;
278
+ const targetParent = parent === undefined && sharedParent !== null ? $getOrInitCollabNodeFromSharedType(binding, sharedParent) : parent || null;
274
279
  if (!(targetParent instanceof CollabElementNode)) {
275
280
  throw Error(`Expected parent to be a collab element node`);
276
281
  }
@@ -461,21 +466,6 @@ function doesSelectionNeedRecovering(selection) {
461
466
  function syncWithTransaction(binding, fn) {
462
467
  binding.doc.transact(fn, binding);
463
468
  }
464
- function createChildrenArray(element, nodeMap) {
465
- const children = [];
466
- let nodeKey = element.__first;
467
- while (nodeKey !== null) {
468
- const node = nodeMap === null ? $getNodeByKey(nodeKey) : nodeMap.get(nodeKey);
469
- if (node === null || node === undefined) {
470
- {
471
- throw Error(`createChildrenArray: node does not exist in nodeMap`);
472
- }
473
- }
474
- children.push(nodeKey);
475
- nodeKey = node.__next;
476
- }
477
- return children;
478
- }
479
469
  function removeFromParent(node) {
480
470
  const oldParent = node.getParent();
481
471
  if (oldParent !== null) {
@@ -526,6 +516,35 @@ function removeFromParent(node) {
526
516
  writableNode.__parent = null;
527
517
  }
528
518
  }
519
+ function $moveSelectionToPreviousNode(anchorNodeKey, currentEditorState) {
520
+ const anchorNode = currentEditorState._nodeMap.get(anchorNodeKey);
521
+ if (!anchorNode) {
522
+ $getRoot().selectStart();
523
+ return;
524
+ }
525
+ // Get previous node
526
+ const prevNodeKey = anchorNode.__prev;
527
+ let prevNode = null;
528
+ if (prevNodeKey) {
529
+ prevNode = $getNodeByKey(prevNodeKey);
530
+ }
531
+
532
+ // If previous node not found, get parent node
533
+ if (prevNode === null && anchorNode.__parent !== null) {
534
+ prevNode = $getNodeByKey(anchorNode.__parent);
535
+ }
536
+ if (prevNode === null) {
537
+ $getRoot().selectStart();
538
+ return;
539
+ }
540
+ if (prevNode !== null && prevNode.isAttached()) {
541
+ prevNode.selectEnd();
542
+ return;
543
+ } else {
544
+ // If the found node is also deleted, select the next one
545
+ $moveSelectionToPreviousNode(prevNode.__key, currentEditorState);
546
+ }
547
+ }
529
548
 
530
549
  /**
531
550
  * Copyright (c) Meta Platforms, Inc. and affiliates.
@@ -534,6 +553,7 @@ function removeFromParent(node) {
534
553
  * LICENSE file in the root directory of this source tree.
535
554
  *
536
555
  */
556
+
537
557
  class CollabDecoratorNode {
538
558
  constructor(xmlElem, parent, type) {
539
559
  this._key = '';
@@ -599,6 +619,7 @@ function $createCollabDecoratorNode(xmlElem, parent, type) {
599
619
  * LICENSE file in the root directory of this source tree.
600
620
  *
601
621
  */
622
+
602
623
  class CollabElementNode {
603
624
  constructor(xmlText, parent, type) {
604
625
  this._key = '';
@@ -715,7 +736,7 @@ class CollabElementNode {
715
736
  const {
716
737
  nodeIndex
717
738
  } = getPositionFromElementAndOffset(this, currIndex, false);
718
- const collabNode = getOrInitCollabNodeFromSharedType(binding, sharedType, this);
739
+ const collabNode = $getOrInitCollabNodeFromSharedType(binding, sharedType, this);
719
740
  children.splice(nodeIndex, 0, collabNode);
720
741
  currIndex += 1;
721
742
  }
@@ -731,7 +752,7 @@ class CollabElementNode {
731
752
  throw Error(`syncChildrenFromYjs: could not find element node`);
732
753
  }
733
754
  const key = lexicalNode.__key;
734
- const prevLexicalChildrenKeys = createChildrenArray(lexicalNode, null);
755
+ const prevLexicalChildrenKeys = $createChildrenArray(lexicalNode, null);
735
756
  const lexicalChildrenKeysLength = prevLexicalChildrenKeys.length;
736
757
  const collabChildren = this._children;
737
758
  const collabChildrenLength = collabChildren.length;
@@ -852,8 +873,8 @@ class CollabElementNode {
852
873
  }
853
874
  syncChildrenFromLexical(binding, nextLexicalNode, prevNodeMap, dirtyElements, dirtyLeaves) {
854
875
  const prevLexicalNode = this.getPrevNode(prevNodeMap);
855
- const prevChildren = prevLexicalNode === null ? [] : createChildrenArray(prevLexicalNode, prevNodeMap);
856
- const nextChildren = createChildrenArray(nextLexicalNode, null);
876
+ const prevChildren = prevLexicalNode === null ? [] : $createChildrenArray(prevLexicalNode, prevNodeMap);
877
+ const nextChildren = $createChildrenArray(nextLexicalNode, null);
857
878
  const prevEndIndex = prevChildren.length - 1;
858
879
  const nextEndIndex = nextChildren.length - 1;
859
880
  const collabNodeMap = binding.collabNodeMap;
@@ -1013,6 +1034,7 @@ function $createCollabElementNode(xmlText, parent, type) {
1013
1034
  * LICENSE file in the root directory of this source tree.
1014
1035
  *
1015
1036
  */
1037
+
1016
1038
  function createBinding(editor, provider, id, doc, docMap, excludedProperties) {
1017
1039
  if (!(doc !== undefined && doc !== null)) {
1018
1040
  throw Error(`createBinding: doc is null or undefined`);
@@ -1042,6 +1064,7 @@ function createBinding(editor, provider, id, doc, docMap, excludedProperties) {
1042
1064
  * LICENSE file in the root directory of this source tree.
1043
1065
  *
1044
1066
  */
1067
+
1045
1068
  function createRelativePosition(point, binding) {
1046
1069
  const collabNodeMap = binding.collabNodeMap;
1047
1070
  const collabNode = collabNodeMap.get(point.key);
@@ -1218,7 +1241,7 @@ function updateCursor(binding, cursor, nextSelection, nodeMap) {
1218
1241
  selections.pop();
1219
1242
  }
1220
1243
  }
1221
- function syncLocalCursorPosition(binding, provider) {
1244
+ function $syncLocalCursorPosition(binding, provider) {
1222
1245
  const awareness = provider.awareness;
1223
1246
  const localState = awareness.getLocalState();
1224
1247
  if (localState === null) {
@@ -1241,13 +1264,13 @@ function syncLocalCursorPosition(binding, provider) {
1241
1264
  }
1242
1265
  const anchor = selection.anchor;
1243
1266
  const focus = selection.focus;
1244
- setPoint(anchor, anchorKey, anchorOffset);
1245
- setPoint(focus, focusKey, focusOffset);
1267
+ $setPoint(anchor, anchorKey, anchorOffset);
1268
+ $setPoint(focus, focusKey, focusOffset);
1246
1269
  }
1247
1270
  }
1248
1271
  }
1249
1272
  }
1250
- function setPoint(point, key, offset) {
1273
+ function $setPoint(point, key, offset) {
1251
1274
  if (point.key !== key || point.offset !== offset) {
1252
1275
  let anchorNode = $getNodeByKey(key);
1253
1276
  if (anchorNode !== null && !$isElementNode(anchorNode) && !$isTextNode(anchorNode)) {
@@ -1387,12 +1410,13 @@ function syncLexicalSelectionToYjs(binding, provider, prevSelection, nextSelecti
1387
1410
  *
1388
1411
  */
1389
1412
 
1413
+
1390
1414
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1391
- function syncEvent(binding, event) {
1415
+ function $syncEvent(binding, event) {
1392
1416
  const {
1393
1417
  target
1394
1418
  } = event;
1395
- const collabNode = getOrInitCollabNodeFromSharedType(binding, target);
1419
+ const collabNode = $getOrInitCollabNodeFromSharedType(binding, target);
1396
1420
  if (collabNode instanceof CollabElementNode && event instanceof YTextEvent) {
1397
1421
  // @ts-expect-error We need to access the private property of the class
1398
1422
  const {
@@ -1444,10 +1468,9 @@ function syncYjsChangesToLexical(binding, provider, events, isFromUndoManger) {
1444
1468
  // https://github.com/yjs/yjs/blob/00ef472d68545cb260abd35c2de4b3b78719c9e4/src/utils/YEvent.js#L132
1445
1469
  events.forEach(event => event.delta);
1446
1470
  editor.update(() => {
1447
- const pendingEditorState = editor._pendingEditorState;
1448
1471
  for (let i = 0; i < events.length; i++) {
1449
1472
  const event = events[i];
1450
- syncEvent(binding, event);
1473
+ $syncEvent(binding, event);
1451
1474
  }
1452
1475
  // If there was a collision on the top level paragraph
1453
1476
  // we need to re-add a paragraph
@@ -1456,30 +1479,19 @@ function syncYjsChangesToLexical(binding, provider, events, isFromUndoManger) {
1456
1479
  }
1457
1480
  const selection = $getSelection();
1458
1481
  if ($isRangeSelection(selection)) {
1459
- // We can't use Yjs's cursor position here, as it doesn't always
1460
- // handle selection recovery correctly – especially on elements that
1461
- // get moved around or split. So instead, we roll our own solution.
1462
1482
  if (doesSelectionNeedRecovering(selection)) {
1463
1483
  const prevSelection = currentEditorState._selection;
1464
1484
  if ($isRangeSelection(prevSelection)) {
1465
- const prevOffsetView = $createOffsetView(editor, 0, currentEditorState);
1466
- const nextOffsetView = $createOffsetView(editor, 0, pendingEditorState);
1467
- const [start, end] = prevOffsetView.getOffsetsFromSelection(prevSelection);
1468
- const nextSelection = start >= 0 && end >= 0 ? nextOffsetView.createSelectionFromOffsets(start, end, prevOffsetView) : null;
1469
- if (nextSelection !== null) {
1470
- $setSelection(nextSelection);
1471
- } else {
1472
- // Fallback is to use the Yjs cursor position
1473
- syncLocalCursorPosition(binding, provider);
1474
- if (doesSelectionNeedRecovering(selection)) {
1475
- // Fallback
1476
- $getRoot().selectEnd();
1477
- }
1485
+ $syncLocalCursorPosition(binding, provider);
1486
+ if (doesSelectionNeedRecovering(selection)) {
1487
+ // If the selected node is deleted, move the selection to the previous or parent node.
1488
+ const anchorNodeKey = selection.anchor.key;
1489
+ $moveSelectionToPreviousNode(anchorNodeKey, currentEditorState);
1478
1490
  }
1479
1491
  }
1480
1492
  syncLexicalSelectionToYjs(binding, provider, prevSelection, $getSelection());
1481
1493
  } else {
1482
- syncLocalCursorPosition(binding, provider);
1494
+ $syncLocalCursorPosition(binding, provider);
1483
1495
  }
1484
1496
  }
1485
1497
  }, {
@@ -1490,7 +1502,7 @@ function syncYjsChangesToLexical(binding, provider, events, isFromUndoManger) {
1490
1502
  tag: isFromUndoManger ? 'historic' : 'collaboration'
1491
1503
  });
1492
1504
  }
1493
- function handleNormalizationMergeConflicts(binding, normalizedNodes) {
1505
+ function $handleNormalizationMergeConflicts(binding, normalizedNodes) {
1494
1506
  // We handle the merge operations here
1495
1507
  const normalizedNodesKeys = Array.from(normalizedNodes);
1496
1508
  const collabNodeMap = binding.collabNodeMap;
@@ -1538,7 +1550,7 @@ function syncLexicalUpdateToYjs(binding, provider, prevEditorState, currEditorSt
1538
1550
  // when we need to handle normalization merge conflicts.
1539
1551
  if (tags.has('collaboration') || tags.has('historic')) {
1540
1552
  if (normalizedNodes.size > 0) {
1541
- handleNormalizationMergeConflicts(binding, normalizedNodes);
1553
+ $handleNormalizationMergeConflicts(binding, normalizedNodes);
1542
1554
  }
1543
1555
  return;
1544
1556
  }
@@ -1563,6 +1575,7 @@ function syncLexicalUpdateToYjs(binding, provider, prevEditorState, currEditorSt
1563
1575
  * LICENSE file in the root directory of this source tree.
1564
1576
  *
1565
1577
  */
1578
+
1566
1579
  const CONNECTED_COMMAND = createCommand('CONNECTED_COMMAND');
1567
1580
  const TOGGLE_CONNECT_COMMAND = createCommand('TOGGLE_CONNECT_COMMAND');
1568
1581
  function createUndoManager(binding, root) {
package/LexicalYjs.js CHANGED
@@ -3,7 +3,9 @@
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
+ *
6
7
  */
8
+
7
9
  'use strict'
8
10
  const LexicalYjs = process.env.NODE_ENV === 'development' ? require('./LexicalYjs.dev.js') : require('./LexicalYjs.prod.js');
9
11
  module.exports = LexicalYjs;
package/LexicalYjs.mjs CHANGED
@@ -3,7 +3,9 @@
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
+ *
6
7
  */
8
+
7
9
  import * as modDev from './LexicalYjs.dev.mjs';
8
10
  import * as modProd from './LexicalYjs.prod.mjs';
9
11
  const mod = process.env.NODE_ENV === 'development' ? modDev : modProd;
@@ -3,7 +3,9 @@
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
+ *
6
7
  */
8
+
7
9
  const mod = await (process.env.NODE_ENV === 'development' ? import('./LexicalYjs.dev.mjs') : import('./LexicalYjs.prod.mjs'));
8
10
  export const CONNECTED_COMMAND = mod.CONNECTED_COMMAND;
9
11
  export const TOGGLE_CONNECT_COMMAND = mod.TOGGLE_CONNECT_COMMAND;
@@ -3,48 +3,50 @@
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
+ *
6
7
  */
7
- 'use strict';var r=require("lexical"),v=require("yjs"),y=require("@lexical/selection"),z=require("@lexical/offset");function A(a){let b=new URLSearchParams;b.append("code",a);for(let c=1;c<arguments.length;c++)b.append("v",arguments[c]);throw Error(`Minified Lexical error #${a}; visit https://lexical.dev/docs/error?${b} for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}
8
- class C{constructor(a,b){this._key="";this._map=a;this._parent=b;this._type="linebreak"}getNode(){let a=r.$getNodeByKey(this._key);return r.$isLineBreakNode(a)?a:null}getKey(){return this._key}getSharedType(){return this._map}getType(){return this._type}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}destroy(a){a.collabNodeMap.delete(this._key)}}function E(a,b){b=new C(a,b);return a._collabNode=b}
9
- class F{constructor(a,b,c,d){this._key="";this._map=a;this._parent=c;this._text=b;this._type=d;this._normalized=!1}getPrevNode(a){if(null===a)return null;a=a.get(this._key);return r.$isTextNode(a)?a:null}getNode(){let a=r.$getNodeByKey(this._key);return r.$isTextNode(a)?a:null}getSharedType(){return this._map}getType(){return this._type}getKey(){return this._key}getSize(){return this._text.length+(this._normalized?0:1)}getOffset(){return this._parent.getChildOffset(this)}spliceText(a,b,c){let d=this._parent._xmlText;
10
- a=this.getOffset()+1+a;0!==b&&d.delete(a,b);""!==c&&d.insert(a,c)}syncPropertiesAndTextFromLexical(a,b,c){var d=this.getPrevNode(c);c=b.__text;G(a,this._map,d,b);if(null!==d&&(a=d.__text,a!==c)){d=b.__key;b=a;var e=r.$getSelection();a=c.length;r.$isRangeSelection(e)&&e.isCollapsed()&&(e=e.anchor,e.key===d&&(a=e.offset));d=b.length;let f=c.length,h=e=0;for(;e<d&&e<f&&b[e]===c[e]&&e<a;)e++;for(;h+e<d&&h+e<f&&b[d-h-1]===c[f-h-1];)h++;for(;h+e<d&&h+e<f&&b[e]===c[e];)e++;b=e;a=c.slice(e,f-h);d=d-e-h;this.spliceText(b,
11
- d,a);this._text=c}}syncPropertiesAndTextFromYjs(a,b){let c=this.getNode();null===c&&A(84);H(a,this._map,c,b);a=this._text;c.__text!==a&&(c.getWritable().__text=a)}destroy(a){a.collabNodeMap.delete(this._key)}}function I(a,b,c,d){b=new F(a,b,c,d);return a._collabNode=b}let aa=new Set(["__key","__parent","__next","__prev"]),ba=new Set(["__first","__last","__size"]),ja=new Set(["__cachedText"]),ka=new Set(["__text"]);
12
- function J(a,b,c){if(aa.has(a))return!0;if(r.$isTextNode(b)){if(ka.has(a))return!0}else if(r.$isElementNode(b)&&(ba.has(a)||r.$isRootNode(b)&&ja.has(a)))return!0;b=c.excludedProperties.get(b.constructor);return null!=b&&b.has(a)}function K(a){a=r.$getNodeByKey(a);null===a&&A(85);return a}
13
- function L(a,b,c){let d=b.__type;if(r.$isElementNode(b)){var e=new v.XmlText;e=M(e,c,d);e.syncPropertiesFromLexical(a,b,null);e.syncChildrenFromLexical(a,b,null,null,null)}else r.$isTextNode(b)?(e=new v.Map,e=I(e,b.__text,c,d),e.syncPropertiesAndTextFromLexical(a,b,null)):r.$isLineBreakNode(b)?(a=new v.Map,a.set("__type","linebreak"),e=E(a,c)):r.$isDecoratorNode(b)?(e=new v.XmlElement,e=N(e,c,d),e.syncPropertiesFromLexical(a,b,null)):A(86);e._key=b.__key;return e}
14
- function O(a,b,c){let d=b._collabNode;if(void 0===d){var e=a.editor._nodes;let f=b instanceof v.Map?b.get("__type"):b.getAttribute("__type");null==f&&A(87);void 0===e.get(f)&&A(88,f);e=b.parent;a=void 0===c&&null!==e?O(a,e):c||null;a instanceof P||A(89);if(b instanceof v.XmlText)return M(b,a,f);if(b instanceof v.Map)return"linebreak"===f?E(b,a):I(b,"",a,f);if(b instanceof v.XmlElement)return N(b,a,f)}return d}
15
- function H(a,b,c,d){d=null===d?b instanceof v.Map?Array.from(b.keys()):Object.keys(b.getAttributes()):Array.from(d);let e;for(let h=0;h<d.length;h++){let g=d[h];if(J(g,c,a))continue;var f=c[g];let m=b instanceof v.Map?b.get(g):b.getAttribute(g);if(f!==m){if(m instanceof v.Doc){let p=a.docMap;f instanceof v.Doc&&p.delete(f.guid);f=r.createEditor();let n=m.guid;f._key=n;p.set(n,m);m=f}void 0===e&&(e=c.getWritable());e[g]=m}}}
16
- function G(a,b,c,d){var e=d.__type,f=a.nodeProperties;let h=f.get(e);void 0===h&&(h=Object.keys(d).filter(m=>!J(m,d,a)),f.set(e,h));e=a.editor.constructor;for(f=0;f<h.length;f++){let m=h[f];var g=null===c?void 0:c[m];let p=d[m];if(g!==p){if(p instanceof e){let n=a.docMap,k;g instanceof e&&(g=g._key,k=n.get(g),n.delete(g));g=k||new v.Doc;let l=g.guid;p._key=l;n.set(l,g);p=g;a.editor.update(()=>{d.markDirty()})}b instanceof v.Map?b.set(m,p):b.setAttribute(m,p)}}}
17
- function Q(a,b,c){let d=0,e=0,f=a._children,h=f.length;for(;e<h;e++){a=f[e];let g=d,m=a.getSize();d+=m;if((c?d>=b:d>b)&&a instanceof F)return c=b-g-1,0>c&&(c=0),{length:d-b,node:a,nodeIndex:e,offset:c};if(d>b)return{length:0,node:a,nodeIndex:e,offset:g};if(e===h-1)return{length:0,node:null,nodeIndex:e+1,offset:g+1}}return{length:0,node:null,nodeIndex:0,offset:0}}
18
- function R(a){let b=a.anchor;a=a.focus;let c=!1;try{let d=b.getNode(),e=a.getNode();if(!d.isAttached()||!e.isAttached()||r.$isTextNode(d)&&b.offset>d.getTextContentSize()||r.$isTextNode(e)&&a.offset>e.getTextContentSize())c=!0}catch(d){c=!0}return c}function la(a,b){a.doc.transact(b,a)}function S(a,b){let c=[];for(a=a.__first;null!==a;){let d=null===b?r.$getNodeByKey(a):b.get(a);null!==d&&void 0!==d||A(101);c.push(a);a=d.__next}return c}
8
+
9
+ 'use strict';var n=require("lexical"),v=require("yjs"),x=require("@lexical/offset"),z=require("@lexical/selection"),A;function C(a){let b=new URLSearchParams;b.append("code",a);for(let c=1;c<arguments.length;c++)b.append("v",arguments[c]);throw Error(`Minified Lexical error #${a}; visit https://lexical.dev/docs/error?${b} for the full message or `+"use the non-minified dev environment for full errors and additional helpful warnings.");}
10
+ A=C&&C.__esModule&&Object.prototype.hasOwnProperty.call(C,"default")?C["default"]:C;class E{constructor(a,b){this._key="";this._map=a;this._parent=b;this._type="linebreak"}getNode(){let a=n.$getNodeByKey(this._key);return n.$isLineBreakNode(a)?a:null}getKey(){return this._key}getSharedType(){return this._map}getType(){return this._type}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}destroy(a){a.collabNodeMap.delete(this._key)}}
11
+ function F(a,b){b=new E(a,b);return a._collabNode=b}
12
+ class G{constructor(a,b,c,d){this._key="";this._map=a;this._parent=c;this._text=b;this._type=d;this._normalized=!1}getPrevNode(a){if(null===a)return null;a=a.get(this._key);return n.$isTextNode(a)?a:null}getNode(){let a=n.$getNodeByKey(this._key);return n.$isTextNode(a)?a:null}getSharedType(){return this._map}getType(){return this._type}getKey(){return this._key}getSize(){return this._text.length+(this._normalized?0:1)}getOffset(){return this._parent.getChildOffset(this)}spliceText(a,b,c){let d=this._parent._xmlText;
13
+ a=this.getOffset()+1+a;0!==b&&d.delete(a,b);""!==c&&d.insert(a,c)}syncPropertiesAndTextFromLexical(a,b,c){var d=this.getPrevNode(c);c=b.__text;H(a,this._map,d,b);if(null!==d&&(a=d.__text,a!==c)){d=b.__key;b=a;var e=n.$getSelection();a=c.length;n.$isRangeSelection(e)&&e.isCollapsed()&&(e=e.anchor,e.key===d&&(a=e.offset));d=b.length;let f=c.length,h=e=0;for(;e<d&&e<f&&b[e]===c[e]&&e<a;)e++;for(;h+e<d&&h+e<f&&b[d-h-1]===c[f-h-1];)h++;for(;h+e<d&&h+e<f&&b[e]===c[e];)e++;b=e;a=c.slice(e,f-h);d=d-e-h;this.spliceText(b,
14
+ d,a);this._text=c}}syncPropertiesAndTextFromYjs(a,b){let c=this.getNode();null===c&&A(84);I(a,this._map,c,b);a=this._text;c.__text!==a&&(c.getWritable().__text=a)}destroy(a){a.collabNodeMap.delete(this._key)}}function J(a,b,c,d){b=new G(a,b,c,d);return a._collabNode=b}let aa=new Set(["__key","__parent","__next","__prev"]),ba=new Set(["__first","__last","__size"]),ca=new Set(["__cachedText"]),ka=new Set(["__text"]);
15
+ function K(a,b,c){if(aa.has(a))return!0;if(n.$isTextNode(b)){if(ka.has(a))return!0}else if(n.$isElementNode(b)&&(ba.has(a)||n.$isRootNode(b)&&ca.has(a)))return!0;b=c.excludedProperties.get(b.constructor);return null!=b&&b.has(a)}function L(a){a=n.$getNodeByKey(a);null===a&&A(85);return a}
16
+ function M(a,b,c){let d=b.__type;if(n.$isElementNode(b)){var e=new v.XmlText;e=N(e,c,d);e.syncPropertiesFromLexical(a,b,null);e.syncChildrenFromLexical(a,b,null,null,null)}else n.$isTextNode(b)?(e=new v.Map,e=J(e,b.__text,c,d),e.syncPropertiesAndTextFromLexical(a,b,null)):n.$isLineBreakNode(b)?(a=new v.Map,a.set("__type","linebreak"),e=F(a,c)):n.$isDecoratorNode(b)?(e=new v.XmlElement,e=O(e,c,d),e.syncPropertiesFromLexical(a,b,null)):A(86);e._key=b.__key;return e}
17
+ function P(a,b,c){let d=b._collabNode;if(void 0===d){var e=a.editor._nodes;let f=b instanceof v.Map?b.get("__type"):b.getAttribute("__type");null==f&&A(87);void 0===e.get(f)&&A(88,f);e=b.parent;a=void 0===c&&null!==e?P(a,e):c||null;a instanceof Q||A(89);if(b instanceof v.XmlText)return N(b,a,f);if(b instanceof v.Map)return"linebreak"===f?F(b,a):J(b,"",a,f);if(b instanceof v.XmlElement)return O(b,a,f)}return d}
18
+ function I(a,b,c,d){d=null===d?b instanceof v.Map?Array.from(b.keys()):Object.keys(b.getAttributes()):Array.from(d);let e;for(let h=0;h<d.length;h++){let g=d[h];if(K(g,c,a))continue;var f=c[g];let k=b instanceof v.Map?b.get(g):b.getAttribute(g);if(f!==k){if(k instanceof v.Doc){let p=a.docMap;f instanceof v.Doc&&p.delete(f.guid);f=n.createEditor();let t=k.guid;f._key=t;p.set(t,k);k=f}void 0===e&&(e=c.getWritable());e[g]=k}}}
19
+ function H(a,b,c,d){var e=d.__type,f=a.nodeProperties;let h=f.get(e);void 0===h&&(h=Object.keys(d).filter(k=>!K(k,d,a)),f.set(e,h));e=a.editor.constructor;for(f=0;f<h.length;f++){let k=h[f];var g=null===c?void 0:c[k];let p=d[k];if(g!==p){if(p instanceof e){let t=a.docMap,m;g instanceof e&&(g=g._key,m=t.get(g),t.delete(g));g=m||new v.Doc;let l=g.guid;p._key=l;t.set(l,g);p=g;a.editor.update(()=>{d.markDirty()})}b instanceof v.Map?b.set(k,p):b.setAttribute(k,p)}}}
20
+ function R(a,b,c){let d=0,e=0,f=a._children,h=f.length;for(;e<h;e++){a=f[e];let g=d,k=a.getSize();d+=k;if((c?d>=b:d>b)&&a instanceof G)return c=b-g-1,0>c&&(c=0),{length:d-b,node:a,nodeIndex:e,offset:c};if(d>b)return{length:0,node:a,nodeIndex:e,offset:g};if(e===h-1)return{length:0,node:null,nodeIndex:e+1,offset:g+1}}return{length:0,node:null,nodeIndex:0,offset:0}}
21
+ function S(a){let b=a.anchor;a=a.focus;let c=!1;try{let d=b.getNode(),e=a.getNode();if(!d.isAttached()||!e.isAttached()||n.$isTextNode(d)&&b.offset>d.getTextContentSize()||n.$isTextNode(e)&&a.offset>e.getTextContentSize())c=!0}catch(d){c=!0}return c}function la(a,b){a.doc.transact(b,a)}
19
22
  function T(a){var b=a.getParent();if(null!==b){let e=a.getWritable();b=b.getWritable();var c=a.getPreviousSibling();a=a.getNextSibling();if(null===c)if(null!==a){var d=a.getWritable();b.__first=a.__key;d.__prev=null}else b.__first=null;else{d=c.getWritable();if(null!==a){let f=a.getWritable();f.__prev=d.__key;d.__next=f.__key}else d.__next=null;e.__prev=null}null===a?null!==c?(a=c.getWritable(),b.__last=c.__key,a.__next=null):b.__last=null:(a=a.getWritable(),null!==c?(c=c.getWritable(),c.__next=a.__key,
20
- a.__prev=c.__key):a.__prev=null,e.__next=null);b.__size--;e.__parent=null}}
21
- class U{constructor(a,b,c){this._key="";this._xmlElem=a;this._parent=b;this._type=c}getPrevNode(a){if(null===a)return null;a=a.get(this._key);return r.$isDecoratorNode(a)?a:null}getNode(){let a=r.$getNodeByKey(this._key);return r.$isDecoratorNode(a)?a:null}getSharedType(){return this._xmlElem}getType(){return this._type}getKey(){return this._key}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}syncPropertiesFromLexical(a,b,c){c=this.getPrevNode(c);G(a,this._xmlElem,c,b)}syncPropertiesFromYjs(a,
22
- b){let c=this.getNode();null===c&&A(83);H(a,this._xmlElem,c,b)}destroy(a){a.collabNodeMap.delete(this._key)}}function N(a,b,c){b=new U(a,b,c);return a._collabNode=b}
23
- class P{constructor(a,b,c){this._key="";this._children=[];this._xmlText=a;this._type=c;this._parent=b}getPrevNode(a){if(null===a)return null;a=a.get(this._key);return r.$isElementNode(a)?a:null}getNode(){let a=r.$getNodeByKey(this._key);return r.$isElementNode(a)?a:null}getSharedType(){return this._xmlText}getType(){return this._type}getKey(){return this._key}isEmpty(){return 0===this._children.length}getSize(){return 1}getOffset(){let a=this._parent;null===a&&A(90);return a.getChildOffset(this)}syncPropertiesFromYjs(a,
24
- b){let c=this.getNode();null===c&&A(91);H(a,this._xmlText,c,b)}applyChildrenYjsDelta(a,b){let c=this._children,d=0;for(let n=0;n<b.length;n++){var e=b[n],f=e.insert,h=e.delete;if(null!=e.retain)d+=e.retain;else if("number"===typeof h)for(f=h;0<f;){let {node:k,nodeIndex:l,offset:q,length:u}=Q(this,d,!1);if(k instanceof P||k instanceof C||k instanceof U)c.splice(l,1),--f;else if(k instanceof F){e=Math.min(f,u);h=0!==l?c[l-1]:null;var g=k.getSize();if(0===q&&1===e&&0<l&&h instanceof F&&u===g&&0===Array.from(k._map.keys()).length)h._text+=
25
- k._text,c.splice(l,1);else if(0===q&&e===g)c.splice(l,1);else{h=k;g=k._text;var m=q,p=e;g=g.slice(0,m)+""+g.slice(m+p);h._text=g}f-=e}else break}else if(null!=f)if("string"===typeof f){let {node:k,offset:l}=Q(this,d,!0);k instanceof F?(e=k,h=k._text,g=l,m=f,h=h.slice(0,g)+m+h.slice(g+0),e._text=h):this._xmlText.delete(l,f.length);d+=f.length}else e=f,{nodeIndex:f}=Q(this,d,!1),e=O(a,e,this),c.splice(f,0,e),d+=1;else throw Error("Unexpected delta format");}}syncChildrenFromYjs(a){var b=this.getNode();
26
- null===b&&A(92);var c=b.__key;let d=S(b,null),e=d.length;var f=this._children;let h=f.length,g=a.collabNodeMap,m=new Set,p;let n=0;var k=null;h!==e&&b.getWritable();for(let x=0;x<h;x++){var l=d[n],q=f[x];var u=q.getNode();var t=q._key;if(null!==u&&l===t)k=r.$isTextNode(u),m.add(l),k&&(q._key=l,q instanceof P?(k=q._xmlText,q.syncPropertiesFromYjs(a,null),q.applyChildrenYjsDelta(a,k.toDelta()),q.syncChildrenFromYjs(a)):q instanceof F?q.syncPropertiesAndTextFromYjs(a,null):q instanceof U?q.syncPropertiesFromYjs(a,
27
- null):q instanceof C||A(93)),k=u,n++;else{if(void 0===p)for(p=new Set,t=0;t<h;t++){var w=f[t]._key;""!==w&&p.add(w)}if(null!==u&&void 0!==l&&!p.has(l)){q=K(l);T(q);x--;n++;continue}u=b.getWritable();l=a;t=q;w=c;var B=t.getType();let D=l.editor._nodes.get(B);void 0===D&&A(88,B);B=new D.klass;B.__parent=w;t._key=B.__key;t instanceof P?(w=t._xmlText,t.syncPropertiesFromYjs(l,null),t.applyChildrenYjsDelta(l,w.toDelta()),t.syncChildrenFromYjs(l)):t instanceof F?t.syncPropertiesAndTextFromYjs(l,null):t instanceof
28
- U&&t.syncPropertiesFromYjs(l,null);l.collabNodeMap.set(B.__key,t);l=B;t=l.__key;g.set(t,q);null===k?(k=u.getFirstChild(),u.__first=t,null!==k&&(k=k.getWritable(),k.__prev=t,l.__next=k.__key)):(q=k.getWritable(),w=k.getNextSibling(),q.__next=t,l.__prev=k.__key,null!==w&&(k=w.getWritable(),k.__prev=t,l.__next=k.__key));x===h-1&&(u.__last=t);u.__size++;k=l}}for(b=0;b<e;b++)f=d[b],m.has(f)||(c=K(f),f=a.collabNodeMap.get(f),void 0!==f&&f.destroy(a),T(c))}syncPropertiesFromLexical(a,b,c){G(a,this._xmlText,
29
- this.getPrevNode(c),b)}_syncChildFromLexical(a,b,c,d,e,f){b=this._children[b];c=K(c);b instanceof P&&r.$isElementNode(c)?(b.syncPropertiesFromLexical(a,c,d),b.syncChildrenFromLexical(a,c,d,e,f)):b instanceof F&&r.$isTextNode(c)?b.syncPropertiesAndTextFromLexical(a,c,d):b instanceof U&&r.$isDecoratorNode(c)&&b.syncPropertiesFromLexical(a,c,d)}syncChildrenFromLexical(a,b,c,d,e){var f=this.getPrevNode(c);let h=null===f?[]:S(f,c);f=S(b,null);let g=h.length-1,m=f.length-1,p=a.collabNodeMap,n,k,l=0;for(b=
30
- 0;l<=g&&b<=m;){var q=h[l];let t=f[b];if(q===t)this._syncChildFromLexical(a,b,t,c,d,e),l++,b++;else{void 0===n&&(n=new Set(h));void 0===k&&(k=new Set(f));var u=k.has(q);q=n.has(t);u?(u=K(t),u=L(a,u,this),p.set(t,u),q?(this.splice(a,b,1,u),l++):this.splice(a,b,0,u),b++):(this.splice(a,b,1),l++)}}c=l>g;d=b>m;if(c&&!d)for(;b<=m;++b)c=f[b],d=K(c),d=L(a,d,this),this.append(d),p.set(c,d);else if(d&&!c)for(f=this._children.length-1;f>=b;f--)this.splice(a,f,1)}append(a){let b=this._xmlText;var c=this._children;
31
- c=c[c.length-1];c=void 0!==c?c.getOffset()+c.getSize():0;if(a instanceof P)b.insertEmbed(c,a._xmlText);else if(a instanceof F){let d=a._map;null===d.parent&&b.insertEmbed(c,d);b.insert(c+1,a._text)}else a instanceof C?b.insertEmbed(c,a._map):a instanceof U&&b.insertEmbed(c,a._xmlElem);this._children.push(a)}splice(a,b,c,d){let e=this._children;var f=e[b];if(void 0===f)void 0===d&&A(94),this.append(d);else{var h=f.getOffset();-1===h&&A(95);var g=this._xmlText;0!==c&&g.delete(h,f.getSize());d instanceof
32
- P?g.insertEmbed(h,d._xmlText):d instanceof F?(f=d._map,null===f.parent&&g.insertEmbed(h,f),g.insert(h+1,d._text)):d instanceof C?g.insertEmbed(h,d._map):d instanceof U&&g.insertEmbed(h,d._xmlElem);if(0!==c)for(h=e.slice(b,b+c),g=0;g<h.length;g++)h[g].destroy(a);void 0!==d?e.splice(b,c,d):e.splice(b,c)}}getChildOffset(a){let b=0,c=this._children;for(let d=0;d<c.length;d++){let e=c[d];if(e===a)return b;b+=e.getSize()}return-1}destroy(a){let b=a.collabNodeMap,c=this._children;for(let d=0;d<c.length;d++)c[d].destroy(a);
33
- b.delete(this._key)}}function M(a,b,c){b=new P(a,b,c);return a._collabNode=b}
34
- function V(a,b){var c=b.collabNodeMap.get(a.key);if(void 0===c)return null;b=a.offset;let d=c.getSharedType();if(c instanceof F){d=c._parent._xmlText;a=c.getOffset();if(-1===a)return null;b=a+1+b}else if(c instanceof P&&"element"===a.type){var e=a.getNode();if(!r.$isElementNode(e))throw Error("Element point must be an element node");c=a=0;for(e=e.getFirstChild();null!==e&&c++<b;)r.$isTextNode(e)?a+=e.getTextContentSize()+1:a++,e=e.getNextSibling();b=a}return v.createRelativePositionFromTypeIndex(d,
35
- b)}function W(a,b){if(null==a){if(null!=b)return!0}else if(null==b||!v.compareRelativePositions(a,b))return!0;return!1}function X(a,b){a=a.cursorsContainer;if(null!==a){b=b.selections;let c=b.length;for(let d=0;d<c;d++)a.removeChild(b[d])}}
36
- function Y(a,b){var c=b.awareness.getLocalState();if(null!==c&&(b=c.anchorPos,c=c.focusPos,null!==b&&null!==c&&(b=v.createAbsolutePositionFromRelativePosition(b,a.doc),a=v.createAbsolutePositionFromRelativePosition(c,a.doc),null!==b&&null!==a))){let [d,e]=Z(b.type,b.index),[f,h]=Z(a.type,a.index);if(null!==d&&null!==f){b=d.getKey();c=f.getKey();let g=r.$getSelection();r.$isRangeSelection(g)&&(a=g.focus,ma(g.anchor,b,e),ma(a,c,h))}}}
37
- function ma(a,b,c){if(a.key!==b||a.offset!==c){let d=r.$getNodeByKey(b);if(null!==d&&!r.$isElementNode(d)&&!r.$isTextNode(d)){let e=d.getParentOrThrow();b=e.getKey();c=d.getIndexWithinParent();d=e}a.set(b,c,r.$isElementNode(d)?"element":"text")}}function Z(a,b){a=a._collabNode;if(void 0===a)return[null,0];if(a instanceof P){let {node:c,offset:d}=Q(a,b,!0);return null===c?[a,0]:[c,d]}return[null,0]}
38
- function na(a,b){var c=Array.from(b.awareness.getStates()),d=a.clientID;b=a.cursors;var e=a.editor._editorState._nodeMap;let f=new Set;for(var h=0;h<c.length;h++){let [D,pa]=c[h];if(D!==d){f.add(D);let {anchorPos:ca,focusPos:da,name:qa,color:ra,focusing:sa}=pa;var g=null,m=b.get(D);void 0===m&&(m={color:ra,name:qa,selection:null},b.set(D,m));if(null!==ca&&null!==da&&sa){var p=v.createAbsolutePositionFromRelativePosition(ca,a.doc),n=v.createAbsolutePositionFromRelativePosition(da,a.doc);if(null!==
39
- p&&null!==n){let [ea,fa]=Z(p.type,p.index),[ha,ia]=Z(n.type,n.index);if(null!==ea&&null!==ha){p=ea.getKey();var k=ha.getKey();g=m.selection;if(null===g){g=m;n=fa;var l=ia,q=g.color,u=document.createElement("span");u.style.cssText=`position:absolute;top:0;bottom:0;right:-1px;width:1px;background-color:${q};z-index:10;`;var t=document.createElement("span");t.textContent=g.name;t.style.cssText=`position:absolute;left:-2px;top:-16px;background-color:${q};color:#fff;line-height:12px;font-size:12px;padding:2px;font-family:Arial;font-weight:bold;white-space:nowrap;`;
40
- u.appendChild(t);g={anchor:{key:p,offset:n},caret:u,color:q,focus:{key:k,offset:l},name:t,selections:[]}}else n=g.anchor,l=g.focus,n.key=p,n.offset=fa,l.key=k,l.offset=ia}}}a:if(p=a,n=m,u=g,q=e,l=p.editor,g=l.getRootElement(),m=p.cursorsContainer,null!==m&&null!==g&&(g=m.offsetParent,null!==g))if(g=g.getBoundingClientRect(),k=n.selection,null===u)null!==k&&(n.selection=null,X(p,k));else{n.selection=u;n=u.caret;k=u.color;p=u.selections;t=u.anchor;u=u.focus;var w=t.key,B=u.key,x=q.get(w);q=q.get(B);
41
- if(null!=x&&null!=q){if(x===q&&r.$isLineBreakNode(x))q=[l.getElementByKey(w).getBoundingClientRect()];else{q=y.createDOMRange(l,x,t.offset,q,u.offset);if(null===q)break a;q=y.createRectsFromDOMRange(l,q)}u=p.length;l=q.length;for(t=0;t<l;t++)x=q[t],w=p[t],void 0===w&&(w=document.createElement("span"),p[t]=w,B=document.createElement("span"),w.appendChild(B),m.appendChild(w)),x=`position:absolute;top:${x.top-g.top}px;left:${x.left-g.left}px;height:${x.height}px;width:${x.width}px;pointer-events:none;z-index:5;`,
42
- w.style.cssText=x,w.firstChild.style.cssText=`${x}left:0;top:0;background-color:${k};opacity:0.3;`,t===l-1&&n.parentNode!==w&&w.appendChild(n);for(g=u-1;g>=l;g--)m.removeChild(p[g]),p.pop()}}}}c=Array.from(b.keys());for(d=0;d<c.length;d++)e=c[d],f.has(e)||(h=b.get(e),void 0!==h&&(h=h.selection,null!==h&&X(a,h),b.delete(e)))}
43
- function oa(a,b,c,d){b=b.awareness;var e=b.getLocalState();if(null!==e){var {anchorPos:f,focusPos:h,name:g,color:m,focusing:p,awarenessData:n}=e,k=e=null;if(null!==d&&(null===f||d.is(c))||null!==c)r.$isRangeSelection(d)&&(e=V(d.anchor,a),k=V(d.focus,a)),(W(f,e)||W(h,k))&&b.setLocalState({anchorPos:e,awarenessData:n,color:m,focusPos:k,focusing:p,name:g})}}let ta=r.createCommand("CONNECTED_COMMAND"),ua=r.createCommand("TOGGLE_CONNECT_COMMAND");exports.CONNECTED_COMMAND=ta;
44
- exports.TOGGLE_CONNECT_COMMAND=ua;exports.createBinding=function(a,b,c,d,e,f){void 0!==d&&null!==d||A(81);b=d.get("root",v.XmlText);b=M(b,null,"root");b._key="root";return{clientID:d.clientID,collabNodeMap:new Map,cursors:new Map,cursorsContainer:null,doc:d,docMap:e,editor:a,excludedProperties:f||new Map,id:c,nodeProperties:new Map,root:b}};exports.createUndoManager=function(a,b){return new v.UndoManager(b,{trackedOrigins:new Set([a,null])})};
45
- exports.initLocalState=function(a,b,c,d,e){a.awareness.setLocalState({anchorPos:null,awarenessData:e,color:c,focusPos:null,focusing:d,name:b})};exports.setLocalStateFocus=function(a,b,c,d,e){({awareness:a}=a);let f=a.getLocalState();null===f&&(f={anchorPos:null,awarenessData:e,color:c,focusPos:null,focusing:d,name:b});f.focusing=d;a.setLocalState(f)};exports.syncCursorPositions=na;
46
- exports.syncLexicalUpdateToYjs=function(a,b,c,d,e,f,h,g){la(a,()=>{d.read(()=>{if(g.has("collaboration")||g.has("historic")){if(0<h.size){var m=Array.from(h),p=a.collabNodeMap,n=[];for(let u=0;u<m.length;u++){var k=m[u],l=r.$getNodeByKey(k),q=p.get(k);if(q instanceof F)if(r.$isTextNode(l))n.push([q,l.__text]);else{l=q.getOffset();if(-1===l)continue;let t=q._parent;q._normalized=!0;t._xmlText.delete(l,1);p.delete(k);k=t._children;q=k.indexOf(q);k.splice(q,1)}}for(m=0;m<n.length;m++){let [u,t]=n[m];
47
- u instanceof F&&"string"===typeof t&&(u._text=t)}}}else e.has("root")&&(n=c._nodeMap,m=r.$getRoot(),p=a.root,p.syncPropertiesFromLexical(a,m,n),p.syncChildrenFromLexical(a,m,n,e,f)),n=r.$getSelection(),oa(a,b,c._selection,n)})})};
48
- exports.syncYjsChangesToLexical=function(a,b,c,d){let e=a.editor,f=e._editorState;c.forEach(h=>h.delta);e.update(()=>{var h=e._pendingEditorState;for(var g=0;g<c.length;g++){var m=a,p=c[g],{target:n}=p;n=O(m,n);if(n instanceof P&&p instanceof v.YTextEvent){let {keysChanged:k,childListChanged:l,delta:q}=p;0<k.size&&n.syncPropertiesFromYjs(m,k);l&&(n.applyChildrenYjsDelta(m,q),n.syncChildrenFromYjs(m))}else n instanceof F&&p instanceof v.YMapEvent?({keysChanged:p}=p,0<p.size&&n.syncPropertiesAndTextFromYjs(m,
49
- p)):n instanceof U&&p instanceof v.YXmlEvent?({attributesChanged:p}=p,0<p.size&&n.syncPropertiesFromYjs(m,p)):A(82)}0===r.$getRoot().getChildrenSize()&&r.$getRoot().append(r.$createParagraphNode());m=r.$getSelection();if(r.$isRangeSelection(m))if(R(m)){g=f._selection;if(r.$isRangeSelection(g)){n=z.$createOffsetView(e,0,f);h=z.$createOffsetView(e,0,h);let [k,l]=n.getOffsetsFromSelection(g);h=0<=k&&0<=l?h.createSelectionFromOffsets(k,l,n):null;null!==h?r.$setSelection(h):(Y(a,b),R(m)&&r.$getRoot().selectEnd())}oa(a,
50
- b,g,r.$getSelection())}else Y(a,b)},{onUpdate:()=>{na(a,b)},skipTransforms:!0,tag:d?"historic":"collaboration"})}
23
+ a.__prev=c.__key):a.__prev=null,e.__next=null);b.__size--;e.__parent=null}}function U(a,b){if(a=b._nodeMap.get(a)){var c=a.__prev,d=null;c&&(d=n.$getNodeByKey(c));null===d&&null!==a.__parent&&(d=n.$getNodeByKey(a.__parent));null===d?n.$getRoot().selectStart():null!==d&&d.isAttached()?d.selectEnd():U(d.__key,b)}else n.$getRoot().selectStart()}
24
+ class V{constructor(a,b,c){this._key="";this._xmlElem=a;this._parent=b;this._type=c}getPrevNode(a){if(null===a)return null;a=a.get(this._key);return n.$isDecoratorNode(a)?a:null}getNode(){let a=n.$getNodeByKey(this._key);return n.$isDecoratorNode(a)?a:null}getSharedType(){return this._xmlElem}getType(){return this._type}getKey(){return this._key}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}syncPropertiesFromLexical(a,b,c){c=this.getPrevNode(c);H(a,this._xmlElem,c,b)}syncPropertiesFromYjs(a,
25
+ b){let c=this.getNode();null===c&&A(83);I(a,this._xmlElem,c,b)}destroy(a){a.collabNodeMap.delete(this._key)}}function O(a,b,c){b=new V(a,b,c);return a._collabNode=b}
26
+ class Q{constructor(a,b,c){this._key="";this._children=[];this._xmlText=a;this._type=c;this._parent=b}getPrevNode(a){if(null===a)return null;a=a.get(this._key);return n.$isElementNode(a)?a:null}getNode(){let a=n.$getNodeByKey(this._key);return n.$isElementNode(a)?a:null}getSharedType(){return this._xmlText}getType(){return this._type}getKey(){return this._key}isEmpty(){return 0===this._children.length}getSize(){return 1}getOffset(){let a=this._parent;null===a&&A(90);return a.getChildOffset(this)}syncPropertiesFromYjs(a,
27
+ b){let c=this.getNode();null===c&&A(91);I(a,this._xmlText,c,b)}applyChildrenYjsDelta(a,b){let c=this._children,d=0;for(let t=0;t<b.length;t++){var e=b[t],f=e.insert,h=e.delete;if(null!=e.retain)d+=e.retain;else if("number"===typeof h)for(f=h;0<f;){let {node:m,nodeIndex:l,offset:q,length:u}=R(this,d,!1);if(m instanceof Q||m instanceof E||m instanceof V)c.splice(l,1),--f;else if(m instanceof G){e=Math.min(f,u);h=0!==l?c[l-1]:null;var g=m.getSize();if(0===q&&1===e&&0<l&&h instanceof G&&u===g&&0===Array.from(m._map.keys()).length)h._text+=
28
+ m._text,c.splice(l,1);else if(0===q&&e===g)c.splice(l,1);else{h=m;g=m._text;var k=q,p=e;g=g.slice(0,k)+""+g.slice(k+p);h._text=g}f-=e}else break}else if(null!=f)if("string"===typeof f){let {node:m,offset:l}=R(this,d,!0);m instanceof G?(e=m,h=m._text,g=l,k=f,h=h.slice(0,g)+k+h.slice(g+0),e._text=h):this._xmlText.delete(l,f.length);d+=f.length}else e=f,{nodeIndex:f}=R(this,d,!1),e=P(a,e,this),c.splice(f,0,e),d+=1;else throw Error("Unexpected delta format");}}syncChildrenFromYjs(a){var b=this.getNode();
29
+ null===b&&A(92);var c=b.__key;let d=x.$createChildrenArray(b,null),e=d.length;var f=this._children;let h=f.length,g=a.collabNodeMap,k=new Set,p;let t=0;var m=null;h!==e&&b.getWritable();for(let y=0;y<h;y++){var l=d[t],q=f[y];var u=q.getNode();var r=q._key;if(null!==u&&l===r)m=n.$isTextNode(u),k.add(l),m&&(q._key=l,q instanceof Q?(m=q._xmlText,q.syncPropertiesFromYjs(a,null),q.applyChildrenYjsDelta(a,m.toDelta()),q.syncChildrenFromYjs(a)):q instanceof G?q.syncPropertiesAndTextFromYjs(a,null):q instanceof
30
+ V?q.syncPropertiesFromYjs(a,null):q instanceof E||A(93)),m=u,t++;else{if(void 0===p)for(p=new Set,r=0;r<h;r++){var w=f[r]._key;""!==w&&p.add(w)}if(null!==u&&void 0!==l&&!p.has(l)){q=L(l);T(q);y--;t++;continue}u=b.getWritable();l=a;r=q;w=c;var B=r.getType();let D=l.editor._nodes.get(B);void 0===D&&A(88,B);B=new D.klass;B.__parent=w;r._key=B.__key;r instanceof Q?(w=r._xmlText,r.syncPropertiesFromYjs(l,null),r.applyChildrenYjsDelta(l,w.toDelta()),r.syncChildrenFromYjs(l)):r instanceof G?r.syncPropertiesAndTextFromYjs(l,
31
+ null):r instanceof V&&r.syncPropertiesFromYjs(l,null);l.collabNodeMap.set(B.__key,r);l=B;r=l.__key;g.set(r,q);null===m?(m=u.getFirstChild(),u.__first=r,null!==m&&(m=m.getWritable(),m.__prev=r,l.__next=m.__key)):(q=m.getWritable(),w=m.getNextSibling(),q.__next=r,l.__prev=m.__key,null!==w&&(m=w.getWritable(),m.__prev=r,l.__next=m.__key));y===h-1&&(u.__last=r);u.__size++;m=l}}for(b=0;b<e;b++)f=d[b],k.has(f)||(c=L(f),f=a.collabNodeMap.get(f),void 0!==f&&f.destroy(a),T(c))}syncPropertiesFromLexical(a,
32
+ b,c){H(a,this._xmlText,this.getPrevNode(c),b)}_syncChildFromLexical(a,b,c,d,e,f){b=this._children[b];c=L(c);b instanceof Q&&n.$isElementNode(c)?(b.syncPropertiesFromLexical(a,c,d),b.syncChildrenFromLexical(a,c,d,e,f)):b instanceof G&&n.$isTextNode(c)?b.syncPropertiesAndTextFromLexical(a,c,d):b instanceof V&&n.$isDecoratorNode(c)&&b.syncPropertiesFromLexical(a,c,d)}syncChildrenFromLexical(a,b,c,d,e){var f=this.getPrevNode(c);let h=null===f?[]:x.$createChildrenArray(f,c);f=x.$createChildrenArray(b,
33
+ null);let g=h.length-1,k=f.length-1,p=a.collabNodeMap,t,m,l=0;for(b=0;l<=g&&b<=k;){var q=h[l];let r=f[b];if(q===r)this._syncChildFromLexical(a,b,r,c,d,e),l++,b++;else{void 0===t&&(t=new Set(h));void 0===m&&(m=new Set(f));var u=m.has(q);q=t.has(r);u?(u=L(r),u=M(a,u,this),p.set(r,u),q?(this.splice(a,b,1,u),l++):this.splice(a,b,0,u),b++):(this.splice(a,b,1),l++)}}c=l>g;d=b>k;if(c&&!d)for(;b<=k;++b)c=f[b],d=L(c),d=M(a,d,this),this.append(d),p.set(c,d);else if(d&&!c)for(f=this._children.length-1;f>=b;f--)this.splice(a,
34
+ f,1)}append(a){let b=this._xmlText;var c=this._children;c=c[c.length-1];c=void 0!==c?c.getOffset()+c.getSize():0;if(a instanceof Q)b.insertEmbed(c,a._xmlText);else if(a instanceof G){let d=a._map;null===d.parent&&b.insertEmbed(c,d);b.insert(c+1,a._text)}else a instanceof E?b.insertEmbed(c,a._map):a instanceof V&&b.insertEmbed(c,a._xmlElem);this._children.push(a)}splice(a,b,c,d){let e=this._children;var f=e[b];if(void 0===f)void 0===d&&A(94),this.append(d);else{var h=f.getOffset();-1===h&&A(95);var g=
35
+ this._xmlText;0!==c&&g.delete(h,f.getSize());d instanceof Q?g.insertEmbed(h,d._xmlText):d instanceof G?(f=d._map,null===f.parent&&g.insertEmbed(h,f),g.insert(h+1,d._text)):d instanceof E?g.insertEmbed(h,d._map):d instanceof V&&g.insertEmbed(h,d._xmlElem);if(0!==c)for(h=e.slice(b,b+c),g=0;g<h.length;g++)h[g].destroy(a);void 0!==d?e.splice(b,c,d):e.splice(b,c)}}getChildOffset(a){let b=0,c=this._children;for(let d=0;d<c.length;d++){let e=c[d];if(e===a)return b;b+=e.getSize()}return-1}destroy(a){let b=
36
+ a.collabNodeMap,c=this._children;for(let d=0;d<c.length;d++)c[d].destroy(a);b.delete(this._key)}}function N(a,b,c){b=new Q(a,b,c);return a._collabNode=b}
37
+ function W(a,b){var c=b.collabNodeMap.get(a.key);if(void 0===c)return null;b=a.offset;let d=c.getSharedType();if(c instanceof G){d=c._parent._xmlText;a=c.getOffset();if(-1===a)return null;b=a+1+b}else if(c instanceof Q&&"element"===a.type){var e=a.getNode();n.$isElementNode(e)||A(184);c=a=0;for(e=e.getFirstChild();null!==e&&c++<b;)n.$isTextNode(e)?a+=e.getTextContentSize()+1:a++,e=e.getNextSibling();b=a}return v.createRelativePositionFromTypeIndex(d,b)}
38
+ function X(a,b){if(null==a){if(null!=b)return!0}else if(null==b||!v.compareRelativePositions(a,b))return!0;return!1}function Y(a,b){a=a.cursorsContainer;if(null!==a){b=b.selections;let c=b.length;for(let d=0;d<c;d++)a.removeChild(b[d])}}
39
+ function ma(a,b){var c=b.awareness.getLocalState();if(null!==c&&(b=c.anchorPos,c=c.focusPos,null!==b&&null!==c&&(b=v.createAbsolutePositionFromRelativePosition(b,a.doc),a=v.createAbsolutePositionFromRelativePosition(c,a.doc),null!==b&&null!==a))){let [d,e]=Z(b.type,b.index),[f,h]=Z(a.type,a.index);if(null!==d&&null!==f){b=d.getKey();c=f.getKey();let g=n.$getSelection();n.$isRangeSelection(g)&&(a=g.focus,na(g.anchor,b,e),na(a,c,h))}}}
40
+ function na(a,b,c){if(a.key!==b||a.offset!==c){let d=n.$getNodeByKey(b);if(null!==d&&!n.$isElementNode(d)&&!n.$isTextNode(d)){let e=d.getParentOrThrow();b=e.getKey();c=d.getIndexWithinParent();d=e}a.set(b,c,n.$isElementNode(d)?"element":"text")}}function Z(a,b){a=a._collabNode;if(void 0===a)return[null,0];if(a instanceof Q){let {node:c,offset:d}=R(a,b,!0);return null===c?[a,0]:[c,d]}return[null,0]}
41
+ function oa(a,b){var c=Array.from(b.awareness.getStates()),d=a.clientID;b=a.cursors;var e=a.editor._editorState._nodeMap;let f=new Set;for(var h=0;h<c.length;h++){let [D,qa]=c[h];if(D!==d){f.add(D);let {anchorPos:da,focusPos:ea,name:ra,color:sa,focusing:ta}=qa;var g=null,k=b.get(D);void 0===k&&(k={color:sa,name:ra,selection:null},b.set(D,k));if(null!==da&&null!==ea&&ta){var p=v.createAbsolutePositionFromRelativePosition(da,a.doc),t=v.createAbsolutePositionFromRelativePosition(ea,a.doc);if(null!==
42
+ p&&null!==t){let [fa,ha]=Z(p.type,p.index),[ia,ja]=Z(t.type,t.index);if(null!==fa&&null!==ia){p=fa.getKey();var m=ia.getKey();g=k.selection;if(null===g){g=k;t=ha;var l=ja,q=g.color,u=document.createElement("span");u.style.cssText=`position:absolute;top:0;bottom:0;right:-1px;width:1px;background-color:${q};z-index:10;`;var r=document.createElement("span");r.textContent=g.name;r.style.cssText=`position:absolute;left:-2px;top:-16px;background-color:${q};color:#fff;line-height:12px;font-size:12px;padding:2px;font-family:Arial;font-weight:bold;white-space:nowrap;`;
43
+ u.appendChild(r);g={anchor:{key:p,offset:t},caret:u,color:q,focus:{key:m,offset:l},name:r,selections:[]}}else t=g.anchor,l=g.focus,t.key=p,t.offset=ha,l.key=m,l.offset=ja}}}a:if(p=a,t=k,u=g,q=e,l=p.editor,g=l.getRootElement(),k=p.cursorsContainer,null!==k&&null!==g&&(g=k.offsetParent,null!==g))if(g=g.getBoundingClientRect(),m=t.selection,null===u)null!==m&&(t.selection=null,Y(p,m));else{t.selection=u;t=u.caret;m=u.color;p=u.selections;r=u.anchor;u=u.focus;var w=r.key,B=u.key,y=q.get(w);q=q.get(B);
44
+ if(null!=y&&null!=q){if(y===q&&n.$isLineBreakNode(y))q=[l.getElementByKey(w).getBoundingClientRect()];else{q=z.createDOMRange(l,y,r.offset,q,u.offset);if(null===q)break a;q=z.createRectsFromDOMRange(l,q)}u=p.length;l=q.length;for(r=0;r<l;r++)y=q[r],w=p[r],void 0===w&&(w=document.createElement("span"),p[r]=w,B=document.createElement("span"),w.appendChild(B),k.appendChild(w)),y=`position:absolute;top:${y.top-g.top}px;left:${y.left-g.left}px;height:${y.height}px;width:${y.width}px;pointer-events:none;z-index:5;`,
45
+ w.style.cssText=y,w.firstChild.style.cssText=`${y}left:0;top:0;background-color:${m};opacity:0.3;`,r===l-1&&t.parentNode!==w&&w.appendChild(t);for(g=u-1;g>=l;g--)k.removeChild(p[g]),p.pop()}}}}c=Array.from(b.keys());for(d=0;d<c.length;d++)e=c[d],f.has(e)||(h=b.get(e),void 0!==h&&(h=h.selection,null!==h&&Y(a,h),b.delete(e)))}
46
+ function pa(a,b,c,d){b=b.awareness;var e=b.getLocalState();if(null!==e){var {anchorPos:f,focusPos:h,name:g,color:k,focusing:p,awarenessData:t}=e,m=e=null;if(null!==d&&(null===f||d.is(c))||null!==c)n.$isRangeSelection(d)&&(e=W(d.anchor,a),m=W(d.focus,a)),(X(f,e)||X(h,m))&&b.setLocalState({anchorPos:e,awarenessData:t,color:k,focusPos:m,focusing:p,name:g})}}let ua=n.createCommand("CONNECTED_COMMAND"),va=n.createCommand("TOGGLE_CONNECT_COMMAND");exports.CONNECTED_COMMAND=ua;
47
+ exports.TOGGLE_CONNECT_COMMAND=va;exports.createBinding=function(a,b,c,d,e,f){void 0!==d&&null!==d||A(81);b=d.get("root",v.XmlText);b=N(b,null,"root");b._key="root";return{clientID:d.clientID,collabNodeMap:new Map,cursors:new Map,cursorsContainer:null,doc:d,docMap:e,editor:a,excludedProperties:f||new Map,id:c,nodeProperties:new Map,root:b}};exports.createUndoManager=function(a,b){return new v.UndoManager(b,{trackedOrigins:new Set([a,null])})};
48
+ exports.initLocalState=function(a,b,c,d,e){a.awareness.setLocalState({anchorPos:null,awarenessData:e,color:c,focusPos:null,focusing:d,name:b})};exports.setLocalStateFocus=function(a,b,c,d,e){({awareness:a}=a);let f=a.getLocalState();null===f&&(f={anchorPos:null,awarenessData:e,color:c,focusPos:null,focusing:d,name:b});f.focusing=d;a.setLocalState(f)};exports.syncCursorPositions=oa;
49
+ exports.syncLexicalUpdateToYjs=function(a,b,c,d,e,f,h,g){la(a,()=>{d.read(()=>{if(g.has("collaboration")||g.has("historic")){if(0<h.size){var k=Array.from(h),p=a.collabNodeMap,t=[];for(let u=0;u<k.length;u++){var m=k[u],l=n.$getNodeByKey(m),q=p.get(m);if(q instanceof G)if(n.$isTextNode(l))t.push([q,l.__text]);else{l=q.getOffset();if(-1===l)continue;let r=q._parent;q._normalized=!0;r._xmlText.delete(l,1);p.delete(m);m=r._children;q=m.indexOf(q);m.splice(q,1)}}for(k=0;k<t.length;k++){let [u,r]=t[k];
50
+ u instanceof G&&"string"===typeof r&&(u._text=r)}}}else e.has("root")&&(t=c._nodeMap,k=n.$getRoot(),p=a.root,p.syncPropertiesFromLexical(a,k,t),p.syncChildrenFromLexical(a,k,t,e,f)),t=n.$getSelection(),pa(a,b,c._selection,t)})})};
51
+ exports.syncYjsChangesToLexical=function(a,b,c,d){let e=a.editor,f=e._editorState;c.forEach(h=>h.delta);e.update(()=>{for(var h=0;h<c.length;h++){var g=a,k=c[h],{target:p}=k;p=P(g,p);if(p instanceof Q&&k instanceof v.YTextEvent){let {keysChanged:t,childListChanged:m,delta:l}=k;0<t.size&&p.syncPropertiesFromYjs(g,t);m&&(p.applyChildrenYjsDelta(g,l),p.syncChildrenFromYjs(g))}else p instanceof G&&k instanceof v.YMapEvent?({keysChanged:k}=k,0<k.size&&p.syncPropertiesAndTextFromYjs(g,k)):p instanceof V&&
52
+ k instanceof v.YXmlEvent?({attributesChanged:k}=k,0<k.size&&p.syncPropertiesFromYjs(g,k)):A(82)}0===n.$getRoot().getChildrenSize()&&n.$getRoot().append(n.$createParagraphNode());g=n.$getSelection();n.$isRangeSelection(g)&&(S(g)?(h=f._selection,n.$isRangeSelection(h)&&(ma(a,b),S(g)&&U(g.anchor.key,f)),pa(a,b,h,n.$getSelection())):ma(a,b))},{onUpdate:()=>{oa(a,b)},skipTransforms:!0,tag:d?"historic":"collaboration"})}
@@ -3,5 +3,7 @@
3
3
  *
4
4
  * This source code is licensed under the MIT license found in the
5
5
  * LICENSE file in the root directory of this source tree.
6
+ *
6
7
  */
7
- import{$getNodeByKey as e,$isLineBreakNode as t,$isTextNode as n,$getSelection as o,$isRangeSelection as s,createEditor as l,$isElementNode as i,$isRootNode as r,$isDecoratorNode as c,$getRoot as f,$createParagraphNode as a,$setSelection as u,createCommand as d}from"lexical";import{XmlText as _,Map as h,XmlElement as p,Doc as g,createAbsolutePositionFromRelativePosition as y,createRelativePositionFromTypeIndex as x,compareRelativePositions as m,YTextEvent as k,YMapEvent as b,YXmlEvent as v,UndoManager as P}from"yjs";import{createDOMRange as C,createRectsFromDOMRange as w}from"@lexical/selection";import{$createOffsetView as N}from"@lexical/offset";var T=function(e){const t=new URLSearchParams;t.append("code",e);for(let e=1;e<arguments.length;e++)t.append("v",arguments[e]);throw Error(`Minified Lexical error #${e}; visit https://lexical.dev/docs/error?${t} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)};class S{constructor(e,t){this._key="",this._map=e,this._parent=t,this._type="linebreak"}getNode(){const n=e(this._key);return t(n)?n:null}getKey(){return this._key}getSharedType(){return this._map}getType(){return this._type}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}destroy(e){e.collabNodeMap.delete(this._key)}}function F(e,t){const n=new S(e,t);return e._collabNode=n,n}class E{constructor(e,t,n,o){this._key="",this._map=e,this._parent=n,this._text=t,this._type=o,this._normalized=!1}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return n(t)?t:null}getNode(){const t=e(this._key);return n(t)?t:null}getSharedType(){return this._map}getType(){return this._type}getKey(){return this._key}getSize(){return this._text.length+(this._normalized?0:1)}getOffset(){return this._parent.getChildOffset(this)}spliceText(e,t,n){const o=this._parent._xmlText,s=this.getOffset()+1+e;0!==t&&o.delete(s,t),""!==n&&o.insert(s,n)}syncPropertiesAndTextFromLexical(e,t,n){const l=this.getPrevNode(n),i=t.__text;if($(e,this._map,l,t),null!==l){const e=l.__text;if(e!==i){!function(e,t,n,l){const i=o();let r=l.length;if(s(i)&&i.isCollapsed()){const e=i.anchor;e.key===t&&(r=e.offset)}const c=function(e,t,n){const o=e.length,s=t.length;let l=0,i=0;for(;l<o&&l<s&&e[l]===t[l]&&l<n;)l++;for(;i+l<o&&i+l<s&&e[o-i-1]===t[s-i-1];)i++;for(;i+l<o&&i+l<s&&e[l]===t[l];)l++;return{index:l,insert:t.slice(l,s-i),remove:o-l-i}}(n,l,r);e.spliceText(c.index,c.remove,c.insert)}(this,t.__key,e,i),this._text=i}}}syncPropertiesAndTextFromYjs(e,t){const n=this.getNode();null===n&&T(84),K(e,this._map,n,t);const o=this._text;if(n.__text!==o){n.getWritable().__text=o}}destroy(e){e.collabNodeMap.delete(this._key)}}function M(e,t,n,o){const s=new E(e,t,n,o);return e._collabNode=s,s}const z=new Set(["__key","__parent","__next","__prev"]),L=new Set(["__first","__last","__size"]),O=new Set(["__cachedText"]),j=new Set(["__text"]);function A(e,t,o){if(z.has(e))return!0;if(n(t)){if(j.has(e))return!0}else if(i(t)&&(L.has(e)||r(t)&&O.has(e)))return!0;const s=t.constructor,l=o.excludedProperties.get(s);return null!=l&&l.has(e)}function Y(t){const n=e(t);return null===n&&T(85),n}function D(e,o,s){const l=o.__type;let r;if(i(o)){r=V(new _,s,l),r.syncPropertiesFromLexical(e,o,null),r.syncChildrenFromLexical(e,o,null,null,null)}else if(n(o)){r=M(new h,o.__text,s,l),r.syncPropertiesAndTextFromLexical(e,o,null)}else if(t(o)){const e=new h;e.set("__type","linebreak"),r=F(e,s)}else if(c(o)){r=J(new p,s,l),r.syncPropertiesFromLexical(e,o,null)}else T(86);return r._key=o.__key,r}function W(e,t,n){const o=t._collabNode;if(void 0===o){const o=e.editor._nodes,s=function(e){const t=e instanceof h?e.get("__type"):e.getAttribute("__type");return null==t&&T(87),t}(t);void 0===o.get(s)&&T(88,s);const l=t.parent,i=void 0===n&&null!==l?W(e,l):n||null;if(i instanceof Q||T(89),t instanceof _)return V(t,i,s);if(t instanceof h)return"linebreak"===s?F(t,i):M(t,"",i,s);if(t instanceof p)return J(t,i,s)}return o}function I(e,t,n){const o=t.getType(),s=e.editor._nodes.get(o);void 0===s&&T(88,o);const l=new s.klass;if(l.__parent=n,t._key=l.__key,t instanceof Q){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof E?t.syncPropertiesAndTextFromYjs(e,null):t instanceof H&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(l.__key,t),l}function K(e,t,n,o){const s=null===o?t instanceof h?Array.from(t.keys()):Object.keys(t.getAttributes()):Array.from(o);let i;for(let o=0;o<s.length;o++){const r=s[o];if(A(r,n,e))continue;const c=n[r];let f=t instanceof h?t.get(r):t.getAttribute(r);if(c!==f){if(f instanceof g){const t=e.docMap;c instanceof g&&t.delete(c.guid);const n=l(),o=f.guid;n._key=o,t.set(o,f),f=n}void 0===i&&(i=n.getWritable()),i[r]=f}}}function $(e,t,n,o){const s=o.__type,l=e.nodeProperties;let i=l.get(s);void 0===i&&(i=Object.keys(o).filter((t=>!A(t,o,e))),l.set(s,i));const r=e.editor.constructor;for(let s=0;s<i.length;s++){const l=i[s],c=null===n?void 0:n[l];let f=o[l];if(c!==f){if(f instanceof r){const t=e.docMap;let n;if(c instanceof r){const e=c._key;n=t.get(e),t.delete(e)}const s=n||new g,l=s.guid;f._key=l,t.set(l,s),f=s,e.editor.update((()=>{o.markDirty()}))}t instanceof h?t.set(l,f):t.setAttribute(l,f)}}}function R(e,t,n,o){return e.slice(0,t)+o+e.slice(t+n)}function B(e,t,n){let o=0,s=0;const l=e._children,i=l.length;for(;s<i;s++){const e=l[s],r=o;o+=e.getSize();if((n?o>=t:o>t)&&e instanceof E){let n=t-r-1;n<0&&(n=0);return{length:o-t,node:e,nodeIndex:s,offset:n}}if(o>t)return{length:0,node:e,nodeIndex:s,offset:r};if(s===i-1)return{length:0,node:null,nodeIndex:s+1,offset:r+1}}return{length:0,node:null,nodeIndex:0,offset:0}}function U(e){const t=e.anchor,o=e.focus;let s=!1;try{const e=t.getNode(),l=o.getNode();(!e.isAttached()||!l.isAttached()||n(e)&&t.offset>e.getTextContentSize()||n(l)&&o.offset>l.getTextContentSize())&&(s=!0)}catch(e){s=!0}return s}function G(t,n){const o=[];let s=t.__first;for(;null!==s;){const t=null===n?e(s):n.get(s);null==t&&T(101),o.push(s),s=t.__next}return o}function q(e){const t=e.getParent();if(null!==t){const n=e.getWritable(),o=t.getWritable(),s=e.getPreviousSibling(),l=e.getNextSibling();if(null===s)if(null!==l){const e=l.getWritable();o.__first=l.__key,e.__prev=null}else o.__first=null;else{const e=s.getWritable();if(null!==l){const t=l.getWritable();t.__prev=e.__key,e.__next=t.__key}else e.__next=null;n.__prev=null}if(null===l)if(null!==s){const e=s.getWritable();o.__last=s.__key,e.__next=null}else o.__last=null;else{const e=l.getWritable();if(null!==s){const t=s.getWritable();t.__next=e.__key,e.__prev=t.__key}else e.__prev=null;n.__next=null}o.__size--,n.__parent=null}}class H{constructor(e,t,n){this._key="",this._xmlElem=e,this._parent=t,this._type=n}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return c(t)?t:null}getNode(){const t=e(this._key);return c(t)?t:null}getSharedType(){return this._xmlElem}getType(){return this._type}getKey(){return this._key}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}syncPropertiesFromLexical(e,t,n){const o=this.getPrevNode(n);$(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&T(83);K(e,this._xmlElem,n,t)}destroy(e){e.collabNodeMap.delete(this._key)}}function J(e,t,n){const o=new H(e,t,n);return e._collabNode=o,o}class Q{constructor(e,t,n){this._key="",this._children=[],this._xmlText=e,this._type=n,this._parent=t}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return i(t)?t:null}getNode(){const t=e(this._key);return i(t)?t:null}getSharedType(){return this._xmlText}getType(){return this._type}getKey(){return this._key}isEmpty(){return 0===this._children.length}getSize(){return 1}getOffset(){const e=this._parent;return null===e&&T(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&T(91),K(e,this._xmlText,n,t)}applyChildrenYjsDelta(e,t){const n=this._children;let o=0;for(let s=0;s<t.length;s++){const l=t[s],i=l.insert,r=l.delete;if(null!=l.retain)o+=l.retain;else if("number"==typeof r){let e=r;for(;e>0;){const{node:t,nodeIndex:s,offset:l,length:i}=B(this,o,!1);if(t instanceof Q||t instanceof S||t instanceof H)n.splice(s,1),e-=1;else{if(!(t instanceof E))break;{const o=Math.min(e,i),r=0!==s?n[s-1]:null,c=t.getSize();0===l&&1===o&&s>0&&r instanceof E&&i===c&&0===Array.from(t._map.keys()).length?(r._text+=t._text,n.splice(s,1)):0===l&&o===c?n.splice(s,1):t._text=R(t._text,l,o,""),e-=o}}}}else{if(null==i)throw new Error("Unexpected delta format");if("string"==typeof i){const{node:e,offset:t}=B(this,o,!0);e instanceof E?e._text=R(e._text,t,0,i):this._xmlText.delete(t,i.length),o+=i.length}else{const t=i,{nodeIndex:s}=B(this,o,!1),l=W(e,t,this);n.splice(s,0,l),o+=1}}}}syncChildrenFromYjs(e){const t=this.getNode();null===t&&T(92);const o=t.__key,s=G(t,null),l=s.length,i=this._children,r=i.length,c=e.collabNodeMap,f=new Set;let a,u,d=0,_=null;r!==l&&(u=t.getWritable());for(let l=0;l<r;l++){const h=s[d],p=i[l],g=p.getNode(),y=p._key;if(null!==g&&h===y){const t=n(g);if(f.add(h),t)if(p._key=h,p instanceof Q){const t=p._xmlText;p.syncPropertiesFromYjs(e,null),p.applyChildrenYjsDelta(e,t.toDelta()),p.syncChildrenFromYjs(e)}else p instanceof E?p.syncPropertiesAndTextFromYjs(e,null):p instanceof H?p.syncPropertiesFromYjs(e,null):p instanceof S||T(93);_=g,d++}else{if(void 0===a){a=new Set;for(let e=0;e<r;e++){const t=i[e]._key;""!==t&&a.add(t)}}if(null!==g&&void 0!==h&&!a.has(h)){q(Y(h)),l--,d++;continue}u=t.getWritable();const n=I(e,p,o),s=n.__key;if(c.set(s,p),null===_){const e=u.getFirstChild();if(u.__first=s,null!==e){const t=e.getWritable();t.__prev=s,n.__next=t.__key}}else{const e=_.getWritable(),t=_.getNextSibling();if(e.__next=s,n.__prev=_.__key,null!==t){const e=t.getWritable();e.__prev=s,n.__next=e.__key}}l===r-1&&(u.__last=s),u.__size++,_=n}}for(let t=0;t<l;t++){const n=s[t];if(!f.has(n)){const t=Y(n),o=e.collabNodeMap.get(n);void 0!==o&&o.destroy(e),q(t)}}}syncPropertiesFromLexical(e,t,n){$(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(e,t,o,s,l,r){const f=this._children[t],a=Y(o);f instanceof Q&&i(a)?(f.syncPropertiesFromLexical(e,a,s),f.syncChildrenFromLexical(e,a,s,l,r)):f instanceof E&&n(a)?f.syncPropertiesAndTextFromLexical(e,a,s):f instanceof H&&c(a)&&f.syncPropertiesFromLexical(e,a,s)}syncChildrenFromLexical(e,t,n,o,s){const l=this.getPrevNode(n),i=null===l?[]:G(l,n),r=G(t,null),c=i.length-1,f=r.length-1,a=e.collabNodeMap;let u,d,_=0,h=0;for(;_<=c&&h<=f;){const t=i[_],l=r[h];if(t===l)this._syncChildFromLexical(e,h,l,n,o,s),_++,h++;else{void 0===u&&(u=new Set(i)),void 0===d&&(d=new Set(r));const n=d.has(t),o=u.has(l);if(n){const t=D(e,Y(l),this);a.set(l,t),o?(this.splice(e,h,1,t),_++,h++):(this.splice(e,h,0,t),h++)}else this.splice(e,h,1),_++}}const p=_>c,g=h>f;if(p&&!g)for(;h<=f;++h){const t=r[h],n=D(e,Y(t),this);this.append(n),a.set(t,n)}else if(g&&!p)for(let t=this._children.length-1;t>=h;t--)this.splice(e,t,1)}append(e){const t=this._xmlText,n=this._children,o=n[n.length-1],s=void 0!==o?o.getOffset()+o.getSize():0;if(e instanceof Q)t.insertEmbed(s,e._xmlText);else if(e instanceof E){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof S?t.insertEmbed(s,e._map):e instanceof H&&t.insertEmbed(s,e._xmlElem);this._children.push(e)}splice(e,t,n,o){const s=this._children,l=s[t];if(void 0===l)return void 0===o&&T(94),void this.append(o);const i=l.getOffset();-1===i&&T(95);const r=this._xmlText;if(0!==n&&r.delete(i,l.getSize()),o instanceof Q)r.insertEmbed(i,o._xmlText);else if(o instanceof E){const e=o._map;null===e.parent&&r.insertEmbed(i,e),r.insert(i+1,o._text)}else o instanceof S?r.insertEmbed(i,o._map):o instanceof H&&r.insertEmbed(i,o._xmlElem);if(0!==n){const o=s.slice(t,t+n);for(let t=0;t<o.length;t++)o[t].destroy(e)}void 0!==o?s.splice(t,n,o):s.splice(t,n)}getChildOffset(e){let t=0;const n=this._children;for(let o=0;o<n.length;o++){const s=n[o];if(s===e)return t;t+=s.getSize()}return-1}destroy(e){const t=e.collabNodeMap,n=this._children;for(let t=0;t<n.length;t++)n[t].destroy(e);t.delete(this._key)}}function V(e,t,n){const o=new Q(e,t,n);return e._collabNode=o,o}function X(e,t,n,o,s,l){null==o&&T(81);const i=V(o.get("root",_),null,"root");return i._key="root",{clientID:o.clientID,collabNodeMap:new Map,cursors:new Map,cursorsContainer:null,doc:o,docMap:s,editor:e,excludedProperties:l||new Map,id:n,nodeProperties:new Map,root:i}}function Z(e,t){const o=t.collabNodeMap.get(e.key);if(void 0===o)return null;let s=e.offset,l=o.getSharedType();if(o instanceof E){l=o._parent._xmlText;const e=o.getOffset();if(-1===e)return null;s=e+1+s}else if(o instanceof Q&&"element"===e.type){const t=e.getNode();if(!i(t))throw Error("Element point must be an element node");let o=0,l=0,r=t.getFirstChild();for(;null!==r&&l++<s;)n(r)?o+=r.getTextContentSize()+1:o++,r=r.getNextSibling();s=o}return x(l,s)}function ee(e,t){return y(e,t.doc)}function te(e,t){if(null==e){if(null!=t)return!0}else if(null==t||!m(e,t))return!0;return!1}function ne(e,t){return{color:t,name:e,selection:null}}function oe(e,t){const n=e.cursorsContainer;if(null!==n){const e=t.selections,o=e.length;for(let t=0;t<o;t++)n.removeChild(e[t])}}function se(e,t){const n=t.selection;null!==n&&oe(e,n)}function le(e,t,n,o,s){const l=e.color,i=document.createElement("span");i.style.cssText=`position:absolute;top:0;bottom:0;right:-1px;width:1px;background-color:${l};z-index:10;`;const r=document.createElement("span");return r.textContent=e.name,r.style.cssText=`position:absolute;left:-2px;top:-16px;background-color:${l};color:#fff;line-height:12px;font-size:12px;padding:2px;font-family:Arial;font-weight:bold;white-space:nowrap;`,i.appendChild(r),{anchor:{key:t,offset:n},caret:i,color:l,focus:{key:o,offset:s},name:r,selections:[]}}function ie(e,n,o,s){const l=e.editor,i=l.getRootElement(),r=e.cursorsContainer;if(null===r||null===i)return;const c=r.offsetParent;if(null===c)return;const f=c.getBoundingClientRect(),a=n.selection;if(null===o)return null===a?void 0:(n.selection=null,void oe(e,a));n.selection=o;const u=o.caret,d=o.color,_=o.selections,h=o.anchor,p=o.focus,g=h.key,y=p.key,x=s.get(g),m=s.get(y);if(null==x||null==m)return;let k;if(x===m&&t(x)){k=[l.getElementByKey(g).getBoundingClientRect()]}else{const e=C(l,x,h.offset,m,p.offset);if(null===e)return;k=w(l,e)}const b=_.length,v=k.length;for(let e=0;e<v;e++){const t=k[e];let n=_[e];if(void 0===n){n=document.createElement("span"),_[e]=n;const t=document.createElement("span");n.appendChild(t),r.appendChild(n)}const o=`position:absolute;top:${t.top-f.top}px;left:${t.left-f.left}px;height:${t.height}px;width:${t.width}px;pointer-events:none;z-index:5;`;n.style.cssText=o,n.firstChild.style.cssText=`${o}left:0;top:0;background-color:${d};opacity:0.3;`,e===v-1&&u.parentNode!==n&&n.appendChild(u)}for(let e=b-1;e>=v;e--){const t=_[e];r.removeChild(t),_.pop()}}function re(e,t){const n=t.awareness.getLocalState();if(null===n)return;const l=n.anchorPos,i=n.focusPos;if(null!==l&&null!==i){const t=ee(l,e),n=ee(i,e);if(null!==t&&null!==n){const[e,l]=fe(t.type,t.index),[i,r]=fe(n.type,n.index);if(null!==e&&null!==i){const t=e.getKey(),n=i.getKey(),c=o();if(!s(c))return;const f=c.anchor,a=c.focus;ce(f,t,l),ce(a,n,r)}}}}function ce(t,o,s){if(t.key!==o||t.offset!==s){let l=e(o);if(null!==l&&!i(l)&&!n(l)){const e=l.getParentOrThrow();o=e.getKey(),s=l.getIndexWithinParent(),l=e}t.set(o,s,i(l)?"element":"text")}}function fe(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof Q){const{node:e,offset:o}=B(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function ae(e,t){const n=Array.from(t.awareness.getStates()),o=e.clientID,s=e.cursors,l=e.editor._editorState._nodeMap,i=new Set;for(let t=0;t<n.length;t++){const r=n[t],[c,f]=r;if(c!==o){i.add(c);const{anchorPos:t,focusPos:n,name:o,color:r,focusing:a}=f;let u=null,d=s.get(c);if(void 0===d&&(d=ne(o,r),s.set(c,d)),null!==t&&null!==n&&a){const o=ee(t,e),s=ee(n,e);if(null!==o&&null!==s){const[e,t]=fe(o.type,o.index),[n,l]=fe(s.type,s.index);if(null!==e&&null!==n){const o=e.getKey(),s=n.getKey();if(u=d.selection,null===u)u=le(d,o,t,s,l);else{const e=u.anchor,n=u.focus;e.key=o,e.offset=t,n.key=s,n.offset=l}}}}ie(e,d,u,l)}}const r=Array.from(s.keys());for(let t=0;t<r.length;t++){const n=r[t];if(!i.has(n)){const t=s.get(n);void 0!==t&&(se(e,t),s.delete(n))}}}function ue(e,t,n,o){const l=t.awareness,i=l.getLocalState();if(null===i)return;const{anchorPos:r,focusPos:c,name:f,color:a,focusing:u,awarenessData:d}=i;let _=null,h=null;(null!==o&&(null===r||o.is(n))||null!==n)&&(s(o)&&(_=Z(o.anchor,e),h=Z(o.focus,e)),(te(r,_)||te(c,h))&&l.setLocalState({anchorPos:_,awarenessData:d,color:a,focusPos:h,focusing:u,name:f}))}function de(e,t){const{target:n}=t,o=W(e,n);if(o instanceof Q&&t instanceof k){const{keysChanged:n,childListChanged:s,delta:l}=t;n.size>0&&o.syncPropertiesFromYjs(e,n),s&&(o.applyChildrenYjsDelta(e,l),o.syncChildrenFromYjs(e))}else if(o instanceof E&&t instanceof b){const{keysChanged:n}=t;n.size>0&&o.syncPropertiesAndTextFromYjs(e,n)}else if(o instanceof H&&t instanceof v){const{attributesChanged:n}=t;n.size>0&&o.syncPropertiesFromYjs(e,n)}else T(82)}function _e(e,t,n,l){const i=e.editor,r=i._editorState;n.forEach((e=>e.delta)),i.update((()=>{const l=i._pendingEditorState;for(let t=0;t<n.length;t++){const o=n[t];de(e,o)}0===f().getChildrenSize()&&f().append(a());const c=o();if(s(c))if(U(c)){const n=r._selection;if(s(n)){const o=N(i,0,r),s=N(i,0,l),[a,d]=o.getOffsetsFromSelection(n),_=a>=0&&d>=0?s.createSelectionFromOffsets(a,d,o):null;null!==_?u(_):(re(e,t),U(c)&&f().selectEnd())}ue(e,t,n,o())}else re(e,t)}),{onUpdate:()=>{ae(e,t)},skipTransforms:!0,tag:l?"historic":"collaboration"})}function he(t,s,l,i,r,c,a,u){!function(e,t){e.doc.transact(t,e)}(t,(()=>{i.read((()=>{if(u.has("collaboration")||u.has("historic"))return void(a.size>0&&function(t,o){const s=Array.from(o),l=t.collabNodeMap,i=[];for(let t=0;t<s.length;t++){const o=s[t],r=e(o),c=l.get(o);if(c instanceof E)if(n(r))i.push([c,r.__text]);else{const e=c.getOffset();if(-1===e)continue;const t=c._parent;c._normalized=!0,t._xmlText.delete(e,1),l.delete(o);const n=t._children,s=n.indexOf(c);n.splice(s,1)}}for(let e=0;e<i.length;e++){const[t,n]=i[e];t instanceof E&&"string"==typeof n&&(t._text=n)}}(t,a));if(r.has("root")){const e=l._nodeMap,n=f(),o=t.root;o.syncPropertiesFromLexical(t,n,e),o.syncChildrenFromLexical(t,n,e,r,c)}const i=o(),d=l._selection;ue(t,s,d,i)}))}))}const pe=d("CONNECTED_COMMAND"),ge=d("TOGGLE_CONNECT_COMMAND");function ye(e,t){return new P(t,{trackedOrigins:new Set([e,null])})}function xe(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})}function me(e,t,n,o,s){const{awareness:l}=e;let i=l.getLocalState();null===i&&(i={anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t}),i.focusing=o,l.setLocalState(i)}export{pe as CONNECTED_COMMAND,ge as TOGGLE_CONNECT_COMMAND,X as createBinding,ye as createUndoManager,xe as initLocalState,me as setLocalStateFocus,ae as syncCursorPositions,he as syncLexicalUpdateToYjs,_e as syncYjsChangesToLexical};
8
+
9
+ import{$getNodeByKey as e,$isLineBreakNode as t,$isTextNode as n,$getSelection as o,$isRangeSelection as s,createEditor as l,$getRoot as i,$isElementNode as r,$isRootNode as c,$isDecoratorNode 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 k,YXmlEvent as b,UndoManager as v}from"yjs";import{$createChildrenArray as P}from"@lexical/offset";import{createDOMRange as C,createRectsFromDOMRange as w}from"@lexical/selection";function N(e){return e&&e.__esModule&&Object.prototype.hasOwnProperty.call(e,"default")?e.default:e}var T=N((function(e){const t=new URLSearchParams;t.append("code",e);for(let e=1;e<arguments.length;e++)t.append("v",arguments[e]);throw Error(`Minified Lexical error #${e}; visit https://lexical.dev/docs/error?${t} for the full message or use the non-minified dev environment for full errors and additional helpful warnings.`)}));class S{constructor(e,t){this._key="",this._map=e,this._parent=t,this._type="linebreak"}getNode(){const n=e(this._key);return t(n)?n:null}getKey(){return this._key}getSharedType(){return this._map}getType(){return this._type}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}destroy(e){e.collabNodeMap.delete(this._key)}}function F(e,t){const n=new S(e,t);return e._collabNode=n,n}class E{constructor(e,t,n,o){this._key="",this._map=e,this._parent=n,this._text=t,this._type=o,this._normalized=!1}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return n(t)?t:null}getNode(){const t=e(this._key);return n(t)?t:null}getSharedType(){return this._map}getType(){return this._type}getKey(){return this._key}getSize(){return this._text.length+(this._normalized?0:1)}getOffset(){return this._parent.getChildOffset(this)}spliceText(e,t,n){const o=this._parent._xmlText,s=this.getOffset()+1+e;0!==t&&o.delete(s,t),""!==n&&o.insert(s,n)}syncPropertiesAndTextFromLexical(e,t,n){const l=this.getPrevNode(n),i=t.__text;if($(e,this._map,l,t),null!==l){const e=l.__text;if(e!==i){!function(e,t,n,l){const i=o();let r=l.length;if(s(i)&&i.isCollapsed()){const e=i.anchor;e.key===t&&(r=e.offset)}const c=function(e,t,n){const o=e.length,s=t.length;let l=0,i=0;for(;l<o&&l<s&&e[l]===t[l]&&l<n;)l++;for(;i+l<o&&i+l<s&&e[o-i-1]===t[s-i-1];)i++;for(;i+l<o&&i+l<s&&e[l]===t[l];)l++;return{index:l,insert:t.slice(l,s-i),remove:o-l-i}}(n,l,r);e.spliceText(c.index,c.remove,c.insert)}(this,t.__key,e,i),this._text=i}}}syncPropertiesAndTextFromYjs(e,t){const n=this.getNode();null===n&&T(84),K(e,this._map,n,t);const o=this._text;if(n.__text!==o){n.getWritable().__text=o}}destroy(e){e.collabNodeMap.delete(this._key)}}function M(e,t,n,o){const s=new E(e,t,n,o);return e._collabNode=s,s}const z=new Set(["__key","__parent","__next","__prev"]),L=new Set(["__first","__last","__size"]),O=new Set(["__cachedText"]),j=new Set(["__text"]);function A(e,t,o){if(z.has(e))return!0;if(n(t)){if(j.has(e))return!0}else if(r(t)&&(L.has(e)||c(t)&&O.has(e)))return!0;const s=t.constructor,l=o.excludedProperties.get(s);return null!=l&&l.has(e)}function Y(t){const n=e(t);return null===n&&T(85),n}function D(e,o,s){const l=o.__type;let i;if(r(o)){i=V(new d,s,l),i.syncPropertiesFromLexical(e,o,null),i.syncChildrenFromLexical(e,o,null,null,null)}else if(n(o)){i=M(new _,o.__text,s,l),i.syncPropertiesAndTextFromLexical(e,o,null)}else if(t(o)){const e=new _;e.set("__type","linebreak"),i=F(e,s)}else if(a(o)){i=J(new h,s,l),i.syncPropertiesFromLexical(e,o,null)}else T(86);return i._key=o.__key,i}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):M(t,"",i,s);if(t instanceof h)return J(t,i,s)}return o}function I(e,t,n){const o=t.getType(),s=e.editor._nodes.get(o);void 0===s&&T(88,o);const l=new s.klass;if(l.__parent=n,t._key=l.__key,t instanceof Q){const n=t._xmlText;t.syncPropertiesFromYjs(e,null),t.applyChildrenYjsDelta(e,n.toDelta()),t.syncChildrenFromYjs(e)}else t instanceof E?t.syncPropertiesAndTextFromYjs(e,null):t instanceof H&&t.syncPropertiesFromYjs(e,null);return e.collabNodeMap.set(l.__key,t),l}function K(e,t,n,o){const s=null===o?t instanceof _?Array.from(t.keys()):Object.keys(t.getAttributes()):Array.from(o);let i;for(let o=0;o<s.length;o++){const r=s[o];if(A(r,n,e))continue;const c=n[r];let a=t instanceof _?t.get(r):t.getAttribute(r);if(c!==a){if(a instanceof p){const t=e.docMap;c instanceof p&&t.delete(c.guid);const n=l(),o=a.guid;n._key=o,t.set(o,a),a=n}void 0===i&&(i=n.getWritable()),i[r]=a}}}function $(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 B(e,t,n){let o=0,s=0;const l=e._children,i=l.length;for(;s<i;s++){const e=l[s],r=o;o+=e.getSize();if((n?o>=t:o>t)&&e instanceof E){let n=t-r-1;n<0&&(n=0);return{length:o-t,node:e,nodeIndex:s,offset:n}}if(o>t)return{length:0,node:e,nodeIndex:s,offset:r};if(s===i-1)return{length:0,node:null,nodeIndex:s+1,offset:r+1}}return{length:0,node:null,nodeIndex:0,offset:0}}function U(e){const t=e.anchor,o=e.focus;let s=!1;try{const e=t.getNode(),l=o.getNode();(!e.isAttached()||!l.isAttached()||n(e)&&t.offset>e.getTextContentSize()||n(l)&&o.offset>l.getTextContentSize())&&(s=!0)}catch(e){s=!0}return s}function G(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 i().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):i().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 a(t)?t:null}getNode(){const t=e(this._key);return a(t)?t:null}getSharedType(){return this._xmlElem}getType(){return this._type}getKey(){return this._key}getSize(){return 1}getOffset(){return this._parent.getChildOffset(this)}syncPropertiesFromLexical(e,t,n){const o=this.getPrevNode(n);$(e,this._xmlElem,o,t)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&T(83);K(e,this._xmlElem,n,t)}destroy(e){e.collabNodeMap.delete(this._key)}}function J(e,t,n){const o=new H(e,t,n);return e._collabNode=o,o}class Q{constructor(e,t,n){this._key="",this._children=[],this._xmlText=e,this._type=n,this._parent=t}getPrevNode(e){if(null===e)return null;const t=e.get(this._key);return r(t)?t:null}getNode(){const t=e(this._key);return r(t)?t:null}getSharedType(){return this._xmlText}getType(){return this._type}getKey(){return this._key}isEmpty(){return 0===this._children.length}getSize(){return 1}getOffset(){const e=this._parent;return null===e&&T(90),e.getChildOffset(this)}syncPropertiesFromYjs(e,t){const n=this.getNode();null===n&&T(91),K(e,this._xmlText,n,t)}applyChildrenYjsDelta(e,t){const n=this._children;let o=0;for(let s=0;s<t.length;s++){const l=t[s],i=l.insert,r=l.delete;if(null!=l.retain)o+=l.retain;else if("number"==typeof r){let e=r;for(;e>0;){const{node:t,nodeIndex:s,offset:l,length:i}=B(this,o,!1);if(t instanceof Q||t instanceof S||t instanceof H)n.splice(s,1),e-=1;else{if(!(t instanceof E))break;{const o=Math.min(e,i),r=0!==s?n[s-1]:null,c=t.getSize();0===l&&1===o&&s>0&&r instanceof E&&i===c&&0===Array.from(t._map.keys()).length?(r._text+=t._text,n.splice(s,1)):0===l&&o===c?n.splice(s,1):t._text=R(t._text,l,o,""),e-=o}}}}else{if(null==i)throw new Error("Unexpected delta format");if("string"==typeof i){const{node:e,offset:t}=B(this,o,!0);e instanceof E?e._text=R(e._text,t,0,i):this._xmlText.delete(t,i.length),o+=i.length}else{const t=i,{nodeIndex:s}=B(this,o,!1),l=W(e,t,this);n.splice(s,0,l),o+=1}}}}syncChildrenFromYjs(e){const t=this.getNode();null===t&&T(92);const o=t.__key,s=P(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 E?p.syncPropertiesAndTextFromYjs(e,null):p instanceof H?p.syncPropertiesFromYjs(e,null):p instanceof S||T(93);_=g,d++}else{if(void 0===f){f=new Set;for(let e=0;e<r;e++){const t=i[e]._key;""!==t&&f.add(t)}}if(null!==g&&void 0!==h&&!f.has(h)){G(Y(h)),l--,d++;continue}u=t.getWritable();const n=I(e,p,o),s=n.__key;if(c.set(s,p),null===_){const e=u.getFirstChild();if(u.__first=s,null!==e){const t=e.getWritable();t.__prev=s,n.__next=t.__key}}else{const e=_.getWritable(),t=_.getNextSibling();if(e.__next=s,n.__prev=_.__key,null!==t){const e=t.getWritable();e.__prev=s,n.__next=e.__key}}l===r-1&&(u.__last=s),u.__size++,_=n}}for(let t=0;t<l;t++){const n=s[t];if(!a.has(n)){const t=Y(n),o=e.collabNodeMap.get(n);void 0!==o&&o.destroy(e),G(t)}}}syncPropertiesFromLexical(e,t,n){$(e,this._xmlText,this.getPrevNode(n),t)}_syncChildFromLexical(e,t,o,s,l,i){const c=this._children[t],f=Y(o);c instanceof Q&&r(f)?(c.syncPropertiesFromLexical(e,f,s),c.syncChildrenFromLexical(e,f,s,l,i)):c instanceof E&&n(f)?c.syncPropertiesAndTextFromLexical(e,f,s):c instanceof H&&a(f)&&c.syncPropertiesFromLexical(e,f,s)}syncChildrenFromLexical(e,t,n,o,s){const l=this.getPrevNode(n),i=null===l?[]:P(l,n),r=P(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 E){const n=e._map;null===n.parent&&t.insertEmbed(s,n),t.insert(s+1,e._text)}else e instanceof S?t.insertEmbed(s,e._map):e instanceof H&&t.insertEmbed(s,e._xmlElem);this._children.push(e)}splice(e,t,n,o){const s=this._children,l=s[t];if(void 0===l)return void 0===o&&T(94),void this.append(o);const i=l.getOffset();-1===i&&T(95);const r=this._xmlText;if(0!==n&&r.delete(i,l.getSize()),o instanceof Q)r.insertEmbed(i,o._xmlText);else if(o instanceof E){const e=o._map;null===e.parent&&r.insertEmbed(i,e),r.insert(i+1,o._text)}else o instanceof S?r.insertEmbed(i,o._map):o instanceof H&&r.insertEmbed(i,o._xmlElem);if(0!==n){const o=s.slice(t,t+n);for(let t=0;t<o.length;t++)o[t].destroy(e)}void 0!==o?s.splice(t,n,o):s.splice(t,n)}getChildOffset(e){let t=0;const n=this._children;for(let o=0;o<n.length;o++){const s=n[o];if(s===e)return t;t+=s.getSize()}return-1}destroy(e){const t=e.collabNodeMap,n=this._children;for(let t=0;t<n.length;t++)n[t].destroy(e);t.delete(this._key)}}function V(e,t,n){const o=new Q(e,t,n);return e._collabNode=o,o}function X(e,t,n,o,s,l){null==o&&T(81);const i=V(o.get("root",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,l=o.getSharedType();if(o instanceof E){l=o._parent._xmlText;const e=o.getOffset();if(-1===e)return null;s=e+1+s}else if(o instanceof Q&&"element"===e.type){const t=e.getNode();r(t)||T(184);let o=0,l=0,i=t.getFirstChild();for(;null!==i&&l++<s;)n(i)?o+=i.getTextContentSize()+1:o++,i=i.getNextSibling();s=o}return y(l,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 k;if(x===m&&t(x)){k=[l.getElementByKey(g).getBoundingClientRect()]}else{const e=C(l,x,h.offset,m,p.offset);if(null===e)return;k=w(l,e)}const b=_.length,v=k.length;for(let e=0;e<v;e++){const t=k[e];let n=_[e];if(void 0===n){n=document.createElement("span"),_[e]=n;const t=document.createElement("span");n.appendChild(t),r.appendChild(n)}const o=`position:absolute;top:${t.top-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=b-1;e>=v;e--){const t=_[e];r.removeChild(t),_.pop()}}function re(e,t){const n=t.awareness.getLocalState();if(null===n)return;const l=n.anchorPos,i=n.focusPos;if(null!==l&&null!==i){const t=ee(l,e),n=ee(i,e);if(null!==t&&null!==n){const[e,l]=ae(t.type,t.index),[i,r]=ae(n.type,n.index);if(null!==e&&null!==i){const t=e.getKey(),n=i.getKey(),c=o();if(!s(c))return;const a=c.anchor,f=c.focus;ce(a,t,l),ce(f,n,r)}}}}function ce(t,o,s){if(t.key!==o||t.offset!==s){let l=e(o);if(null!==l&&!r(l)&&!n(l)){const e=l.getParentOrThrow();o=e.getKey(),s=l.getIndexWithinParent(),l=e}t.set(o,s,r(l)?"element":"text")}}function ae(e,t){const n=e._collabNode;if(void 0===n)return[null,0];if(n instanceof Q){const{node:e,offset:o}=B(n,t,!0);return null===e?[n,0]:[e,o]}return[null,0]}function fe(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{anchorPos:t,focusPos:n,name:o,color:r,focusing:f}=a;let u=null,d=s.get(c);if(void 0===d&&(d=ne(o,r),s.set(c,d)),null!==t&&null!==n&&f){const o=ee(t,e),s=ee(n,e);if(null!==o&&null!==s){const[e,t]=ae(o.type,o.index),[n,l]=ae(s.type,s.index);if(null!==e&&null!==n){const o=e.getKey(),s=n.getKey();if(u=d.selection,null===u)u=le(d,o,t,s,l);else{const e=u.anchor,n=u.focus;e.key=o,e.offset=t,n.key=s,n.offset=l}}}}ie(e,d,u,l)}}const r=Array.from(s.keys());for(let t=0;t<r.length;t++){const n=r[t];if(!i.has(n)){const t=s.get(n);void 0!==t&&(se(e,t),s.delete(n))}}}function ue(e,t,n,o){const l=t.awareness,i=l.getLocalState();if(null===i)return;const{anchorPos:r,focusPos:c,name: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({anchorPos:_,awarenessData:d,color:f,focusPos:h,focusing:u,name:a}))}function de(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 E&&t instanceof k){const{keysChanged:n}=t;n.size>0&&o.syncPropertiesAndTextFromYjs(e,n)}else if(o instanceof H&&t instanceof b){const{attributesChanged:n}=t;n.size>0&&o.syncPropertiesFromYjs(e,n)}else T(82)}function _e(e,t,n,l){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];de(e,o)}0===i().getChildrenSize()&&i().append(f());const l=o();if(s(l))if(U(l)){const n=c._selection;if(s(n)&&(re(e,t),U(l))){q(l.anchor.key,c)}ue(e,t,n,o())}else re(e,t)}),{onUpdate:()=>{fe(e,t)},skipTransforms:!0,tag:l?"historic":"collaboration"})}function he(t,s,l,r,c,a,f,u){!function(e,t){e.doc.transact(t,e)}(t,(()=>{r.read((()=>{if(u.has("collaboration")||u.has("historic"))return void(f.size>0&&function(t,o){const s=Array.from(o),l=t.collabNodeMap,i=[];for(let t=0;t<s.length;t++){const o=s[t],r=e(o),c=l.get(o);if(c instanceof E)if(n(r))i.push([c,r.__text]);else{const e=c.getOffset();if(-1===e)continue;const t=c._parent;c._normalized=!0,t._xmlText.delete(e,1),l.delete(o);const n=t._children,s=n.indexOf(c);n.splice(s,1)}}for(let e=0;e<i.length;e++){const[t,n]=i[e];t instanceof E&&"string"==typeof n&&(t._text=n)}}(t,f));if(c.has("root")){const e=l._nodeMap,n=i(),o=t.root;o.syncPropertiesFromLexical(t,n,e),o.syncChildrenFromLexical(t,n,e,c,a)}const r=o(),d=l._selection;ue(t,s,d,r)}))}))}const pe=u("CONNECTED_COMMAND"),ge=u("TOGGLE_CONNECT_COMMAND");function ye(e,t){return new v(t,{trackedOrigins:new Set([e,null])})}function xe(e,t,n,o,s){e.awareness.setLocalState({anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t})}function me(e,t,n,o,s){const{awareness:l}=e;let i=l.getLocalState();null===i&&(i={anchorPos:null,awarenessData:s,color:n,focusPos:null,focusing:o,name:t}),i.focusing=o,l.setLocalState(i)}export{pe as CONNECTED_COMMAND,ge as TOGGLE_CONNECT_COMMAND,X as createBinding,ye as createUndoManager,xe as initLocalState,me as setLocalStateFocus,fe as syncCursorPositions,he as syncLexicalUpdateToYjs,_e as syncYjsChangesToLexical};
package/SyncCursors.d.ts CHANGED
@@ -27,6 +27,6 @@ export type Cursor = {
27
27
  name: string;
28
28
  selection: null | CursorSelection;
29
29
  };
30
- export declare function syncLocalCursorPosition(binding: Binding, provider: Provider): void;
30
+ export declare function $syncLocalCursorPosition(binding: Binding, provider: Provider): void;
31
31
  export declare function syncCursorPositions(binding: Binding, provider: Provider): void;
32
32
  export declare function syncLexicalSelectionToYjs(binding: Binding, provider: Provider, prevSelection: null | BaseSelection, nextSelection: null | BaseSelection): void;
package/Utils.d.ts CHANGED
@@ -6,7 +6,7 @@
6
6
  *
7
7
  */
8
8
  import type { Binding, YjsNode } from '.';
9
- import type { ElementNode, LexicalNode, NodeMap, RangeSelection } from 'lexical';
9
+ import type { EditorState, LexicalNode, RangeSelection } from 'lexical';
10
10
  import { NodeKey } from 'lexical';
11
11
  import { Map as YMap, XmlElement, XmlText } from 'yjs';
12
12
  import { CollabDecoratorNode } from './CollabDecoratorNode';
@@ -16,7 +16,7 @@ import { CollabTextNode } from './CollabTextNode';
16
16
  export declare function getIndexOfYjsNode(yjsParentNode: YjsNode, yjsNode: YjsNode): number;
17
17
  export declare function $getNodeByKeyOrThrow(key: NodeKey): LexicalNode;
18
18
  export declare function $createCollabNodeFromLexicalNode(binding: Binding, lexicalNode: LexicalNode, parent: CollabElementNode): CollabElementNode | CollabTextNode | CollabLineBreakNode | CollabDecoratorNode;
19
- export declare function getOrInitCollabNodeFromSharedType(binding: Binding, sharedType: XmlText | YMap<unknown> | XmlElement, parent?: CollabElementNode): CollabElementNode | CollabTextNode | CollabLineBreakNode | CollabDecoratorNode;
19
+ export declare function $getOrInitCollabNodeFromSharedType(binding: Binding, sharedType: XmlText | YMap<unknown> | XmlElement, parent?: CollabElementNode): CollabElementNode | CollabTextNode | CollabLineBreakNode | CollabDecoratorNode;
20
20
  export declare function createLexicalNodeFromCollabNode(binding: Binding, collabNode: CollabElementNode | CollabTextNode | CollabDecoratorNode | CollabLineBreakNode, parentKey: NodeKey): LexicalNode;
21
21
  export declare function syncPropertiesFromYjs(binding: Binding, sharedType: XmlText | YMap<unknown> | XmlElement, lexicalNode: LexicalNode, keysChanged: null | Set<string>): void;
22
22
  export declare function syncPropertiesFromLexical(binding: Binding, sharedType: XmlText | YMap<unknown> | XmlElement, prevLexicalNode: null | LexicalNode, nextLexicalNode: LexicalNode): void;
@@ -29,5 +29,5 @@ export declare function getPositionFromElementAndOffset(node: CollabElementNode,
29
29
  };
30
30
  export declare function doesSelectionNeedRecovering(selection: RangeSelection): boolean;
31
31
  export declare function syncWithTransaction(binding: Binding, fn: () => void): void;
32
- export declare function createChildrenArray(element: ElementNode, nodeMap: null | NodeMap): Array<NodeKey>;
33
32
  export declare function removeFromParent(node: LexicalNode): void;
33
+ export declare function $moveSelectionToPreviousNode(anchorNodeKey: string, currentEditorState: EditorState): void;
package/package.json CHANGED
@@ -11,12 +11,12 @@
11
11
  "crdt"
12
12
  ],
13
13
  "license": "MIT",
14
- "version": "0.14.4",
14
+ "version": "0.15.0",
15
15
  "main": "LexicalYjs.js",
16
16
  "types": "index.d.ts",
17
17
  "dependencies": {
18
- "@lexical/offset": "0.14.4",
19
- "lexical": "0.14.4"
18
+ "@lexical/offset": "0.15.0",
19
+ "lexical": "0.15.0"
20
20
  },
21
21
  "peerDependencies": {
22
22
  "yjs": ">=13.5.22"