/
edit.js
185 lines (169 loc) · 4.71 KB
/
edit.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/**
* External dependencies
*/
import classnames from 'classnames';
/**
* Internal dependencies
*/
import Controls from './controls';
import Inspector from './inspector';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { compose } from '@wordpress/compose';
import { mediaUpload } from '@wordpress/editor';
import { withSelect, select } from '@wordpress/data';
import { Component, Fragment } from '@wordpress/element';
import { Button, Dashicon, DropZone } from '@wordpress/components';
import { RichText, InnerBlocks, MediaUpload, MediaUploadCheck, withColors, withFontSizes } from '@wordpress/block-editor';
class AuthorEdit extends Component {
constructor() {
super( ...arguments );
this.addImage = this.addImage.bind( this );
this.onSelectImage = this.onSelectImage.bind( this );
}
onSelectImage( media ) {
if ( media && media.url ) {
this.props.setAttributes( { imgUrl: media.url } );
}
}
addImage( files ) {
mediaUpload( {
allowedTypes: [ 'image' ],
filesList: files,
onFileChange: ( [ media ] ) => this.onSelectImage( media ),
} );
}
render() {
const {
attributes,
backgroundColor,
className,
clientId,
fontSize,
isSelected,
selectedParentClientId,
setAttributes,
textColor,
} = this.props;
const {
biography,
imgUrl,
name,
} = attributes;
const hasImage = !! imgUrl;
const dropZone = (
<DropZone
onFilesDrop={ this.addImage }
/* translators: image to represent the post author */
label={ __( 'Drop to upload as avatar', 'coblocks' ) }
/>
);
const classes = classnames(
className, {
'has-background': backgroundColor.color,
'has-text-color': textColor.color,
[ backgroundColor.class ]: backgroundColor.class,
[ textColor.class ]: textColor.class,
}
);
const styles = {
backgroundColor: backgroundColor.color,
color: textColor.color,
fontSize: fontSize.size ? fontSize.size + 'px' : undefined,
};
const onUploadImage = ( media ) => setAttributes( { imgUrl: media.url, imgId: media.id } );
return (
<Fragment>
{ isSelected && (
<Controls
{ ...this.props }
/>
) }
{ isSelected && (
<Inspector
{ ...this.props }
/>
) }
<div className={ classes } style={ styles }>
{ dropZone }
{ ( !! isSelected || clientId === selectedParentClientId || hasImage ) ?
<figure className="wp-block-coblocks-author__avatar">
<MediaUploadCheck>
<MediaUpload
onSelect={ onUploadImage }
allowedTypes={ [ 'image' ] }
value={ imgUrl }
render={ ( { open } ) => (
<Button onClick={ open }>
{ ! imgUrl ?
<Dashicon icon="format-image" /> :
<img className="wp-block-coblocks-author__avatar-img"
src={ imgUrl }
alt="avatar"
/>
}
</Button>
) }
>
</MediaUpload>
</MediaUploadCheck>
</figure> : null }
<div className={ `${ className }__content` }>
<RichText
identifier="name"
multiline={ false }
tagName="span"
className="wp-block-coblocks-author__name"
placeholder={
/* translators: placeholder text used for the author name */
__( 'Write author name…', 'coblocks' )
}
value={ name }
onChange={ ( nextName ) => {
setAttributes( { name: nextName } );
} }
keepPlaceholderOnFocus={ true }
/>
<RichText
identifier="biography"
multiline={ false }
tagName="p"
className="wp-block-coblocks-author__biography"
placeholder={
/* translators: placeholder text used for the biography */
__( 'Write a biography that distills objective credibility and authority to your readers…', 'coblocks' )
}
value={ biography }
onChange={ ( nextBio ) => {
setAttributes( { biography: nextBio } );
} }
keepPlaceholderOnFocus={ true }
/>
<InnerBlocks
template={ [ [ 'core/button', { placeholder: /* translators: content placeholder */ __( 'Author link…', 'coblocks' ) } ] ] }
templateLock="all"
allowedBlocks={ [ 'core/button' ] }
templateInsertUpdatesSelection={ false }
/>
</div>
</div>
</Fragment>
);
}
}
const applyWithSelect = withSelect( () => {
const selectedClientId = select( 'core/block-editor' ).getBlockSelectionStart();
const parentClientId = select( 'core/block-editor' ).getBlockRootClientId(
selectedClientId
);
return {
selectedParentClientId: parentClientId,
};
} );
export default compose( [
withColors( 'backgroundColor', { textColor: 'color' } ),
withFontSizes( 'fontSize' ),
applyWithSelect,
] )( AuthorEdit );