@wordpress/block-library
Version:
Block library for the WordPress editor.
179 lines (178 loc) • 6.45 kB
JavaScript
import { useEffect, useRef } from "@wordpress/element";
import { useEvent, useRefEffect } from "@wordpress/compose";
import { __, _x } from "@wordpress/i18n";
import { applyWaveformPlayerStyles, initWaveformPlayer, setupPlayButtonArtwork, updateSeekControlLabel } from "./waveform-utils.mjs";
import { jsx } from "react/jsx-runtime";
// packages/block-library/src/utils/waveform-player.jsx
/**
* Update the metadata of a WaveformPlayer element to reflect current props.
*
* `loadTrack()` owns full track swaps, but it also resets the audio element.
* This keeps same-source metadata edits lightweight in the editor.
*
* @param {Object} player - The waveform player.
* @param {Object} metadata - The track metadata.
* @param {string} metadata.title - The track title.
* @param {string} metadata.artist - The artist name.
* @param {string} metadata.image - The track image URL.
* @param {string} metadata.imageAlt - The track image alt text.
* @param {boolean} showPlayButtonArtwork - Whether to show artwork on the play button.
*/
function updatePlayerMetadata(player, { title, artist, image, imageAlt }, showPlayButtonArtwork) {
const { instance, container } = player;
const playerArtwork = showPlayButtonArtwork ? void 0 : image;
if (instance.titleEl) instance.titleEl.textContent = title ?? "";
updateSeekControlLabel(instance, title || __("Seek"));
if (typeof instance.syncArtist === "function") instance.syncArtist(artist || "");
else if (instance.artistEl) {
instance.artistEl.textContent = artist ?? "";
instance.artistEl.style.display = artist ? "" : "none";
}
if (typeof instance.syncArtwork === "function") instance.syncArtwork(playerArtwork || null, playerArtwork ? imageAlt || "" : "");
else if (instance.artworkEl && playerArtwork) {
instance.artworkEl.src = playerArtwork;
instance.artworkEl.alt = imageAlt || "";
}
if (showPlayButtonArtwork) setupPlayButtonArtwork(container, image);
}
/**
* A reusable WaveformPlayer component for the block editor.
*
* Renders an audio waveform visualization with play/pause controls.
* Automatically inherits colors from the parent block's text color.
*
* @param {Object} props - Component props.
* @param {string} props.src - The audio file URL.
* @param {string} props.title - The track title.
* @param {string} props.artist - The artist name.
* @param {string} props.image - The track image URL.
* @param {string} props.imageAlt - The track image alt text.
* @param {string} props.color - The waveform color.
* @param {string} props.gradient - The waveform gradient.
* @param {string} props.backgroundColor - The waveform background color.
* @param {string} props.backgroundGradient - The waveform background gradient.
* @param {string} props.textColor - The player text color.
* @param {string} props.waveformStyle - Waveform style (bars, mirror, line, blocks, dots, seekbar).
* @param {Function} props.onEnded - Callback when the track finishes playing.
* @param {boolean} props.showPlayButtonArtwork - Whether to show artwork on the play button.
* @return {Element} The WaveformPlayer element.
*/
function WaveformPlayer({ src, title, artist, image, imageAlt, color, gradient, backgroundColor, backgroundGradient, textColor, waveformStyle, onEnded, showPlayButtonArtwork = false }) {
const onEndedEvent = useEvent(onEnded);
const playerRef = useRef();
const hasSrc = !!src;
const getLatestProps = useEvent(() => ({
src,
title,
artist,
image,
imageAlt,
color,
gradient,
backgroundColor,
backgroundGradient,
textColor
}));
useEffect(() => {
if (playerRef.current?.container) applyWaveformPlayerStyles(playerRef.current.container, {
backgroundColor,
backgroundGradient,
textColor,
playButtonColor: showPlayButtonArtwork ? void 0 : color,
playButtonGradient: showPlayButtonArtwork ? void 0 : gradient
});
}, [
backgroundColor,
backgroundGradient,
color,
gradient,
showPlayButtonArtwork,
textColor
]);
const ref = useRefEffect((element) => {
if (!hasSrc) return;
let cancelled = false;
let playerDestroy;
function init() {
if (cancelled) return;
const latest = getLatestProps();
const player = initWaveformPlayer(element, {
src: latest.src,
title: latest.title,
artist: latest.artist,
image: latest.image,
imageAlt: latest.imageAlt,
waveformColor: latest.color,
waveformGradient: latest.gradient,
backgroundColor: latest.backgroundColor,
backgroundGradient: latest.backgroundGradient,
textColor: latest.textColor,
waveformStyle,
labels: {
seek: __("Seek"),
seekValueText: _x("%1$s of %2$s", "audio current time of total duration")
},
onEnded: () => onEndedEvent?.(),
showPlayButtonArtwork
});
playerRef.current = player;
const { destroy } = player;
playerDestroy = destroy;
}
const timeoutId = setTimeout(init, 100);
return () => {
cancelled = true;
clearTimeout(timeoutId);
playerRef.current = void 0;
playerDestroy?.();
};
}, [
onEndedEvent,
hasSrc,
waveformStyle,
color,
gradient,
textColor,
showPlayButtonArtwork
]);
useEffect(() => {
if (playerRef.current?.instance) {
const player = playerRef.current;
if (player) updatePlayerMetadata(player, {
title,
artist,
image,
imageAlt
}, showPlayButtonArtwork);
}
}, [
title,
artist,
image,
imageAlt,
showPlayButtonArtwork
]);
useEffect(() => {
if (src && playerRef.current?.instance) {
const wasPlaying = playerRef.current.instance.isPlaying;
const latest = getLatestProps();
playerRef.current.instance.loadTrack(src, latest.title, latest.artist, {
artwork: showPlayButtonArtwork ? void 0 : latest.image,
artworkAlt: showPlayButtonArtwork ? "" : latest.imageAlt
}).then(() => {
if (showPlayButtonArtwork && playerRef.current?.container) setupPlayButtonArtwork(playerRef.current.container, getLatestProps().image);
if (!wasPlaying) playerRef.current?.instance.pause();
});
}
}, [
src,
showPlayButtonArtwork,
getLatestProps
]);
return /* @__PURE__ */ jsx("div", {
ref,
className: "wp-block-playlist__waveform-player"
});
}
export { WaveformPlayer };
//# sourceMappingURL=waveform-player.mjs.map