Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Framework: Bootstrap Block Templates #3668

Merged
merged 2 commits into from Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions editor/actions.js
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
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
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, ( [ 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
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
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 ) {
Copy link
Member

@aduth aduth Nov 27, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notice: Undefined property: WP_Post_Type::$template in /srv/www/editor/htdocs/wp-content/plugins/gutenberg/lib/client-assets.php on line 786

I suggest running with high error reporting:

error_reporting( E_ALL );

(Add near top of site root index.php)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want empty here:

if ( ! empty( $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