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

Commit 329e21a

Browse files
committed
changed to always handle enter
1 parent 93ce9da commit 329e21a

File tree

3 files changed

+32
-30
lines changed

3 files changed

+32
-30
lines changed

src/__test__/plugin.test.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,10 @@ describe("draft-js-markdown-plugin", () => {
104104
});
105105
describe("handleReturn", () => {
106106
beforeEach(() => {
107-
subject = () => {
108-
event = new window.KeyboardEvent("keydown", { which: 13 });
109-
jest.spyOn(event, "preventDefault");
110-
return plugin.keyBindingFn(event, store);
111-
};
107+
subject = () =>
108+
plugin.handleReturn(event, store.getEditorState(), store);
112109
});
113-
it("does not handle", () => {
110+
it("does always handle", () => {
114111
currentRawContentState = {
115112
entityMap: {},
116113
blocks: [
@@ -125,7 +122,7 @@ describe("draft-js-markdown-plugin", () => {
125122
},
126123
],
127124
};
128-
expect(subject()).toBe(null);
125+
expect(subject()).toBe("handled");
129126
expect(modifierSpy).not.toHaveBeenCalledTimes(1);
130127
expect(store.setEditorState).not.toHaveBeenCalled();
131128
});

src/index.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@ import {
66
CHECKABLE_LIST_ITEM,
77
} from "draft-js-checkable-list-item";
88

9-
import { Map } from "immutable";
10-
import { getDefaultKeyBinding, Modifier, EditorState } from "draft-js";
9+
import { Map, OrderedSet } from "immutable";
10+
import {
11+
getDefaultKeyBinding,
12+
Modifier,
13+
EditorState,
14+
RichUtils,
15+
DefaultDraftInlineStyle,
16+
} from "draft-js";
1117
import adjustBlockDepth from "./modifiers/adjustBlockDepth";
1218
import handleBlockType from "./modifiers/handleBlockType";
1319
import handleInlineStyle from "./modifiers/handleInlineStyle";
1420
import handleNewCodeBlock from "./modifiers/handleNewCodeBlock";
21+
import resetInlineStyle from "./modifiers/resetInlineStyle";
1522
import insertEmptyBlock from "./modifiers/insertEmptyBlock";
1623
import handleLink from "./modifiers/handleLink";
1724
import handleImage from "./modifiers/handleImage";
@@ -152,31 +159,24 @@ const createMarkdownPlugin = (config = {}) => {
152159
}
153160
return "not-handled";
154161
},
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-
}
162+
handleReturn(ev, editorState, { setEditorState }) {
163+
let newEditorState = checkReturnForState(editorState, ev);
164+
let selection = newEditorState.getSelection();
163165

164-
newEditorState = checkCharacterForState(editorState, "");
165-
if (!inCodeBlock(editorState) && editorState !== newEditorState) {
166-
const contentState = Modifier.splitBlock(
167-
newEditorState.getCurrentContent(),
168-
newEditorState.getSelection()
169-
);
166+
newEditorState = checkCharacterForState(newEditorState, "");
167+
let content = newEditorState.getCurrentContent();
170168

171-
setEditorState(
172-
EditorState.push(newEditorState, contentState, "split-block")
173-
);
169+
content = Modifier.splitBlock(content, selection);
174170

175-
return "handled";
176-
}
177-
}
171+
setEditorState(
172+
EditorState.push(
173+
resetInlineStyle(newEditorState),
174+
content,
175+
"split-block"
176+
)
177+
);
178178

179-
return getDefaultKeyBinding(ev);
179+
return "handled";
180180
},
181181
handleBeforeInput(character, editorState, { setEditorState }) {
182182
if (character !== " ") {

src/modifiers/resetInlineStyle.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { OrderedSet } from "immutable";
2+
import { EditorState } from "draft-js";
3+
4+
export default editorState =>
5+
EditorState.setInlineStyleOverride(editorState, OrderedSet());

0 commit comments

Comments
 (0)