UNPKG

@wordpress/block-library

Version:
239 lines (238 loc) • 11.8 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); // \0rolldown/runtime.js var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) { key = keys[i]; if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: ((k) => from[k]).bind(null, key), enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod)); let _wordpress_i18n = require("@wordpress/i18n"); let _wordpress_element = require("@wordpress/element"); let _wordpress_components = require("@wordpress/components"); let _wordpress_block_editor = require("@wordpress/block-editor"); let __shared_icon_jsx = require("./shared-icon.cjs"); let __shared_js = require("./shared.cjs"); let __gallery_jsx = require("./gallery.cjs"); __gallery_jsx = __toESM(__gallery_jsx); let __dynamic_source_js = require("./dynamic-source.cjs"); let react_jsx_runtime = require("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__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalConfirmDialog, { isOpen: true, title: (0, _wordpress_i18n.__)("Detach Gallery"), __experimentalHideHeader: false, confirmButtonText: (0, _wordpress_i18n.__)("Detach"), onConfirm, onCancel, size: "medium", children: (0, _wordpress_i18n.__)("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] = (0, _wordpress_element.useState)(false); const [isConfirmingDetach, setIsConfirmingDetach] = (0, _wordpress_element.useState)(false); function requestEnableDynamicMode() { if (hasImages) setIsConfirming(true); else enableDynamicMode(); } if (isDynamic) return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_components.PanelBody, { title: (0, _wordpress_i18n.__)("Source"), children: [/* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", { className: "wp-block-gallery__source-settings", children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { className: "wp-block-gallery__source-description", children: sourceDescriptor?.description ?? (0, _wordpress_i18n.__)("Dynamic images.") }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Button, { __next40pxDefaultSize: true, variant: "secondary", onClick: () => setIsConfirmingDetach(true), disabled: isResolvingDynamic, accessibleWhenDisabled: true, children: (0, _wordpress_i18n.__)("Detach Gallery") })] }), hasMoreImagesThanCap && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Notice, { className: "wp-block-gallery__source-notice", status: "warning", isDismissible: false, children: (0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("Only the first %1$d of %2$d images will be displayed."), __dynamic_source_js.MAX_IMAGES, dynamicMediaTotal) })] }), isConfirmingDetach && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DetachGalleryDialog, { onConfirm: () => { convertToStatic(); setIsConfirmingDetach(false); }, onCancel: () => setIsConfirmingDetach(false) })] }); if (!canUseDynamicSource) return null; return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.PanelBody, { title: (0, _wordpress_i18n.__)("Source"), children: /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", { className: "wp-block-gallery__source-settings", children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("p", { className: "wp-block-gallery__source-description", children: (0, _wordpress_i18n.__)("Images added to the gallery.") }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Button, { __next40pxDefaultSize: true, variant: "secondary", onClick: requestEnableDynamicMode, children: (0, _wordpress_i18n.__)("Use attached images") })] }) }), isConfirming && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.__experimentalConfirmDialog, { isOpen: true, title: (0, _wordpress_i18n.__)("Use images attached to the post?"), __experimentalHideHeader: false, confirmButtonText: (0, _wordpress_i18n.__)("Use attached images"), onConfirm: () => { enableDynamicMode(); setIsConfirming(false); }, onCancel: () => setIsConfirming(false), size: "medium", children: (0, _wordpress_i18n.__)("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 } = (0, _wordpress_block_editor.__experimentalUseBlockPreview)({ blocks: imageBlocks, layout }); return /* @__PURE__ */ (0, react_jsx_runtime.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 = (0, _wordpress_block_editor.useBlockEditingMode)(); const [isConfirmingDetach, setIsConfirmingDetach] = (0, _wordpress_element.useState)(false); const previewLayout = (0, _wordpress_element.useMemo)(() => (0, __shared_js.isGalleryFlexLayout)(attributes.layout) ? { ...attributes.layout, type: "flex" } : attributes.layout, [attributes.layout]); const emptyInstructions = isResolvingDynamic ? (0, _wordpress_i18n.__)("Loading images…") : sourceDescriptor?.emptyMessage ?? (0, _wordpress_i18n.__)("Dynamic images will appear here."); return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [ blockEditingMode === "default" && /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.BlockControls, { group: "other", children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToolbarButton, { onClick: () => setIsConfirmingDetach(true), disabled: isResolvingDynamic, children: (0, _wordpress_i18n.__)("Detach") }) }), isConfirmingDetach && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(DetachGalleryDialog, { onConfirm: () => { convertToStatic(); setIsConfirmingDetach(false); }, onCancel: () => setIsConfirmingDetach(false) })] }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__gallery_jsx.default, { ...galleryProps, children: dynamicImageBlocks.length ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.BlockContextProvider, { value: galleryContext, children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(GalleryImagesPreview, { imageBlocks: dynamicImageBlocks, layout: previewLayout }) }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Placeholder, { icon: __shared_icon_jsx.sharedIcon, label: (0, _wordpress_i18n.__)("Gallery"), instructions: emptyInstructions, children: isResolvingDynamic && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Spinner, {}) }) }), innerBlocksProps.children ] }); } exports.GalleryDynamicView = GalleryDynamicView; exports.GallerySourcePanel = GallerySourcePanel; //# sourceMappingURL=dynamic-gallery.cjs.map