UNPKG

@wordpress/block-library

Version:
211 lines (210 loc) • 9.22 kB
import { __, sprintf } from "@wordpress/i18n"; import { useMemo, useState } from "@wordpress/element"; import { Button, Notice, PanelBody, Placeholder, Spinner, ToolbarButton, __experimentalConfirmDialog } from "@wordpress/components"; import { BlockContextProvider, BlockControls, __experimentalUseBlockPreview, useBlockEditingMode } from "@wordpress/block-editor"; import { sharedIcon } from "./shared-icon.mjs"; import { isGalleryFlexLayout } from "./shared.mjs"; import Gallery from "./gallery.mjs"; import { MAX_IMAGES } from "./dynamic-source.mjs"; import { Fragment, jsx, jsxs } from "react/jsx-runtime"; // packages/block-library/src/gallery/dynamic-gallery.jsx /** * Confirmation for leaving dynamic mode, shown from both the block toolbar and * the Source panel so the two entry points explain the change identically. * * Detaching keeps the images the gallery currently shows but breaks the link to * its source, so it's worth confirming — mirroring the dialog `GallerySourcePanel` * shows for the opposite direction. * * @param {Object} props * @param {Function} props.onConfirm Called when the user confirms detaching. * @param {Function} props.onCancel Called when the user dismisses the dialog. */ function DetachGalleryDialog({ onConfirm, onCancel }) { return /* @__PURE__ */ jsx(__experimentalConfirmDialog, { 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.") }); } /** * The Gallery block's "Source" inspector panel. * * In dynamic mode it shows the resolved source and a control to detach the * gallery from it. In static mode it offers the entry point into dynamic mode. * Either direction is a one-way change, so both are behind a confirmation * dialog this panel owns. Rendered inside the block's `InspectorControls`, * alongside the Settings panel, which owns the ordering for both modes. * * @param {Object} props * @param {Object} props.dynamic The `useDynamicGallery` result. * @param {boolean} props.hasImages Whether the gallery has manually-added images. */ function GallerySourcePanel({ dynamic, hasImages }) { const { dynamicContent, canUseDynamicSource, sourceDescriptor, convertToStatic, enableDynamicMode, 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(PanelBody, { title: __("Source"), 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(__("Only the first %1$d of %2$d images will be displayed."), MAX_IMAGES, dynamicMediaTotal) })] }), 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__ */ jsxs("div", { className: "wp-block-gallery__source-settings", children: [/* @__PURE__ */ jsx("p", { className: "wp-block-gallery__source-description", children: __("Images added to the gallery.") }), /* @__PURE__ */ jsx(Button, { __next40pxDefaultSize: true, variant: "secondary", onClick: requestEnableDynamicMode, children: __("Use attached images") })] }) }), isConfirming && /* @__PURE__ */ jsx(__experimentalConfirmDialog, { 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.") })] }); } /** * Renders the resolved image blocks as a read-only preview. * * `useBlockPreview` returns a `useDisabled` ref that makes its subtree inert, so * previewed images (including any links) aren't interactive in the editor. The * ref needs a real element, yet the images must stay flex children of the gallery * `<figure>` and sit beside an editable caption. `display: contents` resolves * this: the wrapper carries the ref but generates no box, so the image figures * remain the figure's flex items and only they are disabled — the caption sibling * stays editable. This relies on the gallery's image styles using descendant * (not direct-child) selectors, which the box-less wrapper leaves intact. * * The gallery's layout is passed through so the previewed images see the same * parent layout that real inner blocks would (`useBlockPreview` provides it to * their layout context). Without it they resolve to the default flow layout and * behave as if they weren't in a gallery — most visibly, an image inside a * cropped gallery would keep its baseline `height: auto` and defeat the * gallery's cropping CSS. * * @param {Object} props * @param {Object[]} props.imageBlocks Non-persisted `core/image` blocks to preview. * @param {Object} props.layout The gallery's layout, for the preview's layout context. */ function GalleryImagesPreview({ imageBlocks, layout }) { const { children, ref, className } = __experimentalUseBlockPreview({ blocks: imageBlocks, layout }); return /* @__PURE__ */ jsx("div", { ref, className, style: { display: "contents" }, children }); } /** * Renders a dynamic-mode gallery on the canvas: * * - a block-toolbar control to detach the gallery from its source, confirmed in * a dialog; * - the gallery `<figure>` and caption (see `gallery.jsx`) holding a * non-editable preview of the resolved media (or a placeholder while * resolving / when nothing is found), with the gallery's provided context so * the previewed images inherit gallery-wide settings; * - the (empty) inner blocks kept mounted so the container's `allowedBlocks: []` * keeps syncing to block list settings (which blocks insertion and hides the * List View). * * @param {Object} props * @param {Object} props.dynamic The `useDynamicGallery` result. * @param {Object} props.innerBlocksProps The gallery's `useInnerBlocksProps()` result. */ function GalleryDynamicView({ dynamic, innerBlocksProps, ...galleryProps }) { const { attributes } = galleryProps; const { sourceDescriptor, dynamicImageBlocks, galleryContext, isResolvingDynamic, convertToStatic } = dynamic; const blockEditingMode = useBlockEditingMode(); const [isConfirmingDetach, setIsConfirmingDetach] = useState(false); const previewLayout = useMemo(() => isGalleryFlexLayout(attributes.layout) ? { ...attributes.layout, type: "flex" } : attributes.layout, [attributes.layout]); 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__ */ jsx(Gallery, { ...galleryProps, children: dynamicImageBlocks.length ? /* @__PURE__ */ jsx(BlockContextProvider, { value: galleryContext, children: /* @__PURE__ */ jsx(GalleryImagesPreview, { imageBlocks: dynamicImageBlocks, layout: previewLayout }) }) : /* @__PURE__ */ jsx(Placeholder, { icon: sharedIcon, label: __("Gallery"), instructions: emptyInstructions, children: isResolvingDynamic && /* @__PURE__ */ jsx(Spinner, {}) }) }), innerBlocksProps.children ] }); } export { GalleryDynamicView, GallerySourcePanel }; //# sourceMappingURL=dynamic-gallery.mjs.map