Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,92 @@
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { MenuItem } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';
import {
MenuItem,
__experimentalConfirmDialog as ConfirmDialog,
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { decodeEntities } from '@wordpress/html-entities';
import { useState } from '@wordpress/element';

export default function ConvertToRegularBlocks( { clientId, onClose } ) {
const [ showConfirmDialog, setShowConfirmDialog ] = useState( false );

const { getBlocks } = useSelect( blockEditorStore );
const { replaceBlocks } = useDispatch( blockEditorStore );

const canRemove = useSelect(
( select ) => select( blockEditorStore ).canRemoveBlock( clientId ),
const { canRemove, templatePartTitle } = useSelect(
( select ) => {
const { canRemoveBlock, getBlock } = select( blockEditorStore );
const { getEntityRecord, getCurrentTheme } = select( coreStore );

const block = getBlock( clientId );
const { slug, theme } = block?.attributes ?? {};
const themeSlug = theme || getCurrentTheme()?.stylesheet;
const templatePartId =
themeSlug && slug ? `${ themeSlug }//${ slug }` : null;
const entity = templatePartId
? getEntityRecord(
'postType',
'wp_template_part',
templatePartId
)
: null;

return {
canRemove: canRemoveBlock( clientId ),
templatePartTitle: entity?.title?.rendered
? decodeEntities( entity.title.rendered )
: null,
};
},
[ clientId ]
);

if ( ! canRemove ) {
return null;
}

const title = templatePartTitle
? sprintf(
/* translators: %s: template part title, e.g. "Header" */
__( 'Detach %s?' ),
templatePartTitle
)
: __( 'Detach template part?' );

const message = templatePartTitle
? sprintf(
/* translators: %s: template part title, e.g. "Header" */
__(
'The blocks will be separated from the original template part and will be fully editable. Future changes to the %s template part will not apply here.'
),
templatePartTitle
)
: __(
'The blocks will be separated from the original template part and will be fully editable. Future changes to the template part will not apply here.'
);

return (
<MenuItem
onClick={ () => {
replaceBlocks( clientId, getBlocks( clientId ) );
onClose();
} }
>
{ __( 'Detach' ) }
</MenuItem>
<>
<MenuItem onClick={ () => setShowConfirmDialog( true ) }>
{ __( 'Detach' ) }
</MenuItem>
<ConfirmDialog
isOpen={ showConfirmDialog }
onConfirm={ () => {
replaceBlocks( clientId, getBlocks( clientId ) );
onClose();
} }
onCancel={ () => setShowConfirmDialog( false ) }
confirmButtonText={ __( 'Detach' ) }
size="medium"
title={ title }
__experimentalHideHeader={ false }
>
{ message }
</ConfirmDialog>
</>
);
}
16 changes: 3 additions & 13 deletions packages/patterns/src/components/patterns-manage-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,15 @@ function PatternsManageButton( { clientId, onClose } ) {
{ canDetach && (
<>
<MenuItem onClick={ () => setShowConfirmDialog( true ) }>
{ isSyncedPattern
? __( 'Disconnect pattern' )
: __( 'Detach pattern' ) }
{ __( 'Detach' ) }
</MenuItem>
<ConfirmDialog
isOpen={ showConfirmDialog }
onConfirm={ handleDetach }
onCancel={ () => setShowConfirmDialog( false ) }
confirmButtonText={
isSyncedPattern
? __( 'Disconnect' )
: __( 'Detach' )
}
confirmButtonText={ __( 'Detach' ) }
size="medium"
title={
isSyncedPattern
? __( 'Disconnect pattern?' )
: __( 'Detach pattern?' )
}
title={ __( 'Detach pattern?' ) }
__experimentalHideHeader={ false }
>
{ isSyncedPattern
Expand Down
8 changes: 4 additions & 4 deletions test/e2e/specs/editor/various/pattern-overrides.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -691,10 +691,10 @@ test.describe( 'Pattern Overrides', () => {
editor.canvas.getByRole( 'document', { name: 'Block: Pattern' } )
);
await editor.showBlockToolbar();
await editor.clickBlockOptionsMenuItem( 'Disconnect pattern' );
await editor.clickBlockOptionsMenuItem( 'Detach' );
await page
.getByRole( 'dialog' )
.getByRole( 'button', { name: 'Disconnect' } )
.getByRole( 'button', { name: 'Detach' } )
.click();

// Check that the overrides remain.
Expand Down Expand Up @@ -737,10 +737,10 @@ test.describe( 'Pattern Overrides', () => {
editor.canvas.getByRole( 'document', { name: 'Block: Pattern' } )
);
await editor.showBlockToolbar();
await editor.clickBlockOptionsMenuItem( 'Disconnect pattern' );
await editor.clickBlockOptionsMenuItem( 'Detach' );
await page
.getByRole( 'dialog' )
.getByRole( 'button', { name: 'Disconnect' } )
.getByRole( 'button', { name: 'Detach' } )
.click();

// Check that the overrides remain.
Expand Down
16 changes: 8 additions & 8 deletions test/e2e/specs/editor/various/patterns.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -479,8 +479,8 @@ test.describe( 'Unsynced pattern', () => {
editor.canvas.getByRole( 'document', { name: 'Block: Paragraph' } )
);

// Open the block options menu and click "Detach pattern".
await editor.clickBlockOptionsMenuItem( 'Detach pattern' );
// Open the block options menu and click "Detach".
await editor.clickBlockOptionsMenuItem( 'Detach' );
await page
.getByRole( 'dialog' )
.getByRole( 'button', { name: 'Detach' } )
Expand Down Expand Up @@ -838,10 +838,10 @@ test.describe( 'Synced pattern', () => {
await editor.selectBlocks(
editor.canvas.getByRole( 'document', { name: 'Block: Pattern' } )
);
await editor.clickBlockOptionsMenuItem( 'Disconnect pattern' );
await editor.clickBlockOptionsMenuItem( 'Detach' );
await page
.getByRole( 'dialog' )
.getByRole( 'button', { name: 'Disconnect' } )
.getByRole( 'button', { name: 'Detach' } )
.click();

await expect.poll( editor.getBlocks ).toMatchObject( [
Expand Down Expand Up @@ -878,10 +878,10 @@ test.describe( 'Synced pattern', () => {
await editor.selectBlocks(
editor.canvas.getByRole( 'document', { name: 'Block: Pattern' } )
);
await editor.clickBlockOptionsMenuItem( 'Disconnect pattern' );
await editor.clickBlockOptionsMenuItem( 'Detach' );
await page
.getByRole( 'dialog' )
.getByRole( 'button', { name: 'Disconnect' } )
.getByRole( 'button', { name: 'Detach' } )
.click();

await expect.poll( editor.getBlocks ).toMatchObject( [
Expand Down Expand Up @@ -984,10 +984,10 @@ test.describe( 'Synced pattern', () => {
await editor.selectBlocks(
editor.canvas.getByRole( 'document', { name: 'Block: Pattern' } )
);
await editor.clickBlockOptionsMenuItem( 'Disconnect pattern' );
await editor.clickBlockOptionsMenuItem( 'Detach' );
await page
.getByRole( 'dialog' )
.getByRole( 'button', { name: 'Disconnect' } )
.getByRole( 'button', { name: 'Detach' } )
.click();

await expect.poll( editor.getBlocks ).toMatchObject( [
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/specs/site-editor/template-part.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ test.describe( 'Template Part', () => {
test( 'can detach blocks from a template part', async ( {
admin,
editor,
page,
} ) => {
const paragraphText = 'Test 3';

Expand Down Expand Up @@ -210,6 +211,10 @@ test.describe( 'Template Part', () => {
// Detach the paragraph from the header template part.
await editor.selectBlocks( templatePartWithParagraph );
await editor.clickBlockOptionsMenuItem( 'Detach' );
await page
.getByRole( 'dialog' )
.getByRole( 'button', { name: 'Detach' } )
.click();

// There should be a paragraph but no header template part.
await expect( paragraph ).toBeVisible();
Expand Down
Loading