@wordpress/block-library
Version:
Block library for the WordPress editor.
138 lines (137 loc) • 5.18 kB
JavaScript
import clsx from "clsx";
import { Placeholder, ResizableBox, Spinner } from "@wordpress/components";
import { BlockControls, BlockIcon, MediaPlaceholder, MediaReplaceFlow, store } from "@wordpress/block-editor";
import { __ } from "@wordpress/i18n";
import { useViewportMatch } from "@wordpress/compose";
import { useDispatch } from "@wordpress/data";
import { forwardRef } from "@wordpress/element";
import { createBlobURL, isBlobURL } from "@wordpress/blob";
import { store as store$1 } from "@wordpress/notices";
import { media } from "@wordpress/icons";
import { imageFillStyles } from "./image-fill.mjs";
import { ALLOWED_MEDIA_TYPES } from "./constants.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/media-text/media-container.jsx
/**
* Constants
*/
const noop = () => {};
const ResizableBoxContainer = forwardRef(({ isSelected, isStackedOnMobile, ...props }, ref) => {
const isMobile = useViewportMatch("small", "<");
return /* @__PURE__ */ jsx(ResizableBox, {
ref,
showHandle: isSelected && (!isMobile || !isStackedOnMobile),
...props
});
});
function ToolbarEditButton({ mediaId, mediaUrl, onSelectMedia, toggleUseFeaturedImage, useFeaturedImage }) {
return /* @__PURE__ */ jsx(BlockControls, {
group: "other",
children: /* @__PURE__ */ jsx(MediaReplaceFlow, {
mediaId,
mediaURL: mediaUrl,
allowedTypes: ALLOWED_MEDIA_TYPES,
onSelect: onSelectMedia,
onToggleFeaturedImage: toggleUseFeaturedImage,
useFeaturedImage,
name: mediaUrl ? __("Replace") : __("Add media"),
onReset: () => onSelectMedia(void 0)
})
});
}
function PlaceholderContainer({ className, mediaUrl, onSelectMedia, toggleUseFeaturedImage }) {
const { createErrorNotice } = useDispatch(store$1);
const onUploadError = (message) => {
createErrorNotice(message, { type: "snackbar" });
};
const onFilesPreUpload = (files) => {
if (files.length === 1) onSelectMedia({ url: createBlobURL(files[0]) });
};
return /* @__PURE__ */ jsx(MediaPlaceholder, {
icon: /* @__PURE__ */ jsx(BlockIcon, { icon: media }),
labels: { title: __("Media area") },
className,
onSelect: onSelectMedia,
onToggleFeaturedImage: toggleUseFeaturedImage,
allowedTypes: ALLOWED_MEDIA_TYPES,
onFilesPreUpload,
onError: onUploadError,
disableMediaButtons: mediaUrl
});
}
function MediaContainer(props, ref) {
const { className, commitWidthChange, focalPoint, imageFill, isSelected, isStackedOnMobile, mediaAlt, mediaId, mediaPosition, mediaType, mediaUrl, mediaWidth, onSelectMedia, onWidthChange, enableResize, toggleUseFeaturedImage, useFeaturedImage, featuredImageURL, featuredImageAlt, refMedia } = props;
const isTemporaryMedia = !mediaId && isBlobURL(mediaUrl);
const { toggleSelection } = useDispatch(store);
const toolbarEditButton = /* @__PURE__ */ jsx(ToolbarEditButton, {
onSelectMedia,
mediaUrl: useFeaturedImage && featuredImageURL ? featuredImageURL : mediaUrl,
mediaId,
toggleUseFeaturedImage,
useFeaturedImage
});
if (mediaUrl || featuredImageURL || useFeaturedImage) {
const onResizeStart = () => {
toggleSelection(false);
};
const onResize = (event, direction, elt) => {
onWidthChange(parseInt(elt.style.width));
};
const onResizeStop = (event, direction, elt) => {
toggleSelection(true);
commitWidthChange(parseInt(elt.style.width));
};
const enablePositions = {
right: enableResize && mediaPosition === "left",
left: enableResize && mediaPosition === "right"
};
const positionStyles = mediaType === "image" && imageFill ? imageFillStyles(mediaUrl || featuredImageURL, focalPoint) : {};
return /* @__PURE__ */ jsxs(ResizableBoxContainer, {
as: "figure",
className: clsx(className, "editor-media-container__resizer", { "is-transient": isTemporaryMedia }),
size: { width: mediaWidth + "%" },
minWidth: "10%",
maxWidth: "100%",
enable: enablePositions,
onResizeStart,
onResize,
onResizeStop,
axis: "x",
isSelected,
isStackedOnMobile,
ref,
children: [
toolbarEditButton,
({
image: () => useFeaturedImage && featuredImageURL ? /* @__PURE__ */ jsx("img", {
ref: refMedia,
src: featuredImageURL,
alt: featuredImageAlt,
style: positionStyles
}) : mediaUrl && /* @__PURE__ */ jsx("img", {
ref: refMedia,
src: mediaUrl,
alt: mediaAlt,
style: positionStyles
}),
video: () => /* @__PURE__ */ jsx("video", {
controls: true,
ref: refMedia,
src: mediaUrl
})
}[mediaType] || noop)(),
isTemporaryMedia && /* @__PURE__ */ jsx(Spinner, {}),
!useFeaturedImage && /* @__PURE__ */ jsx(PlaceholderContainer, { ...props }),
!featuredImageURL && useFeaturedImage && /* @__PURE__ */ jsx(Placeholder, {
className: "wp-block-media-text--placeholder-image",
style: positionStyles,
withIllustration: true
})
]
});
}
return /* @__PURE__ */ jsxs(Fragment, { children: [toolbarEditButton, /* @__PURE__ */ jsx(PlaceholderContainer, { ...props })] });
}
var media_container_default = forwardRef(MediaContainer);
export { media_container_default as default };
//# sourceMappingURL=media-container.mjs.map