carbon-fields icon indicating copy to clipboard operation
carbon-fields copied to clipboard

Make it possible to register custom script in new Block

Open labmorales opened this issue 3 years ago • 1 comments

This pull request makes it possible to set a custom script for the frontend and backend (editor) for the block.

Followed the same exact patterns used by the set_style methods.

How to use it:

	wp_register_script('shiny_block', get_template_directory_uri().'/assets/js/block-test.js', [], false, true);
	wp_register_script('shiny_block_editor', get_template_directory_uri().'/assets/js/block-test-editor.js', [], false, true);

	Block::make( __( 'My Shiny Gutenberg Block 2' ) )
		->set_script('shiny_block')
		->set_editor_script('shiny_block_editor')
		->set_render_callback( function ( $fields, $attributes, $inner_blocks ) {
			?>
			<div class="block">
				....
			</div><!-- /.block -->	
			<?php
		} );

Don't know if it's a bug or simply by design. But in the editor interface it runs both scripts.

This attend partially to a feature requested in #664 .

labmorales avatar May 01 '21 03:05 labmorales

Hello,

I would like to add also some javascript scripts if one custom block is used... It will be great also to add a parameter to choose a priority - it will allow to choose the order of the scripts. Add priority field to style too

vptcnt avatar May 28 '21 18:05 vptcnt

Hi @labmorales ,

Thank you for your PR, we've implemented our solution for this issue in the new release.

HTMLBurger-NG avatar Feb 20 '23 10:02 HTMLBurger-NG

@HTMLBurger-NG how was it implemented? Can't seem to find any reference to it.

josecoelhomelo avatar May 24 '23 20:05 josecoelhomelo