File tree Expand file tree Collapse file tree 1 file changed +47
-10
lines changed
assets/src/scripts/blocks Expand file tree Collapse file tree 1 file changed +47
-10
lines changed Original file line number Diff line number Diff line change 1
1
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 ;
3
8
4
9
registerBlockType ( 'gutenberg-test/hello-world' , {
5
10
title : 'Hello World' ,
@@ -8,23 +13,55 @@ registerBlockType( 'gutenberg-test/hello-world', {
8
13
9
14
attributes : {
10
15
content : {
11
- type : 'array ' ,
12
- source : 'children ' ,
16
+ type : 'string ' ,
17
+ source : 'html ' ,
13
18
selector : 'p'
19
+ } ,
20
+ alignment : {
21
+ type : 'string'
14
22
}
15
23
} ,
16
24
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 ) {
20
29
setAttributes ( { content : newContent } ) ;
21
- } ;
30
+ }
31
+
32
+ function onChangeAlignment ( newAlignment ) {
33
+ setAttributes ( { alignment : newAlignment } ) ;
34
+ }
35
+
22
36
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 >
24
53
) ;
25
54
} ,
26
55
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
+ ) ;
29
66
}
30
67
} ) ;
You can’t perform that action at this time.
0 commit comments