import { createBlobURL } from '@wordpress/blob'; import { RichText } from '@wordpress/block-editor'; import { createBlock } from '@wordpress/blocks'; import type { Block, BlockConfiguration } from '@wordpress/blocks'; import { select } from '@wordpress/data'; import { store as coreStore } from '@wordpress/core-data'; import { escapeEditableHTML } from '@wordpress/escape-html'; import { _x } from '@wordpress/i18n'; import { getFilename } from '@wordpress/url'; import type { FileMediaTransformSourceAttributes } from './types'; // Transforms bypass the default variation, so set the localized default here. const downloadButtonText = _x( 'Download', 'button label' ); // The File → audio/video/image transforms are identical apart from the target // block, its media MIME type, and the src attribute name (image uses `url`). const toMediaTransform = ( blockName: string, mediaType: string, srcAttribute: string ) => ( { type: 'block' as const, blocks: [ blockName ], isMatch: ( attributes: Record< string, unknown > | Record< string, unknown >[] ) => { const { id } = attributes as { id?: number }; if ( ! id ) { return false; } const { getEntityRecord } = select( coreStore ); const media = getEntityRecord( 'postType', 'attachment', id, { context: 'view', } ); return !! media && media.mime_type.includes( mediaType ); }, transform: ( attributes: Record< string, unknown > | Record< string, unknown >[] ) => { const attrs = attributes as Record< string, unknown >; return createBlock( blockName, { [ srcAttribute ]: attrs.href, caption: attrs.fileName, id: attrs.id, anchor: attrs.anchor, } ); }, } ); const transforms: BlockConfiguration[ 'transforms' ] = { from: [ { type: 'files', isMatch( files ) { return files.length > 0; }, // We define a lower priority (higher number) than the default of 10. This // ensures that the File block is only created as a fallback. priority: 15, transform: ( files ) => { const blocks: Block[] = []; files.forEach( ( file ) => { const blobURL = createBlobURL( file ); // File will be uploaded in componentDidMount() if ( file.type.startsWith( 'video/' ) ) { blocks.push( createBlock( 'core/video', { blob: createBlobURL( file ), } ) ); } else if ( file.type.startsWith( 'image/' ) ) { blocks.push( createBlock( 'core/image', { blob: createBlobURL( file ), } ) ); } else if ( file.type.startsWith( 'audio/' ) ) { blocks.push( createBlock( 'core/audio', { blob: createBlobURL( file ), } ) ); } else { blocks.push( createBlock( 'core/file', { blob: blobURL, fileName: file.name, downloadButtonText, } ) ); } } ); return blocks; }, }, { type: 'block', blocks: [ 'core/audio', 'core/video', 'core/image' ], transform: ( attributes ) => { const attrs = attributes as FileMediaTransformSourceAttributes; // Audio/Video use `src`, Image uses `url`. const href = ( attrs.src ?? attrs.url ) as string; const block = createBlock( 'core/file', { href, fileName: RichText.isEmpty( attrs.caption ) ? escapeEditableHTML( getFilename( href ) ?? '' ) : attrs.caption, textLinkHref: href, id: attrs.id, anchor: attrs.anchor, downloadButtonText, } ); // Link the download button to the file name, as `onSelectFile` does. block.attributes.fileId = `wp-block-file--media-${ block.clientId }`; return block; }, }, ], to: [ toMediaTransform( 'core/audio', 'audio', 'src' ), toMediaTransform( 'core/video', 'video', 'src' ), toMediaTransform( 'core/image', 'image', 'url' ), ], }; export default transforms;