Skip to content

Commit

Permalink
Refactor core blocks to have save and transforms in their own files (…
Browse files Browse the repository at this point in the history
…part 4) (#14903)
  • Loading branch information
gziolo authored Apr 18, 2019
1 parent 64de3c1 commit ddf0cd7
Show file tree
Hide file tree
Showing 16 changed files with 255 additions and 222 deletions.
33 changes: 4 additions & 29 deletions packages/block-library/src/subhead/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,30 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { createBlock } from '@wordpress/blocks';
import { RichText } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import edit from './edit';
import icon from './icon';
import metadata from './block.json';
import save from './save';
import transforms from './tranforms';

const { name } = metadata;

export { metadata, name };

export const settings = {
title: __( 'Subheading (deprecated)' ),

description: __( 'This block is deprecated. Please use the Paragraph block instead.' ),

icon,

supports: {
// Hide from inserter as this block is deprecated.
inserter: false,
multiple: false,
},

transforms: {
to: [
{
type: 'block',
blocks: [ 'core/paragraph' ],
transform: ( attributes ) =>
createBlock( 'core/paragraph', attributes ),
},
],
},

transforms,
edit,

save( { attributes } ) {
const { align, content } = attributes;

return (
<RichText.Content
tagName="p"
style={ { textAlign: align } }
value={ content }
/>
);
},
save,
};
16 changes: 16 additions & 0 deletions packages/block-library/src/subhead/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';

export default function save( { attributes } ) {
const { align, content } = attributes;

return (
<RichText.Content
tagName="p"
style={ { textAlign: align } }
value={ content }
/>
);
}
17 changes: 17 additions & 0 deletions packages/block-library/src/subhead/tranforms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* WordPress dependencies
*/
import { createBlock } from '@wordpress/blocks';

const transforms = {
to: [
{
type: 'block',
blocks: [ 'core/paragraph' ],
transform: ( attributes ) =>
createBlock( 'core/paragraph', attributes ),
},
],
};

export default transforms;
5 changes: 0 additions & 5 deletions packages/block-library/src/tag-cloud/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@ export const name = 'core/tag-cloud';

export const settings = {
title: __( 'Tag Cloud' ),

description: __( 'A cloud of your most used tags.' ),

icon: 'tag',

category: 'widgets',

supports: {
html: false,
align: true,
},

edit,
};
8 changes: 8 additions & 0 deletions packages/block-library/src/template/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* WordPress dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor';

export default function TemplateEdit() {
return <InnerBlocks />;
}
16 changes: 4 additions & 12 deletions packages/block-library/src/template/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,28 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { InnerBlocks } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import edit from './edit';
import icon from './icon';
import metadata from './block.json';
import save from './save';

const { name } = metadata;

export { metadata, name };

export const settings = {
title: __( 'Reusable Template' ),

description: __( 'Template block used as a container.' ),

icon,

supports: {
customClassName: false,
html: false,
inserter: false,
},

edit() {
return <InnerBlocks />;
},

save() {
return <InnerBlocks.Content />;
},
edit,
save,
};
8 changes: 8 additions & 0 deletions packages/block-library/src/template/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* WordPress dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor';

export default function save() {
return <InnerBlocks.Content />;
}
54 changes: 4 additions & 50 deletions packages/block-library/src/text-columns/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
/**
* External dependencies
*/
import { get, times } from 'lodash';

/**
* WordPress dependencies
*/
import { createBlock } from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';
import { RichText } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import edit from './edit';
import metadata from './block.json';
import save from './save';
import transforms from './tranforms';

const { name } = metadata;

Expand All @@ -25,56 +20,15 @@ export const settings = {
supports: {
inserter: false,
},

title: __( 'Text Columns (deprecated)' ),

description: __( 'This block is deprecated. Please use the Columns block instead.' ),

transforms: {
to: [
{
type: 'block',
blocks: [ 'core/columns' ],
transform: ( { className, columns, content, width } ) => (
createBlock(
'core/columns',
{
align: ( 'wide' === width || 'full' === width ) ? width : undefined,
className,
columns,
},
content.map( ( { children } ) =>
createBlock(
'core/column',
{},
[ createBlock( 'core/paragraph', { content: children } ) ]
)
)
)
),
},
],
},

transforms,
getEditWrapperProps( attributes ) {
const { width } = attributes;
if ( 'wide' === width || 'full' === width ) {
return { 'data-align': width };
}
},

edit,

save( { attributes } ) {
const { width, content, columns } = attributes;
return (
<div className={ `align${ width } columns-${ columns }` }>
{ times( columns, ( index ) =>
<div className="wp-block-column" key={ `column-${ index }` }>
<RichText.Content tagName="p" value={ get( content, [ index, 'children' ] ) } />
</div>
) }
</div>
);
},
save,
};
22 changes: 22 additions & 0 deletions packages/block-library/src/text-columns/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* External dependencies
*/
import { get, times } from 'lodash';

/**
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';

export default function save( { attributes } ) {
const { width, content, columns } = attributes;
return (
<div className={ `align${ width } columns-${ columns }` }>
{ times( columns, ( index ) =>
<div className="wp-block-column" key={ `column-${ index }` }>
<RichText.Content tagName="p" value={ get( content, [ index, 'children' ] ) } />
</div>
) }
</div>
);
}
32 changes: 32 additions & 0 deletions packages/block-library/src/text-columns/tranforms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* WordPress dependencies
*/
import { createBlock } from '@wordpress/blocks';

const transforms = {
to: [
{
type: 'block',
blocks: [ 'core/columns' ],
transform: ( { className, columns, content, width } ) => (
createBlock(
'core/columns',
{
align: ( 'wide' === width || 'full' === width ) ? width : undefined,
className,
columns,
},
content.map( ( { children } ) =>
createBlock(
'core/column',
{},
[ createBlock( 'core/paragraph', { content: children } ) ]
)
)
)
),
},
],
};

export default transforms;
42 changes: 4 additions & 38 deletions packages/block-library/src/verse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,62 +2,28 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { createBlock } from '@wordpress/blocks';
import { RichText } from '@wordpress/block-editor';

/**
* Internal dependencies
*/
import edit from './edit';
import icon from './icon';
import metadata from './block.json';
import save from './save';
import transforms from './tranforms';

const { name } = metadata;

export { metadata, name };

export const settings = {
title: __( 'Verse' ),

description: __( 'Insert poetry. Use special spacing formats. Or quote song lyrics.' ),

icon,

keywords: [ __( 'poetry' ) ],

transforms: {
from: [
{
type: 'block',
blocks: [ 'core/paragraph' ],
transform: ( attributes ) =>
createBlock( 'core/verse', attributes ),
},
],
to: [
{
type: 'block',
blocks: [ 'core/paragraph' ],
transform: ( attributes ) =>
createBlock( 'core/paragraph', attributes ),
},
],
},

transforms,
edit,

save( { attributes } ) {
const { textAlign, content } = attributes;

return (
<RichText.Content
tagName="pre"
style={ { textAlign } }
value={ content }
/>
);
},

save,
merge( attributes, attributesToMerge ) {
return {
content: attributes.content + attributesToMerge.content,
Expand Down
16 changes: 16 additions & 0 deletions packages/block-library/src/verse/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';

export default function save( { attributes } ) {
const { textAlign, content } = attributes;

return (
<RichText.Content
tagName="pre"
style={ { textAlign } }
value={ content }
/>
);
}
Loading

0 comments on commit ddf0cd7

Please sign in to comment.