mirror of
https://github.com/docmost/docmost.git
synced 2026-06-10 10:13:01 +08:00
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:
@@ -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(),
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user