UNPKG

@wordpress/block-library

Version:
184 lines (183 loc) • 6.91 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_element = require("@wordpress/element"); let _wordpress_compose = require("@wordpress/compose"); let _wordpress_i18n = require("@wordpress/i18n"); let __waveform_utils_js = require("./waveform-utils.cjs"); let react_jsx_runtime = require("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 ?? ""; (0, __waveform_utils_js.updateSeekControlLabel)(instance, title || (0, _wordpress_i18n.__)("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) (0, __waveform_utils_js.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 = (0, _wordpress_compose.useEvent)(onEnded); const playerRef = (0, _wordpress_element.useRef)(); const hasSrc = !!src; const getLatestProps = (0, _wordpress_compose.useEvent)(() => ({ src, title, artist, image, imageAlt, color, gradient, backgroundColor, backgroundGradient, textColor })); (0, _wordpress_element.useEffect)(() => { if (playerRef.current?.container) (0, __waveform_utils_js.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 = (0, _wordpress_compose.useRefEffect)((element) => { if (!hasSrc) return; let cancelled = false; let playerDestroy; function init() { if (cancelled) return; const latest = getLatestProps(); const player = (0, __waveform_utils_js.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: (0, _wordpress_i18n.__)("Seek"), seekValueText: (0, _wordpress_i18n._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 ]); (0, _wordpress_element.useEffect)(() => { if (playerRef.current?.instance) { const player = playerRef.current; if (player) updatePlayerMetadata(player, { title, artist, image, imageAlt }, showPlayButtonArtwork); } }, [ title, artist, image, imageAlt, showPlayButtonArtwork ]); (0, _wordpress_element.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) (0, __waveform_utils_js.setupPlayButtonArtwork)(playerRef.current.container, getLatestProps().image); if (!wasPlaying) playerRef.current?.instance.pause(); }); } }, [ src, showPlayButtonArtwork, getLatestProps ]); return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { ref, className: "wp-block-playlist__waveform-player" }); } exports.WaveformPlayer = WaveformPlayer; //# sourceMappingURL=waveform-player.cjs.map