Skip to content
Merged
2 changes: 2 additions & 0 deletions packages/block-library/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

- Playlist Track: Use a dedicated icon for the block toolbar. ([#80959](https://github.com/WordPress/gutenberg/pull/80959))
- Playlist: Expose the parent "Add track" toolbar control to selected Playlist Track child blocks via block toolbar sharing ([#80368](https://github.com/WordPress/gutenberg/pull/80368)).
- Playlist: Allow selecting audio tracks individually in the Media Library without holding Shift or Command, transform multiple Audio blocks into a Playlist, and transform a one-track Playlist into Audio. ([#80926](https://github.com/WordPress/gutenberg/pull/80926))
- Gallery: Rename the dynamic variation's "Convert to images" action to "Detach", and confirm it in a dialog explaining that the gallery will keep its current images but stop updating automatically ([#80727](https://github.com/WordPress/gutenberg/pull/80727)).
Comment thread
t-hamano marked this conversation as resolved.
Resolved
Comment thread
t-hamano marked this conversation as resolved.
Resolved
- Page List: Rename the "Edit" action to "Detach", and confirm it in a dialog explaining that the list will keep its current pages but stop adding new ones automatically, matching the Gallery block ([#80847](https://github.com/WordPress/gutenberg/pull/80847)).
- Tabs: Start new tabs with an empty label showing a "Tab title" placeholder instead of a generic "Tab" ([#81009](https://github.com/WordPress/gutenberg/pull/81009)).

Expand Down
6 changes: 3 additions & 3 deletions packages/block-library/src/playlist/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ const PlaylistEdit = ( {
} }
onSelect={ onSelectTracks }
accept="audio/*"
multiple
multiple="add"
Comment thread
getdave marked this conversation as resolved.
Resolved
handleUpload={ false }
allowedTypes={ ALLOWED_MEDIA_TYPES }
onError={ onUploadError }
Expand All @@ -476,7 +476,7 @@ const PlaylistEdit = ( {
name={ __( 'Add track' ) }
onSelect={ onAddTracks }
accept="audio/*"
multiple
multiple="add"
handleUpload={ false }
allowedTypes={ ALLOWED_MEDIA_TYPES }
onError={ onUploadError }
Expand Down Expand Up @@ -671,7 +671,7 @@ const PlaylistEdit = ( {
<MediaPlaceholder
onSelect={ onAddTracks }
accept="audio/*"
multiple
multiple="add"
handleUpload={ false }
disableMediaButtons
allowedTypes={ ALLOWED_MEDIA_TYPES }
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/playlist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import initBlock from '../utils/init-block';
import metadata from './block.json';
import edit from './edit';
import save from './save';
import transforms from './transforms';

const { name } = metadata;
export { metadata, name };
Expand All @@ -11,6 +12,7 @@ export const settings = {
icon,
edit,
save,
transforms,
};

export const init = () => initBlock( { name, metadata, settings } );
71 changes: 57 additions & 14 deletions packages/block-library/src/playlist/test/edit-component.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,34 @@ import { fireEvent, render, screen } from '@testing-library/react';
import { useDispatch, useSelect } from '@wordpress/data';
import PlaylistEdit from '../edit';

let mediaPlaceholderProps;
let mediaReplaceFlowProps;

jest.mock( '@wordpress/block-editor', () => ( {
store: {},
BlockControls: ( { children } ) => <div>{ children }</div>,
BlockIcon: () => <span />,
InspectorControls: ( { children } ) => <div>{ children }</div>,
MediaPlaceholder: () => <div />,
MediaReplaceFlow: ( { name, onSelect } ) => (
<button
onClick={ () =>
onSelect( {
id: 2,
url: 'https://example.com/second-track.mp3',
title: 'Second track',
} )
}
>
{ name }
</button>
),
MediaPlaceholder: ( props ) => {
mediaPlaceholderProps = props;
return <div />;
},
MediaReplaceFlow: ( props ) => {
mediaReplaceFlowProps = props;
return (
<button
onClick={ () =>
props.onSelect( {
id: 2,
url: 'https://example.com/second-track.mp3',
title: 'Second track',
} )
}
>
{ props.name }
</button>
);
},
useBlockProps: () => ( { className: 'wp-block-playlist' } ),
useInnerBlocksProps: ( blockProps ) => ( {
...blockProps,
Expand Down Expand Up @@ -100,6 +109,8 @@ describe( 'PlaylistEdit', () => {
let selectBlock;

beforeEach( () => {
mediaPlaceholderProps = undefined;
mediaReplaceFlowProps = undefined;
replaceInnerBlocks = jest.fn();
selectBlock = jest.fn();
useDispatch.mockReturnValue( {
Expand All @@ -121,6 +132,38 @@ describe( 'PlaylistEdit', () => {
} );
} );

it( 'lets users select audio tracks individually from the Media Library', () => {
useSelect.mockReturnValue( {
innerBlockTracks: [],
} );

render(
<PlaylistEdit
attributes={ defaultAttributes }
clientId="playlist-1"
insertBlocksAfter={ jest.fn() }
isSelected={ false }
setAttributes={ jest.fn() }
/>
);

expect( mediaPlaceholderProps.multiple ).toBe( 'add' );
} );

it( 'lets users select additional audio tracks individually from the Media Library', () => {
render(
<PlaylistEdit
attributes={ defaultAttributes }
clientId="playlist-1"
insertBlocksAfter={ jest.fn() }
isSelected={ false }
setAttributes={ jest.fn() }
/>
);

expect( mediaReplaceFlowProps.multiple ).toBe( 'add' );
} );

it( 'keeps track blocks mounted when the tracklist is hidden', () => {
render(
<PlaylistEdit
Expand Down
189 changes: 189 additions & 0 deletions packages/block-library/src/playlist/test/transforms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import {
createBlock,
getPossibleBlockTransformations,
registerBlockType,
switchToBlockType,
unregisterBlockType,
} from '@wordpress/blocks';
import {
metadata as playlistMetadata,
settings as playlistSettings,
} from '../index';
import playlistTrackMetadata from '../../playlist-track/block.json';
import audioMetadata from '../../audio/block.json';

describe( 'Playlist transforms', () => {
beforeAll( () => {
registerBlockType( playlistMetadata, playlistSettings );
registerBlockType( playlistTrackMetadata, { edit: () => null } );
registerBlockType( audioMetadata, { edit: () => null } );
} );

afterAll( () => {
unregisterBlockType( playlistMetadata.name );
unregisterBlockType( playlistTrackMetadata.name );
unregisterBlockType( audioMetadata.name );
} );

it( 'converts an Audio block into a Playlist with its audio as a track', () => {
const audio = createBlock( 'core/audio', {
blob: 'blob:https://example.com/track',
caption: 'A playlist caption',
id: 123,
src: 'https://example.com/track.mp3',
} );

const [ playlist ] = switchToBlockType( audio, 'core/playlist' );

expect( playlist ).toMatchObject( {
name: 'core/playlist',
attributes: {
caption: 'A playlist caption',
},
} );
expect( playlist.innerBlocks ).toEqual( [
expect.objectContaining( {
name: 'core/playlist-track',
attributes: expect.objectContaining( {
blob: 'blob:https://example.com/track',
id: 123,
src: 'https://example.com/track.mp3',
title: 'track.mp3',
} ),
} ),
] );
} );

it( 'converts multiple selected Audio blocks into one Playlist', () => {
const audioBlocks = [
createBlock( 'core/audio', {
blob: 'blob:https://example.com/first-track',
id: 123,
src: 'https://example.com/first-track.mp3',
} ),
createBlock( 'core/audio', {
blob: 'blob:https://example.com/second-track',
src: 'https://example.com/second-track.mp3',
} ),
];

const [ playlist ] = switchToBlockType( audioBlocks, 'core/playlist' );

expect( playlist.innerBlocks ).toEqual( [
expect.objectContaining( {
name: 'core/playlist-track',
attributes: expect.objectContaining( {
blob: 'blob:https://example.com/first-track',
id: 123,
src: 'https://example.com/first-track.mp3',
title: 'first-track.mp3',
} ),
} ),
expect.objectContaining( {
name: 'core/playlist-track',
attributes: expect.objectContaining( {
blob: 'blob:https://example.com/second-track',
src: 'https://example.com/second-track.mp3',
title: 'second-track.mp3',
} ),
} ),
] );
} );

it( 'preserves shared layout attributes when converting Audio to Playlist', () => {
const audio = createBlock( 'core/audio', {
align: 'wide',
anchor: 'my-audio',
style: {
spacing: {
margin: { top: '1rem' },
padding: { bottom: '2rem' },
},
},
} );

const [ playlist ] = switchToBlockType( audio, 'core/playlist' );

expect( playlist.attributes ).toMatchObject( {
align: 'wide',
anchor: 'my-audio',
style: {
spacing: {
margin: { top: '1rem' },
padding: { bottom: '2rem' },
},
},
} );
} );

it( 'converts Audio without an attachment ID', () => {
const audio = createBlock( 'core/audio', {
src: 'https://example.com/track.mp3',
} );

const [ playlist ] = switchToBlockType( audio, 'core/playlist' );

expect( playlist.innerBlocks[ 0 ].attributes ).not.toHaveProperty(
'id'
);
} );

it( 'converts a one-track Playlist into Audio', () => {
const playlist = createBlock(
'core/playlist',
{
align: 'wide',
anchor: 'my-playlist',
caption: 'A playlist caption',
style: { spacing: { margin: { top: '1rem' } } },
},
[
createBlock( 'core/playlist-track', {
blob: 'blob:https://example.com/track',
id: 123,
src: 'https://example.com/track.mp3',
} ),
]
);

expect( getPossibleBlockTransformations( [ playlist ] ) ).toEqual(
expect.arrayContaining( [
expect.objectContaining( { name: 'core/audio' } ),
] )
);

const [ audio ] = switchToBlockType( playlist, 'core/audio' );

expect( audio ).toMatchObject( {
name: 'core/audio',
attributes: {
align: 'wide',
anchor: 'my-playlist',
blob: 'blob:https://example.com/track',
id: 123,
src: 'https://example.com/track.mp3',
style: { spacing: { margin: { top: '1rem' } } },
},
} );
expect( audio.attributes.caption.toString() ).toBe(
'A playlist caption'
);
} );

it( 'does not offer Playlist to Audio for multiple tracks', () => {
const playlist = createBlock( 'core/playlist', {}, [
createBlock( 'core/playlist-track', {
src: 'https://example.com/first-track.mp3',
} ),
createBlock( 'core/playlist-track', {
src: 'https://example.com/second-track.mp3',
} ),
] );

expect( getPossibleBlockTransformations( [ playlist ] ) ).not.toEqual(
expect.arrayContaining( [
expect.objectContaining( { name: 'core/audio' } ),
] )
);
} );
} );
46 changes: 46 additions & 0 deletions packages/block-library/src/playlist/transforms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { createBlock } from '@wordpress/blocks';
import { getFilename } from '@wordpress/url';

const transforms = {
from: [
{
type: 'block',
isMultiBlock: true,
blocks: [ 'core/audio' ],
transform: ( attributes ) =>
createBlock(
'core/playlist',
{ ...attributes[ 0 ] },
attributes.map( ( { blob, id, src } ) =>
createBlock( 'core/playlist-track', {
blob,
id,
src,
title: getFilename( src ),
} )
)
),
},
],
to: [
{
type: 'block',
blocks: [ 'core/audio' ],
isMatch: ( {}, block ) =>
block.innerBlocks.length === 1 &&
block.innerBlocks[ 0 ].name === 'core/playlist-track',
transform: ( { style, ...attributes }, [ track ] ) =>
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;
Loading