Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DataViews Extensibility: Allow unregistering the view post action #64467

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 3 additions & 40 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/**
* WordPress dependencies
*/
import { external } from '@wordpress/icons';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { __ } from '@wordpress/i18n';
import { useMemo, useEffect } from '@wordpress/element';

/**
Expand All @@ -13,30 +10,11 @@ import { useMemo, useEffect } from '@wordpress/element';
import { store as editorStore } from '../../store';
import { unlock } from '../../lock-unlock';

const viewPostAction = {
id: 'view-post',
label: __( 'View' ),
isPrimary: true,
icon: external,
isEligible( post ) {
return post.status !== 'trash';
},
callback( posts, { onActionPerformed } ) {
const post = posts[ 0 ];
window.open( post.link, '_blank' );
if ( onActionPerformed ) {
onActionPerformed( posts );
}
},
};

export function usePostActions( { postType, onActionPerformed, context } ) {
const { defaultActions, postTypeObject } = useSelect(
const { defaultActions } = useSelect(
( select ) => {
const { getPostType } = select( coreStore );
const { getEntityActions } = unlock( select( editorStore ) );
return {
postTypeObject: getPostType( postType ),
defaultActions: getEntityActions( 'postType', postType ),
};
},
Expand All @@ -48,23 +26,14 @@ export function usePostActions( { postType, onActionPerformed, context } ) {
registerPostTypeActions( postType );
}, [ registerPostTypeActions, postType ] );

const isLoaded = !! postTypeObject;
return useMemo( () => {
if ( ! isLoaded ) {
return [];
}

let actions = [
postTypeObject?.viewable && viewPostAction,
...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 ) => {
const actions = defaultActions.filter( ( action ) => {
if ( ! action.context ) {
return true;
}
Expand Down Expand Up @@ -119,11 +88,5 @@ export function usePostActions( { postType, onActionPerformed, context } ) {
}

return actions;
}, [
defaultActions,
postTypeObject?.viewable,
onActionPerformed,
isLoaded,
context,
] );
}, [ defaultActions, onActionPerformed, context ] );
}
30 changes: 30 additions & 0 deletions packages/editor/src/dataviews/actions/view-post.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* WordPress dependencies
*/
import { external } from '@wordpress/icons';
import { __ } from '@wordpress/i18n';
import type { Action } from '@wordpress/dataviews';

/**
* Internal dependencies
*/
import type { BasePost } from '../types';

const viewPost: Action< BasePost > = {
id: 'view-post',
label: __( 'View' ),
isPrimary: true,
icon: external,
isEligible( post ) {
return post.status !== 'trash';
},
callback( posts, { onActionPerformed } ) {
const post = posts[ 0 ];
window.open( post?.link, '_blank' );
if ( onActionPerformed ) {
onActionPerformed( posts );
}
},
};

export default viewPost;
11 changes: 7 additions & 4 deletions packages/editor/src/dataviews/store/private-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { store as editorStore } from '../../store';
import { unlock } from '../../lock-unlock';
import duplicatePost from '../actions/duplicate-post';
import viewPostRevisions from '../actions/view-post-revisions';
import viewPost from '../actions/view-post';

export function registerEntityAction< Item >(
kind: string,
Expand Down Expand Up @@ -89,6 +90,7 @@ export const registerPostTypeActions =
.getCurrentTheme();

const actions = [
postTypeConfig.viewable ? viewPost : undefined,
!! postTypeConfig?.supports?.revisions
? viewPostRevisions
: undefined,
Expand All @@ -101,9 +103,10 @@ export const registerPostTypeActions =
duplicatePost
: undefined,
postTypeConfig.slug === 'wp_template_part' &&
canCreate &&
currentTheme?.is_block_theme &&
duplicateTemplatePart,
canCreate &&
currentTheme?.is_block_theme
? duplicateTemplatePart
: undefined,
canCreate && postTypeConfig.slug === 'wp_block'
? duplicatePattern
: undefined,
Expand All @@ -121,7 +124,7 @@ export const registerPostTypeActions =

registry.batch( () => {
actions.forEach( ( action ) => {
if ( action === undefined ) {
if ( ! action ) {
return;
}
unlock( registry.dispatch( editorStore ) ).registerEntityAction(
Expand Down
2 changes: 2 additions & 0 deletions packages/editor/src/dataviews/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface BasePost extends CommonPost {
featured_media?: number;
menu_order?: number;
ping_status?: 'open' | 'closed';
link?: string;
}

export interface Template extends CommonPost {
Expand Down Expand Up @@ -72,6 +73,7 @@ export type PostWithPermissions = Post & {

export interface PostType {
slug: string;
viewable: boolean;
supports?: {
'page-attributes'?: boolean;
title?: boolean;
Expand Down
Loading