edytor 0.0.1 → 0.0.6

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.
@@ -1,56 +1,8 @@
1
+ import { Map as Map$1, Text, Array as Array$1, Doc } from 'yjs';
1
2
  import { customAlphabet } from 'nanoid';
2
- import { Text, Map as Map$1, encodeStateAsUpdateV2, Doc, Array as Array$1, applyUpdateV2 } from 'yjs';
3
3
  import isHotkey from 'is-hotkey';
4
- import { WebrtcProvider } from 'y-webrtc';
5
4
  import { WebsocketProvider } from 'y-websocket';
6
5
 
7
- var nanoid = function nanoid() {
8
- return "y-" + customAlphabet("346789ABCDEFGHJKLMNPQRTUV-WXYabcdefghijkmnpqrtwxyz", 20)();
9
- };
10
- var getNode = function getNode(node) {
11
- var _node$parent;
12
-
13
- return node == null ? void 0 : (_node$parent = node.parent) == null ? void 0 : _node$parent.parent;
14
- };
15
- var getIndex = function getIndex(node) {
16
- var index = 0;
17
- var stop = false;
18
- var n = node.parent._start;
19
-
20
- while (n !== null && !stop) {
21
- if (n.countable && !n.deleted) {
22
- var c = n.content.getContent();
23
-
24
- for (var i = 0; i < c.length; i++) {
25
- if (c[i] === node) {
26
- stop = true;
27
- break;
28
- }
29
-
30
- index++;
31
- }
32
- }
33
-
34
- n = n.right;
35
- }
36
-
37
- return index;
38
- };
39
- var getPath = function getPath(node) {
40
- var p = [];
41
- var n = node;
42
-
43
- while (!!n) {
44
- p.push(getIndex(n));
45
- n = getNode(n);
46
- }
47
-
48
- return p.reverse();
49
- };
50
- var getId = function getId(node) {
51
- return node.get("id");
52
- };
53
-
54
6
  function _extends() {
55
7
  _extends = Object.assign || function (target) {
56
8
  for (var i = 1; i < arguments.length; i++) {
@@ -100,13 +52,78 @@ function _objectWithoutPropertiesLoose(source, excluded) {
100
52
  return target;
101
53
  }
102
54
 
103
- function _assertThisInitialized(self) {
104
- if (self === void 0) {
105
- throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
55
+ var _excluded = ["id", "text", "data"];
56
+ var createLeaf$1 = function createLeaf(props) {
57
+ var leaf = new Map$1();
58
+ leaf.set("id", (props == null ? void 0 : props.id) || nanoid());
59
+ leaf.set("text", new Text((props == null ? void 0 : props.text) || ""));
60
+
61
+ if (props) {
62
+ var text = props.text,
63
+ data = props.data,
64
+ marks = _objectWithoutPropertiesLoose(props, _excluded);
65
+
66
+ leaf.set("text", new Text(text || ""));
67
+ data && leaf.set("data", new Map$1(Object.entries(data)));
68
+ marks && Object.keys(marks).forEach(function (mark) {
69
+ leaf.set(mark, marks[mark]);
70
+ });
106
71
  }
107
72
 
108
- return self;
109
- }
73
+ return leaf;
74
+ };
75
+
76
+ var createNode = function createNode(type, props) {
77
+ var node = new Map$1();
78
+ node.set("id", (props == null ? void 0 : props.id) || nanoid());
79
+ node.set("type", type);
80
+
81
+ if (props != null && props.data) {
82
+ node.set("data", new Map$1(Object.entries(props.data)));
83
+ }
84
+
85
+ node.set("content", (props == null ? void 0 : props.content) instanceof Array$1 ? props.content : Array$1.from((props == null ? void 0 : props.content) || [createLeaf$1()]));
86
+ node.set("children", (props == null ? void 0 : props.children) instanceof Array$1 ? props.children : Array$1.from((props == null ? void 0 : props.children) || []));
87
+ return node;
88
+ };
89
+ var YNode = /*#__PURE__*/function (_Map) {
90
+ _inheritsLoose(YNode, _Map);
91
+
92
+ function YNode() {
93
+ return _Map.apply(this, arguments) || this;
94
+ }
95
+
96
+ return YNode;
97
+ }(Map$1);
98
+
99
+ var _excluded$1 = ["type", "content", "children"];
100
+ var getChildren = function getChildren(_ref) {
101
+ var type = _ref.type,
102
+ _ref$content = _ref.content,
103
+ content = _ref$content === void 0 ? [] : _ref$content,
104
+ _ref$children = _ref.children,
105
+ children = _ref$children === void 0 ? [] : _ref$children,
106
+ props = _objectWithoutPropertiesLoose(_ref, _excluded$1);
107
+
108
+ return createNode(type, _extends({}, props, {
109
+ children: children.map(getChildren),
110
+ content: content.map(createLeaf$1)
111
+ }));
112
+ };
113
+ var EdytorDoc = /*#__PURE__*/function (_Doc) {
114
+ _inheritsLoose(EdytorDoc, _Doc);
115
+
116
+ function EdytorDoc() {
117
+ return _Doc.apply(this, arguments) || this;
118
+ }
119
+
120
+ return EdytorDoc;
121
+ }(Doc);
122
+ var DocFromJson = function DocFromJson(value, initialDoc) {
123
+ var array = (initialDoc || new Doc()).getArray("children");
124
+ array.insert(0, value.map(getChildren));
125
+ return array.doc;
126
+ };
110
127
 
111
128
  var deleteText = function deleteText(editor, _ref) {
112
129
  var mode = _ref.mode,
@@ -124,21 +141,28 @@ var deleteText = function deleteText(editor, _ref) {
124
141
  case "collapsed":
125
142
  {
126
143
  if (mode === "backward" && edges.startNode) {
127
- console.log(start.nodeIndex, leafNodeChildrenLength(start.leaf), start.path.length); // unnest node if its the last of its parent and if he is nested
144
+ if (edges.startDocument) return; // unnest node if its the last of its parent and if he is nested or it's the first its the only child of its parent
128
145
 
129
- if (start.nodeIndex === start.node.parent.length - 1 && start.path.length > 1 && start.nodeIndex !== 0) {
130
- console.log(start.leafId);
146
+ if (start.nodeIndex === start.node.parent.length - 1 && start.path.length > 2 && (start.nodeIndex !== 0 || start.node.parent.length === 1)) {
131
147
  var node = copyNode(start.node);
148
+ var nodeGrandParent = start.node.parent.parent;
149
+ var index = getIndex(nodeGrandParent);
132
150
  start.node.parent["delete"](start.nodeIndex);
133
- start.node.parent.parent.parent.insert(getIndex(start.node.parent.parent) + 1, [node]);
134
- setPosition(start.leafId, {
151
+ nodeGrandParent.parent.insert(index + 1, [node]);
152
+ return setPosition(start.leafId, {
135
153
  offset: 0
136
154
  });
137
155
  } else {
138
- // merge node with the prev one if it's not the last of its parent regardless of its nesting
139
- mergeContentWithPrevLeaf(editor);
140
- return setPosition(start.leafId, {
141
- offset: 0
156
+ return editor.doc.transact(function () {
157
+ var _mergeContentWithPrev = mergeContentWithPrevLeaf(editor),
158
+ prevLeaf = _mergeContentWithPrev[0],
159
+ offset = _mergeContentWithPrev[1];
160
+
161
+ setTimeout(function () {
162
+ setPosition(prevLeaf.get("id"), {
163
+ offset: offset
164
+ });
165
+ });
142
166
  });
143
167
  }
144
168
  }
@@ -154,14 +178,22 @@ var deleteText = function deleteText(editor, _ref) {
154
178
 
155
179
  if (mode === "forward" && edges.endNode) {
156
180
  // merge node with the next node because we are at edge end of it
157
- return mergeContentWithNextLeaf(editor);
181
+ return editor.doc.transact(function () {
182
+ mergeContentWithNextLeaf(editor);
183
+ setTimeout(function () {
184
+ setPosition(start.leafId, {
185
+ offset: start.offset
186
+ });
187
+ });
188
+ });
158
189
  }
159
190
 
160
191
  if (mode === "forward" && edges.endLeaf) {
161
192
  // delete content in the next leaf because we are at its edge end
162
193
  var nextLeaf = leafNodeContent(start.leaf).get(start.leafIndex + 1);
163
194
  return deleteLeafText(nextLeaf, 0, length || 1);
164
- }
195
+ } // just delete text
196
+
165
197
 
166
198
  deleteLeafText(start.leaf, start.offset + (mode === "backward" ? -length || -1 : length), length || 1);
167
199
  setPosition(start.leaf.get("id"), {
@@ -174,11 +206,11 @@ var deleteText = function deleteText(editor, _ref) {
174
206
  {
175
207
  deleteLeafText(start.leaf, start.offset, length, true);
176
208
 
177
- if (hasChildren(getNode(start.leaf))) {
178
- leafNodeContent(start.leaf).insert(0, [new YLeaf()]);
209
+ if (isNodeContentEmpty(start.node)) {
210
+ leafNodeContent(start.leaf).insert(0, [createLeaf$1()]);
179
211
  }
180
212
 
181
- setPosition(start.leaf.get("id"), {
213
+ setPosition(start.leafId, {
182
214
  offset: start.offset
183
215
  });
184
216
  break;
@@ -194,7 +226,7 @@ var deleteText = function deleteText(editor, _ref) {
194
226
  reap = _LeavesHarvest.reap,
195
227
  burn = _LeavesHarvest.burn;
196
228
 
197
- editor.doc.traverse(function (leaf, isText) {
229
+ traverse(editor, function (leaf, isText) {
198
230
  if (isText) {
199
231
  var path = getPath(leaf);
200
232
  var l = leafLength(leaf);
@@ -226,7 +258,7 @@ var deleteText = function deleteText(editor, _ref) {
226
258
 
227
259
  var applyMarksFromParent = function applyMarksFromParent(parent, leafs) {
228
260
  Array.from(parent.keys(), function (key) {
229
- if (key !== "text") {
261
+ if (key !== "text" && key !== "id" && key !== "data") {
230
262
  leafs.forEach(function (leaf) {
231
263
  leaf.set(key, parent.get(key));
232
264
  });
@@ -258,13 +290,13 @@ var formatAtEqualPath = function formatAtEqualPath(_ref) {
258
290
  var _extends2;
259
291
 
260
292
  deleteLeafText(leaf, start.offset, leafLength(leaf));
261
- var formatedLeaf = new YLeaf(_extends({}, leaf.toJSON(), (_extends2 = {
293
+ var formatedLeaf = createLeaf$1(_extends({}, leaf.toJSON(), (_extends2 = {
262
294
  id: undefined
263
295
  }, _extends2[format] = value, _extends2.text = content.substring(start.offset, end.offset), _extends2)));
264
296
  var newLeaves = [formatedLeaf];
265
297
 
266
298
  if (remainingText.length > 0) {
267
- newLeaves.push(new YLeaf(_extends({}, leaf.toJSON(), {
299
+ newLeaves.push(createLeaf$1(_extends({}, leaf.toJSON(), {
268
300
  id: undefined,
269
301
  text: remainingText
270
302
  })));
@@ -298,67 +330,76 @@ var formatText = function formatText(editor, mark) {
298
330
  switch (type) {
299
331
  case "collapsed":
300
332
  {
301
- var isMarkActive = start.leaf.has(format);
333
+ var isMarkActive = start.leaf.has(format) && start.leaf.get(format) === value;
302
334
 
303
- if (isMarkActive && isLeafEmpty(start.leaf)) {
304
- start.leaf["delete"](format);
305
- newPath = leafNodeContent(start.leaf).toArray().indexOf(start.leaf);
335
+ if (isLeafEmpty(start.leaf)) {
336
+ // if the leaf is empty we only toggle the mark
337
+ isMarkActive ? start.leaf["delete"](format) : start.leaf.set(format, value);
306
338
  } else {
339
+ var _createLeaf;
340
+
341
+ // if leaf is not empty we split the current leaf in half to insert an empty fragment with the desired mark
342
+ splitLeaf(editor);
343
+ var fragment = createLeaf$1((_createLeaf = {
344
+ text: ""
345
+ }, _createLeaf[format] = value, _createLeaf)); // we are applying the current marks on the fragment
346
+
347
+ applyMarksFromParent(start.leaf, [fragment]); // we remove the mark if its here
348
+
349
+ if (isMarkActive) fragment["delete"](format); // we insert the fragment after the spitted leaf
350
+
351
+ leafNodeContent(start.leaf).insert(start.leafIndex + 1, [fragment]); // we remove the spitted leaf if empty
352
+
307
353
  if (isLeafEmpty(start.leaf)) {
308
- isMarkActive ? start.leaf["delete"](format) : start.leaf.set(format, value);
309
- } else {
310
- var _YLeaf;
354
+ leafNodeContent(start.leaf)["delete"](start.leafIndex);
355
+ }
311
356
 
312
- splitLeaf(editor, {
313
- yText: start.leaf.get("text"),
314
- at: start
357
+ setTimeout(function () {
358
+ setPosition(fragment.get("id"), {
359
+ offset: 0
315
360
  });
316
- var newChild = new YLeaf((_YLeaf = {
317
- text: ""
318
- }, _YLeaf[format] = value, _YLeaf));
319
- applyMarksFromParent(start.leaf, [newChild]);
320
- if (isMarkActive) newChild["delete"](format);
321
- leafNodeContent(start.leaf).insert(start.path.slice().reverse()[0] + 1, [newChild]);
322
-
323
- if (isLeafEmpty(start.leaf)) {
324
- leafNodeContent(start.leaf)["delete"](start.path.slice().reverse()[0]);
325
- }
361
+ console.log(leafNodeContent(start.leaf).toJSON());
362
+ });
363
+ } // we merge leaves to avoid two subsequent leaves with the same marks
326
364
 
327
- newPath = leafNodeContent(start.leaf).toArray().indexOf(newChild);
328
- }
329
- }
330
365
 
331
366
  mergeLeafs(leafNodeContent(start.leaf));
332
- break; // return newPath;
367
+ break;
333
368
  }
334
369
 
335
370
  case "singlenode":
336
371
  {
337
- editor.doc.transact(function () {
338
- var formatedLeaf = formatAtEqualPath({
339
- format: format,
340
- value: value,
341
- start: start,
342
- end: end,
343
- length: length
372
+ var formatedLeaf = formatAtEqualPath({
373
+ format: format,
374
+ value: value,
375
+ start: start,
376
+ end: end,
377
+ length: length
378
+ });
379
+ mergeLeafs(leafNodeContent(start.leaf));
380
+ var isRemoved = formatedLeaf._item.deleted;
381
+
382
+ if (!isRemoved) {
383
+ setPosition(start.leaf.get("id"), {
384
+ offset: start.offset
344
385
  });
345
- mergeLeafs(leafNodeContent(start.leaf));
346
386
  setTimeout(function () {
347
- if (!formatedLeaf._item.deleted) {
348
- setPosition(formatedLeaf.get("id"), {
349
- offset: 0,
350
- end: leafLength(formatedLeaf)
351
- });
352
- } else {
353
- var remainingLeaf = formatedLeaf.parent.get(start.leafIndex - 1);
354
- var offset = leafText(remainingLeaf).toJSON().indexOf(selectedText);
355
- setPosition(remainingLeaf.get("id"), {
356
- offset: offset,
357
- end: offset + selectedText.length
358
- });
359
- }
387
+ setPosition(formatedLeaf.get("id"), {
388
+ offset: 0,
389
+ end: leafLength(formatedLeaf)
390
+ });
360
391
  });
361
- });
392
+ } else {
393
+ var remainingLeaf = formatedLeaf.parent.get(start.leafIndex - 1);
394
+ var offset = leafText(remainingLeaf).toJSON().indexOf(selectedText);
395
+ setTimeout(function () {
396
+ setPosition(remainingLeaf.get("id"), {
397
+ offset: offset,
398
+ end: offset + selectedText.length
399
+ });
400
+ });
401
+ }
402
+
362
403
  break;
363
404
  }
364
405
 
@@ -369,7 +410,7 @@ var formatText = function formatText(editor, mark) {
369
410
  var endPathString = end.path.join(",");
370
411
  var started = false;
371
412
  var ended = false;
372
- editor.doc.traverse(function (leaf, isText) {
413
+ traverse(editor, function (leaf, isText) {
373
414
  if (isText) {
374
415
  var path = getPath(leaf);
375
416
  var isStart = path.join(",") === startPathString;
@@ -405,8 +446,10 @@ var formatText = function formatText(editor, mark) {
405
446
 
406
447
  mergeLeafs(leafNodeContent(leaf));
407
448
  }
408
- } // { start: start.path[0], end: end.path[0] + 1 }
409
- );
449
+ }, {
450
+ start: start.path[0],
451
+ end: end.path[0] + 1
452
+ });
410
453
  break;
411
454
  }
412
455
  }
@@ -454,7 +497,7 @@ var insertText = function insertText(editor, _ref) {
454
497
  reap = _LeavesHarvest.reap,
455
498
  burn = _LeavesHarvest.burn;
456
499
 
457
- editor.doc.traverse(function (leaf, isText) {
500
+ traverse(editor, function (leaf, isText) {
458
501
  if (isText) {
459
502
  var path = getPath(leaf);
460
503
 
@@ -537,21 +580,19 @@ var removeEmptyText = function removeEmptyText(yText) {
537
580
  }
538
581
  };
539
582
 
540
- var splitLeaf = function splitLeaf(editor, _ref) {
541
- var _yText$parent;
542
-
543
- var yText = _ref.yText,
544
- at = _ref.at;
545
- var content = yText.toString();
546
- var rightText = content.substring(at.offset, yText.length);
547
- if (at.offset === yText.length) return;
548
- var newText = new Text(rightText);
549
- var newLeave = new Map$1();
550
- newLeave.set("text", newText);
551
- applyMarksFromParent(yText.parent, [newLeave]);
552
- var parent = yText == null ? void 0 : (_yText$parent = yText.parent) == null ? void 0 : _yText$parent.parent;
553
- yText["delete"](at.offset, yText.length);
554
- parent.insert(at.path.slice().reverse()[0] + 1, [newLeave]);
583
+ var splitLeaf = function splitLeaf(editor) {
584
+ var _editor$selection$sta = editor.selection.start,
585
+ leaf = _editor$selection$sta.leaf,
586
+ leafIndex = _editor$selection$sta.leafIndex,
587
+ offset = _editor$selection$sta.offset;
588
+ var content = leafString(leaf);
589
+ var rightText = content.substring(offset, content.length);
590
+ if (offset === content.length) return;
591
+ deleteLeafText(leaf, offset, rightText.length);
592
+ leafNodeContent(leaf).insert(leafIndex + 1, [createLeaf$1(_extends({}, leaf.toJSON(), {
593
+ text: rightText,
594
+ id: undefined
595
+ }))]);
555
596
  };
556
597
 
557
598
  var splitNode = function splitNode(editor) {
@@ -573,28 +614,27 @@ var splitNode = function splitNode(editor) {
573
614
  var rightText = leafString(start.leaf).substring(start == null ? void 0 : start.offset, leafLength(start.leaf));
574
615
  deleteLeafText(start.leaf, start == null ? void 0 : start.offset, rightText.length, false);
575
616
  var newParent = hasChildren(node) ? getNodeChildren(node) : node.parent;
576
- var newLeaf = new YLeaf(_extends({}, leaf.toJSON(), {
617
+ var newLeaf = createLeaf$1(_extends({}, leaf.toJSON(), {
577
618
  id: undefined,
578
619
  text: rightText
579
620
  }));
580
- newParent.insert(hasChildren(node) ? 0 : indexOfNode + 1, [new YNode(editor.defaultBlock, {
621
+ newParent.insert(hasChildren(node) ? 0 : indexOfNode + 1, [createNode(editor.defaultBlock, {
581
622
  children: [],
582
623
  content: [newLeaf].concat(nextLeaves.map(function (leaf) {
583
- return new YLeaf(leaf.toJSON());
624
+ return createLeaf$1(leaf.toJSON());
584
625
  }))
585
626
  })]);
627
+ setPosition(newLeaf.get("id"), {
628
+ offset: 0
629
+ });
586
630
  leafContent["delete"](indexOfLeaf + 1, leafNodeContentLength(start.leaf) - indexOfLeaf - 1);
587
- setTimeout(function () {
588
- setPosition(newLeaf.get("id"), {
589
- offset: 0
590
- });
591
- }, 10);
592
631
  };
593
632
 
594
633
  var doSplit = (!edges.startNode || !edges.endNode) && type !== "multinodes";
595
634
 
596
635
  if ((edges.endNode || edges.startNode) && type === "collapsed") {
597
- var newNode = new YNode(editor.defaultBlock);
636
+ // if cursor is at edge node with insert a default block before of after the current node
637
+ var newNode = createNode(editor.defaultBlock);
598
638
  node.parent.insert(indexOfNode + (edges.endNode ? 1 : 0), [newNode]);
599
639
  return setTimeout(function () {
600
640
  setPosition(newNode.get("content").get(0).get("id"), {
@@ -643,14 +683,14 @@ var nestNode = function nestNode(editor) {
643
683
  end = _editor$selection.end,
644
684
  setPosition = _editor$selection.setPosition;
645
685
  var startNode = getNode(start.leaf);
646
- var startJsonNode = startNode.toJSON();
686
+ var nodeCopy = copyNode(startNode);
647
687
  var startPath = getPath(startNode);
648
688
 
649
689
  var _startPath$slice$reve = startPath.slice().reverse(),
650
690
  index = _startPath$slice$reve[0],
651
691
  newPath = _startPath$slice$reve.slice(1);
652
692
 
653
- var prevNode = editor.doc.getNodeAtPath([].concat(newPath, [index - 1]));
693
+ var prevNode = getNodeAtPath(editor, [].concat(newPath, [index - 1]));
654
694
  var startContainer = getNodeContainer(startNode);
655
695
 
656
696
  switch (type) {
@@ -661,10 +701,7 @@ var nestNode = function nestNode(editor) {
661
701
  var id = start.leaf.get("id");
662
702
  var prevChildren = prevNode.get("children");
663
703
  startContainer["delete"](index);
664
- prevChildren.insert(prevChildren.length, [new YNode(startJsonNode.type, _extends({}, startJsonNode, {
665
- children: startJsonNode.children.map(getChildren),
666
- content: startJsonNode.content.map(getContent)
667
- }))]);
704
+ prevChildren.insert(prevChildren.length, [nodeCopy]);
668
705
  setPosition(id, {
669
706
  offset: start.offset
670
707
  });
@@ -681,18 +718,14 @@ var nestNode = function nestNode(editor) {
681
718
 
682
719
  var startPathString = start.path.slice(0, start.path.length - 1).join("");
683
720
  var endPathString = end.path.slice(0, start.path.length - 1).join("");
684
- editor.doc.traverse(function (node, isText) {
721
+ traverse(editor, function (node, isText) {
685
722
  if (!isText) {
686
723
  var path = getPath(node).join("");
687
724
 
688
725
  if (path.length === startPath.length) {
689
726
  if (path <= endPathString && path >= startPathString) {
690
727
  length++;
691
- var jsonNode = node.toJSON();
692
- newNodes.push(new YNode(jsonNode.type, _extends({}, jsonNode, {
693
- children: jsonNode.children.map(getChildren),
694
- content: jsonNode.content.map(getContent)
695
- })));
728
+ newNodes.push(copyNode(node));
696
729
  }
697
730
  }
698
731
  }
@@ -731,199 +764,12 @@ var moveNode = function moveNode(_ref) {
731
764
  return nodesCopies[0].get("content").get(0).get("id");
732
765
  };
733
766
 
734
- var _excluded = ["type", "content", "children"];
735
- var getContent = function getContent(leaf) {
736
- return new YLeaf(leaf);
737
- };
738
- var getChildren = function getChildren(_ref) {
739
- var type = _ref.type,
740
- _ref$content = _ref.content,
741
- content = _ref$content === void 0 ? [] : _ref$content,
742
- _ref$children = _ref.children,
743
- children = _ref$children === void 0 ? [] : _ref$children,
744
- props = _objectWithoutPropertiesLoose(_ref, _excluded);
745
-
746
- return new YNode(type, _extends({}, props, {
747
- children: children.map(getChildren),
748
- content: content.map(getContent)
749
- }));
750
- };
751
- var EdytorDoc = /*#__PURE__*/function (_Doc) {
752
- _inheritsLoose(EdytorDoc, _Doc);
753
-
754
- function EdytorDoc(value) {
755
- var _this;
756
-
757
- _this = _Doc.call(this) || this;
758
-
759
- _this.getLeafAtPath = function (_ref2) {
760
- var start = _ref2[0],
761
- path = _ref2.slice(1);
762
-
763
- var node = _this.children.get(start);
764
-
765
- var i = 0;
766
-
767
- while (i < path.length - 1) {
768
- var index = path[i];
769
- i++;
770
- node = node.get("children").get(index);
771
- }
772
-
773
- return node && node.get("content").get(path.slice().reverse()[0]);
774
- };
775
-
776
- _this.getNodeAtPath = function (_ref3) {
777
- var start = _ref3[0],
778
- path = _ref3.slice(1);
779
-
780
- var node = _this.children.get(start);
781
-
782
- for (var i = 0; i < path.length; i++) {
783
- var index = path[i];
784
-
785
- if (node.get) {
786
- node = node.get("children").get(index);
787
- } else {
788
- node = undefined;
789
- }
790
- }
791
-
792
- return node;
793
- };
794
-
795
- _this.getContainerAtPath = function (path) {
796
- if (path.length === 1) {
797
- return _this.children;
798
- } else {
799
- return _this.getNodeAtPath(path.slice(1, path.length)).get("children");
800
- }
801
- };
802
-
803
- _this.traverse = function (cb, opts) {
804
- var traverse = function traverse(node, path) {
805
- if (node.has("text")) {
806
- cb(node, true, path);
807
- } else {
808
- var _array = getNodeContent(node).toArray() //@ts-ignore
809
- .concat(getNodeChildren(node).toArray());
810
-
811
- cb(node, false, path);
812
-
813
- for (var i = 0; i < _array.length; i++) {
814
- traverse(_array[i], [].concat(path.slice(0, path.length - 1), [0, i]));
815
- }
816
- }
817
- };
818
-
819
- var array = _this.children.toArray().slice((opts == null ? void 0 : opts.start) || 0, (opts == null ? void 0 : opts.end) || _this.children.length);
820
-
821
- for (var i = 0; i < array.length; i++) {
822
- traverse(array[i], [i + ((opts == null ? void 0 : opts.start) || 0)]);
823
- }
824
- };
825
-
826
- _this.toJSON = function () {
827
- return {
828
- children: _this.children.toJSON(),
829
- config: _this.config.toJSON()
830
- };
831
- };
832
-
833
- _this.toUpdate = function () {
834
- return encodeStateAsUpdateV2(_assertThisInitialized(_this));
835
- };
836
-
837
- _this.string = function () {
838
- var t = "";
839
-
840
- var array = _this.children.toArray();
841
-
842
- for (var i = 0; i < array.length; i++) {
843
- t += array[i].string();
844
- }
845
-
846
- return t;
847
- };
848
-
849
- if (value) {
850
- var array = _this.getArray("children");
851
-
852
- array.insert(0, value.map(getChildren));
853
- }
854
-
855
- _this.config = _this.getMap("config");
856
- _this.children = _this.getArray("children");
857
- return _this;
858
- }
859
-
860
- return EdytorDoc;
861
- }(Doc);
862
-
863
- var YNode = /*#__PURE__*/function (_Map) {
864
- _inheritsLoose(YNode, _Map);
865
-
866
- function YNode(type, props) {
867
- var _this;
868
-
869
- _this = _Map.call(this) || this;
870
-
871
- _this.set("id", (props == null ? void 0 : props.id) || nanoid());
872
-
873
- _this.set("type", type);
874
-
875
- if (props != null && props.data) {
876
- _this.set("data", new Map$1(Object.entries(props.data)));
877
- }
878
-
879
- _this.set("content", (props == null ? void 0 : props.content) instanceof Array$1 ? props.content : Array$1.from((props == null ? void 0 : props.content) || [new YLeaf()]));
880
-
881
- _this.set("children", (props == null ? void 0 : props.children) instanceof Array$1 ? props.children : Array$1.from((props == null ? void 0 : props.children) || []));
882
-
883
- return _this;
884
- }
885
-
886
- return YNode;
887
- }(Map$1);
888
-
889
- var _excluded$1 = ["id", "text", "data"];
890
- var YLeaf = /*#__PURE__*/function (_Map) {
891
- _inheritsLoose(YLeaf, _Map);
892
-
893
- function YLeaf(props) {
894
- var _this;
895
-
896
- _this = _Map.call(this) || this;
897
-
898
- _this.set("id", (props == null ? void 0 : props.id) || nanoid());
899
-
900
- _this.set("text", new Text((props == null ? void 0 : props.text) || ""));
901
-
902
- if (props) {
903
- var text = props.text,
904
- data = props.data,
905
- marks = _objectWithoutPropertiesLoose(props, _excluded$1);
906
-
907
- _this.set("text", new Text(text || ""));
908
-
909
- data && _this.set("data", new Map$1(Object.entries(data)));
910
- marks && Object.keys(marks).forEach(function (mark) {
911
- _this.set(mark, marks[mark]);
912
- });
913
- }
914
-
915
- return _this;
916
- }
917
-
918
- return YLeaf;
919
- }(Map$1);
920
-
921
767
  var _excluded$2 = ["type"];
922
768
  var mergeContentWithPrevLeaf = function mergeContentWithPrevLeaf(editor) {
923
769
  var start = editor.selection.start;
924
770
  var prevLeaf;
925
771
  var stop = false;
926
- editor.doc.traverse(function (node, isText) {
772
+ traverse(editor, function (node, isText) {
927
773
  if (isText) {
928
774
  if (node === start.leaf) {
929
775
  stop = true;
@@ -933,17 +779,26 @@ var mergeContentWithPrevLeaf = function mergeContentWithPrevLeaf(editor) {
933
779
  }
934
780
  });
935
781
  if (!prevLeaf) return;
782
+ var prevLeafLength = leafLength(prevLeaf);
936
783
  leafNodeContent(prevLeaf).insert(leafNodeContentLength(prevLeaf), leafNodeContent(start.leaf).toArray().map(function (leaf) {
937
- return new YLeaf(leaf.toJSON());
784
+ return createLeaf$1(leaf.toJSON());
938
785
  }));
939
786
  mergeLeafs(leafNodeContent(prevLeaf));
940
- start.leaf.parent.parent["delete"](getIndex(getNode(start.leaf)));
787
+ var children = start.node.get("children").toArray().map(copyNode);
788
+
789
+ if (children.length) {
790
+ start.node.parent.insert(start.nodeIndex, children);
791
+ }
792
+
793
+ start.node.parent["delete"](start.nodeIndex + children.length);
794
+ return [prevLeaf, prevLeafLength]; // (start.leaf.parent.parent as YArray<YNode>).delete(index);
941
795
  };
942
796
  var mergeContentWithNextLeaf = function mergeContentWithNextLeaf(editor) {
943
- var start = editor.selection.start;
797
+ var start = editor.selection.start; // 1 .get the next leaf
798
+
944
799
  var nextLeaf;
945
800
  var stop = false;
946
- editor.doc.traverse(function (node, isText) {
801
+ traverse(editor, function (node, isText) {
947
802
  if (isText) {
948
803
  if (node === start.leaf) {
949
804
  stop = true;
@@ -952,24 +807,21 @@ var mergeContentWithNextLeaf = function mergeContentWithNextLeaf(editor) {
952
807
  stop = false;
953
808
  }
954
809
  }
955
- }); //TODO the children should be pull to the depth level of the removed parent
810
+ }); // end of document do nothing
811
+
812
+ if (!nextLeaf) return; // 2 .merge the next leaf with the start leaf
956
813
 
957
- console.log(leafNodeContent(nextLeaf).toArray());
958
814
  leafNodeContent(start.leaf).insert(leafNodeContentLength(start.leaf), leafNodeContent(nextLeaf).toArray().map(function (leaf) {
959
- return new YLeaf(leaf.toJSON());
815
+ return createLeaf$1(leaf.toJSON());
960
816
  }));
961
- mergeLeafs(leafNodeContent(start.leaf));
962
- console.log(start.leaf.parent.toJSON());
963
- var node = getNode(nextLeaf);
964
-
965
- if (hasChildren(node)) {
966
- console.log(node.get("children").toJSON());
967
- node.parent.insert(getIndex(node), node.get("children").toJSON().map(function (node) {
968
- return new YNode(node.type, node);
969
- }));
970
- }
817
+ mergeLeafs(leafNodeContent(start.leaf)); // 3 get the merged node children if any and place it where the merged node was
971
818
 
972
- node.parent["delete"](getIndex(node));
819
+ var node = getNode(nextLeaf);
820
+ var index = getIndex(node);
821
+ var parent = node.parent;
822
+ var children = node.get("children").toArray();
823
+ children.length > 0 && parent.insert(index, children.map(copyNode));
824
+ parent["delete"](index + children.length);
973
825
  return nextLeaf;
974
826
  };
975
827
  var deleteNode = function deleteNode(node, defaultBlock) {
@@ -983,13 +835,13 @@ var deleteNode = function deleteNode(node, defaultBlock) {
983
835
  var type = _ref.type,
984
836
  props = _objectWithoutPropertiesLoose(_ref, _excluded$2);
985
837
 
986
- return new YNode(type, props);
838
+ return createNode(type, props);
987
839
  }));
988
840
  }
989
841
 
990
- if (node.parent === node.doc.children && node.doc.children.length === 0) {
991
- node.parent.insert(0, [new YNode(defaultBlock, {
992
- content: [new YLeaf()]
842
+ if (node.parent === (node.doc.getArray("children") && node.doc.getArray("children").length === 0)) {
843
+ node.parent.insert(0, [createNode(defaultBlock, {
844
+ content: [createLeaf$1()]
993
845
  })]);
994
846
  }
995
847
  };
@@ -1013,11 +865,140 @@ var isNodeContentEmpty = function isNodeContentEmpty(node) {
1013
865
  };
1014
866
  var copyNode = function copyNode(node) {
1015
867
  var jsonNode = node.toJSON();
1016
- return new YNode(jsonNode.type, _extends({}, jsonNode, {
868
+ return createNode(jsonNode.type, _extends({}, jsonNode, {
1017
869
  children: jsonNode.children.map(getChildren),
1018
- content: jsonNode.content.map(getContent)
870
+ content: jsonNode.content.map(createLeaf$1)
1019
871
  }));
1020
872
  };
873
+ var nodeString = function nodeString(node) {
874
+ var text = "";
875
+ getNodeContent(node).forEach(function (leaf) {
876
+ text += leafString(leaf);
877
+ });
878
+ getNodeChildren(node).forEach(function (node) {
879
+ text += nodeString(node);
880
+ });
881
+ return text;
882
+ };
883
+
884
+ var nanoid = function nanoid() {
885
+ return "y-" + customAlphabet("346789ABCDEFGHJKLMNPQRTUV-WXYabcdefghijkmnpqrtwxyz", 20)();
886
+ };
887
+ var getNode = function getNode(node) {
888
+ var _node$parent;
889
+
890
+ return node == null ? void 0 : (_node$parent = node.parent) == null ? void 0 : _node$parent.parent;
891
+ };
892
+ var getIndex = function getIndex(node) {
893
+ var index = 0;
894
+ var stop = false;
895
+ var n = node.parent._start;
896
+
897
+ while (n !== null && !stop) {
898
+ if (n.countable && !n.deleted) {
899
+ var c = n.content.getContent();
900
+
901
+ for (var i = 0; i < c.length; i++) {
902
+ if (c[i] === node) {
903
+ stop = true;
904
+ break;
905
+ }
906
+
907
+ index++;
908
+ }
909
+ }
910
+
911
+ n = n.right;
912
+ }
913
+
914
+ return index;
915
+ };
916
+ var getPath = function getPath(node) {
917
+ var p = [];
918
+ var n = node;
919
+
920
+ while (!!n) {
921
+ p.push(getIndex(n));
922
+ n = getNode(n);
923
+ }
924
+
925
+ return p.reverse();
926
+ };
927
+ var getId = function getId(node) {
928
+ return node.get("id");
929
+ };
930
+ var getLeafAtPath = function getLeafAtPath(editor, _ref) {
931
+ var start = _ref[0],
932
+ path = _ref.slice(1);
933
+
934
+ var node = editor.children.get(start);
935
+ var i = 0;
936
+
937
+ while (i < path.length - 1) {
938
+ var index = path[i];
939
+ i++;
940
+ node = node.get("children").get(index);
941
+ }
942
+
943
+ return node && node.get("content").get(path.slice().reverse()[0]);
944
+ };
945
+ var getNodeAtPath = function getNodeAtPath(editor, _ref2) {
946
+ var start = _ref2[0],
947
+ path = _ref2.slice(1);
948
+
949
+ var node = editor.children.get(start);
950
+
951
+ for (var i = 0; i < path.length; i++) {
952
+ var index = path[i];
953
+
954
+ if (node.get) {
955
+ node = node.get("children").get(index);
956
+ } else {
957
+ node = undefined;
958
+ }
959
+ }
960
+
961
+ return node;
962
+ };
963
+ var getContainerAtPath = function getContainerAtPath(editor, path) {
964
+ if (path.length === 1) {
965
+ return editor.children;
966
+ } else {
967
+ return getNodeAtPath(editor, path.slice(1, path.length)).get("children");
968
+ }
969
+ };
970
+ var traverse = function traverse(editor, cb, opts) {
971
+ var t = function t(node, path) {
972
+ if (node.has("text")) {
973
+ cb(node, true, path);
974
+ } else {
975
+ var _array = getNodeContent(node).toArray() //@ts-ignore
976
+ .concat(getNodeChildren(node).toArray());
977
+
978
+ cb(node, false, path);
979
+
980
+ for (var i = 0; i < _array.length; i++) {
981
+ t(_array[i], [].concat(path.slice(0, path.length - 1), [0, i]));
982
+ }
983
+ }
984
+ };
985
+
986
+ var array = editor.children.toArray().slice((opts == null ? void 0 : opts.start) || 0, (opts == null ? void 0 : opts.end) || editor.children.length);
987
+
988
+ for (var i = 0; i < array.length; i++) {
989
+ t(array[i], [i + ((opts == null ? void 0 : opts.start) || 0)]);
990
+ }
991
+ };
992
+ var toString = function toString(editor) {
993
+ var t = "";
994
+ var array = editor.children.toArray();
995
+
996
+ for (var i = 0; i < array.length; i++) {
997
+ t += nodeString(array[i]);
998
+ }
999
+
1000
+ return t;
1001
+ };
1021
1002
 
1022
1003
  var LeavesHarvest = function LeavesHarvest() {
1023
1004
  var _this = this;
@@ -1051,7 +1032,7 @@ var LeavesHarvest = function LeavesHarvest() {
1051
1032
  content["delete"](indexes[0], indexes.length);
1052
1033
 
1053
1034
  if (content.length === 0 && !shouldDeleteNode) {
1054
- content.insert(0, [new YLeaf()]);
1035
+ content.insert(0, [createLeaf()]);
1055
1036
  } else if (shouldDeleteNode) return deleteNode(node, editor == null ? void 0 : editor.defaultBlock);
1056
1037
  });
1057
1038
  };
@@ -1101,6 +1082,7 @@ var setLeafData = function setLeafData(leaf, data) {
1101
1082
  };
1102
1083
  var removeIfEmpty = function removeIfEmpty(leaf) {
1103
1084
  if (isLeafEmpty(leaf)) {
1085
+ console.log("isEmpty", leafNodeContent(leaf).toJSON());
1104
1086
  leafNodeContent(leaf)["delete"](getIndex(leaf)); // if (leafNodeContentLength(leaf) === 0) {
1105
1087
  // deleteNode(leafNode(leaf));
1106
1088
  // }
@@ -1130,336 +1112,23 @@ var leafNodeChildrenLength = function leafNodeChildrenLength(leaf) {
1130
1112
  return leafNodeChildren(leaf).length;
1131
1113
  };
1132
1114
 
1133
- var getLeaf = function getLeaf(editor, anchorNode) {
1134
- var leaf;
1135
- var node = anchorNode;
1136
- var i = 0;
1137
-
1138
- while (!leaf && ((_node = node) == null ? void 0 : _node.id) !== editor.editorId && node && i < 100) {
1139
- var _node;
1140
-
1141
- leaf = editor.ID_TO_NODE.get(node.id);
1142
- node = node.parentElement;
1143
- i++;
1144
- }
1145
-
1146
- if (!leaf) return undefined;
1147
- return [leaf, getPath(leaf)];
1148
- };
1149
-
1150
- var getRange = function getRange(editor, selection) {
1151
- var _range$toString;
1152
-
1153
- var anchorNode = selection.anchorNode,
1154
- focusNode = selection.focusNode,
1155
- anchorOffset = selection.anchorOffset,
1156
- focusOffset = selection.focusOffset,
1157
- isCollapsed = selection.isCollapsed,
1158
- rangeCount = selection.rangeCount;
1159
- if (focusNode === null) return;
1160
-
1161
- var _getLeaf = getLeaf(editor, anchorNode),
1162
- leaf1 = _getLeaf[0],
1163
- path1 = _getLeaf[1];
1164
-
1165
- var _getLeaf2 = getLeaf(editor, focusNode),
1166
- leaf2 = _getLeaf2[0],
1167
- path2 = _getLeaf2[1];
1168
-
1169
- if (!leaf1) return;
1170
- var equalPaths = path1.join("") === path2.join("");
1171
- var isFollowing = equalPaths ? anchorOffset < focusOffset : path1.join() < path2.join(); // console.log(isFollowing, equalPaths, { path1, path2 });
1172
-
1173
- var start = {
1174
- node: isFollowing ? anchorNode : focusNode,
1175
- path: isFollowing ? path1 : path2,
1176
- ancestor: document.querySelector("[data-edytor-path=\"" + (isFollowing ? path1 : path2).slice(0, 1) + "\"]"),
1177
- offset: isFollowing ? anchorOffset : focusOffset,
1178
- leaf: isFollowing ? leaf1 : leaf2
1179
- };
1180
- var end = isCollapsed ? start : {
1181
- node: !isFollowing ? anchorNode : focusNode,
1182
- path: !isFollowing ? path1 : path2,
1183
- offset: !isFollowing ? anchorOffset : focusOffset,
1184
- leaf: !isFollowing ? leaf1 : leaf2
1185
- };
1186
- var range = rangeCount >= 1 ? selection == null ? void 0 : selection.getRangeAt(0) : undefined; //@ts-ignore
1187
-
1188
- var edytorSelection = {
1189
- start: start,
1190
- end: end,
1191
- arePathsEquals: equalPaths,
1192
- length: range && ((_range$toString = range.toString()) == null ? void 0 : _range$toString.length),
1193
- range: range,
1194
- boundingRect: range && range.getBoundingClientRect(),
1195
- selection: selection,
1196
- edges: {
1197
- start: start.offset === 0,
1198
- end: end.offset === leafLength(end.leaf)
1199
- },
1200
- type: isCollapsed ? "collapsed" : equalPaths ? "singlenode" : "multinodes",
1201
- editorOffset: editor.cursor().getEditorOffset(selection)
1202
- };
1203
- console.log({
1204
- edytorSelection: edytorSelection
1205
- });
1206
- return edytorSelection;
1207
- };
1208
-
1209
- var Cursor = /*#__PURE__*/function () {
1210
- function Cursor(_ref) {
1211
- var _this = this;
1212
-
1213
- var container = _ref.container,
1214
- selection = _ref.selection;
1215
-
1216
- this.getEditorOffset = function (selection) {
1217
- var focusNode = selection.focusNode,
1218
- focusOffset = selection.focusOffset;
1219
- var charCount = -1,
1220
- node;
1221
-
1222
- if (!focusNode) {
1223
- var _selection = window.getSelection();
1224
-
1225
- focusNode = _selection.focusNode;
1226
- focusOffset = _selection == null ? void 0 : _selection.focusOffset;
1227
- }
1228
-
1229
- if (focusNode) {
1230
- if (Cursor._isChildOf(focusNode, _this.container)) {
1231
- node = focusNode;
1232
- charCount = focusOffset;
1233
-
1234
- while (node) {
1235
- if (node === _this.container) {
1236
- break;
1237
- }
1238
-
1239
- if (node.previousSibling) {
1240
- node = node.previousSibling;
1241
- charCount += node.textContent.length;
1242
- } else {
1243
- node = node.parentNode;
1244
-
1245
- if (node === null) {
1246
- break;
1247
- }
1248
- }
1249
- }
1250
- }
1251
- }
1252
-
1253
- return charCount;
1254
- };
1255
-
1256
- this.set = function (offset) {
1257
- Cursor.setCurrentCursorPosition(offset, _this.container, _this.selection.selection);
1258
- };
1259
-
1260
- this.setRange = function (offset, length) {
1261
- var _this$selection = _this.selection,
1262
- selection = _this$selection.selection;
1263
-
1264
- var _this$getRangeBoundar = _this.getRangeBoundaries(offset, length),
1265
- startNode = _this$getRangeBoundar.startNode,
1266
- startOffset = _this$getRangeBoundar.startOffset,
1267
- endNode = _this$getRangeBoundar.endNode,
1268
- endOffset = _this$getRangeBoundar.endOffset;
1269
-
1270
- selection.removeAllRanges();
1271
- var R = document.createRange();
1272
- R.selectNode(_this.container);
1273
- R.setStart(startNode, startOffset);
1274
- R.setEnd(endNode, endOffset);
1275
- selection.addRange(R);
1276
- };
1277
-
1278
- this.getRangeBoundaries = function (offset, length) {
1279
- var _this$getRangeBoundar2 = _this.getRangeBoundary(_this.container, {
1280
- count: offset - length
1281
- }, true),
1282
- startNode = _this$getRangeBoundar2[0],
1283
- startOffset = _this$getRangeBoundar2[1];
1284
-
1285
- var _this$getRangeBoundar3 = _this.getRangeBoundary(_this.container, {
1286
- count: offset
1287
- }, false),
1288
- endNode = _this$getRangeBoundar3[0],
1289
- endOffset = _this$getRangeBoundar3[1];
1290
-
1291
- if (startNode.length === startOffset) {
1292
- var getNextNode = function getNextNode(node) {
1293
- var childNodes = Array.from(node.parentElement.childNodes);
1294
- var index = childNodes.indexOf(node);
1295
- console.log({
1296
- index: index,
1297
- childNodes: childNodes,
1298
- next: childNodes[index + 1]
1299
- });
1300
- };
1301
-
1302
- getNextNode(startNode);
1303
- }
1304
-
1305
- return {
1306
- startNode: startNode,
1307
- startOffset: startOffset,
1308
- endNode: endNode,
1309
- endOffset: endOffset
1310
- };
1311
- };
1312
-
1313
- this.getRangeBoundary = function (node, chars, isStart) {
1314
- var result;
1315
-
1316
- if (chars.count === 0) {
1317
- result = [node, chars.count];
1318
- } else if (node && chars.count > 0) {
1319
- if (node.nodeType === Node.TEXT_NODE) {
1320
- if (node.textContent.length < chars.count) {
1321
- chars.count -= node.textContent.length;
1322
- } else {
1323
- result = [node, chars.count];
1324
- chars.count = 0;
1325
- }
1326
- } else {
1327
- for (var lp = 0; lp < node.childNodes.length; lp++) {
1328
- result = _this.getRangeBoundary(node.childNodes[lp], chars, isStart);
1329
-
1330
- if (chars.count === 0) {
1331
- break;
1332
- }
1333
- }
1334
- }
1335
- }
1336
-
1337
- return result;
1338
- };
1339
-
1340
- this.container = container;
1341
- this.selection = selection;
1342
- }
1343
-
1344
- Cursor.getCurrentCursorPosition = function getCurrentCursorPosition(parentElement, focusNode, focusOffset) {
1345
- var charCount = -1,
1346
- node;
1347
-
1348
- if (!focusNode) {
1349
- var selection = window.getSelection();
1350
- focusNode = selection.focusNode;
1351
- focusOffset = selection == null ? void 0 : selection.focusOffset;
1352
- }
1353
-
1354
- if (focusNode) {
1355
- if (Cursor._isChildOf(focusNode, parentElement)) {
1356
- node = focusNode;
1357
- charCount = focusOffset;
1358
-
1359
- while (node) {
1360
- if (node === parentElement) {
1361
- break;
1362
- }
1363
-
1364
- if (node.previousSibling) {
1365
- node = node.previousSibling;
1366
- charCount += node.textContent.length;
1367
- } else {
1368
- node = node.parentNode;
1369
-
1370
- if (node === null) {
1371
- break;
1372
- }
1373
- }
1374
- }
1375
- }
1376
- }
1377
-
1378
- return charCount;
1379
- };
1380
-
1381
- Cursor.setCurrentCursorPosition = function setCurrentCursorPosition(chars, element, s) {
1382
- if (chars >= 0) {
1383
- var selection = s || window.getSelection();
1384
-
1385
- var range = Cursor._createRange(element, {
1386
- count: chars
1387
- });
1388
-
1389
- if (range) {
1390
- range.collapse(false);
1391
- selection.removeAllRanges();
1392
- selection.addRange(range);
1393
- }
1394
- }
1395
- };
1396
-
1397
- Cursor.selectNode = function selectNode(node, startOffset, endOffset, selection) {
1398
- if (!selection) selection = window.getSelection();
1399
- var textNode = node;
1400
-
1401
- while (((_textNode = textNode) == null ? void 0 : _textNode.nodeType) !== 3 && (_textNode2 = textNode) != null && _textNode2.firstChild) {
1402
- var _textNode, _textNode2, _textNode3;
1403
-
1404
- textNode = (_textNode3 = textNode) == null ? void 0 : _textNode3.firstChild;
1405
- }
1406
-
1407
- selection.removeAllRanges();
1408
- var R = document.createRange();
1409
- R.setStart(textNode, startOffset);
1410
- R.setEnd(textNode, endOffset);
1411
- selection.addRange(R);
1412
- };
1413
-
1414
- Cursor._createRange = function _createRange(node, chars, range) {
1415
- if (!range) {
1416
- range = document.createRange();
1417
- range.selectNode(node);
1418
- range.setStart(node, 0);
1419
- }
1420
-
1421
- if (chars.count === 0) {
1422
- range.setEnd(node, chars.count);
1423
- } else if (node && chars.count > 0) {
1424
- if (node.nodeType === Node.TEXT_NODE) {
1425
- if (node.textContent.length < chars.count) {
1426
- chars.count -= node.textContent.length;
1427
- } else {
1428
- range.setEnd(node, chars.count);
1429
- chars.count = 0;
1430
- }
1431
- } else {
1432
- for (var lp = 0; lp < node.childNodes.length; lp++) {
1433
- range = Cursor._createRange(node.childNodes[lp], chars, range);
1434
-
1435
- if (chars.count === 0) {
1436
- break;
1437
- }
1438
- }
1439
- }
1440
- }
1441
-
1442
- return range;
1443
- };
1444
-
1445
- Cursor._isChildOf = function _isChildOf(node, parentElement) {
1446
- while (node !== null) {
1447
- if (node === parentElement) return true;
1448
- node = node.parentNode;
1449
- }
1115
+ var Dropper = function Dropper() {
1116
+ var _this = this;
1450
1117
 
1451
- return false;
1118
+ this.init = function (editor, container) {
1119
+ _this.editor = editor;
1120
+ _this.doc = editor.doc;
1121
+ _this.container = container;
1122
+ _this.editorId = editor.editorId;
1123
+ _this.selection = editor.selection;
1124
+ _this.ID_TO_NODE = editor.ID_TO_NODE;
1125
+ _this.allowNesting = editor.allowNesting;
1452
1126
  };
1453
1127
 
1454
- return Cursor;
1455
- }();
1456
-
1457
- var Dropper = function Dropper(doc, editorId, ID_TO_NODE, selection, allowNesting) {
1458
- var _this = this;
1459
-
1460
1128
  this.startDrag = function (node, e) {
1461
1129
  var _e$dataTransfer;
1462
1130
 
1131
+ if (!_this.editor.dnd.active) return;
1463
1132
  _this.node = node;
1464
1133
  var dndIndicator = document.getElementById("dndIndicator");
1465
1134
  dndIndicator.style.opacity = "1";
@@ -1522,6 +1191,8 @@ var Dropper = function Dropper(doc, editorId, ID_TO_NODE, selection, allowNestin
1522
1191
  };
1523
1192
 
1524
1193
  this.onDrag = function (e) {
1194
+ if (!_this.editor.dnd.active) return;
1195
+
1525
1196
  var _this$getHoveredNode = _this.getHoveredNode(e.target),
1526
1197
  hoveredElement = _this$getHoveredNode.hoveredElement,
1527
1198
  hoveredNode = _this$getHoveredNode.hoveredNode;
@@ -1550,23 +1221,18 @@ var Dropper = function Dropper(doc, editorId, ID_TO_NODE, selection, allowNestin
1550
1221
  index = index + (isOnTop ? 0 : 1);
1551
1222
  if (index === -1) index = 0;
1552
1223
  _this.to = {
1553
- container: isNested ? hoveredNode.get("children") : path.length === 1 ? _this.doc.children : hoveredNode.parent,
1224
+ container: isNested ? hoveredNode.get("children") : path.length === 1 ? _this.editor.children : hoveredNode.parent,
1554
1225
  at: isNested ? 0 : index
1555
1226
  };
1556
1227
  };
1557
-
1558
- this.doc = doc;
1559
- this.editorId = editorId;
1560
- this.selection = selection;
1561
- this.ID_TO_NODE = ID_TO_NODE;
1562
- this.allowNesting = allowNesting;
1563
1228
  };
1564
1229
 
1565
- var EdytorSelection = function EdytorSelection(ID_TO_NODE, onSelectionChange) {
1230
+ var EdytorSelection = function EdytorSelection() {
1566
1231
  var _this = this;
1567
1232
 
1568
- this.init = function (container) {
1233
+ this.init = function (editor, container) {
1569
1234
  _this.container = container;
1235
+ _this.editor = editor;
1570
1236
  document.addEventListener("selectionchange", _this.onSelection);
1571
1237
 
1572
1238
  _this.container.addEventListener("focus", _this.onFocus);
@@ -1616,7 +1282,7 @@ var EdytorSelection = function EdytorSelection(ID_TO_NODE, onSelectionChange) {
1616
1282
  var node = anchorNode;
1617
1283
 
1618
1284
  while (!leaf && node !== _this.container && node) {
1619
- leaf = _this.ID_TO_NODE.get(node.id);
1285
+ leaf = _this.editor.ID_TO_NODE.get(node.id);
1620
1286
 
1621
1287
  if (!leaf) {
1622
1288
  node = node.parentElement;
@@ -1676,7 +1342,7 @@ var EdytorSelection = function EdytorSelection(ID_TO_NODE, onSelectionChange) {
1676
1342
  offset: isFollowing ? anchorOffset : focusOffset,
1677
1343
  leaf: isFollowing ? leaf1 : leaf2
1678
1344
  });
1679
- _this.end = isCollapsed ? _this.start : _extends({}, _this.getNodeBoundingRect(!isFollowing ? node1 : node2), {
1345
+ _this.end = isCollapsed || node2 === null ? _this.start : _extends({}, _this.getNodeBoundingRect(!isFollowing ? node1 : node2), {
1680
1346
  node: !isFollowing ? node1 : node2,
1681
1347
  leafId: (!isFollowing ? leaf1 : leaf2).get("id"),
1682
1348
  leafIndex: !isFollowing ? path1[path1.length - 1] : path2[path2.length - 1],
@@ -1691,11 +1357,15 @@ var EdytorSelection = function EdytorSelection(ID_TO_NODE, onSelectionChange) {
1691
1357
  _this.length = _this.selectedText.length;
1692
1358
  _this.range = range;
1693
1359
  _this.selection = selection;
1360
+ var startNode = _this.start.offset === 0 && getIndex(_this.start.leaf) === 0;
1361
+ var endNode = _this.end.offset === leafLength(_this.end.leaf) && getIndex(_this.start.leaf) === leafNodeContentLength(_this.end.leaf) - 1;
1694
1362
  _this.edges = {
1695
1363
  startLeaf: _this.start.offset === 0,
1696
- startNode: _this.start.offset === 0 && getIndex(_this.start.leaf) === 0,
1697
1364
  endLeaf: _this.end.offset === leafLength(_this.end.leaf),
1698
- endNode: _this.end.offset === leafLength(_this.end.leaf) && getIndex(_this.start.leaf) === leafNodeContentLength(_this.end.leaf) - 1
1365
+ startNode: startNode,
1366
+ endNode: endNode,
1367
+ startDocument: startNode && _this.start.path.join("") === "00",
1368
+ endDocument: false
1699
1369
  };
1700
1370
  _this.type = isCollapsed ? "collapsed" : equalPaths ? "singlenode" : _this.start.node === _this.end.node ? "multileaves" : "multinodes";
1701
1371
 
@@ -1703,13 +1373,18 @@ var EdytorSelection = function EdytorSelection(ID_TO_NODE, onSelectionChange) {
1703
1373
  };
1704
1374
 
1705
1375
  this.onChange = function () {
1706
- _this.onSelectionChange && _this.onSelectionChange(_this);
1376
+ _this.observers.forEach(function (o) {
1377
+ return o(_this);
1378
+ }); // console.log(this.editor.awareness);
1379
+ // if (this.collaborationPosition?.node !== this.start.node.get("id")) {
1380
+ // this.collaborationPosition = {
1381
+ // node: this.start.node.get("id"),
1382
+ // leaf: this.start.leaf.get("id"),
1383
+ // offset: this.start.offset
1384
+ // };
1385
+ // this.editor.awareness.setLocalStateField("position", this.collaborationPosition);
1386
+ // }
1707
1387
 
1708
- if (_this.observers) {
1709
- _this.observers.forEach(function (o) {
1710
- return o(_this);
1711
- });
1712
- }
1713
1388
  };
1714
1389
 
1715
1390
  this.setPosition = function (id, _ref) {
@@ -1721,6 +1396,10 @@ var EdytorSelection = function EdytorSelection(ID_TO_NODE, onSelectionChange) {
1721
1396
 
1722
1397
  var node = _this.container.querySelector("#" + id);
1723
1398
 
1399
+ console.log({
1400
+ node: node
1401
+ });
1402
+
1724
1403
  while (node && node.nodeType !== 3 && (_node = node) != null && _node.firstChild) {
1725
1404
  var _node;
1726
1405
 
@@ -1751,8 +1430,7 @@ var EdytorSelection = function EdytorSelection(ID_TO_NODE, onSelectionChange) {
1751
1430
  });
1752
1431
  };
1753
1432
 
1754
- this.onSelectionChange = onSelectionChange;
1755
- this.ID_TO_NODE = ID_TO_NODE;
1433
+ this.observers = [];
1756
1434
  };
1757
1435
 
1758
1436
  var prevent = function prevent(e) {
@@ -1765,13 +1443,21 @@ var getDataTransfer = function getDataTransfer(e) {
1765
1443
  };
1766
1444
 
1767
1445
  var onBeforeInput = function onBeforeInput(_ref, e) {
1768
- var doc = _ref[0],
1769
- onChange = _ref[1],
1446
+ var _e$target;
1447
+
1448
+ var onChange = _ref[1],
1770
1449
  editor = _ref[2];
1771
- prevent(e); // console.log(e);
1450
+ // console.log(e);
1772
1451
  var _editor$selection = editor.selection,
1773
1452
  start = _editor$selection.start,
1774
- length = _editor$selection.length;
1453
+ length = _editor$selection.length,
1454
+ setPosition = _editor$selection.setPosition; // if (e.composedPath().some((el) => el.getAttribute && el.getAttribute("data-edytor-void") === "true")) {
1455
+ // return;
1456
+ // }
1457
+
1458
+ var tag = (_e$target = e.target) == null ? void 0 : _e$target.tagName;
1459
+ if (tag === "TEXTAREA" || tag === "INPUT") return;
1460
+ prevent(e);
1775
1461
 
1776
1462
  switch (e.inputType) {
1777
1463
  case "insertFromPaste":
@@ -1868,8 +1554,9 @@ var onBeforeInput = function onBeforeInput(_ref, e) {
1868
1554
 
1869
1555
  case "insertReplacementText":
1870
1556
  {
1871
- doc().doc.transact(function () {
1872
- replaceLeafText(start.leaf, start.offset, length, getDataTransfer(e));
1557
+ replaceLeafText(start.leaf, start.offset, length, getDataTransfer(e));
1558
+ setPosition(start.leafId, {
1559
+ offset: start.offset
1873
1560
  });
1874
1561
  }
1875
1562
  }
@@ -1894,32 +1581,71 @@ var onKeyDown = function onKeyDown(editor, e) {
1894
1581
  return editor.undoManager.redo();
1895
1582
  }
1896
1583
 
1897
- for (var i = 0; i < editor.hotkeys.length; i++) {
1898
- var _editor$hotkeys$i = editor.hotkeys[i],
1899
- keys = _editor$hotkeys$i.keys,
1900
- operation = _editor$hotkeys$i.operation,
1901
- mark = _editor$hotkeys$i.mark;
1584
+ if (editor.hotkeys) {
1585
+ for (var i = 0; i < editor.hotkeys.length; i++) {
1586
+ var _editor$hotkeys$i = editor.hotkeys[i],
1587
+ keys = _editor$hotkeys$i.keys,
1588
+ operation = _editor$hotkeys$i.operation,
1589
+ mark = _editor$hotkeys$i.mark;
1902
1590
 
1903
- if (isHotkey(keys, e)) {
1904
- e.preventDefault();
1905
- e.stopPropagation();
1591
+ if (isHotkey(keys, e)) {
1592
+ e.preventDefault();
1593
+ e.stopPropagation();
1906
1594
 
1907
- if (typeof operation === "function") {
1908
- return operation(editor);
1909
- }
1595
+ if (typeof operation === "function") {
1596
+ return operation(editor);
1597
+ }
1910
1598
 
1911
- switch (operation) {
1912
- case "formatText":
1913
- {
1914
- return formatText(editor, mark);
1915
- }
1916
- }
1599
+ switch (operation) {
1600
+ case "formatText":
1601
+ {
1602
+ return formatText(editor, mark);
1603
+ }
1604
+ }
1917
1605
 
1918
- break;
1606
+ break;
1607
+ }
1919
1608
  }
1920
1609
  }
1921
1610
  }
1922
1611
  };
1612
+ var defaultHotkeys = [{
1613
+ operation: "formatText",
1614
+ keys: "mod+b",
1615
+ mark: {
1616
+ bold: true
1617
+ }
1618
+ }, {
1619
+ operation: "formatText",
1620
+ keys: "mod+i",
1621
+ mark: {
1622
+ italic: true
1623
+ }
1624
+ }, {
1625
+ operation: "formatText",
1626
+ keys: "mod+u",
1627
+ mark: {
1628
+ underline: true
1629
+ }
1630
+ }, {
1631
+ operation: "formatText",
1632
+ keys: "mod+shift+c",
1633
+ mark: {
1634
+ code: true
1635
+ }
1636
+ }, {
1637
+ operation: "formatText",
1638
+ keys: "mod+shift+x",
1639
+ mark: {
1640
+ strikethrough: true
1641
+ }
1642
+ }, {
1643
+ operation: "formatText",
1644
+ keys: "mod+shift+h",
1645
+ mark: {
1646
+ highlight: true
1647
+ }
1648
+ }];
1923
1649
 
1924
1650
  var onDrop = function onDrop(editor, e) {
1925
1651
  var _e$dataTransfer;
@@ -1944,49 +1670,23 @@ var onDragOver = function onDragOver(editor, e) {
1944
1670
  }
1945
1671
  };
1946
1672
 
1947
- var createWebRtcProvider = function createWebRtcProvider(doc, room, opts) {
1948
- if (room === void 0) {
1949
- room = "hello";
1950
- }
1951
-
1952
- var fakeDoc = new Doc();
1953
- var provider = new WebrtcProvider(room, doc, opts);
1954
- provider.emit("ping", ["lkeazlk"]);
1955
- provider.connect();
1956
- provider.on("ping", function () {
1957
- console.log("ping");
1958
- });
1959
- provider.on("*", function (event) {
1960
- console.log(event);
1961
- });
1962
- provider.on("connect", function (event) {
1963
- console.log(event);
1964
- });
1965
- return provider;
1966
- };
1967
-
1968
- var createWSProvider = function createWSProvider(doc, room, opts) {
1969
- if (room === void 0) {
1970
- room = "hello";
1971
- }
1972
-
1973
- var fakeDoc = new EdytorDoc();
1974
- var provider = new WebsocketProvider("ws://localhost:1234", room, fakeDoc);
1975
- provider.on("status", function (_ref) {
1976
- var status = _ref.status;
1977
-
1978
- if (status === "connected") {
1979
- var isEmpty = Array.from(provider.awareness.getStates().values()).length === 1;
1980
- console.log(isEmpty, Array.from(provider.awareness.getStates().values()).length);
1981
- provider.awareness.setLocalStateField("connected", true);
1673
+ var createWSProvider = function createWSProvider(room) {
1674
+ return new Promise(function (res) {
1675
+ var doc = new Doc();
1676
+ var prod = "wss://edytor-production.up.railway.app";
1677
+ console.log(process.env.NODE_ENV);
1678
+ var provider = new WebsocketProvider(process.env.NODE_ENV === "production" ? prod : prod, room || "edytor", doc, {
1679
+ connect: true
1680
+ });
1681
+ provider.on("status", function (_ref) {
1682
+ var status = _ref.status;
1982
1683
 
1983
- if (isEmpty) {
1984
- applyUpdateV2(fakeDoc, encodeStateAsUpdateV2(doc));
1684
+ if (status === "connected") {
1685
+ return res(provider);
1985
1686
  }
1986
- }
1687
+ });
1987
1688
  });
1988
- return provider.doc;
1989
1689
  };
1990
1690
 
1991
- export { Cursor, Dropper, EdytorDoc, EdytorSelection, LeavesHarvest, YLeaf, YNode, applyMarksFromParent, areLeafsMergeable, copyNode, createWSProvider, createWebRtcProvider, deleteLeafText, deleteNode, deleteText, formatAtEqualPath, formatText, getChildren, getContent, getId, getIndex, getNode, getNodeChildren, getNodeContainer, getNodeContent, getPath, getRange, hasChildren, insertText, insertTextInLeaf, isLeafEmpty, isNodeContentEmpty, isNodeEmpty, leafData, leafLength, leafNode, leafNodeChildren, leafNodeChildrenLength, leafNodeContent, leafNodeContentLength, leafNodeContentStringLength, leafNodeId, leafString, leafText, mergeContentWithNextLeaf, mergeContentWithPrevLeaf, mergeLeafs, moveNode, nanoid, nestNode, onBeforeInput, onDragOver, onDrop, onKeyDown, removeEmptyText, removeIfEmpty, replaceLeafText, setLeafData, setLeafText, splitLeaf, splitNode };
1691
+ export { DocFromJson, Dropper, EdytorDoc, EdytorSelection, LeavesHarvest, YNode, applyMarksFromParent, areLeafsMergeable, copyNode, createLeaf$1 as createLeaf, createNode, createWSProvider, defaultHotkeys, deleteLeafText, deleteNode, deleteText, formatAtEqualPath, formatText, getChildren, getContainerAtPath, getId, getIndex, getLeafAtPath, getNode, getNodeAtPath, getNodeChildren, getNodeContainer, getNodeContent, getPath, hasChildren, insertText, insertTextInLeaf, isLeafEmpty, isNodeContentEmpty, isNodeEmpty, leafData, leafLength, leafNode, leafNodeChildren, leafNodeChildrenLength, leafNodeContent, leafNodeContentLength, leafNodeContentStringLength, leafNodeId, leafString, leafText, mergeContentWithNextLeaf, mergeContentWithPrevLeaf, mergeLeafs, moveNode, nanoid, nestNode, nodeString, onBeforeInput, onDragOver, onDrop, onKeyDown, removeEmptyText, removeIfEmpty, replaceLeafText, setLeafData, setLeafText, splitLeaf, splitNode, toString, traverse };
1992
1692
  //# sourceMappingURL=edytor.esm.js.map