Skip to content
Merged
Changes from 1 commit
Commits
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
Prev Previous commit
Next Next commit
Convert templates containing a "children" format into html strings
  • Loading branch information
youknowriad committed Oct 10, 2018
commit 9ec19f0e45a97b48f26cde3891f99ca3f2dae0fc
45 changes: 43 additions & 2 deletions packages/blocks/src/api/templates.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
/**
* External dependencies
*/
import { every, map } from 'lodash';
import { every, map, get, mapValues, isArray } from 'lodash';

/**
* WordPress dependencies
*/
import { renderToString } from '@wordpress/element';

/**
* Internal dependencies
*/
import { createBlock } from './factory';
import { getBlockType } from './registration';

/**
* Checks whether a list of blocks matches a template by comparing the block names.
Expand Down Expand Up @@ -56,9 +62,44 @@ export function synchronizeBlocksWithTemplate( blocks = [], template ) {
return { ...block, innerBlocks };
}

// To support old templates that were using the "children" format
// for the attributes using "html" strings now, we normalize the template attributes
// before creating the blocks.

const blockType = getBlockType( name );
const isHTMLAttribute = ( attributeDefinition ) => get( attributeDefinition, [ 'source' ] ) === 'html';
const isQueryAttribute = ( attributeDefinition ) => get( attributeDefinition, [ 'source' ] ) === 'query';

const normalizeAttributes = ( schema, values ) => {
return mapValues( values, ( value, key ) => {
return normalizeAttribute( schema[ key ], value );
} );
};
const normalizeAttribute = ( definition, value ) => {
if ( isHTMLAttribute( definition ) && isArray( value ) ) {
// Introduce a deprecated call at this point
// When we're confident that "children" format should be removed from the templates.

return renderToString( value );
}

if ( isQueryAttribute( definition ) && value ) {
return value.map( ( subValues ) => {
return normalizeAttributes( definition.query, subValues );
} );
}

return value;
};

const normalizedAttributes = normalizeAttributes(
get( blockType, [ 'attributes' ], {} ),
attributes
);

return createBlock(
name,
attributes,
normalizedAttributes,
synchronizeBlocksWithTemplate( [], innerBlocksTemplate )
);
} );
Expand Down