-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
3ce9dc7
Updating documentation to be more consistent with the example repo Wo…
terriann a394cd0
Updating documentation to be more consistent with the example repo Wo…
terriann 1b05c17
Updating documentation to be more consistent with the example repo Wo…
terriann 8a9fdb5
Adding note documenting the discremency between the example repo cont…
terriann File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
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, | ||
{ | ||
|
@@ -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 | ||
) ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See comment above to switch this to just |
||
``` | ||
{% 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 | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.