Skip to content

Commit

Permalink
Replace local store with gutenberg store
Browse files Browse the repository at this point in the history
  • Loading branch information
Tug committed Nov 7, 2018
1 parent ec8daf1 commit e4826ec
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/app/App.js
Expand Up @@ -8,7 +8,7 @@ import { Provider } from 'react-redux';
import { setupStore, html2State } from '../store';
import AppContainer from './AppContainer';
import { Store } from 'redux';
import { withDispatch, withSelect } from '@wordpress/data';
import { withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { BlockEdit } from '@wordpress/editor';

Expand Down
40 changes: 14 additions & 26 deletions src/app/AppContainer.js
Expand Up @@ -2,17 +2,6 @@
* @format */

import { connect } from 'react-redux';
import {
updateBlockAttributes,
focusBlockAction,
moveBlockUpAction,
moveBlockDownAction,
deleteBlockAction,
createBlockAction,
parseBlocksAction,
serializeToNativeAction,
mergeBlocksAction,
} from '../store/actions';
import MainApp from './MainApp';

import { parse, serialize } from '@wordpress/blocks';
Expand All @@ -21,49 +10,46 @@ import { compose } from '@wordpress/compose';
import { BlockEdit } from '@wordpress/editor';
import RNReactNativeGutenbergBridge from 'react-native-gutenberg-bridge';

const mapStateToProps = ( state ) => ( {
...state,
} );
const mapStateToProps = ( state, ownProps ) => {
return {
blocks: ownProps.blocks.map( block => {
block.focused = ownProps.isBlockSelected( block.clientId );
return block;
} ),
refresh: ! ownProps.refresh
}
};

const mapDispatchToProps = ( dispatch, ownProps ) => {
return {
...ownProps,
onChange: ( clientId, attributes ) => {
dispatch( updateBlockAttributes( clientId, attributes ) );
ownProps.onAttributesUpdate( clientId, attributes );
},
focusBlockAction: ( clientId ) => {
dispatch( focusBlockAction( clientId ) );
ownProps.onSelect( clientId );
},
moveBlockUpAction: ( clientId ) => {
dispatch( moveBlockUpAction( clientId ) );
ownProps.onMoveUp( clientId );
},
moveBlockDownAction: ( clientId ) => {
dispatch( moveBlockDownAction( clientId ) );
ownProps.onMoveDown( clientId );
},
deleteBlockAction: ( clientId ) => {
dispatch( deleteBlockAction( clientId ) );
ownProps.onRemove( clientId );
},
createBlockAction: ( clientId, block, clientIdAbove ) => {
dispatch( createBlockAction( clientId, block, clientIdAbove ) );
createBlockAction: ( clientId, block ) => {
ownProps.onInsertBlock( block, ownProps.selectedBlockIndex + 1 );
},
parseBlocksAction: ( html ) => {
dispatch( parseBlocksAction( html ) );
const parsed = parse( html );
ownProps.onResetBlocks( parsed );
},
serializeToNativeAction: () => {
dispatch( serializeToNativeAction() );
const html = serialize( ownProps.blocks );
RNReactNativeGutenbergBridge.provideToNative_Html( html );
},
mergeBlocksAction: ( blockOneClientId, blockTwoClientId, block ) => {
dispatch( mergeBlocksAction( blockOneClientId, blockTwoClientId, block ) );
mergeBlocksAction: ( blockOneClientId, blockTwoClientId ) => {
ownProps.onMerge( blockOneClientId, blockTwoClientId );
},
};
Expand All @@ -77,11 +63,13 @@ export default compose( [
getBlockIndex,
getBlocks,
getSelectedBlockClientId,
isBlockSelected,
} = select( 'core/editor' );
const { rootClientId } = ownProps;
const selectedBlockClientId = getSelectedBlockClientId();

return {
isBlockSelected,
selectedBlockIndex: getBlockIndex( selectedBlockClientId, rootClientId ),
blocks: getBlocks(),
};
Expand All @@ -106,7 +94,7 @@ export default compose( [
onRemove: removeBlock,
onResetBlocks: resetBlocks,
onSelect: selectBlock,
onAttributesUpdate: updateBlockAttributes
onAttributesUpdate: updateBlockAttributes,
};
} ),
] )( AppContainer );
15 changes: 1 addition & 14 deletions src/block-management/block-holder.js
Expand Up @@ -23,20 +23,7 @@ type PropsType = BlockType & {
mergeBlocks: ( forward: boolean ) => void,
};

type StateType = {
selected: boolean,
focused: boolean,
};

export default class BlockHolder extends React.Component<PropsType, StateType> {
constructor( props: PropsType ) {
super( props );
this.state = {
selected: false,
focused: false,
};
}

export default class BlockHolder extends React.Component<PropsType> {
renderToolbarIfBlockFocused() {
if ( this.props.focused ) {
return (
Expand Down
30 changes: 8 additions & 22 deletions src/block-management/block-manager.js
Expand Up @@ -45,7 +45,8 @@ export default class BlockManager extends React.Component<PropsType, StateType>
constructor( props: PropsType ) {
super( props );
this.state = {
dataSource: new DataSource( this.props.blocks, ( item: BlockType ) => item.clientId ),
blocks: [],
dataSource: null,
showHtml: false,
inspectBlocks: false,
blockTypePickerVisible: false,
Expand Down Expand Up @@ -115,12 +116,14 @@ export default class BlockManager extends React.Component<PropsType, StateType>
}

static getDerivedStateFromProps( props: PropsType, state: StateType ) {
if ( props.fullparse === true ) {
if ( state.blocks !== props.blocks ) {
return {
...state,
blocks: props.blocks,
dataSource: new DataSource( props.blocks, ( item: BlockType ) => item.clientId ),
};
}

// no state change necessary
return null;
}
Expand Down Expand Up @@ -210,24 +213,7 @@ export default class BlockManager extends React.Component<PropsType, StateType>
return;
}

// Calling the merge to update the attributes and remove the block to be merged
const updatedAttributes = blockType.merge(
blockA.attributes,
blocksWithTheSameType[ 0 ].attributes
);

const newBlock = {
...blockA,
attributes: {
...blockA.attributes,
...updatedAttributes,
},
};
newBlock.focused = true;

// set it into the datasource, and use the same object instance to send it to props/redux
this.state.dataSource.splice( this.getDataSourceIndexFromClientId( blockA.clientId ), 2, newBlock );
this.props.mergeBlocksAction( blockA.clientId, blockB.clientId, newBlock );
this.props.mergeBlocksAction( blockA.clientId, blockB.clientId );
}

onChange( clientId: string, attributes: mixed ) {
Expand Down Expand Up @@ -263,8 +249,8 @@ export default class BlockManager extends React.Component<PropsType, StateType>
list = (
<FlatList
style={ styles.list }
data={ this.props.blocks }
extraData={ this.props.refresh, this.state.inspectBlocks }
data={ this.state.blocks }
extraData={ { refresh: this.props.refresh, inspectBlocks: this.state.inspectBlocks } }
keyExtractor={ ( item ) => item.clientId }
renderItem={ this.renderItem.bind( this ) }
/>
Expand Down

0 comments on commit e4826ec

Please sign in to comment.