-
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.
DataViews Extensibility: Allow unregistering the duplicate template p…
…art action (#64388) Co-authored-by: youknowriad <[email protected]> Co-authored-by: ntsekouras <[email protected]>
- Loading branch information
1 parent
eecd89d
commit 9f5ffd2
Showing
4 changed files
with
83 additions
and
71 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
70 changes: 70 additions & 0 deletions
70
packages/editor/src/dataviews/actions/duplicate-template-part.tsx
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,70 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useDispatch } from '@wordpress/data'; | ||
import { __, sprintf, _x } from '@wordpress/i18n'; | ||
import { store as noticesStore } from '@wordpress/notices'; | ||
import { useMemo } from '@wordpress/element'; | ||
// @ts-ignore | ||
import { parse } from '@wordpress/blocks'; | ||
import type { Action } from '@wordpress/dataviews'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { TEMPLATE_PART_POST_TYPE } from '../../store/constants'; | ||
import { CreateTemplatePartModalContents } from '../../components/create-template-part-modal'; | ||
import { getItemTitle } from './utils'; | ||
import type { TemplatePart } from '../types'; | ||
|
||
const duplicateTemplatePart: Action< TemplatePart > = { | ||
id: 'duplicate-template-part', | ||
label: _x( 'Duplicate', 'action label' ), | ||
isEligible: ( item ) => item.type === TEMPLATE_PART_POST_TYPE, | ||
modalHeader: _x( 'Duplicate template part', 'action label' ), | ||
RenderModal: ( { items, closeModal } ) => { | ||
const [ item ] = items; | ||
const blocks = useMemo( () => { | ||
return ( | ||
item.blocks ?? | ||
parse( | ||
typeof item.content === 'string' | ||
? item.content | ||
: item.content.raw, | ||
{ | ||
__unstableSkipMigrationLogs: true, | ||
} | ||
) | ||
); | ||
}, [ item.content, item.blocks ] ); | ||
const { createSuccessNotice } = useDispatch( noticesStore ); | ||
function onTemplatePartSuccess() { | ||
createSuccessNotice( | ||
sprintf( | ||
// translators: %s: The new template part's title e.g. 'Call to action (copy)'. | ||
__( '"%s" duplicated.' ), | ||
getItemTitle( item ) | ||
), | ||
{ type: 'snackbar', id: 'edit-site-patterns-success' } | ||
); | ||
closeModal?.(); | ||
} | ||
return ( | ||
<CreateTemplatePartModalContents | ||
blocks={ blocks } | ||
defaultArea={ item.area } | ||
defaultTitle={ sprintf( | ||
/* translators: %s: Existing template part title */ | ||
__( '%s (Copy)' ), | ||
getItemTitle( item ) | ||
) } | ||
onCreate={ onTemplatePartSuccess } | ||
onError={ closeModal } | ||
confirmLabel={ _x( 'Duplicate', 'action label' ) } | ||
closeModal={ closeModal } | ||
/> | ||
); | ||
}, | ||
}; | ||
|
||
export default duplicateTemplatePart; |
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
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