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

Remove “Columns” slider from Columns block #64722

Closed
wants to merge 2 commits into from
Closed
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
177 changes: 13 additions & 164 deletions packages/block-library/src/columns/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ import clsx from 'clsx';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
Notice,
PanelBody,
RangeControl,
ToggleControl,
} from '@wordpress/components';
import { PanelBody, ToggleControl } from '@wordpress/components';

import {
InspectorControls,
Expand All @@ -25,167 +20,14 @@ import {
} from '@wordpress/block-editor';
import { useDispatch, useSelect, useRegistry } from '@wordpress/data';
import {
createBlock,
createBlocksFromInnerBlocksTemplate,
store as blocksStore,
} from '@wordpress/blocks';

/**
* Internal dependencies
*/
import {
hasExplicitPercentColumnWidths,
getMappedColumnWidths,
getRedistributedColumnWidths,
toWidthPrecision,
} from './utils';

const DEFAULT_BLOCK = {
name: 'core/column',
};

function ColumnInspectorControls( {
clientId,
setAttributes,
isStackedOnMobile,
} ) {
const { count, canInsertColumnBlock, minCount } = useSelect(
( select ) => {
const {
canInsertBlockType,
canRemoveBlock,
getBlocks,
getBlockCount,
} = select( blockEditorStore );
const innerBlocks = getBlocks( clientId );

// Get the indexes of columns for which removal is prevented.
// The highest index will be used to determine the minimum column count.
const preventRemovalBlockIndexes = innerBlocks.reduce(
( acc, block, index ) => {
if ( ! canRemoveBlock( block.clientId ) ) {
acc.push( index );
}
return acc;
},
[]
);

return {
count: getBlockCount( clientId ),
canInsertColumnBlock: canInsertBlockType(
'core/column',
clientId
),
minCount: Math.max( ...preventRemovalBlockIndexes ) + 1,
};
},
[ clientId ]
);
const { getBlocks } = useSelect( blockEditorStore );
const { replaceInnerBlocks } = useDispatch( blockEditorStore );

/**
* Updates the column count, including necessary revisions to child Column
* blocks to grant required or redistribute available space.
*
* @param {number} previousColumns Previous column count.
* @param {number} newColumns New column count.
*/
function updateColumns( previousColumns, newColumns ) {
let innerBlocks = getBlocks( clientId );
const hasExplicitWidths = hasExplicitPercentColumnWidths( innerBlocks );

// Redistribute available width for existing inner blocks.
const isAddingColumn = newColumns > previousColumns;

if ( isAddingColumn && hasExplicitWidths ) {
// If adding a new column, assign width to the new column equal to
// as if it were `1 / columns` of the total available space.
const newColumnWidth = toWidthPrecision( 100 / newColumns );
const newlyAddedColumns = newColumns - previousColumns;

// Redistribute in consideration of pending block insertion as
// constraining the available working width.
const widths = getRedistributedColumnWidths(
innerBlocks,
100 - newColumnWidth * newlyAddedColumns
);

innerBlocks = [
...getMappedColumnWidths( innerBlocks, widths ),
...Array.from( {
length: newlyAddedColumns,
} ).map( () => {
return createBlock( 'core/column', {
width: `${ newColumnWidth }%`,
} );
} ),
];
} else if ( isAddingColumn ) {
innerBlocks = [
...innerBlocks,
...Array.from( {
length: newColumns - previousColumns,
} ).map( () => {
return createBlock( 'core/column' );
} ),
];
} else if ( newColumns < previousColumns ) {
// The removed column will be the last of the inner blocks.
innerBlocks = innerBlocks.slice(
0,
-( previousColumns - newColumns )
);
if ( hasExplicitWidths ) {
// Redistribute as if block is already removed.
const widths = getRedistributedColumnWidths( innerBlocks, 100 );

innerBlocks = getMappedColumnWidths( innerBlocks, widths );
}
}

replaceInnerBlocks( clientId, innerBlocks );
}

return (
<PanelBody title={ __( 'Settings' ) }>
{ canInsertColumnBlock && (
<>
<RangeControl
__nextHasNoMarginBottom
__next40pxDefaultSize
label={ __( 'Columns' ) }
value={ count }
onChange={ ( value ) =>
updateColumns( count, Math.max( minCount, value ) )
}
min={ Math.max( 1, minCount ) }
max={ Math.max( 6, count ) }
/>
{ count > 6 && (
<Notice status="warning" isDismissible={ false }>
{ __(
'This column count exceeds the recommended amount and may cause visual breakage.'
) }
</Notice>
) }
</>
) }
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Stack on mobile' ) }
checked={ isStackedOnMobile }
onChange={ () =>
setAttributes( {
isStackedOnMobile: ! isStackedOnMobile,
} )
}
/>
</PanelBody>
);
}

function ColumnsEditContainer( { attributes, setAttributes, clientId } ) {
const { isStackedOnMobile, verticalAlignment, templateLock } = attributes;
const registry = useRegistry();
Expand Down Expand Up @@ -237,11 +79,18 @@ function ColumnsEditContainer( { attributes, setAttributes, clientId } ) {
/>
</BlockControls>
<InspectorControls>
<ColumnInspectorControls
clientId={ clientId }
setAttributes={ setAttributes }
isStackedOnMobile={ isStackedOnMobile }
/>
<PanelBody title={ __( 'Settings' ) }>
<ToggleControl
__nextHasNoMarginBottom
label={ __( 'Stack on mobile' ) }
checked={ isStackedOnMobile }
onChange={ () =>
setAttributes( {
isStackedOnMobile: ! isStackedOnMobile,
} )
}
/>
</PanelBody>
</InspectorControls>
<div { ...innerBlocksProps } />
</>
Expand Down
18 changes: 0 additions & 18 deletions packages/block-library/src/columns/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { View, Dimensions } from 'react-native';
import { __, sprintf } from '@wordpress/i18n';
import {
PanelBody,
RangeControl,
FooterMessageControl,
UnitControl,
getValueAndUnit,
Expand All @@ -36,7 +35,6 @@ import {
} from '@wordpress/element';
import { useResizeObserver } from '@wordpress/compose';
import { createBlock } from '@wordpress/blocks';
import { columns } from '@wordpress/icons';
/**
* Internal dependencies
*/
Expand Down Expand Up @@ -214,28 +212,12 @@ function ColumnsEditContainer( {
} );
}, [ editorSidebarOpened, isSelected, innerWidths ] );

const onChangeColumnsNum = useCallback(
( value ) => {
updateColumns( columnCount, value );
},
[ columnCount ]
);

return (
<>
{ isSelected && (
<>
<InspectorControls>
<PanelBody title={ __( 'Columns Settings' ) }>
<RangeControl
label={ __( 'Number of columns' ) }
icon={ columns }
value={ columnCount }
onChange={ onChangeColumnsNum }
min={ MIN_COLUMNS_NUM }
max={ columnCount + 1 }
type="stepper"
/>
{ getColumnsSliders }
</PanelBody>
<PanelBody>
Expand Down
109 changes: 0 additions & 109 deletions test/e2e/specs/editor/blocks/columns.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,46 +46,6 @@ test.describe( 'Columns', () => {
await expect( inserterOptions ).toHaveText( 'Column' );
} );

test( 'prevent the removal of locked column block from the column count change UI', async ( {
page,
editor,
pageUtils,
} ) => {
// Open Columns
await editor.insertBlock( { name: 'core/columns' } );
await editor.canvas
.locator( '[aria-label="Three columns; equal split"]' )
.click();

// Lock last column block
await editor.selectBlocks(
editor.canvas.locator(
'role=document[name="Block: Column (3 of 3)"i]'
)
);
await editor.clickBlockToolbarButton( 'Options' );
await page.click( 'role=menuitem[name="Lock"i]' );
await page.locator( 'role=checkbox[name="Prevent removal"i]' ).check();
await page.click( 'role=button[name="Apply"i]' );

// Select columns block
await editor.selectBlocks(
editor.canvas.locator( 'role=document[name="Block: Columns"i]' )
);
await editor.openDocumentSettingsSidebar();

const columnsChangeInput = page.locator(
'role=spinbutton[name="Columns"i]'
);

// The min attribute should take into account locked columns
await expect( columnsChangeInput ).toHaveAttribute( 'min', '3' );

// Changing the number of columns should take into account locked columns
await page.fill( 'role=spinbutton[name="Columns"i]', '1' );
await pageUtils.pressKeys( 'Tab' );
await expect( columnsChangeInput ).toHaveValue( '3' );
} );
test( 'Ungroup properly', async ( { editor } ) => {
await editor.insertBlock( {
name: 'core/columns',
Expand Down Expand Up @@ -175,75 +135,6 @@ test.describe( 'Columns', () => {
] );
} );

test.describe( 'should update the column widths correctly', () => {
const initialColumnWidths = [ '10%', '20%', '30%', '40%' ];

const expected = [
{
newColumnCount: 2,
newColumnWidths: [ '33.33%', '66.67%' ],
},
{
newColumnCount: 3,
newColumnWidths: [ '16.67%', '33.33%', '50%' ],
},
{
newColumnCount: 5,
newColumnWidths: [ '8%', '16%', '24%', '32%', '20%' ],
},
{
newColumnCount: 6,
newColumnWidths: [
'6.67%',
'13.33%',
'20%',
'26.66%',
'16.67%',
'16.67%',
],
},
];

expected.forEach( ( { newColumnCount, newColumnWidths } ) => {
test( `when the column count is changed to ${ newColumnCount }`, async ( {
editor,
page,
} ) => {
await editor.insertBlock( {
name: 'core/columns',
attributes: {
columns: initialColumnWidths.length,
},
innerBlocks: initialColumnWidths.map( ( width ) => ( {
name: 'core/column',
attributes: { width },
} ) ),
} );

await editor.selectBlocks(
editor.canvas.getByRole( 'document', {
name: 'Block: Columns',
} )
);
await editor.openDocumentSettingsSidebar();

await page
.getByRole( 'spinbutton', { name: 'Columns' } )
.fill( newColumnCount.toString() );

await expect( editor.getBlocks() ).resolves.toMatchObject( [
{
name: 'core/columns',
innerBlocks: newColumnWidths.map( ( width ) => ( {
name: 'core/column',
attributes: { width },
} ) ),
},
] );
} );
} );
} );

test( 'should not split in middle', async ( { editor, page } ) => {
await editor.insertBlock( {
name: 'core/columns',
Expand Down
Loading
Loading