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

Parse html into known blocks #100

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c6b1b08
Added action to parse html into blocks
etoledom Aug 9, 2018
8e26b60
Merge branch 'feature/editable-html-view-feature-branch' into feature…
etoledom Aug 9, 2018
01be09f
Remove reducer dependency on parser
etoledom Aug 9, 2018
e7a7ff8
Lint fixes
etoledom Aug 9, 2018
885a47b
AppContainer: Removed unused `parser` property from `mapStateToProps`
etoledom Aug 9, 2018
288178a
Minor code clean up
etoledom Aug 9, 2018
ce96e20
block-manager: added `html` property as a prop.
etoledom Aug 10, 2018
a9647ea
lint --fix
etoledom Aug 10, 2018
79935ed
block-manager: removed parser and html from props
etoledom Aug 10, 2018
419e4fa
block-manager: remove unnecesary `componentDidMount`
etoledom Aug 10, 2018
770eeaa
Actions: remove `uid` from `parseBlocksAction`
etoledom Aug 10, 2018
eb2a720
block-manager: Adding `html` as part of the component state
etoledom Aug 10, 2018
18acd22
BlockListType: remove `parser` from `parseBlocksAction`
etoledom Aug 10, 2018
75dd4bf
fix parseBlocksAction test
etoledom Aug 10, 2018
4aed861
Merge branch 'master' into feature/parse-html-into-blocks
etoledom Aug 10, 2018
77b2c10
Merge branch 'feature/editable-html-view-feature-branch' into feature…
etoledom Aug 10, 2018
55716fe
parseBlocksAction: rename `payload` to `html`
etoledom Aug 13, 2018
91ffa52
reducer tests: Moved `registerCoreBlocks()` to `beforeAll()`
etoledom Aug 13, 2018
cc6534a
merge submodule commit
etoledom Aug 13, 2018
1b79efb
Merge branch 'feature/editable-html-view-feature-branch' into feature…
etoledom Aug 13, 2018
586526b
restart datasource after full parse of html
mzorz Aug 14, 2018
59db4ae
Merge pull request #116 from wordpress-mobile/try/mario-full-parse
mzorz Aug 15, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' );
})
} );
} );