-
-
Notifications
You must be signed in to change notification settings - Fork 550
onDelete, onRemove or onRemoveBlocks event handler #1566
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is related to an issue we are tracking: #1566 |
oh, very nice. I will keep an eye out for any news. |
Hi there, this is an awesome feat! Are there any plans to expose block specific changes on a block level as well (so the change logic could get handled inside custom blocks)? e.g.: |
@Tim-Pet I don't completely understand what you are trying to achieve with this. But, it does not make sense to expose this sort of API within a custom block, because the change is derived from what already happened, so the custom block's instance will just have been destroyed (or at least about to), so it wouldn't really be able to do anything about it. |
Ah fair point, thanks for pointing me in the right direction.
In the end I want to prevent certain blocks from deletion or apply custom behavior for an improved UX for complex custom blocks
Yeah that totally makes sense to me. thanks for jumping in on that. Appreciated |
Ah, clearer use-case, that, I can help you with. Here is an implementation of that (using the primitive that this change API is built on top of): import { BlockNoteView } from "@blocknote/mantine";
import { useCreateBlockNote } from "@blocknote/react";
import { getBlocksChangedByTransaction } from "@blocknote/core";
import { Plugin, PluginKey } from "prosemirror-state";
// instantiate a prosemirror plugin which will filter out transactions which meet some criteria
const MyFilterExtension = {
plugin: new Plugin({
filterTransaction: (transaction) => {
const blocksChanged = getBlocksChangedByTransaction(transaction);
console.log(blocksChanged);
if (JSON.stringify(blocksChanged).includes("NOPE")) {
return false;
}
return true;
},
key: new PluginKey("my-extension"),
}),
};
export default function App() {
const editor = useCreateBlockNote({
_extensions: {
filterExtension: MyFilterExtension,
},
});
return <BlockNoteView editor={editor} />;
} This will reject any transaction that contains the text "NOPE" in it (just to show that you can arbitrarily reject transactions). You can see it working in this video: Screen.Recording.2025-05-02.at.12.53.11.mp4Maybe we could add something like this in the future, not sure what that API would look like though |
thank you so much, this is amazing! |
Closing as done with #1585 |
Is your feature request related to a problem? Please describe.
I would like to have a specific event handler when a block or blocks are deleted, because I have some linked blocks that are being added as pairs, e.g (startBlock and endBlock) so if I delete a startBlock the endBlock is automatically deleted, because they are "pairs" in my implementation.
So I don't want to do that validation on the
onChange
method every time.Describe the solution you'd like
Having a
onDelete
oronRemove
oronContentDeleted
oronRemoveBlocks
event, so it receives a callback with the deleted blocks[] as argument.something like this:
Describe alternatives you've considered
Right now I do a validation for ALL the blocks on every
onChange
, but I think isn't the best approach, I would like to do those validations only when a block is deleted, if we are editing or creating/adding blocks I don't need to do extra validations.is there any other possible solution?
The text was updated successfully, but these errors were encountered: