UNPKG

@wordpress/block-library

Version:
297 lines (296 loc) 9.02 kB
// packages/block-library/src/gallery/dynamic-gallery.js import { __, sprintf } from "@wordpress/i18n"; import { useState } from "@wordpress/element"; import { Button, Notice, PanelBody, Placeholder, SelectControl, Spinner, ToolbarButton, __experimentalToolsPanel as ToolsPanel, __experimentalToolsPanelItem as ToolsPanelItem, __experimentalConfirmDialog as ConfirmDialog } from "@wordpress/components"; import { BlockContextProvider, BlockControls, useBlockEditingMode, __experimentalUseBlockPreview as useBlockPreview } from "@wordpress/block-editor"; import { sharedIcon } from "./shared-icon.mjs"; import { Caption } from "../utils/caption.mjs"; import { DEFAULT_ORDERBY, DEFAULT_ORDER, MAX_IMAGES } from "./dynamic-source.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; var ORDER_OPTIONS = [ { label: __("Newest to oldest"), value: "date/desc" }, { label: __("Oldest to newest"), value: "date/asc" }, { /* translators: Label for ordering images by title in ascending order. */ label: __("A → Z"), value: "title/asc" }, { /* translators: Label for ordering images by title in descending order. */ label: __("Z → A"), value: "title/desc" } ]; function OrderControl({ orderby, order, onChange }) { return /* @__PURE__ */ jsx( SelectControl, { label: __("Order by"), value: `${orderby}/${order}`, options: ORDER_OPTIONS, onChange: (value) => { const [newOrderby, newOrder] = value.split("/"); onChange({ orderby: newOrderby, order: newOrder }); } } ); } function DetachGalleryDialog({ onConfirm, onCancel }) { return /* @__PURE__ */ jsx( ConfirmDialog, { isOpen: true, title: __("Detach Gallery"), __experimentalHideHeader: false, confirmButtonText: __("Detach"), onConfirm, onCancel, size: "medium", children: __( "The gallery displays the images attached to the post. Detaching will enable you to add, delete, or reorder images. However, new attachments will no longer be added automatically." ) } ); } function GallerySourcePanel({ dynamic, dropdownMenuProps, hasImages }) { const { dynamicContent, canUseDynamicSource, sourceDescriptor, sourceOrderby, sourceOrder, setSourceOrder, convertToStatic, enableDynamicMode, resetSource, isResolvingDynamic, hasMoreImagesThanCap, dynamicMediaTotal } = dynamic; const isDynamic = !!dynamicContent; const [isConfirming, setIsConfirming] = useState(false); const [isConfirmingDetach, setIsConfirmingDetach] = useState(false); function requestEnableDynamicMode() { if (hasImages) { setIsConfirming(true); } else { enableDynamicMode(); } } if (isDynamic) { return /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsxs( ToolsPanel, { label: __("Source"), resetAll: resetSource, dropdownMenuProps, children: [ /* @__PURE__ */ jsxs("div", { className: "wp-block-gallery__source-settings", children: [ /* @__PURE__ */ jsx("p", { className: "wp-block-gallery__source-description", children: sourceDescriptor?.description ?? __("Dynamic images.") }), /* @__PURE__ */ jsx( Button, { __next40pxDefaultSize: true, variant: "secondary", onClick: () => setIsConfirmingDetach(true), disabled: isResolvingDynamic, accessibleWhenDisabled: true, children: __("Detach Gallery") } ) ] }), hasMoreImagesThanCap && /* @__PURE__ */ jsx( Notice, { className: "wp-block-gallery__source-notice", status: "warning", isDismissible: false, children: sprintf( /* translators: 1: number of images shown. 2: total number of matching images. */ __( "Only the first %1$d of %2$d images will be displayed." ), MAX_IMAGES, dynamicMediaTotal ) } ), /* @__PURE__ */ jsx( ToolsPanelItem, { isShownByDefault: true, label: __("Order by"), hasValue: () => sourceOrderby !== DEFAULT_ORDERBY || sourceOrder !== DEFAULT_ORDER, onDeselect: () => setSourceOrder(void 0, void 0), children: /* @__PURE__ */ jsx( OrderControl, { orderby: sourceOrderby, order: sourceOrder, onChange: ({ orderby, order }) => setSourceOrder(orderby, order) } ) } ) ] } ), isConfirmingDetach && /* @__PURE__ */ jsx( DetachGalleryDialog, { onConfirm: () => { convertToStatic(); setIsConfirmingDetach(false); }, onCancel: () => setIsConfirmingDetach(false) } ) ] }); } if (!canUseDynamicSource) { return null; } return /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx(PanelBody, { title: __("Source"), children: /* @__PURE__ */ jsx("div", { className: "wp-block-gallery__source-settings", children: /* @__PURE__ */ jsx( Button, { __next40pxDefaultSize: true, variant: "secondary", onClick: requestEnableDynamicMode, children: __("Use images attached to the post") } ) }) }), isConfirming && /* @__PURE__ */ jsx( ConfirmDialog, { isOpen: true, title: __("Use images attached to the post?"), __experimentalHideHeader: false, confirmButtonText: __("Use attached images"), onConfirm: () => { enableDynamicMode(); setIsConfirming(false); }, onCancel: () => setIsConfirming(false), size: "medium", children: __( "The images in this gallery will be replaced, but will remain in the media library." ) } ) ] }); } function GalleryImagesPreview({ imageBlocks }) { const { children, ref, className } = useBlockPreview({ blocks: imageBlocks }); return /* @__PURE__ */ jsx( "div", { ref, className, style: { display: "contents" }, children } ); } function GalleryDynamicView({ dynamic, blockProps, innerBlocksProps, attributes, setAttributes, isSelected, insertBlocksAfter, isContentLocked, multiGallerySelection }) { const { sourceDescriptor, dynamicImageBlocks, galleryContext, isResolvingDynamic, convertToStatic } = dynamic; const blockEditingMode = useBlockEditingMode(); const [isConfirmingDetach, setIsConfirmingDetach] = useState(false); const emptyInstructions = isResolvingDynamic ? __("Loading images…") : sourceDescriptor?.emptyMessage ?? __("Dynamic images will appear here."); return /* @__PURE__ */ jsxs(Fragment, { children: [ blockEditingMode === "default" && /* @__PURE__ */ jsxs(Fragment, { children: [ /* @__PURE__ */ jsx(BlockControls, { group: "other", children: /* @__PURE__ */ jsx( ToolbarButton, { onClick: () => setIsConfirmingDetach(true), disabled: isResolvingDynamic, children: __("Detach") } ) }), isConfirmingDetach && /* @__PURE__ */ jsx( DetachGalleryDialog, { onConfirm: () => { convertToStatic(); setIsConfirmingDetach(false); }, onCancel: () => setIsConfirmingDetach(false) } ) ] }), /* @__PURE__ */ jsxs("figure", { ...blockProps, children: [ dynamicImageBlocks.length ? /* @__PURE__ */ jsx(BlockContextProvider, { value: galleryContext, children: /* @__PURE__ */ jsx( GalleryImagesPreview, { imageBlocks: dynamicImageBlocks } ) }) : /* @__PURE__ */ jsx( Placeholder, { icon: sharedIcon, label: __("Gallery"), instructions: emptyInstructions, children: isResolvingDynamic && /* @__PURE__ */ jsx(Spinner, {}) } ), /* @__PURE__ */ jsx( Caption, { attributes, setAttributes, isSelected, insertBlocksAfter, showToolbarButton: !multiGallerySelection && !isContentLocked, className: "blocks-gallery-caption", label: __("Gallery caption text"), placeholder: __("Add gallery caption") } ) ] }), innerBlocksProps.children ] }); } export { GalleryDynamicView, GallerySourcePanel }; //# sourceMappingURL=dynamic-gallery.mjs.map