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

Improve block filters #48420

Closed
wants to merge 3 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
139 changes: 75 additions & 64 deletions packages/block-editor/src/hooks/align.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,55 @@ export function addAttribute( settings ) {
return settings;
}

function AlignControls( props ) {
const { name: blockName } = props;
// Compute the block valid alignments by taking into account,
// if the theme supports wide alignments or not and the layout's
// availble alignments. We do that for conditionally rendering
// Slot.
const blockAllowedAlignments = getValidAlignments(
getBlockSupport( blockName, 'align' ),
hasBlockSupport( blockName, 'alignWide', true )
);

const validAlignments = useAvailableAlignments(
blockAllowedAlignments
).map( ( { name } ) => name );
const isContentLocked = useSelect(
( select ) => {
return select( blockEditorStore ).__unstableGetContentLockingParent(
props.clientId
);
},
[ props.clientId ]
);

if ( ! validAlignments.length || isContentLocked ) {
return null;
}

const updateAlignment = ( nextAlign ) => {
if ( ! nextAlign ) {
const blockType = getBlockType( props.name );
const blockDefaultAlign = blockType?.attributes?.align?.default;
if ( blockDefaultAlign ) {
nextAlign = '';
}
}
props.setAttributes( { align: nextAlign } );
};

return (
<BlockControls group="block" __experimentalShareWithChildBlocks>
<BlockAlignmentControl
value={ props.attributes.align }
onChange={ updateAlignment }
controls={ validAlignments }
/>
</BlockControls>
);
}

/**
* Override the default edit UI to include new toolbar controls for block
* alignment, if block defines support.
Expand All @@ -119,59 +168,33 @@ export function addAttribute( settings ) {
*/
export const withToolbarControls = createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
const blockEdit = <BlockEdit key="edit" { ...props } />;
const { name: blockName } = props;
// Compute the block valid alignments by taking into account,
// if the theme supports wide alignments or not and the layout's
// availble alignments. We do that for conditionally rendering
// Slot.
const blockAllowedAlignments = getValidAlignments(
getBlockSupport( blockName, 'align' ),
hasBlockSupport( blockName, 'alignWide', true )
);

const validAlignments = useAvailableAlignments(
blockAllowedAlignments
).map( ( { name } ) => name );
const isContentLocked = useSelect(
( select ) => {
return select(
blockEditorStore
).__unstableGetContentLockingParent( props.clientId );
},
[ props.clientId ]
);
if ( ! validAlignments.length || isContentLocked ) {
return blockEdit;
}

const updateAlignment = ( nextAlign ) => {
if ( ! nextAlign ) {
const blockType = getBlockType( props.name );
const blockDefaultAlign = blockType?.attributes?.align?.default;
if ( blockDefaultAlign ) {
nextAlign = '';
}
}
props.setAttributes( { align: nextAlign } );
};

return (
<>
<BlockControls group="block" __experimentalShareWithChildBlocks>
<BlockAlignmentControl
value={ props.attributes.align }
onChange={ updateAlignment }
controls={ validAlignments }
/>
</BlockControls>
{ blockEdit }
{ props.isSelected && <AlignControls { ...props } /> }
<BlockEdit { ...props } />
</>
);
},
'withToolbarControls'
);

function BlockListBlockWithAlign( { BlockListBlock, ...props } ) {
const { name, attributes } = props;
const { align } = attributes;
const blockAllowedAlignments = getValidAlignments(
getBlockSupport( name, 'align' ),
hasBlockSupport( name, 'alignWide', true )
);
const validAlignments = useAvailableAlignments( blockAllowedAlignments );

let wrapperProps = props.wrapperProps;
if ( validAlignments.some( ( alignment ) => alignment.name === align ) ) {
wrapperProps = { ...wrapperProps, 'data-align': align };
}

return <BlockListBlock { ...props } wrapperProps={ wrapperProps } />;
}

/**
* Override the default block element to add alignment wrapper props.
*
Expand All @@ -181,30 +204,18 @@ export const withToolbarControls = createHigherOrderComponent(
*/
export const withDataAlign = createHigherOrderComponent(
( BlockListBlock ) => ( props ) => {
const { name, attributes } = props;
const { align } = attributes;
const blockAllowedAlignments = getValidAlignments(
getBlockSupport( name, 'align' ),
hasBlockSupport( name, 'alignWide', true )
);
const validAlignments = useAvailableAlignments(
blockAllowedAlignments
);

// If an alignment is not assigned, there's no need to go through the
// effort to validate or assign its value.
if ( align === undefined ) {
if ( props.attributes.align === undefined ) {
Copy link
Contributor

Choose a reason for hiding this comment

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

The problem with the change here is that the React tree of elements is different, meaning when the value of the "align" attribute changes, it will cause a "remount" of the whole block which we know can create unexpected bugs.

return <BlockListBlock { ...props } />;
}

let wrapperProps = props.wrapperProps;
if (
validAlignments.some( ( alignment ) => alignment.name === align )
) {
wrapperProps = { ...wrapperProps, 'data-align': align };
}

return <BlockListBlock { ...props } wrapperProps={ wrapperProps } />;
return (
<BlockListBlockWithAlign
BlockListBlock={ BlockListBlock }
{ ...props }
/>
);
}
);

Expand Down
38 changes: 22 additions & 16 deletions packages/block-editor/src/hooks/duotone.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,27 @@ function addDuotoneAttributes( settings ) {
return settings;
}

function DuoToneControls( props ) {
const hasDuotoneSupport = hasBlockSupport(
props.name,
'color.__experimentalDuotone'
);
const isContentLocked = useSelect(
( select ) => {
return select( blockEditorStore ).__unstableGetContentLockingParent(
props.clientId
);
},
[ props.clientId ]
);

if ( ! hasDuotoneSupport || isContentLocked ) {
return null;
}

return <DuotonePanel { ...props } />;
}

/**
* Override the default edit UI to include toolbar controls for duotone if the
* block supports duotone.
Expand All @@ -189,28 +210,13 @@ function addDuotoneAttributes( settings ) {
*/
const withDuotoneControls = createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
const hasDuotoneSupport = hasBlockSupport(
props.name,
'color.__experimentalDuotone'
);
const isContentLocked = useSelect(
( select ) => {
return select(
blockEditorStore
).__unstableGetContentLockingParent( props.clientId );
},
[ props.clientId ]
);

// CAUTION: code added before this line will be executed
// for all blocks, not just those that support duotone. Code added
// above this line should be carefully evaluated for its impact on
// performance.
return (
<>
{ hasDuotoneSupport && ! isContentLocked && (
<DuotonePanel { ...props } />
) }
{ props.isSelected && <DuoToneControls { ...props } /> }
<BlockEdit { ...props } />
</>
);
Expand Down
Loading