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] Update image size UI control #19232

Merged
merged 12 commits into from
Feb 12, 2020
Merged
27 changes: 10 additions & 17 deletions packages/block-library/src/image/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
requestImageUploadCancelDialog,
requestImageFullscreenPreview,
} from 'react-native-gutenberg-bridge';
import { isEmpty, map, get } from 'lodash';
import { isEmpty, get } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -53,21 +53,9 @@ import { getUpdatedLinkTargetSettings } from './utils';
import {
LINK_DESTINATION_CUSTOM,
LINK_DESTINATION_NONE,
DEFAULT_SIZE_SLUG,
} from './constants';

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;
const sizeOptionLabels = {
[ IMAGE_SIZE_THUMBNAIL ]: __( 'Thumbnail' ),
[ IMAGE_SIZE_MEDIUM ]: __( 'Medium' ),
[ IMAGE_SIZE_LARGE ]: __( 'Large' ),
[ IMAGE_SIZE_FULL_SIZE ]: __( 'Full Size' ),
};
const sizeOptions = map( sizeOptionLabels, ( label, option ) => ( { value: option, label } ) );

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!

// Default Image ratio 4:3
const IMAGE_ASPECT_RATIO = 4 / 3;

Expand Down Expand Up @@ -296,7 +284,7 @@ export class ImageEdit extends React.Component {
}

render() {
const { attributes, isSelected, image } = this.props;
const { attributes, isSelected, image, imageSizes } = this.props;
const { align, url, height, width, alt, href, id, linkTarget, sizeSlug } = attributes;

const actions = [ { label: __( 'Clear All Settings' ), onPress: this.onClearSettings } ];
Expand Down Expand Up @@ -343,9 +331,9 @@ export class ImageEdit extends React.Component {
hideCancelButton
icon={ 'editor-expand' }
label={ __( 'Size' ) }
value={ sizeOptionLabels[ sizeSlug || DEFAULT_SIZE_SLUG ] }
value={ sizeSlug || DEFAULT_SIZE_SLUG }
onChangeValue={ ( newValue ) => this.onSetSizeSlug( newValue ) }
options={ sizeOptions }
options={ imageSizes }
/> }
<TextControl
icon={ 'editor-textcolor' }
Expand Down Expand Up @@ -484,10 +472,15 @@ export class ImageEdit extends React.Component {
export default compose( [
withSelect( ( select, props ) => {
const { getMedia } = select( 'core' );
const { getSettings } = select( 'core/block-editor' );
const { attributes: { id }, isSelected } = props;
const {
imageSizes,
} = getSettings();

return {
image: id && isSelected ? getMedia( id ) : null,
imageSizes,
};
} ),
withPreferredColorScheme,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* External dependencies
*/
import { findIndex } from 'lodash';
/**
* Internal dependencies
*/
import Cell from './cell';

export default function BottomSheetCyclePickerCell( props ) {
const {
value,
options,
onChangeValue,
...cellProps
} = props;

const cycleOptionValue = () => {
Copy link
Contributor

@Tug Tug Jan 27, 2020

Choose a reason for hiding this comment

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

A better name for this function imo would be nextOptionValue. Cycle does indicate that the possible options form a cycle but it's hard to understand that this is returning the next one from the currently selected one.
I think readability could be improved here as well, something as simple as this would really help I think

const nextOptionValue = () => {
	const selectedOptionIndex = findIndex( options, { value } );
	return options[ ( selectedOptionIndex + 1 ) % options.length ].value;
};

Copy link
Contributor

Choose a reason for hiding this comment

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

We might want to handle the case where options is en empty array as well ;)

Copy link
Member Author

Choose a reason for hiding this comment

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

Updated constant name 9aa8aa7 . Added checks here 12ebc85 so the image block will hide image size options if sizes / default are ever empty or not matching, and a check so that the CycleSelectControl value will just be blank if it is ever passed empty options.

return options[ ( findIndex( options, [ 'slug', value ] ) + 1 ) % options.length ].slug;
};

return (
<Cell
onPress={ () => onChangeValue( cycleOptionValue() ) }
editable={ false }
value={ options[ findIndex( options, [ 'slug', value ] ) ].label }
{ ...cellProps }
/>
);
}
4 changes: 2 additions & 2 deletions packages/components/src/mobile/bottom-sheet/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { withPreferredColorScheme } from '@wordpress/compose';
import styles from './styles.scss';
import Button from './button';
import Cell from './cell';
import PickerCell from './picker-cell';
import CyclePickerCell from './cycle-picker-cell';
import SwitchCell from './switch-cell';
import RangeCell from './range-cell';
import KeyboardAvoidingView from './keyboard-avoiding-view';
Expand Down Expand Up @@ -147,7 +147,7 @@ const ThemedBottomSheet = withPreferredColorScheme( BottomSheet );
ThemedBottomSheet.getWidth = getWidth;
ThemedBottomSheet.Button = Button;
ThemedBottomSheet.Cell = Cell;
ThemedBottomSheet.PickerCell = PickerCell;
ThemedBottomSheet.CyclePickerCell = CyclePickerCell;
ThemedBottomSheet.SwitchCell = SwitchCell;
ThemedBottomSheet.RangeCell = RangeCell;

Expand Down
4 changes: 2 additions & 2 deletions packages/components/src/select-control/index.native.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Internal dependencies
*/
import PickerCell from '../mobile/bottom-sheet/picker-cell';
import CyclePickerCell from '../mobile/bottom-sheet/cycle-picker-cell';

function SelectControl( {
help,
Expand All @@ -17,7 +17,7 @@ function SelectControl( {
const id = `inspector-select-control-${ instanceId }`;

return (
<PickerCell
<CyclePickerCell
Copy link
Member Author

Choose a reason for hiding this comment

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

Though I did not include it in this PR yet. I am proposing that we remove PickerCell.(though Picker is still super useful!). Though PickerCell gets the BottomSheet nested menu about 80% there, it feels like a fundamental restriction of a react-native-modal Modal nested inside of another, that we will not be able to close the containing Modal before showing the child Modal without editing the underlying library. @Tug @etoledom thoughts?

One reason this might be worth doing now is in order to decouple PickerCell from Cross Platform component refactoring efforts, like seen here in SelectControl.

label={ label }
hideLabelFromVision={ hideLabelFromVision }
id={ id }
Expand Down