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

Commit

Permalink
Merge pull request #3 from withspectrum/fix-first-block
Browse files Browse the repository at this point in the history
Fix deleting styled blocks
  • Loading branch information
mxstbr authored Sep 22, 2017
2 parents 016ee0c + 4c15dc1 commit b11c066
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,35 @@ const createMarkdownPlugin = (config = {}) => {
}
return "not-handled";
},
handleKeyCommand(command, editorState, { setEditorState }) {
switch (command) {
case "backspace": {
// When a styled block is the first thing in the editor,
// you cannot delete it. Typing backspace only deletes the content
// but never deletes the block styling.
// This piece of code fixes the issue by changing the block type
// to 'unstyled' if we're on the first block of the editor and it's empty
const selection = editorState.getSelection();
const currentBlockKey = selection.getStartKey();
if (!currentBlockKey) return "not-handled";

const content = editorState.getCurrentContent();
const currentBlock = content.getBlockForKey(currentBlockKey);
const firstBlock = content.getFirstBlock();
if (firstBlock !== currentBlock) return "not-handled";

const currentBlockType = currentBlock.getType();
const isEmpty = currentBlock.getLength() === 0;
if (!isEmpty || currentBlockType === "unstyled") return "not-handled";

setEditorState(changeCurrentBlockType(editorState, "unstyled", ""));
return "handled";
}
default: {
return "not-handled";
}
}
},
handlePastedText(text, html, editorState, { setEditorState }) {
if (html) {
return "not-handled";
Expand Down

0 comments on commit b11c066

Please sign in to comment.