Skip to content

Commit

Permalink
Add native support for Inserter (Ported from gutenberg-mobile) (#16114)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tug authored Jun 13, 2019
1 parent 70c39bc commit 8589bc1
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 0 deletions.
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 @@ -20,6 +20,7 @@ export { default as BlockInvalidWarning } from './block-list/block-invalid-warni

// Content Related Components
export { default as DefaultBlockAppender } from './default-block-appender';
export { default as Inserter } from './inserter';

// State Related Components
export { default as BlockEditorProvider } from './provider';
96 changes: 96 additions & 0 deletions packages/block-editor/src/components/inserter/index.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* External dependencies
*/
import { FlatList, Text, TouchableHighlight, View } from 'react-native';

/**
* WordPress dependencies
*/
import { BottomSheet, Icon } from '@wordpress/components';
import { Component } from '@wordpress/element';
import { withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { getUnregisteredTypeHandlerName } from '@wordpress/blocks';

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

class Inserter extends Component {
calculateNumberOfColumns() {
const bottomSheetWidth = BottomSheet.getWidth();
const { paddingLeft: itemPaddingLeft, paddingRight: itemPaddingRight } = styles.modalItem;
const { paddingLeft: containerPaddingLeft, paddingRight: containerPaddingRight } = styles.content;
const { width: itemWidth } = styles.modalIconWrapper;
const itemTotalWidth = itemWidth + itemPaddingLeft + itemPaddingRight;
const containerTotalWidth = bottomSheetWidth - ( containerPaddingLeft + containerPaddingRight );
return Math.floor( containerTotalWidth / itemTotalWidth );
}

render() {
const numberOfColumns = this.calculateNumberOfColumns();
const bottomPadding = this.props.addExtraBottomPadding && styles.contentBottomPadding;

return (
<BottomSheet
isVisible={ true }
onClose={ this.props.onDismiss }
contentStyle={ [ styles.content, bottomPadding ] }
hideHeader
>
<FlatList
scrollEnabled={ false }
key={ `InserterUI-${ numberOfColumns }` } //re-render when numberOfColumns changes
keyboardShouldPersistTaps="always"
numColumns={ numberOfColumns }
data={ this.props.items }
ItemSeparatorComponent={ () =>
<View style={ styles.rowSeparator } />
}
keyExtractor={ ( item ) => item.name }
renderItem={ ( { item } ) =>
<TouchableHighlight
style={ styles.touchableArea }
underlayColor="transparent"
activeOpacity={ .5 }
accessibilityLabel={ item.title }
onPress={ () => this.props.onValueSelected( item.name ) }>
<View style={ styles.modalItem }>
<View style={ styles.modalIconWrapper }>
<View style={ styles.modalIcon }>
<Icon icon={ item.icon.src } fill={ styles.modalIcon.fill } size={ styles.modalIcon.width } />
</View>
</View>
<Text style={ styles.modalItemLabel }>{ item.title }</Text>
</View>
</TouchableHighlight>
}
/>
</BottomSheet>
);
}
}

export default compose( [
withSelect( ( select, { clientId, isAppender, rootClientId } ) => {
const {
getInserterItems,
getBlockRootClientId,
getBlockSelectionEnd,
} = select( 'core/block-editor' );

let destinationRootClientId = rootClientId;
if ( ! destinationRootClientId && ! clientId && ! isAppender ) {
const end = getBlockSelectionEnd();
if ( end ) {
destinationRootClientId = getBlockRootClientId( end ) || undefined;
}
}
const inserterItems = getInserterItems( destinationRootClientId );

return {
items: inserterItems.filter( ( { name } ) => name !== getUnregisteredTypeHandlerName() ),
};
} ),
] )( Inserter );
57 changes: 57 additions & 0 deletions packages/block-editor/src/components/inserter/style.native.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/** @format */

.touchableArea {
border-radius: 8px 8px 8px 8px;
}

.content {
padding: 0 0 0 0;
align-items: center;
justify-content: space-evenly;
}

.contentBottomPadding {
padding-bottom: 20px;
}

.rowSeparator {
height: 12px;
}

.modalItem {
flex-direction: column;
justify-content: center;
align-items: center;
padding-left: 8;
padding-right: 8;
padding-top: 0;
padding-bottom: 0;
}

.modalIconWrapper {
width: 104px;
height: 64px;
background-color: $gray-light; //#f3f6f8
border-radius: 8px 8px 8px 8px;
justify-content: center;
align-items: center;
}

.modalIcon {
width: 32px;
height: 32px;
justify-content: center;
align-items: center;
fill: $gray-dark;
}

.modalItemLabel {
background-color: transparent;
padding-left: 2;
padding-right: 2;
padding-top: 4;
padding-bottom: 0;
justify-content: center;
font-size: 12;
color: $gray-dark;
}
4 changes: 4 additions & 0 deletions packages/blocks/src/api/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ export {
getUnregisteredTypeHandlerName,
getBlockType,
getBlockTypes,
getBlockSupport,
hasBlockSupport,
isReusableBlock,
getChildBlockNames,
hasChildBlocks,
hasChildBlocksWithInserterSupport,
setDefaultBlockName,
getDefaultBlockName,
} from './registration';
Expand Down

0 comments on commit 8589bc1

Please sign in to comment.