Skip to content

Commit

Permalink
Merge pull request #100 from wordpress-mobile/feature/parse-html-into…
Browse files Browse the repository at this point in the history
…-blocks

Parse html into known blocks
  • Loading branch information
etoledom committed Aug 15, 2018
2 parents 15c626a + 59db4ae commit 2265ab4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 4 deletions.
5 changes: 5 additions & 0 deletions src/app/AppContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
moveBlockUpAction,
moveBlockDownAction,
deleteBlockAction,
parseBlocksAction,
} from '../store/actions';
import MainApp from './MainApp';

Expand All @@ -31,6 +32,10 @@ const mapDispatchToProps = ( dispatch, ownProps ) => {
deleteBlockAction: ( clientId ) => {
dispatch( deleteBlockAction( clientId ) );
},
parseBlocksAction: ( html ) => {
dispatch( parseBlocksAction( html ) );
},

};
};

Expand Down
46 changes: 42 additions & 4 deletions src/block-management/block-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type BlockListType = {
moveBlockUpAction: string => mixed,
moveBlockDownAction: string => mixed,
deleteBlockAction: string => mixed,
parseBlocksAction: string => mixed,
blocks: Array<BlockType>,
aztechtml: string,
refresh: boolean,
Expand All @@ -31,6 +32,7 @@ type PropsType = BlockListType;
type StateType = {
dataSource: DataSource,
showHtml: boolean,
html: string,
};

export default class BlockManager extends React.Component<PropsType, StateType> {
Expand All @@ -41,6 +43,7 @@ export default class BlockManager extends React.Component<PropsType, StateType>
this.state = {
dataSource: new DataSource( this.props.blocks, ( item: BlockType ) => item.clientId ),
showHtml: false,
html: '',
};
}

Expand All @@ -58,6 +61,17 @@ export default class BlockManager extends React.Component<PropsType, StateType>
return -1;
}

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

onToolbarButtonPressed( button: number, clientId: string ) {
const dataSourceBlockIndex = this.getDataSourceIndexFromUid( clientId );
switch ( button ) {
Expand Down Expand Up @@ -96,6 +110,12 @@ export default class BlockManager extends React.Component<PropsType, StateType>
}, '' );
}

parseHTML() {
const { parseBlocksAction } = this.props;
const { html } = this.state;
parseBlocksAction( html );
}

componentDidUpdate() {
// List has been updated, tell the recycler view to update the view
this.state.dataSource.setDirty();
Expand All @@ -106,7 +126,8 @@ export default class BlockManager extends React.Component<PropsType, StateType>
const index = this.getDataSourceIndexFromUid( clientId );
const dataSource = this.state.dataSource;
const block = dataSource.get( this.getDataSourceIndexFromUid( clientId ) );
dataSource.set( index, { ...block, attributes: attributes } );
block.attributes = attributes;
dataSource.set( index, block );
// Update Redux store
this.props.onChange( clientId, attributes );
}
Expand Down Expand Up @@ -149,7 +170,7 @@ export default class BlockManager extends React.Component<PropsType, StateType>
activeText={ 'On' }
inActiveText={ 'Off' }
value={ this.state.showHtml }
onValueChange={ ( value ) => this.setState( { showHtml: value } ) }
onValueChange={ this.handleSwitchEditor }
/>
</View>
{ this.state.showHtml && this.renderHTML() }
Expand All @@ -158,6 +179,21 @@ export default class BlockManager extends React.Component<PropsType, StateType>
);
}

handleSwitchEditor = ( showHtml: boolean ) => {
if ( showHtml ) {
const html = this.serializeToHtml();
this.handleHTMLUpdate( html );
} else {
this.parseHTML();
}

this.setState( { showHtml } );
}

handleHTMLUpdate = ( html: string ) => {
this.setState( { html } );
}

renderItem( value: { item: BlockType, clientId: string } ) {
return (
<BlockHolder
Expand All @@ -179,8 +215,10 @@ export default class BlockManager extends React.Component<PropsType, StateType>
<TextInput
multiline
numberOfLines={ 0 }
style={ styles.htmlView }>
{ this.serializeToHtml() }
style={ styles.htmlView }
value={ this.state.html }
onChangeText={ this.handleHTMLUpdate }>

</TextInput>
</KeyboardAvoidingView>
);
Expand Down
1 change: 1 addition & 0 deletions src/store/actions/ActionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ export default {
MOVE_UP: 'BLOCK_MOVE_UP_ACTION',
MOVE_DOWN: 'BLOCK_MOVE_DOWN_ACTION',
DELETE: 'BLOCK_DELETE_ACTION',
PARSE: 'BLOCK_PARSE_ACTION',
},
};
10 changes: 10 additions & 0 deletions src/store/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ export type BlockActionType = string => {
clientId: string,
};

export type ParseActionType = string => {
type: $Values<typeof ActionTypes.BLOCK>,
html: string,
};

export function updateBlockAttributes( clientId: string, attributes: mixed ) {
return {
type: ActionTypes.BLOCK.UPDATE_ATTRIBUTES,
Expand Down Expand Up @@ -37,3 +42,8 @@ export const deleteBlockAction: BlockActionType = clientId => ( {
type: ActionTypes.BLOCK.DELETE,
clientId,
} );

export const parseBlocksAction: ParseActionType = html => ( {
type: ActionTypes.BLOCK.PARSE,
html,
} );
5 changes: 5 additions & 0 deletions src/store/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { find, findIndex, reduce } from 'lodash';
import ActionTypes from '../actions/ActionTypes';
import type { StateType } from '../';
import type { BlockActionType } from '../actions';
import { parse } from '@wordpress/blocks';

function findBlock( blocks, clientId: string ) {
return find( blocks, obj => {
Expand Down Expand Up @@ -108,6 +109,10 @@ export const reducer = (
blocks.splice( index, 1 );
return { blocks: blocks, refresh: ! state.refresh };
}
case ActionTypes.BLOCK.PARSE: {
const parsed = parse(action.html)
return { blocks: parsed, refresh: ! state.refresh, fullparse: true };
}
default:
return state;
}
Expand Down
14 changes: 14 additions & 0 deletions src/store/reducers/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import { reducer } from './';
import * as actions from '../actions/';
import { registerCoreBlocks } from '@gutenberg/core-blocks';

describe( 'Store', () => {
describe( 'reducer', () => {
Expand All @@ -12,6 +13,8 @@ describe( 'Store', () => {
let initialState;

beforeAll( () => {
registerCoreBlocks()

__iniState = {
blocks: [
{
Expand Down Expand Up @@ -156,5 +159,16 @@ describe( 'Store', () => {
// the code block should be at the bottom
expect( newState.blocks[ 1 ].blockType ).toEqual( 'core/code' );
} );

it( 'parses the html string into a new array of blocks', () => {
const htmlContent = '<!--more-->'
const html = '<!-- wp:more -->' + htmlContent + '<!-- /wp:more -->'

const newState = reducer( initialState, actions.parseBlocksAction( html ) );

expect( newState.blocks ).toHaveLength( 1 );
expect( newState.blocks[ 0 ].originalContent ).toEqual( htmlContent );
expect( newState.blocks[ 0 ].name ).toEqual( 'core/more' );
})
} );
} );

0 comments on commit 2265ab4

Please sign in to comment.