Skip to content

Commit

Permalink
Merge pull request #1162 from yabwe/create-paragraph-on-enter-in-bloc…
Browse files Browse the repository at this point in the history
…kquote

when caret is at the end of blockquote and you press enter, create paragraph, not another blockquote
  • Loading branch information
nmielnik committed Jul 30, 2016
2 parents 77e941a + e497496 commit 80356a4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
29 changes: 29 additions & 0 deletions spec/content.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,35 @@ describe('Content TestCase', function () {
});
expect(document.execCommand).toHaveBeenCalledWith('unlink', false, null);
});

describe('within a blockquote element', function () {

it('at the end of the blockquote, p tag should be created next, not blockquote', function () {
this.el.innerHTML = '<blockquote>lorem ipsum</blockquote>';
var editor = this.newMediumEditor('.editor'),
target = editor.elements[0].querySelector('blockquote').firstChild;

placeCursorInsideElement(target, target.textContent.length);
fireEvent(target, 'keydown', {
keyCode: MediumEditor.util.keyCode.ENTER
});

expect(this.el.innerHTML).toBe('<blockquote>lorem ipsum</blockquote><p><br></p>');
});

it('NOT at the start of the blockqoute, no formatting should be changed', function () {
this.el.innerHTML = '<blockquote>lorem ipsum</blockquote>';
var editor = this.newMediumEditor('.editor'),
target = editor.elements[0].querySelector('blockquote').firstChild;

placeCursorInsideElement(target, 0);
fireEvent(target, 'keydown', {
keyCode: MediumEditor.util.keyCode.ENTER
});

expect(this.el.innerHTML).toBe('<blockquote>lorem ipsum</blockquote>');
});
});
});

describe('when the ctrl key and m key is pressed', function () {
Expand Down
14 changes: 14 additions & 0 deletions src/js/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@
// then pressing backspace key should change the <blockquote> to a <p> tag
event.preventDefault();
MediumEditor.util.execFormatBlock(this.options.ownerDocument, 'p');
} else if (MediumEditor.util.isKey(event, MediumEditor.util.keyCode.ENTER) &&
(MediumEditor.util.getClosestTag(node, 'blockquote') !== false) &&
MediumEditor.selection.getCaretOffsets(node).right === 0) {

// when cursor is at the end of <blockquote>,
// then pressing enter key should create <p> tag, not <blockquote>
p = this.options.ownerDocument.createElement('p');
p.innerHTML = '<br>';
node.parentElement.insertBefore(p, node.nextSibling);

// move the cursor into the new paragraph
MediumEditor.selection.moveCursor(this.options.ownerDocument, p);

event.preventDefault();
}
}

Expand Down

0 comments on commit 80356a4

Please sign in to comment.