Skip to content

Add View button to navigation link blocks #70986

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

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Changes from 1 commit
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
43 changes: 41 additions & 2 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ import {
useBlockEditingMode,
} from '@wordpress/block-editor';
import { isURL, prependHTTP, safeDecodeURI } from '@wordpress/url';
import { useState, useEffect, useRef } from '@wordpress/element';
import { useState, useEffect, useRef, useCallback } from '@wordpress/element';
import { __unstableStripHTML as stripHTML } from '@wordpress/dom';
import { decodeEntities } from '@wordpress/html-entities';
import { link as linkIcon, addSubmenu } from '@wordpress/icons';
import { store as coreStore } from '@wordpress/core-data';
import { useMergeRefs, usePrevious } from '@wordpress/compose';
import { privateApis as routerPrivateApis } from '@wordpress/router';

/**
* Internal dependencies
Expand All @@ -44,6 +45,10 @@ import { LinkUI } from './link-ui';
import { updateAttributes } from './update-attributes';
import { getColors } from '../navigation/edit/utils';
import { useToolsPanelDropdownMenuProps } from '../utils/hooks';
import { unlock } from '../lock-unlock';

// Safely access useHistory - it may not be available in all contexts
const { useHistory } = routerPrivateApis ? unlock( routerPrivateApis ) : {};

const DEFAULT_BLOCK = { name: 'core/navigation-link' };
const NESTING_BLOCK_NAMES = [
Expand Down Expand Up @@ -425,7 +430,11 @@ export default function NavigationLinkEdit( {
__unstableMarkNextChangeAsNotPersistent();
transformToSubmenu();
}
}, [ hasChildren ] );
}, [
hasChildren,
__unstableMarkNextChangeAsNotPersistent,
transformToSubmenu,
] );

// If the LinkControl popover is open and the URL has changed, close the LinkControl and focus the label text.
useEffect( () => {
Expand Down Expand Up @@ -486,6 +495,24 @@ export default function NavigationLinkEdit( {
customBackgroundColor,
} = getColors( context, ! isTopLevelLink );

const history = useHistory();

const onViewPage = useCallback( () => {
if ( kind === 'post-type' && id && type ) {
if ( history ) {
// Site editor context
history.navigate( `/${ type }/${ id }?canvas=edit` );
} else {
// Post editor context - navigate to the post

window.open(
`/wp-admin/post.php?post=${ id }&action=edit`,
'_blank'
);
}
}
}, [ kind, id, type, history ] );

function onKeyDown( event ) {
if ( isKeyboardEvent.primary( event, 'k' ) ) {
// Required to prevent the command center from opening,
Expand Down Expand Up @@ -571,6 +598,18 @@ export default function NavigationLinkEdit( {
/>
) }
</ToolbarGroup>
{ /* View button for page-type links */ }
{ kind === 'post-type' && id && type && (
<ToolbarGroup>
<ToolbarButton
name="view"
title={ __( 'View' ) }
onClick={ onViewPage }
>
{ __( 'View' ) }
</ToolbarButton>
</ToolbarGroup>
) }
</BlockControls>
{ /* Warning, this duplicated in packages/block-library/src/navigation-submenu/edit.js */ }
<InspectorControls>
Expand Down
Loading