diff --git a/editor/actions.js b/editor/actions.js index a35fb7ba337ff2..d6583902e701b7 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 b9b0b3e99816ca..b1723e11abb3ea 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 0d3e5869d13097..02aaaa813a1942 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 be6bb7fb9fb78b..bf2ab9ce4fb5e8 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 76aa327d8b09e0..9fdf448316d267 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 .= <<