Skip to content

Commit

Permalink
Editor: Integrate post-title and post-content with custom sources.
Browse files Browse the repository at this point in the history
  • Loading branch information
epiqueras committed Jul 8, 2019
1 parent 214f929 commit eaa31b9
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 9 deletions.
3 changes: 2 additions & 1 deletion packages/block-library/src/post-content/block.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "core/post-content",
"category": "common"
"category": "common",
"multiple": false
}
4 changes: 3 additions & 1 deletion packages/block-library/src/post-title/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"category": "common",
"attributes": {
"title": {
"type": "string"
"type": "string",
"source": "post",
"attribute": "title"
}
}
}
17 changes: 12 additions & 5 deletions packages/blocks/src/api/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,21 @@ export function synchronizeBlocksWithTemplate( blocks = [], template ) {
}

return map( template, ( templateBlock, index ) => {
const [ name, attributes, innerBlocksTemplate ] = Array.isArray( templateBlock ) ?
const [ name, attributes, _innerBlocksTemplate ] = Array.isArray( templateBlock ) ?
templateBlock :
[ templateBlock.name, templateBlock.attributes, templateBlock.innerBlocksTemplate ];
[ templateBlock.name, templateBlock.attributes, templateBlock.innerBlocks ];
const block = blocks[ index ];
let innerBlocksTemplate = _innerBlocksTemplate;

if ( block && block.name === name ) {
const innerBlocks = synchronizeBlocksWithTemplate( block.innerBlocks, innerBlocksTemplate );
return { ...block, innerBlocks };
if ( block ) {
if ( block.name === name ) {
const innerBlocks = synchronizeBlocksWithTemplate( block.innerBlocks, innerBlocksTemplate );
return { ...block, innerBlocks };
}

if ( 'core/post-content' === name ) {
innerBlocksTemplate = blocks;
}
}

// To support old templates that were using the "children" format
Expand Down
4 changes: 3 additions & 1 deletion packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { dispatch, select, apiFetch } from '@wordpress/data-controls';
import {
parse,
synchronizeBlocksWithTemplate,
serialize,
} from '@wordpress/blocks';
import isShallowEqual from '@wordpress/is-shallow-equal';

Expand Down Expand Up @@ -453,7 +454,8 @@ export function* savePost( options = {} ) {
content: editedPostContent,
},
} );
editedPostContent = '';
const postContentBlock = ( yield select( STORE_KEY, 'getBlocksForSerialization' ) ).find( ( block ) => block.name === 'core/post-content' );
editedPostContent = postContentBlock ? serialize( postContentBlock.innerBlocks ) : '';
}

let toSend = {
Expand Down
3 changes: 2 additions & 1 deletion packages/editor/src/store/block-sources/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* Internal dependencies
*/
import * as post from './post';
import * as meta from './meta';

export { meta };
export { post, meta };
46 changes: 46 additions & 0 deletions packages/editor/src/store/block-sources/post.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* External dependencies
*/
import { get, set } from 'lodash';

/**
* WordPress dependencies
*/
import { select } from '@wordpress/data-controls';

/**
* Internal dependencies
*/
import { editPost } from '../actions';
import { EDIT_MERGE_PROPERTIES } from '../constants';

export function* getDependencies() {
return {
post: yield select( 'core/editor', 'getCurrentPost' ),
content: yield select( 'core/editor', 'getEditedPostContent' ),
edits: yield select( 'core/editor', 'getPostEdits' ),
};
}

export function apply( { attribute }, { post, content, edits } ) {
if ( 'content' === attribute ) {
return content;
}

if ( undefined === get( edits, attribute ) ) {
return get( post, attribute );
}

if ( EDIT_MERGE_PROPERTIES.has( attribute ) ) {
return {
...get( post, attribute ),
...get( edits, attribute ),
};
}

return get( edits, attribute );
}

export function* update( { attribute }, value ) {
yield editPost( set( {}, attribute, value ) );
}

0 comments on commit eaa31b9

Please sign in to comment.