@seafile/seafile-editor 0.3.79 → 0.3.80

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,5 +1,5 @@
1
- import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
2
1
  import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
2
+ import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
3
3
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
4
4
  import { Editor } from 'slate';
5
5
  import ListUtils from '../editor-utils/block-element-utils/list-utils';
@@ -146,6 +146,27 @@ var withBlock = function withBlock(editor) {
146
146
 
147
147
  if (firstFragmentNode.type === 'paragraph') {
148
148
  Editor.insertFragment(editor, fragment);
149
+ return;
150
+ }
151
+ /**
152
+ * if the first fragment element is a list, get normalized list items with function getNormalizedListItems and insert
153
+ */
154
+
155
+
156
+ if (firstFragmentNode.type.includes('_list')) {
157
+ var listItems = listUtils.getNormalizedListItems(firstFragmentNode);
158
+ var newFragment = fragment.slice(1);
159
+ Editor.insertNodes(editor, [{
160
+ type: firstFragmentNode.type,
161
+ children: listItems
162
+ }].concat(_toConsumableArray(newFragment)));
163
+
164
+ if (SfEditor.isEmptyParagraph(currentBlock)) {
165
+ Editor.delete(editor, {
166
+ at: path
167
+ });
168
+ }
169
+
149
170
  return;
150
171
  } // if current node is an empty paragraph, insert the fragment and
151
172
  // delete the empty paragraph
@@ -247,15 +268,16 @@ var withBlock = function withBlock(editor) {
247
268
  }
248
269
  });
