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

Table Block: Fixed width table cells on by default #49672

Merged
merged 9 commits into from
Mar 21, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
2 changes: 1 addition & 1 deletion packages/block-library/src/table/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"attributes": {
"hasFixedLayout": {
"type": "boolean",
"default": false
"default": true
},
"caption": {
"type": "rich-text",
Expand Down
297 changes: 296 additions & 1 deletion packages/block-library/src/table/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
useBlockProps,
__experimentalGetBorderClassesAndStyles as getBorderClassesAndStyles,
__experimentalGetColorClassesAndStyles as getColorClassesAndStyles,
__experimentalGetElementClassName,
} from '@wordpress/block-editor';

// As the previous arbitrary colors won't match theme color palettes, the hex
Expand All @@ -24,6 +25,300 @@ const oldColors = {
'subtle-pale-pink': '#fcf0ef',
};

// Fixed width table cells on by default.
const v4 = {
attributes: {
hasFixedLayout: {
type: 'boolean',
default: false,
},
caption: {
type: 'rich-text',
source: 'rich-text',
selector: 'figcaption',
},
head: {
type: 'array',
default: [],
source: 'query',
selector: 'thead tr',
query: {
aaronrobertshaw marked this conversation as resolved.
Show resolved Hide resolved
cells: {
type: 'array',
default: [],
source: 'query',
selector: 'td,th',
query: {
content: {
type: 'rich-text',
source: 'rich-text',
},
tag: {
type: 'string',
default: 'td',
source: 'tag',
},
scope: {
type: 'string',
source: 'attribute',
attribute: 'scope',
},
align: {
type: 'string',
source: 'attribute',
attribute: 'data-align',
},
colspan: {
type: 'string',
source: 'attribute',
attribute: 'colspan',
},
rowspan: {
type: 'string',
source: 'attribute',
attribute: 'rowspan',
},
},
},
},
},
body: {
type: 'array',
default: [],
source: 'query',
selector: 'tbody tr',
query: {
cells: {
type: 'array',
default: [],
source: 'query',
selector: 'td,th',
query: {
content: {
type: 'rich-text',
source: 'rich-text',
},
tag: {
type: 'string',
default: 'td',
source: 'tag',
},
scope: {
type: 'string',
source: 'attribute',
attribute: 'scope',
},
align: {
type: 'string',
source: 'attribute',
attribute: 'data-align',
},
colspan: {
type: 'string',
source: 'attribute',
attribute: 'colspan',
},
rowspan: {
type: 'string',
source: 'attribute',
attribute: 'rowspan',
},
},
},
},
},
foot: {
type: 'array',
default: [],
source: 'query',
selector: 'tfoot tr',
query: {
cells: {
type: 'array',
default: [],
source: 'query',
selector: 'td,th',
query: {
content: {
type: 'rich-text',
source: 'rich-text',
},
tag: {
type: 'string',
default: 'td',
source: 'tag',
},
scope: {
type: 'string',
source: 'attribute',
attribute: 'scope',
},
align: {
type: 'string',
source: 'attribute',
attribute: 'data-align',
},
colspan: {
type: 'string',
source: 'attribute',
attribute: 'colspan',
},
rowspan: {
type: 'string',
source: 'attribute',
attribute: 'rowspan',
},
},
},
},
},
},
supports: {
anchor: true,
align: true,
color: {
__experimentalSkipSerialization: true,
gradients: true,
__experimentalDefaultControls: {
background: true,
text: true,
},
},
spacing: {
margin: true,
padding: true,
__experimentalDefaultControls: {
margin: false,
padding: false,
},
},
typography: {
fontSize: true,
lineHeight: true,
__experimentalFontFamily: true,
__experimentalFontStyle: true,
__experimentalFontWeight: true,
__experimentalLetterSpacing: true,
__experimentalTextTransform: true,
__experimentalTextDecoration: true,
__experimentalDefaultControls: {
fontSize: true,
},
},
__experimentalBorder: {
__experimentalSkipSerialization: true,
color: true,
style: true,
width: true,
__experimentalDefaultControls: {
color: true,
style: true,
width: true,
},
},
__experimentalSelector: '.wp-block-table > table',
interactivity: {
clientNavigation: true,
},
},
save( { attributes } ) {
const { hasFixedLayout, head, body, foot, caption } = attributes;
const isEmpty = ! head.length && ! body.length && ! foot.length;

if ( isEmpty ) {
return null;
}

const colorProps = getColorClassesAndStyles( attributes );
const borderProps = getBorderClassesAndStyles( attributes );

const classes = classnames(
colorProps.className,
borderProps.className,
{
'has-fixed-layout': hasFixedLayout,
}
);

const hasCaption = ! RichText.isEmpty( caption );

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,
scope,
align,
colspan,
rowspan,
},
cellIndex
) => {
const cellClasses = classnames( {
[ `has-text-align-${ align }` ]: align,
} );

return (
<RichText.Content
className={
cellClasses
? cellClasses
: undefined
}
data-align={ align }
tagName={ tag }
value={ content }
key={ cellIndex }
scope={
tag === 'th' ? scope : undefined
}
colSpan={ colspan }
rowSpan={ rowspan }
/>
);
}
) }
</tr>
) ) }
</Tag>
);
};

