Skip to content

Commit

Permalink
[RNMobile] Simplify media insertion flow Part 2 - media upload (#29547)
Browse files Browse the repository at this point in the history
* added autoOpenMediaUpload prop to the MediaPlaceholder

* Added the auto-opening capabilities to the MediaUpload component.

* Added documentation for the new autoOpenMediaUpload prop

* renamed autoOpenMediaUpload to autoOpen in the MediaUpload component.

* [RNMobile] Simplify media insertion flow - Part 3 component integration (#29548)

* Track the clientId of the block that is inserted.

* implemented auto opening utilizing last block inserted from the store

* added dismissal support for the auto opening picker to the UI tests.

* Updated Dismiss button in closePicker function to Cancel
  • Loading branch information
jd-alexander authored Mar 16, 2021
1 parent e3fefb6 commit ee4d502
Show file tree
Hide file tree
Showing 11 changed files with 147 additions and 10 deletions.
4 changes: 4 additions & 0 deletions packages/block-editor/src/components/inserter/menu.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ function InserterMenu( {
insertDefaultBlock,
} = useDispatch( blockEditorStore );

const { addLastBlockInserted } = useDispatch( 'core/editor' );

const {
items,
destinationRootClientId,
Expand Down Expand Up @@ -110,6 +112,8 @@ function InserterMenu( {
innerBlocks
);

addLastBlockInserted( newBlock.clientId );

insertBlock( newBlock, insertionIndex, destinationRootClientId );
},
[ insertBlock, destinationRootClientId, insertionIndex ]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ This property is similar to the `accept` property. The difference is the format
- Required: No
- Platform: Web | Mobile

### autoOpenMediaUpload

If true, the MediaUpload component auto-opens the picker of the respective platform.

- Type: `Boolean`
- Required: No
- Default: `false`
- Platform: Mobile

### className

Class name added to the placeholder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function MediaPlaceholder( props ) {
height,
backgroundColor,
hideContent,
autoOpenMediaUpload,
} = props;

// use ref to keep media array current for callbacks during rerenders
Expand Down Expand Up @@ -160,6 +161,7 @@ function MediaPlaceholder( props ) {
}
multiple={ multiple }
isReplacingMedia={ false }
autoOpen={ autoOpenMediaUpload }
render={ ( { open, getMediaOptions } ) => {
return (
<TouchableWithoutFeedback
Expand Down
9 changes: 9 additions & 0 deletions packages/block-editor/src/components/media-upload/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ Only applies if `gallery === true`.
- Default: `false`
- Platform: Web

### autoOpen

If true, the picker of the respective platform auto-opens.

- Type: `Boolean`
- Required: No
- Default: `false`
- Platform: Mobile

### gallery

If true, the component will initiate all the states required to represent a gallery. By default, the media modal opens in the gallery edit frame, but that can be changed using the `addToGallery`flag.
Expand Down
32 changes: 28 additions & 4 deletions packages/block-editor/src/components/media-upload/index.native.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
/**
* External dependencies
*/
import { Platform } from 'react-native';

import { delay } from 'lodash';

/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { Component, React } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { Picker } from '@wordpress/components';
import {
Expand All @@ -26,20 +33,21 @@ 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' );

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 = [] } = this.props;
const { allowedTypes = [], autoOpen } = this.props;
getOtherMediaOptions( allowedTypes, ( otherMediaOptions ) => {
const otherMediaOptionsWithIcons = otherMediaOptions.map(
( option ) => {
Expand All @@ -54,6 +62,10 @@ export class MediaUpload extends Component {

this.setState( { otherMediaOptions: otherMediaOptionsWithIcons } );
} );

if ( autoOpen ) {
this.onPickerPresent();
}
}

getAllSources() {
Expand Down Expand Up @@ -136,8 +148,20 @@ export class MediaUpload extends Component {
}

onPickerPresent() {
const { autoOpen } = this.props;
const isIOS = Platform.OS === 'ios';

if ( this.picker ) {
this.picker.presentPicker();
// 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();
}
}
}

Expand Down
22 changes: 21 additions & 1 deletion packages/block-library/src/gallery/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import {
import { Platform, useEffect, useState, useMemo } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import { getBlobByURL, isBlobURL, revokeBlobURL } from '@wordpress/blob';
import { useDispatch, withSelect } from '@wordpress/data';
import { useDispatch, withSelect, withDispatch } from '@wordpress/data';
import { withViewportMatch } from '@wordpress/viewport';
import { View } from '@wordpress/primitives';
import { store as coreStore } from '@wordpress/core-data';
Expand Down Expand Up @@ -81,6 +81,7 @@ function GalleryEdit( props ) {
imageSizes,
resizedImages,
onFocus,
wasBlockJustInserted,
} = props;
const {
columns = defaultColumnsNumber( attributes ),
Expand Down Expand Up @@ -343,6 +344,9 @@ function GalleryEdit( props ) {
onError={ onUploadError }
notices={ hasImages ? undefined : noticeUI }
onFocus={ onFocus }
autoOpenMediaUpload={
! hasImages && isSelected && wasBlockJustInserted()
}
/>
);

Expand Down Expand Up @@ -466,6 +470,22 @@ export default compose( [
resizedImages,
};
} ),
withDispatch( ( dispatch, { clientId }, { select } ) => {
return {
wasBlockJustInserted() {
const { clearLastBlockInserted } = dispatch( 'core/editor' );
const { wasBlockJustInserted } = select( 'core/editor' );

const result = wasBlockJustInserted( clientId );

if ( result ) {
clearLastBlockInserted();
return true;
}
return false;
},
};
} ),
withNotices,
withViewportMatch( { isNarrow: '< small' } ),
] )( GalleryEdit );
29 changes: 27 additions & 2 deletions packages/block-library/src/image/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import { __, sprintf } from '@wordpress/i18n';
import { getProtocol, hasQueryArg } from '@wordpress/url';
import { doAction, hasAction } from '@wordpress/hooks';
import { compose, withPreferredColorScheme } from '@wordpress/compose';
import { withSelect } from '@wordpress/data';
import { withSelect, withDispatch } from '@wordpress/data';
import {
image as placeholderIcon,
textColor,
Expand Down Expand Up @@ -399,7 +399,13 @@ export class ImageEdit extends Component {

render() {
const { isCaptionSelected } = this.state;
const { attributes, isSelected, image, clientId } = this.props;
const {
attributes,
isSelected,
image,
clientId,
wasBlockJustInserted,
} = this.props;
const { align, url, alt, id, sizeSlug, className } = attributes;

const sizeOptionsValid = find( this.sizeOptions, [
Expand Down Expand Up @@ -461,6 +467,9 @@ export class ImageEdit extends Component {
onSelect={ this.onSelectMediaUploadOption }
icon={ this.getPlaceholderIcon() }
onFocus={ this.props.onFocus }
autoOpenMediaUpload={
isSelected && ! url && wasBlockJustInserted()
}
/>
</View>
);
Expand Down Expand Up @@ -579,5 +588,21 @@ export default compose( [
imageSizes,
};
} ),
withDispatch( ( dispatch, { clientId }, { select } ) => {
return {
wasBlockJustInserted() {
const { clearLastBlockInserted } = dispatch( 'core/editor' );
const { wasBlockJustInserted } = select( 'core/editor' );

const result = wasBlockJustInserted( clientId );

if ( result ) {
clearLastBlockInserted();
return true;
}
return false;
},
};
} ),
withPreferredColorScheme,
] )( ImageEdit );
33 changes: 30 additions & 3 deletions packages/block-library/src/video/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
ToolbarGroup,
PanelBody,
} from '@wordpress/components';
import { withPreferredColorScheme } from '@wordpress/compose';
import { withPreferredColorScheme, compose } from '@wordpress/compose';
import {
BlockCaption,
MediaPlaceholder,
Expand All @@ -35,6 +35,7 @@ import { __, sprintf } from '@wordpress/i18n';
import { isURL, getProtocol } from '@wordpress/url';
import { doAction, hasAction } from '@wordpress/hooks';
import { video as SvgIcon, replace } from '@wordpress/icons';
import { withDispatch } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -189,7 +190,12 @@ class VideoEdit extends Component {
}

render() {
const { setAttributes, attributes, isSelected } = this.props;
const {
setAttributes,
attributes,
isSelected,
wasBlockJustInserted,
} = this.props;
const { id, src } = attributes;
const { videoContainerHeight } = this.state;

Expand Down Expand Up @@ -221,6 +227,9 @@ class VideoEdit extends Component {
onSelect={ this.onSelectMediaUploadOption }
icon={ this.getIcon( ICON_TYPE.PLACEHOLDER ) }
onFocus={ this.props.onFocus }
autoOpenMediaUpload={
isSelected && ! src && wasBlockJustInserted()
}
/>
</View>
);
Expand Down Expand Up @@ -361,4 +370,22 @@ class VideoEdit extends Component {
}
}

export default withPreferredColorScheme( VideoEdit );
export default compose( [
withDispatch( ( dispatch, { clientId }, { select } ) => {
return {
wasBlockJustInserted() {
const { clearLastBlockInserted } = dispatch( 'core/editor' );
const { wasBlockJustInserted } = select( 'core/editor' );

const result = wasBlockJustInserted( clientId );

if ( result ) {
clearLastBlockInserted();
return true;
}
return false;
},
};
} ),
withPreferredColorScheme,
] )( VideoEdit );
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import { blockNames } from './pages/editor-page';
describe( 'Gutenberg Editor Gallery Block tests', () => {
it( 'should be able to add a gallery block', async () => {
await editorPage.addNewBlock( blockNames.gallery );
await editorPage.driver.sleep( 1000 );
await editorPage.closePicker();

const galleryBlock = await editorPage.getBlockAtPosition(
blockNames.gallery
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import testData from './helpers/test-data';
describe( 'Gutenberg Editor Image Block tests', () => {
it( 'should be able to add an image block', async () => {
await editorPage.addNewBlock( blockNames.image );
await editorPage.driver.sleep( 1000 );
await editorPage.closePicker();

let imageBlock = await editorPage.getBlockAtPosition(
blockNames.image
);
Expand Down
11 changes: 11 additions & 0 deletions packages/react-native-editor/__device-tests__/pages/editor-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,17 @@ class EditorPage {
return await typeString( this.driver, textViewElement, text, clear );
}

async closePicker() {
if ( isAndroid() ) {
await swipeDown( this.driver );
} else {
const cancelButton = await this.driver.elementByAccessibilityId(
'Cancel'
);
await cancelButton.click();
}
}

// =============================
// Unsupported Block functions
// =============================
Expand Down

0 comments on commit ee4d502

Please sign in to comment.