@wordpress/block-library
Version:
Block library for the WordPress editor.
50 lines (49 loc) • 1.69 kB
JavaScript
import { __ } from "@wordpress/i18n";
import { decodeEntities } from "@wordpress/html-entities";
// packages/block-library/src/playlist/utils.js
function getMediaUrl(media) {
return media?.url ?? media?.source_url;
}
function getMediaTitle(media) {
const title = media?.title;
if (title === void 0) return;
if (typeof title === "string") return title;
return decodeEntities(title?.raw || title?.rendered || "");
}
/**
* Transform media library image data into track image attributes.
*
* @param {Object} image - Image object from the media library.
* @return {Object} Track image attributes for the playlist-track block.
*/
function getTrackImageAttributes(image) {
const imageSrc = image?.src ?? getMediaUrl(image);
if (imageSrc?.endsWith("/images/media/audio.svg")) return {
image: "",
imageAlt: ""
};
return {
image: imageSrc,
imageAlt: imageSrc ? image?.alt || image?.alt_text || "" : void 0
};
}
/**
* Transform media library data into track block attributes.
*
* @param {Object} media - Media object from the media library.
* @return {Object} Track attributes for the playlist-track block.
*/
function getTrackAttributes(media) {
const mediaUrl = getMediaUrl(media);
return {
id: media.id || mediaUrl,
src: mediaUrl,
title: getMediaTitle(media),
artist: media.artist || media?.meta?.artist || media?.media_details?.artist || __("Unknown artist"),
album: media.album || media?.meta?.album || media?.media_details?.album || __("Unknown album"),
length: media?.fileLength || media?.media_details?.length_formatted,
...getTrackImageAttributes(media?.image)
};
}
export { getTrackAttributes, getTrackImageAttributes };
//# sourceMappingURL=utils.mjs.map