Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove first empty paragraph on backspace #1187

Merged
merged 1 commit into from
Aug 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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