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

Commit 4e314bd

Browse files
committed
Add empty unstyled block after code block
Ref #56
1 parent 388d1e1 commit 4e314bd

File tree

2 files changed

+9
-6
lines changed

2 files changed

+9
-6
lines changed

src/modifiers/handleNewCodeBlock.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import changeCurrentBlockType from "./changeCurrentBlockType";
22
import insertEmptyBlock from "./insertEmptyBlock";
3+
import splitBlockAndChange from './splitBlockAndChange';
34
import { CODE_BLOCK_REGEX } from "../constants";
45

56
const handleNewCodeBlock = editorState => {
@@ -19,7 +20,8 @@ const handleNewCodeBlock = editorState => {
1920
if (language) {
2021
data.language = language;
2122
}
22-
return changeCurrentBlockType(editorState, "code-block", "", data);
23+
const editorStateWithCodeBlock = changeCurrentBlockType(editorState, "code-block", "", data);
24+
return splitBlockAndChange(editorStateWithCodeBlock, undefined, undefined, false)
2325
}
2426
const type = currentBlock.getType();
2527
if (type === "code-block" && isLast) {

src/modifiers/splitBlockAndChange.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,21 @@ import { EditorState, Modifier } from "draft-js";
33
const splitBlockAndChange = (
44
editorState,
55
type = "unstyled",
6-
blockMetadata = {}
6+
blockMetadata = {},
7+
selectNewBlock = true
78
) => {
89
let currentContent = editorState.getCurrentContent();
9-
let selection = editorState.getSelection();
10-
currentContent = Modifier.splitBlock(currentContent, selection);
11-
selection = currentContent.getSelectionAfter();
10+
const currentSelection = editorState.getSelection();
11+
currentContent = Modifier.splitBlock(currentContent, currentSelection);
12+
const selection = currentContent.getSelectionAfter();
1213
const key = selection.getStartKey();
1314
const blockMap = currentContent.getBlockMap();
1415
const block = blockMap.get(key);
1516
const data = block.getData().merge(blockMetadata);
1617
const newBlock = block.merge({ type, data });
1718
const newContentState = currentContent.merge({
1819
blockMap: blockMap.set(key, newBlock),
19-
selectionAfter: selection,
20+
selectionAfter: selectNewBlock ? selection : currentSelection,
2021
});
2122

2223
return EditorState.push(editorState, newContentState, "split-block");

0 commit comments

Comments
 (0)