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] Support percentage in columns #23828

Merged
merged 31 commits into from
Aug 7, 2020
Merged
Show file tree
Hide file tree
Changes from 30 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
ed434d9
Initial work on column percentage mechanism
lukewalczak Jun 29, 2020
dddd80e
Revert width changes
lukewalczak Jul 8, 2020
330ffbd
Add columns presets
lukewalczak Jul 9, 2020
67fbb8d
Add toFixed in RangeCell
lukewalczak Jul 9, 2020
1491e3c
Merge branch 'master' into rnmobile/column-percentage
lukewalczak Jul 9, 2020
9e03916
Code refactor, adjusting to comments
lukewalczak Jul 10, 2020
789902c
Fix paddings and typo
lukewalczak Jul 14, 2020
ff1fa9f
Merge branch 'master' into rnmobile/column-percentage
lukewalczak Jul 14, 2020
2192e43
Display layout picker only for default columns
lukewalczak Jul 15, 2020
458d291
Merge branch 'master' into rnmobile/column-percentage
lukewalczak Jul 15, 2020
b11bee4
Add styles in menu item
lukewalczak Jul 15, 2020
01c4dc8
Create columns preview
lukewalczak Jul 24, 2020
4f7b6df
Merge branch 'master' into rnmobile/column-percentage
lukewalczak Jul 24, 2020
33dbc70
Merge branch 'master' into rnmobile/column-percentage
lukewalczak Jul 27, 2020
fbad1ac
Columns refactor
lukewalczak Jul 27, 2020
c995483
Refactor columns preview
lukewalczak Jul 28, 2020
e5b79f7
Refactor after CR
lukewalczak Jul 29, 2020
991fc85
Merge branch 'master' into rnmobile/column-percentage
lukewalczak Jul 29, 2020
fdaeeb7
Merge branch 'master' into rnmobile/column-percentage
lukewalczak Jul 29, 2020
cda443e
Extract column preview to the separate file
lukewalczak Jul 30, 2020
978bd8f
Merge branch 'master' into rnmobile/column-percentage
lukewalczak Jul 31, 2020
54db548
Correct useMemo in variation picker
lukewalczak Jul 31, 2020
2453eef
Change button color and slider step
lukewalczak Aug 3, 2020
a6d1feb
Change width label
lukewalczak Aug 4, 2020
87d25c2
Refactor styles
lukewalczak Aug 5, 2020
a639621
Merge branch 'master' into rnmobile/column-percentage
lukewalczak Aug 7, 2020
5a7dbd1
Merge branch 'master' into rnmobile/column-percentage
lukewalczak Aug 7, 2020
cf5ba1c
Merge branch 'master' into rnmobile/column-percentage
lukewalczak Aug 7, 2020
249a71c
Use FooterMessageControl
lukewalczak Aug 7, 2020
c569bd2
Change styles name
lukewalczak Aug 7, 2020
bc32349
Set textAlign=center as a default in FooterMessageControl
lukewalczak Aug 7, 2020
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
/**
* External dependencies
*/
import {
ScrollView,
View,
Text,
TouchableWithoutFeedback,
Platform,
} from 'react-native';
import { map } from 'lodash';

/**
* WordPress dependencies
*/
import { withSelect, useDispatch } from '@wordpress/data';
import { compose, usePreferredColorSchemeStyle } from '@wordpress/compose';
import { createBlock } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';
import {
PanelBody,
BottomSheet,
FooterMessageControl,
} from '@wordpress/components';
import { Icon, close } from '@wordpress/icons';
import { useMemo } from '@wordpress/element';

/**
* Internal dependencies
*/
import MenuItem from '../inserter/menu-item';
import styles from './style.scss';

const hitSlop = { top: 22, bottom: 22, left: 22, right: 22 };

function createBlocksFromInnerBlocksTemplate( innerBlocksTemplate ) {
return map(
innerBlocksTemplate,
( [ name, attributes, innerBlocks = [] ] ) =>
createBlock(
name,
attributes,
createBlocksFromInnerBlocksTemplate( innerBlocks )
)
);
}

