edytor 0.0.5 → 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,7 +4,7 @@ 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 Y = require('yjs');
7
+ var yjs = require('yjs');
8
8
  var nanoid$1 = require('nanoid');
9
9
  var isHotkey = _interopDefault(require('is-hotkey'));
10
10
  var yWebsocket = require('y-websocket');
@@ -60,17 +60,17 @@ function _objectWithoutPropertiesLoose(source, excluded) {
60
60
 
61
61
  var _excluded = ["id", "text", "data"];
62
62
  var createLeaf$1 = function createLeaf(props) {
63
- var leaf = new Y.Map();
63
+ var leaf = new yjs.Map();
64
64
  leaf.set("id", (props == null ? void 0 : props.id) || nanoid());
65
- leaf.set("text", new Y.Text((props == null ? void 0 : props.text) || ""));
65
+ leaf.set("text", new yjs.Text((props == null ? void 0 : props.text) || ""));
66
66
 
67
67
  if (props) {
68
68
  var text = props.text,
69
69
  data = props.data,
70
70
  marks = _objectWithoutPropertiesLoose(props, _excluded);
71
71
 
72
- leaf.set("text", new Y.Text(text || ""));
73
- data && leaf.set("data", new Y.Map(Object.entries(data)));
72
+ leaf.set("text", new yjs.Text(text || ""));
73
+ data && leaf.set("data", new yjs.Map(Object.entries(data)));
74
74
  marks && Object.keys(marks).forEach(function (mark) {
75
75
  leaf.set(mark, marks[mark]);
76
76
  });
@@ -80,16 +80,16 @@ var createLeaf$1 = function createLeaf(props) {
80
80
  };
81
81
 
82
82
  var createNode = function createNode(type, props) {
83
- var node = new Y.Map();
83
+ var node = new yjs.Map();
84
84
  node.set("id", (props == null ? void 0 : props.id) || nanoid());
85
85
  node.set("type", type);
86
86
 
87
87
  if (props != null && props.data) {
88
- node.set("data", new Y.Map(Object.entries(props.data)));
88
+ node.set("data", new yjs.Map(Object.entries(props.data)));
89
89
  }
90
90
 
91
- node.set("content", (props == null ? void 0 : props.content) instanceof Y.Array ? props.content : Y.Array.from((props == null ? void 0 : props.content) || [createLeaf$1()]));
92
- node.set("children", (props == null ? void 0 : props.children) instanceof Y.Array ? props.children : Y.Array.from((props == null ? void 0 : props.children) || []));
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
93
  return node;
94
94
  };
95
95
  var YNode = /*#__PURE__*/function (_Map) {
@@ -100,12 +100,9 @@ var YNode = /*#__PURE__*/function (_Map) {
100
100
  }
101
101
 
102
102
  return YNode;
103
- }(Y.Map);
103
+ }(yjs.Map);
104
104
 
105
105
  var _excluded$1 = ["type", "content", "children"];
106
- var getContent = function getContent(leaf) {
107
- return createLeaf$1(leaf);
108
- };
109
106
  var getChildren = function getChildren(_ref) {
110
107
  var type = _ref.type,
111
108
  _ref$content = _ref.content,
@@ -116,27 +113,22 @@ var getChildren = function getChildren(_ref) {
116
113
 
117
114
  return createNode(type, _extends({}, props, {
118
115
  children: children.map(getChildren),
119
- content: content.map(getContent)
116
+ content: content.map(createLeaf$1)
120
117
  }));
121
118
  };
122
119
  var EdytorDoc = /*#__PURE__*/function (_Doc) {
123
120
  _inheritsLoose(EdytorDoc, _Doc);
124
121
 
125
- function EdytorDoc(value) {
126
- var _this;
127
-
128
- _this = _Doc.call(this) || this;
129
-
130
- return _this;
122
+ function EdytorDoc() {
123
+ return _Doc.apply(this, arguments) || this;
131
124
  }
132
125
 
133
126
  return EdytorDoc;
134
- }(Y.Doc);
135
- var DocFromJson = function DocFromJson(value) {
136
- var doc = new Y.Doc();
137
- var array = doc.getArray("children");
127
+ }(yjs.Doc);
128
+ var DocFromJson = function DocFromJson(value, initialDoc) {
129
+ var array = (initialDoc || new yjs.Doc()).getArray("children");
138
130
  array.insert(0, value.map(getChildren));
139
- return doc;
131
+ return array.doc;
140
132
  };
141
133
 
142
134
  var deleteText = function deleteText(editor, _ref) {
@@ -155,21 +147,28 @@ var deleteText = function deleteText(editor, _ref) {
155
147
  case "collapsed":
156
148
  {
157
149
  if (mode === "backward" && edges.startNode) {
158
- 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
159
151
 
160
- if (start.nodeIndex === start.node.parent.length - 1 && start.path.length > 1 && start.nodeIndex !== 0) {
161
- 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)) {
162
153
  var node = copyNode(start.node);
154
+ var nodeGrandParent = start.node.parent.parent;
155
+ var index = getIndex(nodeGrandParent);
163
156
  start.node.parent["delete"](start.nodeIndex);
164
- start.node.parent.parent.parent.insert(getIndex(start.node.parent.parent) + 1, [node]);
165
- setPosition(start.leafId, {
157
+ nodeGrandParent.parent.insert(index + 1, [node]);
158
+ return setPosition(start.leafId, {
166
159
  offset: 0
167
160
  });
168
161
  } else {
169
- // merge node with the prev one if it's not the last of its parent regardless of its nesting
170
- mergeContentWithPrevLeaf(editor);
171
- return setPosition(start.leafId, {
172
- 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
+ });
173
172
  });
174
173
  }
175
174
  }
@@ -185,14 +184,22 @@ var deleteText = function deleteText(editor, _ref) {
185
184
 
186
185
  if (mode === "forward" && edges.endNode) {
187
186
  // merge node with the next node because we are at edge end of it
188
- 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
+ });
189
195
  }
190
196
 
191
197
  if (mode === "forward" && edges.endLeaf) {
192
198
  // delete content in the next leaf because we are at its edge end
193
199
  var nextLeaf = leafNodeContent(start.leaf).get(start.leafIndex + 1);
194
200
  return deleteLeafText(nextLeaf, 0, length || 1);
195
- }
201
+ } // just delete text
202
+
196
203
 
197
204
  deleteLeafText(start.leaf, start.offset + (mode === "backward" ? -length || -1 : length), length || 1);
198
205
  setPosition(start.leaf.get("id"), {
@@ -205,11 +212,11 @@ var deleteText = function deleteText(editor, _ref) {
205
212
  {
206
213
  deleteLeafText(start.leaf, start.offset, length, true);
207
214
 
208
- if (hasChildren(getNode(start.leaf))) {
215
+ if (isNodeContentEmpty(start.node)) {
209
216
  leafNodeContent(start.leaf).insert(0, [createLeaf$1()]);
210
217
  }
211
218
 
212
- setPosition(start.leaf.get("id"), {
219
+ setPosition(start.leafId, {
213
220
  offset: start.offset
214
221
  });
215
222
  break;
@@ -257,7 +264,7 @@ var deleteText = function deleteText(editor, _ref) {
257
264
 
258
265
  var applyMarksFromParent = function applyMarksFromParent(parent, leafs) {
259
266
  Array.from(parent.keys(), function (key) {
260
- if (key !== "text") {
267
+ if (key !== "text" && key !== "id" && key !== "data") {
261
268
  leafs.forEach(function (leaf) {
262
269
  leaf.set(key, parent.get(key));
263
270
  });
@@ -329,74 +336,76 @@ var formatText = function formatText(editor, mark) {
329
336
  switch (type) {
330
337
  case "collapsed":
331
338
  {
332
- var isMarkActive = start.leaf.has(format);
339
+ var isMarkActive = start.leaf.has(format) && start.leaf.get(format) === value;
333
340
 
334
- if (isMarkActive && isLeafEmpty(start.leaf)) {
335
- start.leaf["delete"](format);
336
- 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);
337
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
+
338
359
  if (isLeafEmpty(start.leaf)) {
339
- isMarkActive ? start.leaf["delete"](format) : start.leaf.set(format, value);
340
- } else {
341
- var _createLeaf;
360
+ leafNodeContent(start.leaf)["delete"](start.leafIndex);
361
+ }
342
362
 
343
- splitLeaf(editor, {
344
- yText: start.leaf.get("text"),
345
- at: start
363
+ setTimeout(function () {
364
+ setPosition(fragment.get("id"), {
365
+ offset: 0
346
366
  });
347
- var newChild = createLeaf$1((_createLeaf = {
348
- text: ""
349
- }, _createLeaf[format] = value, _createLeaf));
350
- applyMarksFromParent(start.leaf, [newChild]);
351
- if (isMarkActive) newChild["delete"](format);
352
- leafNodeContent(start.leaf).insert(start.path.slice().reverse()[0] + 1, [newChild]);
353
-
354
- if (isLeafEmpty(start.leaf)) {
355
- leafNodeContent(start.leaf)["delete"](start.path.slice().reverse()[0]);
356
- }
367
+ console.log(leafNodeContent(start.leaf).toJSON());
368
+ });
369
+ } // we merge leaves to avoid two subsequent leaves with the same marks
357
370
 
358
- newPath = leafNodeContent(start.leaf).toArray().indexOf(newChild);
359
- }
360
- }
361
371
 
362
372
  mergeLeafs(leafNodeContent(start.leaf));
363
- break; // return newPath;
373
+ break;
364
374
  }
365
375
 
366
376
  case "singlenode":
367
377
  {
368
- editor.doc.transact(function () {
369
- var formatedLeaf = formatAtEqualPath({
370
- format: format,
371
- value: value,
372
- start: start,
373
- end: end,
374
- length: length
375
- });
376
- mergeLeafs(leafNodeContent(start.leaf));
377
- var isRemoved = formatedLeaf._item.deleted;
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;
378
387
 
379
- if (!isRemoved) {
380
- setPosition(start.leaf.get("id"), {
381
- offset: start.offset
382
- });
383
- setTimeout(function () {
384
- setPosition(formatedLeaf.get("id"), {
385
- offset: 0,
386
- end: leafLength(formatedLeaf)
387
- });
388
+ if (!isRemoved) {
389
+ setPosition(start.leaf.get("id"), {
390
+ offset: start.offset
391
+ });
392
+ setTimeout(function () {
393
+ setPosition(formatedLeaf.get("id"), {
394
+ offset: 0,
395
+ end: leafLength(formatedLeaf)
388
396
  });
389
- } else {
390
- var remainingLeaf = formatedLeaf.parent.get(start.leafIndex - 1);
391
- var offset = leafText(remainingLeaf).toJSON().indexOf(selectedText);
392
- setTimeout(function () {
393
- setPosition(remainingLeaf.get("id"), {
394
- offset: offset,
395
- end: offset + selectedText.length
396
- });
397
+ });
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
397
405
  });
398
- }
399
- });
406
+ });
407
+ }
408
+
400
409
  break;
401
410
  }
402
411
 
@@ -577,21 +586,19 @@ var removeEmptyText = function removeEmptyText(yText) {
577
586
  }
578
587
  };
579
588
 
580
- var splitLeaf = function splitLeaf(editor, _ref) {
581
- var _yText$parent;
582
-
583
- var yText = _ref.yText,
584
- at = _ref.at;
585
- var content = yText.toString();
586
- var rightText = content.substring(at.offset, yText.length);
587
- if (at.offset === yText.length) return;
588
- var newText = new Y.Text(rightText);
589
- var newLeave = new Y.Map();
590
- newLeave.set("text", newText);
591
- applyMarksFromParent(yText.parent, [newLeave]);
592
- var parent = yText == null ? void 0 : (_yText$parent = yText.parent) == null ? void 0 : _yText$parent.parent;
593
- yText["delete"](at.offset, yText.length);
594
- 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
+ }))]);
595
602
  };
596
603
 
597
604
  var splitNode = function splitNode(editor) {
@@ -623,17 +630,16 @@ var splitNode = function splitNode(editor) {
623
630
  return createLeaf$1(leaf.toJSON());
624
631
  }))
625
632
  })]);
633
+ setPosition(newLeaf.get("id"), {
634
+ offset: 0
635
+ });
626
636
  leafContent["delete"](indexOfLeaf + 1, leafNodeContentLength(start.leaf) - indexOfLeaf - 1);
627
- setTimeout(function () {
628
- setPosition(newLeaf.get("id"), {
629
- offset: 0
630
- });
631
- }, 10);
632
637
  };
633
638
 
634
639
  var doSplit = (!edges.startNode || !edges.endNode) && type !== "multinodes";
635
640
 
636
641
  if ((edges.endNode || edges.startNode) && type === "collapsed") {
642
+ // if cursor is at edge node with insert a default block before of after the current node
637
643
  var newNode = createNode(editor.defaultBlock);
638
644
  node.parent.insert(indexOfNode + (edges.endNode ? 1 : 0), [newNode]);
639
645
  return setTimeout(function () {
@@ -683,7 +689,7 @@ var nestNode = function nestNode(editor) {
683
689
  end = _editor$selection.end,
684
690
  setPosition = _editor$selection.setPosition;
685
691
  var startNode = getNode(start.leaf);
686
- var startJsonNode = startNode.toJSON();
692
+ var nodeCopy = copyNode(startNode);
687
693
  var startPath = getPath(startNode);
688
694
 
689
695
  var _startPath$slice$reve = startPath.slice().reverse(),
@@ -701,10 +707,7 @@ var nestNode = function nestNode(editor) {
701
707
  var id = start.leaf.get("id");
702
708
  var prevChildren = prevNode.get("children");
703
709
  startContainer["delete"](index);
704
- prevChildren.insert(prevChildren.length, [createNode(startJsonNode.type, _extends({}, startJsonNode, {
705
- children: startJsonNode.children.map(getChildren),
706
- content: startJsonNode.content.map(getContent)
707
- }))]);
710
+ prevChildren.insert(prevChildren.length, [nodeCopy]);
708
711
  setPosition(id, {
709
712
  offset: start.offset
710
713
  });
@@ -728,11 +731,7 @@ var nestNode = function nestNode(editor) {
728
731
  if (path.length === startPath.length) {
729
732
  if (path <= endPathString && path >= startPathString) {
730
733
  length++;
731
- var jsonNode = node.toJSON();
732
- newNodes.push(createNode(jsonNode.type, _extends({}, jsonNode, {
733
- children: jsonNode.children.map(getChildren),
734
- content: jsonNode.content.map(getContent)
735
- })));
734
+ newNodes.push(copyNode(node));
736
735
  }
737
736
  }
738
737
  }
@@ -786,17 +785,26 @@ var mergeContentWithPrevLeaf = function mergeContentWithPrevLeaf(editor) {
786
785
  }
787
786
  });
788
787
  if (!prevLeaf) return;
788
+ var prevLeafLength = leafLength(prevLeaf);
789
789
  leafNodeContent(prevLeaf).insert(leafNodeContentLength(prevLeaf), leafNodeContent(start.leaf).toArray().map(function (leaf) {
790
790
  return createLeaf$1(leaf.toJSON());
791
791
  }));
792
792
  mergeLeafs(leafNodeContent(prevLeaf));
793
- 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);
794
801
  };
795
802
  var mergeContentWithNextLeaf = function mergeContentWithNextLeaf(editor) {
796
- var start = editor.selection.start;
803
+ var start = editor.selection.start; // 1 .get the next leaf
804
+
797
805
  var nextLeaf;
798
806
  var stop = false;
799
- editor.doc.traverse(function (node, isText) {
807
+ traverse(editor, function (node, isText) {
800
808
  if (isText) {
801
809
  if (node === start.leaf) {
802
810
  stop = true;
@@ -805,24 +813,21 @@ var mergeContentWithNextLeaf = function mergeContentWithNextLeaf(editor) {
805
813
  stop = false;
806
814
  }
807
815
  }
808
- }); //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
809
819
 
810
- console.log(leafNodeContent(nextLeaf).toArray());
811
820
  leafNodeContent(start.leaf).insert(leafNodeContentLength(start.leaf), leafNodeContent(nextLeaf).toArray().map(function (leaf) {
812
821
  return createLeaf$1(leaf.toJSON());
813
822
  }));
814
- mergeLeafs(leafNodeContent(start.leaf));
815
- console.log(start.leaf.parent.toJSON());
816
- 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
817
824
 
818
- if (hasChildren(node)) {
819
- console.log(node.get("children").toJSON());
820
- node.parent.insert(getIndex(node), node.get("children").toJSON().map(function (node) {
821
- return createNode(node.type, node);
822
- }));
823
- }
824
-
825
- 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);
826
831
  return nextLeaf;
827
832
  };
828
833
  var deleteNode = function deleteNode(node, defaultBlock) {
@@ -868,9 +873,19 @@ var copyNode = function copyNode(node) {
868
873
  var jsonNode = node.toJSON();
869
874
  return createNode(jsonNode.type, _extends({}, jsonNode, {
870
875
  children: jsonNode.children.map(getChildren),
871
- content: jsonNode.content.map(getContent)
876
+ content: jsonNode.content.map(createLeaf$1)
872
877
  }));
873
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
+ };
874
889
 
875
890
  var nanoid = function nanoid() {
876
891
  return "y-" + nanoid$1.customAlphabet("346789ABCDEFGHJKLMNPQRTUV-WXYabcdefghijkmnpqrtwxyz", 20)();
@@ -985,7 +1000,7 @@ var toString = function toString(editor) {
985
1000
  var array = editor.children.toArray();
986
1001
 
987
1002
  for (var i = 0; i < array.length; i++) {
988
- t += leafString(array[i]);
1003
+ t += nodeString(array[i]);
989
1004
  }
990
1005
 
991
1006
  return t;
@@ -1073,6 +1088,7 @@ var setLeafData = function setLeafData(leaf, data) {
1073
1088
  };
1074
1089
  var removeIfEmpty = function removeIfEmpty(leaf) {
1075
1090
  if (isLeafEmpty(leaf)) {
1091
+ console.log("isEmpty", leafNodeContent(leaf).toJSON());
1076
1092
  leafNodeContent(leaf)["delete"](getIndex(leaf)); // if (leafNodeContentLength(leaf) === 0) {
1077
1093
  // deleteNode(leafNode(leaf));
1078
1094
  // }
@@ -1118,6 +1134,7 @@ var Dropper = function Dropper() {
1118
1134
  this.startDrag = function (node, e) {
1119
1135
  var _e$dataTransfer;
1120
1136
 
1137
+ if (!_this.editor.dnd.active) return;
1121
1138
  _this.node = node;
1122
1139
  var dndIndicator = document.getElementById("dndIndicator");
1123
1140
  dndIndicator.style.opacity = "1";
@@ -1180,6 +1197,8 @@ var Dropper = function Dropper() {
1180
1197
  };
1181
1198
 
1182
1199
  this.onDrag = function (e) {
1200
+ if (!_this.editor.dnd.active) return;
1201
+
1183
1202
  var _this$getHoveredNode = _this.getHoveredNode(e.target),
1184
1203
  hoveredElement = _this$getHoveredNode.hoveredElement,
1185
1204
  hoveredNode = _this$getHoveredNode.hoveredNode;
@@ -1344,11 +1363,15 @@ var EdytorSelection = function EdytorSelection() {
1344
1363
  _this.length = _this.selectedText.length;
1345
1364
  _this.range = range;
1346
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;
1347
1368
  _this.edges = {
1348
1369
  startLeaf: _this.start.offset === 0,
1349
- startNode: _this.start.offset === 0 && getIndex(_this.start.leaf) === 0,
1350
1370
  endLeaf: _this.end.offset === leafLength(_this.end.leaf),
1351
- 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
1352
1375
  };
1353
1376
  _this.type = isCollapsed ? "collapsed" : equalPaths ? "singlenode" : _this.start.node === _this.end.node ? "multileaves" : "multinodes";
1354
1377
 
@@ -1358,7 +1381,16 @@ var EdytorSelection = function EdytorSelection() {
1358
1381
  this.onChange = function () {
1359
1382
  _this.observers.forEach(function (o) {
1360
1383
  return o(_this);
1361
- });
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
+ // }
1393
+
1362
1394
  };
1363
1395
 
1364
1396
  this.setPosition = function (id, _ref) {
@@ -1370,6 +1402,10 @@ var EdytorSelection = function EdytorSelection() {
1370
1402
 
1371
1403
  var node = _this.container.querySelector("#" + id);
1372
1404
 
1405
+ console.log({
1406
+ node: node
1407
+ });
1408
+
1373
1409
  while (node && node.nodeType !== 3 && (_node = node) != null && _node.firstChild) {
1374
1410
  var _node;
1375
1411
 
@@ -1413,14 +1449,21 @@ var getDataTransfer = function getDataTransfer(e) {
1413
1449
  };
1414
1450
 
1415
1451
  var onBeforeInput = function onBeforeInput(_ref, e) {
1416
- var doc = _ref[0],
1417
- onChange = _ref[1],
1418
- editor = _ref[2];
1419
- prevent(e); // console.log(e);
1452
+ var _e$target;
1420
1453
 
1454
+ var onChange = _ref[1],
1455
+ editor = _ref[2];
1456
+ // console.log(e);
1421
1457
  var _editor$selection = editor.selection,
1422
1458
  start = _editor$selection.start,
1423
- 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);
1424
1467
 
1425
1468
  switch (e.inputType) {
1426
1469
  case "insertFromPaste":
@@ -1517,8 +1560,9 @@ var onBeforeInput = function onBeforeInput(_ref, e) {
1517
1560
 
1518
1561
  case "insertReplacementText":
1519
1562
  {
1520
- doc().doc.transact(function () {
1521
- 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
1522
1566
  });
1523
1567
  }
1524
1568
  }
@@ -1543,32 +1587,71 @@ var onKeyDown = function onKeyDown(editor, e) {
1543
1587
  return editor.undoManager.redo();
1544
1588
  }
1545
1589
 
1546
- for (var i = 0; i < editor.hotkeys.length; i++) {
1547
- var _editor$hotkeys$i = editor.hotkeys[i],
1548
- keys = _editor$hotkeys$i.keys,
1549
- operation = _editor$hotkeys$i.operation,
1550
- 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;
1551
1596
 
1552
- if (isHotkey(keys, e)) {
1553
- e.preventDefault();
1554
- e.stopPropagation();
1597
+ if (isHotkey(keys, e)) {
1598
+ e.preventDefault();
1599
+ e.stopPropagation();
1555
1600
 
1556
- if (typeof operation === "function") {
1557
- return operation(editor);
1558
- }
1601
+ if (typeof operation === "function") {
1602
+ return operation(editor);
1603
+ }
1559
1604
 
1560
- switch (operation) {
1561
- case "formatText":
1562
- {
1563
- return formatText(editor, mark);
1564
- }
1565
- }
1605
+ switch (operation) {
1606
+ case "formatText":
1607
+ {
1608
+ return formatText(editor, mark);
1609
+ }
1610
+ }
1566
1611
 
1567
- break;
1612
+ break;
1613
+ }
1568
1614
  }
1569
1615
  }
1570
1616
  }
1571
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
+ }];
1572
1655
 
1573
1656
  var onDrop = function onDrop(editor, e) {
1574
1657
  var _e$dataTransfer;
@@ -1593,15 +1676,12 @@ var onDragOver = function onDragOver(editor, e) {
1593
1676
  }
1594
1677
  };
1595
1678
 
1596
- var createWebRtcProvider = function createWebRtcProvider(doc, room, opts) {// const fakeDoc = new Doc();
1597
- };
1598
-
1599
1679
  var createWSProvider = function createWSProvider(room) {
1600
1680
  return new Promise(function (res) {
1601
- var doc = new Y.Doc();
1602
- var dev = "ws://localhost:1234";
1681
+ var doc = new yjs.Doc();
1682
+ var prod = "wss://edytor-production.up.railway.app";
1603
1683
  console.log("development");
1604
- var provider = new yWebsocket.WebsocketProvider( dev, room || "edytor", doc, {
1684
+ var provider = new yWebsocket.WebsocketProvider( prod, room || "edytor", doc, {
1605
1685
  connect: true
1606
1686
  });
1607
1687
  provider.on("status", function (_ref) {
@@ -1626,7 +1706,7 @@ exports.copyNode = copyNode;
1626
1706
  exports.createLeaf = createLeaf$1;
1627
1707
  exports.createNode = createNode;
1628
1708
  exports.createWSProvider = createWSProvider;
1629
- exports.createWebRtcProvider = createWebRtcProvider;
1709
+ exports.defaultHotkeys = defaultHotkeys;
1630
1710
  exports.deleteLeafText = deleteLeafText;
1631
1711
  exports.deleteNode = deleteNode;
1632
1712
  exports.deleteText = deleteText;
@@ -1634,7 +1714,6 @@ exports.formatAtEqualPath = formatAtEqualPath;
1634
1714
  exports.formatText = formatText;
1635
1715
  exports.getChildren = getChildren;
1636
1716
  exports.getContainerAtPath = getContainerAtPath;
1637
- exports.getContent = getContent;
1638
1717
  exports.getId = getId;
1639
1718
  exports.getIndex = getIndex;
1640
1719
  exports.getLeafAtPath = getLeafAtPath;
@@ -1667,6 +1746,7 @@ exports.mergeLeafs = mergeLeafs;
1667
1746
  exports.moveNode = moveNode;
1668
1747
  exports.nanoid = nanoid;
1669
1748
  exports.nestNode = nestNode;
1749
+ exports.nodeString = nodeString;
1670
1750
  exports.onBeforeInput = onBeforeInput;
1671
1751
  exports.onDragOver = onDragOver;
1672
1752
  exports.onDrop = onDrop;