Repository navigation
Playlist: Improve audio conversion and track selection #80926
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 11 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8bdf51d
Playlist: Improve audio conversion and track selection
getdave 9d1177a
Playlist Track: use checkbox-style multi-select in Add flow
Copilot 3d56296
Playlist: Preserve audio transform attributes
getdave 0169a56
Playlist: Keep track add control shared
getdave 6f272d0
Playlist: Add track to audio transform
getdave 693c9fa
Playlist: Fix audio block transformations
getdave b7a6814
Playlist: Cover transform discovery
getdave ac5ba01
Update packages/block-library/src/playlist/transforms.js
scruffian 2b7f204
Update packages/block-library/src/playlist/transforms.js
scruffian fc4b70c
fix linting
scruffian 14db1b0
fix linting
scruffian fd849b3
Remove irrelevant changelog entry
t-hamano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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' } ), | ||
| ] ) | ||
| ); | ||
| } ); | ||
| } ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.