@wordpress/block-editor
Version:
59 lines (55 loc) • 1.82 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getBlockAndPreviewFromMedia = getBlockAndPreviewFromMedia;
var _blocks = require("@wordpress/blocks");
var _jsxRuntime = require("react/jsx-runtime");
/**
* WordPress dependencies
*/
const mediaTypeTag = {
image: 'img',
video: 'video',
audio: 'audio'
};
/** @typedef {import('./hooks').InserterMediaItem} InserterMediaItem */
/**
* Creates a block and a preview element from a media object.
*
* @param {InserterMediaItem} media The media object to create the block from.
* @param {('image'|'audio'|'video')} mediaType The media type to create the block for.
* @return {[WPBlock, JSX.Element]} An array containing the block and the preview element.
*/
function getBlockAndPreviewFromMedia(media, mediaType) {
// Add the common attributes between the different media types.
const attributes = {
id: media.id || undefined,
caption: media.caption || undefined
};
const mediaSrc = media.url;
const alt = media.alt || undefined;
if (mediaType === 'image') {
attributes.url = mediaSrc;
attributes.alt = alt;
} else if (['video', 'audio'].includes(mediaType)) {
attributes.src = mediaSrc;
}
const PreviewTag = mediaTypeTag[mediaType];
const preview = /*#__PURE__*/(0, _jsxRuntime.jsx)(PreviewTag, {
src: media.previewUrl || mediaSrc,
alt: alt,
controls: mediaType === 'audio' ? true : undefined,
inert: "true",
onError: ({
currentTarget
}) => {
// Fall back to the media source if the preview cannot be loaded.
if (currentTarget.src === media.previewUrl) {
currentTarget.src = mediaSrc;
}
}
});
return [(0, _blocks.createBlock)(`core/${mediaType}`, attributes), preview];
}
//# sourceMappingURL=utils.js.map