Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 7 additions & 0 deletions packages/block-editor/src/components/list-view/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { store as blockEditorStore } from '../../store';
import { BlockSettingsDropdown } from '../block-settings-menu/block-settings-dropdown';
import { BLOCK_LIST_ITEM_HEIGHT, focusListItem } from './utils';
import useClipboardHandler from './use-clipboard-handler';
import { ListViewContentPopover } from '../inspector-controls/list-view-content-popover';

const expansion = ( state, action ) => {
const { type, clientIds } = action;
Expand Down Expand Up @@ -87,6 +88,7 @@ const expansion = ( state, action ) => {
* @param {string} props.description Optional accessible description for the tree grid component.
* @param {?Function} props.onSelect Optional callback to be invoked when a block is selected. Receives the block object that was selected.
* @param {?ComponentType} props.additionalBlockContent Component that renders additional block content UI.
* @param {?boolean} props.showContentPopover Flag to render the selected block's content controls in a popover anchored to its row. Defaults to `false`.
* @param {Ref} ref Forwarded ref
*/
function ListViewComponent(
Expand All @@ -103,6 +105,7 @@ function ListViewComponent(
description,
onSelect,
additionalBlockContent: AdditionalBlockContent,
showContentPopover = false,
},
ref
) {
Expand Down Expand Up @@ -426,6 +429,9 @@ function ListViewComponent(
</ListViewInsertedBlockContext.Provider>
</ListViewContext.Provider>
</TreeGrid>
{ showContentPopover && (
<ListViewContentPopover listViewRef={ elementRef } />
) }
</AsyncModeProvider>
);
}
Expand All @@ -446,6 +452,7 @@ export default forwardRef( ( props, ref ) => {
onSelect={ null }
additionalBlockContent={ null }
blockSettingsMenu={ undefined }
showContentPopover={ false }
/>
);
} );
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ export default function NavigationMenuEditor( { navigationMenuId } ) {
return [];
}

return [ createBlock( 'core/navigation', { ref: navigationMenuId } ) ];
// Locking the menu to its content gives its items the same editing
// experience as content-only List View editing.
return [
createBlock( 'core/navigation', {
ref: navigationMenuId,
templateLock: 'contentOnly',

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Locking the menu to contentOnly seems to change more than the popover. Under a contentOnly lock, children without a content role get the disabled editing mode (getDerivedBlockEditingModesForTree in block-editor/src/store/reducer.ts), and canInsertBlockType rejects them. Navigation allows two of those: core/spacer and core/loginout.

From testing this branch:

  • The appender's inserter no longer shows Spacer blocks (it does on trunk)
  • On trunk, an existing Spacer block is shown in the sidebar

So a menu that has a Spacer or Login/out loses functionality in the sidebar. Could the popover routing work without the lock, for example, with a block editing mode override in the sidebar editor? Or is this trade-off intended?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the spacer is fine to leave out from this view, as it's a design element. I do think we should bring in the login/logout in a separate PR. That looks like it was mislabelled and should be considered content.

Looking into this more, we should also allow editing button block text/link from the content only sidebar. I think that's a separate PR as well. I'll have claude spin one up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, I think we should keep the contentOnly locked approach. It simplifies the code and reuses existing functionality, and fixes we improve on the contentOnly editing are automatically improved everywhere.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's a PR to bring Login/Logout block to the contentOnly list view: #84849

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds like a good plan, thanks!

} ),
];
}, [ navigationMenuId ] );

if ( ! navigationMenuId || ! blocks?.length ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import {
BlockList,
} from '@wordpress/block-editor';
import { useDispatch, useSelect } from '@wordpress/data';
import { createBlock } from '@wordpress/blocks';
import { useCallback } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';
import { privateApis as blockLibraryPrivateApis } from '@wordpress/block-library';
import { unlock } from '../../lock-unlock';
Expand Down Expand Up @@ -68,23 +66,8 @@ export default function NavigationMenuContent( { rootClientId } ) {
},
[ rootClientId ]
);
const { replaceBlock, __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );

