UNPKG

@wordpress/block-library

Version:
235 lines (234 loc) • 8.1 kB
import { isBlobURL } from "@wordpress/blob"; import { useContext, useEffect, useRef, useState } from "@wordpress/element"; import { BlockControls, BlockIcon, InspectorControls, MediaPlaceholder, MediaReplaceFlow, MediaUpload, MediaUploadCheck, PlainText, useBlockProps } from "@wordpress/block-editor"; import { BaseControl, Button, PanelBody, Spinner, TextControl, TextareaControl } from "@wordpress/components"; import { Link } from "@wordpress/ui"; import { useDispatch } from "@wordpress/data"; import { store } from "@wordpress/notices"; import { __ } from "@wordpress/i18n"; import { audio } from "@wordpress/icons"; import { __unstableStripHTML } from "@wordpress/dom"; import { PlaylistContext } from "../playlist/context.mjs"; import { getTrackAttributes, getTrackImageAttributes } from "../playlist/utils.mjs"; import { useUploadMediaFromBlobURL } from "../utils/hooks.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; // packages/block-library/src/playlist-track/edit.jsx const ALLOWED_MEDIA_TYPES = ["audio"]; const TRACK_IMAGE_ALLOWED_MEDIA_TYPES = ["image"]; const PlaylistTrackEdit = ({ attributes, setAttributes, context, clientId, isSelected }) => { const { id, src, album, artist, image, imageAlt, length, title } = attributes; const [temporaryURL, setTemporaryURL] = useState(attributes.blob); const showArtists = context?.showArtists; const showImages = context?.showImages ?? true; const imageButton = useRef(); const blockProps = useBlockProps(); const { currentTrackClientId, setCurrentTrackClientId, removeTrack } = useContext(PlaylistContext); const { createErrorNotice } = useDispatch(store); /** * Handle audio upload errors. * * @param {string} message Error message to show. * @param {Object} [options] * @param {boolean} [options.removeTrackOnError] Remove the track as well. Useful for drag/drop where a track is optimistically created before upload finishes. */ function onUploadError(message, { removeTrackOnError = false } = {}) { createErrorNotice(message, { type: "snackbar" }); if (removeTrackOnError) { removeTrack(clientId); return; } setTemporaryURL(); } const hasTrackSource = !!src || !!temporaryURL; useEffect(() => { if (isSelected && hasTrackSource && currentTrackClientId !== clientId) setCurrentTrackClientId(clientId); }, [ isSelected, hasTrackSource, clientId, currentTrackClientId, setCurrentTrackClientId ]); useUploadMediaFromBlobURL({ url: temporaryURL, allowedTypes: ALLOWED_MEDIA_TYPES, onChange: onSelectTrack, onError: (message) => { onUploadError(message, { removeTrackOnError: true }); } }); function onSelectTrack(media) { const mediaUrl = media?.url ?? media?.source_url; if (!media || !mediaUrl) { setAttributes({ blob: void 0 }); setTemporaryURL(); return; } if (isBlobURL(mediaUrl)) { setTemporaryURL(mediaUrl); return; } setAttributes({ blob: void 0, ...getTrackAttributes(media) }); setTemporaryURL(); } function onSelectTrackImage(trackImage) { setAttributes(getTrackImageAttributes(trackImage)); } function onRemoveTrackImage() { setAttributes({ image: void 0, imageAlt: void 0 }); imageButton.current.focus(); } if (!hasTrackSource) return /* @__PURE__ */ jsx("div", { ...blockProps, children: /* @__PURE__ */ jsx(MediaPlaceholder, { icon: /* @__PURE__ */ jsx(BlockIcon, { icon: audio }), labels: { title: __("Track"), instructions: __("Upload an audio file or pick one from your media library.") }, onSelect: onSelectTrack, accept: "audio/*", allowedTypes: ALLOWED_MEDIA_TYPES, value: attributes, onError: onUploadError }) }); return /* @__PURE__ */ jsxs(Fragment, { children: [ isSelected && /* @__PURE__ */ jsx(BlockControls, { group: "other", children: /* @__PURE__ */ jsx(MediaReplaceFlow, { name: __("Replace"), onSelect: onSelectTrack, accept: "audio/*", mediaId: id, mediaURL: src, allowedTypes: ALLOWED_MEDIA_TYPES, onError: onUploadError, variant: "toolbar" }) }), /* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(PanelBody, { title: __("Settings"), children: [ isSelected && /* @__PURE__ */ jsx(TextControl, { label: __("Title"), value: title ? __unstableStripHTML(title) : "", onChange: (titleValue) => { setAttributes({ title: titleValue }); } }), /* @__PURE__ */ jsx(TextControl, { label: __("Artist"), value: artist ? __unstableStripHTML(artist) : "", onChange: (artistValue) => { setAttributes({ artist: artistValue }); } }), /* @__PURE__ */ jsx(TextControl, { label: __("Album"), value: album ? __unstableStripHTML(album) : "", onChange: (albumValue) => { setAttributes({ album: albumValue }); } }), /* @__PURE__ */ jsx(MediaUploadCheck, { children: /* @__PURE__ */ jsxs(BaseControl, { children: [/* @__PURE__ */ jsx(BaseControl.VisualLabel, { children: __("Track image") }), /* @__PURE__ */ jsxs("div", { className: "editor-video-poster-control", children: [ !!image && /* @__PURE__ */ jsx("img", { src: image, alt: __("Preview of the track image") }), /* @__PURE__ */ jsx(MediaUpload, { title: __("Select image"), onSelect: onSelectTrackImage, allowedTypes: TRACK_IMAGE_ALLOWED_MEDIA_TYPES, render: ({ open }) => /* @__PURE__ */ jsx(Button, { __next40pxDefaultSize: true, variant: "primary", onClick: open, ref: imageButton, children: !image ? __("Select") : __("Replace") }) }), !!image && /* @__PURE__ */ jsx(Button, { __next40pxDefaultSize: true, onClick: onRemoveTrackImage, variant: "tertiary", children: __("Remove") }) ] })] }) }), !!image && /* @__PURE__ */ jsx(TextareaControl, { label: __("Alternative text"), value: imageAlt || "", onChange: (value) => setAttributes({ imageAlt: value }), help: /* @__PURE__ */ jsx(Link, { openInNewTab: true, href: __("https://www.w3.org/WAI/tutorials/images/decision-tree/"), children: __("Describe the purpose of the image.") }) }) ] }) }), /* @__PURE__ */ jsx("li", { ...blockProps, children: /* @__PURE__ */ jsxs("button", { className: "wp-block-playlist-track__button", onClick: () => setCurrentTrackClientId(clientId), "aria-current": currentTrackClientId === clientId ? "true" : "false", children: [ showImages && !!image && /* @__PURE__ */ jsx("img", { className: "wp-block-playlist-track__image", src: image, alt: imageAlt || "" }), /* @__PURE__ */ jsxs("span", { className: "wp-block-playlist-track__content", children: [/* @__PURE__ */ jsx(PlainText, { tagName: "span", className: "wp-block-playlist-track__title", value: title, "aria-label": __("Track title"), placeholder: __("Track title"), onChange: (value) => { setAttributes({ title: value }); }, __experimentalVersion: 2 }), showArtists && /* @__PURE__ */ jsx(PlainText, { tagName: "span", className: "wp-block-playlist-track__artist", value: artist, "aria-label": __("Track artist"), placeholder: __("Track artist"), onChange: (value) => setAttributes({ artist: value }), __experimentalVersion: 2 })] }), /* @__PURE__ */ jsxs("span", { className: "wp-block-playlist-track__length", children: [ length && /* @__PURE__ */ jsx("span", { className: "screen-reader-text", children: __("Duration:") }), length, !!temporaryURL && /* @__PURE__ */ jsx(Spinner, { className: "wp-block-playlist-track__spinner" }) ] }), /* @__PURE__ */ jsx("span", { className: "screen-reader-text", children: __("Play") }) ] }) }) ] }); }; export { PlaylistTrackEdit as default }; //# sourceMappingURL=edit.mjs.map