Skip to content

Commit 3293466

Browse files
committed
Updated Gutenberg example with current api
1 parent 8391805 commit 3293466

File tree

1 file changed

+47
-10
lines changed

1 file changed

+47
-10
lines changed
Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
const { registerBlockType } = wp.blocks;
2-
const { RichText } = wp.editor;
2+
const { Fragment } = wp.element;
3+
const {
4+
RichText,
5+
BlockControls,
6+
AlignmentToolbar
7+
} = wp.editor;
38

49
registerBlockType( 'gutenberg-test/hello-world', {
510
title: 'Hello World',
@@ -8,23 +13,55 @@ registerBlockType( 'gutenberg-test/hello-world', {
813

914
attributes: {
1015
content: {
11-
type: 'array',
12-
source: 'children',
16+
type: 'string',
17+
source: 'html',
1318
selector: 'p'
19+
},
20+
alignment: {
21+
type: 'string'
1422
}
1523
},
1624

17-
edit: ( props ) => {
18-
const { attributes: { content }, setAttributes, className } = props;
19-
const onChangeContent = ( newContent ) => {
25+
edit({ attributes, className, setAttributes }) {
26+
const { content, alignment } = attributes;
27+
28+
function onChangeContent( newContent ) {
2029
setAttributes({ content: newContent });
21-
};
30+
}
31+
32+
function onChangeAlignment( newAlignment ) {
33+
setAttributes({ alignment: newAlignment });
34+
}
35+
2236
return (
23-
<RichText tagName="p" className={className} onChange={onChangeContent} value={content}/>
37+
<Fragment>
38+
<BlockControls>
39+
<AlignmentToolbar
40+
value={alignment}
41+
onChange={onChangeAlignment}
42+
/>
43+
</BlockControls>
44+
<RichText
45+
key="editable"
46+
tagName="p"
47+
className={className}
48+
style={{ textAlign: alignment }}
49+
onChange={onChangeContent}
50+
value={content}
51+
/>
52+
</Fragment>
2453
);
2554
},
2655

27-
save: ( props ) => {
28-
return <RichText.Content tagName="p" value={props.attributes.content} />;
56+
save({ attributes }) {
57+
const { content, alignment } = attributes;
58+
59+
return (
60+
<RichText.Content
61+
style={{ textAlign: alignment }}
62+
value={content}
63+
tagName="p"
64+
/>
65+
);
2966
}
3067
});

0 commit comments

Comments
 (0)