Skip to content

Commit

Permalink
remove first empty paragraph on backspace
Browse files Browse the repository at this point in the history
  • Loading branch information
linkesch committed Aug 19, 2016
1 parent cd6c109 commit a255915
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
16 changes: 16 additions & 0 deletions spec/content.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,22 @@ describe('Content TestCase', function () {
expect(this.el.innerHTML).toBe('<p>lorem ipsum</p><ul><li></li><li>lorem ipsum</li></ul>');
});
});

describe('within an empty paragraph which is the first element of the editor', function () {
it('should delete the paragraph and place the caret to the next paragraph', function () {
this.el.innerHTML = '<p class=""><br></p><p>test</p>';
var editor = this.newMediumEditor('.editor'),
target = editor.elements[0].querySelector('p'),
range;
placeCursorInsideElement(target, 0);
fireEvent(target, 'keydown', {
keyCode: MediumEditor.util.keyCode.BACKSPACE
});
expect(this.el.innerHTML).toBe('<p>test</p>');
range = document.getSelection().getRangeAt(0);
expect(range.commonAncestorContainer.textContent).toBe('test');
});
});
});

describe('with header tags', function () {
Expand Down
11 changes: 11 additions & 0 deletions src/js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,17 @@
MediumEditor.selection.moveCursor(this.options.ownerDocument, p);

event.preventDefault();
} else if (MediumEditor.util.isKey(event, MediumEditor.util.keyCode.BACKSPACE) &&
MediumEditor.util.isMediumEditorElement(node.parentElement) &&
!node.previousElementSibling &&
node.nextElementSibling &&
isEmpty.test(node.innerHTML)) {

// when cursor is in the first element, it's empty and user presses backspace,
// do delete action instead to get rid of the first element and move caret to 2nd
event.preventDefault();
MediumEditor.selection.moveCursor(this.options.ownerDocument, node.nextSibling);
node.parentElement.removeChild(node);
}
}

Expand Down

0 comments on commit a255915

Please sign in to comment.