-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.js
executable file
·145 lines (131 loc) · 3.36 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* Contact Info Block
*
*/
( function( blocks, i18n, element ) {
var el = element.createElement,
__ = i18n.__,
RichText = wp.blocks.RichText,
BlockControls = wp.blocks.BlockControls,
//AlignmentToolbar = wp.blocks.AlignmentToolbar;
BlockAlignmentToolbar = wp.blocks.BlockAlignmentToolbar,
TextControl = wp.blocks.InspectorControls.TextControl;
blocks.registerBlockType( 'contact-info/contact-info-block', {
title: __( 'Example: Contact Info', 'contact-info-block' ),
icon: 'universal-access-alt',
category: 'layout',
attributes: {
address: {
type: 'array',
source: 'children',
selector: '.contact-info-address',
},
phone: {
type: 'array',
source: 'children',
selector: '.contact-info-phone',
},
email: {
type: 'array',
source: 'children',
selector: '.contact-info-email'
},
alignment: {
type: 'string',
default: 'none',
},
},
getEditWrapperProps( attributes ) {
const { alignment } = attributes;
if ( 'center' === alignment || 'wide' === alignment ) {
return { 'data-align': alignment };
}
},
edit: function( props ) {
var phone = props.attributes.phone,
address = props.attributes.address,
email = props.attributes.email,
alignment = props.attributes.alignment,
className = props.className,
isSelected = props.isSelected;
function onChangeAlignment( newAlignment ) {
props.setAttributes( { alignment: newAlignment } );
}
return [
isSelected && el(
BlockControls,
{ key: 'toolbar' },
el(
BlockAlignmentToolbar,
{
value: alignment,
onChange: onChangeAlignment,
controls: [ 'center', 'wide' ],
}
)
),
el( 'div', { className: `${ className } align${ alignment }` },
el(
RichText,
{
tagName: 'span',
className: 'contact-info-address',
inline: false,
placeholder: i18n.__( 'Add address...' ),
keepPlaceholderOnFocus: true,
value: address,
onChange: function( value ) {
props.setAttributes( { address: value } );
},
}
),
el(
RichText,
{
tagName: 'span',
className: 'contact-info-phone',
placeholder: i18n.__( 'Add phone number...' ),
keepPlaceholderOnFocus: true,
value: phone,
isSelected: props.isSelected,
onChange: function( value ) {
props.setAttributes( { phone: value } );
},
}
),
el(
RichText,
{
tagName: 'span',
className: 'contact-info-email',
placeholder: i18n.__( 'Add email address...' ),
keepPlaceholderOnFocus: true,
value: email,
isSelected: props.isSelected,
onChange: function( value ) {
props.setAttributes( { email: value } );
},
}
),
)
];
},
save: function( props ) {
var phone = props.attributes.phone,
email = props.attributes.email,
address = props.attributes.address,
alignment = props.attributes.alignment;
return (
el( 'div', { className: `${ props.className } align${ alignment }` },
el( 'span', { className: 'contact-info-address' }, address ),
el( 'span', { className: 'contact-info-phone' }, phone ),
el( 'span', { className: 'contact-info-email' }, email ),
)
);
},
} );
} )(
window.wp.blocks,
window.wp.i18n,
window.wp.element
);