Skip to content

Commit

Permalink
DataViews Extensibility: Allow unregistering the duplicate pattern ac…
Browse files Browse the repository at this point in the history
…tion (#64373)

Co-authored-by: youknowriad <[email protected]>
Co-authored-by: ntsekouras <[email protected]>
  • Loading branch information
3 people authored and getdave committed Aug 14, 2024
1 parent 49e7868 commit 9348ec5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 27 deletions.
27 changes: 0 additions & 27 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { store as coreStore } from '@wordpress/core-data';
import { __, sprintf, _x } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import { useMemo, useState, useEffect } from '@wordpress/element';
import { privateApis as patternsPrivateApis } from '@wordpress/patterns';
import { parse } from '@wordpress/blocks';
import { DataForm } from '@wordpress/dataviews';
import {
Expand All @@ -31,10 +30,6 @@ import { unlock } from '../../lock-unlock';
import { CreateTemplatePartModalContents } from '../create-template-part-modal';
import { getItemTitle } from '../../dataviews/actions/utils';

// Patterns.
const { CreatePatternModalContents, useDuplicatePatternProps } =
unlock( patternsPrivateApis );

// TODO: this should be shared with other components (see post-fields in edit-site).
const fields = [
{
Expand Down Expand Up @@ -268,27 +263,6 @@ const useDuplicatePostAction = ( postType ) => {
);
};

export const duplicatePatternAction = {
id: 'duplicate-pattern',
label: _x( 'Duplicate', 'action label' ),
isEligible: ( item ) => item.type !== TEMPLATE_PART_POST_TYPE,
modalHeader: _x( 'Duplicate pattern', 'action label' ),
RenderModal: ( { items, closeModal } ) => {
const [ item ] = items;
const duplicatedProps = useDuplicatePatternProps( {
pattern: item,
onSuccess: () => closeModal(),
} );
return (
<CreatePatternModalContents
onClose={ closeModal }
confirmLabel={ _x( 'Duplicate', 'action label' ) }
{ ...duplicatedProps }
/>
);
},
};

export const duplicateTemplatePartAction = {
id: 'duplicate-template-part',
label: _x( 'Duplicate', 'action label' ),
Expand Down Expand Up @@ -383,7 +357,6 @@ export function usePostActions( { postType, onActionPerformed, context } ) {
isTemplateOrTemplatePart &&
userCanCreatePostType &&
duplicateTemplatePartAction,
isPattern && userCanCreatePostType && duplicatePatternAction,
...defaultActions,
].filter( Boolean );
// Filter actions based on provided context. If not provided
Expand Down
40 changes: 40 additions & 0 deletions packages/editor/src/dataviews/actions/duplicate-pattern.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* WordPress dependencies
*/
import { _x } from '@wordpress/i18n';
// @ts-ignore
import { privateApis as patternsPrivateApis } from '@wordpress/patterns';
import type { Action } from '@wordpress/dataviews';

/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import type { Pattern } from '../types';

// Patterns.
const { CreatePatternModalContents, useDuplicatePatternProps } =
unlock( patternsPrivateApis );

const duplicatePattern: Action< Pattern > = {
id: 'duplicate-pattern',
label: _x( 'Duplicate', 'action label' ),
isEligible: ( item ) => item.type !== 'wp_template_part',
modalHeader: _x( 'Duplicate pattern', 'action label' ),
RenderModal: ( { items, closeModal } ) => {
const [ item ] = items;
const duplicatedProps = useDuplicatePatternProps( {
pattern: item,
onSuccess: () => closeModal?.(),
} );
return (
<CreatePatternModalContents
onClose={ closeModal }
confirmLabel={ _x( 'Duplicate', 'action label' ) }
{ ...duplicatedProps }
/>
);
},
};

export default duplicatePattern;
11 changes: 11 additions & 0 deletions packages/editor/src/dataviews/store/private-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { doAction } from '@wordpress/hooks';
* Internal dependencies
*/
import deletePost from '../actions/delete-post';
import duplicatePattern from '../actions/duplicate-pattern';
import exportPattern from '../actions/export-pattern';
import resetPost from '../actions/reset-post';
import trashPost from '../actions/trash-post';
Expand Down Expand Up @@ -74,7 +75,17 @@ export const registerPostTypeActions =
.resolveSelect( coreStore )
.getPostType( postType ) ) as PostType;

const canCreate = await registry
.resolveSelect( coreStore )
.canUser( 'create', {
kind: 'postType',
name: postType,
} );

const actions = [
canCreate && postTypeConfig.slug === 'wp_block'
? duplicatePattern
: undefined,
postTypeConfig.supports?.title ? renamePost : undefined,
postTypeConfig?.supports?.[ 'page-attributes' ]
? reorderPage
Expand Down

0 comments on commit 9348ec5

Please sign in to comment.