@wordpress/block-library
Version:
Block library for the WordPress editor.
341 lines (340 loc) • 16.6 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_components = require("@wordpress/components");
let _wordpress_block_editor = require("@wordpress/block-editor");
let _wordpress_ui = require("@wordpress/ui");
let _wordpress_i18n = require("@wordpress/i18n");
let _wordpress_data = require("@wordpress/data");
let _wordpress_notices = require("@wordpress/notices");
let __shared_js = require("./shared.cjs");
let ___utils_hooks_js = require("../utils/hooks.cjs");
let ___image_utils_js = require("../image/utils.cjs");
let __constants_js = require("./constants.cjs");
let __order_controls_tsx = require("./order-controls.cjs");
let __dynamic_source_js = require("./dynamic-source.cjs");
let __order_images_js = require("./order-images.cjs");
let __responsive_styles_js = require("./responsive-styles.cjs");
let react_jsx_runtime = require("react/jsx-runtime");
// packages/block-library/src/gallery/controls.jsx
const NAVIGATION_BUTTON_TYPE_OPTIONS = [
{
label: (0, _wordpress_i18n.__)("Icon"),
value: "icon"
},
{
label: (0, _wordpress_i18n.__)("Text"),
value: "text"
},
{
label: (0, _wordpress_i18n.__)("Both"),
value: "both"
}
];
/**
* Returns the 'Resolution' options: the image sizes at least one image has.
*
* @param {Array} media Media items of the gallery images.
* @param {Array} imageSizes Image sizes from the block editor settings.
*
* @return {Array} Image size options.
*/
function getImageSizeOptions(media, imageSizes) {
return imageSizes.reduce((options, { name, slug }) => {
if (media.some((image) => image.id && (image.sizes?.[slug]?.url || image.media_details?.sizes?.[slug]?.source_url))) options.push({
value: slug,
label: name
});
return options;
}, []);
}
function GalleryControls({ attributes, setAttributes, clientId, viewport, innerBlockImages, mediaById, dynamic, updateInnerImages, appliedSort, setAppliedSort }) {
const { navigationButtonType, columns, imageCrop, randomOrder, linkTarget, linkTo, sizeSlug, aspectRatio, layout } = attributes;
const isDynamic = !!attributes.dynamicContent;
const isFlexLayout = (0, __shared_js.isGalleryFlexLayout)(layout);
const isViewportStyleState = !!viewport;
const [lightboxSetting, defaultRatios, themeRatios, showDefaultRatios] = (0, _wordpress_block_editor.useSettings)("blocks.core/image.lightbox", "dimensions.aspectRatios.default", "dimensions.aspectRatios.theme", "dimensions.defaultAspectRatios");
const imageSizes = (0, _wordpress_data.useSelect)((select) => select(_wordpress_block_editor.store).getSettings().imageSizes, []);
const { getBlocks } = (0, _wordpress_data.useSelect)(_wordpress_block_editor.store);
const { __unstableMarkNextChangeAsNotPersistent, replaceInnerBlocks } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store);
const { createSuccessNotice } = (0, _wordpress_data.useDispatch)(_wordpress_notices.store);
const dropdownMenuProps = (0, ___utils_hooks_js.useToolsPanelDropdownMenuProps)();
const themeOptions = themeRatios?.map(({ name, ratio }) => ({
label: name,
value: ratio
}));
const defaultOptions = defaultRatios?.map(({ name, ratio }) => ({
label: name,
value: ratio
}));
const aspectRatioOptions = [
{
label: (0, _wordpress_i18n._x)("Original", "Aspect ratio option for dimensions control"),
value: "auto"
},
...showDefaultRatios ? defaultOptions || [] : [],
...themeOptions || []
];
const hasViewportSettings = isFlexLayout || aspectRatioOptions.length > 1;
if (isViewportStyleState && !hasViewportSettings) return null;
const viewportStyle = isViewportStyleState ? (0, __responsive_styles_js.getViewportGalleryStyle)(attributes.style, viewport) : {};
const baseColumns = (0, __responsive_styles_js.isValidGalleryColumns)(columns) ? columns : void 0;
const baseImageCrop = typeof imageCrop === "boolean" ? imageCrop : true;
const baseAspectRatio = aspectRatio || "auto";
const hasViewportColumns = isViewportStyleState && Object.hasOwn(viewportStyle, "columns") && (0, __responsive_styles_js.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 currentOrder = appliedSort && appliedSort.clientIds.length === innerBlockImages.length && appliedSort.clientIds.every((id, index) => innerBlockImages[index].clientId === id) ? appliedSort.order : null;
const canSortImages = (0, __order_images_js.hasSortableImages)(innerBlockImages, mediaById);
const displayedImageCount = isDynamic ? dynamic.dynamicMedia.length : innerBlockImages.length;
let hasLightboxImages;
if (isDynamic) hasLightboxImages = linkTo === __constants_js.LINK_DESTINATION_LIGHTBOX;
else if (lightboxSetting?.enabled) hasLightboxImages = innerBlockImages.some((block) => block.attributes?.lightbox?.enabled === void 0 || block.attributes?.lightbox?.enabled === true);
else hasLightboxImages = innerBlockImages.some((block) => block.attributes.lightbox?.enabled);
const imageSizeOptions = getImageSizeOptions(isDynamic ? dynamic.dynamicMedia : Array.from(mediaById.values()), imageSizes);
function selectRandomOrder() {
setAttributes({ randomOrder: true });
}
function clearRandomOrderAfterEdit() {
if (randomOrder) {
__unstableMarkNextChangeAsNotPersistent();
setAttributes({ randomOrder: false });
}
}
function sortImages(order) {
const sortedBlocks = (0, __order_images_js.sortImageBlocks)(getBlocks(clientId), mediaById, 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 setGallerySettings(settings) {
if (!isViewportStyleState) {
setAttributes(settings);
return;
}
setAttributes({ style: (0, __responsive_styles_js.getUpdatedGalleryStyle)({
style: attributes.style,
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 });
updateInnerImages((block) => (0, ___image_utils_js.getUpdatedLinkTargetSettings)(newLinkTarget, block.attributes));
const noticeText = openInNewTab ? (0, _wordpress_i18n.__)("All gallery images updated to open in new tab") : (0, _wordpress_i18n.__)("All gallery images updated to not open in new tab");
createSuccessNotice(noticeText, {
id: "gallery-attributes-openInNewTab",
type: "snackbar"
});
}
function updateImagesSize(newSizeSlug) {
setAttributes({ sizeSlug: newSizeSlug });
updateInnerImages((block, media) => (0, ___image_utils_js.getImageSizeAttributes)(media, newSizeSlug));
const imageSize = imageSizeOptions.find((size) => size.value === newSizeSlug);
createSuccessNotice((0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("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 {
setAttributes({ aspectRatio: value ?? "auto" });
updateInnerImages(() => (0, __shared_js.getAspectRatioAttributes)(value));
}
const noticeValue = value ?? baseAspectRatio;
const aspectRatioText = aspectRatioOptions.find((option) => option.value === noticeValue);
const noticeText = isViewportStyleState ? (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("Gallery images in the %1$s viewport updated to aspect ratio: %2$s"), viewport.replace("@", ""), aspectRatioText?.label || noticeValue) : (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("All gallery images updated to aspect ratio: %s"), aspectRatioText?.label || noticeValue);
createSuccessNotice(noticeText, {
id: "gallery-attributes-aspectRatio",
type: "snackbar"
});
}
function 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 !== __constants_js.DEFAULT_MEDIA_SIZE_SLUG) updateImagesSize(__constants_js.DEFAULT_MEDIA_SIZE_SLUG);
if (linkTarget) toggleOpenInNewTab(false);
}
const hasLinkTo = linkTo && linkTo !== "none";
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.__experimentalToolsPanel, {
label: (0, _wordpress_i18n.__)("Settings"),
resetAll,
dropdownMenuProps,
children: [
isFlexLayout && displayedImageCount > 1 && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, {
isShownByDefault: true,
label: (0, _wordpress_i18n.__)("Columns"),
hasValue: () => isViewportStyleState ? hasViewportColumns : !!activeColumns && activeColumns !== displayedImageCount,
onDeselect: () => setColumnsNumber(void 0),
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.RangeControl, {
label: (0, _wordpress_i18n.__)("Columns"),
value: activeColumns ? activeColumns : (0, __shared_js.defaultColumnsNumber)(displayedImageCount),
onChange: setColumnsNumber,
min: 1,
max: Math.min(__constants_js.MAX_COLUMNS, displayedImageCount),
required: true
})
}),
!isViewportStyleState && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, {
isShownByDefault: true,
label: (0, _wordpress_i18n.__)("Order by"),
hasValue: () => !!randomOrder || isDynamic && (dynamic.sourceOrderby !== __dynamic_source_js.DEFAULT_ORDERBY || dynamic.sourceOrder !== __dynamic_source_js.DEFAULT_ORDER),
onDeselect: () => {
if (isDynamic) {
dynamic.setSourceOrder(void 0, void 0);
clearRandomOrderAfterEdit();
} else selectCustomOrder();
},
children: isDynamic ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__order_controls_tsx.SourceOrderControl, {
order: {
orderby: dynamic.sourceOrderby,
order: dynamic.sourceOrder
},
isRandom: !!randomOrder,
onChange: changeSourceOrder,
onSelectRandom: selectRandomOrder
}) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__order_controls_tsx.SortImagesControl, {
currentOrder,
isRandom: !!randomOrder,
canSort: canSortImages,
onSort: sortImages,
onSelectCustom: selectCustomOrder,
onSelectRandom: selectRandomOrder
})
}),
!isViewportStyleState && imageSizeOptions.length > 0 && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, {
isShownByDefault: true,
label: (0, _wordpress_i18n.__)("Resolution"),
hasValue: () => sizeSlug !== __constants_js.DEFAULT_MEDIA_SIZE_SLUG,
onDeselect: () => updateImagesSize(__constants_js.DEFAULT_MEDIA_SIZE_SLUG),
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.SelectControl, {
label: (0, _wordpress_i18n.__)("Resolution"),
description: (0, _wordpress_i18n.__)("Select the size of the source images."),
value: imageSizeOptions.find((item) => item.value === sizeSlug) ?? imageSizeOptions[0],
items: imageSizeOptions,
onValueChange: (item) => {
if (item && item.value !== sizeSlug) updateImagesSize(item.value);
}
})
}),
!isViewportStyleState && hasLinkTo && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, {
isShownByDefault: true,
label: (0, _wordpress_i18n.__)("Open images in new tab"),
hasValue: () => !!linkTarget,
onDeselect: () => toggleOpenInNewTab(false),
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToggleControl, {
label: (0, _wordpress_i18n.__)("Open images in new tab"),
checked: linkTarget === "_blank",
onChange: toggleOpenInNewTab
})
}),
aspectRatioOptions.length > 1 && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, {
hasValue: () => isViewportStyleState ? hasViewportAspectRatio : activeAspectRatio !== "auto",
label: (0, _wordpress_i18n.__)("Aspect ratio"),
onDeselect: () => setAspectRatio(isViewportStyleState ? void 0 : "auto"),
isShownByDefault: true,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.SelectControl, {
label: (0, _wordpress_i18n.__)("Aspect ratio"),
description: (0, _wordpress_i18n.__)("Set a consistent aspect ratio for all images in the gallery."),
value: aspectRatioOptions.find((item) => item.value === activeAspectRatio) ?? aspectRatioOptions[0],
items: aspectRatioOptions,
onValueChange: (item) => {
if (item && item.value !== activeAspectRatio) setAspectRatio(item.value);
},
children: aspectRatioOptions.map((item, index) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.SelectControl.Item, {
value: item,
label: item.label,
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_ui.SelectControl.ItemLabel, { children: item.label })
}, `${item.value}-${index}`))
})
}),
isFlexLayout && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, {
isShownByDefault: true,
label: (0, _wordpress_i18n.__)("Crop images to fit"),
hasValue: () => isViewportStyleState ? hasViewportImageCrop : !activeImageCrop,
onDeselect: () => setGallerySettings({ imageCrop: isViewportStyleState ? void 0 : true }),
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToggleControl, {
label: (0, _wordpress_i18n.__)("Crop images to fit"),
checked: activeImageCrop,
onChange: toggleImageCrop
})
}),
!isViewportStyleState && lightboxSetting?.allowEditing && hasLightboxImages && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToolsPanelItem, {
label: (0, _wordpress_i18n.__)("Navigation button type"),
isShownByDefault: true,
hasValue: () => navigationButtonType !== "icon",
onDeselect: () => setAttributes({ navigationButtonType: "icon" }),
children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToggleGroupControl, {
label: (0, _wordpress_i18n.__)("Navigation button type"),
value: navigationButtonType,
onChange: (value) => setAttributes({ navigationButtonType: value }),
isBlock: true,
help: (0, _wordpress_i18n.__)("Adjust the appearance of buttons in the lightbox."),
children: NAVIGATION_BUTTON_TYPE_OPTIONS.map((option) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalToggleGroupControlOption, {
value: option.value,
label: option.label
}, option.value))
})
})
]
});
}
exports.default = GalleryControls;
//# sourceMappingURL=controls.cjs.map