forked from WordPress/gutenberg-examples
-
Notifications
You must be signed in to change notification settings - Fork 1
/
block.js
130 lines (124 loc) · 3.48 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
( function( blocks, editor, i18n, element, components, _ ) {
var el = element.createElement;
var RichText = editor.RichText;
var MediaUpload = editor.MediaUpload;
i18n.setLocaleData( window.gutenberg_examples_05.localeData, 'gutenberg-examples' );
blocks.registerBlockType( 'gutenberg-examples/example-05-recipe-card', {
title: i18n.__( 'Example: Recipe Card', 'gutenberg-examples' ),
icon: 'index-card',
category: 'layout',
attributes: {
title: {
type: 'array',
source: 'children',
selector: 'h2',
},
mediaID: {
type: 'number',
},
mediaURL: {
type: 'string',
source: 'attribute',
selector: 'img',
attribute: 'src',
},
ingredients: {
type: 'array',
source: 'children',
selector: '.ingredients',
},
instructions: {
type: 'array',
source: 'children',
selector: '.steps',
},
},
edit: function( props ) {
var attributes = props.attributes;
var onSelectImage = function( media ) {
return props.setAttributes( {
mediaURL: media.url,
mediaID: media.id,
} );
};
return (
el( 'div', { className: props.className },
el( RichText, {
tagName: 'h2',
inline: true,
placeholder: i18n.__( 'Write Recipe title…', 'gutenberg-examples' ),
value: attributes.title,
onChange: function( value ) {
props.setAttributes( { title: value } );
},
} ),
el( 'div', { className: 'recipe-image' },
el( MediaUpload, {
onSelect: onSelectImage,
type: 'image',
value: attributes.mediaID,
render: function( obj ) {
return el( components.Button, {
className: attributes.mediaID ? 'image-button' : 'button button-large',
onClick: obj.open
},
! attributes.mediaID ? i18n.__( 'Upload Image', 'gutenberg-examples' ) : el( 'img', { src: attributes.mediaURL } )
);
}
} )
),
el( 'h3', {}, i18n.__( 'Ingredients', 'gutenberg-examples' ) ),
el( RichText, {
tagName: 'ul',
multiline: 'li',
placeholder: i18n.__( 'Write a list of ingredients…', 'gutenberg-examples' ),
value: attributes.ingredients,
onChange: function( value ) {
props.setAttributes( { ingredients: value } );
},
className: 'ingredients',
} ),
el( 'h3', {}, i18n.__( 'Instructions', 'gutenberg-examples' ) ),
el( RichText, {
tagName: 'div',
inline: false,
placeholder: i18n.__( 'Write instructions…', 'gutenberg-examples' ),
value: attributes.instructions,
onChange: function( value ) {
props.setAttributes( { instructions: value } );
},
} )
)
);
},
save: function( props ) {
var attributes = props.attributes;
return (
el( 'div', { className: props.className },
el( RichText.Content, {
tagName: 'h2', value: attributes.title
} ),
attributes.mediaURL &&
el( 'div', { className: 'recipe-image' },
el( 'img', { src: attributes.mediaURL } ),
),
el( 'h3', {}, i18n.__( 'Ingredients', 'gutenberg-examples' ) ),
el( RichText.Content, {
tagName: 'ul', className: 'ingredients', value: attributes.ingredients
} ),
el( 'h3', {}, i18n.__( 'Instructions', 'gutenberg-examples' ) ),
el( RichText.Content, {
tagName: 'div', className: 'steps', value: attributes.instructions
} ),
)
);
},
} );
} )(
window.wp.blocks,
window.wp.editor,
window.wp.i18n,
window.wp.element,
window.wp.components,
window._,
);