Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RNMobile] Simplify media insertion flow Part 1 - redux changes #29546

Merged
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
6742b75
created actions for adding and clearing last inserted block event.
jd-alexander Mar 4, 2021
fc8d9b4
added reducer for determining new state based on the action
jd-alexander Mar 4, 2021
fb115ae
added selector to query the state for the last block inserted
jd-alexander Mar 4, 2021
e3fefb6
Merge branch 'trunk' into rnmobile/simplify_image_insertion_flow-redu…
jd-alexander Mar 16, 2021
ee4d502
[RNMobile] Simplify media insertion flow Part 2 - media upload (#29547)
jd-alexander Mar 16, 2021
cb31236
Added release notes for auto-opening.
jd-alexander Mar 17, 2021
3aa051f
Merge branch 'trunk' into rnmobile/simplify_image_insertion_flow-redu…
jd-alexander Mar 17, 2021
b12215f
Merge branch 'trunk' into rnmobile/simplify_image_insertion_flow-redu…
jd-alexander Mar 19, 2021
d865c2b
[RNMobile] Refactor simplify media flow redux store changes (#30123)
jd-alexander Mar 31, 2021
3654e91
Merge branch 'trunk' into rnmobile/simplify_image_insertion_flow-redu…
jd-alexander Mar 31, 2021
4135172
added wasBlockJustInserted prop needed after merge with trunk.
jd-alexander Mar 31, 2021
92ee6f0
Merge branch 'trunk' into rnmobile/simplify_image_insertion_flow-redu…
jd-alexander Apr 7, 2021
d22738f
Merge branch 'trunk' into rnmobile/simplify_image_insertion_flow-redu…
jd-alexander Apr 19, 2021
bb98f4c
removed updateSelection from reducer so it's updated at all times.
jd-alexander Apr 19, 2021
1941ae8
Merge branch 'trunk' into rnmobile/simplify_image_insertion_flow-redu…
jd-alexander Apr 20, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' );
jd-alexander marked this conversation as resolved.
Show resolved Hide resolved

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 );
25 changes: 25 additions & 0 deletions packages/editor/src/store/actions.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,28 @@ export function togglePostTitleSelection( isSelected = true ) {
export function* autosave() {
RNReactNativeGutenbergBridge.editorDidAutosave();
}

/**
* Returns an action object to track the last block that was inserted.
*
* @param {Object} clientId The client id of the block.
*
* @return {Object} Action object.
*/
export function addLastBlockInserted( clientId ) {
return {
type: 'ADD_LAST_BLOCK_INSERTED',
clientId,
};
}

/**
* Returns an action object to clear the last block that was inserted.
*
* @return {Object} Action object.
*/
export function clearLastBlockInserted() {
return {
type: 'CLEAR_LAST_BLOCK_INSERTED',
};
}
20 changes: 20 additions & 0 deletions packages/editor/src/store/reducer.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,25 @@ export function notices( state = [], action ) {
return state;
}

/**
* Reducer returning the block insertion event list state.
*
* @param {Object} state Current state.
* @param {Object} action Dispatched action.
*
* @return {Object} Updated state.
*/
export function lastBlockInserted( state = {}, action ) {
switch ( action.type ) {
case 'ADD_LAST_BLOCK_INSERTED':
return { ...state, clientId: action.clientId };

case 'CLEAR_LAST_BLOCK_INSERTED':
return {};
}
return state;
}
jd-alexander marked this conversation as resolved.
Show resolved Hide resolved

export default combineReducers( {
postId,
postType,
Expand All @@ -93,4 +112,5 @@ export default combineReducers( {
editorSettings,
clipboard,
notices,
lastBlockInserted,
} );
11 changes: 11 additions & 0 deletions packages/editor/src/store/selectors.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,14 @@ export const isEditedPostAutosaveable = createRegistrySelector(
return false;
}
);

/**
* Tells if the block with the passed clientId was just inserted.
*
* @param {Object} state Global application state.
* @param {Object} clientId client id of the block.
* @return {boolean} If the client id exists within the lastBlockInserted state then the block was just inserted.
*/
export function wasBlockJustInserted( state, clientId ) {
return state.lastBlockInserted.clientId === clientId;
}
Loading