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][tech-debt] block-list optimisation #22144

Merged
merged 14 commits into from
May 22, 2020
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/**
* External dependencies
*/
import { View } from 'react-native';

/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { withSelect } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { ReadableContentView } from '@wordpress/components';

/**
* Internal dependencies
*/
import BlockListBlock from './block';
import BlockInsertionPoint from './insertion-point';

const stretchStyle = {
flex: 1,
};

export class BlockListItem extends Component {
render() {
const {
clientId,
isReadOnly,
shouldShowInsertionPointBefore,
shouldShowInsertionPointAfter,
contentResizeMode,
shouldShowInnerBlockAppender,
...restProps
} = this.props;
const readableContentViewStyle =
contentResizeMode === 'stretch' && stretchStyle;
return (
<ReadableContentView style={ readableContentViewStyle }>
<View
style={ readableContentViewStyle }
pointerEvents={ isReadOnly ? 'box-only' : 'auto' }
>
{ shouldShowInsertionPointBefore && (
<BlockInsertionPoint />
) }
<BlockListBlock
key={ clientId }
showTitle={ false }
clientId={ clientId }
{ ...restProps }
/>
{ ! shouldShowInnerBlockAppender() &&
shouldShowInsertionPointAfter && (
mchowning marked this conversation as resolved.
Show resolved Hide resolved
<BlockInsertionPoint />
) }
</View>
</ReadableContentView>
);
}
}

export default compose( [
withSelect(
( select, { rootClientId, isStackedHorizontally, clientId } ) => {
const {
getBlockOrder,
getBlockInsertionPoint,
isBlockInsertionPointVisible,
getSettings,
} = select( 'core/block-editor' );

const blockClientIds = getBlockOrder( rootClientId );
const insertionPoint = getBlockInsertionPoint();
const blockInsertionPointIsVisible = isBlockInsertionPointVisible();
const shouldShowInsertionPointBefore =
! isStackedHorizontally &&
blockInsertionPointIsVisible &&
insertionPoint.rootClientId === rootClientId &&
// if list is empty, show the insertion point (via the default appender)
( blockClientIds.length === 0 ||
// or if the insertion point is right before the denoted block
blockClientIds[ insertionPoint.index ] === clientId );

const shouldShowInsertionPointAfter =
dratwas marked this conversation as resolved.
Show resolved Hide resolved
! isStackedHorizontally &&
blockInsertionPointIsVisible &&
insertionPoint.rootClientId === rootClientId &&
// if the insertion point is at the end of the list
blockClientIds.length === insertionPoint.index &&
// and the denoted block is the last one on the list, show the indicator at the end of the block
blockClientIds[ insertionPoint.index - 1 ] === clientId;

const isReadOnly = getSettings().readOnly;

return {
shouldShowInsertionPointBefore,
shouldShowInsertionPointAfter,
isReadOnly,
};
}
),
] )( BlockListItem );
46 changes: 23 additions & 23 deletions packages/block-editor/src/components/block-list/block.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,15 +216,29 @@ class BlockListBlock extends Component {
}
}

// Helper function to memoize the wrapperProps since getEditWrapperProps always returns a new reference
const wrapperPropsCache = new WeakMap();
const emptyObj = {};
function getWrapperProps( value, getWrapperPropsFunction ) {
if ( ! getWrapperPropsFunction ) {
return emptyObj;
}
const cachedValue = wrapperPropsCache.get( value );
if ( ! cachedValue ) {
const wrapperProps = getWrapperPropsFunction( value );
wrapperPropsCache.set( value, wrapperProps );
return wrapperProps;
}
return cachedValue;
}

export default compose( [
withSelect( ( select, { clientId, rootClientId } ) => {
const {
getBlockIndex,
isBlockSelected,
__unstableGetBlockWithoutInnerBlocks,
getBlockHierarchyRootClientId,
getSelectedBlockClientId,
getBlockRootClientId,
getLowestCommonAncestorWithSelectedBlock,
getBlockParents,
hasSelectedInnerBlock,
Expand All @@ -243,8 +257,6 @@ export default compose( [
const parents = getBlockParents( clientId, true );
const parentId = parents[ 0 ] || '';

const rootBlockId = getBlockHierarchyRootClientId( clientId );

const selectedBlockClientId = getSelectedBlockClientId();

const commonAncestor = getLowestCommonAncestorWithSelectedBlock(
Expand All @@ -256,17 +268,13 @@ export default compose( [
: parents[ parents.length - 1 ];

const isParentSelected =
selectedBlockClientId && selectedBlockClientId === parentId;
const isAncestorSelected =
selectedBlockClientId && parents.includes( selectedBlockClientId );
const isSelectedBlockNested = !! getBlockRootClientId(
selectedBlockClientId
);
// set false as a default value to prevent re-render when it's changed from null to false
( selectedBlockClientId || false ) &&
selectedBlockClientId === parentId;

const selectedParents = selectedBlockClientId
? getBlockParents( selectedBlockClientId )
: [];
const isDescendantSelected = selectedParents.includes( clientId );
const isDescendantOfParentSelected = selectedParents.includes(
parentId
);
Expand All @@ -275,13 +283,6 @@ export default compose( [
isDescendantOfParentSelected ||
isParentSelected ||
parentId === '';
const isDimmed =
! isSelected &&
isSelectedBlockNested &&
! isAncestorSelected &&
! isDescendantSelected &&
( isDescendantOfParentSelected || rootBlockId === clientId );

return {
icon,
name: name || 'core/missing',
Expand All @@ -294,12 +295,11 @@ export default compose( [
isValid,
isParentSelected,
firstToSelectId,
isAncestorSelected,
isTouchable,
isDimmed,
wrapperProps: blockType.getEditWrapperProps
? blockType.getEditWrapperProps( attributes ) || {}
: {},
wrapperProps: getWrapperProps(
attributes,
blockType.getEditWrapperProps
),
};
} ),
withDispatch( ( dispatch, ownProps, { select } ) => {
Expand Down
Loading