Skip to content

Commit

Permalink
Block HTML Mode: Validate the block when changing its content
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Sep 28, 2017
1 parent 0e541a0 commit 38c8d79
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
1 change: 1 addition & 0 deletions blocks/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export { createBlock, switchToBlockType } from './factory';
export { default as parse, getSourcedAttributes } from './parser';
export { default as pasteHandler } from './paste';
export { default as serialize, getBlockDefaultClassname, getBlockContent } from './serializer';
export { isValidBlock } from './validation';
export { getCategories } from './categories';
export {
registerBlockType,
Expand Down
18 changes: 17 additions & 1 deletion editor/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export function resetBlocks( blocks ) {
}

/**
* Returns an action object used in signalling that the block with the
* Returns an action object used in signalling that the block attributes with the
* specified UID has been updated.
*
* @param {String} uid Block UID
Expand All @@ -77,6 +77,22 @@ export function updateBlockAttributes( uid, attributes ) {
};
}

/**
* Returns an action object used in signalling that the block with the
* specified UID has been updated.
*
* @param {String} uid Block UID
* @param {Object} updates Block attributes to be merged
* @return {Object} Action object
*/
export function updateBlock( uid, updates ) {
return {
type: 'UPDATE_BLOCK',
uid,
updates,
};
}

export function focusBlock( uid, config ) {
return {
type: 'UPDATE_FOCUS',
Expand Down
15 changes: 9 additions & 6 deletions editor/modes/visual-editor/block-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { isEqual } from 'lodash';
import { Component } from '@wordpress/element';
import { getBlockContent, getSourcedAttributes, getBlockType } from '@wordpress/blocks';
import { getBlockContent, getSourcedAttributes, getBlockType, isValidBlock } from '@wordpress/blocks';

/**
* External Dependencies
Expand All @@ -14,7 +14,7 @@ import TextareaAutosize from 'react-autosize-textarea';
/**
* Internal Dependencies
*/
import { updateBlockAttributes } from '../../actions';
import { updateBlock } from '../../actions';
import { getBlock } from '../../selectors';

class BlockHTML extends Component {
Expand All @@ -23,13 +23,14 @@ class BlockHTML extends Component {
this.onChange = this.onChange.bind( this );
this.onBlur = this.onBlur.bind( this );
this.state = {
html: getBlockContent( props.block ),
html: props.block.isValid ? getBlockContent( props.block ) : props.block.originalContent,
attributes: props.block.attributes,
};
}

componentWillReceiveProps( nextProps ) {
if ( ! isEqual( nextProps.block.attributes, this.state.attributes ) ) {
console.log( nextProps.block.attributes, this.state.attributes );
this.setState( {
html: getBlockContent( nextProps.block ),
} );
Expand All @@ -39,8 +40,10 @@ class BlockHTML extends Component {
onBlur() {
const blockType = getBlockType( this.props.block.name );
const attributes = getSourcedAttributes( this.state.html, blockType.attributes );
const isValid = isValidBlock( this.state.html, blockType, { ...this.props.block.attributes, attributes } );
console.log( 'isvalid', isValid );
this.setState( { attributes } );
this.props.onChange( this.props.uid, attributes );
this.props.onChange( this.props.uid, attributes, this.state.html, isValid );
}

onChange( event ) {
Expand All @@ -62,8 +65,8 @@ export default connect(
block: getBlock( state, ownProps.uid ),
} ),
{
onChange( uid, attributes ) {
return updateBlockAttributes( uid, attributes );
onChange( uid, attributes, originalContent, isValid ) {
return updateBlock( uid, { attributes, originalContent, isValid } );
},
}
)( BlockHTML );
14 changes: 14 additions & 0 deletions editor/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ export const editor = combineUndoableReducers( {
},
};

case 'UPDATE_BLOCK':
// Ignore updates if block isn't known
if ( ! state[ action.uid ] ) {
return state;
}

return {
...state,
[ action.uid ]: {
...state[ action.uid ],
...action.updates,
},
};

case 'INSERT_BLOCKS':
return {
...state,
Expand Down

0 comments on commit 38c8d79

Please sign in to comment.