-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathindex.native.js
316 lines (282 loc) · 7.6 KB
/
index.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
/**
* External dependencies
*/
import { Platform } from 'react-native';
import { delay } from 'lodash';
import prompt from 'react-native-prompt-android';
/**
* WordPress dependencies
*/
import { Component, React } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Picker } from '@wordpress/components';
import {
getOtherMediaOptions,
requestMediaPicker,
mediaSources,
} from '@wordpress/react-native-bridge';
import {
capturePhoto,
captureVideo,
image,
wordpress,
mobile,
globe,
} from '@wordpress/icons';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { compose } from '@wordpress/compose';
import { withSelect } from '@wordpress/data';
export const MEDIA_TYPE_IMAGE = 'image';
export const MEDIA_TYPE_VIDEO = 'video';
export const MEDIA_TYPE_AUDIO = 'audio';
export const MEDIA_TYPE_ANY = 'any';
export const OPTION_TAKE_VIDEO = __( 'Take a Video' );
export const OPTION_TAKE_PHOTO = __( 'Take a Photo' );
export const OPTION_TAKE_PHOTO_OR_VIDEO = __( 'Take a Photo or Video' );
export const OPTION_INSERT_FROM_URL = __( 'Insert from URL' );
const URL_MEDIA_SOURCE = 'URL';
const PICKER_OPENING_DELAY = 200;
export class MediaUpload extends Component {
constructor( props ) {
super( props );
this.onPickerPresent = this.onPickerPresent.bind( this );
this.onPickerSelect = this.onPickerSelect.bind( this );
this.getAllSources = this.getAllSources.bind( this );
this.state = {
otherMediaOptions: [],
};
}
componentDidMount() {
const { allowedTypes = [], autoOpen } = this.props;
getOtherMediaOptions( allowedTypes, ( otherMediaOptions ) => {
const otherMediaOptionsWithIcons = otherMediaOptions.map(
( option ) => {
return {
...option,
requiresModal: true,
types: allowedTypes,
id: option.value,
};
}
);
this.setState( { otherMediaOptions: otherMediaOptionsWithIcons } );
} );
if ( autoOpen ) {
this.onPickerPresent();
}
}
getAllSources() {
const cameraImageSource = {
id: mediaSources.deviceCamera, // ID is the value sent to native.
value: mediaSources.deviceCamera + '-IMAGE', // This is needed to diferenciate image-camera from video-camera sources.
label: __( 'Take a Photo' ),
requiresModal: true,
types: [ MEDIA_TYPE_IMAGE ],
icon: capturePhoto,
};
const cameraVideoSource = {
id: mediaSources.deviceCamera,
value: mediaSources.deviceCamera,
label: __( 'Take a Video' ),
requiresModal: true,
types: [ MEDIA_TYPE_VIDEO ],
icon: captureVideo,
};
const deviceLibrarySource = {
id: mediaSources.deviceLibrary,
value: mediaSources.deviceLibrary,
label: __( 'Choose from device' ),
requiresModal: true,
types: [ MEDIA_TYPE_IMAGE, MEDIA_TYPE_VIDEO ],
icon: image,
};
const siteLibrarySource = {
id: mediaSources.siteMediaLibrary,
value: mediaSources.siteMediaLibrary,
label: __( 'WordPress Media Library' ),
requiresModal: true,
types: [
MEDIA_TYPE_IMAGE,
MEDIA_TYPE_VIDEO,
MEDIA_TYPE_AUDIO,
MEDIA_TYPE_ANY,
],
icon: wordpress,
mediaLibrary: true,
};
const urlSource = {
id: URL_MEDIA_SOURCE,
value: URL_MEDIA_SOURCE,
label: __( 'Insert from URL' ),
types: [ MEDIA_TYPE_AUDIO ],
icon: globe,
};
const internalSources = [
deviceLibrarySource,
cameraImageSource,
cameraVideoSource,
siteLibrarySource,
urlSource,
];
return internalSources.concat( this.state.otherMediaOptions );
}
getMediaOptionsItems() {
const {
allowedTypes = [],
__experimentalOnlyMediaLibrary,
isAudioBlockMediaUploadEnabled,
} = this.props;
return this.getAllSources()
.filter( ( source ) => {
if ( __experimentalOnlyMediaLibrary ) {
return source.mediaLibrary;
} else if (
allowedTypes.every(
( allowedType ) =>
allowedType === MEDIA_TYPE_AUDIO &&
source.types.includes( allowedType )
) &&
source.id !== URL_MEDIA_SOURCE
) {
return isAudioBlockMediaUploadEnabled === true;
}
return allowedTypes.some( ( allowedType ) =>
source.types.includes( allowedType )
);
} )
.map( ( source ) => {
return {
...source,
icon: source.icon || this.getChooseFromDeviceIcon(),
};
} );
}
getChooseFromDeviceIcon() {
return mobile;
}
onPickerPresent() {
const { autoOpen } = this.props;
const isIOS = Platform.OS === 'ios';
if ( this.picker ) {
// the delay below is required because on iOS this action sheet gets dismissed by the close event of the Inserter
// so this delay allows the Inserter to be closed fully before presenting action sheet.
if ( autoOpen && isIOS ) {
delay(
() => this.picker.presentPicker(),
PICKER_OPENING_DELAY
);
} else {
this.picker.presentPicker();
}
}
}
onPickerSelect( value ) {
const {
allowedTypes = [],
onSelect,
onSelectURL,
multiple = false,
} = this.props;
if ( value === URL_MEDIA_SOURCE ) {
prompt(
__( 'Type a URL' ), // title
undefined, // message
[
{
text: __( 'Cancel' ),
style: 'cancel',
},
{
text: __( 'Apply' ),
onPress: onSelectURL,
},
], // Buttons.
'plain-text', // type
undefined, // defaultValue
'url' // keyboardType
);
return;
}
const mediaSource = this.getAllSources()
.filter( ( source ) => source.value === value )
.shift();
const types = allowedTypes.filter( ( type ) =>
mediaSource.types.includes( type )
);
requestMediaPicker( mediaSource.id, types, multiple, ( media ) => {
if ( ( multiple && media ) || ( media && media.id ) ) {
onSelect( media );
}
} );
}
render() {
const { allowedTypes = [], isReplacingMedia, multiple } = this.props;
const isOneType = allowedTypes.length === 1;
const isImage = isOneType && allowedTypes.includes( MEDIA_TYPE_IMAGE );
const isVideo = isOneType && allowedTypes.includes( MEDIA_TYPE_VIDEO );
const isAudio = isOneType && allowedTypes.includes( MEDIA_TYPE_AUDIO );
const isAnyType = isOneType && allowedTypes.includes( MEDIA_TYPE_ANY );
const isImageOrVideo =
allowedTypes.length === 2 &&
allowedTypes.includes( MEDIA_TYPE_IMAGE ) &&
allowedTypes.includes( MEDIA_TYPE_VIDEO );
let pickerTitle;
if ( isImage ) {
if ( isReplacingMedia ) {
pickerTitle = __( 'Replace image' );
} else {
pickerTitle = multiple
? __( 'Choose images' )
: __( 'Choose image' );
}
} else if ( isVideo ) {
if ( isReplacingMedia ) {
pickerTitle = __( 'Replace video' );
} else {
pickerTitle = __( 'Choose video' );
}
} else if ( isImageOrVideo ) {
if ( isReplacingMedia ) {
pickerTitle = __( 'Replace image or video' );
} else {
pickerTitle = __( 'Choose image or video' );
}
} else if ( isAudio ) {
if ( isReplacingMedia ) {
pickerTitle = __( 'Replace audio' );
} else {
pickerTitle = __( 'Choose audio' );
}
} else if ( isAnyType ) {
pickerTitle = __( 'Choose file' );
if ( isReplacingMedia ) {
pickerTitle = __( 'Replace file' );
} else {
pickerTitle = __( 'Choose file' );
}
}
const getMediaOptions = () => (
<Picker
title={ pickerTitle }
hideCancelButton
ref={ ( instance ) => ( this.picker = instance ) }
options={ this.getMediaOptionsItems() }
onChange={ this.onPickerSelect }
testID="media-options-picker"
/>
);
return this.props.render( {
open: this.onPickerPresent,
getMediaOptions,
} );
}
}
export default compose( [
withSelect( ( select ) => {
return {
isAudioBlockMediaUploadEnabled:
select( blockEditorStore ).getSettings( 'capabilities' )
.isAudioBlockMediaUploadEnabled === true,
};
} ),
] )( MediaUpload );