/
block.js
executable file
·95 lines (88 loc) · 2.81 KB
/
block.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Editable Block Example
*
* https://github.com/modularwp/gutenberg-block-editable-example
*/
( function() {
var __ = wp.i18n.__; // The __() function for internationalization.
var createElement = wp.element.createElement; // The wp.element.createElement() function to create elements.
var registerBlockType = wp.blocks.registerBlockType; // The registerBlockType() function to register blocks.
var RichText = wp.editor.RichText; // For creating editable elements.
var BlockControls = wp.editor.BlockControls; // For adding control elements.
var AlignmentToolbar = wp.editor.AlignmentToolbar; // For creating the alignment toolbar element within the control elements.
/**
* Register block
*
* @param {string} name Block name.
* @param {Object} settings Block settings.
* @return {?WPBlock} Block itself, if registered successfully,
* otherwise "undefined".
*/
registerBlockType(
'mdlr/block-toolbar-control-example', // Block name. Must be string that contains a namespace prefix. Example: my-plugin/my-custom-block.
{
title: __( 'Toolbar Control Example' ), // Block title. __() function allows for internationalization.
icon: 'admin-tools', // Block icon from Dashicons. https://developer.wordpress.org/resource/dashicons/.
category: 'common', // Block category. Group blocks together based on common traits E.g. common, formatting, layout widgets, embed.
attributes: {
content: {
type: 'array',
source: 'children',
selector: 'p',
default: 'Block content can be aligned with toolbar.',
},
alignment: {
type: 'string',
},
},
// Defines the block within the editor.
edit: function( props ) {
var content = props.attributes.content;
var alignment = props.attributes.alignment;
function onChangeContent( updatedContent ) {
props.setAttributes( { content: updatedContent } );
}
function onChangeAlignment( updatedAlignment ) {
props.setAttributes( { alignment: updatedAlignment } );
}
return [
createElement(
BlockControls,
{},
createElement(
AlignmentToolbar,
{
value: alignment,
onChange: onChangeAlignment
}
)
),
createElement(
RichText,
{
tagName: 'p',
className: props.className,
style: { textAlign: alignment },
value: content,
onChange: onChangeContent,
onFocus: props.setFocus
},
)
];
},
// Defines the saved block.
save: function( props ) {
var content = props.attributes.content;
var alignment = props.attributes.alignment;
return createElement(
'p',
{
className: props.className,
style: { textAlign: alignment },
},
content
);
},
}
);
})();