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

Commit 117ffd5

Browse files
Merge pull request #42 from michaelgmcd/master
Do not convert block type if current block type is not simple
2 parents a9e8295 + adc086e commit 117ffd5

File tree

2 files changed

+76
-33
lines changed

2 files changed

+76
-33
lines changed

src/modifiers/__test__/handleBlockType-test.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,44 @@ describe("handleBlockType", () => {
3939
});
4040
});
4141

42+
describe("when current block type is not 'unstyled' or 'paragraph'", () => {
43+
const rawContentState = {
44+
entityMap: {},
45+
blocks: [
46+
{
47+
key: "item1",
48+
text: "# Header",
49+
type: "unordered-list-item",
50+
depth: 0,
51+
inlineStyleRanges: [],
52+
entityRanges: [],
53+
data: {},
54+
},
55+
],
56+
};
57+
const contentState = Draft.convertFromRaw(rawContentState);
58+
const selection = new SelectionState({
59+
anchorKey: "item1",
60+
anchorOffset: 3,
61+
focusKey: "item1",
62+
focusOffset: 3,
63+
isBackward: false,
64+
hasFocus: true,
65+
});
66+
const editorState = EditorState.forceSelection(
67+
EditorState.createWithContent(contentState),
68+
selection
69+
);
70+
71+
it("does not convert block type", () => {
72+
const newEditorState = handleBlockType(editorState, " ");
73+
expect(newEditorState).toEqual(editorState);
74+
expect(Draft.convertToRaw(newEditorState.getCurrentContent())).toEqual(
75+
rawContentState
76+
);
77+
});
78+
});
79+
4280
const testCases = {
4381
"converts from unstyled to header-one": {
4482
before: {

src/modifiers/handleBlockType.js

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,44 +32,49 @@ const handleBlockType = (editorState, character) => {
3232
""
3333
);
3434
const blockType = RichUtils.getCurrentBlockType(editorState);
35-
for (let i = 1; i <= 6; i += 1) {
36-
if (line.indexOf(`${sharps(i)} `) === 0) {
35+
36+
if (blockType === "unstyled" || blockType === "paragraph") {
37+
for (let i = 1; i <= 6; i += 1) {
38+
if (line.indexOf(`${sharps(i)} `) === 0) {
39+
return changeCurrentBlockType(
40+
editorState,
41+
blockTypes[i],
42+
line.replace(/^#+\s/, "")
43+
);
44+
}
45+
}
46+
let matchArr = line.match(/^[*-] (.*)$/);
47+
if (matchArr) {
3748
return changeCurrentBlockType(
3849
editorState,
39-
blockTypes[i],
40-
line.replace(/^#+\s/, "")
50+
"unordered-list-item",
51+
matchArr[1]
52+
);
53+
}
54+
matchArr = line.match(/^[\d]\. (.*)$/);
55+
if (matchArr) {
56+
return changeCurrentBlockType(
57+
editorState,
58+
"ordered-list-item",
59+
matchArr[1]
60+
);
61+
}
62+
matchArr = line.match(/^> (.*)$/);
63+
if (matchArr) {
64+
return changeCurrentBlockType(editorState, "blockquote", matchArr[1]);
65+
}
66+
} else if (blockType === "unordered-list-item") {
67+
let matchArr = line.match(/^\[([x ])] (.*)$/i);
68+
if (matchArr) {
69+
return changeCurrentBlockType(
70+
editorState,
71+
CHECKABLE_LIST_ITEM,
72+
matchArr[2],
73+
{ checked: matchArr[1] !== " " }
4174
);
4275
}
4376
}
44-
let matchArr = line.match(/^[*-] (.*)$/);
45-
if (matchArr) {
46-
return changeCurrentBlockType(
47-
editorState,
48-
"unordered-list-item",
49-
matchArr[1]
50-
);
51-
}
52-
matchArr = line.match(/^[\d]\. (.*)$/);
53-
if (matchArr) {
54-
return changeCurrentBlockType(
55-
editorState,
56-
"ordered-list-item",
57-
matchArr[1]
58-
);
59-
}
60-
matchArr = line.match(/^> (.*)$/);
61-
if (matchArr) {
62-
return changeCurrentBlockType(editorState, "blockquote", matchArr[1]);
63-
}
64-
matchArr = line.match(/^\[([x ])] (.*)$/i);
65-
if (matchArr && blockType === "unordered-list-item") {
66-
return changeCurrentBlockType(
67-
editorState,
68-
CHECKABLE_LIST_ITEM,
69-
matchArr[2],
70-
{ checked: matchArr[1] !== " " }
71-
);
72-
}
77+
7378
return editorState;
7479
};
7580

0 commit comments

Comments
 (0)