Skip to content

Commit

Permalink
Framework: Bootstrap Block Templates
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Nov 27, 2017
1 parent 9864a60 commit 55454fe
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
10 changes: 6 additions & 4 deletions editor/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
}

Expand Down
14 changes: 6 additions & 8 deletions editor/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
12 changes: 11 additions & 1 deletion editor/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions editor/test/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ describe( 'effects', () => {
status: 'draft',
};

const result = handler( { post } );
const result = handler( { post, settings: {} } );

expect( result ).toEqual( [
resetPost( post ),
Expand All @@ -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 ) );
Expand All @@ -383,7 +383,7 @@ describe( 'effects', () => {
status: 'auto-draft',
};

const result = handler( { post } );
const result = handler( { post, settings: {} } );

expect( result ).toEqual( [
resetPost( post ),
Expand Down
5 changes: 5 additions & 0 deletions lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 .= <<<JS
Expand Down

0 comments on commit 55454fe

Please sign in to comment.