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
50 changes: 23 additions & 27 deletions packages/block-library/src/image/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ import {
requestImageFullscreenPreview,
showMediaEditorButton,
} from 'react-native-gutenberg-bridge';
import { isEmpty, map, get } from 'lodash';
import { isEmpty, get, find, map } from 'lodash';

/**
* WordPress dependencies
*/
import {
CycleSelectControl,
Icon,
PanelBody,
PanelActions,
SelectControl,
TextControl,
ToggleControl,
ToolbarButton,
Expand Down Expand Up @@ -59,28 +59,17 @@ import SvgIconRetry from './icon-retry';
import SvgIconCustomize from './icon-customize';
import { getUpdatedLinkTargetSettings } from './utils';

import { LINK_DESTINATION_CUSTOM, LINK_DESTINATION_NONE } from './constants';
import {
LINK_DESTINATION_CUSTOM,
LINK_DESTINATION_NONE,
DEFAULT_SIZE_SLUG,
} from './constants';

const ICON_TYPE = {
PLACEHOLDER: 'placeholder',
RETRY: 'retry',
UPLOAD: 'upload',
};
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,
} ) );

// Default Image ratio 4:3
const IMAGE_ASPECT_RATIO = 4 / 3;
Expand Down Expand Up @@ -348,7 +337,7 @@ export class ImageEdit extends React.Component {
}

render() {
const { attributes, isSelected, image } = this.props;
const { attributes, isSelected, image, imageSizes } = this.props;
const {
align,
url,
Expand All @@ -368,6 +357,15 @@ export class ImageEdit extends React.Component {
},
];

const sizeOptions = map( imageSizes, ( { name, slug } ) => ( {
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 to accommodate this change - #19800

value: slug,
name,
} ) );
const sizeOptionsValid = find( sizeOptions, [
'value',
DEFAULT_SIZE_SLUG,
] );

const getToolbarEditButton = ( open ) => (
<BlockControls>
<ToolbarGroup>
Expand Down Expand Up @@ -405,16 +403,11 @@ export class ImageEdit extends React.Component {
onChange={ this.onSetNewTab }
/>
{ // eslint-disable-next-line no-undef
image && __DEV__ && (
<SelectControl
hideCancelButton
image && sizeOptionsValid && __DEV__ && (
<CycleSelectControl
icon={ 'editor-expand' }
label={ __( 'Size' ) }
value={
sizeOptionLabels[
sizeSlug || DEFAULT_SIZE_SLUG
]
}
value={ sizeSlug || DEFAULT_SIZE_SLUG }
onChangeValue={ ( newValue ) =>
this.onSetSizeSlug( newValue )
}
Expand Down Expand Up @@ -663,13 +656,16 @@ 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
1 change: 1 addition & 0 deletions packages/components/src/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ export { default as ModalHeaderBar } from './mobile/modal-header-bar';
export { default as Picker } from './mobile/picker';
export { default as ReadableContentView } from './mobile/readable-content-view';
export { default as StepperControl } from './mobile/stepper-control';
export { default as CycleSelectControl } from './mobile/cycle-select-control';
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* External dependencies
*/
import { findIndex } from 'lodash';
/**
* Internal dependencies
*/
import Cell from './cell';

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

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

const selectedOptionIndex = findIndex( options, [ 'value', value ] );
const optionsContainsValue =
options.length > 0 && selectedOptionIndex !== -1;

return (
<Cell
onPress={ () =>
optionsContainsValue && onChangeValue( nextOptionValue() )
}
editable={ false }
value={
optionsContainsValue && options[ selectedOptionIndex ].name
}
{ ...cellProps }
/>
);
}
2 changes: 2 additions & 0 deletions packages/components/src/mobile/bottom-sheet/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { withPreferredColorScheme } from '@wordpress/compose';
import styles from './styles.scss';
import Button from './button';
import Cell from './cell';
import CyclePickerCell from './cycle-picker-cell';
import PickerCell from './picker-cell';
import SwitchCell from './switch-cell';
import RangeCell from './range-cell';
Expand Down Expand Up @@ -170,6 +171,7 @@ const ThemedBottomSheet = withPreferredColorScheme( BottomSheet );
ThemedBottomSheet.getWidth = getWidth;
ThemedBottomSheet.Button = Button;
ThemedBottomSheet.Cell = Cell;
ThemedBottomSheet.CyclePickerCell = CyclePickerCell;
ThemedBottomSheet.PickerCell = PickerCell;
ThemedBottomSheet.SwitchCell = SwitchCell;
ThemedBottomSheet.RangeCell = RangeCell;
Expand Down
4 changes: 4 additions & 0 deletions packages/components/src/mobile/cycle-select-control/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CycleSelectControl
===================

`CycleSelectControl` is an experimental alternative to SelectControl for mobile.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* Internal dependencies
*/
import CyclePickerCell from '../bottom-sheet/cycle-picker-cell';

function CycleSelectControl( {
help,
instanceId,
label,
multiple = false,
onChange,
options = [],
className,
hideLabelFromVision,
...props
} ) {
const id = `inspector-select-control-${ instanceId }`;

return (
<CyclePickerCell
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure I see the benefit in having both a CycleSelectControl and a CyclePickerCell, they seem mostly identical.

Copy link
Member Author

Choose a reason for hiding this comment

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

I think I'd prefer to keep it this way for now. It matches other Control / BottomSheet Cell pairs like ToggleControl / SwitchCell and I think there may be implications later on related to cross platform controls, where the control might become independent of where it lives in the UI. This issue has some interesting conversation on that topic #18018

label={ label }
hideLabelFromVision={ hideLabelFromVision }
id={ id }
help={ help }
className={ className }
onChangeValue={ onChange }
aria-describedby={ !! help ? `${ id }__help` : undefined }
multiple={ multiple }
options={ options }
{ ...props }
/>
);
}

export default CycleSelectControl;