-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #226 from wordpress-mobile/add/gb-store
Use Gutenberg store
- Loading branch information
Showing
18 changed files
with
672 additions
and
773 deletions.
There are no files selected for viewing
Submodule gutenberg
updated
21 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,136 @@ | ||
/** @flow | ||
* @format */ | ||
|
||
import { connect } from 'react-redux'; | ||
import { | ||
updateBlockAttributes, | ||
focusBlockAction, | ||
moveBlockUpAction, | ||
moveBlockDownAction, | ||
deleteBlockAction, | ||
createBlockAction, | ||
parseBlocksAction, | ||
serializeToNativeAction, | ||
mergeBlocksAction, | ||
} from '../store/actions'; | ||
import MainApp from './MainApp'; | ||
import React from 'react'; | ||
import { parse, serialize } from '@wordpress/blocks'; | ||
import { withDispatch, withSelect } from '@wordpress/data'; | ||
import { compose } from '@wordpress/compose'; | ||
import RNReactNativeGutenbergBridge from 'react-native-gutenberg-bridge'; | ||
import type { BlockType } from '../store/types'; | ||
|
||
const mapStateToProps = ( state ) => ( { | ||
...state, | ||
} ); | ||
|
||
const mapDispatchToProps = ( dispatch, ownProps ) => { | ||
return { | ||
...ownProps, | ||
onChange: ( clientId, attributes ) => { | ||
dispatch( updateBlockAttributes( clientId, attributes ) ); | ||
}, | ||
focusBlockAction: ( clientId ) => { | ||
dispatch( focusBlockAction( clientId ) ); | ||
}, | ||
moveBlockUpAction: ( clientId ) => { | ||
dispatch( moveBlockUpAction( clientId ) ); | ||
}, | ||
moveBlockDownAction: ( clientId ) => { | ||
dispatch( moveBlockDownAction( clientId ) ); | ||
}, | ||
deleteBlockAction: ( clientId ) => { | ||
dispatch( deleteBlockAction( clientId ) ); | ||
}, | ||
createBlockAction: ( clientId, block, clientIdAbove ) => { | ||
dispatch( createBlockAction( clientId, block, clientIdAbove ) ); | ||
}, | ||
parseBlocksAction: ( html ) => { | ||
dispatch( parseBlocksAction( html ) ); | ||
}, | ||
serializeToNativeAction: () => { | ||
dispatch( serializeToNativeAction() ); | ||
}, | ||
mergeBlocksAction: ( blockOneClientId, blockTwoClientId, block ) => { | ||
dispatch( mergeBlocksAction( blockOneClientId, blockTwoClientId, block ) ); | ||
}, | ||
}; | ||
type PropsType = { | ||
rootClientId: string, | ||
isBlockSelected: string => boolean, | ||
selectedBlockIndex: number, | ||
blocks: Array<BlockType>, | ||
onInsertBlock: ( BlockType, number, string ) => mixed, | ||
onMerge: ( string, string ) => mixed, | ||
onMoveDown: string => mixed, | ||
onMoveUp: string => mixed, | ||
onRemove: string => mixed, | ||
onResetBlocks: Array<BlockType> => mixed, | ||
onSelect: string => mixed, | ||
onAttributesUpdate: ( string, mixed ) => mixed, | ||
initialHtml: string, | ||
}; | ||
|
||
const AppContainer = connect( mapStateToProps, mapDispatchToProps )( MainApp ); | ||
export default AppContainer; | ||
class AppContainer extends React.Component<PropsType> { | ||
constructor( props: PropsType ) { | ||
super( props ); | ||
|
||
this.parseBlocksAction( props.initialHtml ); | ||
} | ||
|
||
onChange = ( clientId, attributes ) => { | ||
this.props.onAttributesUpdate( clientId, attributes ); | ||
}; | ||
|
||
focusBlockAction = ( clientId ) => { | ||
this.props.onSelect( clientId ); | ||
}; | ||
|
||
moveBlockUpAction = ( clientId ) => { | ||
this.props.onMoveUp( clientId ); | ||
}; | ||
|
||
moveBlockDownAction = ( clientId ) => { | ||
this.props.onMoveDown( clientId ); | ||
}; | ||
|
||
deleteBlockAction = ( clientId ) => { | ||
this.props.onRemove( clientId ); | ||
}; | ||
|
||
createBlockAction = ( clientId, block ) => { | ||
this.props.onInsertBlock( block, this.props.selectedBlockIndex + 1, this.props.rootClientId ); | ||
}; | ||
|
||
parseBlocksAction = ( html = '' ) => { | ||
const parsed = parse( html ); | ||
this.props.onResetBlocks( parsed ); | ||
}; | ||
|
||
serializeToNativeAction = () => { | ||
const html = serialize( this.props.blocks ); | ||
RNReactNativeGutenbergBridge.provideToNative_Html( html ); | ||
}; | ||
|
||
mergeBlocksAction = ( blockOneClientId, blockTwoClientId ) => { | ||
this.props.onMerge( blockOneClientId, blockTwoClientId ); | ||
}; | ||
|
||
render() { | ||
return ( | ||
<MainApp | ||
blocks={ this.props.blocks } | ||
onChange={ this.onChange } | ||
focusBlockAction={ this.focusBlockAction } | ||
moveBlockUpAction={ this.moveBlockUpAction } | ||
moveBlockDownAction={ this.moveBlockDownAction } | ||
deleteBlockAction={ this.deleteBlockAction } | ||
createBlockAction={ this.createBlockAction } | ||
parseBlocksAction={ this.parseBlocksAction } | ||
serializeToNativeAction={ this.serializeToNativeAction } | ||
mergeBlocksAction={ this.mergeBlocksAction } | ||
isBlockSelected={ this.props.isBlockSelected } | ||
/> | ||
); | ||
} | ||
} | ||
|
||
export default compose( [ | ||
withSelect( ( select ) => { | ||
const { | ||
getBlockIndex, | ||
getBlocks, | ||
getSelectedBlockClientId, | ||
isBlockSelected, | ||
} = select( 'core/editor' ); | ||
const selectedBlockClientId = getSelectedBlockClientId(); | ||
|
||
return { | ||
isBlockSelected, | ||
selectedBlockIndex: getBlockIndex( selectedBlockClientId ), | ||
blocks: getBlocks(), | ||
}; | ||
} ), | ||
withDispatch( ( dispatch ) => { | ||
const { | ||
clearSelectedBlock, | ||
insertBlock, | ||
mergeBlocks, | ||
moveBlocksDown, | ||
moveBlocksUp, | ||
removeBlock, | ||
resetBlocks, | ||
selectBlock, | ||
updateBlockAttributes, | ||
} = dispatch( 'core/editor' ); | ||
|
||
return { | ||
clearSelectedBlock, | ||
onInsertBlock: insertBlock, | ||
onMerge: mergeBlocks, | ||
onMoveDown: moveBlocksDown, | ||
onMoveUp: moveBlocksUp, | ||
onRemove: removeBlock, | ||
onResetBlocks: resetBlocks, | ||
onSelect: ( clientId ) => { | ||
clearSelectedBlock(); | ||
selectBlock( clientId ); | ||
}, | ||
onAttributesUpdate: updateBlockAttributes, | ||
}; | ||
} ), | ||
] )( AppContainer ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.