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