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

Commit a4ef4ec

Browse files
committed
Fix deleting styled blocks
When the first thing in an editor is an styled block you cannot get rid of it, it'll always stay styled, because backspace only deletes the content. This patch fixes the issue by introducing custom behaviour for this specific condition: If we're in the first block of the editor, it's a styled block, the block is empty and we press backspace we change the block type to 'unstyled'. Fixes ngs#31
1 parent 3cf7df5 commit a4ef4ec

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

0 commit comments

Comments
 (0)