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

EX-1142 fix access of null offset error and add validation on columns of last row #20

Merged
merged 6 commits into from
Jul 10, 2024
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
2 changes: 1 addition & 1 deletion dist/editor/blocks.asset.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-primitives'), 'version' => '7369bf1f984476aa1081');
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-data', 'wp-element', 'wp-hooks', 'wp-i18n', 'wp-primitives'), 'version' => 'a9d2811d6f17d28c57df');
2 changes: 1 addition & 1 deletion dist/editor/blocks.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/editor/blocks.js.map

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions inc/blocks/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,23 @@ function get_border_styles( array $attributes = [] ): array {
'inline_styles' => $inline_styles,
];
}

/**
* Get block wrapper attributes.
*
* @param mixed[] $attributes The block attributes.
*
* @return string
*/
function get_block_wrapper_attributes( array $attributes = [] ): string {
if ( ! is_array( $attributes ) ) {
return '';
}

$normalized_attributes = [];
foreach ( $attributes as $key => $value ) {
$normalized_attributes[] = $key . '="' . esc_attr( $value ) . '"';
}

return implode( ' ', $normalized_attributes );
}
1 change: 1 addition & 0 deletions inc/blocks/table-column.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Travelopia\Blocks\TableColumn;

use function Travelopia\Blocks\Helpers\get_block_wrapper_attributes;
use function Travelopia\Blocks\Helpers\get_border_styles;
use function Travelopia\Blocks\Helpers\get_css_classes;
use function Travelopia\Blocks\Helpers\get_css_styles;
Expand Down
1 change: 1 addition & 0 deletions inc/blocks/table-row-container.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Travelopia\Blocks\TableRowContainer;

use function Travelopia\Blocks\Helpers\get_block_wrapper_attributes;
use function Travelopia\Blocks\Helpers\get_css_classes;

const BLOCK_NAME = 'travelopia/table-row-container';
Expand Down
1 change: 1 addition & 0 deletions inc/blocks/table-row.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Travelopia\Blocks\TableRow;

use function Travelopia\Blocks\Helpers\get_block_wrapper_attributes;
use function Travelopia\Blocks\Helpers\get_border_styles;
use function Travelopia\Blocks\Helpers\get_css_classes;
use function Travelopia\Blocks\Helpers\get_css_styles;
Expand Down
1 change: 1 addition & 0 deletions inc/blocks/table.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Travelopia\Blocks\Table;

use function Travelopia\Blocks\Helpers\get_block_wrapper_attributes;
use function Travelopia\Blocks\Helpers\get_css_classes;
use function Travelopia\Blocks\Helpers\get_css_styles;

Expand Down
33 changes: 25 additions & 8 deletions src/editor/blocks/table-column/toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export default function Toolbar( {
} = dispatch( 'core/block-editor' );

const [ maximumColumnsInCurrentRow, setMaximumColumnsInCurrentRow ] = useState( 0 );
const [ maximumRowsInCurrentColumn, setMaximumRowsInCurrentColumn ] = useState( 0 );

const rowContainerBlockType = useMemo( () => getBlock( rowContainerId )?.attributes?.type, [ rowContainerId, getBlock ] );

Expand All @@ -97,17 +98,33 @@ export default function Toolbar( {
return;
}

// Traverse rows.
tableBlock.innerBlocks.some( ( rowBlock, index ): boolean => {
// Get current row.
if ( rowBlock.name !== rowBlockName || index + 1 !== tableRow || ! rowBlock.innerBlocks.length ) {
// Traverse table.
tableBlock.innerBlocks.some( ( rowContainerBlock ): boolean => {
if ( rowContainerBlock.name !== rowContainerBlockName || ! rowContainerBlock.innerBlocks.length ) {
return false;
}

// Set maximum columns in current row.
setMaximumColumnsInCurrentRow( rowBlock.innerBlocks.length );
let maxRows = 0;
rowContainerBlock.innerBlocks.forEach( ( rowBlock, rowIndex ) => {
if ( rowBlock.name !== rowBlockName || ! rowBlock.innerBlocks.length ) {
return;
}

// Set maximum columns in current row.
if ( rowIndex + 1 === tableRow ) {
setMaximumColumnsInCurrentRow( rowBlock.innerBlocks.length );
}

rowBlock.innerBlocks.forEach( ( columnBlock, columnIndex ) => {
if ( columnBlock.name !== columnBlockName || columnIndex + 1 !== tableColumn ) {
return;
}

maxRows++;
} );
} );

// Short-circuit loop.
setMaximumRowsInCurrentColumn( maxRows );
return true;
} );
}, [ tableRow, tableColumn, getBlock, tableId ] );
Expand Down Expand Up @@ -649,7 +666,7 @@ export default function Toolbar( {
{
icon: arrowDown,
title: __( 'Merge column down', 'tp' ),
isDisabled: ( rowContainerBlockType === 'tfoot' || rowContainerBlockType === 'thead' ),
isDisabled: ( tableRow === maximumRowsInCurrentColumn || rowContainerBlockType === 'tfoot' || rowContainerBlockType === 'thead' ),
onClick: onMergeColumnDown,
},
] as DropdownOption[];
Expand Down