Skip to content

Commit

Permalink
More reliable way to update focus, do not share object references wit…
Browse files Browse the repository at this point in the history
…h dataSource and use react lifecycle methods to update instead
  • Loading branch information
Tug committed Nov 12, 2018
1 parent 37faa04 commit c6d1998
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
28 changes: 12 additions & 16 deletions src/app/AppContainer.js
Expand Up @@ -2,7 +2,7 @@
* @format */

import { connect } from 'react-redux';
import { isEqual } from 'lodash';
import { cloneDeep } from 'lodash';

import MainApp from './MainApp';
import { parse, serialize } from '@wordpress/blocks';
Expand All @@ -11,22 +11,14 @@ import { compose } from '@wordpress/compose';
import RNReactNativeGutenbergBridge from 'react-native-gutenberg-bridge';

const mapStateToProps = ( state, ownProps ) => {
let blocks = ownProps.blocks;
let refresh = false;

const newBlocks = ownProps.blocks.map( ( block ) => {
block.focused = ownProps.isBlockSelected( block.clientId );
return block;
const blocks = ownProps.blocksFromState.map( block => {
const newBlock = cloneDeep( block );
newBlock.focused = ownProps.isBlockSelected( block.clientId );
return newBlock;
} );

if ( ! isEqual( blocks, newBlocks ) ) {
blocks = newBlocks;
refresh = true;
}

return {
blocks,
refresh,
blocks
};
};

Expand Down Expand Up @@ -82,11 +74,12 @@ export default compose( [
rootClientId,
isBlockSelected,
selectedBlockIndex: getBlockIndex( selectedBlockClientId, rootClientId ),
blocks: getBlocks(),
blocksFromState: getBlocks(),
};
} ),
withDispatch( ( dispatch ) => {
const {
clearSelectedBlock,
insertBlock,
mergeBlocks,
moveBlocksDown,
Expand All @@ -104,7 +97,10 @@ export default compose( [
onMoveUp: moveBlocksUp,
onRemove: removeBlock,
onResetBlocks: resetBlocks,
onSelect: selectBlock,
onSelect: ( clientId ) => {
clearSelectedBlock();
selectBlock( clientId );
},
onAttributesUpdate: updateBlockAttributes,
};
} ),
Expand Down
16 changes: 10 additions & 6 deletions src/block-management/block-manager.js
Expand Up @@ -4,6 +4,7 @@
*/

import React from 'react';
import { isEqual } from 'lodash';
import { Platform, Switch, Text, View, FlatList, KeyboardAvoidingView } from 'react-native';
import RecyclerViewList, { DataSource } from 'react-native-recyclerview-list';
import BlockHolder from './block-holder';
Expand All @@ -29,7 +30,6 @@ export type BlockListType = {
mergeBlocksAction: ( string, string ) => mixed,
blocks: Array<BlockType>,
aztechtml: string,
refresh: boolean,
};

type PropsType = BlockListType;
Expand All @@ -40,6 +40,7 @@ type StateType = {
blockTypePickerVisible: boolean,
blocks: Array<BlockType>,
selectedBlockType: string,
refresh: boolean,
};

export default class BlockManager extends React.Component<PropsType, StateType> {
Expand All @@ -52,6 +53,7 @@ export default class BlockManager extends React.Component<PropsType, StateType>
inspectBlocks: false,
blockTypePickerVisible: false,
selectedBlockType: 'core/paragraph', // just any valid type to start from
refresh: false,
};
}

Expand Down Expand Up @@ -121,14 +123,16 @@ export default class BlockManager extends React.Component<PropsType, StateType>
};
}

if ( state.blocks !== props.blocks ) {
if ( ! isEqual( state.blocks, props.blocks ) ) {
const blockFocused = props.blocks.find( block => block.focused );
if ( blockFocused ) {
BlockManager.focusDataSourceItem( state.dataSource, blockFocused.clientId );
}
// TODO: handle attributes change here
return {
...state,
blocks: props.blocks,
refresh: ! state.refresh,
};
}

Expand Down Expand Up @@ -276,7 +280,7 @@ export default class BlockManager extends React.Component<PropsType, StateType>
<FlatList
style={ styles.list }
data={ this.state.blocks }
extraData={ { refresh: this.props.refresh, inspectBlocks: this.state.inspectBlocks } }
extraData={ { refresh: this.state.refresh, inspectBlocks: this.state.inspectBlocks } }
keyExtractor={ ( item ) => item.clientId }
renderItem={ this.renderItem.bind( this ) }
/>
Expand Down Expand Up @@ -337,7 +341,7 @@ export default class BlockManager extends React.Component<PropsType, StateType>
this.setState( { inspectBlocks } );
}

renderItem( value: { item: BlockType, clientId: string } ) {
renderItem( value: { item: BlockType } ) {
const insertHere = (
<View style={ styles.containerStyleAddHere } >
<View style={ styles.lineStyleAddHere }></View>
Expand All @@ -349,13 +353,13 @@ export default class BlockManager extends React.Component<PropsType, StateType>
return (
<View>
<BlockHolder
key={ value.clientId }
key={ value.item.clientId }
onInlineToolbarButtonPressed={ this.onInlineToolbarButtonPressed.bind( this ) }
onBlockHolderPressed={ this.onBlockHolderPressed.bind( this ) }
onChange={ this.onChange.bind( this ) }
showTitle={ this.state.inspectBlocks }
focused={ value.item.focused }
clientId={ value.clientId }
clientId={ value.item.clientId }
insertBlocksAfter={ ( blocks ) =>
this.insertBlocksAfter.bind( this )( value.item.clientId, blocks )
}
Expand Down

0 comments on commit c6d1998

Please sign in to comment.