/
edit.js
159 lines (151 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
/**
* External dependencies
*/
import { Component, Fragment } from '@wordpress/element';
import {
RichText,
BlockControls,
AlignmentToolbar,
MediaUpload,
getFontSizeClass,
withFontSizes,
InspectorControls,
FontSizePicker,
} from '@wordpress/editor';
import { Tooltip, Toolbar, PanelBody } from '@wordpress/components';
import { compose } from '@wordpress/compose';
import classnames from 'classnames';
import { filter } from 'lodash';
/**
* Internal dependencies
*/
import { __ } from '../../utils/i18n';
import './editor.scss';
const getKeyDownHandler = callback => event => {
// Accept only Enter and Space keys
if ( event.key === 'Enter' || event.key === ' ' ) {
callback( event );
}
};
class TestimonialBlock extends Component {
onChangeName = value => void this.props.setAttributes( { name: value } );
onChangeTitle = value => void this.props.setAttributes( { title: value } );
onChangeContent = value => void this.props.setAttributes( { content: value } );
onChangeAlign = value => void this.props.setAttributes( { align: value } );
onChangeMedia = media =>
void this.props.setAttributes( { mediaUrl: media.url, mediaId: media.id } );
render() {
const { attributes, isSelected, fontSize, setFontSize } = this.props;
const hasMedia = attributes.mediaUrl && attributes.mediaId;
const avatarTooltip = hasMedia ? __( 'Change avatar' ) : __( 'Add avatar' );
const fontSizeClass = getFontSizeClass( attributes.fontSize );
const styles = {
fontSize: fontSizeClass ? undefined : attributes.customFontSize,
};
const className = classnames( this.props.className, {
'has-media': hasMedia || isSelected,
[ `is-aligned-${ attributes.align }` ]: !! attributes.align,
[ fontSizeClass ]: fontSizeClass,
} );
return (
<Fragment>
<BlockControls>
<AlignmentToolbar value={ attributes.align } onChange={ this.onChangeAlign } />
<MediaUpload
allowedTypes={ [ 'image' ] }
onSelect={ this.onChangeMedia }
value={ attributes.mediaId }
render={ ( { open } ) => (
<Toolbar
controls={ filter( [
{
icon: 'edit',
title: avatarTooltip,
onClick: open,
},
hasMedia && {
icon: 'no-alt',
title: __( 'Remove avatar' ),
onClick: () => this.props.setAttributes( { mediaId: null, mediaUrl: null } ),
},
] ) }
/>
) }
/>
</BlockControls>
<InspectorControls>
<PanelBody title={ __( 'Size Settings' ) } className="blocks-font-size">
<FontSizePicker value={ fontSize.size } onChange={ setFontSize } />
</PanelBody>
</InspectorControls>
<div style={ styles } className={ className }>
<RichText
tagName="div"
value={ attributes.content }
onChange={ this.onChangeContent }
placeholder={
// translators: placeholder text used for the quote
__( 'Write testimonial…' )
}
wrapperClassName="wp-block-jetpack-testimonial__content"
/>
<div className="wp-block-jetpack-testimonial__author">
{ ( isSelected || hasMedia ) && (
<MediaUpload
allowedTypes={ [ 'image' ] }
onSelect={ this.onChangeMedia }
value={ attributes.mediaId }
render={ ( { open } ) => (
<Tooltip text={ avatarTooltip }>
<div
className={ classnames( 'wp-block-jetpack-testimonial__media', {
'is-placeholder': ! hasMedia,
} ) }
onClick={ open }
onKeyDown={ getKeyDownHandler( open ) }
role="button"
tabIndex="0"
>
{ hasMedia && (
<img
src={ attributes.mediaUrl }
width={ 50 }
height={ 50 }
alt={ attributes.name || '' }
/>
) }
</div>
</Tooltip>
) }
/>
) }
{ ( isSelected || attributes.name || attributes.title ) && (
<div className="wp-block-jetpack-testimonial__meta">
{ ( isSelected || attributes.name ) && (
<RichText
tagName="div"
value={ attributes.name }
onChange={ this.onChangeName }
placeholder={ 'Write name…' }
wrapperClassName="wp-block-jetpack-testimonial__name"
/>
) }
{ ( isSelected || attributes.title ) && (
<RichText
tagName="div"
value={ attributes.title }
onChange={ this.onChangeTitle }
placeholder={ 'Write title…' }
wrapperClassName="wp-block-jetpack-testimonial__title"
/>
) }
</div>
) }
</div>
</div>
</Fragment>
);
}
}
const TestimonialEdit = compose( [ withFontSizes( 'fontSize' ) ] )( TestimonialBlock );
export default TestimonialEdit;