UNPKG

@wordpress/block-library

Version:
336 lines (335 loc) • 14.8 kB
import { RangeControl, ToggleControl, __experimentalToggleGroupControl, __experimentalToggleGroupControlOption, __experimentalToolsPanel, __experimentalToolsPanelItem } from "@wordpress/components"; import { store, useSettings } from "@wordpress/block-editor"; import { SelectControl } from "@wordpress/ui"; import { __, _x, sprintf } from "@wordpress/i18n"; import { useDispatch, useSelect } from "@wordpress/data"; import { store as store$1 } from "@wordpress/notices"; import { defaultColumnsNumber, getAspectRatioAttributes, isGalleryFlexLayout } from "./shared.mjs"; import { useToolsPanelDropdownMenuProps } from "../utils/hooks.mjs"; import { getImageSizeAttributes, getUpdatedLinkTargetSettings } from "../image/utils.mjs"; import { DEFAULT_MEDIA_SIZE_SLUG, LINK_DESTINATION_LIGHTBOX, MAX_COLUMNS } from "./constants.mjs"; import { SortImagesControl, SourceOrderControl } from "./order-controls.mjs"; import { DEFAULT_ORDER, DEFAULT_ORDERBY } from "./dynamic-source.mjs"; import { hasSortableImages, sortImageBlocks } from "./order-images.mjs"; import { getUpdatedGalleryStyle, getViewportGalleryStyle, isValidGalleryColumns } from "./responsive-styles.mjs"; import { jsx, jsxs } from "react/jsx-runtime"; // packages/block-library/src/gallery/controls.jsx const NAVIGATION_BUTTON_TYPE_OPTIONS = [ { label: __("Icon"), value: "icon" }, { label: __("Text"), value: "text" }, { label: __("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 = isGalleryFlexLayout(layout); const isViewportStyleState = !!viewport; const [lightboxSetting, defaultRatios, themeRatios, showDefaultRatios] = useSettings("blocks.core/image.lightbox", "dimensions.aspectRatios.default", "dimensions.aspectRatios.theme", "dimensions.defaultAspectRatios"); const imageSizes = useSelect((select) => select(store).getSettings().imageSizes, []); const { getBlocks } = useSelect(store); const { __unstableMarkNextChangeAsNotPersistent, replaceInnerBlocks } = useDispatch(store); const { createSuccessNotice } = useDispatch(store$1); const dropdownMenuProps = useToolsPanelDropdownMenuProps(); 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 || [] ]; const hasViewportSettings = isFlexLayout || aspectRatioOptions.length > 1; if (isViewportStyleState && !hasViewportSettings) return null; const viewportStyle = isViewportStyleState ? getViewportGalleryStyle(attributes.style, 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 currentOrder = appliedSort && appliedSort.clientIds.length === innerBlockImages.length && appliedSort.clientIds.every((id, index) => innerBlockImages[index].clientId === id) ? appliedSort.order : null; const canSortImages = hasSortableImages(innerBlockImages, mediaById); const displayedImageCount = isDynamic ? dynamic.dynamicMedia.length : innerBlockImages.length; let hasLightboxImages; if (isDynamic) hasLightboxImages = linkTo === 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 = 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: 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) => getUpdatedLinkTargetSettings(newLinkTarget, block.attributes)); 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 }); updateInnerImages((block, media) => getImageSizeAttributes(media, newSizeSlug)); 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 { setAttributes({ aspectRatio: value ?? "auto" }); updateInnerImages(() => getAspectRatioAttributes(value)); } 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"), 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" }); } 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 !== DEFAULT_MEDIA_SIZE_SLUG) updateImagesSize(DEFAULT_MEDIA_SIZE_SLUG); if (linkTarget) toggleOpenInNewTab(false); } const hasLinkTo = linkTo && linkTo !== "none"; return /* @__PURE__ */ jsxs(__experimentalToolsPanel, { label: __("Settings"), resetAll, 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"), description: __("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__ */ 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"), description: __("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__ */ jsx(SelectControl.Item, { value: item, label: item.label, children: /* @__PURE__ */ jsx(SelectControl.ItemLabel, { children: item.label }) }, `${item.value}-${index}`)) }) }), 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)) }) }) ] }); } export { GalleryControls as default }; //# sourceMappingURL=controls.mjs.map