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

Commit b11c066

Browse files
authored
Merge pull request #3 from withspectrum/fix-first-block
Fix deleting styled blocks
2 parents 016ee0c + 4c15dc1 commit b11c066

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/index.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,35 @@ const createMarkdownPlugin = (config = {}) => {
159159
}
160160
return "not-handled";
161161
},
162+
handleKeyCommand(command, editorState, { setEditorState }) {
163+
switch (command) {
164+
case "backspace": {
165+
// When a styled block is the first thing in the editor,
166+
// you cannot delete it. Typing backspace only deletes the content
167+
// but never deletes the block styling.
168+
// This piece of code fixes the issue by changing the block type
169+
// to 'unstyled' if we're on the first block of the editor and it's empty
170+
const selection = editorState.getSelection();
171+
const currentBlockKey = selection.getStartKey();
172+
if (!currentBlockKey) return "not-handled";
173+
174+
const content = editorState.getCurrentContent();
175+
const currentBlock = content.getBlockForKey(currentBlockKey);
176+
const firstBlock = content.getFirstBlock();
177+
if (firstBlock !== currentBlock) return "not-handled";
178+
179+
const currentBlockType = currentBlock.getType();
180+
const isEmpty = currentBlock.getLength() === 0;
181+
if (!isEmpty || currentBlockType === "unstyled") return "not-handled";
182+
183+
setEditorState(changeCurrentBlockType(editorState, "unstyled", ""));
184+
return "handled";
185+
}
186+
default: {
187+
return "not-handled";
188+
}
189+
}
190+
},
162191
handlePastedText(text, html, editorState, { setEditorState }) {
163192
if (html) {
164193
return "not-handled";

0 commit comments

Comments
 (0)