-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Serializer: Adding a block state serializer (blocks => postContent) #348
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5ce0284
Serializer: Adding a block state serializer (blocks => postContent)
youknowriad e4d3086
Flip source of truth to blocks first
aduth 31ccf60
Defer change event to blur
aduth 3c84cc1
Support string return values from save
aduth d1b75f7
Reuse getBlockAttributes from parser
aduth 0dfc163
Extract comment attribute serialization
aduth 20b7b94
Support save serialization by component type
aduth 0db45f2
Document save return value as string
aduth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { difference } from 'lodash'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { getBlockSettings } from './registration'; | ||
| import { getBlockAttributes } from './parser'; | ||
|
|
||
| /** | ||
| * Given a block's save render implementation and attributes, returns the | ||
| * static markup to be saved. | ||
| * | ||
| * @param {Function|WPComponent} save Save render implementation | ||
| * @param {Object} attributes Block attributes | ||
| * @return {string} Save content | ||
| */ | ||
| export function getSaveContent( save, attributes ) { | ||
| let rawContent; | ||
|
|
||
| if ( save.prototype instanceof wp.element.Component ) { | ||
| rawContent = wp.element.createElement( save, { attributes } ); | ||
| } else { | ||
| rawContent = save( { attributes } ); | ||
|
|
||
| // Special-case function render implementation to allow raw HTML return | ||
| if ( 'string' === typeof rawContent ) { | ||
| return rawContent; | ||
| } | ||
| } | ||
|
|
||
| // Otherwise, infer as element | ||
| return wp.element.renderToString( rawContent ); | ||
| } | ||
|
|
||
| /** | ||
| * Returns comment attributes as serialized string, determined by subset of | ||
| * difference between actual attributes of a block and those expected based | ||
| * on its settings. | ||
| * | ||
| * @param {Object} realAttributes Actual block attributes | ||
| * @param {Object} expectedAttributes Expected block attributes | ||
| * @return {string} Comment attributes | ||
| */ | ||
| export function getCommentAttributes( realAttributes, expectedAttributes ) { | ||
| // Find difference and build into object subset of attributes. | ||
| const keys = difference( | ||
| Object.keys( realAttributes ), | ||
| Object.keys( expectedAttributes ) | ||
| ); | ||
|
|
||
| // Serialize the comment attributes | ||
| return keys.reduce( ( memo, key ) => { | ||
| const value = realAttributes[ key ]; | ||
| return memo + `${ key }:${ value } `; | ||
| }, '' ); | ||
| } | ||
|
|
||
| /** | ||
| * Takes a block list and returns the serialized post content | ||
| * | ||
| * @param {Array} blocks Block list | ||
| * @return {String} The post content | ||
| */ | ||
| export default function serialize( blocks ) { | ||
| return blocks.reduce( ( memo, block ) => { | ||
| const blockType = block.blockType; | ||
| const settings = getBlockSettings( blockType ); | ||
|
|
||
| return memo + ( | ||
| '<!-- wp:' + | ||
| blockType + | ||
| ' ' + | ||
| getCommentAttributes( | ||
| block.attributes, | ||
| getBlockAttributes( block, settings ) | ||
| ) + | ||
| '-->' + | ||
| getSaveContent( settings.save, block.attributes ) + | ||
| '<!-- /wp:' + | ||
| blockType + | ||
| ' -->' | ||
| ); | ||
| }, '' ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /** | ||
| * External dependencies | ||
| */ | ||
| import { expect } from 'chai'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import serialize, { getCommentAttributes, getSaveContent } from '../serializer'; | ||
| import { getBlocks, registerBlock, unregisterBlock } from '../registration'; | ||
|
|
||
| describe( 'block serializer', () => { | ||
| afterEach( () => { | ||
| getBlocks().forEach( block => { | ||
| unregisterBlock( block.slug ); | ||
| } ); | ||
| } ); | ||
|
|
||
| describe( 'getSaveContent()', () => { | ||
| context( 'function save', () => { | ||
| it( 'should return string verbatim', () => { | ||
| const saved = getSaveContent( | ||
| ( { attributes } ) => attributes.fruit, | ||
| { fruit: 'Bananas' } | ||
| ); | ||
|
|
||
| expect( saved ).to.equal( 'Bananas' ); | ||
| } ); | ||
|
|
||
| it( 'should return element as string if save returns element', () => { | ||
| const { createElement } = wp.element; | ||
| const saved = getSaveContent( | ||
| ( { attributes } ) => createElement( 'div', null, attributes.fruit ), | ||
| { fruit: 'Bananas' } | ||
| ); | ||
|
|
||
| expect( saved ).to.equal( '<div>Bananas</div>' ); | ||
| } ); | ||
| } ); | ||
|
|
||
| context( 'component save', () => { | ||
| it( 'should return element as string', () => { | ||
| const { Component, createElement } = wp.element; | ||
| const saved = getSaveContent( | ||
| class extends Component { | ||
| render() { | ||
| return createElement( 'div', null, this.props.attributes.fruit ); | ||
| } | ||
| }, | ||
| { fruit: 'Bananas' } | ||
| ); | ||
|
|
||
| expect( saved ).to.equal( '<div>Bananas</div>' ); | ||
| } ); | ||
| } ); | ||
| } ); | ||
|
|
||
| describe( 'getCommentAttributes()', () => { | ||
| it( 'should return empty string if no difference', () => { | ||
| const attributes = getCommentAttributes( {}, {} ); | ||
|
|
||
| expect( attributes ).to.equal( '' ); | ||
| } ); | ||
|
|
||
| it( 'should return joined string of key:value pairs by difference subset', () => { | ||
| const attributes = getCommentAttributes( { | ||
| fruit: 'bananas', | ||
| category: 'food', | ||
| ripeness: 'ripe' | ||
| }, { | ||
| fruit: 'bananas' | ||
| } ); | ||
|
|
||
| expect( attributes ).to.equal( 'category:food ripeness:ripe ' ); | ||
| } ); | ||
| } ); | ||
|
|
||
| describe( 'serialize()', () => { | ||
| it( 'should serialize the post content properly', () => { | ||
| const blockSettings = { | ||
| attributes: ( rawContent ) => { | ||
| return { | ||
| content: rawContent | ||
| }; | ||
| }, | ||
| save( { attributes } ) { | ||
| return <p dangerouslySetInnerHTML={ { __html: attributes.content } } />; | ||
| } | ||
| }; | ||
| registerBlock( 'core/test-block', blockSettings ); | ||
| const blockList = [ | ||
| { | ||
| blockType: 'core/test-block', | ||
| attributes: { | ||
| content: 'Ribs & Chicken', | ||
| align: 'left' | ||
| } | ||
| } | ||
| ]; | ||
| const expectedPostContent = '<!-- wp:core/test-block align:left --><p>Ribs & Chicken</p><!-- /wp:core/test-block -->'; | ||
|
|
||
| expect( serialize( blockList ) ).to.eql( expectedPostContent ); | ||
| } ); | ||
| } ); | ||
| } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think there's a few functions we could split out of this long block perhaps for easier testing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I noted above, it's hard to do without duplicating code (like computing rawContent and blockSettings several times) or passing those as parameters (which creates weird API)