I am new to Wordpress Gutenberg blocks. I created a gallery plugin and I want a user to be able to insert gallery shortcode by simply choosing the desired gallery from a popup window. I use jQuery text() function to inject shortcodes from the popup window with success but the content will not be saved. However, when I type in some text everything works fine.
Here is my Gutenberg js:
var el = wp.element.createElement,
registerBlockType = wp.blocks.registerBlockType,
RichText = wp.editor.RichText,
blockStyle = { backgroundColor: '#0000cc', color: '#fff', padding: '1%', margin: '1%', border: 'none', boxShadow: '5px 5px 5px #000', cursor: 'pointer' };
registerBlockType( 'prtxgal/block', {
title: 'Protex gallery',
icon: 'images-alt',
category: 'common',
attributes: {
content: {
type: 'string',
source: 'html',
selector: 'div',
}
},
edit: function(props) {
var content = props.attributes.content;
function onChangeContent( newContent ) {
props.setAttributes( { content: newContent } );
}
return[ el(
'button', { className: 'button add_prtxgal', /*style: blockStyle*/}, 'Choose gallery'
),
el (RichText,
{
tagName: 'div',
className: props.className,
onChange: onChangeContent,
value: content,
}
),
];
},
save: function(props) {
var content = props.attributes.content;
return el( RichText.Content, {
tagName: 'div',
className: props.className,
value: content
});
},
});
The combination of jQuery
and front end frameworks like React.js (in reality a library not framework) and Angular isn't considered a good combinations so it is recommended not to use it.
Frontend frameworks maintains the state of each component and if we change that component via jQuery or by accessing nodes with JS then those states aren't updated in framework. For updating states framework provides their own methods in React the method is setState(obj)
while in Gutenberg the method is setAttributes(obj)
which is actually a wrapper around React.js setState()
.
Here are the few suggestions how I suggest you to make this component (considering this is not a Server Side Block).
JSX
instead of ES5
syntax of JavaScript. WordPress
docs by default shows sample code of ES5
but there is a tab to
change it to JSX
.core/gallery
block (present on bottom while selecting more than one image).attributes
which also appear on frontend and from frontend I add values into this new block. Gutenberg <RichText/>
component is very helpful for manipulating text content.onChange
event on <RichText/>
.For better understanding of Gutenberg ecosystem I recommend you to familiarize yourself with following things:
setState()
or setAttributes()
not directly by accessing this.props.attributes
or this.props.state
ComponentDidMount
lifecycle of React.js and then update the states. Data can be fetch through any API or even WordPress REST API.