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

Reusable block: Pluralize the message "Convert to regular blocks" depending on the number of blocks contained. #45819

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -23,7 +23,7 @@ import {
isReusableBlock,
} from '@wordpress/blocks';
import { __, sprintf } from '@wordpress/i18n';
import { withDispatch, withSelect } from '@wordpress/data';
import { withDispatch, withSelect, useSelect } from '@wordpress/data';
import { withInstanceId, compose } from '@wordpress/compose';
import { moreHorizontalMobile } from '@wordpress/icons';
import { useRef, useState } from '@wordpress/element';
Expand Down Expand Up @@ -78,6 +78,12 @@ const BlockActionsMenu = ( {
clipboardBlock &&
canInsertBlockType( clipboardBlock.name, rootClientId );

const innerBlockCount = useSelect(
( select ) =>
select( blockEditorStore ).getBlockCount( selectedBlockClientId ),
[ selectedBlockClientId ]
);

const {
actionTitle: {
backward: backwardButtonTitle,
Expand Down Expand Up @@ -187,13 +193,21 @@ const BlockActionsMenu = ( {
},
convertToRegularBlocks: {
id: 'convertToRegularBlocksOption',
label: __( 'Convert to regular blocks' ),
label:
innerBlockCount > 1
? __( 'Convert to regular blocks' )
: __( 'Convert to regular block' ),
value: 'convertToRegularBlocksOption',
onSelect: () => {
const successNotice =
innerBlockCount > 1
? /* translators: %s: name of the reusable block */
__( '%s converted to regular blocks' )
: /* translators: %s: name of the reusable block */
__( '%s converted to regular block' );
createSuccessNotice(
sprintf(
/* translators: %s: name of the reusable block */
__( '%s converted to regular blocks' ),
successNotice,
reusableBlock?.title?.raw || blockTitle
)
);
Expand Down
17 changes: 14 additions & 3 deletions packages/block-library/src/block/edit.js
Expand Up @@ -39,8 +39,15 @@ export default function ReusableBlockEdit( { attributes: { ref }, clientId } ) {
);
const isMissing = hasResolved && ! record;

const canRemove = useSelect(
( select ) => select( blockEditorStore ).canRemoveBlock( clientId ),
const { canRemove, innerBlockCount } = useSelect(
( select ) => {
const { canRemoveBlock, getBlockCount } =
select( blockEditorStore );
return {
canRemove: canRemoveBlock( clientId ),
innerBlockCount: getBlockCount( clientId ),
};
},
[ clientId ]
);

Expand Down Expand Up @@ -109,7 +116,11 @@ export default function ReusableBlockEdit( { attributes: { ref }, clientId } ) {
<ToolbarGroup>
<ToolbarButton
onClick={ () => convertBlockToStatic( clientId ) }
label={ __( 'Convert to regular blocks' ) }
label={
innerBlockCount > 1
? __( 'Convert to regular blocks' )
: __( 'Convert to regular block' )
}
icon={ ungroup }
showTooltip
/>
Expand Down
36 changes: 24 additions & 12 deletions packages/block-library/src/block/edit.native.js
Expand Up @@ -77,7 +77,7 @@ export default function ReusableBlockEdit( {
styles.spinnerDark
);

const { hasResolved, isEditing, isMissing } = useSelect(
const { hasResolved, isEditing, isMissing, innerBlockCount } = useSelect(
( select ) => {
const persistedBlock = select( coreStore ).getEntityRecord(
'postType',
Expand All @@ -88,13 +88,17 @@ export default function ReusableBlockEdit( {
'getEntityRecord',
[ 'postType', 'wp_block', ref ]
);

const { getBlockCount } = select( blockEditorStore );

return {
hasResolved: hasResolvedBlock,
isEditing:
select(
reusableBlocksStore
).__experimentalIsEditingReusableBlock( clientId ),
isMissing: hasResolvedBlock && ! persistedBlock,
innerBlockCount: getBlockCount( clientId ),
};
},
[ ref, clientId ]
Expand Down Expand Up @@ -122,13 +126,13 @@ export default function ReusableBlockEdit( {
}

const onConvertToRegularBlocks = useCallback( () => {
createSuccessNotice(
sprintf(
/* translators: %s: name of the reusable block */
__( '%s converted to regular blocks' ),
title
)
);
const successNotice =
innerBlockCount > 1
? /* translators: %s: name of the reusable block */
__( '%s converted to regular blocks' )
: /* translators: %s: name of the reusable block */
__( '%s converted to regular block' );
createSuccessNotice( sprintf( successNotice, title ) );

clearSelectedBlock();
// Convert action is executed at the end of the current JavaScript execution block
Expand Down Expand Up @@ -162,12 +166,20 @@ export default function ReusableBlockEdit( {
{ infoTitle }
</Text>
<Text style={ [ infoTextStyle, infoDescriptionStyle ] }>
{ __(
'Alternatively, you can detach and edit these blocks separately by tapping “Convert to regular blocks”.'
) }
{ innerBlockCount > 1
? __(
'Alternatively, you can detach and edit these blocks separately by tapping “Convert to regular blocks”.'
)
: __(
'Alternatively, you can detach and edit this block separately by tapping “Convert to regular block”.'
) }
</Text>
<TextControl
label={ __( 'Convert to regular blocks' ) }
label={
innerBlockCount > 1
? __( 'Convert to regular blocks' )
: __( 'Convert to regular block' )
}
separatorType="topFullWidth"
onPress={ onConvertToRegularBlocks }
labelStyle={ actionButtonStyle }
Expand Down
Expand Up @@ -108,7 +108,7 @@ describe( 'Reusable blocks', () => {
await insertReusableBlock( 'Surprised greeting block' );

// Convert block to a regular block.
await clickBlockToolbarButton( 'Convert to regular blocks' );
await clickBlockToolbarButton( 'Convert to regular block' );

// Check that we have a paragraph block on the page.
const paragraphBlock = await page.$(
Expand Down Expand Up @@ -343,7 +343,7 @@ describe( 'Reusable blocks', () => {

// Convert back to regular blocks.
await clickBlockToolbarButton( 'Select Reusable block' );
await clickBlockToolbarButton( 'Convert to regular blocks' );
await clickBlockToolbarButton( 'Convert to regular block' );
await page.waitForXPath( selector, {
hidden: true,
} );
Expand Down
Expand Up @@ -18,9 +18,10 @@ import { store as coreStore } from '@wordpress/core-data';
import { store as reusableBlocksStore } from '../../store';

function ReusableBlocksManageButton( { clientId } ) {
const { canRemove, isVisible } = useSelect(
const { canRemove, isVisible, innerBlockCount } = useSelect(
( select ) => {
const { getBlock, canRemoveBlock } = select( blockEditorStore );
const { getBlock, canRemoveBlock, getBlockCount } =
select( blockEditorStore );
const { canUser } = select( coreStore );
const reusableBlock = getBlock( clientId );

Expand All @@ -34,6 +35,7 @@ function ReusableBlocksManageButton( { clientId } ) {
'blocks',
reusableBlock.attributes.ref
),
innerBlockCount: getBlockCount( clientId ),
};
},
[ clientId ]
Expand All @@ -55,7 +57,9 @@ function ReusableBlocksManageButton( { clientId } ) {
</MenuItem>
{ canRemove && (
<MenuItem onClick={ () => convertBlockToStatic( clientId ) }>
{ __( 'Convert to regular blocks' ) }
{ innerBlockCount > 1
? __( 'Convert to regular blocks' )
: __( 'Convert to regular block' ) }
</MenuItem>
) }
</BlockSettingsMenuControls>
Expand Down