Skip to content

Commit

Permalink
Add context in post actions API
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsekouras committed Jun 10, 2024
1 parent b8fca79 commit ffa2496
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 68 deletions.
2 changes: 2 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

43 changes: 0 additions & 43 deletions packages/edit-site/src/components/dataviews-actions/index.js

This file was deleted.

11 changes: 4 additions & 7 deletions packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import {
import AddNewPageModal from '../add-new-page';
import Media from '../media';
import { unlock } from '../../lock-unlock';
import { useEditPostAction } from '../dataviews-actions';

const { usePostActions } = unlock( editorPrivateApis );
const { useLocation, useHistory } = unlock( routerPrivateApis );
Expand Down Expand Up @@ -456,12 +455,10 @@ export default function PagePages() {
[ authors, view.type, frontPageId, postsPageId ]
);

const postTypeActions = usePostActions( 'page' );
const editAction = useEditPostAction();
const actions = useMemo(
() => [ editAction, ...postTypeActions ],
[ postTypeActions, editAction ]
);
const actions = usePostActions( {
postType: 'page',
context: 'view',
} );

const onChangeView = useCallback(
( newView ) => {
Expand Down
18 changes: 11 additions & 7 deletions packages/edit-site/src/components/page-patterns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import usePatterns from './use-patterns';
import PatternsHeader from './header';
import { useLink } from '../routes/link';
import { useAddedBy } from '../page-templates/hooks';
import { useEditPostAction } from '../dataviews-actions';

const { ExperimentalBlockEditorProvider, useGlobalStyle } = unlock(
blockEditorPrivateApis
Expand Down Expand Up @@ -354,16 +353,21 @@ export default function DataviewsPatterns() {
return filterSortAndPaginate( patterns, viewWithoutFilters, fields );
}, [ patterns, view, fields, type ] );

const templatePartActions = usePostActions( TEMPLATE_PART_POST_TYPE );
const patternActions = usePostActions( PATTERN_TYPES.user );
const editAction = useEditPostAction();
const templatePartActions = usePostActions( {
postType: TEMPLATE_PART_POST_TYPE,
context: 'view',
} );
const patternActions = usePostActions( {
postType: PATTERN_TYPES.user,
context: 'view',
} );

const actions = useMemo( () => {
if ( type === TEMPLATE_PART_POST_TYPE ) {
return [ editAction, ...templatePartActions ].filter( Boolean );
return templatePartActions.filter( Boolean );
}
return [ editAction, ...patternActions ].filter( Boolean );
}, [ editAction, type, templatePartActions, patternActions ] );
return patternActions.filter( Boolean );
}, [ type, templatePartActions, patternActions ] );
const onChangeView = useCallback(
( newView ) => {
if ( newView.type !== view.type ) {
Expand Down
11 changes: 4 additions & 7 deletions packages/edit-site/src/components/page-templates/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ import {

import usePatternSettings from '../page-patterns/use-pattern-settings';
import { unlock } from '../../lock-unlock';
import { useEditPostAction } from '../dataviews-actions';

const { usePostActions } = unlock( editorPrivateApis );

Expand Down Expand Up @@ -323,12 +322,10 @@ export default function PageTemplates() {
return filterSortAndPaginate( records, view, fields );
}, [ records, view, fields ] );

const postTypeActions = usePostActions( TEMPLATE_POST_TYPE );
const editAction = useEditPostAction();
const actions = useMemo(
() => [ editAction, ...postTypeActions ],
[ postTypeActions, editAction ]
);
const actions = usePostActions( {
postType: TEMPLATE_POST_TYPE,
context: 'view',
} );

const onChangeView = useCallback(
( newView ) => {
Expand Down
1 change: 1 addition & 0 deletions packages/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"@wordpress/private-apis": "file:../private-apis",
"@wordpress/reusable-blocks": "file:../reusable-blocks",
"@wordpress/rich-text": "file:../rich-text",
"@wordpress/router": "file:../router",
"@wordpress/server-side-render": "file:../server-side-render",
"@wordpress/url": "file:../url",
"@wordpress/warning": "file:../warning",
Expand Down
55 changes: 52 additions & 3 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { external, trash, backup } from '@wordpress/icons';
import { external, trash, backup, edit } from '@wordpress/icons';
import { addQueryArgs } from '@wordpress/url';
import { useDispatch, useSelect } from '@wordpress/data';
import { decodeEntities } from '@wordpress/html-entities';
Expand All @@ -10,6 +10,7 @@ import { __, _n, sprintf, _x } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import { useMemo, useState } from '@wordpress/element';
import { privateApis as patternsPrivateApis } from '@wordpress/patterns';
import { privateApis as routerPrivateApis } from '@wordpress/router';

import {
Button,
Expand Down Expand Up @@ -38,6 +39,8 @@ import { CreateTemplatePartModalContents } from '../create-template-part-modal';
const { PATTERN_TYPES, CreatePatternModalContents, useDuplicatePatternProps } =
unlock( patternsPrivateApis );

const { useHistory } = unlock( routerPrivateApis );

/**
* Check if a template is removable.
*
Expand Down Expand Up @@ -612,6 +615,36 @@ const postRevisionsAction = {
onActionPerformed( posts );
}
},
context: 'view',
};

export const useEditPostAction = () => {
const history = useHistory();
return useMemo(
() => ( {
id: 'edit-post',
label: __( 'Edit' ),
isPrimary: true,
icon: edit,
isEligible( post ) {
if ( post.status === 'trash' ) {
return false;
}
// It's eligible for all post types except theme patterns.
return post.type !== PATTERN_TYPES.theme;
},
callback( items ) {
const post = items[ 0 ];
history?.push( {
postId: post.id,
postType: post.type,
canvas: 'edit',
} );
},
context: 'view',
} ),
[ history ]
);
};

const renamePostAction = {
Expand Down Expand Up @@ -1029,7 +1062,7 @@ export const duplicateTemplatePartAction = {
},
};

export function usePostActions( postType, onActionPerformed ) {
export function usePostActions( { postType, onActionPerformed, context } ) {
const { postTypeObject } = useSelect(
( select ) => {
const { getPostType } = select( coreStore );
Expand All @@ -1042,6 +1075,7 @@ export function usePostActions( postType, onActionPerformed ) {

const permanentlyDeletePostAction = usePermanentlyDeletePostAction();
const restorePostAction = useRestorePostAction();
const editPostAction = useEditPostAction();
const isTemplateOrTemplatePart = [
TEMPLATE_POST_TYPE,
TEMPLATE_PART_POST_TYPE,
Expand All @@ -1055,7 +1089,8 @@ export function usePostActions( postType, onActionPerformed ) {
return [];
}

const actions = [
let actions = [
editPostAction,
postTypeObject?.viewable && viewPostAction,
supportsRevisions && postRevisionsAction,
globalThis.IS_GUTENBERG_PLUGIN
Expand All @@ -1073,6 +1108,18 @@ export function usePostActions( postType, onActionPerformed ) {
: trashPostAction,
! isTemplateOrTemplatePart && permanentlyDeletePostAction,
].filter( Boolean );
// Filter actions based on provided context. If not provided
// all actions are returned. We'll have a single entry for getting the actions
// and the consumer should provide the context to filter the actions, if needed.
// Actions should also provide the `context` they support, if it's specific, to
// compare with the provided context to get all the actions.
// Right now the only supported context is `view`.
actions = actions.filter( ( action ) => {
if ( ! action.context ) {
return true;
}
return action.context === context;
} );

if ( onActionPerformed ) {
for ( let i = 0; i < actions.length; ++i ) {
Expand Down Expand Up @@ -1126,5 +1173,7 @@ export function usePostActions( postType, onActionPerformed ) {
isLoaded,
supportsRevisions,
supportsTitle,
editPostAction,
context,
] );
}
2 changes: 1 addition & 1 deletion packages/editor/src/components/post-actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function PostActions( { onActionPerformed, buttonProps } ) {
postType: _postType,
};
}, [] );
const allActions = usePostActions( postType, onActionPerformed );
const allActions = usePostActions( { postType, onActionPerformed } );

const actions = useMemo( () => {
return allActions.filter( ( action ) => {
Expand Down

0 comments on commit ffa2496

Please sign in to comment.