UNPKG

@wordpress/block-library

Version:
287 lines (286 loc) • 13.6 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 clsx = require("clsx"); clsx = __toESM(clsx); let _wordpress_components = require("@wordpress/components"); let _wordpress_block_editor = require("@wordpress/block-editor"); let _wordpress_element = require("@wordpress/element"); let _wordpress_i18n = require("@wordpress/i18n"); let _wordpress_data = require("@wordpress/data"); let _wordpress_blocks = require("@wordpress/blocks"); let _wordpress_blob = require("@wordpress/blob"); let _wordpress_notices = require("@wordpress/notices"); let _wordpress_icons = require("@wordpress/icons"); let __shared_icon_jsx = require("./shared-icon.cjs"); let __shared_js = require("./shared.cjs"); let __utils_js = require("./utils.cjs"); let ___image_utils_js = require("../image/utils.cjs"); let __gallery_jsx = require("./gallery.cjs"); __gallery_jsx = __toESM(__gallery_jsx); let __constants_js = require("./constants.cjs"); let __use_get_media_js = require("./use-get-media.cjs"); __use_get_media_js = __toESM(__use_get_media_js); let __use_apply_gallery_settings_ts = require("./use-apply-gallery-settings.cjs"); __use_apply_gallery_settings_ts = __toESM(__use_apply_gallery_settings_ts); let __gallery_styles_js = require("./gallery-styles.cjs"); __gallery_styles_js = __toESM(__gallery_styles_js); let __controls_jsx = require("./controls.cjs"); __controls_jsx = __toESM(__controls_jsx); let __use_dynamic_gallery_js = require("./use-dynamic-gallery.cjs"); __use_dynamic_gallery_js = __toESM(__use_dynamic_gallery_js); let __dynamic_gallery_jsx = require("./dynamic-gallery.cjs"); let __dynamic_source_js = require("./dynamic-source.cjs"); let react_jsx_runtime = require("react/jsx-runtime"); // packages/block-library/src/gallery/edit.jsx const LINK_OPTIONS = [ { icon: _wordpress_icons.customLink, label: (0, _wordpress_i18n.__)("Link images to attachment pages"), value: __constants_js.LINK_DESTINATION_ATTACHMENT, noticeText: (0, _wordpress_i18n.__)("Attachment Pages") }, { icon: _wordpress_icons.image, label: (0, _wordpress_i18n.__)("Link images to media files"), value: __constants_js.LINK_DESTINATION_MEDIA, noticeText: (0, _wordpress_i18n.__)("Media Files") }, { icon: _wordpress_icons.fullscreen, label: (0, _wordpress_i18n.__)("Enlarge on click"), value: __constants_js.LINK_DESTINATION_LIGHTBOX, noticeText: (0, _wordpress_i18n.__)("Lightbox effect"), infoText: (0, _wordpress_i18n.__)("Scale images with a lightbox effect") }, { icon: _wordpress_icons.linkOff, label: (0, _wordpress_i18n._x)("None", "Media item link option"), value: __constants_js.LINK_DESTINATION_NONE, noticeText: (0, _wordpress_i18n.__)("None") } ]; const ALLOWED_MEDIA_TYPES = ["image"]; const PLACEHOLDER_TEXT = (0, _wordpress_i18n.__)("Drag and drop images, upload, or choose from your library."); const DEFAULT_BLOCK = { name: "core/image" }; const EMPTY_ARRAY = []; function GalleryEdit(props) { const { setAttributes, attributes, className, clientId, insertBlocksAfter, context } = props; const postId = context?.postId; const postType = context?.postType; const blockEditingMode = (0, _wordpress_block_editor.useBlockEditingMode)(); const [lightboxSetting] = (0, _wordpress_block_editor.useSettings)("blocks.core/image.lightbox"); const linkOptions = !lightboxSetting?.allowEditing ? LINK_OPTIONS.filter((option) => option.value !== __constants_js.LINK_DESTINATION_LIGHTBOX) : LINK_OPTIONS; const { layout } = attributes; const isDynamic = !!attributes.dynamicContent; const linkTo = attributes.linkTo ?? (isDynamic ? __constants_js.LINK_DESTINATION_NONE : (0, ___image_utils_js.getDefaultLinkDestination)()); const galleryAttributes = { ...attributes, linkTo }; const isFlexLayout = (0, __shared_js.isGalleryFlexLayout)(layout); const { getBlocks } = (0, _wordpress_data.useSelect)(_wordpress_block_editor.store); const { replaceInnerBlocks, updateBlockAttributes, selectBlock } = (0, _wordpress_data.useDispatch)(_wordpress_block_editor.store); const { createSuccessNotice, createErrorNotice } = (0, _wordpress_data.useDispatch)(_wordpress_notices.store); const { innerBlockImages, multiGallerySelection, selectedStyleState } = (0, _wordpress_data.useSelect)((select) => { const { getBlockName, getMultiSelectedBlockClientIds, getBlocks: _getBlocks, getSelectedBlockStyleState } = select(_wordpress_block_editor.store); const multiSelectedClientIds = getMultiSelectedBlockClientIds(); return { innerBlockImages: _getBlocks(clientId), multiGallerySelection: multiSelectedClientIds.length && multiSelectedClientIds.every((_clientId) => getBlockName(_clientId) === "core/gallery"), selectedStyleState: getSelectedBlockStyleState(clientId) }; }, [clientId]); const isViewportStyleState = selectedStyleState?.viewport && selectedStyleState.viewport !== "default"; const imageData = (0, __use_get_media_js.default)(innerBlockImages); const mediaById = (0, _wordpress_element.useMemo)(() => new Map(imageData.map((media) => [media.id, media])), [imageData]); const hasImages = !!innerBlockImages.length; const [appliedSort, setAppliedSort] = (0, _wordpress_element.useState)(null); const dynamic = (0, __use_dynamic_gallery_js.default)({ attributes: galleryAttributes, setAttributes, clientId, postId, postType }); (0, __use_apply_gallery_settings_ts.default)(innerBlockImages, mediaById, (imageAttributes) => (0, __shared_js.getImageAttributes)(galleryAttributes, imageAttributes, mediaById.get(imageAttributes.id))); function isValidFileType(file) { const mediaTypeSelector = file.type; return ALLOWED_MEDIA_TYPES.some((mediaType) => mediaTypeSelector?.indexOf(mediaType) === 0) || file.blob; } function updateImages(selectedImages) { const newFileUploads = Object.prototype.toString.call(selectedImages) === "[object FileList]"; const imageArray = Array.from(selectedImages, (file) => file.url || !newFileUploads && !isValidFileType(file) ? file : { blob: file.blob || (0, _wordpress_blob.createBlobURL)(file) }); if (!imageArray.every(isValidFileType)) createErrorNotice((0, _wordpress_i18n.__)("If uploading to a gallery all files need to be image formats"), { id: "gallery-upload-invalid-file", type: "snackbar" }); const processedImages = imageArray.filter((file) => file.url || isValidFileType(file)); const newOrderMap = processedImages.reduce((result, image, index) => (result[image.id] = index, result), {}); const selectedIds = new Set(processedImages.map((img) => img.id)); const existingImageBlocks = !newFileUploads ? innerBlockImages.filter((block) => selectedIds.has(block.attributes.id)) : innerBlockImages; const existingIds = new Set(existingImageBlocks.map((block) => block.attributes.id)); const newBlocks = processedImages.filter((img) => !existingIds.has(img.id)).map((image) => { return (0, _wordpress_blocks.createBlock)("core/image", { id: image.id, blob: image.blob, url: image.url, caption: image.caption, alt: image.alt }); }); replaceInnerBlocks(clientId, existingImageBlocks.concat(newBlocks).sort((a, b) => newOrderMap[a.attributes.id] - newOrderMap[b.attributes.id])); if (newBlocks?.length > 0) selectBlock(newBlocks[0].clientId); } function onUploadError(message) { createErrorNotice(message, { type: "snackbar" }); } function updateInnerImages(getAttributes) { const images = getBlocks(clientId); updateBlockAttributes(images.map((block) => block.clientId), Object.fromEntries(images.map((block) => [block.clientId, getAttributes(block, mediaById.get(block.attributes.id))])), { uniqueByBlock: true }); } function setLinkTo(value) { setAttributes({ linkTo: value }); updateInnerImages((block, media) => (0, __utils_js.getHrefAndDestination)(media, value, false, block.attributes, lightboxSetting)); const linkToText = [...linkOptions].find((linkType) => linkType.value === value); createSuccessNotice((0, _wordpress_i18n.sprintf)((0, _wordpress_i18n.__)("All gallery image links updated to: %s"), linkToText.noticeText), { id: "gallery-attributes-linkTo", type: "snackbar" }); } const hasImageIds = hasImages && innerBlockImages.some((block) => !!block.attributes.id); const mediaPlaceholderProps = { addToGallery: false, disableMediaButtons: innerBlockImages.some((block) => !block.attributes.id && block.attributes.url?.indexOf("blob:") === 0), value: {} }; const blockProps = (0, _wordpress_block_editor.useBlockProps)({ className: (0, clsx.default)(className, "has-nested-images", `wp-block-gallery-${clientId}`) }); const innerBlocksProps = (0, _wordpress_block_editor.useInnerBlocksProps)(blockProps, { defaultBlock: DEFAULT_BLOCK, directInsert: true, orientation: "horizontal", renderAppender: false, allowedBlocks: isDynamic ? EMPTY_ARRAY : void 0 }); if (!hasImages && !isDynamic) return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", { ...innerBlocksProps, children: [innerBlocksProps.children, /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.MediaPlaceholder, { handleUpload: false, icon: __shared_icon_jsx.sharedIcon, labels: { title: (0, _wordpress_i18n.__)("Gallery"), instructions: PLACEHOLDER_TEXT }, onSelect: updateImages, allowedTypes: ALLOWED_MEDIA_TYPES, multiple: true, onError: onUploadError, ...mediaPlaceholderProps, children: blockEditingMode === "default" && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.Button, { __next40pxDefaultSize: true, variant: "secondary", onClick: dynamic.enableDynamicMode, children: (0, __dynamic_source_js.getDynamicSource)(__dynamic_source_js.ATTACHED_MEDIA).title }) })] }); return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [ /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(_wordpress_block_editor.InspectorControls, { group: isViewportStyleState ? "style-state" : "default", children: [!isViewportStyleState && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__dynamic_gallery_jsx.GallerySourcePanel, { dynamic, hasImages }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__controls_jsx.default, { attributes: galleryAttributes, setAttributes, clientId, viewport: isViewportStyleState ? selectedStyleState.viewport : void 0, innerBlockImages, mediaById, dynamic, updateInnerImages, appliedSort, setAppliedSort })] }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.BlockControls, { group: "block", children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.ToolbarDropdownMenu, { icon: _wordpress_icons.link, label: (0, _wordpress_i18n.__)("Link"), children: ({ onClose }) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.MenuGroup, { children: linkOptions.map((linkItem) => { const isOptionSelected = linkTo === linkItem.value; return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_components.MenuItem, { isSelected: isOptionSelected, className: (0, clsx.default)("components-dropdown-menu__menu-item", { "is-active": isOptionSelected }), iconPosition: "left", icon: linkItem.icon, onClick: () => { setLinkTo(linkItem.value); onClose(); }, role: "menuitemradio", info: linkItem.infoText, children: linkItem.label }, linkItem.value); }) }) }) }), !multiGallerySelection && !isDynamic && /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.BlockControls, { group: "other", children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(_wordpress_block_editor.MediaReplaceFlow, { allowedTypes: ALLOWED_MEDIA_TYPES, handleUpload: false, onSelect: updateImages, name: (0, _wordpress_i18n.__)("Add"), multiple: true, mediaIds: innerBlockImages.filter((block) => block.attributes.id).map((block) => block.attributes.id), addToGallery: hasImageIds, variant: "toolbar" }) }), /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__gallery_styles_js.default, { style: attributes.style, clientId, isFlexLayout }), isDynamic ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__dynamic_gallery_jsx.GalleryDynamicView, { ...props, dynamic, blockProps, innerBlocksProps, multiGallerySelection }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(__gallery_jsx.default, { ...props, blockProps: innerBlocksProps, insertBlocksAfter, multiGallerySelection, children: innerBlocksProps.children }) ] }); } exports.default = GalleryEdit; //# sourceMappingURL=edit.cjs.map