From 55454fe365dab6ce2b77c00f33e4c266c341db48 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Mon, 27 Nov 2017 12:39:04 +0100 Subject: [PATCH 1/2] Framework: Bootstrap Block Templates --- editor/actions.js | 10 ++++++---- editor/components/provider/index.js | 14 ++++++-------- editor/effects.js | 12 +++++++++++- editor/test/effects.js | 6 +++--- lib/client-assets.php | 5 +++++ 5 files changed, 31 insertions(+), 16 deletions(-) diff --git a/editor/actions.js b/editor/actions.js index a35fb7ba337ff..d6583902e701b 100644 --- a/editor/actions.js +++ b/editor/actions.js @@ -6,15 +6,17 @@ import { partial, castArray } from 'lodash'; /** * Returns an action object used in signalling that editor has initialized with - * the specified post object. + * the specified post object and editor settings. * - * @param {Object} post Post object - * @return {Object} Action object + * @param {Object} post Post object + * @param {Object} settings Editor settings object + * @return {Object} Action object */ -export function setupEditor( post ) { +export function setupEditor( post, settings ) { return { type: 'SETUP_EDITOR', post, + settings, }; } diff --git a/editor/components/provider/index.js b/editor/components/provider/index.js index b9b0b3e99816c..b1723e11abb3e 100644 --- a/editor/components/provider/index.js +++ b/editor/components/provider/index.js @@ -42,19 +42,17 @@ class EditorProvider extends Component { constructor( props ) { super( ...arguments ); - const store = createReduxStore( props.initialState ); + this.store = createReduxStore( props.initialState ); + this.settings = { + ...DEFAULT_SETTINGS, + ...props.settings, + }; // If initial state is passed, assume that we don't need to initialize, // as in the case of an error recovery. if ( ! props.initialState ) { - store.dispatch( setupEditor( props.post ) ); + this.store.dispatch( setupEditor( props.post, this.settings ) ); } - - this.store = store; - this.settings = { - ...DEFAULT_SETTINGS, - ...props.settings, - }; } getChildContext() { diff --git a/editor/effects.js b/editor/effects.js index 0d3e5869d1309..02aaaa813a194 100644 --- a/editor/effects.js +++ b/editor/effects.js @@ -268,12 +268,22 @@ export default { dispatch( savePost() ); }, SETUP_EDITOR( action ) { - const { post } = action; + const { post, settings } = action; const effects = []; // Parse content as blocks if ( post.content.raw ) { effects.push( resetBlocks( parse( post.content.raw ) ) ); + } else if ( settings.template ) { + const blocks = map( settings.template.blocks, ( { name, attributes } ) => { + const block = createBlock( name ); + block.attributes = { + ...block.attributes, + ...attributes, + }; + return block; + } ); + effects.push( resetBlocks( blocks ) ); } // Resetting post should occur after blocks have been reset, since it's diff --git a/editor/test/effects.js b/editor/test/effects.js index be6bb7fb9fb78..bf2ab9ce4fb5e 100644 --- a/editor/test/effects.js +++ b/editor/test/effects.js @@ -342,7 +342,7 @@ describe( 'effects', () => { status: 'draft', }; - const result = handler( { post } ); + const result = handler( { post, settings: {} } ); expect( result ).toEqual( [ resetPost( post ), @@ -362,7 +362,7 @@ describe( 'effects', () => { status: 'draft', }; - const result = handler( { post } ); + const result = handler( { post, settings: {} } ); expect( result ).toHaveLength( 2 ); expect( result ).toContainEqual( resetPost( post ) ); @@ -383,7 +383,7 @@ describe( 'effects', () => { status: 'auto-draft', }; - const result = handler( { post } ); + const result = handler( { post, settings: {} } ); expect( result ).toEqual( [ resetPost( post ), diff --git a/lib/client-assets.php b/lib/client-assets.php index 76aa327d8b09e..9fdf448316d26 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -782,6 +782,11 @@ function gutenberg_editor_scripts_and_styles( $hook ) { 'blockTypes' => $allowed_block_types, ); + $post_type_object = get_post_type_object( $post_to_edit['type'] ); + if ( $post_type_object->template ) { + $editor_settings['template'] = $post_type_object->template; + } + $script = '( function() {'; $script .= sprintf( 'var editorSettings = %s;', wp_json_encode( $editor_settings ) ); $script .= << Date: Mon, 27 Nov 2017 14:39:52 +0100 Subject: [PATCH 2/2] Framework: Simplify the block template format --- editor/effects.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/editor/effects.js b/editor/effects.js index 02aaaa813a194..6a9aeddbda673 100644 --- a/editor/effects.js +++ b/editor/effects.js @@ -275,7 +275,7 @@ export default { if ( post.content.raw ) { effects.push( resetBlocks( parse( post.content.raw ) ) ); } else if ( settings.template ) { - const blocks = map( settings.template.blocks, ( { name, attributes } ) => { + const blocks = map( settings.template, ( [ name, attributes ] ) => { const block = createBlock( name ); block.attributes = { ...block.attributes,