From f03482230a2b68b351599143ced0c5fefa848c59 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Thu, 9 May 2024 11:33:17 +0100 Subject: [PATCH] Move the edit action into the site editor --- .../src/components/dataviews-actions/index.js | 38 +++++++++++++++++++ .../src/components/page-pages/index.js | 23 ++++------- .../src/components/page-patterns/index.js | 34 ++++------------- .../src/components/page-templates/index.js | 20 +++------- .../src/components/post-actions/actions.js | 17 +-------- 5 files changed, 61 insertions(+), 71 deletions(-) create mode 100644 packages/edit-site/src/components/dataviews-actions/index.js diff --git a/packages/edit-site/src/components/dataviews-actions/index.js b/packages/edit-site/src/components/dataviews-actions/index.js new file mode 100644 index 00000000000000..ed6522995d3b7b --- /dev/null +++ b/packages/edit-site/src/components/dataviews-actions/index.js @@ -0,0 +1,38 @@ +/** + * WordPress dependencies + */ +import { __ } from '@wordpress/i18n'; +import { edit } from '@wordpress/icons'; +import { useMemo } from '@wordpress/element'; +import { privateApis as routerPrivateApis } from '@wordpress/router'; + +/** + * Internal dependencies + */ +import { unlock } from '../../lock-unlock'; + +const { useHistory } = unlock( routerPrivateApis ); + +export const useEditPostAction = () => { + const history = useHistory(); + return useMemo( + () => ( { + id: 'edit-post', + label: __( 'Edit' ), + isPrimary: true, + icon: edit, + isEligible( { status } ) { + return status !== 'trash'; + }, + callback( items ) { + const post = items[ 0 ]; + history.push( { + postId: post.id, + postType: post.type, + canvas: 'edit', + } ); + }, + } ), + [ history ] + ); +}; diff --git a/packages/edit-site/src/components/page-pages/index.js b/packages/edit-site/src/components/page-pages/index.js index 60f1c6749d4743..dd270315387916 100644 --- a/packages/edit-site/src/components/page-pages/index.js +++ b/packages/edit-site/src/components/page-pages/index.js @@ -32,11 +32,10 @@ import { import AddNewPageModal from '../add-new-page'; import Media from '../media'; import { unlock } from '../../lock-unlock'; +import { useEditPostAction } from '../dataviews-actions'; const { usePostActions } = unlock( editorPrivateApis ); - const { useLocation, useHistory } = unlock( routerPrivateApis ); - const EMPTY_ARRAY = []; function useView( postType ) { @@ -337,20 +336,14 @@ export default function PagePages() { ], [ authors, view.type ] ); - const onActionPerformed = useCallback( - ( actionId, items ) => { - if ( actionId === 'edit-post' ) { - const post = items[ 0 ]; - history.push( { - postId: post.id, - postType: post.type, - canvas: 'edit', - } ); - } - }, - [ history ] + + const postTypActions = usePostActions( 'page' ); + const editAction = useEditPostAction(); + const actions = useMemo( + () => [ editAction, ...postTypActions ], + [ postTypActions, editAction ] ); - const actions = usePostActions( 'page', onActionPerformed ); + const onChangeView = useCallback( ( newView ) => { if ( newView.type !== view.type ) { diff --git a/packages/edit-site/src/components/page-patterns/index.js b/packages/edit-site/src/components/page-patterns/index.js index 4299a67a14c2fa..724f60ba391034 100644 --- a/packages/edit-site/src/components/page-patterns/index.js +++ b/packages/edit-site/src/components/page-patterns/index.js @@ -59,12 +59,13 @@ import usePatterns from './use-patterns'; import PatternsHeader from './header'; import { useLink } from '../routes/link'; import { useAddedBy } from '../page-templates/hooks'; +import { useEditPostAction } from '../dataviews-actions'; const { ExperimentalBlockEditorProvider, useGlobalStyle } = unlock( blockEditorPrivateApis ); const { usePostActions } = unlock( editorPrivateApis ); -const { useHistory, useLocation } = unlock( routerPrivateApis ); +const { useLocation } = unlock( routerPrivateApis ); const EMPTY_ARRAY = []; const defaultConfigPerViewType = { @@ -374,34 +375,14 @@ export default function DataviewsPatterns() { return filterSortAndPaginate( patterns, viewWithoutFilters, fields ); }, [ patterns, view, fields, type ] ); - const history = useHistory(); - const onActionPerformed = useCallback( - ( actionId, items ) => { - if ( actionId === 'edit-post' ) { - const post = items[ 0 ]; - history.push( { - postId: post.id, - postType: post.type, - categoryId, - categoryType: type, - canvas: 'edit', - } ); - } - }, - [ history, categoryId, type ] - ); - const templatePartActions = usePostActions( - TEMPLATE_PART_POST_TYPE, - onActionPerformed - ); - const patternActions = usePostActions( - PATTERN_TYPES.user, - onActionPerformed - ); + const templatePartActions = usePostActions( TEMPLATE_PART_POST_TYPE ); + const patternActions = usePostActions( PATTERN_TYPES.user ); + const editAction = useEditPostAction(); const actions = useMemo( () => { if ( type === TEMPLATE_PART_POST_TYPE ) { return [ + editAction, ...templatePartActions, duplicateTemplatePartAction, resetAction, @@ -409,13 +390,14 @@ export default function DataviewsPatterns() { ].filter( Boolean ); } return [ + editAction, ...patternActions, duplicatePatternAction, exportJSONaction, resetAction, deleteAction, ].filter( Boolean ); - }, [ type, templatePartActions, patternActions ] ); + }, [ editAction, type, templatePartActions, patternActions ] ); const onChangeView = useCallback( ( newView ) => { if ( newView.type !== view.type ) { diff --git a/packages/edit-site/src/components/page-templates/index.js b/packages/edit-site/src/components/page-templates/index.js index ce8f4f6dfc1d94..1c86bf83bfbab6 100644 --- a/packages/edit-site/src/components/page-templates/index.js +++ b/packages/edit-site/src/components/page-templates/index.js @@ -43,6 +43,7 @@ import { import usePatternSettings from '../page-patterns/use-pattern-settings'; import { unlock } from '../../lock-unlock'; +import { useEditPostAction } from '../dataviews-actions'; const { usePostActions } = unlock( editorPrivateApis ); @@ -322,22 +323,13 @@ export default function PageTemplates() { return filterSortAndPaginate( records, view, fields ); }, [ records, view, fields ] ); - const onActionPerformed = useCallback( - ( actionId, items ) => { - if ( actionId === 'edit-post' ) { - const post = items[ 0 ]; - history.push( { - postId: post.id, - postType: post.type, - canvas: 'edit', - } ); - } - }, - [ history ] + const postTypActions = usePostActions( TEMPLATE_POST_TYPE ); + const editAction = useEditPostAction(); + const actions = useMemo( + () => [ editAction, ...postTypActions ], + [ postTypActions, editAction ] ); - const actions = usePostActions( TEMPLATE_POST_TYPE, onActionPerformed ); - const onChangeView = useCallback( ( newView ) => { if ( newView.type !== view.type ) { diff --git a/packages/editor/src/components/post-actions/actions.js b/packages/editor/src/components/post-actions/actions.js index c0035af3d2f76e..aeee1c165a0ea7 100644 --- a/packages/editor/src/components/post-actions/actions.js +++ b/packages/editor/src/components/post-actions/actions.js @@ -1,7 +1,7 @@ /** * WordPress dependencies */ -import { external, trash, edit, backup } from '@wordpress/icons'; +import { external, trash, backup } from '@wordpress/icons'; import { addQueryArgs } from '@wordpress/url'; import { useDispatch, useSelect } from '@wordpress/data'; import { decodeEntities } from '@wordpress/html-entities'; @@ -387,20 +387,6 @@ const viewPostAction = { }, }; -const editPostAction = { - id: 'edit-post', - label: __( 'Edit' ), - isPrimary: true, - icon: edit, - isEligible( { status } ) { - return status !== 'trash'; - }, - callback( posts, onActionPerformed ) { - if ( onActionPerformed ) { - onActionPerformed( posts ); - } - }, -}; const postRevisionsAction = { id: 'view-post-revisions', label: __( 'View revisions' ), @@ -912,7 +898,6 @@ export function usePostActions( postType, onActionPerformed ) { } const actions = [ - editPostAction, isTemplateOrTemplatePart && resetTemplateAction, postTypeObject?.viewable && viewPostAction, ! isTemplateOrTemplatePart && restorePostAction,