UNPKG

@wordpress/block-library

Version:
198 lines (197 loc) 5.71 kB
// packages/block-library/src/utils/waveform-player.js 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"; 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); } } 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 metadataRef = useRef({ src, title, artist, image, imageAlt }); const stylesRef = useRef({ color, gradient, backgroundColor, backgroundGradient, textColor }); useEffect(() => { metadataRef.current = { src, title, artist, image, imageAlt }; }, [src, title, artist, image, imageAlt]); useEffect(() => { stylesRef.current = { color, gradient, backgroundColor, backgroundGradient, textColor }; }, [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 player = initWaveformPlayer(element, { src: metadataRef.current.src, title: metadataRef.current.title, artist: metadataRef.current.artist, image: metadataRef.current.image, imageAlt: metadataRef.current.imageAlt, waveformColor: stylesRef.current.color, waveformGradient: stylesRef.current.gradient, backgroundColor: stylesRef.current.backgroundColor, backgroundGradient: stylesRef.current.backgroundGradient, textColor: stylesRef.current.textColor, waveformStyle, labels: { seek: __("Seek"), /* translators: %1$s: current audio time, %2$s: total audio duration. */ 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 promise = playerRef.current.instance.loadTrack( src, metadataRef.current.title, metadataRef.current.artist, { artwork: showPlayButtonArtwork ? void 0 : metadataRef.current.image, artworkAlt: showPlayButtonArtwork ? "" : metadataRef.current.imageAlt } ); promise.then(() => { if (showPlayButtonArtwork && playerRef.current?.container) { setupPlayButtonArtwork( playerRef.current.container, metadataRef.current.image ); } if (!wasPlaying) { playerRef.current?.instance.pause(); } }); } }, [src, showPlayButtonArtwork]); return /* @__PURE__ */ jsx("div", { ref, className: "wp-block-playlist__waveform-player" }); } export { WaveformPlayer }; //# sourceMappingURL=waveform-player.mjs.map