-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Is there any onAdd event for blocks? #8655
Comments
There are no events in Gutenberg. The idea is that there are selectors that can give you at anytime the data in Gutenberg (for example the list of blocks in the post) and there's a unique event emitter you can use to track whether the state (entire state) change or not. For example you can do: const getBlockList = () => wp.data.select( 'core/editor' ).getBlocks();
let blockList = getBlockList();
wp.data.subscribe(() => {
const newBlockList = getBlockList();
const blockListChanged = newBlockList !== blockList;
blockList = newBlockList;
if ( blockListChanged ) {
// You can trigger here any behavior when the block list in the post changes.
}
}); You can check the handbook for all the available selectors you can use this way https://wordpress.org/gutenberg/handbook/data/ it's also good to take a look at the documentation of the Data Module. I hope this helps. Thanks |
I have no idea how to use this on a targeted block. Here below is my code. I am willing to run the function countdown while this block is added on editor. This is currently running on onChange only
|
your block name is called |
@youknowriad how can you filter the list of blocks by their name? I'm currently using forEach but I'm not sure this is the most performant way: const getBlockList = () => wp.data.select( 'core/block-editor' ).getBlocks();
getBlockList().forEach( function( blockType ){
console.log( blockType.name );
}); |
Using hooksI think a better approach with the introduction of hooks in react 16.8 (the question was asked before that) is to use useEffect hook in block edit function.
Adding empty array as a second parameter to useEffect will trigger the event on initial render. To check for block removal you can return a function in useEffect hooks:
Comparing old vs new blocksThe code provided by @youknowriad is a good start but it was triggered many times especially when typing. This is because it monitors all changes. You can use this code to limit events and check if there has been an addition or removal of the block. const { getBlocks: getBlockList } = wp.data.select('core/editor');
// Get current blocks client ids
let blockList = getBlockList().map((block) => block.clientId);
wp.data.subscribe(() => {
// Get new blocks client ids
const newBlockList = getBlockList().map((block) => block.clientId);
// Compare lengths
const blockListChanged = newBlockList.length !== blockList.length;
if (!blockListChanged) {
return;
}
// Block Added
if (newBlockList > blockList) {
// Get added blocks
const added = newBlockList.filter((x) => !blockList.includes(x));
console.log('added', added);
} else if (newBlockList < blockList) {
// Get removed blocks
const removed = blockList.filter((x) => !newBlockList.includes(x));
console.log('removed', removed);
}
// Update current block list with the new blocks for further comparison
blockList = newBlockList;
}); |
Like on the title, I am willing to be able to add a callback while my custom block is added on editor. Like I am creating some countdown which is initializing by a jquery function call by taking data from html data attributes.
Now I am willing the function to be run while I have added a new countdown element on my editor (by taking it's default value).
The text was updated successfully, but these errors were encountered: