Skip to content

Instantly share code, notes, and snippets.

@rocketdevcorp
Last active August 7, 2019 03:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rocketdevcorp/1c704f8eede329e6f7d45c59bcd2b273 to your computer and use it in GitHub Desktop.
Save rocketdevcorp/1c704f8eede329e6f7d45c59bcd2b273 to your computer and use it in GitHub Desktop.
Footnote: A simple Gutenberg block using the RichText component
/* Footnote.js
*
* <div class="wp-block-myblocks-footnote">
* <p>Line 1</p>
* <p>Line 2</p>
* </div>
*/
( function( i18n, blocks, blockEditor, element, richText ) {
var el = element.createElement;
var __ = i18n.__;
var RichText = blockEditor.RichText;
blocks.registerBlockType( 'myblocks/footnote', {
title: __( 'Footnote', 'myblocks'),
category: 'formatting',
attributes: {
content: {
type: 'string',
source: 'html',
selector: 'div',
multiline: 'p',
},
},
edit: function( props ) {
var attributes = props.attributes,
content = props.attributes.content;
function onChangeContent( newContent ) {
props.setAttributes( {
content: newContent
});
}
// tagName is the block-level HTML element that wraps the input
// multiline (optional) specifies whether line breaks will create new paragraphs
return el( RichText,
{
tagName: 'div',
multiline: true,
onChange: onChangeContent,
value: content
}
);
},
save: function( props ) {
return el( RichText.Content,
{
tagName: 'div',
multiline: 'p',
value: props.attributes.content,
}
);
},
} );
}(
window.wp.i18n,
window.wp.blocks,
window.wp.blockEditor,
window.wp.element,
window.wp.richText
) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment