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 3 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
7 changes: 5 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,10 @@ export default function Table( { templateType } ) {
) : null }
</td>
<td className="edit-site-list-table-column" role="cell">
<Actions template={ template } />
<TemplateActions
template={ template }
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 @@ -83,14 +84,16 @@ function useTemplateTitleAndDescription( postType, postId ) {
</>
);

return { title, description };
return { title, description, template: record };
}

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(
const { title, description, template } = useTemplateTitleAndDescription(
postType,
postId
);
Expand All @@ -99,11 +102,20 @@ export default function SidebarNavigationScreenTemplate() {
<SidebarNavigationScreen
title={ title }
actions={
<SidebarButton
onClick={ () => setCanvasMode( 'edit' ) }
label={ __( 'Edit' ) }
icon={ pencil }
/>
<div>
<TemplateActions
template={ template }
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
Expand Up @@ -11,12 +11,17 @@ 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.

template,
className,
toggleProps,
onRemove,
} ) {
const { removeTemplate, revertTemplate } = useDispatch( editSiteStore );
const { saveEditedEntityRecord } = useDispatch( coreStore );
const { createSuccessNotice, createErrorNotice } =
Expand All @@ -41,7 +46,7 @@ export default function Actions( { template } ) {
sprintf(
/* translators: The template/part's name. */
__( '"%s" reverted.' ),
template.title.rendered
template.title.rendered || template.title
),
{
type: 'snackbar',
Expand All @@ -62,7 +67,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 +83,7 @@ export default function Actions( { template } ) {
isTertiary
onClick={ () => {
removeTemplate( template );
onRemove?.();
onClose();
} }
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import { store as coreStore } from '@wordpress/core-data';
import { store as noticesStore } from '@wordpress/notices';

export default function RenameMenuItem( { template, onClose } ) {
const [ title, setTitle ] = useState( () => template.title.rendered );
const [ title, setTitle ] = useState(
() => template.title?.rendered || template.title
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
);
const [ isModalOpen, setIsModalOpen ] = useState( false );

const { editEntityRecord, saveEditedEntityRecord } =
Expand Down Expand Up @@ -67,7 +69,7 @@ export default function RenameMenuItem( { template, onClose } ) {
<MenuItem
onClick={ () => {
setIsModalOpen( true );
setTitle( template.title.rendered );
setTitle( template.title?.rendered || template.title );
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
} }
>
{ __( 'Rename' ) }
Expand Down
2 changes: 1 addition & 1 deletion packages/edit-site/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,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.

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