import { createBlock } from '@wordpress/blocks'; import type { Block, BlockType } from '@wordpress/blocks'; import { getFilename } from '@wordpress/url'; import type { PlaylistAttributes } from './types'; const transforms = { from: [ { type: 'block', isMultiBlock: true, blocks: [ 'core/audio' ], transform: ( attributes: Record< string, unknown >[] ) => createBlock( 'core/playlist', { ...attributes[ 0 ] }, attributes.map( ( { blob, id, src } ) => createBlock( 'core/playlist-track', { blob, id, src, title: getFilename( src as string ), } ) ) ), }, ], to: [ { type: 'block', blocks: [ 'core/audio' ], isMatch: ( _attributes: Record< string, unknown >, block: Block ) => block.innerBlocks.length === 1 && block.innerBlocks[ 0 ].name === 'core/playlist-track', transform: ( { style, ...attributes }: Record< string, unknown > & { style?: { spacing?: Record< string, unknown > }; }, [ track ]: Block[] ) => createBlock( 'core/audio', { ...attributes, ...( style?.spacing && { style: { spacing: style.spacing }, } ), blob: track.attributes.blob, id: track.attributes.id, src: track.attributes.src, } ), }, ], }; export default transforms as BlockType< PlaylistAttributes >[ 'transforms' ];