Skip to content

Commit

Permalink
Add: View button to the post card panel.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Apr 15, 2024
1 parent 06b3537 commit 45b527d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ import {
/**
* Internal dependencies
*/
import { TEMPLATE_ORIGINS, TEMPLATE_POST_TYPE } from '../../store/constants';
import {
TEMPLATE_ORIGINS,
TEMPLATE_POST_TYPE,
TEMPLATE_PART_POST_TYPE,
PATTERN_POST_TYPE,
} from '../../store/constants';
import { store as editorStore } from '../../store';
import { unlock } from '../../lock-unlock';
import isTemplateRevertable from '../../store/utils/is-template-revertable';
Expand Down Expand Up @@ -365,13 +370,19 @@ function useRestorePostAction() {
);
}

const viewPostAction = {
export const viewPostAction = {
id: 'view-post',
label: __( 'View' ),
isPrimary: true,
icon: external,
isEligible( post ) {
return post.status !== 'trash';
return (
! [
TEMPLATE_POST_TYPE,
TEMPLATE_PART_POST_TYPE,
PATTERN_POST_TYPE,
].includes( post.type ) && post.status !== 'trash'
);
},
callback( posts, onActionPerformed ) {
const post = posts[ 0 ];
Expand Down
23 changes: 23 additions & 0 deletions packages/editor/src/components/post-card-panel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
__experimentalVStack as VStack,
__experimentalText as Text,
PanelBody,
Button,
} from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
import { useSelect } from '@wordpress/data';
Expand All @@ -30,6 +31,24 @@ import {
} from '../../store/constants';
import { unlock } from '../../lock-unlock';
import TemplateAreas from '../template-areas';
import { viewPostAction } from '../post-actions/actions';

function ActionTrigger( { action, item } ) {
if ( ! action.isEligible( item ) ) {
return null;
}
return (
<Button
label={ action.label }
icon={ action.icon }
isDestructive={ action.isDestructive }
size="compact"
onClick={ async () => {
await action.callback( [ item ] );
} }
/>
);
}

export default function PostCardPanel( { className, actions } ) {
const {
Expand All @@ -40,11 +59,13 @@ export default function PostCardPanel( { className, actions } ) {
postType,
postContent,
isPostsPage,
post,
} = useSelect( ( select ) => {
const {
getEditedPostAttribute,
getCurrentPostType,
getCurrentPostId,
getCurrentPost,
__experimentalGetTemplateInfo,
} = select( editorStore );
const { getEditedEntityRecord, getEntityRecord } = select( coreStore );
Expand All @@ -64,6 +85,7 @@ export default function PostCardPanel( { className, actions } ) {
} ),
isPostsPage: +_id === siteSettings?.page_for_posts,
postContent: getEditedPostAttribute( 'content' ),
post: getCurrentPost(),
};
}, [] );
const description = templateInfo?.description;
Expand Down Expand Up @@ -100,6 +122,7 @@ export default function PostCardPanel( { className, actions } ) {
>
{ title ? decodeEntities( title ) : __( 'No Title' ) }
</Text>
<ActionTrigger item={ post } action={ viewPostAction } />
{ actions }
</HStack>
<VStack className="editor-post-card-panel__content">
Expand Down
4 changes: 4 additions & 0 deletions packages/editor/src/components/post-card-panel/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@
color: inherit;
}
}

&__header > button {
margin-top: -$grid-unit-05;
}
}

0 comments on commit 45b527d

Please sign in to comment.