Replies: 4 comments
-
Nevermind, you can just recover the block in the site editor, which will reset the save function |
Beta Was this translation helpful? Give feedback.
-
For development it may be helpful to make and include a script that creates and inserts your blocks as you'd like to test them. That way markup changes won’t require you to recover the blocks. With regards to that, in the config of |
Beta Was this translation helpful? Give feedback.
-
@stokesman Thanks for the answer and sorry for the late response. Currently I still just recover the block, but since I use tailwind it gets tedious to do this for every little change. Also I can not make it a static block since I need JS for the logic. Can you explain me how a script like this look like? And I looked into the |
Beta Was this translation helpful? Give feedback.
-
Hi @NayzR, I think pehaps the best explanation is a working example. Script to insert test blocks into particular posts( ( {
blocks: { createBlock, createBlocksFromInnerBlocksTemplate, store: blocksStore },
blockEditor: { store: blockEditorStore },
data: { dispatch, select },
} ) => {
// A list of post ids in which block insertion is applied.
const applicablePosts = [ '2' ];
const blockToInsert = 'core/cover';
const overlayColors = [ 'peachpuff', 'skyblue' ];
const createBlockWithInnerBlocks = ( blockName, attributes, innerBlocks ) => createBlock(
blockName,
attributes,
createBlocksFromInnerBlocksTemplate( innerBlocks )
);
// Waiting for window load seems suffcient for all blocks to have been registered.
// It’s possible though that a custom block registers itself later than this and in
// such a case subscribing to the blocks store instead of using the load event would
// allow waiting for it before proceeding with the insertion routine. Even if the
// block to insert isn’t a late registrant requiring the subscription, using one can
// have a minor benefit in case the page to insert blocks on already has lots of media
// present as the subscriber will likely trigger the block insertion sooner.
window.addEventListener( 'load', () => {
const locationParams = new URLSearchParams( window.location.search );
// Bails unless the post id is included in the applicable ones.
if ( ! applicablePosts.includes( locationParams.get( 'post' ) ) ) {
return;
}
const { getBlockType } = select( blocksStore );
const { getBlocks } = select( blockEditorStore );
const { insertBlocks, replaceBlocks } = dispatch( blockEditorStore );
const { example: { attributes, innerBlocks } } = getBlockType( blockToInsert );
const blocks = [
createBlockWithInnerBlocks( blockToInsert, attributes, innerBlocks ),
];
// Creates additional blocks to test some attribute variations.
for ( const customOverlayColor of overlayColors ) {
blocks.push( createBlockWithInnerBlocks(
blockToInsert,
{ ...attributes, customOverlayColor },
innerBlocks
) );
}
const currentBlocks = getBlocks();
if ( currentBlocks.length ) {
replaceBlocks(
currentBlocks.map( ( { clientId } ) => clientId ),
blocks
);
return;
}
insertBlocks( blocks );
} );
} )( wp ); Some notes:
|
Beta Was this translation helpful? Give feedback.
-
I understand the importance of deprecation when the blocks are used somewhere. However when I am still in the development phase, is there a way to revert/delete the old save function without the hassle deprecating the block, since the block is not used anywhere?
Beta Was this translation helpful? Give feedback.
All reactions