Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/modifiers/__test__/handleBlockType-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,44 @@ describe("handleBlockType", () => {
});
});

describe("when current block type is not 'unstyled' or 'paragraph'", () => {
const rawContentState = {
entityMap: {},
blocks: [
{
key: "item1",
text: "# Header",
type: "unordered-list-item",
depth: 0,
inlineStyleRanges: [],
entityRanges: [],
data: {},
},
],
};
const contentState = Draft.convertFromRaw(rawContentState);
const selection = new SelectionState({
anchorKey: "item1",
anchorOffset: 3,
focusKey: "item1",
focusOffset: 3,
isBackward: false,
hasFocus: true,
});
const editorState = EditorState.forceSelection(
EditorState.createWithContent(contentState),
selection
);

it("does not convert block type", () => {
const newEditorState = handleBlockType(editorState, " ");
expect(newEditorState).toEqual(editorState);
expect(Draft.convertToRaw(newEditorState.getCurrentContent())).toEqual(
rawContentState
);
});
});

const testCases = {
"converts from unstyled to header-one": {
before: {
Expand Down
71 changes: 38 additions & 33 deletions src/modifiers/handleBlockType.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,44 +32,49 @@ const handleBlockType = (editorState, character) => {
""
);
const blockType = RichUtils.getCurrentBlockType(editorState);
for (let i = 1; i <= 6; i += 1) {
if (line.indexOf(`${sharps(i)} `) === 0) {

if (blockType === "unstyled" || blockType === "paragraph") {
for (let i = 1; i <= 6; i += 1) {
if (line.indexOf(`${sharps(i)} `) === 0) {
return changeCurrentBlockType(
editorState,
blockTypes[i],
line.replace(/^#+\s/, "")
);
}
}
let matchArr = line.match(/^[*-] (.*)$/);
if (matchArr) {
return changeCurrentBlockType(
editorState,
blockTypes[i],
line.replace(/^#+\s/, "")
"unordered-list-item",
matchArr[1]
);
}
matchArr = line.match(/^[\d]\. (.*)$/);
if (matchArr) {
return changeCurrentBlockType(
editorState,
"ordered-list-item",
matchArr[1]
);
}
matchArr = line.match(/^> (.*)$/);
if (matchArr) {
return changeCurrentBlockType(editorState, "blockquote", matchArr[1]);
}
} else if (blockType === "unordered-list-item") {
let matchArr = line.match(/^\[([x ])] (.*)$/i);
if (matchArr) {
return changeCurrentBlockType(
editorState,
CHECKABLE_LIST_ITEM,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch! I suppose the existing tests broke without this?

matchArr[2],
{ checked: matchArr[1] !== " " }
);
}
}
let matchArr = line.match(/^[*-] (.*)$/);
if (matchArr) {
return changeCurrentBlockType(
editorState,
"unordered-list-item",
matchArr[1]
);
}
matchArr = line.match(/^[\d]\. (.*)$/);
if (matchArr) {
return changeCurrentBlockType(
editorState,
"ordered-list-item",
matchArr[1]
);
}
matchArr = line.match(/^> (.*)$/);
if (matchArr) {
return changeCurrentBlockType(editorState, "blockquote", matchArr[1]);
}
matchArr = line.match(/^\[([x ])] (.*)$/i);
if (matchArr && blockType === "unordered-list-item") {
return changeCurrentBlockType(
editorState,
CHECKABLE_LIST_ITEM,
matchArr[2],
{ checked: matchArr[1] !== " " }
);
}

return editorState;
};

Expand Down