Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

fix: codeblock not getting removed when empty on android

thecodrrca4a634c239a
diff --git a/packages/editor/src/extensions/code-block/code-block.ts b/packages/editor/src/extensions/code-block/code-block.ts
index 04b5a2ff4..15306d304 100644
--- a/packages/editor/src/extensions/code-block/code-block.ts
+++ b/packages/editor/src/extensions/code-block/code-block.ts
@@ -272,14 +272,36 @@ export const CodeBlock = Node.create<CodeBlockOptions>({
// remove code block when at start of document or code block is empty
Backspace: () => {
const { empty, $anchor } = this.editor.state.selection;
+
+ const currentNode = $anchor.parent;
+ const nextNode = this.editor.state.doc.nodeAt($anchor.pos + 1);
+ const isCodeBlock = (node: ProsemirrorNode | null) =>
+ node && node.type.name === this.name;
const isAtStart = $anchor.pos === 1;
- if (!empty || $anchor.parent.type.name !== this.name) {
+
+ if (!empty) {
return false;
}
- if (isAtStart || !$anchor.parent.textContent.length) {
+ if (
+ isAtStart ||
+ (isCodeBlock(currentNode) && !currentNode.textContent.length)
+ ) {
return this.editor.commands.deleteNode(this.type);
}
+ // on android due to composition issues with various keyboards,
+ // sometimes backspace is detected one node behind. We need to
+ // manually handle this case.
+ else if (
+ nextNode &&
+ isCodeBlock(nextNode) &&
+ !nextNode.textContent.length
+ ) {
+ return this.editor.commands.command(({ tr }) => {
+ tr.delete($anchor.pos + 1, $anchor.pos + 1 + nextNode.nodeSize);
+ return true;
+ });
+ }
return false;
},