Skip to content
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

Align tutorial with example plugin repo code #12740

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,50 +13,45 @@ You can also customize the toolbar to include controls specific to your block ty
{% codetabs %}
{% ES5 %}
```js
var el = wp.element.createElement,
Fragment = wp.element.Fragment
registerBlockType = wp.blocks.registerBlockType,
RichText = wp.editor.RichText,
BlockControls = wp.editor.BlockControls,
AlignmentToolbar = wp.editor.AlignmentToolbar;

registerBlockType( 'gutenberg-boilerplate-es5/hello-world-step-04', {
title: 'Hello World (Step 4)',

icon: 'universal-access-alt',

category: 'layout',

attributes: {
content: {
type: 'string',
source: 'html',
selector: 'p',
( function( blocks, editor, element ) {
var el = element.createElement;
var RichText = editor.RichText;
var AlignmentToolbar = editor.AlignmentToolbar;
var BlockControls = editor.BlockControls;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer passing in wp as the global, because than you can use additional parts without having to change what is being passed in, or a mistake in the order of items passed in.

( function( wp ) {
	var el = wp.element.createElement;
	var RichText = wp.editor.RichText;
	var AlignmentToolbar = wp.editor.AlignmentToolbar;
	var BlockControls = wp.editor.BlockControls;

blocks.registerBlockType( 'gutenberg-examples/example-04-controls', {
title: 'Example: Controls',,
icon: 'universal-access-alt',
category: 'layout',

attributes: {
content: {
type: 'array',
source: 'children',
selector: 'p',
},
alignment: {
type: 'string',
default: 'none',
},
},
alignment: {
type: 'string',
},
},

edit: function( props ) {
var content = props.attributes.content,
alignment = props.attributes.alignment;
edit: function( props ) {
var content = props.attributes.content;
var alignment = props.attributes.alignment;

function onChangeContent( newContent ) {
props.setAttributes( { content: newContent } );
}
function onChangeContent( newContent ) {
props.setAttributes( { content: newContent } );
}

function onChangeAlignment( newAlignment ) {
props.setAttributes( { alignment: newAlignment } );
}
function onChangeAlignment( newAlignment ) {
props.setAttributes( { alignment: newAlignment === undefined ? 'none' : newAlignment } );
}

return (
el(
Fragment,
null,
return [
el(
BlockControls,
null,
{ key: 'controls' },
el(
AlignmentToolbar,
{
Expand All @@ -68,105 +63,110 @@ registerBlockType( 'gutenberg-boilerplate-es5/hello-world-step-04', {
el(
RichText,
{
key: 'editable',
key: 'richtext',
tagName: 'p',
className: props.className,
style: { textAlign: alignment },
className: props.className,
onChange: onChangeContent,
value: content,
}
)
)
);
},

save: function( props ) {
var content = props.attributes.content,
alignment = props.attributes.alignment;
),
];
},

return el( RichText.Content, {
tagName: 'p',
className: props.className,
style: { textAlign: alignment },
value: content
} );
},
} );
save: function( props ) {
return el( RichText.Content, {
tagName: 'p',
className: 'gutenberg-examples-align-' + props.attributes.alignment,
value: props.attributes.content,
} );
},
} );
}(
window.wp.blocks,
window.wp.editor,
window.wp.element
) );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above to switch this to just ( window.wp )

```
{% ESNext %}
```js
const { registerBlockType } = wp.blocks;
const { Fragment } = wp.element;
const {
registerBlockType,
} = wp.blocks;

const {
RichText,
BlockControls,
AlignmentToolbar,
BlockControls,
} = wp.editor;

registerBlockType( 'gutenberg-boilerplate-esnext/hello-world-step-04', {
title: 'Hello World (Step 4)',

registerBlockType( 'gutenberg-examples/example-04-controls-esnext', {
title: 'Example: Controls (esnext)',
icon: 'universal-access-alt',

category: 'layout',

attributes: {
content: {
type: 'string',
source: 'html',
type: 'array',
source: 'children',
selector: 'p',
},
alignment: {
type: 'string',
default: 'none',
},
},
edit: ( props ) => {
const {
attributes: {
content,
alignment,
},
className,
} = props;

const onChangeContent = ( newContent ) => {
props.setAttributes( { content: newContent } );
};

edit( { attributes, className, setAttributes } ) {
const { content, alignment } = attributes;

function onChangeContent( newContent ) {
setAttributes( { content: newContent } );
}

function onChangeAlignment( newAlignment ) {
setAttributes( { alignment: newAlignment } );
}
const onChangeAlignment = ( newAlignment ) => {
props.setAttributes( { alignment: newAlignment === undefined ? 'none' : newAlignment } );
};

return (
<Fragment>
<BlockControls>
<AlignmentToolbar
value={ alignment }
onChange={ onChangeAlignment }
/>
</BlockControls>
<div>
{
<BlockControls>
<AlignmentToolbar
value={ alignment }
onChange={ onChangeAlignment }
/>
</BlockControls>
}
<RichText
key="editable"
tagName="p"
className={ className }
style={ { textAlign: alignment } }
tagName="p"
onChange={ onChangeContent }
value={ content }
/>
</Fragment>
</div>
);
},

save( { attributes } ) {
const { content, alignment } = attributes;

save: ( props ) => {
return (
<RichText.Content
style={ { textAlign: alignment } }
value={ content }
className={ `gutenberg-examples-align-${ props.attributes.alignment }` }
tagName="p"
value={ props.attributes.content }
/>
);
},
} );
```
{% end %}

Note that internationalization is an important consideration for WordPress development. While the examples in this documentation do not include internationalization, the [accompanying WordPress example block plugin](https://github.com/WordPress/gutenberg-examples) includes the code necessary to support internationalization.

Note that `BlockControls` is only visible when the block is currently selected and in visual editing mode. `BlockControls` are not shown when editing a block in HTML editing mode.

## Inspector
Expand Down
Loading