Skip to content
Draft
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
6 changes: 6 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,12 @@
"markdown_source": "../packages/block-library/src/file/README.md",
"parent": "core-blocks-media"
},
{
"title": "Files",
"slug": "core-block-files",
"markdown_source": "../packages/block-library/src/files/README.md",
"parent": "core-blocks-media"
},
{
"title": "Footnotes",
"slug": "core-block-footnotes",
Expand Down
12 changes: 11 additions & 1 deletion docs/reference-guides/core-blocks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,19 @@ Add a link to a downloadable file.

- **Name:** [core/file](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-media/core-block-file/)
- **Category:** [media](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-media/)
- **Supports:** align, anchor, color (background, gradients, link, ~~text~~), interactivity, spacing (margin, padding)
- **Supports:** align, anchor, color (background, gradients, link, ~~text~~), interactivity, layout (default, ~~allowInheriting~~, ~~allowOrientation~~, ~~allowSwitching~~, ~~allowVerticalAlignment~~, ~~allowWrap~~), spacing (margin, padding)
- **Attributes:** blob, displayPreview, downloadButtonText, fileId, fileName, href, id, previewHeight, showDownloadButton, textLinkHref, textLinkTarget

## Files

Display a list of downloadable files.

- **Name:** [core/files](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-media/core-block-files/)
- **Experimental:** true
- **Category:** [media](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-media/)
- **Allowed Blocks:** core/file
- **Supports:** align (full, wide), anchor, color (background, gradients, link, ~~text~~), interactivity (clientNavigation), layout (~~allowEditing~~), listView, spacing (blockGap, margin, padding), ~~html~~

## Footnotes

Display footnotes added to the page.
Expand Down
1 change: 1 addition & 0 deletions docs/reference-guides/core-blocks/category-media.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- [Audio](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-media/core-block-audio/) — `core/audio`: Embed a simple audio player.
- [Cover](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-media/core-block-cover/) — `core/cover`: Add an image or video with a text overlay.
- [File](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-media/core-block-file/) — `core/file`: Add a link to a downloadable file.
- [Files](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-media/core-block-files/) — `core/files`: Display a list of downloadable files.
- [Gallery](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-media/core-block-gallery/) — `core/gallery`: Display multiple images in a rich gallery.
- [Icon](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-media/core-block-icon/) — `core/icon`: Insert an SVG icon.
- [Image](https://developer.wordpress.org/block-editor/reference-guides/core-blocks/core-blocks-media/core-block-image/) — `core/image`: Insert an image to make a visual statement.
Expand Down
7 changes: 7 additions & 0 deletions packages/block-library/src/file/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ _Defined via the [`supports`](https://developer.wordpress.org/block-editor/refer
- [`link`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#color-link): `true`
- [`text`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#color-text): `false`
- [`interactivity`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#interactivity): `true`
- [`layout`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#layout):
- [`allowSwitching`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#layout-allowswitching): `false`
- [`allowInheriting`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#layout-allowinheriting): `false`
- [`allowOrientation`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#layout-alloworientation): `false`
- [`allowVerticalAlignment`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#layout-allowverticalalignment): `false`
- [`allowWrap`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#layout-allowwrap): `false`
- [`default`](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-supports/#layout-default): `{"type":"flex"}`

## Block Markup

Expand Down
12 changes: 11 additions & 1 deletion packages/block-library/src/file/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,17 @@
"style": true
}
},
"interactivity": true
"interactivity": true,
"layout": {
"allowSwitching": false,
"allowInheriting": false,
"allowOrientation": false,
"allowVerticalAlignment": false,
"allowWrap": false,
"default": {
"type": "flex"
}
}
},
"editorStyle": "wp-block-file-editor",
"style": "wp-block-file"
Expand Down
73 changes: 64 additions & 9 deletions packages/block-library/src/file/edit.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import clsx from 'clsx';
import { isBlobURL } from '@wordpress/blob';
import { createBlock, store as blocksStore } from '@wordpress/blocks';
import {
__unstableGetAnimateClassName as getAnimateClassName,
ResizableBox,
Expand All @@ -22,9 +23,9 @@ import { __ } from '@wordpress/i18n';
import { file as icon } from '@wordpress/icons';
import { store as coreStore } from '@wordpress/core-data';
import { store as noticesStore } from '@wordpress/notices';
import { getFilename } from '@wordpress/url';
import FileBlockInspector from './inspector';
import { browserSupportsPdfs } from './utils';
import { createFileBlocks, isPdf } from './utils/create-file-blocks';
import removeAnchorTag from '../utils/remove-anchor-tag';
import { useUploadMediaFromBlobURL } from '../utils/hooks';

Expand All @@ -51,7 +52,13 @@ function ClipboardToolbarButton( { text, disabled } ) {
);
}

function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
function FileEdit( {
attributes,
isSelected,
setAttributes,
clientId,
__unstableLayoutClassNames: layoutClassNames,
} ) {
const {
id,
fileName,
Expand All @@ -77,9 +84,25 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
} ),
[ id ]
);
const { canSelectMultipleFiles, isInFilesBlock } = useSelect(
( select ) => {
const { getBlockName, getBlockRootClientId } =
select( blockEditorStore );
return {
// Several files are grouped in a Files block, so only allow
// selecting them when that block is available.
canSelectMultipleFiles:
!! select( blocksStore ).getBlockType( 'core/files' ),
isInFilesBlock:
getBlockName( getBlockRootClientId( clientId ) ) ===
'core/files',
};
},
[ clientId ]
);

const { createErrorNotice } = useDispatch( noticesStore );
const { toggleSelection } = useDispatch( blockEditorStore );
const { replaceBlocks, toggleSelection } = useDispatch( blockEditorStore );

useUploadMediaFromBlobURL( {
url: temporaryURL,
Expand Down Expand Up @@ -108,15 +131,18 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
return;
}

const isPdf =
const isPdfFile = isPdf(
// Media Library and REST API use different properties for mime type.
( newMedia.mime || newMedia.mime_type ) === 'application/pdf' ||
getFilename( newMedia.url ).toLowerCase().endsWith( '.pdf' );
newMedia.mime || newMedia.mime_type,
newMedia.url
);
const pdfAttributes = {
displayPreview: isPdf
displayPreview: isPdfFile
? attributes.displayPreview ?? true
: undefined,
previewHeight: isPdf ? attributes.previewHeight ?? 600 : undefined,
previewHeight: isPdfFile
? attributes.previewHeight ?? 600
: undefined,
};

setAttributes( {
Expand All @@ -131,6 +157,24 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
setTemporaryURL();
}

function onSelectFiles( selection ) {
const files = Array.from( selection );
if ( files.length < 2 ) {
onSelectFile( files[ 0 ] );
return;
}

const fileBlocks = createFileBlocks( files );
// Inside a Files block the other files become rows next to this one.
// Anywhere else, all of them are grouped in a new Files block.
replaceBlocks(
clientId,
isInFilesBlock
? fileBlocks
: createBlock( 'core/files', {}, fileBlocks )
);
}

function onUploadError( message ) {
setAttributes( { href: undefined } );
createErrorNotice( message, { type: 'snackbar' } );
Expand Down Expand Up @@ -174,6 +218,7 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {

const blockProps = useBlockProps( {
className: clsx(
layoutClassNames,
!! temporaryURL && getAnimateClassName( { type: 'loading' } ),
{
'is-transient': !! temporaryURL,
Expand All @@ -194,9 +239,19 @@ function FileEdit( { attributes, isSelected, setAttributes, clientId } ) {
'Drag and drop a file, upload, or choose from your library.'
),
} }
onSelect={ onSelectFile }
onSelect={
canSelectMultipleFiles ? onSelectFiles : onSelectFile
}
onError={ onUploadError }
accept="*"
multiple={ canSelectMultipleFiles }
// A single file uploads here, as before. With several, each
// new File block uploads its own file.
handleUpload={
canSelectMultipleFiles
? ( files ) => files.length === 1
: true
}
/>
</div>
);
Expand Down
11 changes: 8 additions & 3 deletions packages/block-library/src/file/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
margin-bottom: 0;

Expand All @@ -32,8 +31,9 @@
left: 0;
}

// Let the block's flex layout place the file name and button.
.wp-block-file__content-wrapper {
flex-grow: 1;
display: contents;
}

a {
Expand All @@ -46,6 +46,11 @@

.wp-block-file__button-richtext-wrapper {
display: inline-block;
margin-left: 0.75em;
}
}

// Classic themes center blocks with a `data-align` wrapper instead of
// `.aligncenter`. Zero specificity, so a justification chosen in the editor wins.
:where(.wp-block[data-align="center"] > .wp-block-file) {
justify-content: center;
}
16 changes: 14 additions & 2 deletions packages/block-library/src/file/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,23 @@
text-align: right;
}

* + .wp-block-file__button {
margin-left: 0.75em;
// Space the file name and button as before the flex layout, whatever the theme's block gap.
&.is-layout-flex {
gap: 0.75em;
}
}

// Keep the alignment of File blocks added before the flex layout. Zero
// specificity, so a justification chosen in the editor wins.
:where(.wp-block-file.is-layout-flex.aligncenter) {
justify-content: center;
}

:where(.wp-block-file.is-layout-flex.alignright) {
// RTL rows start on the right, so flip the value to keep the block on the right.
justify-content: flex-end #{"/*rtl:flex-start;*/"};
}

// Lowest specificity to avoid overriding layout styles.
:where(.wp-block-file) {
margin-bottom: 1.5em;
Expand Down
19 changes: 18 additions & 1 deletion packages/block-library/src/file/transforms.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { createBlobURL } from '@wordpress/blob';
import { createBlock } from '@wordpress/blocks';
import { createBlock, store as blocksStore } from '@wordpress/blocks';
import { select } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import { _x } from '@wordpress/i18n';
import { getFilename } from '@wordpress/url';
import { createFileBlocks } from './utils/create-file-blocks';

// Transforms bypass the default variation, so set the localized default here.
const downloadButtonText = _x( 'Download', 'button label' );
Expand Down Expand Up @@ -44,6 +45,22 @@ const transforms = {
// ensures that the File block is only created as a fallback.
priority: 15,
transform: ( files ) => {
// Several non-media files dropped together are grouped in a
// Files block, when that block is available.
const isGroupOfFiles =
files.length > 1 &&
!! select( blocksStore ).getBlockType( 'core/files' ) &&
files.every(
( file ) => ! /^(image|video|audio)\//.test( file.type )
);
if ( isGroupOfFiles ) {
return createBlock(
'core/files',
{},
createFileBlocks( files )
);
}

const blocks = [];

files.forEach( ( file ) => {
Expand Down
83 changes: 83 additions & 0 deletions packages/block-library/src/file/utils/create-file-blocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { createBlobURL } from '@wordpress/blob';
import { createBlock, type Block } from '@wordpress/blocks';
import { _x } from '@wordpress/i18n';
import { getFilename } from '@wordpress/url';

/**
* A file chosen from the Media Library.
*/
export type MediaItem = {
id: number;
url: string;
title?: string;
// The Media Library and the REST API use different properties for the
// MIME type.
mime?: string;
mime_type?: string;
};

/**
* Whether a file is a PDF, judged by its MIME type or its file name.
*
* @param mimeType The file's MIME type.
* @param url The file's URL or name.
* @return Whether the file is a PDF.
*/
export function isPdf( mimeType?: string, url?: string ): boolean {
return (
mimeType === 'application/pdf' ||
!! getFilename( url ?? '' )
?.toLowerCase()
.endsWith( '.pdf' )
);
}

/**
* Creates a File block for each selected file.
*
* Local files aren't uploaded yet, so each block gets a blob URL and uploads
* its own file once it mounts. Media Library items are already uploaded, so
* their details are set directly.
*
* Inline PDF embeds start turned off: an embed on every row doesn't suit a
* list of files.
*
* @param selection Local files, or items chosen from the Media Library.
* @return File blocks.
*/
export function createFileBlocks(
selection: ArrayLike< File | MediaItem >
): Block[] {
// Blocks created here bypass the default variation, so set the localized
// button text here.
const downloadButtonText = _x( 'Download', 'button label' );

return Array.from( selection ).map( ( item ) => {
if ( item instanceof window.File ) {
return createBlock( 'core/file', {
blob: createBlobURL( item ),
fileName: item.name,
downloadButtonText,
displayPreview: isPdf( item.type, item.name )
? false
: undefined,
} );
}

const block = createBlock( 'core/file', {
href: item.url,
fileName: item.title,
textLinkHref: item.url,
id: item.id,
downloadButtonText,
displayPreview: isPdf( item.mime || item.mime_type, item.url )
? false
: undefined,
} );
// Uploaded files get this ID from the File block itself, which knows
// its client ID. Here the block is created before it's inserted.
block.attributes.fileId = `wp-block-file--media-${ block.clientId }`;

return block;
} );
}
Loading
Loading