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

Add template actions in site editor navigation sidebar #51054

Merged
merged 4 commits into from
May 30, 2023
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
7 changes: 1 addition & 6 deletions packages/edit-site/src/components/editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,7 @@ export default function Editor( { isLoading } ) {
'is-loading': isLoading,
}
) }
notices={
( isEditMode ||
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe there was a recent PR that did that but was reverted due to some side effects. We might want to check whether this change doesn't introduce these side effects back.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I know about the other PR and I think the issues there were due to some scale changes. This doesn't seem to affect anything for now.

window?.__experimentalEnableThemePreviews ) && (
<EditorSnackbars />
)
}
notices={ <EditorSnackbars /> }
content={
<>
<GlobalStylesRenderer />
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-site/src/components/editor/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

// Adjust the position of the notices
.edit-site .components-editor-notices__snackbar {
position: fixed;
position: absolute;
right: 0;
bottom: 40px;
padding-left: 16px;
Expand Down
8 changes: 6 additions & 2 deletions packages/edit-site/src/components/list/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import { decodeEntities } from '@wordpress/html-entities';
/**
* Internal dependencies
*/
import TemplateActions from '../template-actions';
import Link from '../routes/link';
import Actions from './actions';
import AddedBy from './added-by';

export default function Table( { templateType } ) {
Expand Down Expand Up @@ -126,7 +126,11 @@ export default function Table( { templateType } ) {
) : null }
</td>
<td className="edit-site-list-table-column" role="cell">
<Actions template={ template } />
<TemplateActions
postType={ template.type }
postId={ template.id }
className="edit-site-list-table__actions"
/>
</td>
</tr>
) ) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { unlock } from '../../private-apis';
import { store as editSiteStore } from '../../store';
import SidebarButton from '../sidebar-button';
import { useAddedBy } from '../list/added-by';
import TemplateActions from '../template-actions';

function useTemplateTitleAndDescription( postType, postId ) {
const { getDescription, getTitle, record } = useEditedEntityRecord(
Expand Down Expand Up @@ -87,8 +88,10 @@ function useTemplateTitleAndDescription( postType, postId ) {
}

export default function SidebarNavigationScreenTemplate() {
const { params } = useNavigator();
const { postType, postId } = params;
const navigator = useNavigator();
const {
params: { postType, postId },
} = navigator;
const { setCanvasMode } = unlock( useDispatch( editSiteStore ) );
const { title, description } = useTemplateTitleAndDescription(
postType,
Expand All @@ -99,11 +102,21 @@ export default function SidebarNavigationScreenTemplate() {
<SidebarNavigationScreen
title={ title }
actions={
<SidebarButton
onClick={ () => setCanvasMode( 'edit' ) }
label={ __( 'Edit' ) }
icon={ pencil }
/>
<div>
<TemplateActions
postType={ postType }
postId={ postId }
toggleProps={ { as: SidebarButton } }
onRemove={ () => {
navigator.goTo( `/${ postType }/all` );
} }
/>
<SidebarButton
onClick={ () => setCanvasMode( 'edit' ) }
label={ __( 'Edit' ) }
icon={ pencil }
/>
</div>
}
description={ description }
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useDispatch } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { __, sprintf } from '@wordpress/i18n';
import { DropdownMenu, MenuGroup, MenuItem } from '@wordpress/components';
Expand All @@ -11,12 +11,23 @@ import { store as noticesStore } from '@wordpress/notices';
/**
* Internal dependencies
*/
import { store as editSiteStore } from '../../../store';
import isTemplateRemovable from '../../../utils/is-template-removable';
import isTemplateRevertable from '../../../utils/is-template-revertable';
import { store as editSiteStore } from '../../store';
import isTemplateRemovable from '../../utils/is-template-removable';
import isTemplateRevertable from '../../utils/is-template-revertable';
import RenameMenuItem from './rename-menu-item';

export default function Actions( { template } ) {
export default function TemplateActions( {
Copy link
Contributor

Choose a reason for hiding this comment

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

It is great that you were able to reuse this. Personally I always prefer reusable components that receive "ids" instead of objects like this as it's more reusable and also more performant in general (can add extra selectors but this won't force the parent to re-render if it's only this component that cares about the object)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated to fetch the data in this component. Also used getEntityRecord and reverted the checks for title.rendered existence.

postType,
postId,
className,
toggleProps,
onRemove,
} ) {
const template = useSelect(
( select ) =>
select( coreStore ).getEntityRecord( 'postType', postType, postId ),
[ postType, postId ]
);
const { removeTemplate, revertTemplate } = useDispatch( editSiteStore );
const { saveEditedEntityRecord } = useDispatch( coreStore );
const { createSuccessNotice, createErrorNotice } =
Expand Down Expand Up @@ -62,7 +73,8 @@ export default function Actions( { template } ) {
<DropdownMenu
icon={ moreVertical }
label={ __( 'Actions' ) }
className="edit-site-list-table__actions"
className={ className }
toggleProps={ toggleProps }
>
{ ( { onClose } ) => (
<MenuGroup>
Expand All @@ -77,6 +89,7 @@ export default function Actions( { template } ) {
isTertiary
onClick={ () => {
removeTemplate( template );
onRemove?.();
onClose();
} }
>
Expand Down
3 changes: 2 additions & 1 deletion packages/edit-site/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { store as interfaceStore } from '@wordpress/interface';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { speak } from '@wordpress/a11y';
import { store as preferencesStore } from '@wordpress/preferences';
import { decodeEntities } from '@wordpress/html-entities';

/**
* Internal dependencies
Expand Down Expand Up @@ -144,7 +145,7 @@ export const removeTemplate =
sprintf(
/* translators: The template/part's name. */
__( '"%s" deleted.' ),
template.title.rendered
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not related to this PR, but I believe we'll land it soon and it's a small change.

decodeEntities( template.title.rendered )
),
{ type: 'snackbar', id: 'site-editor-template-deleted-success' }
);
Expand Down