@wordpress/block-library
Version:
Block library for the WordPress editor.
641 lines (640 loc) • 27.3 kB
JavaScript
import clsx from "clsx";
import { Button, MenuGroup, MenuItem, RangeControl, SelectControl, ToggleControl, ToolbarDropdownMenu, __experimentalToggleGroupControl, __experimentalToggleGroupControlOption, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components";
import { BlockControls, InspectorControls, MediaPlaceholder, MediaReplaceFlow, store, useBlockEditingMode, useBlockProps, useInnerBlocksProps, useSettings } from "@wordpress/block-editor";
import { useEffect, useMemo, useRef, useState } from "@wordpress/element";
import { __, _x, sprintf } from "@wordpress/i18n";
import { useDispatch, useSelect } from "@wordpress/data";
import { createBlock } from "@wordpress/blocks";
import { createBlobURL } from "@wordpress/blob";
import { store as store$1 } from "@wordpress/notices";
import { customLink, fullscreen, image, link, linkOff } from "@wordpress/icons";
import { sharedIcon } from "./shared-icon.mjs";
import { defaultColumnsNumber, isGalleryFlexLayout, isObject, pickRelevantMediaFiles } from "./shared.mjs";
import { getHrefAndDestination } from "./utils.mjs";
import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs";
import { getImageSizeAttributes, getUpdatedLinkTargetSettings } from "../image/utils.mjs";
import Gallery from "./gallery.mjs";
import { DEFAULT_MEDIA_SIZE_SLUG, LINK_DESTINATION_ATTACHMENT, LINK_DESTINATION_LIGHTBOX, LINK_DESTINATION_MEDIA, LINK_DESTINATION_NONE, MAX_COLUMNS } from "./constants.mjs";
import useImageSizes from "./use-image-sizes.mjs";
import useGetNewImages from "./use-get-new-images.mjs";
import useGetMedia from "./use-get-media.mjs";
import GalleryStyles from "./gallery-styles.mjs";
import useDynamicGallery from "./use-dynamic-gallery.mjs";
import { GalleryDynamicView, GallerySourcePanel } from "./dynamic-gallery.mjs";
import { SortImagesControl, SourceOrderControl } from "./order-controls.mjs";
import { ATTACHED_MEDIA, DEFAULT_ORDER, DEFAULT_ORDERBY, getDynamicSource } from "./dynamic-source.mjs";
import { hasSortableImages, sortImageBlocks } from "./order-images.mjs";
import { getUpdatedGalleryStyle, getViewportGalleryStyle, isValidGalleryColumns } from "./responsive-styles.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/gallery/edit.jsx
const LINK_OPTIONS = [
{
icon: customLink,
label: __("Link images to attachment pages"),
value: LINK_DESTINATION_ATTACHMENT,
noticeText: __("Attachment Pages")
},
{
icon: image,
label: __("Link images to media files"),
value: LINK_DESTINATION_MEDIA,
noticeText: __("Media Files")
},
{
icon: fullscreen,
label: __("Enlarge on click"),
value: LINK_DESTINATION_LIGHTBOX,
noticeText: __("Lightbox effect"),
infoText: __("Scale images with a lightbox effect")
},
{
icon: linkOff,
label: _x("None", "Media item link option"),
value: LINK_DESTINATION_NONE,
noticeText: __("None")
}
];
const NAVIGATION_BUTTON_TYPE_OPTIONS = [
{
label: __("Icon"),
value: "icon"
},
{
label: __("Text"),
value: "text"
},
{
label: __("Both"),
value: "both"
}
];
const ALLOWED_MEDIA_TYPES = ["image"];
const PLACEHOLDER_TEXT = __("Drag and drop images, upload, or choose from your library.");
const DEFAULT_BLOCK = { name: "core/image" };
const EMPTY_ARRAY = [];
function GalleryEdit(props) {
const { setAttributes, attributes, className, clientId, isSelected, insertBlocksAfter, isContentLocked, context, __unstableLayoutClassNames: layoutClassNames } = props;
const postId = context?.postId;
const postType = context?.postType;
const blockEditingMode = useBlockEditingMode();
const [lightboxSetting, defaultRatios, themeRatios, showDefaultRatios] = useSettings("blocks.core/image.lightbox", "dimensions.aspectRatios.default", "dimensions.aspectRatios.theme", "dimensions.defaultAspectRatios");
const linkOptions = !lightboxSetting?.allowEditing ? LINK_OPTIONS.filter((option) => option.value !== LINK_DESTINATION_LIGHTBOX) : LINK_OPTIONS;
const { align, navigationButtonType, columns, imageCrop, randomOrder, linkTarget, linkTo, sizeSlug, aspectRatio, layout } = attributes;
const isFlexLayout = isGalleryFlexLayout(layout);
const previousLayoutRef = useRef(layout);
const { __unstableMarkNextChangeAsNotPersistent, replaceInnerBlocks, updateBlockAttributes, selectBlock } = useDispatch(store);
const { createSuccessNotice, createErrorNotice } = useDispatch(store$1);
useEffect(() => {
const previousLayout = previousLayoutRef.current;
previousLayoutRef.current = layout;
if (!isObject(previousLayout) || !isObject(layout) || previousLayout.type === layout.type) return;
const nextLayout = { ...layout };
let hasPreservedGridSetting = false;
if (!Object.hasOwn(layout, "columnCount") && Number.isInteger(previousLayout.columnCount) && previousLayout.columnCount > 0) {
nextLayout.columnCount = previousLayout.columnCount;
hasPreservedGridSetting = true;
}
if (!Object.hasOwn(layout, "minimumColumnWidth") && typeof previousLayout.minimumColumnWidth === "string") {
nextLayout.minimumColumnWidth = previousLayout.minimumColumnWidth;
hasPreservedGridSetting = true;
}
if (hasPreservedGridSetting) {
previousLayoutRef.current = nextLayout;
__unstableMarkNextChangeAsNotPersistent();
setAttributes({ layout: nextLayout });
}
}, [
layout,
setAttributes,
__unstableMarkNextChangeAsNotPersistent
]);
const { getBlock, getSettings, innerBlockImages, multiGallerySelection, selectedStyleState } = useSelect((select) => {
const { getBlockName, getMultiSelectedBlockClientIds, getSettings: _getSettings, getBlock: _getBlock, getSelectedBlockStyleState } = select(store);
const multiSelectedClientIds = getMultiSelectedBlockClientIds();
return {
getBlock: _getBlock,
getSettings: _getSettings,
innerBlockImages: _getBlock(clientId)?.innerBlocks ?? EMPTY_ARRAY,
multiGallerySelection: multiSelectedClientIds.length && multiSelectedClientIds.every((_clientId) => getBlockName(_clientId) === "core/gallery"),
selectedStyleState: getSelectedBlockStyleState(clientId)
};
}, [clientId]);
const isViewportStyleState = selectedStyleState?.viewport && selectedStyleState.viewport !== "default";
const viewportStyle = isViewportStyleState ? getViewportGalleryStyle(attributes.style, selectedStyleState.viewport) : {};
const baseColumns = isValidGalleryColumns(columns) ? columns : void 0;
const baseImageCrop = typeof imageCrop === "boolean" ? imageCrop : true;
const baseAspectRatio = aspectRatio || "auto";
const hasViewportColumns = isViewportStyleState && Object.hasOwn(viewportStyle, "columns") && isValidGalleryColumns(viewportStyle.columns);
const hasViewportImageCrop = isViewportStyleState && Object.hasOwn(viewportStyle, "imageCrop") && typeof viewportStyle.imageCrop === "boolean";
const hasViewportAspectRatio = isViewportStyleState && Object.hasOwn(viewportStyle, "aspectRatio") && typeof viewportStyle.aspectRatio === "string" && !!viewportStyle.aspectRatio;
const activeColumns = hasViewportColumns ? viewportStyle.columns : baseColumns;
const activeImageCrop = hasViewportImageCrop ? viewportStyle.imageCrop : baseImageCrop;
const activeAspectRatio = hasViewportAspectRatio ? viewportStyle.aspectRatio : baseAspectRatio;
const images = useMemo(() => innerBlockImages?.map((block) => ({
clientId: block.clientId,
id: block.attributes.id,
url: block.attributes.url,
attributes: block.attributes,
fromSavedContent: Boolean(block.originalContent)
})), [innerBlockImages]);
const imageData = useGetMedia(innerBlockImages);
const newImages = useGetNewImages(images, imageData);
const hasImages = !!images.length;
const isDynamic = !!attributes.dynamicContent;
const [appliedSort, setAppliedSort] = useState(null);
const currentOrder = appliedSort && appliedSort.clientIds.length === innerBlockImages.length && appliedSort.clientIds.every((id, index) => innerBlockImages[index].clientId === id) ? appliedSort.order : null;
const canSortImages = hasSortableImages(innerBlockImages, imageData);
const dynamic = useDynamicGallery({
attributes,
setAttributes,
clientId,
postId,
postType
});
const displayedImageCount = isDynamic ? dynamic.dynamicMedia.length : images.length;
let hasLightboxImages;
if (isDynamic) hasLightboxImages = linkTo === LINK_DESTINATION_LIGHTBOX;
else if (lightboxSetting?.enabled) hasLightboxImages = images.filter((image) => image.attributes?.lightbox?.enabled === void 0 || image.attributes?.lightbox?.enabled === true).length > 0;
else hasLightboxImages = images.filter((image) => image.attributes.lightbox?.enabled).length > 0;
const themeOptions = themeRatios?.map(({ name, ratio }) => ({
label: name,
value: ratio
}));
const defaultOptions = defaultRatios?.map(({ name, ratio }) => ({
label: name,
value: ratio
}));
const aspectRatioOptions = [
{
label: _x("Original", "Aspect ratio option for dimensions control"),
value: "auto"
},
...showDefaultRatios ? defaultOptions || [] : [],
...themeOptions || []
];
useEffect(() => {
newImages?.forEach((newImage) => {
__unstableMarkNextChangeAsNotPersistent();
updateBlockAttributes(newImage.clientId, {
...buildImageAttributes(newImage.attributes),
id: newImage.id,
align: void 0
});
});
}, [newImages]);
const imageSizeOptions = useImageSizes(isDynamic ? dynamic.dynamicMedia : imageData, isSelected, getSettings);
/**
* Determines the image attributes that should be applied to an image block
* after the gallery updates.
*
* The gallery will receive the full collection of images when a new image
* is added. As a result we need to reapply the image's original settings if
* it already existed in the gallery. If the image is in fact new, we need
* to apply the gallery's current settings to the image.
*
* @param {Object} imageAttributes Media object for the actual image.
* @return {Object} Attributes to set on the new image block.
*/
function buildImageAttributes(imageAttributes) {
const image = imageAttributes.id ? imageData.find(({ id }) => id === imageAttributes.id) : null;
let newClassName;
if (imageAttributes.className && imageAttributes.className !== "") newClassName = imageAttributes.className;
let newLinkTarget;
if (imageAttributes.linkTarget || imageAttributes.rel) newLinkTarget = {
linkTarget: imageAttributes.linkTarget,
rel: imageAttributes.rel
};
else newLinkTarget = getUpdatedLinkTargetSettings(linkTarget, attributes);
return {
...pickRelevantMediaFiles(image, sizeSlug),
...getHrefAndDestination(image, linkTo, imageAttributes?.linkDestination),
...newLinkTarget,
className: newClassName,
sizeSlug,
caption: imageAttributes.caption.length > 0 ? imageAttributes.caption : image.caption?.raw,
alt: imageAttributes.alt || image.alt_text,
aspectRatio: aspectRatio === "auto" ? void 0 : aspectRatio,
scale: aspectRatio && aspectRatio !== "auto" ? "cover" : void 0
};
}
function isValidFileType(file) {
const mediaTypeSelector = file.type;
return ALLOWED_MEDIA_TYPES.some((mediaType) => mediaTypeSelector?.indexOf(mediaType) === 0) || file.blob;
}
function updateImages(selectedImages) {
const newFileUploads = Object.prototype.toString.call(selectedImages) === "[object FileList]";
const imageArray = newFileUploads ? Array.from(selectedImages).map((file) => {
if (!file.url) return { blob: createBlobURL(file) };
return file;
}) : selectedImages;
if (!imageArray.every(isValidFileType)) createErrorNotice(__("If uploading to a gallery all files need to be image formats"), {
id: "gallery-upload-invalid-file",
type: "snackbar"
});
const processedImages = imageArray.filter((file) => file.url || isValidFileType(file)).map((file) => {
if (!file.url) return { blob: file.blob || createBlobURL(file) };
return file;
});
const newOrderMap = processedImages.reduce((result, image, index) => (result[image.id] = index, result), {});
const existingImageBlocks = !newFileUploads ? innerBlockImages.filter((block) => processedImages.find((img) => img.id === block.attributes.id)) : innerBlockImages;
const newBlocks = processedImages.filter((img) => !existingImageBlocks.find((existingImg) => img.id === existingImg.attributes.id)).map((image) => {
return createBlock("core/image", {
id: image.id,
blob: image.blob,
url: image.url,
caption: image.caption,
alt: image.alt
});
});
replaceInnerBlocks(clientId, existingImageBlocks.concat(newBlocks).sort((a, b) => newOrderMap[a.attributes.id] - newOrderMap[b.attributes.id]));
if (newBlocks?.length > 0) selectBlock(newBlocks[0].clientId);
}
function selectRandomOrder() {
setAttributes({ randomOrder: true });
}
function clearRandomOrderAfterEdit() {
if (randomOrder) {
__unstableMarkNextChangeAsNotPersistent();
setAttributes({ randomOrder: false });
}
}
function sortImages(order) {
const sortedBlocks = sortImageBlocks(getBlock(clientId).innerBlocks, imageData, order);
replaceInnerBlocks(clientId, sortedBlocks);
clearRandomOrderAfterEdit();
setAppliedSort({
order,
clientIds: sortedBlocks.map((block) => block.clientId)
});
}
function selectCustomOrder() {
setAttributes({ randomOrder: false });
setAppliedSort(null);
}
function changeSourceOrder({ orderby, order }) {
dynamic.setSourceOrder(orderby, order);
clearRandomOrderAfterEdit();
}
function onUploadError(message) {
createErrorNotice(message, { type: "snackbar" });
}
function setLinkTo(value) {
setAttributes({ linkTo: value });
const changedAttributes = {};
const blocks = [];
getBlock(clientId).innerBlocks.forEach((block) => {
blocks.push(block.clientId);
const image = block.attributes.id ? imageData.find(({ id }) => id === block.attributes.id) : null;
changedAttributes[block.clientId] = getHrefAndDestination(image, value, false, block.attributes, lightboxSetting);
});
updateBlockAttributes(blocks, changedAttributes, { uniqueByBlock: true });
const linkToText = [...linkOptions].find((linkType) => linkType.value === value);
createSuccessNotice(sprintf(__("All gallery image links updated to: %s"), linkToText.noticeText), {
id: "gallery-attributes-linkTo",
type: "snackbar"
});
}
function setGallerySettings(settings) {
if (!isViewportStyleState) {
setAttributes(settings);
return;
}
setAttributes({ style: getUpdatedGalleryStyle({
style: attributes.style,
viewport: selectedStyleState.viewport,
baseSettings: {
columns: baseColumns,
imageCrop: baseImageCrop,
aspectRatio: baseAspectRatio
},
settings
}) });
}
function setColumnsNumber(value) {
setGallerySettings({ columns: value });
}
function toggleImageCrop() {
setGallerySettings({ imageCrop: !activeImageCrop });
}
function toggleOpenInNewTab(openInNewTab) {
const newLinkTarget = openInNewTab ? "_blank" : void 0;
setAttributes({ linkTarget: newLinkTarget });
const changedAttributes = {};
const blocks = [];
getBlock(clientId).innerBlocks.forEach((block) => {
blocks.push(block.clientId);
changedAttributes[block.clientId] = getUpdatedLinkTargetSettings(newLinkTarget, block.attributes);
});
updateBlockAttributes(blocks, changedAttributes, { uniqueByBlock: true });
const noticeText = openInNewTab ? __("All gallery images updated to open in new tab") : __("All gallery images updated to not open in new tab");
createSuccessNotice(noticeText, {
id: "gallery-attributes-openInNewTab",
type: "snackbar"
});
}
function updateImagesSize(newSizeSlug) {
setAttributes({ sizeSlug: newSizeSlug });
const changedAttributes = {};
const blocks = [];
getBlock(clientId).innerBlocks.forEach((block) => {
blocks.push(block.clientId);
const image = block.attributes.id ? imageData.find(({ id }) => id === block.attributes.id) : null;
changedAttributes[block.clientId] = getImageSizeAttributes(image, newSizeSlug);
});
updateBlockAttributes(blocks, changedAttributes, { uniqueByBlock: true });
const imageSize = imageSizeOptions.find((size) => size.value === newSizeSlug);
createSuccessNotice(sprintf(__("All gallery image sizes updated to: %s"), imageSize?.label ?? newSizeSlug), {
id: "gallery-attributes-sizeSlug",
type: "snackbar"
});
}
/**
* Sets the Gallery's aspect ratio.
*
* The base value is added to the img element as an inline
* style. A viewport value is instead rendered
* from the Gallery's responsive CSS, which overrides that inline style.
*
* @param {string|undefined} value Aspect ratio to set. In a viewport state
* `'auto'` is an override that cancels the
* base ratio, while `undefined` removes the
* override so the base value applies again.
*/
function setAspectRatio(value) {
if (isViewportStyleState) setGallerySettings({ aspectRatio: value });
else {
const cleanValue = !value || value === "auto" ? void 0 : value;
setAttributes({ aspectRatio: value ?? "auto" });
const changedAttributes = {};
const blocks = [];
getBlock(clientId).innerBlocks.forEach((block) => {
blocks.push(block.clientId);
changedAttributes[block.clientId] = {
aspectRatio: cleanValue,
scale: cleanValue ? "cover" : void 0
};
});
updateBlockAttributes(blocks, changedAttributes, true);
}
const noticeValue = value ?? baseAspectRatio;
const aspectRatioText = aspectRatioOptions.find((option) => option.value === noticeValue);
const noticeText = isViewportStyleState ? sprintf(__("Gallery images in the %1$s viewport updated to aspect ratio: %2$s"), selectedStyleState.viewport.replace("@", ""), aspectRatioText?.label || noticeValue) : sprintf(__("All gallery images updated to aspect ratio: %s"), aspectRatioText?.label || noticeValue);
createSuccessNotice(noticeText, {
id: "gallery-attributes-aspectRatio",
type: "snackbar"
});
}
useEffect(() => {
if (!linkTo) {
__unstableMarkNextChangeAsNotPersistent();
setAttributes({ linkTo: window?.wp?.media?.view?.settings?.defaultProps?.link || LINK_DESTINATION_NONE });
}
}, [linkTo]);
const hasImageIds = hasImages && images.some((image) => !!image.id);
const mediaPlaceholderProps = {
addToGallery: false,
disableMediaButtons: images.some((img) => !img.id && img.url?.indexOf("blob:") === 0),
value: {}
};
const blockProps = useBlockProps({ className: clsx(className, "has-nested-images", `wp-block-gallery-${clientId}`, isDynamic && [
layoutClassNames,
"blocks-gallery-grid",
{
[`align${align}`]: align,
[`columns-${columns}`]: isFlexLayout && columns !== void 0,
"columns-default": isFlexLayout && columns === void 0,
"is-cropped": isFlexLayout && imageCrop
}
]) });
const innerBlocksProps = useInnerBlocksProps(blockProps, {
defaultBlock: DEFAULT_BLOCK,
directInsert: true,
orientation: "horizontal",
renderAppender: false,
allowedBlocks: isDynamic ? EMPTY_ARRAY : void 0
});
const dropdownMenuProps = useToolsPanelDropdownMenuProps();
if (!hasImages && !isDynamic) return /* @__PURE__ */ jsxs("div", {
...innerBlocksProps,
children: [innerBlocksProps.children, /* @__PURE__ */ jsx(MediaPlaceholder, {
handleUpload: false,
icon: sharedIcon,
labels: {
title: __("Gallery"),
instructions: PLACEHOLDER_TEXT
},
onSelect: updateImages,
allowedTypes: ALLOWED_MEDIA_TYPES,
multiple: true,
onError: onUploadError,
...mediaPlaceholderProps,
children: blockEditingMode === "default" && /* @__PURE__ */ jsx(Button, {
__next40pxDefaultSize: true,
variant: "secondary",
onClick: dynamic.enableDynamicMode,
children: getDynamicSource(ATTACHED_MEDIA).title
})
})]
});
const hasLinkTo = linkTo && linkTo !== "none";
const hasViewportSettings = isFlexLayout || aspectRatioOptions.length > 1;
return /* @__PURE__ */ jsxs(Fragment, { children: [
/* @__PURE__ */ jsxs(InspectorControls, {
group: isViewportStyleState && hasViewportSettings ? "style-state" : "default",
children: [!isViewportStyleState && /* @__PURE__ */ jsx(GallerySourcePanel, {
dynamic,
hasImages
}), /* @__PURE__ */ jsxs(__experimentalToolsPanel, {
label: __("Settings"),
resetAll: () => {
if (isViewportStyleState) {
setGallerySettings({
columns: void 0,
imageCrop: void 0,
aspectRatio: void 0
});
return;
}
setAttributes({
navigationButtonType: "icon",
randomOrder: false,
...isFlexLayout && {
columns: void 0,
imageCrop: true
}
});
setAspectRatio("auto");
if (isDynamic) dynamic.setSourceOrder(void 0, void 0);
if (sizeSlug !== DEFAULT_MEDIA_SIZE_SLUG) updateImagesSize(DEFAULT_MEDIA_SIZE_SLUG);
if (linkTarget) toggleOpenInNewTab(false);
},
dropdownMenuProps,
children: [
isFlexLayout && displayedImageCount > 1 && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
isShownByDefault: true,
label: __("Columns"),
hasValue: () => isViewportStyleState ? hasViewportColumns : !!activeColumns && activeColumns !== displayedImageCount,
onDeselect: () => setColumnsNumber(void 0),
children: /* @__PURE__ */ jsx(RangeControl, {
label: __("Columns"),
value: activeColumns ? activeColumns : defaultColumnsNumber(displayedImageCount),
onChange: setColumnsNumber,
min: 1,
max: Math.min(MAX_COLUMNS, displayedImageCount),
required: true
})
}),
!isViewportStyleState && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
isShownByDefault: true,
label: __("Order by"),
hasValue: () => !!randomOrder || isDynamic && (dynamic.sourceOrderby !== DEFAULT_ORDERBY || dynamic.sourceOrder !== DEFAULT_ORDER),
onDeselect: () => {
if (isDynamic) {
dynamic.setSourceOrder(void 0, void 0);
clearRandomOrderAfterEdit();
} else selectCustomOrder();
},
children: isDynamic ? /* @__PURE__ */ jsx(SourceOrderControl, {
order: {
orderby: dynamic.sourceOrderby,
order: dynamic.sourceOrder
},
isRandom: !!randomOrder,
onChange: changeSourceOrder,
onSelectRandom: selectRandomOrder
}) : /* @__PURE__ */ jsx(SortImagesControl, {
currentOrder,
isRandom: !!randomOrder,
canSort: canSortImages,
onSort: sortImages,
onSelectCustom: selectCustomOrder,
onSelectRandom: selectRandomOrder
})
}),
!isViewportStyleState && imageSizeOptions?.length > 0 && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
isShownByDefault: true,
label: __("Resolution"),
hasValue: () => sizeSlug !== DEFAULT_MEDIA_SIZE_SLUG,
onDeselect: () => updateImagesSize(DEFAULT_MEDIA_SIZE_SLUG),
children: /* @__PURE__ */ jsx(SelectControl, {
label: __("Resolution"),
help: __("Select the size of the source images."),
value: sizeSlug,
options: imageSizeOptions,
onChange: updateImagesSize,
hideCancelButton: true
})
}),
!isViewportStyleState && hasLinkTo && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
isShownByDefault: true,
label: __("Open images in new tab"),
hasValue: () => !!linkTarget,
onDeselect: () => toggleOpenInNewTab(false),
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Open images in new tab"),
checked: linkTarget === "_blank",
onChange: toggleOpenInNewTab
})
}),
aspectRatioOptions.length > 1 && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
hasValue: () => isViewportStyleState ? hasViewportAspectRatio : activeAspectRatio !== "auto",
label: __("Aspect ratio"),
onDeselect: () => setAspectRatio(isViewportStyleState ? void 0 : "auto"),
isShownByDefault: true,
children: /* @__PURE__ */ jsx(SelectControl, {
label: __("Aspect ratio"),
help: __("Set a consistent aspect ratio for all images in the gallery."),
value: activeAspectRatio,
options: aspectRatioOptions,
onChange: setAspectRatio
})
}),
isFlexLayout && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
isShownByDefault: true,
label: __("Crop images to fit"),
hasValue: () => isViewportStyleState ? hasViewportImageCrop : !activeImageCrop,
onDeselect: () => setGallerySettings({ imageCrop: isViewportStyleState ? void 0 : true }),
children: /* @__PURE__ */ jsx(ToggleControl, {
label: __("Crop images to fit"),
checked: activeImageCrop,
onChange: toggleImageCrop
})
}),
!isViewportStyleState && lightboxSetting?.allowEditing && hasLightboxImages && /* @__PURE__ */ jsx(__experimentalToolsPanelItem, {
label: __("Navigation button type"),
isShownByDefault: true,
hasValue: () => navigationButtonType !== "icon",
onDeselect: () => setAttributes({ navigationButtonType: "icon" }),
children: /* @__PURE__ */ jsx(__experimentalToggleGroupControl, {
label: __("Navigation button type"),
value: navigationButtonType,
onChange: (value) => setAttributes({ navigationButtonType: value }),
isBlock: true,
help: __("Adjust the appearance of buttons in the lightbox."),
children: NAVIGATION_BUTTON_TYPE_OPTIONS.map((option) => /* @__PURE__ */ jsx(__experimentalToggleGroupControlOption, {
value: option.value,
label: option.label
}, option.value))
})
})
]
})]
}),
/* @__PURE__ */ jsx(BlockControls, {
group: "block",
children: /* @__PURE__ */ jsx(ToolbarDropdownMenu, {
icon: link,
label: __("Link"),
children: ({ onClose }) => /* @__PURE__ */ jsx(MenuGroup, { children: linkOptions.map((linkItem) => {
const isOptionSelected = linkTo === linkItem.value;
return /* @__PURE__ */ jsx(MenuItem, {
isSelected: isOptionSelected,
className: clsx("components-dropdown-menu__menu-item", { "is-active": isOptionSelected }),
iconPosition: "left",
icon: linkItem.icon,
onClick: () => {
setLinkTo(linkItem.value);
onClose();
},
role: "menuitemradio",
info: linkItem.infoText,
children: linkItem.label
}, linkItem.value);
}) })
})
}),
/* @__PURE__ */ jsxs(Fragment, { children: [!multiGallerySelection && !isDynamic && /* @__PURE__ */ jsx(BlockControls, {
group: "other",
children: /* @__PURE__ */ jsx(MediaReplaceFlow, {
allowedTypes: ALLOWED_MEDIA_TYPES,
handleUpload: false,
onSelect: updateImages,
name: __("Add"),
multiple: true,
mediaIds: images.filter((image) => image.id).map((image) => image.id),
addToGallery: hasImageIds,
variant: "toolbar"
})
}), /* @__PURE__ */ jsx(GalleryStyles, {
style: attributes.style,
clientId,
isFlexLayout
})] }),
isDynamic ? /* @__PURE__ */ jsx(GalleryDynamicView, {
...props,
dynamic,
blockProps,
innerBlocksProps,
multiGallerySelection
}) : /* @__PURE__ */ jsx(Gallery, {
...props,
isContentLocked,
images,
blockProps: innerBlocksProps,
insertBlocksAfter,
multiGallerySelection
})
] });
}
export { GalleryEdit as default };
//# sourceMappingURL=edit.mjs.map