@wordpress/block-library
Version:
Block library for the WordPress editor.
107 lines (106 loc) • 4.29 kB
JavaScript
import { useState } from "@wordpress/element";
import { BlockControls, MediaReplaceFlow, __experimentalBlockAlignmentMatrixControl, __experimentalBlockFullHeightAligmentControl, privateApis } 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);
function CoverBlockControls({ attributes, setAttributes, onSelectMedia, currentSettings, toggleUseFeaturedImage, onClearMedia, onSelectEmbedUrl, onEditMedia, editMediaButtonRef, showEditMediaButton, isEditMediaDisabled, blockEditingMode }) {
const { contentPosition, id, useFeaturedImage, minHeight, minHeightUnit, backgroundType, allowedVideoProviders } = attributes;
const { hasInnerBlocks, url } = currentSettings;
const filteredVideoProviders = getAllowedVideoProviders(allowedVideoProviders);
const hasAllowedVideoProviders = filteredVideoProviders.length > 0;
const [prevMinHeightValue, setPrevMinHeightValue] = useState(minHeight);
const [prevMinHeightUnit, setPrevMinHeightUnit] = useState(minHeightUnit);
const [isEmbedUrlInputOpen, setIsEmbedUrlInputOpen] = useState(false);
const isMinFullHeight = minHeightUnit === "vh" && minHeight === 100 && !attributes?.style?.dimensions?.aspectRatio;
const isContentOnlyMode = blockEditingMode === "contentOnly";
const toggleMinFullHeight = () => {
if (isMinFullHeight) {
if (prevMinHeightUnit === "vh" && prevMinHeightValue === 100) return setAttributes({
minHeight: void 0,
minHeightUnit: void 0
});
return setAttributes({
minHeight: prevMinHeightValue,
minHeightUnit: prevMinHeightUnit
});
}
setPrevMinHeightValue(minHeight);
setPrevMinHeightUnit(minHeightUnit);
return setAttributes({
minHeight: 100,
minHeightUnit: "vh",
style: cleanEmptyObject({
...attributes?.style,
dimensions: {
...attributes?.style?.dimensions,
aspectRatio: void 0
}
})
});
};
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
}),
/* @__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