function BlockVariationPicker( { isVisible, onClose, clientId, variations } ) {
const { replaceInnerBlocks } = useDispatch( 'core/block-editor' );
const isIOS = Platform.OS === 'ios';

const cancelButtonStyle = usePreferredColorSchemeStyle(
styles.cancelButton,
styles.cancelButtonDark
);

const leftButton = useMemo(
() => (
<TouchableWithoutFeedback onPress={ onClose } hitSlop={ hitSlop }>
<View>
{ isIOS ? (
<Text
style={ cancelButtonStyle }
maxFontSizeMultiplier={ 2 }
>
{ __( 'Cancel' ) }
</Text>
) : (
<Icon
icon={ close }
size={ 24 }
style={ styles.closeIcon }
/>
) }
</View>
</TouchableWithoutFeedback>
),
[ onClose, cancelButtonStyle ]
);

const onVariationSelect = ( variation ) => {
replaceInnerBlocks(
clientId,
createBlocksFromInnerBlocksTemplate( variation.innerBlocks ),
false
);
onClose();
};

return (
<BottomSheet
isVisible={ isVisible }
onClose={ onClose }
title={ __( 'Select a layout' ) }
contentStyle={ styles.contentStyle }
leftButton={ leftButton }
>
<ScrollView
horizontal
showsHorizontalScrollIndicator={ false }
contentContainerStyle={ styles.contentContainerStyle }
style={ styles.containerStyle }
>
{ variations.map( ( v ) => {
return (
<MenuItem
item={ v }
key={ v.name }
onSelect={ () => onVariationSelect( v ) }
/>
);
} ) }
</ScrollView>
<PanelBody>
<FooterMessageControl
label={ __(
'Note: Column layout may vary between themes and screen sizes'
) }
textAlign="center"
/>
</PanelBody>
</BottomSheet>
);
}

export default compose(
withSelect( ( select, {} ) => {
const { getBlockVariations } = select( 'core/blocks' );

return {
date: getBlockVariations( 'core/columns', 'block' ),
};
} )
)( BlockVariationPicker );
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.contentContainerStyle {
flex-direction: row;
padding-bottom: $grid-unit-15;
padding-left: $grid-unit-20 / 2;
padding-right: $grid-unit-20 / 2;
}

.containerStyle {
padding-top: $grid-unit-15;
}

.contentStyle {
padding-left: 0;
padding-right: 0;
}

.cancelButton {
color: $blue-wordpress;
font-size: 16px;
}

.cancelButtonDark {
color: $blue-30;
}