const offCanvasOnselect = useCallback(
( block ) => {
if (
block.name === 'core/navigation-link' &&
! block.attributes.url
) {
__unstableMarkNextChangeAsNotPersistent();
replaceBlock(
block.clientId,
createBlock( 'core/navigation-link', block.attributes )
);
}
},
[ __unstableMarkNextChangeAsNotPersistent, replaceBlock ]
const { openListViewContentPanel } = unlock(
useDispatch( blockEditorStore )
);

// The hidden block is needed because it makes block edit side effects trigger.
Expand All @@ -94,10 +77,11 @@ export default function NavigationMenuContent( { rootClientId } ) {
{ ! isLoading && (
<PrivateListView
rootClientId={ listViewRootClientId }
onSelect={ offCanvasOnselect }
onSelect={ openListViewContentPanel }
blockSettingsMenu={ LeafMoreMenu }
showAppender
additionalBlockContent={ NavigationLinkUI }
showContentPopover
isExpanded
/>
) }
Expand Down
30 changes: 4 additions & 26 deletions routes/navigation-edit/editor/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,10 @@ import {
// @ts-expect-error - No type declarations available for @wordpress/block-editor
} from '@wordpress/block-editor';
import { useDispatch, useSelect } from '@wordpress/data';
import { createBlock } from '@wordpress/blocks';
import { useCallback } from '@wordpress/element';
import { store as coreStore } from '@wordpress/core-data';
import { unlock } from '@wordpress/routes-lock-unlock';
import LeafMoreMenu from './leaf-more-menu';

type Block = {
clientId: string;
name: string;
attributes: Record< string, unknown >;
};

const { PrivateListView } = unlock( blockEditorPrivateApis );

// Needs to be kept in sync with the query used at packages/block-library/src/page-list/edit.jsx.
Expand Down Expand Up @@ -88,23 +80,8 @@ export default function NavigationMenuContent( {
},
[ rootClientId ]
);
const { replaceBlock, __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );

const offCanvasOnselect = useCallback(
( block: Block ) => {
if (
block.name === 'core/navigation-link' &&
! block.attributes.url
) {
__unstableMarkNextChangeAsNotPersistent();
replaceBlock(
block.clientId,
createBlock( 'core/navigation-link', block.attributes )
);
}
},
[ __unstableMarkNextChangeAsNotPersistent, replaceBlock ]
const { openListViewContentPanel } = unlock(
useDispatch( blockEditorStore )
);

// The hidden block is needed because it makes block edit side effects trigger.
Expand All @@ -114,10 +91,11 @@ export default function NavigationMenuContent( {
{ ! isLoading && (
<PrivateListView
rootClientId={ listViewRootClientId }
onSelect={ offCanvasOnselect }
onSelect={ openListViewContentPanel }
blockSettingsMenu={ LeafMoreMenu }
showAppender
additionalBlockContent={ NavigationLinkUI }
showContentPopover
isExpanded
/>
) }
Expand Down
9 changes: 8 additions & 1 deletion routes/navigation-edit/editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ export default function NavigationMenuEditor( { id }: { id: number } ) {
return [];
}

return [ createBlock( 'core/navigation', { ref: id } ) ];
// Locking the menu to its content gives its items the same editing
// experience as content-only List View editing.
return [
createBlock( 'core/navigation', {
ref: id,
templateLock: 'contentOnly',
} ),
];
}, [ assetsReady, id ] );

// The link UI's search needs a suggestions fetcher, which the block
Expand Down
122 changes: 122 additions & 0 deletions test/e2e/specs/site-editor/navigation-sidebar-list-view.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,128 @@ test.describe( 'Navigation sidebar - list view editing', () => {
)
).toBeNull();
} );

test( 'can edit links from the settings popover', async ( {
Comment thread
jeryj marked this conversation as resolved.
Resolved
admin,
page,
requestUtils,
linkControl,
} ) => {
const createdMenu = await requestUtils.createNavigationMenu( {
title: 'Test Navigation Menu',
content:
'<!-- wp:navigation-link {"label":"Existing Item","type":"custom","url":"http://www.wordpress.org/","kind":"custom"} /-->' +
'<!-- wp:navigation-submenu {"label":"Submenu Item","type":"custom","url":"http://www.wordpress.org/submenu/","kind":"custom"} -->' +
'<!-- wp:navigation-link {"label":"Child Item","type":"custom","url":"http://www.wordpress.org/child/","kind":"custom"} /-->' +
'<!-- /wp:navigation-submenu -->' +
'<!-- wp:navigation-link {"label":"No URL"} /-->',
} );

await admin.visitSiteEditor( {
postId: createdMenu?.id,
postType: 'wp_navigation',
} );

const listView = page.getByRole( 'treegrid', {
name: 'Block navigation structure',
} );
const labelInput = page.getByRole( 'textbox', { name: 'Text' } );

await test.step( 'opens when selecting a link', async () => {
await listView
.getByRole( 'link', { name: 'Existing Item' } )
.click();

await expect(
page.getByRole( 'heading', { name: 'Settings' } )
).toBeVisible();
await expect( labelInput ).toHaveValue( 'Existing Item' );
await expect(
page.getByRole( 'checkbox', { name: 'Open in new tab' } )
).toBeVisible();
await expect(
page.getByRole( 'link', { name: 'View' } )
).toBeVisible();
// Matches the content-only popover: less common fields are
// available from the panel menu rather than shown by default.
await expect(
page.getByRole( 'textbox', { name: 'Description' } )
).toBeHidden();
await expect(
page.getByRole( 'textbox', { name: 'Rel attribute' } )
).toBeHidden();
} );

await test.step( 'edits the link label', async () => {
await labelInput.fill( 'Changed Label' );

await expect(
listView.getByRole( 'link', { name: 'Changed Label' } )
).toBeVisible();
await expect(
listView.getByRole( 'link', { name: 'Existing Item' } )
).toBeHidden();
} );

await test.step( 'closes with Escape and returns focus to the item', async () => {
await labelInput.focus();
await page.keyboard.press( 'Escape' );

await expect( labelInput ).toBeHidden();
await expect(
listView.getByRole( 'link', { name: 'Changed Label' } )
).toBeFocused();
} );

await test.step( 'opens with Enter from the keyboard', async () => {
// Focus is on the edited item's row after Escape.
await page.keyboard.press( 'ArrowDown' );
await expect(
listView.getByRole( 'link', { name: 'Submenu Item' } )
).toBeFocused();
await page.keyboard.press( 'Enter' );

await expect( labelInput ).toHaveValue( 'Submenu Item' );
} );

await test.step( 'edits a submenu label', async () => {
await labelInput.fill( 'Changed Submenu' );

await expect(
listView.getByRole( 'link', { name: 'Changed Submenu' } )
).toBeVisible();
} );

await test.step( 'switches to another item while the popover is open', async () => {
// Hold the click as a person does. The popover's focus-outside check
// runs between mouse down and up, before the row's click opens the
// popover for the new item. An instant click would run it afterward.
await listView
.getByRole( 'link', { name: 'No URL' } )
.click( { delay: 100 } );

await expect( labelInput ).toHaveValue( 'No URL' );
} );

await test.step( 'adds a link to an item that has no URL', async () => {
// The item is kept; the popover offers to add its link.
await expect(
listView.getByRole( 'link', { name: 'No URL' } )
).toBeVisible();
const linkPicker = page.getByRole( 'button', {
name: /^Link to/,
} );
await expect( linkPicker ).toHaveText( /Add link/ );
await linkPicker.click();
await linkControl.useLinkControlSearch( 'Test Page 1' );

// The item keeps its label and gains the link.
await expect( linkPicker ).toHaveText( /Test Page 1/ );
await expect(
listView.getByRole( 'link', { name: 'No URL' } )
).toBeVisible();
} );
} );
} );

class LinkControl {
Expand Down
Loading