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

Try Building a Template Block #1970

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions blocks/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,17 @@ export function switchToBlockType( block, name ) {
};
} );
}

/**
* Gives us a `to` transformation that renders the given component into a text block
*
* @param {Object} Component The component to render, it must accept an `attributes` prop. It must include only inline elements, because the text block is a `p`.
* @return {Object} The transformation
*/
export function transformComponentToText( Component ) {
return {
type: 'block',
blocks: [ 'core/text' ],
transform: ( attributes ) => createBlock( 'core/text', { content: <Component attributes={ attributes } /> } ),
};
}
2 changes: 1 addition & 1 deletion blocks/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import * as query from './query';

export { query };
export { createBlock, switchToBlockType } from './factory';
export { createBlock, switchToBlockType, transformComponentToText } from './factory';
export { default as parse } from './parser';
export { default as pasteHandler } from './paste';
export { default as serialize, getBlockDefaultClassname } from './serializer';
Expand Down
67 changes: 66 additions & 1 deletion blocks/api/test/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import { noop } from 'lodash';
/**
* Internal dependencies
*/
import { createBlock, switchToBlockType } from '../factory';
import { createBlock, switchToBlockType, transformComponentToText } from '../factory';
import { getBlockTypes, unregisterBlockType, setUnknownTypeHandler, registerBlockType } from '../registration';
import { Component } from 'element'
Copy link
Member

Choose a reason for hiding this comment

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

This should be in a separate "WordPress dependencies block":

https://github.com/WordPress/gutenberg/blob/master/docs/coding-guidelines.md#imports

Note with #2172, these need to be updated to prefix the dependencies with @wordpress/. You will need to perform a rebase against the latest version of master and apply your changes:

git fetch origin
git rebase origin/master


describe( 'block factory', () => {
const defaultBlockSettings = { save: noop };
Expand Down Expand Up @@ -363,4 +364,68 @@ describe( 'block factory', () => {
} ] );
} );
} );
describe( 'transformComponentToText()', () => {
it( 'should transform a stateless edit/save component to a text block', () => {
const Save = ( { attributes } ) => <span>Hi, { attributes.name }</span>;
registerBlockType( 'test/hi', {
transforms: {
to: [ transformComponentToText( Save ) ],
},
edit: noop,
save: Save,
} );
registerBlockType( 'core/text', defaultBlockSettings );

const block = {
uid: 1,
name: 'test/hi',
attributes: {
name: 'Baba',
},
};

const updatedBlock = switchToBlockType( block, 'core/text' );

expect( updatedBlock ).toEqual( [ {
uid: 1,
name: 'core/text',
attributes: {
content: <Save attributes={ { name: 'Baba' } } />,
},
} ] );
} );
it( 'should transform a full-blown edit/save component to a text block', () => {
const Save = class extends Component {
render() {
return <span>Howdy, { this.props.attributes.name }</span>;
}
};
registerBlockType( 'test/hi', {
transforms: {
to: [ transformComponentToText( Save ) ],
},
edit: noop,
save: Save,
} );
registerBlockType( 'core/text', defaultBlockSettings );

const block = {
uid: 1,
name: 'test/hi',
attributes: {
name: 'Baba',
},
};

const updatedBlock = switchToBlockType( block, 'core/text' );

expect( updatedBlock ).toEqual( [ {
uid: 1,
name: 'core/text',
attributes: {
content: <Save attributes={ { name: 'Baba' } } />,
},
} ] );
} );
} );
} );
1 change: 1 addition & 0 deletions blocks/library/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ import './freeform';
import './latest-posts';
import './cover-image';
import './verse';
import './template';
43 changes: 43 additions & 0 deletions blocks/library/template/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* WordPress dependencies
*/
import { __ } from 'i18n';

/**
* Internal dependencies
*/
import { registerBlockType, transformComponentToText } from '../../api';
import AttributeInput from 'components/attribute-input';

function template( mode ) {
return function( { attributes, setAttributes } ) {
return <span>
Hi, my name is {
'edit' === mode ?
<AttributeInput type="text" placeholder="Peter" value={ attributes.name } attribute="name" setAttributes={ setAttributes } /> :
attributes.name
}! <br />
#intro { tagify( attributes.name ) }
</span>;
};
}

function tagify( name ) {
return name ? '#' + name.toLowerCase().replace( /\s+/g, '-' ) : '';
}

registerBlockType( 'core/template', {
title: __( 'Template for Templates' ),
defaultAttributes: {
name: '',
},
transforms: {
to: [ transformComponentToText( template( 'save' ) ) ],
},

icon: 'list-view',
category: 'widgets',

edit: template( 'edit' ),
save: template( 'save' ),
} );
24 changes: 24 additions & 0 deletions components/attribute-input/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
The `AttributeInput` component can be used to add an `input` to a block, whose value is always synchronized with a certain attribute.

#### Props

The following props are used to tell us which attrbute to connect the `input` to and how to update the attribute value. Any additional props will be passed to the rendered `<input />`.

* `attribute`: (string) the name of the block attribute to connect the `input` to.
* `setAttribute`: (function) the block's `setAttribute` function, we need it to be able to set the attribute value when the input changes.

#### Example:

```javascript
registerBlockType( 'example/hello', {
title: __( 'Hello' ),
icon: 'list-view',
category: 'widgets',
edit: ( { attributes, setAttributes } ) => (
<span>Enter your name: <AttributeInput type="text" value={ attributes.name } placeholder="Baba" attribute="name" setAttributes={ setAttributes } /></span>
),
save: ( { attributes } ) => (
<span>Hello, { attributes.name }!</span>
),
} );
```
3 changes: 3 additions & 0 deletions components/attribute-input/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function AttributeInput( { attribute, setAttributes, value = '', ...inputProps } ) {
return <input value={ value } onChange={ ( e ) => setAttributes( { [ attribute ]: e.target.value } ) } { ...inputProps } />;
}