Skip to content

Commit

Permalink
Add context in post actions API (#62443)
Browse files Browse the repository at this point in the history
Co-authored-by: ntsekouras <[email protected]>
Co-authored-by: mcsf <[email protected]>
Co-authored-by: youknowriad <[email protected]>
Co-authored-by: jorgefilipecosta <[email protected]>
  • Loading branch information
5 people authored Jun 18, 2024
1 parent 33854e1 commit 3800e67
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 14 deletions.
5 changes: 4 additions & 1 deletion packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,10 @@ export default function PagePages() {
[ authors, view.type, frontPageId, postsPageId ]
);

const postTypeActions = usePostActions( 'page' );
const postTypeActions = usePostActions( {
postType: 'page',
context: 'list',
} );
const editAction = useEditPostAction();
const actions = useMemo(
() => [ editAction, ...postTypeActions ],
Expand Down
10 changes: 8 additions & 2 deletions packages/edit-site/src/components/page-patterns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,14 @@ 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 templatePartActions = usePostActions( {
postType: TEMPLATE_PART_POST_TYPE,
context: 'list',
} );
const patternActions = usePostActions( {
postType: PATTERN_TYPES.user,
context: 'list',
} );
const editAction = useEditPostAction();

const actions = useMemo( () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/edit-site/src/components/page-templates/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,10 @@ export default function PageTemplates() {
return filterSortAndPaginate( records, view, fields );
}, [ records, view, fields ] );

const postTypeActions = usePostActions( TEMPLATE_POST_TYPE );
const postTypeActions = usePostActions( {
postType: TEMPLATE_POST_TYPE,
context: 'list',
} );
const editAction = useEditPostAction();
const actions = useMemo(
() => [ editAction, ...postTypeActions ],
Expand Down
18 changes: 16 additions & 2 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,7 @@ const viewPostAction = {

const postRevisionsAction = {
id: 'view-post-revisions',
context: 'list',
label( items ) {
const revisionsCount =
items[ 0 ]._links?.[ 'version-history' ]?.[ 0 ]?.count ?? 0;
Expand Down Expand Up @@ -1073,7 +1074,7 @@ export const duplicateTemplatePartAction = {
},
};

export function usePostActions( postType, onActionPerformed ) {
export function usePostActions( { postType, onActionPerformed, context } ) {
const { defaultActions, postTypeObject } = useSelect(
( select ) => {
const { getPostType } = select( coreStore );
Expand Down Expand Up @@ -1102,7 +1103,7 @@ export function usePostActions( postType, onActionPerformed ) {
return [];
}

const actions = [
let actions = [
postTypeObject?.viewable && viewPostAction,
supportsRevisions && postRevisionsAction,
globalThis.IS_GUTENBERG_PLUGIN
Expand All @@ -1121,6 +1122,18 @@ export function usePostActions( postType, onActionPerformed ) {
! isTemplateOrTemplatePart && permanentlyDeletePostAction,
...defaultActions,
].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 `list`.
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 @@ -1176,5 +1189,6 @@ export function usePostActions( postType, onActionPerformed ) {
isLoaded,
supportsRevisions,
supportsTitle,
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
12 changes: 5 additions & 7 deletions test/e2e/specs/editor/various/footnotes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,14 +363,12 @@ test.describe( 'Footnotes', () => {

// Open revisions.
await editor.openDocumentSettingsSidebar();
const editorSettings = page.getByRole( 'region', {
name: 'Editor settings',
} );
await editorSettings.getByRole( 'tab', { name: 'Post' } ).click();
await editorSettings.getByRole( 'button', { name: 'Actions' } ).click();
await page
.getByRole( 'menu' )
.getByRole( 'menuitem', { name: 'View revisions' } )
.getByRole( 'region', { name: 'Editor settings' } )
.getByRole( 'tab', { name: 'Post' } )
.click();
await page
.locator( '.editor-private-post-last-revision__button' )
.click();
await page.locator( '.revisions-controls .ui-slider-handle' ).focus();
await page.keyboard.press( 'ArrowLeft' );
Expand Down

1 comment on commit 3800e67

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 3800e67.
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/9560664598
📝 Reported issues:

Please sign in to comment.