-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathblock.js
259 lines (239 loc) · 7.9 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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/**
* External dependencies
*/
import classnames from 'classnames';
import ResizableBox from 'react-resizable-box';
import { startCase, findKey } from 'lodash';
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { Component } from '@wordpress/element';
import { mediaUpload } from '@wordpress/utils';
import { Placeholder, Dashicon, Toolbar, DropZone, FormFileUpload } from '@wordpress/components';
/**
* Internal dependencies
*/
import withEditorSettings from '../../with-editor-settings';
import Editable from '../../editable';
import MediaUploadButton from '../../media-upload-button';
import InspectorControls from '../../inspector-controls';
import TextControl from '../../inspector-controls/text-control';
import SelectControl from '../../inspector-controls/select-control';
import BlockControls from '../../block-controls';
import BlockAlignmentToolbar from '../../block-alignment-toolbar';
import BlockDescription from '../../block-description';
import UrlInputButton from '../../url-input/button';
import ImageSize from './image-size';
class ImageBlock extends Component {
constructor() {
super( ...arguments );
this.updateAlt = this.updateAlt.bind( this );
this.updateAlignment = this.updateAlignment.bind( this );
this.onSelectImage = this.onSelectImage.bind( this );
this.onSetHref = this.onSetHref.bind( this );
this.updateImageSize = this.updateImageSize.bind( this );
this.state = {
availableSizes: {},
};
}
componentDidMount() {
if ( this.props.attributes.id ) {
this.fetchMedia( this.props.attributes.id );
}
}
componentWillUnmout() {
if ( this.fetchImageRequest ) {
this.fetchImageRequest.abort();
}
}
onSelectImage( media ) {
this.props.setAttributes( { url: media.url, alt: media.alt, caption: media.caption, id: media.id } );
this.fetchMedia( media.id );
}
onSetHref( value ) {
this.props.setAttributes( { href: value } );
}
updateAlt( newAlt ) {
this.props.setAttributes( { alt: newAlt } );
}
updateAlignment( nextAlign ) {
const extraUpdatedAttributes = [ 'wide', 'full' ].indexOf( nextAlign ) !== -1
? { width: undefined, height: undefined }
: {};
this.props.setAttributes( { ...extraUpdatedAttributes, align: nextAlign } );
}
fetchMedia( id ) {
if ( this.fetchImageRequest ) {
this.fetchImageRequest.abort();
}
this.fetchImageRequest = new wp.api.models.Media( { id } ).fetch();
this.fetchImageRequest.then( ( image ) => {
this.setState( {
availableSizes: image.media_details.sizes,
} );
} );
}
updateImageSize( selectedSize ) {
this.props.setAttributes( {
url: this.state.availableSizes[ selectedSize ].source_url,
} );
}
render() {
const { attributes, setAttributes, focus, setFocus, className, settings } = this.props;
const { url, alt, caption, align, id, href, width, height } = attributes;
const availableSizes = Object.keys( this.state.availableSizes ).sort();
const selectedSize = findKey( this.state.availableSizes, ( size ) => size.source_url === url );
const isResizable = [ 'wide', 'full' ].indexOf( align ) === -1;
const uploadButtonProps = { isLarge: true };
const uploadFromFiles = ( event ) => mediaUpload( event.target.files, setAttributes );
const dropFiles = ( files ) => mediaUpload( files, setAttributes );
const controls = (
focus && (
<BlockControls key="controls">
<BlockAlignmentToolbar
value={ align }
onChange={ this.updateAlignment }
/>
<Toolbar>
<li>
<MediaUploadButton
buttonProps={ {
className: 'components-icon-button components-toolbar__control',
'aria-label': __( 'Edit image' ),
} }
onSelect={ this.onSelectImage }
type="image"
value={ id }
>
<Dashicon icon="edit" />
</MediaUploadButton>
</li>
<UrlInputButton onChange={ this.onSetHref } url={ href } />
</Toolbar>
</BlockControls>
)
);
if ( ! url ) {
return [
controls,
<Placeholder
key="placeholder"
instructions={ __( 'Drag image here or insert from media library' ) }
icon="format-image"
label={ __( 'Image' ) }
className={ className }>
<DropZone
onFilesDrop={ dropFiles }
/>
<FormFileUpload
isLarge
className="wp-block-image__upload-button"
onChange={ uploadFromFiles }
accept="image/*"
>
{ __( 'Upload' ) }
</FormFileUpload>
<MediaUploadButton
buttonProps={ uploadButtonProps }
onSelect={ this.onSelectImage }
type="image"
>
{ __( 'Insert from Media Library' ) }
</MediaUploadButton>
</Placeholder>,
];
}
const focusCaption = ( focusValue ) => setFocus( { editable: 'caption', ...focusValue } );
const classes = classnames( className, {
'is-transient': 0 === url.indexOf( 'blob:' ),
'is-resized': !! width,
'is-focused': !! focus,
} );
// Disable reason: Each block can be selected by clicking on it
/* eslint-disable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */
return [
controls,
focus && (
<InspectorControls key="inspector">
<BlockDescription>
<p>{ __( 'Worth a thousand words.' ) }</p>
</BlockDescription>
<h3>{ __( 'Image Settings' ) }</h3>
<TextControl label={ __( 'Alternate Text' ) } value={ alt } onChange={ this.updateAlt } />
{ !! availableSizes.length && (
<SelectControl
label={ __( 'Size' ) }
value={ selectedSize || '' }
options={ availableSizes.map( ( imageSize ) => ( {
value: imageSize,
label: startCase( imageSize ),
} ) ) }
onChange={ this.updateImageSize }
/>
) }
</InspectorControls>
),
<figure key="image" className={ classes }>
<ImageSize src={ url } dirtynessTrigger={ align }>
{ ( sizes ) => {
const {
imageWidthWithinContainer,
imageHeightWithinContainer,
imageWidth,
imageHeight,
} = sizes;
const currentWidth = width || imageWidthWithinContainer;
const currentHeight = height || imageHeightWithinContainer;
const img = <img src={ url } alt={ alt } onClick={ setFocus } />;
if ( ! isResizable || ! imageWidthWithinContainer ) {
return img;
}
const ratio = imageWidth / imageHeight;
const minWidth = imageWidth < imageHeight ? 10 : 10 * ratio;
const minHeight = imageHeight < imageWidth ? 10 : 10 / ratio;
return (
<ResizableBox
width={ currentWidth }
height={ currentHeight }
minWidth={ minWidth }
maxWidth={ settings.maxWidth }
minHeight={ minHeight }
maxHeight={ settings.maxWidth / ratio }
lockAspectRatio
handlerClasses={ {
topRight: 'wp-block-image__resize-handler-top-right',
bottomRight: 'wp-block-image__resize-handler-bottom-right',
topLeft: 'wp-block-image__resize-handler-top-left',
bottomLeft: 'wp-block-image__resize-handler-bottom-left',
} }
enable={ { top: false, right: true, bottom: false, left: false, topRight: true, bottomRight: true, bottomLeft: true, topLeft: true } }
onResize={ ( event, direction, elt ) => {
setAttributes( {
width: elt.clientWidth,
height: elt.clientHeight,
} );
} }
>
{ img }
</ResizableBox>
);
} }
</ImageSize>
{ ( caption && caption.length > 0 ) || !! focus ? (
<Editable
tagName="figcaption"
placeholder={ __( 'Write caption…' ) }
value={ caption }
focus={ focus && focus.editable === 'caption' ? focus : undefined }
onFocus={ focusCaption }
onChange={ ( value ) => setAttributes( { caption: value } ) }
inlineToolbar
/>
) : null }
</figure>,
];
/* eslint-enable jsx-a11y/no-static-element-interactions, jsx-a11y/onclick-has-role, jsx-a11y/click-events-have-key-events */
}
}
export default withEditorSettings()( ImageBlock );