Skip to content

Commit 53a8a5c

Browse files
sugarshinngs
authored andcommitted
Insert new line character for code block
1 parent 5314c1a commit 53a8a5c

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import insertEmptyBlock from './modifiers/insertEmptyBlock';
1313
import handleLink from './modifiers/handleLink';
1414
import handleImage from './modifiers/handleImage';
1515
import leaveList from './modifiers/leaveList';
16+
import insertText from './modifiers/insertText';
1617
import createLinkDecorator from './decorators/link';
1718
import createImageDecorator from './decorators/image';
1819

@@ -51,6 +52,9 @@ const createMarkdownShortcutsPlugin = (config = {}) => {
5152
(ev.ctrlKey || ev.shiftKey || ev.metaKey || ev.altKey || /^header-/.test(type))) {
5253
newEditorState = insertEmptyBlock(editorState);
5354
}
55+
if (newEditorState === editorState && type === 'code-block') {
56+
newEditorState = insertText(editorState, '\n');
57+
}
5458
if (newEditorState === editorState) {
5559
newEditorState = handleNewCodeBlock(editorState);
5660
}

src/modifiers/insertText.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { EditorState, Modifier } from 'draft-js';
2+
3+
const insertText = (editorState, text) => {
4+
const selection = editorState.getSelection();
5+
const content = editorState.getCurrentContent();
6+
const newContentState = Modifier.insertText(
7+
content,
8+
selection,
9+
text,
10+
editorState.getCurrentInlineStyle()
11+
);
12+
return EditorState.push(
13+
editorState,
14+
newContentState,
15+
'insert-fragment'
16+
);
17+
};
18+
19+
export default insertText;

0 commit comments

Comments
 (0)