UNPKG

@wordpress/block-library

Version:
240 lines (239 loc) • 10.1 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_blob = require("@wordpress/blob"); let _wordpress_element = require("@wordpress/element"); let _wordpress_block_editor = require("@wordpress/block-editor"); let _wordpress_components = require("@wordpress/components"); let _wordpress_ui = require("@wordpress/ui"); let _wordpress_data = require("@wordpress/data"); let _wordpress_notices = require("@wordpress/notices"); let _wordpress_i18n = require("@wordpress/i18n"); let _wordpress_icons = require("@wordpress/icons"); let _wordpress_dom = require("@wordpress/dom"); let ___playlist_context_js = require("../playlist/context.cjs"); let ___playlist_utils_js = require("../playlist/utils.cjs"); let ___utils_hooks_js = require("../utils/hooks.cjs"); let react_jsx_runtime = require("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] = (0, _wordpress_element.useState)(attributes.blob); const showArtists = context?.showArtists; const showImages = context?.showImages ?? true; const imageButton = (0, _wordpress_element.useRef)(); const blockProps = (0, _wordpress_block_editor.useBlockProps)(); const { currentTrackClientId, setCurrentTrackClientId, removeTrack } = (0, _wordpress_element.useContext)(___playlist_context_js.PlaylistContext); const { createErrorNotice } = (0, _wordpress_data.useDispatch)(_wordpress_notices.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; (0, _wordpress_element.useEffect)(() => { if (isSelected && hasTrackSource && currentTrackClientId !== clientId) setCurrentTrackClientId(clientId); }, [ isSelected, hasTrackSource, clientId, currentTrackClientId, setCurrentTrackClientId ]); (0, ___utils_hooks_js.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 ((0, _wordpress_blob.isBlobURL)(mediaUrl)) { setTemporaryURL(mediaUrl); return; } setAttributes({ blob: void 0, ...(0, ___playlist_utils_js.getTrackAttributes)(media) }); setTemporaryURL(); } function onSelectTrackImage(trackImage) { setAttributes((0, ___playlist_utils_js.getTrackImageAttributes)(trackImage)); } function onRemoveTrackImage() { setAttributes({ image: void 0, imageAlt: void 0 }); imageButton.current.focus(); } if (!hasTrackSource) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", { ...blockProps, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.MediaPlaceholder, { icon: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.BlockIcon, { icon: _wordpress_icons.audio }), labels: { title: (0, _wordpress_i18n.__)("Track"), instructions: (0, _wordpress_i18n.__)("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__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [ isSelected && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.BlockControls, { group: "other", children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.MediaReplaceFlow, { name: (0, _wordpress_i18n.__)("Replace"), onSelect: onSelectTrack, accept: "audio/*", mediaId: id, mediaURL: src, allowedTypes: ALLOWED_MEDIA_TYPES, onError: onUploadError, variant: "toolbar" }) }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.InspectorControls, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.PanelBody, { title: (0, _wordpress_i18n.__)("Settings"), children: [ isSelected && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.TextControl, { label: (0, _wordpress_i18n.__)("Title"), value: title ? (0, _wordpress_dom.__unstableStripHTML)(title) : "", onChange: (titleValue) => { setAttributes({ title: titleValue }); } }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.TextControl, { label: (0, _wordpress_i18n.__)("Artist"), value: artist ? (0, _wordpress_dom.__unstableStripHTML)(artist) : "", onChange: (artistValue) => { setAttributes({ artist: artistValue }); } }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.TextControl, { label: (0, _wordpress_i18n.__)("Album"), value: album ? (0, _wordpress_dom.__unstableStripHTML)(album) : "", onChange: (albumValue) => { setAttributes({ album: albumValue }); } }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.MediaUploadCheck, { children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.BaseControl, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.BaseControl.VisualLabel, { children: (0, _wordpress_i18n.__)("Track image") }), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", { className: "editor-video-poster-control", children: [ !!image && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("img", { src: image, alt: (0, _wordpress_i18n.__)("Preview of the track image") }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.MediaUpload, { title: (0, _wordpress_i18n.__)("Select image"), onSelect: onSelectTrackImage, allowedTypes: TRACK_IMAGE_ALLOWED_MEDIA_TYPES, render: ({ open }) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Button, { __next40pxDefaultSize: true, variant: "primary", onClick: open, ref: imageButton, children: !image ? (0, _wordpress_i18n.__)("Select") : (0, _wordpress_i18n.__)("Replace") }) }), !!image && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Button, { __next40pxDefaultSize: true, onClick: onRemoveTrackImage, variant: "tertiary", children: (0, _wordpress_i18n.__)("Remove") }) ] })] }) }), !!image && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.TextareaControl, { label: (0, _wordpress_i18n.__)("Alternative text"), value: imageAlt || "", onChange: (value) => setAttributes({ imageAlt: value }), help: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.Link, { openInNewTab: true, href: (0, _wordpress_i18n.__)("https://www.w3.org/WAI/tutorials/images/decision-tree/"), children: (0, _wordpress_i18n.__)("Describe the purpose of the image.") }) }) ] }) }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("li", { ...blockProps, children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("button", { className: "wp-block-playlist-track__button", onClick: () => setCurrentTrackClientId(clientId), "aria-current": currentTrackClientId === clientId ? "true" : "false", children: [ showImages && !!image && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("img", { className: "wp-block-playlist-track__image", src: image, alt: imageAlt || "" }), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("span", { className: "wp-block-playlist-track__content", children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.PlainText, { tagName: "span", className: "wp-block-playlist-track__title", value: title, "aria-label": (0, _wordpress_i18n.__)("Track title"), placeholder: (0, _wordpress_i18n.__)("Track title"), onChange: (value) => { setAttributes({ title: value }); }, __experimentalVersion: 2 }), showArtists && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.PlainText, { tagName: "span", className: "wp-block-playlist-track__artist", value: artist, "aria-label": (0, _wordpress_i18n.__)("Track artist"), placeholder: (0, _wordpress_i18n.__)("Track artist"), onChange: (value) => setAttributes({ artist: value }), __experimentalVersion: 2 })] }), /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("span", { className: "wp-block-playlist-track__length", children: [ length && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { className: "screen-reader-text", children: (0, _wordpress_i18n.__)("Duration:") }), length, !!temporaryURL && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Spinner, { className: "wp-block-playlist-track__spinner" }) ] }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { className: "screen-reader-text", children: (0, _wordpress_i18n.__)("Play") }) ] }) }) ] }); }; exports.default = PlaylistTrackEdit; //# sourceMappingURL=edit.cjs.map