249
270
  } else {
250
- var listItems = Editor.nodes(editor, {
271
+ var _listItems = Editor.nodes(editor, {
251
272
  at: editor.selection,
252
273
  match: {
253
274
  type: 'list_item'
254
275
  }
255
276
  });
277
+
256
278
  var nearestItem;
257
279
 
258
- var _iterator = _createForOfIteratorHelper(listItems),
280
+ var _iterator = _createForOfIteratorHelper(_listItems),
259
281
  _step;
260
282
 
261
283
  try {
@@ -327,34 +327,54 @@ var withMarkdownShortcut = function withMarkdownShortcut(editor) {
327
327
  if (listUtils.isInlist()) {
328
328
  var _Editor$nodes9 = Editor.nodes(editor, {
329
329
  match: [{
330
- type: 'ordered_list'
331
- }, {
332
- type: 'unordered_list'
330
+ type: 'list_item'
333
331
  }]
334
332
  }),
335
333
  _Editor$nodes10 = _slicedToArray(_Editor$nodes9, 1),
336
334
  _node3 = _Editor$nodes10[0];
337
335
 
338
- var listNode = _node3[0];
336
+ var listItemPath = _node3[1];
337
+ var listNode = Node.parent(editor, listItemPath); // Unwrap the list item when the selection is at the first list item of the first node of document
339
338
 
340
- if (listNode.children.length === 1 && Node.text(listNode).length === 0) {
339
+ if (anchor.path[0] === 0 && anchor.path[1] === 0) {
341
340
  listUtils.unwrapList();
342
341
  return;
343
- } // Unwrap the list item when the selection is at the first list item of the first node of document
342
+ } // unwrap an empty list
344
343
 
345
344
 
346
- if (anchor.path[0] === 0 && anchor.path[1] === 0) {
345
+ if (listNode.children.length === 1 && Node.text(listNode).length === 0) {
347
346
  listUtils.unwrapList();
348
347
  return;
348
+ } // list items with mutiple chidren
349
+
350
+
351
+ var currentChildBlockIndex = anchor.path[anchor.path.length - 2];
352
+ var currentListItem;
353
+
354
+ for (var i = anchor.path.length - 1; i > 0; i--) {
355
+ var _node4 = Node.get(editor, anchor.path.slice(0, i));
356
+
357
+ if (_node4.type === 'list_item') {
358
+ currentListItem = _node4;
359
+ break;
360
+ }
361
+ }
362
+
363
+ if (currentChildBlockIndex === 0 && currentListItem.children.length > 1) {
364
+ Editor.withoutNormalizing(editor, function (editor) {
365
+ listUtils.unwrapList();
366
+ exec(command);
367
+ });
368
+ return;
349
369
  }
350
370
  }
351
371
 
352
372
  if (codeUtils.isInCodeBlock()) {
353
- var _node4 = Editor.match(editor, editor.selection, {
373
+ var _node5 = Editor.match(editor, editor.selection, {
354
374
  type: 'code_block'
355
375
  });
356
376
 
357
- var codeBlock = _node4[0];
377
+ var codeBlock = _node5[0];
358
378
  var children = codeBlock.children;
359
379
  var text = Node.text(codeBlock);
360
380
 
@@ -2,7 +2,7 @@ import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIt
2
2
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
3
3
  import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
4
4
  import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
5
- import { Node, Range, Path } from 'slate';
5
+ import { Node, Range, Path, Editor } from 'slate';
6
6
  import { SfEditor } from '../../custom/custom';
7
7
  import getEventTransfer from '../../custom/get-event-transfer';
8
8
  import { htmlDeserializer } from '../../../utils/deserialize-html';
@@ -14,117 +14,153 @@ var LitsUtils = function LitsUtils(editor) {
14
14
  _classCallCheck(this, LitsUtils);
15
15
 
16
16
  this.unwrapList = function () {
17
- normailizeSelection(_this.editor); // selected list item
17
+ // defer normalization util the unwrap operartions completed
18
+ SfEditor.withoutNormalizing(_this.editor, function () {
19
+ normailizeSelection(_this.editor); // selected list item
18
20
 
19
- var selectedListItem = _this.getCurrentListItem(); // first selected item path and node
21
+ var selectedListItem = _this.getCurrentListItem(); // first selected item path and node
20
22
 
21
23
 
22
- var firstSelectedItem = selectedListItem[0];
23
- var firstSelectedItemPath = firstSelectedItem[1]; // get selected list and the path of the list
24
+ var firstSelectedItem = selectedListItem[0];
25
+ var firstSelectedItemPath = firstSelectedItem[1]; // get selected list and the path of the list
24
26
 
25
- var selectedListPath = firstSelectedItemPath.slice(0, firstSelectedItemPath.length - 1);
26
- var selectedList = Node.get(_this.editor, selectedListPath); // get the node and path of parent of selected list
27
+ var selectedListPath = firstSelectedItemPath.slice(0, firstSelectedItemPath.length - 1);
28
+ var selectedList = Node.get(_this.editor, selectedListPath); // get the node and path of parent of selected list
27
29
 
28
- var listParent = Node.parent(_this.editor, selectedListPath);
29
- var listParentPath = selectedListPath.slice(0, selectedListPath.length - 1); // unwrap list of selected list item
30
- // if the type of parent of selected list is a list and it should be a list item
30
+ var listParent = Node.parent(_this.editor, selectedListPath);
31
+ var listParentPath = selectedListPath.slice(0, selectedListPath.length - 1); // unwrap list of selected list item
32
+ // if the type of parent of selected list is a list and it should be a list item
31
33
 
32
- if (listParent.type && listParent.type.includes('list')) {
33
- // get the path of parent of listParent
34
- var ancesstorPath = listParentPath.slice(0, listParentPath.length - 1);
35
- var lastSelectedListItem = selectedListItem.slice(-1)[0];
36
- var lastSelectedListItemPath = lastSelectedListItem[1];
37
- var lastSelectedListItemIndex = lastSelectedListItemPath.slice(-1)[0];
38
- var unSelectedListItem = selectedList.children.slice(selectedListItem.slice(-1)[0][1].slice(-1)[0] + 1); // insert a new list to the last selected list item
34
+ if (listParent.type && listParent.type.includes('list')) {
35
+ // get the path of parent of listParent
36
+ var ancesstorPath = listParentPath.slice(0, listParentPath.length - 1);
37
+ var lastSelectedListItem = selectedListItem.slice(-1)[0];
38
+ var lastSelectedListItemPath = lastSelectedListItem[1];
39
+ var lastSelectedListItemIndex = lastSelectedListItemPath.slice(-1)[0];
40
+ var unSelectedListItem = selectedList.children.slice(selectedListItem.slice(-1)[0][1].slice(-1)[0] + 1); // insert a new list to the last selected list item
39
41
 
40
- if (!(lastSelectedListItemIndex === selectedList.children.length - 1)) {
41
- SfEditor.insertNodes(_this.editor, {
42
- type: selectedList.type,
43
- children: []
44
- }, {
45
- at: [].concat(_toConsumableArray(lastSelectedListItemPath), [lastSelectedListItem[0].children.length])
46
- });
47
- } // move the unselected list item to the new created list
42
+ if (!(lastSelectedListItemIndex === selectedList.children.length - 1)) {
43
+ SfEditor.insertNodes(_this.editor, {
44
+ type: selectedList.type,
45
+ children: []
46
+ }, {
47
+ at: [].concat(_toConsumableArray(lastSelectedListItemPath), [lastSelectedListItem[0].children.length])
48
+ });
49
+ } // move the unselected list item to the new created list
48
50
 
49
51
 
50
- unSelectedListItem.forEach(function (item, index) {
51
- SfEditor.moveNodes(_this.editor, {
52
- at: [].concat(_toConsumableArray(lastSelectedListItemPath.slice(0, -1)), [lastSelectedListItemIndex + 1]),
53
- to: [].concat(_toConsumableArray(lastSelectedListItemPath), [lastSelectedListItem[0].children.length, index])
52
+ unSelectedListItem.forEach(function (item, index) {
53
+ SfEditor.moveNodes(_this.editor, {
54
+ at: [].concat(_toConsumableArray(lastSelectedListItemPath.slice(0, -1)), [lastSelectedListItemIndex + 1]),
55
+ to: [].concat(_toConsumableArray(lastSelectedListItemPath), [lastSelectedListItem[0].children.length, index])
56
+ });
54
57
  });
55
- });
56
- var currentItemPath = firstSelectedItemPath;
57
- var startIndexOfItemInNewList = listParentPath[listParentPath.length - 1] + 1; // move item to outer list by path
58
-
59
- selectedListItem.forEach(function (item, index) {
60
- var itemTargetPath = [].concat(_toConsumableArray(ancesstorPath), [index + startIndexOfItemInNewList]);
61
- SfEditor.moveNodes(_this.editor, {
62
- at: currentItemPath,
63
- to: itemTargetPath
58
+ var currentItemPath = firstSelectedItemPath;
59
+ var startIndexOfItemInNewList = listParentPath[listParentPath.length - 1] + 1; // move item to outer list by path
60
+
61
+ selectedListItem.forEach(function (item, index) {
62
+ var itemTargetPath = [].concat(_toConsumableArray(ancesstorPath), [index + startIndexOfItemInNewList]);
63
+ SfEditor.moveNodes(_this.editor, {
64
+ at: currentItemPath,
65
+ to: itemTargetPath
66
+ });
64
67
  });
65
- });
66
- var currentList = Node.get(_this.editor, selectedListPath); // delete empty list
68
+ var currentList = Node.get(_this.editor, selectedListPath); // delete empty list
67
69
 
68
- if (!currentList.children[0] || currentList.children[0].type !== 'list_item') {
69
- SfEditor.removeNodes(_this.editor, {
70
- at: selectedListPath
70
+ if (!currentList.children[0] || currentList.children[0].type !== 'list_item') {
71
+ SfEditor.removeNodes(_this.editor, {
72
+ at: selectedListPath
73
+ });
74
+ }
75
+ } else {
76
+ // unwrap list item if the selected list is the outest of the root
77
+ var firstListItemIndex = firstSelectedItem[1].slice(-1)[0];
78
+ var restCount = 0;
79
+ selectedListItem.forEach(function (item) {
80
+ SfEditor.unwrapNodesByTypeAtRange(_this.editor, {
81
+ match: {
82
+ type: 'list_item'
83
+ },
84
+ mode: 'highest',
85
+ at: [].concat(_toConsumableArray(selectedListPath), [firstListItemIndex + restCount])
86
+ });
87
+ restCount += item[0].children.length;
71
88
  });
72
- }
73
- } else {
74
- // unwrap list item if the selected list is the outest of the root
75
- SfEditor.unwrapNodesByTypeAtRange(_this.editor, {
76
- match: [{
77
- type: selectedList.type
78
- }],
79
- split: true,
80
- mode: 'highest'
81
- });
82
- var firstListItemIndex = firstSelectedItem[1].slice(-1)[0];
83
-
84
- var _listParentPath = Path.parent(selectedListPath);
85
-
86
- var selectedListIndex = selectedListPath.slice(-1)[0];
87
- var startIndex = firstListItemIndex === 0 ? selectedListIndex : selectedListIndex + 1;
88
- selectedListItem.forEach(function (item, index) {
89
+ var startItemRange = Editor.range(_this.editor, [].concat(_toConsumableArray(selectedListPath), [firstListItemIndex]));
90
+ var endItemRange = Editor.range(_this.editor, [].concat(_toConsumableArray(selectedListPath), [firstListItemIndex + restCount - 1]));
89
91
  SfEditor.unwrapNodesByTypeAtRange(_this.editor, {
90
- match: {
91
- type: 'list_item'
92
- },
92
+ match: [{
93
+ type: selectedList.type
94
+ }],
95
+ split: true,
93
96
  mode: 'highest',
94
- at: [].concat(_toConsumableArray(_listParentPath), [startIndex + index])
97
+ at: {
98
+ anchor: startItemRange.anchor,
99
+ focus: endItemRange.focus
100
+ }
95
101
  });
96
- });
97
- }
102
+ }
103
+ });
98
104
  };
99
105
 
100
106
  this.wrapList = function (type) {
101
107
  normailizeSelection(_this.editor);
102
- var nodes = SfEditor.nodes(_this.editor, {
103
- match: 'block'
104
- });
105
- SfEditor.wrapNodes(_this.editor, {
106
- type: type,
107
- children: []
108
- }, {
109
- split: false
110
- }); // Get selected block node from an iterator.
111
- // Modified according to the function *nodes from slate/src/interface/node.ts
112
-
113
- var items = Array.from(nodes); // wrap selected nodes into list_item
114
-
115
- for (var _i = 0, _items = items; _i < _items.length; _i++) {
116
- var _items$_i = _slicedToArray(_items[_i], 2),
117
- node = _items$_i[0],
118
- path = _items$_i[1];
119
-
120
- SfEditor.wrapNodes(_this.editor, {
121
- type: 'list_item',
122
- children: [node],
123
- data: {}
124
- }, {
125
- at: path
108
+ var selection = _this.editor.selection;
109
+ var focusPath = selection.focus.path;
110
+ var anchorPath = selection.anchor.path;
111
+
112
+ if (Path.equals(focusPath, anchorPath)) {
113
+ var _SfEditor$match = SfEditor.match(_this.editor, _this.editor.selection, 'block'),
114
+ _SfEditor$match2 = _slicedToArray(_SfEditor$match, 2),
115
+ block = _SfEditor$match2[0],
116
+ path = _SfEditor$match2[1];
117
+
118
+ Editor.withoutNormalizing(_this.editor, function (editor) {
119
+ SfEditor.wrapNodes(_this.editor, {
120
+ type: 'list_item',
121
+ children: [block],
122
+ data: {}
123
+ }, {
124
+ at: path
125
+ });
126
+ SfEditor.wrapNodes(_this.editor, {
127
+ type: type,
128
+ children: []
129
+ }, {
130
+ split: false,
131
+ at: path
132
+ });
126
133
  });
134
+ return;
127
135
  }
136
+
137
+ var commonPath = Path.common(focusPath, anchorPath);
138
+ var commonAncestor = Node.get(_this.editor, commonPath);
139
+ var startIndex = anchorPath[commonPath.length];
140
+ var endIndex = focusPath[commonPath.length]; // get hightest selected commonAncestor block items
141
+
142
+ var selectedBlock = commonAncestor.children.slice(startIndex, endIndex + 1);
143
+ Editor.withoutNormalizing(_this.editor, function (editor) {
144
+ selectedBlock.forEach(function (blockItem, index) {
145
+ // wrap block into list
146
+ if (!blockItem.type.includes('list')) {
147
+ SfEditor.wrapNodes(_this.editor, {
148
+ type: 'list_item',
149
+ children: [blockItem],
150
+ data: {}
151
+ }, {
152
+ at: [].concat(_toConsumableArray(commonPath), [startIndex + index])
153
+ });
154
+ SfEditor.wrapNodes(_this.editor, {
155
+ type: type,
156
+ children: []
157
+ }, {
158
+ split: false,
159
+ at: [].concat(_toConsumableArray(commonPath), [startIndex + index])
160
+ });
161
+ }
162
+ });
163
+ });
128
164
  };
129
165
 
130
166
  this.getCurrentListItem = function () {
@@ -224,34 +260,6 @@ var LitsUtils = function LitsUtils(editor) {
224
260
  return [[commonAncestor, commonPath]];
225
261
  }
226
262
 
227
- if (commonPath.length === 0) {
228
- var listNodes = SfEditor.nodes(_this.editor, {
229
- match: [{
230
- type: 'list_item'
231
- }, {
232
- type: 'list_item'
233
- }],
234
- mode: 'highest'
235
- });
236
- var list = [];
237
-
238
- var _iterator2 = _createForOfIteratorHelper(listNodes),
239
- _step2;
240
-
241
- try {
242
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
243
- var iterator = _step2.value;
244
- list.push(iterator);
245
- }
246
- } catch (err) {
247
- _iterator2.e(err);
248
- } finally {
249
- _iterator2.f();
250
- }
251
-
252
- return list;
253
- }
254
-
255
263
  return [];
256
264
  };
257
265
 
@@ -263,19 +271,19 @@ var LitsUtils = function LitsUtils(editor) {
263
271
  });
264
272
  var node; //get the nearest list_item of current selection
265
273
 
266
- var _iterator3 = _createForOfIteratorHelper(listNodes),
267
- _step3;
274
+ var _iterator2 = _createForOfIteratorHelper(listNodes),
275
+ _step2;
268
276
 
269
277
  try {
270
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
271
- var item = _step3.value;
278
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
279
+ var item = _step2.value;
272
280
  node = item;
273
281
  } // if current list item is the first child of the list return
274
282
 
275
283
  } catch (err) {
276
- _iterator3.e(err);
284
+ _iterator2.e(err);
277
285
  } finally {
278
- _iterator3.f();
286
+ _iterator2.f();
279
287
  }
280
288
 
281
289
  var listItemPath = _toConsumableArray(node[1]);
@@ -287,18 +295,21 @@ var LitsUtils = function LitsUtils(editor) {
287
295
  var previousListItem = listNode.children[listItemIndex - 1];
288
296
  var previousListItemPath = [].concat(_toConsumableArray(listNodePath), [listItemIndex - 1]);
289
297
  var lastIndex = previousListItem.children.length;
290
- var newListPath = [].concat(_toConsumableArray(previousListItemPath), [lastIndex]);
291
- SfEditor.insertNodes(_this.editor, {
292
- type: listNode.type,
293
- children: []
294
- }, {
295
- at: newListPath,
296
- split: true
297
- });
298
- var newListItemPath = [].concat(_toConsumableArray(newListPath), [0]);
299
- SfEditor.moveNodes(_this.editor, {
300
- at: node[1],
301
- to: newListItemPath
298
+ var newListPath = [].concat(_toConsumableArray(previousListItemPath), [lastIndex]); // Deferring normalization list untils after operations completes.
299
+
300
+ SfEditor.withoutNormalizing(_this.editor, function () {
301
+ SfEditor.insertNodes(_this.editor, {
302
+ type: listNode.type,
303
+ children: []
304
+ }, {
305
+ at: newListPath,
306
+ split: true
307
+ });
308
+ var newListItemPath = [].concat(_toConsumableArray(newListPath), [0]);
309
+ SfEditor.moveNodes(_this.editor, {
310
+ at: node[1],
311
+ to: newListItemPath
312
+ });
302
313
  });
303
314
  };
304
315
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seafile/seafile-editor",
3
- "version": "0.3.79",
3
+ "version": "0.3.80",
4
4
  "private": false,
5
5
  "dependencies": {
6
6
  "@seafile/slate-react": "^0.54.13",