@wordpress/block-library
Version:
Block library for the WordPress editor.
97 lines (96 loc) • 4.13 kB
JavaScript
import { useState } from "@wordpress/element";
import { BlockControls, MediaReplaceFlow, __experimentalBlockAlignmentMatrixControl, __experimentalBlockFullHeightAligmentControl, privateApis, useSettings } from "@wordpress/block-editor";
import { __ } from "@wordpress/i18n";
import { MenuItem, ToolbarButton } from "@wordpress/components";
import { crop, link } from "@wordpress/icons";
import { ALLOWED_MEDIA_TYPES, EMBED_VIDEO_BACKGROUND_TYPE } from "../shared.mjs";
import { unlock } from "../../lock-unlock.mjs";
import EmbedVideoUrlInput from "./embed-video-url-input.mjs";
import { getAllowedVideoProviders } from "../embed-video-utils.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/cover/edit/block-controls.jsx
const { cleanEmptyObject } = unlock(privateApis);
const FULL_HEIGHT = "100vh";
function CoverBlockControls({ attributes, setAttributes, onSelectMedia, currentSettings, toggleUseFeaturedImage, onClearMedia, onSelectEmbedUrl, onEditMedia, editMediaButtonRef, showEditMediaButton, isEditMediaDisabled, blockEditingMode }) {
const { contentPosition, id, useFeaturedImage, style, backgroundType, allowedVideoProviders } = attributes;
const { hasInnerBlocks, url } = currentSettings;
const filteredVideoProviders = getAllowedVideoProviders(allowedVideoProviders);
const hasAllowedVideoProviders = filteredVideoProviders.length > 0;
const [isMinHeightEnabled] = useSettings("dimensions.minHeight");
const minHeight = style?.dimensions?.minHeight;
const [prevMinHeight, setPrevMinHeight] = useState(minHeight);
const [isEmbedUrlInputOpen, setIsEmbedUrlInputOpen] = useState(false);
const isMinFullHeight = minHeight === FULL_HEIGHT && !style?.dimensions?.aspectRatio;
const isContentOnlyMode = blockEditingMode === "contentOnly";
const setMinHeight = (nextMinHeight) => setAttributes({ style: cleanEmptyObject({
...style,
dimensions: {
...style?.dimensions,
minHeight: nextMinHeight,
aspectRatio: void 0
}
}) });
const toggleMinFullHeight = () => {
if (isMinFullHeight) return setMinHeight(prevMinHeight === FULL_HEIGHT ? void 0 : prevMinHeight);
setPrevMinHeight(minHeight);
return setMinHeight(FULL_HEIGHT);
};
return /* @__PURE__ */ jsxs(Fragment, { children: [
!isContentOnlyMode && /* @__PURE__ */ jsxs(BlockControls, {
group: "block",
children: [
/* @__PURE__ */ jsx(__experimentalBlockAlignmentMatrixControl, {
label: __("Change content position"),
value: contentPosition,
onChange: (nextPosition) => setAttributes({ contentPosition: nextPosition }),
isDisabled: !hasInnerBlocks
}),
isMinHeightEnabled && /* @__PURE__ */ jsx(__experimentalBlockFullHeightAligmentControl, {
isActive: isMinFullHeight,
onToggle: toggleMinFullHeight,
isDisabled: !hasInnerBlocks
}),
showEditMediaButton && /* @__PURE__ */ jsx(ToolbarButton, {
ref: editMediaButtonRef,
icon: crop,
label: __("Edit image"),
onClick: onEditMedia,
"aria-haspopup": "dialog",
disabled: isEditMediaDisabled
})
]
}),
/* @__PURE__ */ jsx(BlockControls, {
group: "other",
children: /* @__PURE__ */ jsx(MediaReplaceFlow, {
mediaId: id,
mediaURL: url,
allowedTypes: ALLOWED_MEDIA_TYPES,
onSelect: onSelectMedia,
onToggleFeaturedImage: toggleUseFeaturedImage,
useFeaturedImage,
name: !url ? __("Add media") : __("Replace"),
onReset: onClearMedia,
variant: "toolbar",
children: ({ onClose }) => hasAllowedVideoProviders ? /* @__PURE__ */ jsx(MenuItem, {
icon: link,
onClick: () => {
setIsEmbedUrlInputOpen(true);
onClose();
},
children: __("Embed video from URL")
}) : null
})
}),
isEmbedUrlInputOpen && /* @__PURE__ */ jsx(EmbedVideoUrlInput, {
onSubmit: (embedUrl) => {
onSelectEmbedUrl(embedUrl);
},
onClose: () => setIsEmbedUrlInputOpen(false),
initialUrl: backgroundType === EMBED_VIDEO_BACKGROUND_TYPE ? url : "",
allowedVideoProviders: filteredVideoProviders
})
] });
}
export { CoverBlockControls as default };
//# sourceMappingURL=block-controls.mjs.map