diff --git a/lib/onEnter.js b/lib/onEnter.js index a1b7b9e4..86ba9ea5 100644 --- a/lib/onEnter.js +++ b/lib/onEnter.js @@ -12,32 +12,35 @@ const getItemDepth = require('./getItemDepth'); * Shift+Enter in a list item should make a new line */ function onEnter(event, data, state, opts) { - const { startBlock } = state; - - if (!getCurrentItem(opts, state)) { - return null; - } - // Pressing Shift+Enter // should split block normally if (data.isShift) { return null; } + const currentItem = getCurrentItem(opts, state); + + // Not in a list + if (!currentItem) { + return null; + } + event.preventDefault(); - if (startBlock.length > 0) { + const isEmpty = currentItem.nodes.size <= 1 && currentItem.length === 0; + if (isEmpty) { + // Block is empty, we exit the list + if (getItemDepth(opts, state) > 1) { + return decreaseItemDepth(opts, state.transform()).apply(); + } else { + // Exit list + return unwrapList(opts, state.transform()).apply(); + } + } else { // Split list item return splitListItem(opts, state.transform()).apply(); } - // Block is empty, we exit the list - if (getItemDepth(opts, state) > 1) { - return decreaseItemDepth(opts, state.transform()).apply(); - } else { - // Exit list - return unwrapList(opts, state.transform()).apply(); - } } module.exports = onEnter; diff --git a/tests/enter-empty-block-in-item/expected.yaml b/tests/enter-empty-block-in-item/expected.yaml new file mode 100644 index 00000000..a95e89c7 --- /dev/null +++ b/tests/enter-empty-block-in-item/expected.yaml @@ -0,0 +1,33 @@ +nodes: +- kind: block + type: ul_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: 'First item' + - kind: block + type: list_item + nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: 'A first paragraph' + - kind: block + type: paragraph + nodes: + - kind: text + text: '' + - kind: block + type: list_item + nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: '' diff --git a/tests/enter-empty-block-in-item/input.yaml b/tests/enter-empty-block-in-item/input.yaml new file mode 100644 index 00000000..a8a63c2b --- /dev/null +++ b/tests/enter-empty-block-in-item/input.yaml @@ -0,0 +1,36 @@ + +document: + nodes: + - kind: block + type: ul_list + nodes: + - kind: block + type: list_item + nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: 'First item' + + - kind: block + type: list_item + nodes: + - kind: block + type: paragraph + nodes: + - kind: text + text: 'A first paragraph' + - kind: block + type: paragraph + nodes: + - kind: text + key: 'start' + text: '' + +selection: + anchorKey: 'start' + anchorOffset: 0 + focusKey: 'start' + focusOffset: 0 + isFocused: true diff --git a/tests/enter-empty-block-in-item/transform.js b/tests/enter-empty-block-in-item/transform.js new file mode 100644 index 00000000..4f2dc525 --- /dev/null +++ b/tests/enter-empty-block-in-item/transform.js @@ -0,0 +1,11 @@ + +module.exports = function(plugin, state) { + return plugin.onKeyDown( + { + preventDefault: () => {}, + stopPropagation: () => {} + }, + { key: 'enter' }, + state + ); +};