Skip to content
Closed
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
4 changes: 4 additions & 0 deletions packages/block-library/src/playlist/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
"type": "boolean",
"default": true
},
"repeat": {
"type": "boolean",
"default": false
},
"caption": {
"type": "string"
}
Expand Down
23 changes: 20 additions & 3 deletions packages/block-library/src/playlist/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const PlaylistEdit = ( {
showImages,
showArtists,
currentTrack,
repeat,
} = attributes;
const blockProps = useBlockProps();
const { replaceInnerBlocks, __unstableMarkNextChangeAsNotPersistent } =
Expand Down Expand Up @@ -201,16 +202,17 @@ const PlaylistEdit = ( {
( track ) => track.uniqueId === currentTrack
);

// Handle track end - advance to next track or loop to first.
// Handle track end - advance to next track, or loop to first if repeat is enabled.
const onTrackEnded = useCallback( () => {
const currentIndex = tracks.findIndex(
( track ) => track.uniqueId === currentTrack
);
const nextTrack = tracks[ currentIndex + 1 ] || tracks[ 0 ];
const nextTrack =
tracks[ currentIndex + 1 ] || ( repeat ? tracks[ 0 ] : null );
if ( nextTrack?.uniqueId ) {
setAttributes( { currentTrack: nextTrack.uniqueId } );
}
}, [ currentTrack, tracks, setAttributes ] );
}, [ currentTrack, tracks, repeat, setAttributes ] );

const onChangeOrder = useCallback(
( trackOrder ) => {
Expand Down Expand Up @@ -310,6 +312,7 @@ const PlaylistEdit = ( {
showNumbers: true,
showImages: true,
order: 'asc',
repeat: false,
} );
} }
dropdownMenuProps={ dropdownMenuProps }
Expand Down Expand Up @@ -397,6 +400,20 @@ const PlaylistEdit = ( {
onChange={ ( value ) => onChangeOrder( value ) }
/>
</ToolsPanelItem>
<ToolsPanelItem
label={ __( 'Repeat' ) }
isShownByDefault
hasValue={ () => repeat !== false }
onDeselect={ () =>
setAttributes( { repeat: false } )
}
>
<ToggleControl
label={ __( 'Repeat' ) }
onChange={ toggleAttribute( 'repeat' ) }
checked={ repeat }
/>
</ToolsPanelItem>
</ToolsPanel>
</InspectorControls>
<figure { ...blockProps }>
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/playlist/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ function render_block_core_playlist( $attributes, $content, $block ) {
'playlistId' => $playlist_id,
'currentId' => $current_unique_id,
'tracks' => $playlist_tracks,
'repeat' => ! empty( $attributes['repeat'] ),
)
)
);
Expand Down
4 changes: 3 additions & 1 deletion packages/block-library/src/playlist/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ function initPlayer( ref, track, shouldAutoPlay, context ) {
const currentIndex = context.tracks.findIndex(
( uniqueId ) => uniqueId === context.currentId
);
const nextTrack = context.tracks[ currentIndex + 1 ];
const nextTrack =
context.tracks[ currentIndex + 1 ] ||
( context.repeat ? context.tracks[ 0 ] : null );
if ( nextTrack ) {
context.currentId = nextTrack;
}
Expand Down
Loading