-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Patterns: Add renaming, duplication, and deletion options (#52270)
- Loading branch information
1 parent
4e9b121
commit bb5c6d0
Showing
6 changed files
with
421 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
196 changes: 196 additions & 0 deletions
196
packages/edit-site/src/components/page-patterns/duplicate-menu-item.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,196 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { MenuItem } from '@wordpress/components'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { useDispatch } from '@wordpress/data'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { store as noticesStore } from '@wordpress/notices'; | ||
import { privateApis as routerPrivateApis } from '@wordpress/router'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { | ||
TEMPLATE_PARTS, | ||
PATTERNS, | ||
SYNC_TYPES, | ||
USER_PATTERNS, | ||
USER_PATTERN_CATEGORY, | ||
} from './utils'; | ||
import { | ||
useExistingTemplateParts, | ||
getUniqueTemplatePartTitle, | ||
getCleanTemplatePartSlug, | ||
} from '../../utils/template-part-create'; | ||
import { unlock } from '../../lock-unlock'; | ||
|
||
const { useHistory } = unlock( routerPrivateApis ); | ||
|
||
function getPatternMeta( item ) { | ||
if ( item.type === PATTERNS ) { | ||
return { wp_pattern_sync_status: SYNC_TYPES.unsynced }; | ||
} | ||
|
||
const syncStatus = item.reusableBlock.wp_pattern_sync_status; | ||
const isUnsynced = syncStatus === SYNC_TYPES.unsynced; | ||
|
||
return { | ||
...item.reusableBlock.meta, | ||
wp_pattern_sync_status: isUnsynced ? syncStatus : undefined, | ||
}; | ||
} | ||
|
||
export default function DuplicateMenuItem( { | ||
categoryId, | ||
item, | ||
label = __( 'Duplicate' ), | ||
onClose, | ||
} ) { | ||
const { saveEntityRecord } = useDispatch( coreStore ); | ||
const { createErrorNotice, createSuccessNotice } = | ||
useDispatch( noticesStore ); | ||
|
||
const history = useHistory(); | ||
const existingTemplateParts = useExistingTemplateParts(); | ||
|
||
async function createTemplatePart() { | ||
try { | ||
const copiedTitle = sprintf( | ||
/* translators: %s: Existing template part title */ | ||
__( '%s (Copy)' ), | ||
item.title | ||
); | ||
const title = getUniqueTemplatePartTitle( | ||
copiedTitle, | ||
existingTemplateParts | ||
); | ||
const slug = getCleanTemplatePartSlug( title ); | ||
const { area, content } = item.templatePart; | ||
|
||
const result = await saveEntityRecord( | ||
'postType', | ||
'wp_template_part', | ||
{ slug, title, content, area }, | ||
{ throwOnError: true } | ||
); | ||
|
||
createSuccessNotice( | ||
sprintf( | ||
// translators: %s: The new template part's title e.g. 'Call to action (copy)'. | ||
__( '"%s" created.' ), | ||
title | ||
), | ||
{ | ||
type: 'snackbar', | ||
id: 'edit-site-patterns-success', | ||
actions: [ | ||
{ | ||
label: __( 'Edit' ), | ||
onClick: () => | ||
history.push( { | ||
postType: TEMPLATE_PARTS, | ||
postId: result?.id, | ||
categoryType: TEMPLATE_PARTS, | ||
categoryId, | ||
} ), | ||
}, | ||
], | ||
} | ||
); | ||
|
||
onClose(); | ||
} catch ( error ) { | ||
const errorMessage = | ||
error.message && error.code !== 'unknown_error' | ||
? error.message | ||
: __( | ||
'An error occurred while creating the template part.' | ||
); | ||
|
||
createErrorNotice( errorMessage, { | ||
type: 'snackbar', | ||
id: 'edit-site-patterns-error', | ||
} ); | ||
onClose(); | ||
} | ||
} | ||
|
||
async function createPattern() { | ||
try { | ||
const isThemePattern = item.type === PATTERNS; | ||
const title = sprintf( | ||
/* translators: %s: Existing pattern title */ | ||
__( '%s (Copy)' ), | ||
item.title | ||
); | ||
|
||
const result = await saveEntityRecord( | ||
'postType', | ||
'wp_block', | ||
{ | ||
content: isThemePattern | ||
? item.content | ||
: item.reusableBlock.content, | ||
meta: getPatternMeta( item ), | ||
status: 'publish', | ||
title, | ||
}, | ||
{ throwOnError: true } | ||
); | ||
|
||
const actionLabel = isThemePattern | ||
? __( 'View my patterns' ) | ||
: __( 'Edit' ); | ||
|
||
const newLocation = isThemePattern | ||
? { | ||
categoryType: USER_PATTERNS, | ||
categoryId: USER_PATTERN_CATEGORY, | ||
path: '/patterns', | ||
} | ||
: { | ||
categoryType: USER_PATTERNS, | ||
categoryId: USER_PATTERN_CATEGORY, | ||
postType: USER_PATTERNS, | ||
postId: result?.id, | ||
}; | ||
|
||
createSuccessNotice( | ||
sprintf( | ||
// translators: %s: The new pattern's title e.g. 'Call to action (copy)'. | ||
__( '"%s" added to my patterns.' ), | ||
title | ||
), | ||
{ | ||
type: 'snackbar', | ||
id: 'edit-site-patterns-success', | ||
actions: [ | ||
{ | ||
label: actionLabel, | ||
onClick: () => history.push( newLocation ), | ||
}, | ||
], | ||
} | ||
); | ||
|
||
onClose(); | ||
} catch ( error ) { | ||
const errorMessage = | ||
error.message && error.code !== 'unknown_error' | ||
? error.message | ||
: __( 'An error occurred while creating the pattern.' ); | ||
|
||
createErrorNotice( errorMessage, { | ||
type: 'snackbar', | ||
id: 'edit-site-patterns-error', | ||
} ); | ||
onClose(); | ||
} | ||
} | ||
|
||
const createItem = | ||
item.type === TEMPLATE_PARTS ? createTemplatePart : createPattern; | ||
|
||
return <MenuItem onClick={ createItem }>{ label }</MenuItem>; | ||
} |
Oops, something went wrong.
bb5c6d0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flaky tests detected in bb5c6d0.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5483552709
📝 Reported issues:
/test/e2e/specs/site-editor/templates.spec.js