Skip to content

Commit

Permalink
Move the edit action into the site editor
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed May 9, 2024
1 parent 0e1da0b commit f034822
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 71 deletions.
38 changes: 38 additions & 0 deletions packages/edit-site/src/components/dataviews-actions/index.js
Original file line number Diff line number Diff line change
@@ -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 ]
);
};
23 changes: 8 additions & 15 deletions packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down Expand Up @@ -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 ) {
Expand Down
34 changes: 8 additions & 26 deletions packages/edit-site/src/components/page-patterns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -374,48 +375,29 @@ 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,
deleteAction,
].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 ) {
Expand Down
20 changes: 6 additions & 14 deletions packages/edit-site/src/components/page-templates/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

Expand Down Expand Up @@ -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 ) {
Expand Down
17 changes: 1 addition & 16 deletions packages/editor/src/components/post-actions/actions.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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' ),
Expand Down Expand Up @@ -912,7 +898,6 @@ export function usePostActions( postType, onActionPerformed ) {
}

const actions = [
editPostAction,
isTemplateOrTemplatePart && resetTemplateAction,
postTypeObject?.viewable && viewPostAction,
! isTemplateOrTemplatePart && restorePostAction,
Expand Down

0 comments on commit f034822

Please sign in to comment.