UNPKG

1.22 kBJavaScriptView Raw
1'use strict';
2
3var unwrapList = require('./changes/unwrapList');
4var splitListItem = require('./changes/splitListItem');
5var decreaseItemDepth = require('./changes/decreaseItemDepth');
6var getCurrentItem = require('./getCurrentItem');
7var getItemDepth = require('./getItemDepth');
8
9/**
10 * User pressed Enter in an editor
11 *
12 * Enter in a list item should split the list item
13 * Enter in an empty list item should remove it
14 * Shift+Enter in a list item should make a new line
15 */
16function onEnter(event, change, editor, opts) {
17 // Pressing Shift+Enter
18 // should split block normally
19 if (event.shiftKey) {
20 return null;
21 }
22
23 var value = change.value;
24
25 var currentItem = getCurrentItem(opts, value);
26
27 // Not in a list
28 if (!currentItem) {
29 return null;
30 }
31
32 event.preventDefault();
33 if (currentItem.isEmpty) {
34 // Block is empty, we exit the list
35 if (getItemDepth(opts, value) > 1) {
36 return decreaseItemDepth(opts, change);
37 } else {
38 // Exit list
39 return unwrapList(opts, change);
40 }
41 } else {
42 // Split list item
43 return splitListItem(opts, change);
44 }
45}
46
47module.exports = onEnter;
\No newline at end of file