Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.

Commit 93ce9da

Browse files
committed
- use keyBindingFn for handling enter - so that fallback resolves without interference
1 parent 65e6779 commit 93ce9da

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

src/__test__/plugin.test.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import sinon from "sinon";
21
import Draft, { EditorState, SelectionState, ContentBlock } from "draft-js";
32
import {
43
CheckableListItem,
@@ -105,8 +104,11 @@ describe("draft-js-markdown-plugin", () => {
105104
});
106105
describe("handleReturn", () => {
107106
beforeEach(() => {
108-
subject = () =>
109-
plugin.handleReturn(event, store.getEditorState(), store);
107+
subject = () => {
108+
event = new window.KeyboardEvent("keydown", { which: 13 });
109+
jest.spyOn(event, "preventDefault");
110+
return plugin.keyBindingFn(event, store);
111+
};
110112
});
111113
it("does not handle", () => {
112114
currentRawContentState = {
@@ -123,7 +125,7 @@ describe("draft-js-markdown-plugin", () => {
123125
},
124126
],
125127
};
126-
expect(subject()).toBe("not-handled");
128+
expect(subject()).toBe(null);
127129
expect(modifierSpy).not.toHaveBeenCalledTimes(1);
128130
expect(store.setEditorState).not.toHaveBeenCalled();
129131
});

src/index.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from "draft-js-checkable-list-item";
88

99
import { Map } from "immutable";
10-
10+
import { getDefaultKeyBinding, Modifier, EditorState } from "draft-js";
1111
import adjustBlockDepth from "./modifiers/adjustBlockDepth";
1212
import handleBlockType from "./modifiers/handleBlockType";
1313
import handleInlineStyle from "./modifiers/handleInlineStyle";
@@ -152,23 +152,31 @@ const createMarkdownPlugin = (config = {}) => {
152152
}
153153
return "not-handled";
154154
},
155-
handleReturn(ev, editorState, { setEditorState }) {
156-
let newEditorState = checkReturnForState(editorState, ev);
157-
if (editorState !== newEditorState) {
158-
setEditorState(newEditorState);
159-
return "handled";
160-
}
155+
keyBindingFn(ev, { getEditorState, setEditorState }) {
156+
if (ev.which === 13) {
157+
const editorState = getEditorState();
158+
let newEditorState = checkReturnForState(editorState, ev);
159+
if (editorState !== newEditorState) {
160+
setEditorState(newEditorState);
161+
return "handled";
162+
}
161163

162-
// If we're in a code block don't add markdown to it
163-
if (inCodeBlock(editorState)) return "not-handled";
164+
newEditorState = checkCharacterForState(editorState, "");
165+
if (!inCodeBlock(editorState) && editorState !== newEditorState) {
166+
const contentState = Modifier.splitBlock(
167+
newEditorState.getCurrentContent(),
168+
newEditorState.getSelection()
169+
);
164170

165-
newEditorState = checkCharacterForState(editorState, "\n");
166-
if (editorState !== newEditorState) {
167-
setEditorState(newEditorState);
168-
return "handled";
171+
setEditorState(
172+
EditorState.push(newEditorState, contentState, "split-block")
173+
);
174+
175+
return "handled";
176+
}
169177
}
170178

171-
return "not-handled";
179+
return getDefaultKeyBinding(ev);
172180
},
173181
handleBeforeInput(character, editorState, { setEditorState }) {
174182
if (character !== " ") {

src/modifiers/changeCurrentInlineStyle.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const changeCurrentInlineStyle = (editorState, matchArr, style, character) => {
2727
wordSelection.merge({
2828
anchorOffset: wordSelection.getFocusOffset() - markdownCharacterLength,
2929
}),
30-
character || " "
30+
character == null ? " " : character
3131
);
3232

3333
let afterSelection = newContentState.getSelectionAfter();

0 commit comments

Comments
 (0)