-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathedit.native.js
455 lines (403 loc) · 12.6 KB
/
edit.native.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/**
* External dependencies
*/
import React from 'react';
import { View, ImageBackground, Text, TouchableWithoutFeedback, Dimensions } from 'react-native';
import {
requestMediaImport,
mediaUploadSync,
requestImageFailedRetryDialog,
requestImageUploadCancelDialog,
} from 'react-native-gutenberg-bridge';
import { isEmpty } from 'lodash';
/**
* WordPress dependencies
*/
import {
BottomSheet,
Icon,
Toolbar,
ToolbarButton,
Picker,
} from '@wordpress/components';
import {
Caption,
MediaPlaceholder,
MediaUpload,
MEDIA_TYPE_IMAGE,
BlockControls,
InspectorControls,
} from '@wordpress/block-editor';
import { __, sprintf } from '@wordpress/i18n';
import { isURL } from '@wordpress/url';
import { doAction, hasAction } from '@wordpress/hooks';
/**
* Internal dependencies
*/
import styles from './styles.scss';
import MediaUploadProgress from './media-upload-progress';
import SvgIcon from './icon';
import SvgIconRetry from './icon-retry';
const LINK_DESTINATION_CUSTOM = 'custom';
const LINK_DESTINATION_NONE = 'none';
const IMAGE_SIZE_THUMBNAIL = 'thumbnail';
const IMAGE_SIZE_MEDIUM = 'medium';
const IMAGE_SIZE_LARGE = 'large';
const IMAGE_SIZE_FULL_SIZE = 'full';
const DEFAULT_SIZE_SLUG = IMAGE_SIZE_LARGE;
//If this value is below ~25, you start to see problems with ActionSheetIOS
const BOTTOM_SHEET_TO_PICKER_ANIMATION_DELAY_MILLISECONDS = 50;
// Default Image ratio 4:3
const IMAGE_ASPECT_RATIO = 4 / 3;
class ImageEdit extends React.Component {
menuTransitionTimeout = 0;
constructor( props ) {
super( props );
this.state = {
showSettings: false,
isCaptionSelected: false,
};
this.finishMediaUploadWithSuccess = this.finishMediaUploadWithSuccess.bind( this );
this.finishMediaUploadWithFailure = this.finishMediaUploadWithFailure.bind( this );
this.mediaUploadStateReset = this.mediaUploadStateReset.bind( this );
this.onSelectMediaUploadOption = this.onSelectMediaUploadOption.bind( this );
this.updateMediaProgress = this.updateMediaProgress.bind( this );
this.updateAlt = this.updateAlt.bind( this );
this.updateImageURL = this.updateImageURL.bind( this );
this.onSetLinkDestination = this.onSetLinkDestination.bind( this );
this.onSetSizeSlug = this.onSetSizeSlug.bind( this );
this.onImagePressed = this.onImagePressed.bind( this );
this.onClearSettings = this.onClearSettings.bind( this );
this.onFocusCaption = this.onFocusCaption.bind( this );
}
componentDidMount() {
const { attributes, setAttributes } = this.props;
// This will warn when we have `id` defined, while `url` is undefined.
// This may help track this issue: https://github.com/wordpress-mobile/WordPress-Android/issues/9768
// where a cancelled image upload was resulting in a subsequent crash.
if ( attributes.id && ! attributes.url ) {
// eslint-disable-next-line no-console
console.warn( 'Attributes has id with no url.' );
}
if ( attributes.id && attributes.url && ! isURL( attributes.url ) ) {
if ( attributes.url.indexOf( 'file:' ) === 0 ) {
requestMediaImport( attributes.url, ( mediaId, mediaUri ) => {
if ( mediaUri ) {
setAttributes( { url: mediaUri, id: mediaId } );
}
} );
}
mediaUploadSync();
}
}
componentWillUnmount() {
// this action will only exist if the user pressed the trash button on the block holder
if ( hasAction( 'blocks.onRemoveBlockCheckUpload' ) && this.state.isUploadInProgress ) {
doAction( 'blocks.onRemoveBlockCheckUpload', this.props.attributes.id );
}
clearTimeout( this.menuTransitionTimeout );
}
static getDerivedStateFromProps( props, state ) {
// Avoid a UI flicker in the toolbar by insuring that isCaptionSelected
// is updated immediately any time the isSelected prop becomes false
return {
isCaptionSelected: props.isSelected && state.isCaptionSelected,
};
}
onImagePressed() {
const { attributes } = this.props;
if ( this.state.isUploadInProgress ) {
requestImageUploadCancelDialog( attributes.id );
} else if ( attributes.id && ! isURL( attributes.url ) ) {
requestImageFailedRetryDialog( attributes.id );
}
this.setState( {
isCaptionSelected: false,
} );
}
updateMediaProgress( payload ) {
const { setAttributes } = this.props;
if ( payload.mediaUrl ) {
setAttributes( { url: payload.mediaUrl } );
}
if ( ! this.state.isUploadInProgress ) {
this.setState( { isUploadInProgress: true } );
}
}
finishMediaUploadWithSuccess( payload ) {
const { setAttributes } = this.props;
setAttributes( { url: payload.mediaUrl, id: payload.mediaServerId } );
this.setState( { isUploadInProgress: false } );
}
finishMediaUploadWithFailure( payload ) {
const { setAttributes } = this.props;
setAttributes( { id: payload.mediaId } );
this.setState( { isUploadInProgress: false } );
}
mediaUploadStateReset() {
const { setAttributes } = this.props;
setAttributes( { id: null, url: null } );
this.setState( { isUploadInProgress: false } );
}
updateAlt( newAlt ) {
this.props.setAttributes( { alt: newAlt } );
}
updateImageURL( url ) {
this.props.setAttributes( { url, width: undefined, height: undefined } );
}
onSetLinkDestination( href ) {
this.props.setAttributes( {
linkDestination: LINK_DESTINATION_CUSTOM,
href,
} );
}
onSetSizeSlug( sizeSlug ) {
this.props.setAttributes( {
sizeSlug,
} );
}
onClearSettings() {
this.props.setAttributes( {
alt: '',
linkDestination: LINK_DESTINATION_NONE,
href: undefined,
sizeSlug: DEFAULT_SIZE_SLUG,
} );
}
onSelectMediaUploadOption( mediaId, mediaUrl ) {
const { setAttributes } = this.props;
setAttributes( { url: mediaUrl, id: mediaId } );
}
onFocusCaption() {
if ( this.props.onFocus ) {
this.props.onFocus();
}
if ( ! this.state.isCaptionSelected ) {
this.setState( {
isCaptionSelected: true,
} );
}
}
getIcon( isRetryIcon ) {
if ( isRetryIcon ) {
return <Icon icon={ SvgIconRetry } { ...styles.iconRetry } />;
}
return <Icon icon={ SvgIcon } { ...styles.icon } />;
}
render() {
const { attributes, isSelected } = this.props;
const { url, height, width, alt, href, id, sizeSlug } = attributes;
const onImageSettingsButtonPressed = () => {
this.setState( { showSettings: true } );
};
const onImageSettingsClose = () => {
this.setState( { showSettings: false } );
};
const getToolbarEditButton = ( open ) => (
<BlockControls>
<Toolbar>
<ToolbarButton
title={ __( 'Edit image' ) }
icon="edit"
onClick={ open }
/>
</Toolbar>
</BlockControls>
);
const getInspectorControls = () => (
<BottomSheet
isVisible={ this.state.showSettings }
onClose={ onImageSettingsClose }
hideHeader
>
<BottomSheet.Cell
icon={ 'admin-links' }
label={ __( 'Link To' ) }
value={ href || '' }
valuePlaceholder={ __( 'Add URL' ) }
onChangeValue={ this.onSetLinkDestination }
autoCapitalize="none"
autoCorrect={ false }
keyboardType="url"
/>
<BottomSheet.Cell
icon={ 'empty' }
label={ __( 'Size' ) }
labelStyle={ styles.sizeInspectorControlsCell }
value={ getSizeSlugDisplay( sizeSlug ) }
editable={ false }
onChangeValue={ this.onSetLinkDestination }
onPress={ onPickerPresent }
/>
<BottomSheet.Cell
icon={ 'editor-textcolor' }
label={ __( 'Alt Text' ) }
value={ alt || '' }
valuePlaceholder={ __( 'None' ) }
separatorType={ 'fullWidth' }
onChangeValue={ this.updateAlt }
/>
<BottomSheet.Cell
label={ __( 'Clear All Settings' ) }
labelStyle={ styles.clearSettingsButton }
separatorType={ 'none' }
onPress={ this.onClearSettings }
/>
</BottomSheet>
);
//Used for display of Inspector Controls
const getSizeSlugDisplay = ( sizeSlugValue ) => {
if ( sizeSlugValue === undefined ) {
sizeSlugValue = DEFAULT_SIZE_SLUG;
}
const sizeDisplayItems = getSizeOptionsItems();
switch ( sizeSlugValue ) {
case IMAGE_SIZE_THUMBNAIL:
return sizeDisplayItems[ 0 ].label;
case IMAGE_SIZE_MEDIUM:
return sizeDisplayItems[ 1 ].label;
case IMAGE_SIZE_FULL_SIZE:
return sizeDisplayItems[ 3 ].label;
case IMAGE_SIZE_LARGE:
default:
return sizeDisplayItems[ 2 ].label;
}
};
const getSizeOptionsItems = () => {
return [
{ value: IMAGE_SIZE_THUMBNAIL, label: __( 'Thumbnail' ) },
{ value: IMAGE_SIZE_MEDIUM, label: __( 'Medium' ) },
{ value: IMAGE_SIZE_LARGE, label: __( 'Large' ) },
{ value: IMAGE_SIZE_FULL_SIZE, label: __( 'Full Size' ) },
];
};
let picker;
const getSizeOptions = () => (
<Picker
hideCancelButton={ true }
ref={ ( instance ) => picker = instance }
options={ getSizeOptionsItems() }
onChange={ ( value ) => {
this.onSetSizeSlug( value );
} }
/>
);
const onPickerPresent = () => {
const pickerInstance = picker;
this.setState( {
showSettings: false,
}, () => {
this.menuTransitionTimeout = setTimeout( function() {
pickerInstance.presentPicker();
}, BottomSheet.ANIMATION_OUT_DURATION_MILLISECONDS + BOTTOM_SHEET_TO_PICKER_ANIMATION_DELAY_MILLISECONDS );
} );
};
if ( ! url ) {
return (
<View style={ { flex: 1 } } >
<MediaPlaceholder
mediaType={ MEDIA_TYPE_IMAGE }
onSelectURL={ this.onSelectMediaUploadOption }
icon={ this.getIcon( false ) }
onFocus={ this.props.onFocus }
/>
</View>
);
}
const imageContainerHeight = Dimensions.get( 'window' ).width / IMAGE_ASPECT_RATIO;
const getImageComponent = ( openMediaOptions, getMediaOptions ) => (
<TouchableWithoutFeedback
accessible={ ! isSelected }
onPress={ this.onImagePressed }
onLongPress={ openMediaOptions }
disabled={ ! isSelected }
>
<View style={ { flex: 1 } }>
{ getInspectorControls() }
{ getSizeOptions() }
{ getMediaOptions() }
{ ( ! this.state.isCaptionSelected ) &&
getToolbarEditButton( openMediaOptions )
}
<InspectorControls>
<ToolbarButton
title={ __( 'Image Settings' ) }
icon="admin-generic"
onClick={ onImageSettingsButtonPressed }
/>
</InspectorControls>
<MediaUploadProgress
height={ height }
width={ width }
coverUrl={ url }
mediaId={ id }
onUpdateMediaProgress={ this.updateMediaProgress }
onFinishMediaUploadWithSuccess={ this.finishMediaUploadWithSuccess }
onFinishMediaUploadWithFailure={ this.finishMediaUploadWithFailure }
onMediaUploadStateReset={ this.mediaUploadStateReset }
renderContent={ ( { isUploadInProgress, isUploadFailed, finalWidth, finalHeight, imageWidthWithinContainer, retryMessage } ) => {
const opacity = isUploadInProgress ? 0.3 : 1;
const icon = this.getIcon( isUploadFailed );
const iconContainer = (
<View style={ styles.modalIcon }>
{ icon }
</View>
);
return (
<View style={ { flex: 1 } } >
{ ! imageWidthWithinContainer &&
<View style={ [ styles.imageContainer, { height: imageContainerHeight } ] } >
{ this.getIcon( false ) }
</View> }
<ImageBackground
accessible={ true }
disabled={ ! isSelected }
accessibilityLabel={ alt }
accessibilityHint={ __( 'Double tap and hold to edit' ) }
accessibilityRole={ 'imagebutton' }
style={ { width: finalWidth, height: finalHeight, opacity } }
resizeMethod="scale"
source={ { uri: url } }
key={ url }
>
{ isUploadFailed &&
<View style={ [ styles.imageContainer, { flex: 1, backgroundColor: 'rgba(0, 0, 0, 0.5)' } ] } >
{ iconContainer }
<Text style={ styles.uploadFailedText }>{ retryMessage }</Text>
</View>
}
</ImageBackground>
</View>
);
} }
/>
<Caption
clientId={ this.props.clientId }
isSelected={ this.state.isCaptionSelected }
accessible={ true }
accessibilityLabelCreator={ ( caption ) =>
isEmpty( caption ) ?
/* translators: accessibility text. Empty image caption. */
( 'Image caption. Empty' ) :
sprintf(
/* translators: accessibility text. %s: image caption. */
__( 'Image caption. %s' ),
caption )
}
onFocus={ this.onFocusCaption }
onBlur={ this.props.onBlur } // always assign onBlur as props
/>
</View>
</TouchableWithoutFeedback>
);
return (
<MediaUpload mediaType={ MEDIA_TYPE_IMAGE }
onSelectURL={ this.onSelectMediaUploadOption }
render={ ( { open, getMediaOptions } ) => {
return getImageComponent( open, getMediaOptions );
} }
/>
);
}
}
export default ImageEdit;