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

Commit 94f9a58

Browse files
authored
Merge pull request #11 from withspectrum/enter-on-return
Insert markdown on enter
2 parents bf27f6e + a670dd4 commit 94f9a58

File tree

4 files changed

+51
-9
lines changed

4 files changed

+51
-9
lines changed

src/index.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,28 @@ const createMarkdownPlugin = (config = {}) => {
140140
return "not-handled";
141141
},
142142
handleReturn(ev, editorState, { setEditorState }) {
143-
const newEditorState = checkReturnForState(editorState, ev);
143+
let newEditorState = checkReturnForState(editorState, ev);
144144
if (editorState !== newEditorState) {
145145
setEditorState(newEditorState);
146146
return "handled";
147147
}
148+
149+
// If we're in a code block don't add markdown to it
150+
const startKey = editorState.getSelection().getStartKey();
151+
if (startKey) {
152+
const currentBlockType = editorState
153+
.getCurrentContent()
154+
.getBlockForKey(startKey)
155+
.getType();
156+
if (currentBlockType === "code-block") return "not-handled";
157+
}
158+
159+
newEditorState = checkCharacterForState(editorState, "\n");
160+
if (editorState !== newEditorState) {
161+
setEditorState(newEditorState);
162+
return "handled";
163+
}
164+
148165
return "not-handled";
149166
},
150167
handleBeforeInput(character, editorState, { setEditorState }) {

src/modifiers/__test__/changeCurrentInlineStyle-test.js

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { expect } from "chai";
21
import Draft, { EditorState, SelectionState } from "draft-js";
32
import changeCurrentInlineStyle from "../changeCurrentInlineStyle";
43

@@ -43,10 +42,8 @@ describe("changeCurrentInlineStyle", () => {
4342
matchArr,
4443
"CODE"
4544
);
46-
expect(newEditorState).not.to.equal(editorState);
47-
expect(
48-
Draft.convertToRaw(newEditorState.getCurrentContent())
49-
).to.deep.equal(
45+
expect(newEditorState).not.toEqual(editorState);
46+
expect(Draft.convertToRaw(newEditorState.getCurrentContent())).toEqual(
5047
rawContentState(
5148
"foo bar baz",
5249
[
@@ -60,4 +57,31 @@ describe("changeCurrentInlineStyle", () => {
6057
)
6158
);
6259
});
60+
it("inserts the character at the end", () => {
61+
const text = "foo `bar` baz";
62+
const editorState = createEditorState(text, []);
63+
const matchArr = ["`bar`", "bar"];
64+
matchArr.index = 4;
65+
matchArr.input = text;
66+
const newEditorState = changeCurrentInlineStyle(
67+
editorState,
68+
matchArr,
69+
"CODE",
70+
"\n"
71+
);
72+
expect(newEditorState).not.toEqual(editorState);
73+
expect(Draft.convertToRaw(newEditorState.getCurrentContent())).toEqual(
74+
rawContentState(
75+
"foo bar\n baz",
76+
[
77+
{
78+
length: 3,
79+
offset: 4,
80+
style: "CODE",
81+
},
82+
],
83+
"CODE"
84+
)
85+
);
86+
});
6387
});

src/modifiers/changeCurrentInlineStyle.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EditorState, SelectionState, Modifier } from "draft-js";
22

3-
const changeCurrentInlineStyle = (editorState, matchArr, style) => {
3+
const changeCurrentInlineStyle = (editorState, matchArr, style, character) => {
44
const currentContent = editorState.getCurrentContent();
55
const selection = editorState.getSelection();
66
const key = selection.getStartKey();
@@ -23,7 +23,7 @@ const changeCurrentInlineStyle = (editorState, matchArr, style) => {
2323
newContentState = Modifier.insertText(
2424
newContentState,
2525
newContentState.getSelectionAfter(),
26-
" "
26+
character || " "
2727
);
2828
const newEditorState = EditorState.push(
2929
editorState,

src/modifiers/handleInlineStyle.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ const handleInlineStyle = (editorState, character) => {
2424
newEditorState = changeCurrentInlineStyle(
2525
newEditorState,
2626
matchArr,
27-
k
27+
k,
28+
character
2829
);
2930
}
3031
} while (matchArr);

0 commit comments

Comments
 (0)