Skip to content

Commit 7f66d08

Browse files
committed
Refactor View button to use editor hook pattern
- Move View button implementation from navigation-link block to editor hook - Create navigation-link-view-button.js hook following pattern-overrides.js pattern - Hook targets core/navigation-link and core/navigation-submenu blocks - Use __unstableBlockToolbarLastItem to position button as last item - Remove router dependency issues by using editor hook context - Register hook in editor hooks index
1 parent 355f423 commit 7f66d08

File tree

3 files changed

+75
-39
lines changed

3 files changed

+75
-39
lines changed

packages/block-library/src/navigation-link/edit.js

Lines changed: 1 addition & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,14 @@ import {
2828
getColorClassName,
2929
useInnerBlocksProps,
3030
useBlockEditingMode,
31-
__unstableBlockToolbarLastItem as BlockToolbarLastItem,
3231
} from '@wordpress/block-editor';
3332
import { isURL, prependHTTP, safeDecodeURI } from '@wordpress/url';
34-
import { useState, useEffect, useRef, useCallback } from '@wordpress/element';
33+
import { useState, useEffect, useRef } from '@wordpress/element';
3534
import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
3635
import { decodeEntities } from '@wordpress/html-entities';
3736
import { link as linkIcon, addSubmenu } from '@wordpress/icons';
3837
import { store as coreStore } from '@wordpress/core-data';
3938
import { useMergeRefs, usePrevious } from '@wordpress/compose';
40-
import { privateApis as routerPrivateApis } from '@wordpress/router';
4139

4240
/**
4341
* Internal dependencies
@@ -46,10 +44,6 @@ import { LinkUI } from './link-ui';
4644
import { updateAttributes } from './update-attributes';
4745
import { getColors } from '../navigation/edit/utils';
4846
import { useToolsPanelDropdownMenuProps } from '../utils/hooks';
49-
import { unlock } from '../lock-unlock';
50-
51-
// Safely access useHistory - it may not be available in all contexts
52-
const { useHistory } = routerPrivateApis ? unlock( routerPrivateApis ) : {};
5347

5448
const DEFAULT_BLOCK = { name: 'core/navigation-link' };
5549
const NESTING_BLOCK_NAMES = [
@@ -492,24 +486,6 @@ export default function NavigationLinkEdit( {
492486
customBackgroundColor,
493487
} = getColors( context, ! isTopLevelLink );
494488

495-
const history = useHistory();
496-
497-
const onViewPage = useCallback( () => {
498-
if ( kind === 'post-type' && id && type ) {
499-
if ( history ) {
500-
// Site editor context
501-
history.navigate( `/${ type }/${ id }?canvas=edit` );
502-
} else {
503-
// Post editor context - navigate to the post
504-
505-
window.open(
506-
`/wp-admin/post.php?post=${ id }&action=edit`,
507-
'_blank'
508-
);
509-
}
510-
}
511-
}, [ kind, id, type, history ] );
512-
513489
function onKeyDown( event ) {
514490
if ( isKeyboardEvent.primary( event, 'k' ) ) {
515491
// Required to prevent the command center from opening,
@@ -595,20 +571,6 @@ export default function NavigationLinkEdit( {
595571
/>
596572
) }
597573
</ToolbarGroup>
598-
{ /* View button for page-type links */ }
599-
{ kind === 'post-type' && id && type && (
600-
<BlockToolbarLastItem>
601-
<ToolbarGroup>
602-
<ToolbarButton
603-
name="view"
604-
title={ __( 'View' ) }
605-
onClick={ onViewPage }
606-
>
607-
{ __( 'View' ) }
608-
</ToolbarButton>
609-
</ToolbarGroup>
610-
</BlockToolbarLastItem>
611-
) }
612574
</BlockControls>
613575
{ /* Warning, this duplicated in packages/block-library/src/navigation-submenu/edit.js */ }
614576
<InspectorControls>

packages/editor/src/hooks/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ import './custom-sources-backwards-compatibility';
55
import './default-autocompleters';
66
import './media-upload';
77
import './pattern-overrides';
8+
import './navigation-link-view-button';
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* WordPress dependencies
3+
*/
4+
import { addFilter } from '@wordpress/hooks';
5+
import { createHigherOrderComponent } from '@wordpress/compose';
6+
import { useCallback } from '@wordpress/element';
7+
import { __ } from '@wordpress/i18n';
8+
import { __unstableBlockToolbarLastItem as BlockToolbarLastItem } from '@wordpress/block-editor';
9+
import { ToolbarButton, ToolbarGroup } from '@wordpress/components';
10+
11+
// Target blocks that should have the View button
12+
const SUPPORTED_BLOCKS = [ 'core/navigation-link', 'core/navigation-submenu' ];
13+
14+
/**
15+
* Component that renders the View button for navigation blocks
16+
*
17+
* @param {Object} props Component props.
18+
* @param {Object} props.attributes Block attributes.
19+
*/
20+
function NavigationViewButton( { attributes } ) {
21+
const { kind, id, type } = attributes;
22+
23+
const onViewPage = useCallback( () => {
24+
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' );
28+
}
29+
}, [ kind, id, type ] );
30+
31+
// Only show for page-type links
32+
if ( kind !== 'post-type' || ! id || ! type ) {
33+
return null;
34+
}
35+
36+
return (
37+
<BlockToolbarLastItem>
38+
<ToolbarGroup>
39+
<ToolbarButton
40+
name="view"
41+
title={ __( 'View' ) }
42+
onClick={ onViewPage }
43+
>
44+
{ __( 'View' ) }
45+
</ToolbarButton>
46+
</ToolbarGroup>
47+
</BlockToolbarLastItem>
48+
);
49+
}
50+
51+
/**
52+
* Higher-order component that adds the View button to navigation blocks
53+
*/
54+
const withNavigationViewButton = createHigherOrderComponent(
55+
( BlockEdit ) => ( props ) => {
56+
const isSupportedBlock = SUPPORTED_BLOCKS.includes( props.name );
57+
58+
return (
59+
<>
60+
<BlockEdit key="edit" { ...props } />
61+
{ isSupportedBlock && <NavigationViewButton { ...props } /> }
62+
</>
63+
);
64+
},
65+
'withNavigationViewButton'
66+
);
67+
68+
// Register the filter
69+
addFilter(
70+
'editor.BlockEdit',
71+
'core/editor/with-navigation-view-button',
72+
withNavigationViewButton
73+
);

0 commit comments

Comments
 (0)