-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from all commits
Commits
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 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,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" | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -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 ) { | ||
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, | ||
}; |
This file contains 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,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> | ||
); | ||
} |
This file contains 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,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; |
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.
JSON format doesn't allow using functions so it had to be duplicated 3 times.