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

Commit 938fdb8

Browse files
authored
Merge pull request #55 from withspectrum/new-code-block-on-space
Create new code block on space
2 parents 2a9e3e2 + 96ac1a7 commit 938fdb8

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/__test__/plugin.test.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -613,6 +613,27 @@ describe("draft-js-markdown-plugin", () => {
613613
expect(subject()).toBe("not-handled");
614614
});
615615
});
616+
it("handles new code block on space", () => {
617+
createMarkdownPlugin.__Rewire__("handleNewCodeBlock", modifierSpy); // eslint-disable-line no-underscore-dangle
618+
currentRawContentState = {
619+
entityMap: {},
620+
blocks: [
621+
{
622+
key: "item1",
623+
text: "```",
624+
type: "unstyled",
625+
depth: 0,
626+
inlineStyleRanges: [],
627+
entityRanges: [],
628+
data: {},
629+
},
630+
],
631+
};
632+
character = " ";
633+
expect(subject()).toBe("handled");
634+
expect(modifierSpy).toHaveBeenCalledTimes(1);
635+
expect(store.setEditorState).toHaveBeenCalledWith(newEditorState);
636+
});
616637
});
617638
describe("handlePastedText", () => {
618639
let pastedText;

src/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,19 @@ function checkCharacterForState(config, editorState, character) {
109109
) {
110110
newEditorState = handleLink(editorState, character);
111111
}
112+
if (
113+
newEditorState === editorState &&
114+
config.features.block.includes("CODE")
115+
) {
116+
const contentState = editorState.getCurrentContent();
117+
const selection = editorState.getSelection();
118+
const key = selection.getStartKey();
119+
const currentBlock = contentState.getBlockForKey(key);
120+
const text = currentBlock.getText();
121+
const type = currentBlock.getType();
122+
if (type !== "code-block" && CODE_BLOCK_REGEX.test(text))
123+
newEditorState = handleNewCodeBlock(editorState);
124+
}
112125
if (editorState === newEditorState) {
113126
newEditorState = handleInlineStyle(
114127
config.features.inline,

0 commit comments

Comments
 (0)