From 61d171b97bf36d8382558d224dda8c692ed5f12d Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Tue, 13 Oct 2026 13:53:55 +0530 Subject: [PATCH 1/7] File Block: Add a Media section and a setup-state toolbar option --- packages/block-library/src/file/edit.jsx | 101 ++++++++++++++---- .../src/file/test/edit.browser.test.js | 73 +++++++++++++ .../block-library/src/utils/media-control.jsx | 3 + 3 files changed, 154 insertions(+), 23 deletions(-) create mode 100644 packages/block-library/src/file/test/edit.browser.test.js diff --git a/packages/block-library/src/file/edit.jsx b/packages/block-library/src/file/edit.jsx index 9ad14ae2cf47df..3052a1d298e19e 100644 --- a/packages/block-library/src/file/edit.jsx +++ b/packages/block-library/src/file/edit.jsx @@ -4,11 +4,14 @@ import { __unstableGetAnimateClassName as getAnimateClassName, ResizableBox, ToolbarButton, + __experimentalToolsPanel as ToolsPanel, + __experimentalToolsPanelItem as ToolsPanelItem, } from '@wordpress/components'; import { useSelect, useDispatch } from '@wordpress/data'; import { BlockControls, BlockIcon, + InspectorControls, MediaPlaceholder, MediaReplaceFlow, RichText, @@ -26,7 +29,11 @@ import { getFilename } from '@wordpress/url'; import FileBlockInspector from './inspector'; import { browserSupportsPdfs } from './utils'; import removeAnchorTag from '../utils/remove-anchor-tag'; -import { useUploadMediaFromBlobURL } from '../utils/hooks'; +import { + useUploadMediaFromBlobURL, + useToolsPanelDropdownMenuProps, +} from '../utils/hooks'; +import { MediaControl } from '../utils/media-control'; export const MIN_PREVIEW_HEIGHT = 200; export const MAX_PREVIEW_HEIGHT = 2000; @@ -80,6 +87,7 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) { const { createErrorNotice } = useDispatch( noticesStore ); const { toggleSelection } = useDispatch( blockEditorStore ); + const dropdownMenuProps = useToolsPanelDropdownMenuProps(); useUploadMediaFromBlobURL( { url: temporaryURL, @@ -185,22 +193,75 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) { const displayPreviewInEditor = browserSupportsPdfs() && displayPreview; + /* + * Rendered whether or not a file has been set, so that a file block in its + * setup state can be given media from the toolbar and the inspector as well + * as from the placeholder. The settings in `FileBlockInspector` stay behind + * their own condition, having nothing to configure until there is a file. + */ + const mediaToolbar = ( + + onSelectFile( undefined ) } + name={ href ? __( 'Replace' ) : __( 'Add file' ) } + /> + + ); + + const mediaInspectorPanel = isSelected && ( + + onSelectFile( undefined ) } + dropdownMenuProps={ dropdownMenuProps } + > + !! href } + onDeselect={ () => onSelectFile( undefined ) } + isShownByDefault + > + onSelectFile( undefined ) } + isUploading={ !! temporaryURL } + emptyLabel={ __( 'Add file' ) } + /> + + + + ); + if ( ! href && ! temporaryURL ) { return ( -
- } - labels={ { - title: __( 'File' ), - instructions: __( - 'Drag and drop a file, upload, or choose from your library.' - ), - } } - onSelect={ onSelectFile } - onError={ onUploadError } - accept="*" - /> -
+ <> + { mediaToolbar } + { mediaInspectorPanel } +
+ } + labels={ { + title: __( 'File' ), + instructions: __( + 'Drag and drop a file, upload, or choose from your library.' + ), + } } + onSelect={ onSelectFile } + onError={ onUploadError } + accept="*" + /> +
+ ); } @@ -224,15 +285,9 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) { changePreviewHeight, } } /> + { mediaToolbar } + { mediaInspectorPanel } - onSelectFile( undefined ) } - /> { + beforeEach( () => registerCoreBlocks() ); + + describe( 'Media panel', () => { + test( 'offers media from the inspector in the setup state', async () => { + await setup( {} ); + const panel = await getMediaPanel(); + + expect( + panel.getByRole( 'button', { name: 'Add file' } ) + ).toBeInTheDocument(); + } ); + + test( 'shows the filename and a reset once a file is set', async () => { + await setup( { href: 'http://localhost/my-doc.pdf', id: 7 } ); + const panel = await getMediaPanel(); + + expect( + panel.getByRole( 'button', { name: 'my-doc.pdf' } ) + ).toBeInTheDocument(); + expect( + panel.getByRole( 'button', { name: 'Reset' } ) + ).toBeInTheDocument(); + } ); + } ); + + describe( 'Block toolbar', () => { + test( 'offers media in the setup state, labelled "Add file"', async () => { + await setup( {} ); + const toolbar = await getToolbar(); + + expect( + toolbar.getByRole( 'button', { name: 'Add file' } ) + ).toBeInTheDocument(); + } ); + + test( 'reads "Replace" once a file is set', async () => { + await setup( { href: 'http://localhost/my-doc.pdf', id: 7 } ); + const toolbar = await getToolbar(); + + expect( + toolbar.getByRole( 'button', { name: 'Replace' } ) + ).toBeInTheDocument(); + } ); + } ); +} ); diff --git a/packages/block-library/src/utils/media-control.jsx b/packages/block-library/src/utils/media-control.jsx index 63819711d33204..d96b656873b441 100644 --- a/packages/block-library/src/utils/media-control.jsx +++ b/packages/block-library/src/utils/media-control.jsx @@ -80,6 +80,7 @@ export function MediaControlPreview( { * @param {string} props.mediaUrl Media URL * @param {string} props.filename Filename to display * @param {Array} props.allowedTypes Allowed media types + * @param {string} props.accept Comma-separated file types the upload input accepts * @param {Function} props.onSelect Callback when media selected * @param {Function} props.onSelectURL Callback when URL entered * @param {Function} props.onError Error callback @@ -95,6 +96,7 @@ export function MediaControl( { mediaUrl, filename, allowedTypes, + accept, onSelect, onSelectURL, onError, @@ -132,6 +134,7 @@ export function MediaControl( { mediaId={ mediaId } mediaURL={ mediaUrl } allowedTypes={ allowedTypes } + accept={ accept } onSelect={ onSelect } onSelectURL={ onSelectURL } onError={ onError } From e5d6bda728db047611ea2e96263126abc80cc7f5 Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Wed, 14 Oct 2026 15:21:49 +0530 Subject: [PATCH 2/7] File Block: Drop the unused accept passthrough --- packages/block-library/src/file/edit.jsx | 1 - packages/block-library/src/utils/media-control.jsx | 3 --- 2 files changed, 4 deletions(-) diff --git a/packages/block-library/src/file/edit.jsx b/packages/block-library/src/file/edit.jsx index 3052a1d298e19e..a870cb7fc36ecd 100644 --- a/packages/block-library/src/file/edit.jsx +++ b/packages/block-library/src/file/edit.jsx @@ -230,7 +230,6 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) { mediaId={ id } mediaUrl={ href } filename={ getFilename( href ) } - accept="*" onSelect={ onSelectFile } onError={ onUploadError } onReset={ () => onSelectFile( undefined ) } diff --git a/packages/block-library/src/utils/media-control.jsx b/packages/block-library/src/utils/media-control.jsx index d96b656873b441..63819711d33204 100644 --- a/packages/block-library/src/utils/media-control.jsx +++ b/packages/block-library/src/utils/media-control.jsx @@ -80,7 +80,6 @@ export function MediaControlPreview( { * @param {string} props.mediaUrl Media URL * @param {string} props.filename Filename to display * @param {Array} props.allowedTypes Allowed media types - * @param {string} props.accept Comma-separated file types the upload input accepts * @param {Function} props.onSelect Callback when media selected * @param {Function} props.onSelectURL Callback when URL entered * @param {Function} props.onError Error callback @@ -96,7 +95,6 @@ export function MediaControl( { mediaUrl, filename, allowedTypes, - accept, onSelect, onSelectURL, onError, @@ -134,7 +132,6 @@ export function MediaControl( { mediaId={ mediaId } mediaURL={ mediaUrl } allowedTypes={ allowedTypes } - accept={ accept } onSelect={ onSelect } onSelectURL={ onSelectURL } onError={ onError } From afc7ecf1e9b1f1f569590adc338d206035df7bb4 Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Wed, 14 Oct 2026 15:56:39 +0530 Subject: [PATCH 3/7] File Block: Rename the panel and drop the unused accept passthrough --- packages/block-library/src/file/edit.jsx | 2 +- .../block-library/src/file/test/edit.browser.test.js | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/packages/block-library/src/file/edit.jsx b/packages/block-library/src/file/edit.jsx index a870cb7fc36ecd..ab68f1ebc54f62 100644 --- a/packages/block-library/src/file/edit.jsx +++ b/packages/block-library/src/file/edit.jsx @@ -216,7 +216,7 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) { const mediaInspectorPanel = isSelected && ( onSelectFile( undefined ) } dropdownMenuProps={ dropdownMenuProps } > diff --git a/packages/block-library/src/file/test/edit.browser.test.js b/packages/block-library/src/file/test/edit.browser.test.js index 60e7e3e8f95156..4dceca4b304a5a 100644 --- a/packages/block-library/src/file/test/edit.browser.test.js +++ b/packages/block-library/src/file/test/edit.browser.test.js @@ -15,9 +15,17 @@ async function setup( attributes ) { return view; } +// The inspector shows the block name as a heading too, so pick the one that +// sits inside the panel. async function getMediaPanel() { - const heading = await screen.findByRole( 'heading', { name: 'Media' } ); - return within( heading.closest( '[data-wp-component="ToolsPanel"]' ) ); + const headings = await screen.findAllByRole( 'heading', { name: 'File' } ); + const panel = headings + .map( ( heading ) => + // eslint-disable-next-line testing-library/no-node-access + heading.closest( '[data-wp-component="ToolsPanel"]' ) + ) + .find( Boolean ); + return within( panel ); } // The inspector offers the same labels, so scope to the block toolbar. From 5e38785ffceaa388ec75cab6539e14876693563d Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Mon, 19 Oct 2026 10:47:58 +0530 Subject: [PATCH 4/7] File: Remove the editor test --- .../src/file/test/edit.browser.test.js | 81 ------------------- 1 file changed, 81 deletions(-) delete mode 100644 packages/block-library/src/file/test/edit.browser.test.js diff --git a/packages/block-library/src/file/test/edit.browser.test.js b/packages/block-library/src/file/test/edit.browser.test.js deleted file mode 100644 index 4dceca4b304a5a..00000000000000 --- a/packages/block-library/src/file/test/edit.browser.test.js +++ /dev/null @@ -1,81 +0,0 @@ -import { beforeEach, describe, expect, test } from 'vitest'; -import { screen, within } from '@testing-library/react'; -import { - initializeEditor, - selectBlock, -} from '@wordpress/integration-tests/helpers/integration-test-editor'; -import { registerCoreBlocks } from '@wordpress/block-library'; - -async function setup( attributes ) { - const view = await initializeEditor( - { name: 'core/file', attributes }, - false - ); - await selectBlock( 'Block: File' ); - return view; -} - -// The inspector shows the block name as a heading too, so pick the one that -// sits inside the panel. -async function getMediaPanel() { - const headings = await screen.findAllByRole( 'heading', { name: 'File' } ); - const panel = headings - .map( ( heading ) => - // eslint-disable-next-line testing-library/no-node-access - heading.closest( '[data-wp-component="ToolsPanel"]' ) - ) - .find( Boolean ); - return within( panel ); -} - -// The inspector offers the same labels, so scope to the block toolbar. -async function getToolbar() { - return within( await screen.findByRole( 'toolbar' ) ); -} - -describe( 'File block', () => { - beforeEach( () => registerCoreBlocks() ); - - describe( 'Media panel', () => { - test( 'offers media from the inspector in the setup state', async () => { - await setup( {} ); - const panel = await getMediaPanel(); - - expect( - panel.getByRole( 'button', { name: 'Add file' } ) - ).toBeInTheDocument(); - } ); - - test( 'shows the filename and a reset once a file is set', async () => { - await setup( { href: 'http://localhost/my-doc.pdf', id: 7 } ); - const panel = await getMediaPanel(); - - expect( - panel.getByRole( 'button', { name: 'my-doc.pdf' } ) - ).toBeInTheDocument(); - expect( - panel.getByRole( 'button', { name: 'Reset' } ) - ).toBeInTheDocument(); - } ); - } ); - - describe( 'Block toolbar', () => { - test( 'offers media in the setup state, labelled "Add file"', async () => { - await setup( {} ); - const toolbar = await getToolbar(); - - expect( - toolbar.getByRole( 'button', { name: 'Add file' } ) - ).toBeInTheDocument(); - } ); - - test( 'reads "Replace" once a file is set', async () => { - await setup( { href: 'http://localhost/my-doc.pdf', id: 7 } ); - const toolbar = await getToolbar(); - - expect( - toolbar.getByRole( 'button', { name: 'Replace' } ) - ).toBeInTheDocument(); - } ); - } ); -} ); From 64f3fa7f18742a438a2bb08ef96c0ab54d0fd7ba Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Wed, 25 Nov 2026 09:53:20 +0530 Subject: [PATCH 5/7] File Block: Show a file icon in the media control --- packages/block-library/src/file/edit.jsx | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/block-library/src/file/edit.jsx b/packages/block-library/src/file/edit.jsx index ab68f1ebc54f62..095fbd83642379 100644 --- a/packages/block-library/src/file/edit.jsx +++ b/packages/block-library/src/file/edit.jsx @@ -229,6 +229,7 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) { Date: Thu, 3 Dec 2026 09:32:15 +0530 Subject: [PATCH 6/7] File Block: Fix the filename type for MediaControl --- packages/block-library/src/file/edit.tsx | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/block-library/src/file/edit.tsx b/packages/block-library/src/file/edit.tsx index 061b7b304c4248..89a7029dcb0e23 100644 --- a/packages/block-library/src/file/edit.tsx +++ b/packages/block-library/src/file/edit.tsx @@ -256,7 +256,11 @@ function FileEdit( { mediaId={ id } mediaUrl={ href } mediaType="file" - filename={ getFilename( href ) } + filename={ + href + ? ( getFilename( href ) as string | undefined ) + : undefined + } onSelect={ onSelectFile } onError={ onUploadError } onReset={ () => onSelectFile( undefined ) } From 9650f684090a05370f24dd5dfa6336e7328ee46e Mon Sep 17 00:00:00 2001 From: Eshaan Dabasiya <76681468+im3dabasia@users.noreply.github.com> Date: Thu, 3 Dec 2026 15:44:14 +0530 Subject: [PATCH 7/7] Update packages/block-library/src/file/edit.tsx Co-authored-by: Aki Hamano <54422211+t-hamano@users.noreply.github.com> --- packages/block-library/src/file/edit.tsx | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/packages/block-library/src/file/edit.tsx b/packages/block-library/src/file/edit.tsx index 89a7029dcb0e23..061b7b304c4248 100644 --- a/packages/block-library/src/file/edit.tsx +++ b/packages/block-library/src/file/edit.tsx @@ -256,11 +256,7 @@ function FileEdit( { mediaId={ id } mediaUrl={ href } mediaType="file" - filename={ - href - ? ( getFilename( href ) as string | undefined ) - : undefined - } + filename={ getFilename( href ) } onSelect={ onSelectFile } onError={ onUploadError } onReset={ () => onSelectFile( undefined ) }