.closeIcon {
color: $gray;
}
1 change: 1 addition & 0 deletions packages/block-editor/src/components/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export {
export { default as BlockList } from './block-list';
export { default as BlockMover } from './block-mover';
export { default as BlockToolbar } from './block-toolbar';
export { default as BlockVariationPicker } from './block-variation-picker';
export { default as DefaultBlockAppender } from './default-block-appender';
export { default as __unstableEditorStyles } from './editor-styles';
export { default as Inserter } from './inserter';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class MenuItem extends Component {
>
<View style={ modalIconStyle }>
<Icon
icon={ item.icon.src }
icon={ item.icon.src || item.icon }
fill={ modalIconStyle.fill }
size={ modalIconStyle.width }
/>
Expand Down
45 changes: 45 additions & 0 deletions packages/block-library/src/column/column-preview.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* External dependencies
*/
import { View } from 'react-native';

/**
* WordPress dependencies
*/
import { usePreferredColorSchemeStyle } from '@wordpress/compose';

/**
* Internal dependencies
*/
import styles from './editor.scss';

function ColumnsPreview( { columnWidths, selectedColumnIndex } ) {
const columnsPreviewStyle = usePreferredColorSchemeStyle(
styles.columnsPreview,
styles.columnsPreviewDark
);

const columnIndicatorStyle = usePreferredColorSchemeStyle(
styles.columnIndicator,
styles.columnIndicatorDark
);

return (
<View style={ columnsPreviewStyle }>
{ columnWidths.map( ( width, index ) => {
const isSelectedColumn = index === selectedColumnIndex;
return (
<View
style={ [
isSelectedColumn && columnIndicatorStyle,
{ flex: width },
] }
key={ index }
/>
);
} ) }
</View>
);
}

export default ColumnsPreview;
62 changes: 60 additions & 2 deletions packages/block-library/src/column/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,20 @@ import {
InnerBlocks,
BlockControls,
BlockVerticalAlignmentToolbar,
InspectorControls,
} from '@wordpress/block-editor';
import {
PanelBody,
RangeControl,
FooterMessageControl,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
/**
* Internal dependencies
*/
import styles from './editor.scss';
import ColumnsPreview from './column-preview';
import { getColumnWidths } from '../columns/utils';

function ColumnEdit( {
attributes,
Expand All @@ -26,13 +35,26 @@ function ColumnEdit( {
getStylesFromColorScheme,
isParentSelected,
contentStyle,
columns,
columnCount,
selectedColumnIndex,
} ) {
const { verticalAlignment } = attributes;

const updateAlignment = ( alignment ) => {
setAttributes( { verticalAlignment: alignment } );
};

const onWidthChange = ( width ) => {
setAttributes( {
width,
} );
};

const columnWidths = Object.values(
getColumnWidths( columns, columnCount )
);

if ( ! isSelected && ! hasChildren ) {
return (
<View
Expand All @@ -45,7 +67,7 @@ function ColumnEdit( {
contentStyle,
styles.columnPlaceholderNotSelected,
] }
></View>
/>
);
}

Expand All @@ -57,6 +79,32 @@ function ColumnEdit( {
value={ verticalAlignment }
/>
</BlockControls>
<InspectorControls>
<PanelBody title={ __( 'Column settings' ) }>
<RangeControl
label={ __( 'Width' ) }
min={ 1 }
max={ 100 }
value={ columnWidths[ selectedColumnIndex ] }
onChange={ onWidthChange }
toFixed={ 1 }
rangePreview={
<ColumnsPreview
columnWidths={ columnWidths }
selectedColumnIndex={ selectedColumnIndex }
/>
}
/>
</PanelBody>
<PanelBody>
<FooterMessageControl
label={ __(
'Note: Column layout may vary between themes and screen sizes'
) }
textAlign="center"
lukewalczak marked this conversation as resolved.
Show resolved Hide resolved
/>
</PanelBody>
</InspectorControls>
<View
style={ [
contentStyle,
Expand Down Expand Up @@ -97,21 +145,31 @@ export default compose( [
getBlockCount,
getBlockRootClientId,
getSelectedBlockClientId,
getBlocks,
getBlockOrder,
} = select( 'core/block-editor' );

const selectedBlockClientId = getSelectedBlockClientId();
const isSelected = selectedBlockClientId === clientId;

const parentId = getBlockRootClientId( clientId );
const hasChildren = !! getBlockCount( clientId );

const isParentSelected =
selectedBlockClientId && selectedBlockClientId === parentId;

const blockOrder = getBlockOrder( parentId );

const selectedColumnIndex = blockOrder.indexOf( clientId );
const columnCount = getBlockCount( parentId );
const columns = getBlocks( parentId );

return {
hasChildren,
isParentSelected,
isSelected,
selectedColumnIndex,
columns,
columnCount,
};
} ),
withPreferredColorScheme,
Expand Down
22 changes: 22 additions & 0 deletions packages/block-library/src/column/editor.native.scss
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,25 @@
.flexBase {
flex: 1;
}

.columnsPreview {
min-height: 18px;
min-width: 24px;
border-color: $toolbar-button;
flex-direction: row;
margin-right: 10px;
border-width: $border-width;
border-radius: $radius-block-ui;
}

.columnsPreviewDark {
border-color: $gray-20;
}

.columnIndicator {
background-color: $toolbar-button;
}

.columnIndicatorDark {
background-color: $gray-20;
}
Loading