feat(editor-ext): block accidental delete of selected base embed

Add Backspace/Delete keyboard shortcuts on the BaseEmbed node that
return true (handled, no-op) when the current selection is a
NodeSelection of this node — the 'click the embed and hit delete'
accidental path. Other deletion paths (range selections covering
the node, programmatic transactions, sync) still go through normally.
This commit is contained in:
Philipinho
2026-04-27 04:51:18 +01:00
parent 00e9d1d724
commit 144838aa89
@@ -1,4 +1,5 @@
import { Node, mergeAttributes } from '@tiptap/core';
import { NodeSelection } from '@tiptap/pm/state';
export interface BaseEmbedOptions {
HTMLAttributes: Record<string, any>;
@@ -58,4 +59,24 @@ export const BaseEmbed = Node.create<BaseEmbedOptions>({
}),
};
},
addKeyboardShortcuts() {
// Block Backspace / Delete when the base embed itself is the
// current selection — that's the "click on the embed and hit
// delete" accidental-delete path. Returning true tells TipTap
// we've handled the key, preventing the default removal.
// Other deletion paths (range selections covering the node,
// programmatic transactions) still go through.
const isThisNodeSelected = (): boolean => {
const { selection } = this.editor.state;
return (
selection instanceof NodeSelection &&
selection.node.type.name === this.name
);
};
return {
Backspace: () => isThisNodeSelected(),
Delete: () => isThisNodeSelected(),
};
},
});