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 3 component integration #29548

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
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;
},
};
} ),
Comment on lines +473 to +488
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I repeated this withDispatch logic in three places. I am not sure how to make this reusable hence the duplication. Instinctively, I thought of hooks but these are class components so they are incompatible and the nature of this PR is not to do this conversion for these components. Let me know if you have any thoughts on this.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also wondered if this logic could somehow live within the store, but I wasn't sure about this either.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree finding a way to reuse this would be a nice plus. I'd need to take time to play around with it to make a suggestion though as I've been out of the RN side for a little while now.

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' );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am getting a warning about using the string literals to access the data store. I haven't seen a store definition for core-editor in other parts of the codebase as yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just found the PR that replaced store names and I saw that the core/editor is now editorStore

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the changes for this locally. I am encountering the error below. I will look into what could be causing in the morning

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't see this error pop up for me so you probably have it fixed now. This sounds similar to an issue solved in this PR where a setting was removed that the mobile side counted on.


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 );
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This delay and the one below ensures the picker has been opened before attempting to close it.

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