return (
<figure { ...useBlockProps.save() }>
<table
className={ classes === '' ? undefined : classes }
style={ { ...colorProps.style, ...borderProps.style } }
>
<Section type="head" rows={ head } />
<Section type="body" rows={ body } />
<Section type="foot" rows={ foot } />
</table>
{ hasCaption && (
<RichText.Content
tagName="figcaption"
value={ caption }
className={ __experimentalGetElementClassName(
'caption'
) }
/>
) }
</figure>
);
},
};

// In #41140 support was added to global styles for caption elements which
// added a `wp-element-caption` classname to the embed figcaption element.
const v3 = {
Expand Down Expand Up @@ -665,4 +960,4 @@ const v1 = {
*
* See block-deprecation.md
*/
export default [ v3, v2, v1 ];
export default [ v4, v3, v2, v1 ];
4 changes: 2 additions & 2 deletions packages/blocks/src/api/raw-handling/test/paste-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe( 'pasteHandler', () => {

delete result.attributes.caption;
expect( result.attributes ).toEqual( {
hasFixedLayout: false,
hasFixedLayout: true,
head: [
{
cells: [
Expand Down Expand Up @@ -117,7 +117,7 @@ describe( 'pasteHandler', () => {

delete result.attributes.caption;
expect( result.attributes ).toEqual( {
hasFixedLayout: false,
hasFixedLayout: true,
head: [
{
cells: [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:table -->
<figure class="wp-block-table"><table><tbody><tr><td></td><td></td></tr><tr><td></td><td></td></tr></tbody></table><figcaption class="wp-element-caption">Caption!</figcaption></figure>
<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><td></td><td></td></tr><tr><td></td><td></td></tr></tbody></table><figcaption class="wp-element-caption">Caption!</figcaption></figure>
<!-- /wp:table -->
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:table -->
<figure class="wp-block-table"><table><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td></tr></tbody><tfoot><tr><td></td><td></td><td></td></tr></tfoot></table></figure>
<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th></th><th></th><th></th></tr></thead><tbody><tr><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td></tr></tbody><tfoot><tr><td></td><td></td><td></td></tr></tfoot></table></figure>
<!-- /wp:table -->
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:table -->
<figure class="wp-block-table"><table><thead><tr><th></th><th></th></tr></thead><tbody><tr><td></td><td></td></tr><tr><td></td><td></td></tr></tbody><tfoot><tr><td></td><td></td></tr></tfoot></table></figure>
<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th></th><th></th></tr></thead><tbody><tr><td></td><td></td></tr><tr><td></td><td></td></tr></tbody><tfoot><tr><td></td><td></td></tr></tfoot></table></figure>
<!-- /wp:table -->
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:table {"hasFixedLayout":true} -->
<!-- wp:table -->
<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><td><br><br><br><br></td><td>Second cell.</td></tr><tr><td></td><td></td></tr></tbody></table></figure>
<!-- /wp:table -->
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:table -->
<figure class="wp-block-table"><table><tbody><tr><td>None</td><td class="has-text-align-left" data-align="left">To the left</td><td class="has-text-align-center" data-align="center">Centered</td><td class="has-text-align-right" data-align="right">Right aligned</td></tr><tr><td></td><td class="has-text-align-left" data-align="left"></td><td class="has-text-align-center" data-align="center"></td><td class="has-text-align-right" data-align="right"></td></tr></tbody></table></figure>
<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><td>None</td><td class="has-text-align-left" data-align="left">To the left</td><td class="has-text-align-center" data-align="center">Centered</td><td class="has-text-align-right" data-align="right">Right aligned</td></tr><tr><td></td><td class="has-text-align-left" data-align="left"></td><td class="has-text-align-center" data-align="center"></td><td class="has-text-align-right" data-align="right"></td></tr></tbody></table></figure>
<!-- /wp:table -->
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:table -->
<figure class="wp-block-table"><table><thead><tr><th>header</th><th></th></tr></thead><tbody><tr><td>body</td><td></td></tr><tr><td></td><td></td></tr></tbody><tfoot><tr><td>footer</td><td></td></tr></tfoot></table></figure>
<figure class="wp-block-table"><table class="has-fixed-layout"><thead><tr><th>header</th><th></th></tr></thead><tbody><tr><td>body</td><td></td></tr><tr><td></td><td></td></tr></tbody><tfoot><tr><td>footer</td><td></td></tr></tfoot></table></figure>
<!-- /wp:table -->
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:table -->
<figure class="wp-block-table"><table><tbody><tr><td>body</td><td></td></tr><tr><td></td><td></td></tr></tbody></table></figure>
<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><td>body</td><td></td></tr><tr><td></td><td></td></tr></tbody></table></figure>
<!-- /wp:table -->
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:table -->
<figure class="wp-block-table"><table><tbody><tr><td>This</td><td>is</td></tr><tr><td>table</td><td>block</td></tr></tbody></table></figure>
<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><td>This</td><td>is</td></tr><tr><td>table</td><td>block</td></tr></tbody></table></figure>
<!-- /wp:table -->
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:table -->
<figure class="wp-block-table"><table><tbody><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr></tbody></table></figure>
<figure class="wp-block-table"><table class="has-fixed-layout"><tbody><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr></tbody></table></figure>
<!-- /wp:table -->
Loading
Loading