@wordpress/block-library
Version:
Block library for the WordPress editor.
210 lines (209 loc) • 6.88 kB
JavaScript
import clsx from "clsx";
import { isBlobURL } from "@wordpress/blob";
import { Spinner, ToggleControl, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components";
import { inertValue } from "@wordpress/react-inert-value";
import { SelectControl, Text } from "@wordpress/ui";
import { BlockControls, BlockIcon, InspectorControls, MediaPlaceholder, MediaReplaceFlow, useBlockEditingMode, useBlockProps } from "@wordpress/block-editor";
import { __, _x } from "@wordpress/i18n";
import { useDispatch } from "@wordpress/data";
import { audio } from "@wordpress/icons";
import { store } from "@wordpress/notices";
import { useState } from "@wordpress/element";
import { createUpgradedEmbedBlock } from "../embed/util.mjs";
import { useToolsPanelDropdownMenuProps, useUploadMediaFromBlobURL } from "../utils/hooks.mjs";
import { Caption } from "../utils/caption.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/audio/edit.jsx
const ALLOWED_MEDIA_TYPES = ["audio"];
const PRELOAD_ITEMS = [
{
value: null,
label: __("Browser default")
},
{
value: "auto",
label: __("Auto")
},
{
value: "metadata",
label: __("Metadata")
},
{
value: "none",
label: _x("None", "Preload value")
}
];
/**
* Renders the `core/audio` block on the editor.
*
* @param {Object} props React props.
* @param {Object} props.attributes Block attributes: `blob`, `src`, `caption`, `id`, `autoplay`, `loop` and `preload`.
* @param {string} props.className Class name generated for the block.
* @param {Function} props.setAttributes Callback for updating block attributes.
* @param {Function} props.onReplace Callback for replacing this block with other blocks.
* @param {boolean} props.isSelected Whether the block is the only selected block.
* @param {Function} props.insertBlocksAfter Callback for inserting blocks after this one.
*
* @return {React.JSX.Element} React element.
*/
function AudioEdit({ attributes, className, setAttributes, onReplace, isSelected: isSingleSelected, insertBlocksAfter }) {
const { id, autoplay, loop, preload, src } = attributes;
const [temporaryURL, setTemporaryURL] = useState(attributes.blob);
const hasNonContentControls = useBlockEditingMode() === "default";
useUploadMediaFromBlobURL({
url: temporaryURL,
allowedTypes: ALLOWED_MEDIA_TYPES,
onChange: onSelectAudio,
onError: onUploadError
});
function toggleAttribute(attribute) {
return (newValue) => {
setAttributes({ [attribute]: newValue });
};
}
function onSelectURL(newSrc) {
if (newSrc !== src) {
const embedBlock = createUpgradedEmbedBlock({ attributes: { url: newSrc } });
if (void 0 !== embedBlock && onReplace) {
onReplace(embedBlock);
return;
}
setAttributes({
src: newSrc,
id: void 0,
blob: void 0
});
setTemporaryURL();
}
}
const { createErrorNotice } = useDispatch(store);
function onUploadError(message) {
createErrorNotice(message, { type: "snackbar" });
}
function onSelectAudio(media) {
if (!media || !media.url) {
setAttributes({
src: void 0,
id: void 0,
caption: void 0,
blob: void 0
});
setTemporaryURL();
return;
}
if (isBlobURL(media.url)) {
setTemporaryURL(media.url);
return;
}
setAttributes({
blob: void 0,
src: media.url,
id: media.id,
caption: media.caption
});
setTemporaryURL();
}
const classes = clsx(className, { "is-transient": !!temporaryURL });
const blockProps = useBlockProps({ className: classes });
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
if (!src && !temporaryURL) return /* @__PURE__ */ jsx("div", {
...blockProps,
children: /* @__PURE__ */ jsx(MediaPlaceholder, {
icon: /* @__PURE__ */ jsx(BlockIcon, { icon: audio }),
onSelect: onSelectAudio,
onSelectURL,
accept: "audio/*",
allowedTypes: ALLOWED_MEDIA_TYPES,
value: attributes,
onError: onUploadError
})
});
return /* @__PURE__ */ jsxs(Fragment, { children: [
isSingleSelected && /* @__PURE__ */ jsx(BlockControls, {
group: "other",
children: /* @__PURE__ */ jsx(MediaReplaceFlow, {
mediaId: id,
mediaURL: src,
allowedTypes: ALLOWED_MEDIA_TYPES,
accept: "audio/*",
onSelect: onSelectAudio,
onSelectURL,
onError: onUploadError,
onReset: () => onSelectAudio(void 0),
variant: "toolbar"
})
}),
/* @__PURE__ */ jsx(InspectorControls, { children: /* @__PURE__ */ jsxs(__experimentalToolsPanel, {
label: __("Settings"),
resetAll: () => {
setAttributes({
autoplay: false,
loop: false,
preload: void 0
});
},
dropdownMenuProps,
children: [
/* @__PURE__ */ jsx(Text, {
className: "wp-block-audio__autoplay-help-text",
children: __("Most browsers block audio until a visitor interacts with the page, so autoplay often won’t work, especially on mobile. Unexpected sound can also be disruptive for screen reader users.")
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Autoplay"),
isShownByDefault: true,
hasValue: () => !!autoplay,
onDeselect: () => setAttributes({ autoplay: false }),
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Autoplay"),
onChange: toggleAttribute("autoplay"),
checked: !!autoplay
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Loop"),
isShownByDefault: true,
hasValue: () => !!loop,
onDeselect: () => setAttributes({ loop: false }),
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Loop"),
onChange: toggleAttribute("loop"),
checked: !!loop
})
}),
/* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Preload"),
isShownByDefault: true,
hasValue: () => !!preload,
onDeselect: () => setAttributes({ preload: void 0 }),
children: /* @__PURE__ */ jsx(SelectControl, {
label: _x("Preload", "noun; Audio block parameter"),
items: PRELOAD_ITEMS,
value: PRELOAD_ITEMS.find((item) => item.value === (preload || null)) ?? PRELOAD_ITEMS[0],
onValueChange: (item) => item && setAttributes({ preload: item.value || void 0 })
})
})
]
}) }),
/* @__PURE__ */ jsxs("figure", {
...blockProps,
children: [
/* @__PURE__ */ jsx("audio", {
controls: "controls",
inert: inertValue(!isSingleSelected),
src: src ?? temporaryURL
}),
!!temporaryURL && /* @__PURE__ */ jsx(Spinner, {}),
/* @__PURE__ */ jsx(Caption, {
attributes,
setAttributes,
isSelected: isSingleSelected,
insertBlocksAfter,
label: __("Audio caption text"),
showToolbarButton: isSingleSelected && hasNonContentControls
})
]
})
] });
}
export { AudioEdit as default };
//# sourceMappingURL=edit.mjs.map