const { test, expect } = require( '@wordpress/e2e-test-utils-playwright' ); test.describe( 'List View', () => { test.use( { listViewUtils: async ( { page, pageUtils, editor }, use ) => { await use( new ListViewUtils( { page, pageUtils, editor } ) ); }, } ); test.beforeEach( async ( { admin } ) => { await admin.createNewPost(); } ); test( 'allows a user to drag a block to a new sibling position', async ( { editor, page, pageUtils, } ) => { // Insert a couple of blocks of different types. await editor.insertBlock( { name: 'core/heading' } ); await editor.insertBlock( { name: 'core/image' } ); await editor.insertBlock( { name: 'core/paragraph' } ); // Open List View. await pageUtils.pressKeys( 'access+o' ); const listView = page.getByRole( 'treegrid', { name: 'Block navigation structure', } ); // The last inserted block should be selected. await expect( listView.getByRole( 'gridcell', { name: 'Paragraph', exact: true, selected: true, } ) ).toBeVisible(); // Ensure the setup is correct before dragging. await expect .poll( editor.getBlocks ) .toMatchObject( [ { name: 'core/heading' }, { name: 'core/image' }, { name: 'core/paragraph' }, ] ); // Drag the paragraph above the heading. const paragraphBlockItem = listView.getByRole( 'gridcell', { name: 'Paragraph', exact: true, } ); const imageBlockItem = listView.getByRole( 'gridcell', { name: 'Image', exact: true, } ); const headingBlockItem = listView.getByRole( 'gridcell', { name: 'Heading 2', exact: true, } ); await paragraphBlockItem.hover(); await page.mouse.down(); // To work around a drag and drop bug in Safari, the list view applies // `pointer-events: none` to the list view while dragging, so that // `onDragLeave` is not fired when dragging within the list view. // Without the `force: true` option, the `hover` action will fail // as playwright will complain that pointer-events are intercepted. // https://bugs.webkit.org/show_bug.cgi?id=66547 // See: https://github.com/WordPress/gutenberg/pull/56625 // Hover over each block to mimic moving up the list view. // Also, hover twice to ensure a dragover event is dispatched. // See: https://playwright.dev/docs/input#dragging-manually await imageBlockItem.hover( { force: true } ); await imageBlockItem.hover( { force: true } ); await headingBlockItem.hover( { force: true } ); await headingBlockItem.hover( { force: true } ); // Disable reason: Need to wait until the throttle timeout of 250ms has passed. /* eslint-disable playwright/no-wait-for-timeout */ await editor.page.waitForTimeout( 300 ); /* eslint-enable playwright/no-wait-for-timeout */ await page.mouse.up(); // Ensure the block was dropped correctly. await expect .poll( editor.getBlocks ) .toMatchObject( [ { name: 'core/paragraph' }, { name: 'core/heading' }, { name: 'core/image' }, ] ); } ); test( "nests a block dragged downward over the next block's indentation", async ( { editor, page, pageUtils, listViewUtils, } ) => { for ( const name of [ 'Group A', 'Group B', 'Group C' ] ) { await editor.insertBlock( { name: 'core/group', attributes: { metadata: { name } }, } ); } // Open List View. await pageUtils.pressKeys( 'access+o' ); const listView = page.getByRole( 'treegrid', { name: 'Block navigation structure', } ); // Drag Group A down over the bottom half of Group B, one indentation // level to the right: a nesting gesture. Dragging downward displaces // the target row while the drop indicator shows, which must not cancel // the nesting. await listViewUtils.dragBlockTo( { from: listView.getByRole( 'gridcell', { name: 'Group A', exact: true, } ), to: listView.getByRole( 'gridcell', { name: 'Group B', exact: true, } ), indentation: 2, } ); await expect.poll( editor.getBlocks ).toMatchObject( [ { name: 'core/group', attributes: { metadata: { name: 'Group B' } }, innerBlocks: [ { name: 'core/group', attributes: { metadata: { name: 'Group A' } }, }, ], }, { name: 'core/group', attributes: { metadata: { name: 'Group C' } }, }, ] ); } ); // Check for regressions of https://github.com/WordPress/gutenberg/issues/38763. test( 'shows the correct amount of blocks after a block is removed in the canvas', async ( { editor, page, pageUtils, } ) => { // Insert a couple of blocks of different types. await editor.insertBlock( { name: 'core/image' } ); await editor.insertBlock( { name: 'core/heading' } ); await editor.insertBlock( { name: 'core/paragraph' } ); // Open List View. await pageUtils.pressKeys( 'access+o' ); const listView = page.getByRole( 'treegrid', { name: 'Block navigation structure', } ); // The last inserted block should be selected. await expect( listView.getByRole( 'gridcell', { name: 'Paragraph', exact: true, selected: true, } ) ).toBeVisible(); // Go to the image block in List View. await pageUtils.pressKeys( 'ArrowUp', { times: 2 } ); await expect( listView.getByRole( 'link', { name: 'Image', } ) ).toBeFocused(); // Select the image block in the canvas. await page.keyboard.press( 'Enter' ); const imageBlock = editor.canvas.getByRole( 'document', { name: 'Block: Image', } ); await expect( imageBlock.getByRole( 'button', { name: 'Upload' } ) ).toBeFocused(); await page.keyboard.press( 'ArrowUp' ); await expect( imageBlock ).toBeFocused(); // Delete the image block in the canvas. await page.keyboard.press( 'Backspace' ); // List View should have two rows. await expect( listView.getByRole( 'row' ) ).toHaveCount( 2 ); } ); test( 'expands and collapses nested list items', async ( { editor, page, pageUtils, } ) => { await editor.insertBlock( { name: 'core/cover' } ); await editor.insertBlock( { name: 'core/group' } ); // Click first color option from the block placeholder's color picker to // make the inner blocks appear. await editor.canvas .getByRole( 'document', { name: 'Block: Cover' } ) .getByRole( 'group', { name: 'Overlay color', } ) .getByRole( 'button' ) .first() .click(); // Open List View. await pageUtils.pressKeys( 'access+o' ); const listView = page.getByRole( 'treegrid', { name: 'Block navigation structure', } ); // Things start off expanded. await expect( listView.getByRole( 'link', { name: 'Cover', expanded: true, } ) ).toBeVisible(); // The child paragraph block should be selected. await expect( listView.getByRole( 'gridcell', { name: 'Paragraph', exact: true, selected: true, } ) ).toBeVisible(); // Collapse the Cover block. await listView .getByRole( 'gridcell', { name: 'Cover', exact: true } ) .getByTestId( 'list-view-expander', { includeHidden: true } ) // Force the click to bypass the visibility check. The expander is // intentionally aria-hidden. See the implementation for details. .click( { force: true } ); // Check that blocks are collapsed: // 2 blocks: (one Cover block, one Group block). await expect( listView.getByRole( 'row' ) ).toHaveCount( 2 ); // Click the Cover block List View item. await listView .getByRole( 'link', { name: 'Cover', expanded: false, } ) .click(); // Click the Cover block title placeholder. await editor.canvas .getByRole( 'document', { name: 'Block: Cover' } ) .getByRole( 'document', { name: /Empty block/i } ) .click(); // The child paragraph block in List View should be selected. await expect( listView.getByRole( 'gridcell', { name: 'Paragraph', exact: true, selected: true, } ) ).toBeVisible(); // Check that blocks are expanded: // 3 blocks: (one Cover block containing a Paragraph block, one Group block). await expect( listView.getByRole( 'row' ) ).toHaveCount( 3 ); await listView .getByRole( 'gridcell', { name: 'Paragraph', exact: true } ) .click(); // Move down to the Group block. await page.keyboard.press( 'ArrowDown' ); // Collapse all but the Group block. await pageUtils.pressKeys( 'alt+l' ); // Check that the Cover block is collapsed. await expect( listView.getByRole( 'link', { name: 'Cover', expanded: false, } ) ).toBeVisible(); // Check that blocks are collapsed: // 2 blocks: (one Cover block, one Group block). await expect( listView.getByRole( 'row' ) ).toHaveCount( 2 ); } ); test( 'moves focus to start/end of list with Home/End keys', async ( { editor, page, pageUtils, } ) => { // Insert a couple of blocks of different types. await editor.insertBlock( { name: 'core/image' } ); await editor.insertBlock( { name: 'core/heading' } ); await editor.insertBlock( { name: 'core/paragraph' } ); await editor.insertBlock( { name: 'core/columns' } ); await editor.insertBlock( { name: 'core/group' } ); // Open List View. await pageUtils.pressKeys( 'access+o' ); const listView = page.getByRole( 'treegrid', { name: 'Block navigation structure', } ); // The last inserted block should be selected. await expect( listView.getByRole( 'gridcell', { name: 'Group', exact: true, selected: true, } ) ).toBeVisible(); // Press Home to go to the first inserted block (image). await page.keyboard.press( 'Home' ); await expect( listView.getByRole( 'link', { name: 'Image', } ) ).toBeFocused(); // Press End followed by Arrow Up to go to the second to last block (columns). await page.keyboard.press( 'End' ); await page.keyboard.press( 'ArrowUp' ); await expect( listView.getByRole( 'link', { name: 'Columns', exact: true, } ) ).toBeFocused(); // Navigate the right column to image block options button via Home key. await page.keyboard.press( 'ArrowRight' ); await page.keyboard.press( 'Home' ); const imageItem = listView.getByRole( 'gridcell', { name: 'Image', } ); await expect( imageItem .locator( '..' ) // parent selector. .getByRole( 'button', { name: 'Options', } ) ).toBeFocused(); // Navigate the right column to group block options button. await page.keyboard.press( 'End' ); const groupItem = listView.getByRole( 'gridcell', { name: 'Group', } ); await expect( groupItem .locator( '..' ) // parent selector. .getByRole( 'button', { name: 'Options', } ) ).toBeFocused(); } ); // If list view sidebar is open and focus is not inside the sidebar, move // focus to the sidebar when using the shortcut. If focus is inside the // sidebar, shortcut should close the sidebar. test( 'ensures List View global shortcut works properly', async ( { editor, page, pageUtils, } ) => { await editor.insertBlock( { name: 'core/image' } ); await editor.insertBlock( { name: 'core/paragraph', attributes: { content: 'Paragraph text' }, } ); await expect .poll( () => editor.ownsSelection( editor.canvas.getByRole( 'document', { name: 'Block: Paragraph', } ) ) ) .toBe( true ); // Open List View. await pageUtils.pressKeys( 'access+o' ); const listView = page.getByRole( 'treegrid', { name: 'Block navigation structure', } ); // The paragraph item should be selected and focused. await expect( listView.getByRole( 'gridcell', { name: 'Paragraph', exact: true, selected: true, } ) ).toBeVisible(); await expect( listView.getByRole( 'link', { name: 'Paragraph' } ) ).toBeFocused(); // Navigate to the image block item. await page.keyboard.press( 'ArrowUp' ); const imageItem = listView.getByRole( 'link', { name: 'Image', } ); await expect( imageItem ).toBeFocused(); // Hit enter to focus the Image block. await page.keyboard.press( 'Enter' ); await expect( editor.canvas .getByRole( 'document', { name: 'Block: Image', } ) .getByRole( 'button', { name: 'Upload' } ) ).toBeFocused(); // Since focus is now at the image block upload button in the canvas, // pressing the list view shortcut should bring focus back to the image // block in the list view. await pageUtils.pressKeys( 'access+o' ); await expect( imageItem ).toBeFocused(); // Since focus is now inside the list view, the shortcut should close // the sidebar. await pageUtils.pressKeys( 'access+o' ); // Focus should now be on the list view toggle button. await expect( page.getByRole( 'button', { name: 'Document Overview' } ) ).toBeFocused(); // List View should be closed. await expect( listView ).toBeHidden(); // Open List View. await pageUtils.pressKeys( 'access+o' ); await expect( imageItem ).toBeFocused(); // Focus the list view close button and make sure the shortcut will // close the list view. This is to catch a bug where elements could be // out of range of the sidebar region. Must shift+tab 2 times to reach // close button before tab panel. await pageUtils.pressKeys( 'shift+Tab' ); await pageUtils.pressKeys( 'shift+Tab' ); await expect( page .getByRole( 'region', { name: 'Document Overview' } ) .getByRole( 'button', { name: 'Close', } ) ).toBeFocused(); // Close List View and ensure it's closed. await pageUtils.pressKeys( 'access+o' ); await expect( listView ).toBeHidden(); // Open List View. await pageUtils.pressKeys( 'access+o' ); await expect( imageItem ).toBeFocused(); // Focus the outline tab and select it. This test ensures the outline // tab receives similar focus events based on the shortcut. await pageUtils.pressKeys( 'shift+Tab' ); await page.keyboard.press( 'ArrowRight' ); const outlineButton = page.getByRole( 'tab', { name: 'Outline', } ); await expect( outlineButton ).toBeFocused(); await page.keyboard.press( 'Enter' ); // From here, tab in to the editor so focus can be checked on return to // the outline tab in the sidebar. await pageUtils.pressKeys( 'Tab', { times: 2 } ); // Focus should be placed on the outline tab button since there is // nothing to focus inside the tab itself. await pageUtils.pressKeys( 'access+o' ); await expect( outlineButton ).toBeFocused(); // Close List View and ensure it's closed. await pageUtils.pressKeys( 'access+o' ); await expect( listView ).toBeHidden(); } ); test( 'should place focus on the currently selected block in the canvas', async ( { editor, page, pageUtils, } ) => { // Insert a couple of blocks of different types. await editor.insertBlock( { name: 'core/image' } ); await editor.insertBlock( { name: 'core/heading' } ); await editor.insertBlock( { name: 'core/paragraph' } ); // Open List View. await pageUtils.pressKeys( 'access+o' ); const listView = page.getByRole( 'treegrid', { name: 'Block navigation structure', } ); // The last inserted block should be selected. await expect( listView.getByRole( 'gridcell', { name: 'Paragraph', exact: true, selected: true, } ) ).toBeVisible(); // Go to the image block in List View. await pageUtils.pressKeys( 'ArrowUp', { times: 2 } ); await expect( listView.getByRole( 'link', { name: 'Image', } ) ).toBeFocused(); // Select the image block in the canvas. await page.keyboard.press( 'Enter' ); const imageBlock = editor.canvas.getByRole( 'document', { name: 'Block: Image', } ); await expect( imageBlock.getByRole( 'button', { name: 'Upload' } ) ).toBeFocused(); // Triggering the List View shortcut should result in the image block gaining focus. await pageUtils.pressKeys( 'access+o' ); await expect( listView.getByRole( 'link', { name: 'Image', } ) ).toBeFocused(); } ); test( 'should place focus on the first block when no block is selected', async ( { editor, page, pageUtils, } ) => { await editor.insertBlock( { name: 'core/heading', attributes: { content: 'First' }, } ); await editor.insertBlock( { name: 'core/paragraph' } ); // Clicking the title deselects the blocks. await editor.canvas .getByRole( 'textbox', { name: 'Add title' } ) .click(); // Open List View. await pageUtils.pressKeys( 'access+o' ); const listView = page.getByRole( 'treegrid', { name: 'Block navigation structure', } ); await expect( listView.getByRole( 'link', { name: 'First' } ) ).toBeFocused(); } ); test( 'should duplicate block using keyboard', async ( { editor, pageUtils, listViewUtils, } ) => { // Insert blocks of different types. await editor.insertBlock( { name: 'core/group' } ); await editor.insertBlock( { name: 'core/file' } ); await editor.insertBlock( { name: 'core/image' } ); // Open List View. const listView = await listViewUtils.openListView(); // Move focus and selection to the file block to set up for testing duplication. await listView .getByRole( 'gridcell', { name: 'File', exact: true } ) .dblclick(); // Test duplication behaviour. await pageUtils.pressKeys( 'primaryShift+d' ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'Duplicating a block should retain selection on existing block, move focus to duplicated block.' ) .toMatchObject( [ { name: 'core/group' }, { name: 'core/file', selected: true }, { name: 'core/file', focused: true }, { name: 'core/image' }, ] ); } ); test( 'should copy and paste blocks using keyboard', async ( { editor, page, pageUtils, listViewUtils, } ) => { // Insert blocks of different types. await editor.insertBlock( { name: 'core/heading' } ); await editor.insertBlock( { name: 'core/file' } ); await editor.insertBlock( { name: 'core/group', innerBlocks: [ { name: 'core/paragraph' }, { name: 'core/pullquote' }, ], } ); // Open List View. const listView = await listViewUtils.openListView(); // Click the newly inserted Group block List View item to ensure it is focused. await listView .getByRole( 'link', { name: 'Group', expanded: false, } ) .click(); // Move down to group block, expand, and then move to the paragraph block. await page.keyboard.press( 'ArrowDown' ); await page.keyboard.press( 'ArrowRight' ); await page.keyboard.press( 'ArrowDown' ); await page.keyboard.press( 'ArrowDown' ); await pageUtils.pressKeys( 'primary+c' ); await page.keyboard.press( 'ArrowUp' ); await pageUtils.pressKeys( 'primary+v' ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'Should be able to copy focused block and paste in the list view via keyboard shortcuts' ) .toMatchObject( [ { name: 'core/heading', selected: false, focused: false }, { name: 'core/file', selected: false, focused: false }, { name: 'core/group', selected: true, innerBlocks: [ { name: 'core/pullquote', selected: false, focused: true, }, { name: 'core/pullquote', selected: false, focused: false, }, ], }, ] ); } ); test( 'should paste block styles using keyboard', async ( { editor, page, pageUtils, listViewUtils, context, } ) => { // Grant clipboard access for copying and pasting styles between blocks. await context.grantPermissions( [ 'clipboard-read', 'clipboard-write', ] ); // Insert a block without styles. await editor.insertBlock( { name: 'core/paragraph', attributes: { content: 'First Block', }, } ); // Insert a block with styles. await editor.insertBlock( { name: 'core/paragraph', attributes: { content: 'Second Block', style: { color: { text: '#ff0000', background: '#00ff00', }, }, }, } ); // Copy the styles from the second block. await editor.clickBlockToolbarButton( 'Options' ); await page .getByRole( 'menu', { name: 'Options' } ) .getByRole( 'menuitem', { name: 'Copy Styles', } ) .click(); // Wait for the copy confirmation. It only shows up once the styles // have actually been written to the clipboard: the click on "Copy // Styles" resolves before the asynchronous clipboard write settles, // so pasting right away can read the clipboard before the styles // are in it. await expect( page .getByTestId( 'snackbar' ) .getByText( 'Styles copied to clipboard.' ) ).toBeVisible(); // Open List View. await listViewUtils.openListView(); // Navigate to the block without styles. await page.keyboard.press( 'ArrowUp' ); // Paste the copied styles. await pageUtils.pressKeys( 'primaryAlt+v' ); await expect.poll( editor.getBlocks ).toMatchObject( [ { name: 'core/paragraph', attributes: { content: 'First Block', style: { color: { text: '#ff0000', background: '#00ff00', }, }, }, }, { name: 'core/paragraph', attributes: { content: 'Second Block', style: { color: { text: '#ff0000', background: '#00ff00', }, }, }, }, ] ); } ); test( 'should cut and paste blocks using keyboard', async ( { editor, pageUtils, listViewUtils, } ) => { // Insert blocks of different types. await editor.insertBlock( { name: 'core/heading' } ); await editor.insertBlock( { name: 'core/file' } ); await editor.insertBlock( { name: 'core/group', innerBlocks: [ { name: 'core/pullquote' }, { name: 'core/pullquote' }, ], } ); // Open List View. const listView = await listViewUtils.openListView(); // Click the newly inserted Group block List View item to ensure it is focused. await listView .getByRole( 'link', { name: 'Group', expanded: false, } ) .click(); // Cut the block. await pageUtils.pressKeys( 'primary+x' ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'Should be able to cut a block in the list view, with the preceding block being selected' ) .toMatchObject( [ { name: 'core/heading', selected: false, focused: false }, { name: 'core/file', selected: true, focused: true }, ] ); await pageUtils.pressKeys( 'primary+v' ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'Should be able to paste previously cut block in the list view via keyboard shortcuts' ) .toMatchObject( [ { name: 'core/heading', selected: false, focused: false }, { name: 'core/group', selected: true, focused: true, innerBlocks: [ { name: 'core/pullquote', selected: false, focused: false, }, { name: 'core/pullquote', selected: false, focused: false, }, ], }, ] ); } ); test( 'should select blocks using keyboard', async ( { editor, page, pageUtils, listViewUtils, } ) => { // Insert some blocks of different types. await editor.insertBlock( { name: 'core/group', innerBlocks: [ { name: 'core/pullquote' } ], } ); await editor.insertBlock( { name: 'core/columns', innerBlocks: [ { name: 'core/column', innerBlocks: [ { name: 'core/heading' }, { name: 'core/paragraph' }, ], }, { name: 'core/column', innerBlocks: [ { name: 'core/verse' } ], }, ], } ); await editor.insertBlock( { name: 'core/file' } ); // Open List View. await listViewUtils.openListView(); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'The last inserted block should be selected and focused.' ) .toMatchObject( [ { name: 'core/group' }, { name: 'core/columns' }, { name: 'core/file', selected: true, focused: true }, ] ); // Move up to columns block, expand, and then move to the first column block. await page.keyboard.press( 'ArrowUp' ); await page.keyboard.press( 'ArrowRight' ); await page.keyboard.press( 'ArrowDown' ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'The last inserted block should be selected, while the first column block should be focused.' ) .toMatchObject( [ { name: 'core/group' }, { name: 'core/columns', innerBlocks: [ { name: 'core/column', selected: false, focused: true }, { name: 'core/column' }, ], }, { name: 'core/file', selected: true, focused: false }, ] ); // Select all sibling column blocks at current level. await pageUtils.pressKeys( 'primary+a' ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'All column blocks should be selected, with the first one focused.' ) .toMatchObject( [ { name: 'core/group', selected: false, focused: false }, { name: 'core/columns', innerBlocks: [ { name: 'core/column', selected: true, focused: true }, { name: 'core/column', selected: true, focused: false }, ], selected: false, }, { name: 'core/file', selected: false, focused: false }, ] ); // Select next parent (the columns block). await pageUtils.pressKeys( 'primary+a' ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'The columns block should be selected and focused.' ) .toMatchObject( [ { name: 'core/group', selected: false, focused: false }, { name: 'core/columns', innerBlocks: [ { name: 'core/column' }, { name: 'core/column' }, ], selected: true, focused: true, }, { name: 'core/file', selected: false, focused: false }, ] ); // Select all siblings at root level. await pageUtils.pressKeys( 'primary+a' ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'All blocks should be selected.' ) .toMatchObject( [ { name: 'core/group', selected: true, focused: false }, { name: 'core/columns', innerBlocks: [ { name: 'core/column' }, { name: 'core/column' }, ], selected: true, focused: true, }, { name: 'core/file', selected: true, focused: false }, ] ); } ); test( 'should deselect blocks using keyboard', async ( { editor, page, pageUtils, listViewUtils, } ) => { // Insert some blocks of different types. await editor.insertBlock( { name: 'core/group', innerBlocks: [ { name: 'core/pullquote' } ], } ); await editor.insertBlock( { name: 'core/columns', innerBlocks: [ { name: 'core/column', innerBlocks: [ { name: 'core/heading' }, { name: 'core/paragraph' }, ], }, { name: 'core/column', innerBlocks: [ { name: 'core/verse' } ], }, ], } ); await editor.insertBlock( { name: 'core/file' } ); // Open List View. const listView = await listViewUtils.openListView(); // Select all the blocks. await pageUtils.pressKeys( 'primary+a' ); // Deselect all blocks via Escape key. await page.keyboard.press( 'Escape' ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'All blocks should be deselected, with focus on the last inserted block.' ) .toMatchObject( [ { name: 'core/group', selected: false, focused: false }, { name: 'core/columns', selected: false, focused: false, }, { name: 'core/file', selected: false, focused: true }, ] ); // Select the columns block and focus the file block await listView .getByRole( 'gridcell', { name: 'Columns', exact: true } ) .click(); await listView .getByRole( 'gridcell', { name: 'File' } ) .getByRole( 'link' ) .focus(); // Deselect single block via Escape key. await page.keyboard.press( 'Escape' ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'Pressing Escape should deselect the Columns block' ) .toMatchObject( [ { name: 'core/group', selected: false, focused: false }, { name: 'core/columns', selected: false, focused: false, }, { name: 'core/file', selected: false, focused: true }, ] ); } ); test( 'should insert blocks using keyboard', async ( { editor, pageUtils, listViewUtils, } ) => { // Insert multiple blocks of different types. await editor.insertBlock( { name: 'core/group' } ); await editor.insertBlock( { name: 'core/file' } ); // Open List View. await listViewUtils.openListView(); // Test insert before. await pageUtils.pressKeys( 'primaryAlt+t' ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'Inserting a block before should move selection and focus to the inserted block.' ) .toMatchObject( [ { name: 'core/group', focused: false, selected: false }, { name: 'core/paragraph', focused: true, selected: true }, { name: 'core/file', selected: false }, ] ); // Test insert after. await pageUtils.pressKeys( 'primaryAlt+y' ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'Inserting a block after should move selection and focus to the inserted block.' ) .toMatchObject( [ { name: 'core/group', focused: false, selected: false }, { name: 'core/paragraph', focused: false, selected: false }, { name: 'core/paragraph', focused: true, selected: true }, { name: 'core/file', selected: false }, ] ); } ); test( 'should delete blocks using keyboard', async ( { editor, page, pageUtils, listViewUtils, } ) => { // Insert some blocks of different types. await editor.insertBlock( { name: 'core/group' } ); await editor.insertBlock( { name: 'core/columns', innerBlocks: [ { name: 'core/column', innerBlocks: [ { name: 'core/heading' }, { name: 'core/paragraph' }, ], }, { name: 'core/column', innerBlocks: [ { name: 'core/verse' } ], }, ], } ); await editor.insertBlock( { name: 'core/file' } ); await editor.insertBlock( { name: 'core/file' } ); // Open List View. const listView = await listViewUtils.openListView(); // Select the first file block. await listView .getByRole( 'gridcell', { name: 'File', exact: true } ) .first() .dblclick(); // Delete the first File block. await page.keyboard.press( 'Delete' ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'Deleting a block should move focus and selection to the previous block' ) .toMatchObject( [ { name: 'core/group' }, { name: 'core/columns', selected: true, focused: true }, { name: 'core/file' }, ] ); // Expand the current column. await page.keyboard.press( 'ArrowRight' ); await page.keyboard.press( 'ArrowDown' ); await page.keyboard.press( 'ArrowDown' ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'Move focus but do not select the second column' ) .toMatchObject( [ { name: 'core/group' }, { name: 'core/columns', selected: true, innerBlocks: [ { name: 'core/column' }, { name: 'core/column', focused: true }, ], }, { name: 'core/file' }, ] ); await page.keyboard.press( 'Delete' ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'Deleting a inner block moves focus to the previous inner block' ) .toMatchObject( [ { name: 'core/group' }, { name: 'core/columns', selected: true, innerBlocks: [ { name: 'core/column', selected: false, focused: true, }, ], }, { name: 'core/file' }, ] ); // Expand the current column. await page.keyboard.press( 'ArrowRight' ); // Move focus and select the Heading block. await listView .getByRole( 'gridcell', { name: 'Heading 2', exact: true } ) .dblclick(); // Select both inner blocks in the column. await page.keyboard.press( 'Shift+ArrowDown' ); await page.keyboard.press( 'Backspace' ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'Deleting multiple blocks moves focus to the parent block' ) .toMatchObject( [ { name: 'core/group' }, { name: 'core/columns', innerBlocks: [ { name: 'core/column', selected: true, focused: true, innerBlocks: [], }, ], }, { name: 'core/file' }, ] ); // Move focus and select the first block. await listView .getByRole( 'gridcell', { name: 'Group', exact: true } ) .dblclick(); await page.keyboard.press( 'Backspace' ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'Deleting the first block moves focus to the second block' ) .toMatchObject( [ { name: 'core/columns', selected: true, focused: true, }, { name: 'core/file' }, ] ); // Delete remaining blocks. // Keyboard shortcut should also work. await pageUtils.pressKeys( 'access+z' ); await pageUtils.pressKeys( 'access+z' ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'Deleting the only blocks left will create a default block and focus/select it' ) .toMatchObject( [ { name: 'core/paragraph', selected: true, focused: true, }, ] ); await editor.insertBlock( { name: 'core/heading' } ); await page.evaluate( () => window.wp.data.dispatch( 'core/block-editor' ).clearSelectedBlock() ); await listView .getByRole( 'gridcell', { name: 'Paragraph', exact: true } ) .getByRole( 'link' ) .focus(); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'Block selection is cleared and focus is on the paragraph block' ) .toMatchObject( [ { name: 'core/paragraph', selected: false, focused: true }, { name: 'core/heading', selected: false }, ] ); await pageUtils.pressKeys( 'access+z' ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'Deleting blocks without existing selection will not select blocks' ) .toMatchObject( [ { name: 'core/heading', selected: false, focused: true }, ] ); // Insert a block that is locked and cannot be removed. await editor.insertBlock( { name: 'core/file', attributes: { lock: { move: false, remove: true } }, } ); // Click on the Heading block to select it. await listView .getByRole( 'gridcell', { name: 'Heading 2', exact: true } ) .click(); await listView .getByRole( 'gridcell', { name: 'File' } ) .getByRole( 'link' ) .focus(); for ( const keys of [ 'Delete', 'Backspace', 'access+z' ] ) { await pageUtils.pressKeys( keys ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'Trying to delete locked blocks should not do anything' ) .toMatchObject( [ { name: 'core/heading', selected: true, focused: false }, { name: 'core/file', selected: false, focused: true }, ] ); } } ); test( 'should create a group block from the selected multiple blocks', async ( { editor, pageUtils, listViewUtils, } ) => { // Insert some blocks of different types. await editor.insertBlock( { name: 'core/paragraph' } ); await editor.insertBlock( { name: 'core/heading' } ); await editor.insertBlock( { name: 'core/file' } ); await listViewUtils.openListView(); // Group Heading and File blocks. await pageUtils.pressKeys( 'shift+ArrowUp' ); await pageUtils.pressKeys( 'primary+g' ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes ) .toMatchObject( [ { name: 'core/paragraph', selected: false, focused: false }, { name: 'core/group', selected: true, focused: true, innerBlocks: [ { name: 'core/heading', selected: false, focused: false, }, { name: 'core/file', selected: false, focused: false, }, ], }, ] ); } ); test( 'opens the block settings menu on right-click', async ( { editor, page, listViewUtils, } ) => { await editor.insertBlock( { name: 'core/heading' } ); await editor.insertBlock( { name: 'core/file' } ); const listView = await listViewUtils.openListView(); await listView .getByRole( 'link', { name: 'Heading' } ) .click( { button: 'right' } ); await expect( page .getByRole( 'menu', { name: 'Options' } ) .getByRole( 'menuitem', { name: 'Duplicate' } ) ).toBeVisible(); } ); test( 'block settings dropdown menu', async ( { editor, page, pageUtils, listViewUtils, } ) => { // Insert some blocks of different types. await editor.insertBlock( { name: 'core/heading' } ); await editor.insertBlock( { name: 'core/file' } ); // Open List View. const listView = await listViewUtils.openListView(); const headingRows = listView.getByRole( 'row' ).filter( { has: page.getByRole( 'gridcell', { name: 'Heading' } ), } ); await headingRows.hover(); await headingRows.getByRole( 'button', { name: 'Options' } ).click(); await page .getByRole( 'menu', { name: 'Options' } ) .getByRole( 'menuitem', { name: 'Duplicate' } ) .click(); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'Should duplicate a block and move focus' ) .toMatchObject( [ { name: 'core/heading', selected: false }, { name: 'core/heading', selected: false, focused: true }, { name: 'core/file', selected: true }, ] ); await page.keyboard.press( 'Shift+ArrowUp' ); await headingRows.first().hover(); await headingRows .first() .getByRole( 'button', { name: 'Options' } ) .click(); await page .getByRole( 'menu', { name: 'Options' } ) .getByRole( 'menuitem', { name: 'Delete' } ) .click(); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'Should delete multiple selected blocks using the dropdown menu' ) .toMatchObject( [ { name: 'core/file', selected: true, focused: true }, ] ); await page.keyboard.press( 'ArrowRight' ); const optionsForFileToggle = listView .getByRole( 'row' ) .filter( { has: page.getByRole( 'gridcell', { name: 'File' } ), } ) .getByRole( 'button', { name: 'Options' } ); const optionsForFileMenu = page.getByRole( 'menu', { name: 'Options', } ); // The menu moves focus to its first item on mount. Keys sent before // that lands are handled by the toggle button instead of the menu. const firstMenuItem = optionsForFileMenu .getByRole( 'menuitem' ) .first(); await expect( optionsForFileToggle, 'Pressing arrow right should move focus to the menu dropdown toggle button' ).toBeFocused(); await page.keyboard.press( 'Enter' ); await expect( optionsForFileMenu, 'Pressing Enter should open the menu dropdown' ).toBeVisible(); await expect( firstMenuItem ).toBeFocused(); await page.keyboard.press( 'Escape' ); await expect( optionsForFileMenu, 'Pressing Escape should close the menu dropdown' ).toBeHidden(); await expect( optionsForFileToggle, 'Should move focus back to the toggle button' ).toBeFocused(); await page.keyboard.press( 'Space' ); await expect( optionsForFileMenu, 'Pressing Space should also open the menu dropdown' ).toBeVisible(); await expect( firstMenuItem ).toBeFocused(); await pageUtils.pressKeys( 'primaryAlt+t' ); // Keyboard shortcut for Insert before. await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'Pressing keyboard shortcut should also work when the menu is opened and focused' ) .toMatchObject( [ { name: 'core/paragraph', selected: true, focused: true }, { name: 'core/file', selected: false, focused: false }, ] ); await expect( optionsForFileMenu, 'The menu should be closed after pressing keyboard shortcut' ).toBeHidden(); await optionsForFileToggle.click(); await expect( optionsForFileMenu, 'Clicking should also open the menu dropdown' ).toBeVisible(); await expect( optionsForFileMenu ).toBeFocused(); await page.keyboard.press( 'ArrowDown' ); await expect( firstMenuItem ).toBeFocused(); await pageUtils.pressKeys( 'access+z' ); // Keyboard shortcut for Delete. await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'Deleting blocks should move focus and selection' ) .toMatchObject( [ { name: 'core/paragraph', selected: true, focused: true }, ] ); // Insert a block that is locked and cannot be removed. await editor.insertBlock( { name: 'core/file', attributes: { lock: { move: false, remove: true } }, } ); await optionsForFileToggle.click(); await expect( optionsForFileMenu.getByRole( 'menuitem', { name: 'Delete' } ), 'The delete menu item should be hidden for locked blocks' ).toBeHidden(); await expect( optionsForFileMenu ).toBeFocused(); await page.keyboard.press( 'ArrowDown' ); await expect( firstMenuItem ).toBeFocused(); await pageUtils.pressKeys( 'access+z' ); await expect .poll( listViewUtils.getBlocksWithA11yAttributes, 'Pressing keyboard shortcut should not delete locked blocks either' ) .toMatchObject( [ { name: 'core/paragraph' }, { name: 'core/file', selected: true }, ] ); await expect( optionsForFileMenu, 'The dropdown menu should also be visible' ).toBeVisible(); } ); test( 'should place the caret at the start of the block when activating from List View', async ( { editor, page, listViewUtils, } ) => { // Insert a paragraph with some text. await editor.insertBlock( { name: 'core/paragraph', attributes: { content: 'First paragraph' }, } ); await editor.insertBlock( { name: 'core/paragraph', attributes: { content: 'Second paragraph' }, } ); // Open List View. const listView = await listViewUtils.openListView(); // Click the first paragraph in List View to select it, // then press Enter to activate it (transfer focus to canvas). // Keyboard activation places the caret at the start of the first // field. Mouse click keeps focus in the list view. await listView .getByRole( 'gridcell', { name: 'Paragraph' } ) .first() .click(); await page.keyboard.press( 'Enter' ); // Press Enter to split the block at the caret position. // With the caret at the start, this creates a new block before // the first paragraph. await page.keyboard.press( 'Enter' ); await expect.poll( editor.getBlocks ).toMatchObject( [ { name: 'core/paragraph', attributes: { content: '' } }, { name: 'core/paragraph', attributes: { content: 'First paragraph' }, }, { name: 'core/paragraph', attributes: { content: 'Second paragraph' }, }, ] ); } ); test( 'should focus the first cell of a Table when activating from List View', async ( { editor, page, listViewUtils, } ) => { await editor.insertBlock( { name: 'core/table', attributes: { body: [ { cells: [ { content: 'R1C1', tag: 'td' }, { content: 'R1C2', tag: 'td' }, ], }, { cells: [ { content: 'R2C1', tag: 'td' }, { content: 'R2C2', tag: 'td' }, ], }, ], }, } ); const listView = await listViewUtils.openListView(); await listView.getByRole( 'gridcell', { name: 'Table' } ).click(); await page.keyboard.press( 'Enter' ); await page.keyboard.type( 'X' ); await expect.poll( editor.getBlocks ).toMatchObject( [ { name: 'core/table', attributes: { body: [ { cells: [ { content: 'XR1C1', tag: 'td' }, { content: 'R1C2', tag: 'td' }, ], }, { cells: [ { content: 'R2C1', tag: 'td' }, { content: 'R2C2', tag: 'td' }, ], }, ], }, }, ] ); } ); test( 'keeps the canvas in place while scrolling to the selected block', async ( { editor, listViewUtils, page, } ) => { await page.emulateMedia( { reducedMotion: 'no-preference' } ); await editor.insertBlock( { name: 'core/spacer', attributes: { height: '2000px' }, } ); await editor.insertBlock( { name: 'core/paragraph' } ); const listView = await listViewUtils.openListView(); await listView.getByRole( 'link', { name: 'Spacer' } ).click(); await editor.canvas .locator( 'body' ) .evaluate( () => ( document.scrollingElement.scrollTop = 0 ) ); const canvas = page.locator( '.editor-visual-editor' ); await canvas.evaluate( ( node ) => node.addEventListener( 'scroll', () => node.setAttribute( 'data-scrolled', '' ) ) ); await listView.getByRole( 'link', { name: 'Paragraph' } ).click(); await expect( editor.canvas.getByRole( 'document', { name: 'Empty block' } ) ).toBeInViewport( { ratio: 1 } ); await expect( canvas ).not.toHaveAttribute( 'data-scrolled' ); } ); test.describe( 'long lists', () => { test.beforeEach( async ( { editor } ) => { // Sets the content in one go, as inserting the blocks one by one // selects the last one and opens the list at its end. await editor.setContent( Array.from( { length: 200 }, ( _, index ) => `

` ).join( '' ) ); } ); test( 'shows the rows in view while scrolling', async ( { page, listViewUtils, } ) => { await listViewUtils.openListView(); await listViewUtils.getRow( 5 ).hover(); await page.mouse.wheel( 0, 4000 ); await expect( listViewUtils.getRow( 130 ) ).toBeInViewport(); await listViewUtils.expectNoBlankRows(); await page.mouse.wheel( 0, -4000 ); await expect( listViewUtils.getRow( 5 ) ).toBeInViewport(); await listViewUtils.expectNoBlankRows(); } ); test( 'opens at a selected block far down the list', async ( { page, listViewUtils, } ) => { await page.evaluate( () => { const { getBlockOrder } = window.wp.data.select( 'core/block-editor' ); window.wp.data .dispatch( 'core/block-editor' ) .selectBlock( getBlockOrder()[ 149 ] ); } ); await listViewUtils.openListView(); await expect( listViewUtils.getRow( 150 ) ).toBeInViewport(); await listViewUtils.expectNoBlankRows(); } ); test( 'keeps focus on a row scrolled out of view', async ( { page, listViewUtils, } ) => { await listViewUtils.openListView(); await expect( listViewUtils.getRow( 1 ) ).toBeFocused(); await page.keyboard.press( 'ArrowDown' ); await expect( listViewUtils.getRow( 2 ) ).toBeFocused(); await listViewUtils.getRow( 5 ).hover(); await page.mouse.wheel( 0, 4000 ); await expect( listViewUtils.getRow( 130 ) ).toBeInViewport(); await expect( listViewUtils.getRow( 2 ) ).toBeFocused(); } ); test( 'pages through the list with Page Down @firefox @webkit', async ( { page, listViewUtils, } ) => { await listViewUtils.openListView(); await expect( listViewUtils.getRow( 1 ) ).toBeFocused(); await page.keyboard.press( 'PageDown' ); await expect( listViewUtils.getRow( 1 ) ).not.toBeInViewport(); await listViewUtils.expectNoBlankRows(); } ); test( 'moves to both ends of the list with End and Home @firefox @webkit', async ( { page, listViewUtils, } ) => { await listViewUtils.openListView(); await expect( listViewUtils.getRow( 1 ) ).toBeFocused(); await page.keyboard.press( 'End' ); await expect( listViewUtils.getRow( 200 ) ).toBeFocused(); await expect( listViewUtils.getRow( 200 ) ).toBeInViewport(); await listViewUtils.expectNoBlankRows(); await page.keyboard.press( 'Home' ); await expect( listViewUtils.getRow( 1 ) ).toBeFocused(); await expect( listViewUtils.getRow( 1 ) ).toBeInViewport(); await listViewUtils.expectNoBlankRows(); } ); test( 'moves to both ends of a nested list with End and Home', async ( { editor, page, listViewUtils, } ) => { await editor.setContent( Array.from( { length: 100 }, ( _, group ) => `
${ Array.from( { length: 4 }, ( __, paragraph ) => `

` ).join( '' ) }
` ).join( '' ) ); const listView = await listViewUtils.openListView(); const getGroup = ( position ) => listView.getByRole( 'link', { name: `Group ${ position }`, exact: true, } ); await expect( getGroup( 1 ) ).toBeFocused(); await page.keyboard.press( 'End' ); await expect( getGroup( 100 ) ).toBeFocused(); await expect( getGroup( 100 ) ).toBeInViewport(); // Expands the last group, then leaves it far outside of the // rendered rows. await page.keyboard.press( 'ArrowRight' ); await page.keyboard.press( 'Home' ); await expect( getGroup( 1 ) ).toBeFocused(); await expect( getGroup( 1 ) ).toBeInViewport(); await page.keyboard.press( 'End' ); await expect( listViewUtils.getRow( '100.4' ) ).toBeFocused(); await expect( listViewUtils.getRow( '100.4' ) ).toBeInViewport(); } ); test( 'moves to a parent far above with Arrow Left', async ( { editor, page, listViewUtils, } ) => { await editor.setContent( `

${ Array.from( { length: 60 }, ( _, index ) => `

` ).join( '' ) }

` ); await page.evaluate( () => { const [ outer ] = window.wp.data .select( 'core/block-editor' ) .getBlocks(); window.wp.data .dispatch( 'core/block-editor' ) .selectBlock( outer.innerBlocks[ 1 ].innerBlocks[ 49 ].clientId ); } ); const listView = await listViewUtils.openListView(); await expect( listViewUtils.getRow( 50 ) ).toBeFocused(); await page.keyboard.press( 'ArrowLeft' ); await expect( listView.getByRole( 'link', { name: 'Inner', exact: true } ) ).toBeFocused(); } ); } ); } ); /** @typedef {import('@playwright/test').Locator} Locator */ class ListViewUtils { #page; #pageUtils; #editor; constructor( { page, pageUtils, editor } ) { this.#page = page; this.#pageUtils = pageUtils; this.#editor = editor; /** @type {Locator} */ this.listView = page.getByRole( 'treegrid', { name: 'Block navigation structure', } ); } /** * @param {number} position One-based position of a paragraph in the list. * @return {Locator} The link in the paragraph's row. */ getRow = ( position ) => this.listView.getByRole( 'link', { name: `Paragraph ${ position }`, exact: true, } ); /** * Rows outside of the render window are replaced by a spacer before and * after the rendered rows, so neither should show up in the list. */ expectNoBlankRows = async () => { const spacers = this.listView.locator( '.block-editor-list-view-placeholder' ); await expect( spacers.first() ).not.toBeInViewport(); await expect( spacers.last() ).not.toBeInViewport(); }; /** * Drags a List View row onto the bottom half of another row, with the * cursor over the given indentation level, and drops it there. * * The cursor moves horizontally first, then vertically to the target, the * way a drag between two rows happens. The drop zone reads the row * geometry while the drop indicator displaces the rows, so the cursor * must end on the drop position rather than jump to it. * * @param {Object} options * @param {Object} options.from Locator of the dragged row's cell. * @param {Object} options.to Locator of the target row's cell. * @param {number} options.indentation Nesting level to hold the cursor over. `1` is the * target's own level, `2` nests inside it. */ dragBlockTo = async ( { from, to, indentation } ) => { // The drop zone measures the cursor against positions 24px apart from // the row's left edge, one per nesting level, and rounds to the // nearest one. Stay a few pixels right of the requested level's // position so the rounding resolves to that level. const NESTING_LEVEL_INDENTATION = 24; const fromRow = from.locator( 'xpath=ancestor::*[@role="row"][1]' ); const toRow = to.locator( 'xpath=ancestor::*[@role="row"][1]' ); const fromRect = await fromRow.boundingBox(); const toRect = await toRow.boundingBox(); const x = toRect.x + NESTING_LEVEL_INDENTATION * indentation + NESTING_LEVEL_INDENTATION / 4; const y = toRect.y + toRect.height * 0.75; await from.hover(); await this.#page.mouse.down(); await this.#page.mouse.move( x, fromRect.y + fromRect.height / 2, { steps: 5, } ); await this.#page.mouse.move( x, y, { steps: 20 } ); // Dispatch a final dragover at the drop position; see // https://playwright.dev/docs/input#dragging-manually await this.#page.mouse.move( x + 1, y, { steps: 2 } ); // Wait until the throttle timeout of 250ms has passed and the drop // target has rendered, so the drop uses the final cursor position. await this.#page.waitForTimeout( 300 ); await this.#page.mouse.up(); }; /** * @return {Promise} The list view locator. */ openListView = async () => { await this.#pageUtils.pressKeys( 'access+o' ); return this.listView; }; getBlocksWithA11yAttributes = async () => { const selectedRows = await this.listView .getByRole( 'row' ) .filter( { has: this.#page.getByRole( 'gridcell', { selected: true } ), } ) .all(); const selectedClientIds = await Promise.all( selectedRows.map( ( row ) => row.getAttribute( 'data-block' ) ) ); const focusedRows = await this.listView .getByRole( 'row' ) .filter( { has: this.#page.locator( ':focus' ) } ) .all(); const focusedClientId = focusedRows.length > 0 ? await focusedRows[ focusedRows.length - 1 ].getAttribute( 'data-block' ) : null; // Don't use the util to get the unmodified default block when it's empty. const blocks = await this.#page.evaluate( () => window.wp.data.select( 'core/block-editor' ).getBlocks() ); function recursivelyApplyAttributes( _blocks ) { return _blocks.map( ( block ) => ( { name: block.name, selected: selectedClientIds.includes( block.clientId ), focused: block.clientId === focusedClientId, innerBlocks: recursivelyApplyAttributes( block.innerBlocks ), } ) ); } return recursivelyApplyAttributes( blocks ); }; }