Skip to content
This repository has been archived by the owner on Feb 21, 2019. It is now read-only.

Commit

Permalink
Fix #37 Enter in empty block of list item (#39)
Browse files Browse the repository at this point in the history
* Add failing tests for #37

* Fix Enter in empty block of list item

Fixes #37
  • Loading branch information
Soreine authored Feb 10, 2017
1 parent 94c29ad commit 95d758a
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 14 deletions.
31 changes: 17 additions & 14 deletions lib/onEnter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
33 changes: 33 additions & 0 deletions tests/enter-empty-block-in-item/expected.yaml
Original file line number Diff line number Diff line change
@@ -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: ''
36 changes: 36 additions & 0 deletions tests/enter-empty-block-in-item/input.yaml
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions tests/enter-empty-block-in-item/transform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

module.exports = function(plugin, state) {
return plugin.onKeyDown(
{
preventDefault: () => {},
stopPropagation: () => {}
},
{ key: 'enter' },
state
);
};

0 comments on commit 95d758a

Please sign in to comment.