@seafile/seafile-editor 0.3.76 → 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,116 +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
- },
93
- at: [].concat(_toConsumableArray(_listParentPath), [startIndex + index])
92
+ match: [{
93
+ type: selectedList.type
94
+ }],
95
+ split: true,
96
+ mode: 'highest',
97
+ at: {
98
+ anchor: startItemRange.anchor,
99
+ focus: endItemRange.focus
100
+ }
94
101
  });
95
- });
96
- }
102
+ }
103
+ });
97
104
  };
98
105
 
99
106
  this.wrapList = function (type) {
100
107
  normailizeSelection(_this.editor);
101
- var nodes = SfEditor.nodes(_this.editor, {
102
- match: 'block'
103
- });
104
- SfEditor.wrapNodes(_this.editor, {
105
- type: type,
106
- children: []
107
- }, {
108
- split: false
109
- }); // Get selected block node from an iterator.
110
- // Modified according to the function *nodes from slate/src/interface/node.ts
111
-
112
- var items = Array.from(nodes); // wrap selected nodes into list_item
113
-
114
- for (var _i = 0, _items = items; _i < _items.length; _i++) {
115
- var _items$_i = _slicedToArray(_items[_i], 2),
116
- node = _items$_i[0],
117
- path = _items$_i[1];
118
-
119
- SfEditor.wrapNodes(_this.editor, {
120
- type: 'list_item',
121
- children: [node],
122
- data: {}
123
- }, {
124
- 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
+ });
125
133
  });
134
+ return;
126
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
+ });
127
164
  };
128
165
 
129
166
  this.getCurrentListItem = function () {
@@ -223,34 +260,6 @@ var LitsUtils = function LitsUtils(editor) {
223
260
  return [[commonAncestor, commonPath]];
224
261
  }
225
262
 
226
- if (commonPath.length === 0) {
227
- var listNodes = SfEditor.nodes(_this.editor, {
228
- match: [{
229
- type: 'list_item'
230
- }, {
231
- type: 'list_item'
232
- }],
233
- mode: 'highest'
234
- });
235
- var list = [];
236
-
237
- var _iterator2 = _createForOfIteratorHelper(listNodes),
238
- _step2;
239
-
240
- try {
241
- for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
242
- var iterator = _step2.value;
243
- list.push(iterator);
244
- }
245
- } catch (err) {
246
- _iterator2.e(err);
247
- } finally {
248
- _iterator2.f();
249
- }
250
-
251
- return list;
252
- }
253
-
254
263
  return [];
255
264
  };
256
265
 
@@ -262,19 +271,19 @@ var LitsUtils = function LitsUtils(editor) {
262
271
  });
263
272
  var node; //get the nearest list_item of current selection
264
273
 
265
- var _iterator3 = _createForOfIteratorHelper(listNodes),
266
- _step3;
274
+ var _iterator2 = _createForOfIteratorHelper(listNodes),
275
+ _step2;
267
276
 
268
277
  try {
269
- for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
270
- var item = _step3.value;
278
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
279
+ var item = _step2.value;
271
280
  node = item;
272
281
  } // if current list item is the first child of the list return
273
282
 
274
283
  } catch (err) {
275
- _iterator3.e(err);
284
+ _iterator2.e(err);
276
285
  } finally {
277
- _iterator3.f();
286
+ _iterator2.f();
278
287
  }
279
288
 
280
289
  var listItemPath = _toConsumableArray(node[1]);
@@ -286,18 +295,21 @@ var LitsUtils = function LitsUtils(editor) {
286
295
  var previousListItem = listNode.children[listItemIndex - 1];
287
296
  var previousListItemPath = [].concat(_toConsumableArray(listNodePath), [listItemIndex - 1]);
288
297
  var lastIndex = previousListItem.children.length;
289
- var newListPath = [].concat(_toConsumableArray(previousListItemPath), [lastIndex]);
290
- SfEditor.insertNodes(_this.editor, {
291
- type: listNode.type,
292
- children: []
293
- }, {
294
- at: newListPath,
295
- split: true
296
- });
297
- var newListItemPath = [].concat(_toConsumableArray(newListPath), [0]);
298
- SfEditor.moveNodes(_this.editor, {
299
- at: node[1],
300
- 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
+ });
301
313
  });
302
314
  };
303
315
 
@@ -308,6 +320,21 @@ var LitsUtils = function LitsUtils(editor) {
308
320
  return false;
309
321
  };
310
322
 
323
+ this.getNormalizedListItems = function (list) {
324
+ var listItems = [];
325
+ list.children.forEach(function (listItem) {
326
+ var children = listItem.children || [];
327
+ var firstChild = children[0];
328
+
329
+ if (firstChild && firstChild.type.includes('_list')) {
330
+ listItems.push.apply(listItems, _toConsumableArray(_this.getNormalizedListItems(firstChild)));
331
+ } else {
332
+ listItems.push(listItem);
333
+ }
334
+ });
335
+ return listItems;
336
+ };
337
+
311
338
  this.pasteContentInList = function (command) {
312
339
  var data = command.data;
313
340
 
@@ -340,7 +367,8 @@ var LitsUtils = function LitsUtils(editor) {
340
367
  var listPath = currentItemPath.slice(0, currentItemPath.length - 1);
341
368
 
342
369
  if (firstBlockType === 'ordered_list' || firstBlockType === 'unordered_list') {
343
- var listItems = firstBlock.children;
370
+ var listItems = _this.getNormalizedListItems(firstBlock);
371
+
344
372
  listItems.forEach(function (listItem, index) {
345
373
  var itemChildren = listItem.children;
346
374
  SfEditor.insertFragment(_this.editor, [itemChildren[0]]);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seafile/seafile-editor",
3
- "version": "0.3.76",
3
+ "version": "0.3.80",
4
4
  "private": false,
5
5
  "dependencies": {
6
6
  "@seafile/slate-react": "^0.54.13",