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

Commit 78459bf

Browse files
committed
WIP: Insert on enter
1 parent bf27f6e commit 78459bf

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,33 @@ describe("changeCurrentInlineStyle", () => {
6060
)
6161
);
6262
});
63+
it("inserts the character at the end", () => {
64+
const text = "foo `bar` baz";
65+
const editorState = createEditorState(text, []);
66+
const matchArr = ["`bar`", "bar"];
67+
matchArr.index = 4;
68+
matchArr.input = text;
69+
const newEditorState = changeCurrentInlineStyle(
70+
editorState,
71+
matchArr,
72+
"CODE",
73+
"\n"
74+
);
75+
expect(newEditorState).not.to.equal(editorState);
76+
expect(
77+
Draft.convertToRaw(newEditorState.getCurrentContent())
78+
).to.deep.equal(
79+
rawContentState(
80+
"foo bar baz",
81+
[
82+
{
83+
length: 3,
84+
offset: 4,
85+
style: "CODE",
86+
},
87+
],
88+
"CODE"
89+
)
90+
);
91+
});
6392
});

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)