Skip to content

Commit 9586f6b

Browse files
authored
Merge pull request #38 from troutowicz/header-return-fix
Only create new line after header on EOL return
2 parents 655c2d3 + 666e618 commit 9586f6b

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

src/__test__/plugin-test.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,21 @@ describe('draft-js-markdown-shortcuts-plugin', () => {
150150
data: {}
151151
}]
152152
};
153+
currentSelectionState = new SelectionState({
154+
anchorKey: 'item1',
155+
anchorOffset: 5,
156+
focusKey: 'item1',
157+
focusOffset: 5,
158+
isBackward: false,
159+
hasFocus: true
160+
});
153161
expect(subject()).to.equal('handled');
154162
expect(modifierSpy).to.have.been.calledOnce();
155163
expect(store.setEditorState).to.have.been.calledWith(newEditorState);
156164
};
157165
['one', 'two', 'three', 'four', 'five', 'six'].forEach((level) => {
158166
describe(`on header-${level}`, () => {
159-
it('inserts new empty block', testInsertNewBlock(`header-${level}`));
167+
it('inserts new empty block on end of header return', testInsertNewBlock(`header-${level}`));
160168
});
161169
});
162170
['ctrlKey', 'shiftKey', 'metaKey', 'altKey'].forEach((key) => {

src/index.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,9 @@ function checkReturnForState(editorState, ev) {
4949
if (/-list-item$/.test(type) && text === '') {
5050
newEditorState = leaveList(editorState);
5151
}
52-
if (newEditorState === editorState &&
53-
(ev.ctrlKey || ev.shiftKey || ev.metaKey || ev.altKey || /^header-/.test(type))) {
52+
if (newEditorState === editorState
53+
&& (ev.ctrlKey || ev.shiftKey || ev.metaKey || ev.altKey
54+
|| (/^header-/.test(type) && selection.isCollapsed() && selection.getEndOffset() === text.length))) {
5455
newEditorState = insertEmptyBlock(editorState);
5556
}
5657
if (newEditorState === editorState && type !== 'code-block' && /^```([\w-]+)?$/.test(text)) {

0 commit comments

Comments
 (0)