diff --git a/packages/editor-ext/src/lib/base-embed/base-embed.ts b/packages/editor-ext/src/lib/base-embed/base-embed.ts index 50cd0269a..90cd7584c 100644 --- a/packages/editor-ext/src/lib/base-embed/base-embed.ts +++ b/packages/editor-ext/src/lib/base-embed/base-embed.ts @@ -1,4 +1,5 @@ import { Node, mergeAttributes } from '@tiptap/core'; +import { NodeSelection } from '@tiptap/pm/state'; export interface BaseEmbedOptions { HTMLAttributes: Record; @@ -58,4 +59,24 @@ export const BaseEmbed = Node.create({ }), }; }, + + 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(), + }; + }, });