Skip to content

Commit 4255fe3

Browse files
committed
Use onNavigateToEntityRecord for View button navigation
- Replace router-based navigation with editor's onNavigateToEntityRecord - Add conditional focusMode support to onNavigateToEntityRecord - Disable focusMode for View button navigation for better UX - Maintain backwards compatibility for existing usage - Only show View button when block is selected
1 parent 7f66d08 commit 4255fe3

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

packages/edit-site/src/components/block-editor/use-navigate-to-entity-record.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,17 @@ export default function useNavigateToEntityRecord() {
1616

1717
const onNavigateToEntityRecord = useCallback(
1818
( params ) => {
19+
const { focusMode = true, ...navigationParams } = params;
20+
const queryParams = [ 'canvas=edit' ];
21+
22+
if ( focusMode ) {
23+
queryParams.push( 'focusMode=true' );
24+
}
25+
1926
history.navigate(
20-
`/${ params.postType }/${ params.postId }?canvas=edit&focusMode=true`
27+
`/${ navigationParams.postType }/${
28+
navigationParams.postId
29+
}?${ queryParams.join( '&' ) }`
2130
);
2231
},
2332
[ history ]

packages/editor/src/hooks/navigation-link-view-button.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ import { addFilter } from '@wordpress/hooks';
55
import { createHigherOrderComponent } from '@wordpress/compose';
66
import { useCallback } from '@wordpress/element';
77
import { __ } from '@wordpress/i18n';
8-
import { __unstableBlockToolbarLastItem as BlockToolbarLastItem } from '@wordpress/block-editor';
8+
import {
9+
__unstableBlockToolbarLastItem as BlockToolbarLastItem,
10+
store as blockEditorStore,
11+
} from '@wordpress/block-editor';
912
import { ToolbarButton, ToolbarGroup } from '@wordpress/components';
13+
import { useSelect } from '@wordpress/data';
1014

1115
// Target blocks that should have the View button
1216
const SUPPORTED_BLOCKS = [ 'core/navigation-link', 'core/navigation-submenu' ];
@@ -20,13 +24,21 @@ const SUPPORTED_BLOCKS = [ 'core/navigation-link', 'core/navigation-submenu' ];
2024
function NavigationViewButton( { attributes } ) {
2125
const { kind, id, type } = attributes;
2226

27+
const onNavigateToEntityRecord = useSelect(
28+
( select ) =>
29+
select( blockEditorStore ).getSettings().onNavigateToEntityRecord,
30+
[]
31+
);
32+
2333
const onViewPage = useCallback( () => {
2434
if ( kind === 'post-type' && id && type ) {
25-
// Navigate to the post in edit mode
26-
const url = `/wp-admin/post.php?post=${ id }&action=edit`;
27-
window.open( url, '_blank' );
35+
onNavigateToEntityRecord( {
36+
postId: id,
37+
postType: type,
38+
focusMode: false,
39+
} );
2840
}
29-
}, [ kind, id, type ] );
41+
}, [ kind, id, type, onNavigateToEntityRecord ] );
3042

3143
// Only show for page-type links
3244
if ( kind !== 'post-type' || ! id || ! type ) {
@@ -58,7 +70,9 @@ const withNavigationViewButton = createHigherOrderComponent(
5870
return (
5971
<>
6072
<BlockEdit key="edit" { ...props } />
61-
{ isSupportedBlock && <NavigationViewButton { ...props } /> }
73+
{ props.isSelected && isSupportedBlock && (
74+
<NavigationViewButton { ...props } />
75+
) }
6276
</>
6377
);
6478
},

0 commit comments

Comments
 (0)