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 11const { 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
49registerBlockType ( '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} ) ;
You can’t perform that action at this time.
0 commit comments