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

Commit d0c5dcb

Browse files
authored
Merge pull request #57 from withspectrum/exit-this-way-pleasee
Add empty unstyled block after new code block
2 parents 288d816 + 522a5bc commit d0c5dcb

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

src/modifiers/__test__/handleNewCodeBlock-test.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ import sinon from "sinon";
22
import Draft, { EditorState, SelectionState } from "draft-js";
33
import handleNewCodeBlock from "../handleNewCodeBlock";
44

5+
const removeBlockKeys = rawContentState => {
6+
return {
7+
...rawContentState,
8+
blocks: rawContentState.blocks.map(block => {
9+
delete block.key;
10+
return block;
11+
}),
12+
};
13+
};
14+
515
describe("handleNewCodeBlock", () => {
616
describe("in unstyled block with three backquotes", () => {
717
const testNewCodeBlock = (text, data) => () => {
@@ -23,14 +33,21 @@ describe("handleNewCodeBlock", () => {
2333
entityMap: {},
2434
blocks: [
2535
{
26-
key: "item1",
2736
text: "",
2837
type: "code-block",
2938
depth: 0,
3039
inlineStyleRanges: [],
3140
entityRanges: [],
3241
data,
3342
},
43+
{
44+
text: "",
45+
type: "unstyled",
46+
inlineStyleRanges: [],
47+
entityRanges: [],
48+
data: {},
49+
depth: 0,
50+
},
3451
],
3552
};
3653
const contentState = Draft.convertFromRaw(beforeRawContentState);
@@ -49,9 +66,11 @@ describe("handleNewCodeBlock", () => {
4966
it("creates new code block", () => {
5067
const newEditorState = handleNewCodeBlock(editorState);
5168
expect(newEditorState).not.toEqual(editorState);
52-
expect(Draft.convertToRaw(newEditorState.getCurrentContent())).toEqual(
53-
afterRawContentState
54-
);
69+
expect(
70+
removeBlockKeys(
71+
Draft.convertToRaw(newEditorState.getCurrentContent())
72+
)
73+
).toEqual(removeBlockKeys(afterRawContentState));
5574
});
5675
};
5776

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)