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

Refactor core blocks to have save and transforms in their own files (part 1) #14882

Merged
merged 1 commit into from
Apr 12, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions packages/block-library/src/archives/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,12 @@ export const name = 'core/archives';

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

description: __( 'Display a monthly archive of your posts.' ),

icon,

category: 'widgets',

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

edit,
};
73 changes: 4 additions & 69 deletions packages/block-library/src/audio/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
/**
* WordPress dependencies
*/
import { createBlobURL } from '@wordpress/blob';
import { createBlock } from '@wordpress/blocks';
import { RichText } from '@wordpress/block-editor';
import { __ } from '@wordpress/i18n';

/**
Expand All @@ -12,83 +9,21 @@ import { __ } from '@wordpress/i18n';
import edit from './edit';
import icon from './icon';
import metadata from './block.json';
import save from './save';
import transforms from './transforms';

const { name } = metadata;

export { metadata, name };

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

description: __( 'Embed a simple audio player.' ),

icon,

transforms: {
from: [
{
type: 'files',
isMatch( files ) {
return files.length === 1 && files[ 0 ].type.indexOf( 'audio/' ) === 0;
},
transform( files ) {
const file = files[ 0 ];
// We don't need to upload the media directly here
// It's already done as part of the `componentDidMount`
// in the audio block
const block = createBlock( 'core/audio', {
src: createBlobURL( file ),
} );

return block;
},
},
{
type: 'shortcode',
tag: 'audio',
attributes: {
src: {
type: 'string',
shortcode: ( { named: { src } } ) => {
return src;
},
},
loop: {
type: 'string',
shortcode: ( { named: { loop } } ) => {
return loop;
},
},
autoplay: {
type: 'srting',
shortcode: ( { named: { autoplay } } ) => {
return autoplay;
},
},
preload: {
type: 'string',
shortcode: ( { named: { preload } } ) => {
return preload;
},
},
},
},
],
},

transforms,
supports: {
align: true,
},

edit,

save( { attributes } ) {
const { autoplay, caption, loop, preload, src } = attributes;
return (
<figure>
<audio controls="controls" src={ src } autoPlay={ autoplay } loop={ loop } preload={ preload } />
{ ! RichText.isEmpty( caption ) && <RichText.Content tagName="figcaption" value={ caption } /> }
</figure>
);
},
save,
};
15 changes: 15 additions & 0 deletions packages/block-library/src/audio/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* WordPress dependencies
*/
import { RichText } from '@wordpress/block-editor';

export default function save( { attributes } ) {
const { autoplay, caption, loop, preload, src } = attributes;

return (
<figure>
<audio controls="controls" src={ src } autoPlay={ autoplay } loop={ loop } preload={ preload } />
{ ! RichText.isEmpty( caption ) && <RichText.Content tagName="figcaption" value={ caption } /> }
</figure>
);
}
59 changes: 59 additions & 0 deletions packages/block-library/src/audio/transforms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* WordPress dependencies
*/
import { createBlobURL } from '@wordpress/blob';
import { createBlock } from '@wordpress/blocks';

const transforms = {
from: [
{
type: 'files',
isMatch( files ) {
return files.length === 1 && files[ 0 ].type.indexOf( 'audio/' ) === 0;
},
transform( files ) {
const file = files[ 0 ];
// We don't need to upload the media directly here
// It's already done as part of the `componentDidMount`
// in the audio block
const block = createBlock( 'core/audio', {
src: createBlobURL( file ),
} );

return block;
},
},
{
type: 'shortcode',
tag: 'audio',
attributes: {
src: {
type: 'string',
shortcode: ( { named: { src } } ) => {
return src;
},
},
loop: {
type: 'string',
shortcode: ( { named: { loop } } ) => {
return loop;
},
},
autoplay: {
type: 'srting',
shortcode: ( { named: { autoplay } } ) => {
return autoplay;
},
},
preload: {
type: 'string',
shortcode: ( { named: { preload } } ) => {
return preload;
},
},
},
},
],
};

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

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

category: 'reusable',

description: __( 'Create content, and save it for you and other contributors to reuse across your site. Update the block, and the changes apply everywhere it’s used.' ),

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

edit,
};
55 changes: 3 additions & 52 deletions packages/block-library/src/button/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
/**
* External dependencies
*/
import classnames from 'classnames';
import { omit, pick } from 'lodash';

/**
* WordPress dependencies
*/
import { __, _x } from '@wordpress/i18n';
import {
RichText,
getColorClassName,
} from '@wordpress/block-editor';
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';

const { name, attributes: blockAttributes } = metadata;

Expand All @@ -34,66 +31,20 @@ const colorsMigration = ( attributes ) => {

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

description: __( 'Prompt visitors to take action with a button-style link.' ),

icon,

keywords: [ __( 'link' ) ],

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

styles: [
{ name: 'default', label: _x( 'Default', 'block style' ), isDefault: true },
{ name: 'outline', label: __( 'Outline' ) },
{ name: 'squared', label: _x( 'Squared', 'block style' ) },
],

edit,

save( { attributes } ) {
const {
url,
text,
title,
backgroundColor,
textColor,
customBackgroundColor,
customTextColor,
} = attributes;

const textClass = getColorClassName( 'color', textColor );
const backgroundClass = getColorClassName( 'background-color', backgroundColor );

const buttonClasses = classnames( 'wp-block-button__link', {
'has-text-color': textColor || customTextColor,
[ textClass ]: textClass,
'has-background': backgroundColor || customBackgroundColor,
[ backgroundClass ]: backgroundClass,
} );

const buttonStyle = {
backgroundColor: backgroundClass ? undefined : customBackgroundColor,
color: textClass ? undefined : customTextColor,
};

return (
<div>
<RichText.Content
tagName="a"
className={ buttonClasses }
href={ url }
title={ title }
style={ buttonStyle }
value={ text }
/>
</div>
);
},

save,
deprecated: [ {
attributes: {
...pick( blockAttributes, [ 'url', 'title', 'text' ] ),
Expand Down
52 changes: 52 additions & 0 deletions packages/block-library/src/button/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* External dependencies
*/
import classnames from 'classnames';

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

export default function save( { attributes } ) {
const {
url,
text,
title,
backgroundColor,
textColor,
customBackgroundColor,
customTextColor,
} = attributes;

const textClass = getColorClassName( 'color', textColor );
const backgroundClass = getColorClassName( 'background-color', backgroundColor );

const buttonClasses = classnames( 'wp-block-button__link', {
'has-text-color': textColor || customTextColor,
[ textClass ]: textClass,
'has-background': backgroundColor || customBackgroundColor,
[ backgroundClass ]: backgroundClass,
} );

const buttonStyle = {
backgroundColor: backgroundClass ? undefined : customBackgroundColor,
color: textClass ? undefined : customTextColor,
};

return (
<div>
<RichText.Content
tagName="a"
className={ buttonClasses }
href={ url }
title={ title }
style={ buttonStyle }
value={ text }
/>
</div>
);
}
6 changes: 0 additions & 6 deletions packages/block-library/src/calendar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,12 @@ export const name = 'core/calendar';

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

description: __( 'A calendar of your site’s posts.' ),

icon: 'calendar',

category: 'widgets',

keywords: [ __( 'posts' ), __( 'archive' ) ],

supports: {
align: true,
},

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

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

description: __( 'Display a list of all categories.' ),

icon,

category: 'widgets',

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

edit,
};
Loading