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 Tabel block to use block.json metadata #14863

Merged
merged 1 commit into from
Apr 9, 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
88 changes: 88 additions & 0 deletions packages/block-library/src/table/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
{
"name": "core/table",
"category": "formatting",
"attributes": {
"hasFixedLayout": {
"type": "boolean",
"default": false
},
"backgroundColor": {
"type": "string"
},
"head": {
"type": "array",
"default": [],
"source": "query",
"selector": "thead tr",
"query": {
"cells": {
"type": "array",
"default": [],
"source": "query",
"selector": "td,th",
"query": {
"content": {
"type": "string",
"source": "html"
},
"tag": {
"type": "string",
"default": "td",
"source": "tag"
}
}
}
}
},
"body": {
"type": "array",
"default": [],
"source": "query",
"selector": "tbody tr",
"query": {
"cells": {
"type": "array",
"default": [],
"source": "query",
"selector": "td,th",
"query": {
"content": {
"type": "string",
"source": "html"
},
"tag": {
"type": "string",
"default": "td",
"source": "tag"
}
}
}
}
},
"foot": {
"type": "array",
"default": [],
"source": "query",
"selector": "tfoot tr",
"query": {
"cells": {
"type": "array",
"default": [],
"source": "query",
"selector": "td,th",
"query": {
"content": {
"type": "string",
"source": "html"
},
"tag": {
"type": "string",
"default": "td",
"source": "tag"
}
}
}
}
}
}
}
158 changes: 7 additions & 151 deletions packages/block-library/src/table/index.js
Original file line number Diff line number Diff line change
@@ -1,177 +1,33 @@
/**
* External dependencies
*/
import classnames from 'classnames';

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

const tableContentPasteSchema = {
tr: {
allowEmpty: true,
children: {
th: {
allowEmpty: true,
children: getPhrasingContentSchema(),
},
td: {
allowEmpty: true,
children: getPhrasingContentSchema(),
},
},
},
};
const { name } = metadata;

const tablePasteSchema = {
table: {
children: {
thead: {
allowEmpty: true,
children: tableContentPasteSchema,
},
tfoot: {
allowEmpty: true,
children: tableContentPasteSchema,
},
tbody: {
allowEmpty: true,
children: tableContentPasteSchema,
},
},
},
};

function getTableSectionAttributeSchema( section ) {
Copy link
Member Author

Choose a reason for hiding this comment

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

JSON format doesn't allow using functions so it had to be duplicated 3 times.

return {
type: 'array',
default: [],
source: 'query',
selector: `t${ section } tr`,
query: {
cells: {
type: 'array',
default: [],
source: 'query',
selector: 'td,th',
query: {
content: {
type: 'string',
source: 'html',
},
tag: {
type: 'string',
default: 'td',
source: 'tag',
},
},
},
},
};
}

export const name = 'core/table';
export { metadata, name };

export const settings = {
title: __( 'Table' ),
description: __( 'Insert a table — perfect for sharing charts and data.' ),
icon,
category: 'formatting',

attributes: {
hasFixedLayout: {
type: 'boolean',
default: false,
},
backgroundColor: {
type: 'string',
},
head: getTableSectionAttributeSchema( 'head' ),
body: getTableSectionAttributeSchema( 'body' ),
foot: getTableSectionAttributeSchema( 'foot' ),
},

styles: [
{ name: 'regular', label: _x( 'Default', 'block style' ), isDefault: true },
{ name: 'stripes', label: __( 'Stripes' ) },
],

supports: {
align: true,
},

transforms: {
from: [
{
type: 'raw',
selector: 'table',
schema: tablePasteSchema,
},
],
},

transforms,
edit,

save( { attributes } ) {
const {
hasFixedLayout,
head,
body,
foot,
backgroundColor,
} = attributes;
const isEmpty = ! head.length && ! body.length && ! foot.length;

if ( isEmpty ) {
return null;
}

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

const classes = classnames( backgroundClass, {
'has-fixed-layout': hasFixedLayout,
'has-background': !! backgroundClass,
} );

const Section = ( { type, rows } ) => {
if ( ! rows.length ) {
return null;
}

const Tag = `t${ type }`;

return (
<Tag>
{ rows.map( ( { cells }, rowIndex ) => (
<tr key={ rowIndex }>
{ cells.map( ( { content, tag }, cellIndex ) =>
<RichText.Content
tagName={ tag }
value={ content }
key={ cellIndex }
/>
) }
</tr>
) ) }
</Tag>
);
};

return (
<table className={ classes }>
<Section type="head" rows={ head } />
<Section type="body" rows={ body } />
<Section type="foot" rows={ foot } />
</table>
);
},
save,
};
63 changes: 63 additions & 0 deletions packages/block-library/src/table/save.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* External dependencies
*/
import classnames from 'classnames';

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

export default function save( { attributes } ) {
const {
hasFixedLayout,
head,
body,
foot,
backgroundColor,
} = attributes;
const isEmpty = ! head.length && ! body.length && ! foot.length;

if ( isEmpty ) {
return null;
}

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

const classes = classnames( backgroundClass, {
'has-fixed-layout': hasFixedLayout,
'has-background': !! backgroundClass,
} );

const Section = ( { type, rows } ) => {
if ( ! rows.length ) {
return null;
}

const Tag = `t${ type }`;

return (
<Tag>
{ rows.map( ( { cells }, rowIndex ) => (
<tr key={ rowIndex }>
{ cells.map( ( { content, tag }, cellIndex ) =>
<RichText.Content
tagName={ tag }
value={ content }
key={ cellIndex }
/>
) }
</tr>
) ) }
</Tag>
);
};

return (
<table className={ classes }>
<Section type="head" rows={ head } />
<Section type="body" rows={ body } />
<Section type="foot" rows={ foot } />
</table>
);
}
51 changes: 51 additions & 0 deletions packages/block-library/src/table/transforms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* WordPress dependencies
*/
import { getPhrasingContentSchema } from '@wordpress/blocks';

const tableContentPasteSchema = {
tr: {
allowEmpty: true,
children: {
th: {
allowEmpty: true,
children: getPhrasingContentSchema(),
},
td: {
allowEmpty: true,
children: getPhrasingContentSchema(),
},
},
},
};

const tablePasteSchema = {
table: {
children: {
thead: {
allowEmpty: true,
children: tableContentPasteSchema,
},
tfoot: {
allowEmpty: true,
children: tableContentPasteSchema,
},
tbody: {
allowEmpty: true,
children: tableContentPasteSchema,
},
},
},
};

const transforms = {
from: [
{
type: 'raw',
selector: 'table',
schema: tablePasteSchema,
},
],
};

export default transforms;