@wordpress/block-library
Version:
Block library for the WordPress editor.
295 lines (294 loc) • 10.9 kB
JavaScript
import clsx from "clsx";
import { createBlobURL, isBlobURL } from "@wordpress/blob";
import { createBlock, getBlockBindingsSource } from "@wordpress/blocks";
import { Placeholder } from "@wordpress/components";
import { useDispatch, useSelect } from "@wordpress/data";
import { BlockIcon, MediaPlaceholder, __experimentalGetShadowClassesAndStyles, __experimentalUseBorderProps, store, useBlockEditingMode, useBlockProps } from "@wordpress/block-editor";
import { useEffect, useRef, useState } from "@wordpress/element";
import { __, sprintf } from "@wordpress/i18n";
import { image, plugins } from "@wordpress/icons";
import { store as store$1 } from "@wordpress/notices";
import { useResizeObserver } from "@wordpress/compose";
import { getProtocol, prependHTTPS } from "@wordpress/url";
import { store as store$2 } from "@wordpress/upload-media";
import { useUploadMediaFromBlobURL } from "../utils/hooks.mjs";
import Image from "./image.mjs";
import { isValidFileType } from "./utils.mjs";
import { useMaxWidthObserver } from "./use-max-width-observer.mjs";
import { ALLOWED_MEDIA_TYPES, DEFAULT_MEDIA_SIZE_SLUG, LINK_DESTINATION_ATTACHMENT, LINK_DESTINATION_CUSTOM, LINK_DESTINATION_MEDIA, LINK_DESTINATION_NONE } from "./constants.mjs";
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
// packages/block-library/src/image/edit.jsx
const pickRelevantMediaFiles = (image, size) => {
const imageProps = Object.fromEntries(Object.entries(image ?? {}).filter(([key]) => [
"alt",
"id",
"link",
"caption"
].includes(key)));
imageProps.url = image?.sizes?.[size]?.url || image?.media_details?.sizes?.[size]?.source_url || image.url;
return imageProps;
};
/**
* Is the url for the image hosted externally. An externally hosted image has no
* id and is not a blob url.
*
* @param {number=} id The id of the image.
* @param {string=} url The url of the image.
*
* @return {boolean} Is the url an externally hosted url?
*/
const isExternalImage = (id, url) => url && !id && !isBlobURL(url);
/**
* Checks if WP generated the specified image size. Size generation is skipped
* when the image is smaller than the said size.
*
* @param {Object} image
* @param {string} size
*
* @return {boolean} Whether or not it has default image size.
*/
function hasSize(image, size) {
return "url" in (image?.sizes?.[size] ?? {}) || "source_url" in (image?.media_details?.sizes?.[size] ?? {});
}
function ImageEdit({ attributes, setAttributes, isSelected: isSingleSelected, className, insertBlocksAfter, onReplace, context, clientId, __unstableParentLayout: parentLayout }) {
const { url = "", id, width, height, sizeSlug, aspectRatio, scale, align, metadata } = attributes;
const [temporaryURL, setTemporaryURL] = useState(attributes.blob);
const containerRef = useRef();
const layoutType = parentLayout?.type || parentLayout?.default?.type;
const isMaxWidthContainerWidth = !layoutType || layoutType !== "flex" && layoutType !== "grid";
const [maxWidthObserver, maxContentWidth] = useMaxWidthObserver();
const [placeholderResizeListener, { width: placeholderWidth }] = useResizeObserver();
const isSmallContainer = placeholderWidth && placeholderWidth < 160;
const { __unstableMarkNextChangeAsNotPersistent, replaceBlock } = useDispatch(store);
useEffect(() => {
if (["wide", "full"].includes(align)) {
__unstableMarkNextChangeAsNotPersistent();
setAttributes({
width: void 0,
height: void 0
});
}
}, [
__unstableMarkNextChangeAsNotPersistent,
align,
setAttributes
]);
const { getSettings, getBlockRootClientId, getBlockName, canInsertBlockType } = useSelect(store);
const blockEditingMode = useBlockEditingMode();
const { createErrorNotice } = useDispatch(store$1);
function onUploadError(message) {
createErrorNotice(message, {
type: "snackbar",
explicitDismiss: true
});
setTemporaryURL();
setAttributes({
src: void 0,
id: void 0,
url: void 0,
blob: void 0
});
}
function onFilesPreUpload(files) {
if (files.length === 1) setTemporaryURL(createBlobURL(files[0]));
}
function onSelectImagesList(images) {
const win = containerRef.current?.ownerDocument.defaultView;
if (images.every((file) => file instanceof win.File)) {
/** @type {File[]} */
const files = images;
const rootClientId = getBlockRootClientId(clientId);
if (files.some((file) => !isValidFileType(file))) createErrorNotice(__("If uploading to a gallery all files need to be image formats"), {
id: "gallery-upload-invalid-file",
type: "snackbar"
});
const imageBlocks = files.filter((file) => isValidFileType(file)).map((file) => createBlock("core/image", { blob: createBlobURL(file) }));
if (getBlockName(rootClientId) === "core/gallery") replaceBlock(clientId, imageBlocks);
else if (canInsertBlockType("core/gallery", rootClientId)) {
const galleryBlock = createBlock("core/gallery", {}, imageBlocks);
replaceBlock(clientId, galleryBlock);
}
}
}
function onSelectImage(media) {
if (Array.isArray(media)) {
onSelectImagesList(media);
return;
}
if (!media || !media.url) {
setAttributes({
url: void 0,
alt: void 0,
id: void 0,
title: void 0,
caption: void 0,
blob: void 0
});
setTemporaryURL();
return;
}
if (isBlobURL(media.url)) {
setTemporaryURL(media.url);
return;
}
const { imageDefaultSize } = getSettings();
let newSize = DEFAULT_MEDIA_SIZE_SLUG;
if (sizeSlug && hasSize(media, sizeSlug)) newSize = sizeSlug;
else if (hasSize(media, imageDefaultSize)) newSize = imageDefaultSize;
let mediaAttributes = pickRelevantMediaFiles(media, newSize);
if (typeof mediaAttributes.caption === "string" && mediaAttributes.caption.includes("\n")) mediaAttributes.caption = mediaAttributes.caption.replace(/\n/g, "<br>");
if (!mediaAttributes.caption) {
const { caption: omittedCaption, ...restMediaAttributes } = mediaAttributes;
mediaAttributes = restMediaAttributes;
}
let additionalAttributes;
if (!media.id || media.id !== id) additionalAttributes = { sizeSlug: newSize };
let linkDestination = attributes.linkDestination;
if (!linkDestination) switch (window?.wp?.media?.view?.settings?.defaultProps?.link || LINK_DESTINATION_NONE) {
case "file":
case LINK_DESTINATION_MEDIA:
linkDestination = LINK_DESTINATION_MEDIA;
break;
case "post":
case LINK_DESTINATION_ATTACHMENT:
linkDestination = LINK_DESTINATION_ATTACHMENT;
break;
case LINK_DESTINATION_CUSTOM:
linkDestination = LINK_DESTINATION_CUSTOM;
break;
case LINK_DESTINATION_NONE: linkDestination = LINK_DESTINATION_NONE;
}
let href;
switch (linkDestination) {
case LINK_DESTINATION_MEDIA:
href = media.url;
break;
case LINK_DESTINATION_ATTACHMENT: href = media.link;
}
mediaAttributes.href = href;
setAttributes({
blob: void 0,
...mediaAttributes,
...additionalAttributes,
linkDestination
});
setTemporaryURL();
}
function onSelectURL(newURL) {
const normalizedNewURL = getProtocol(newURL) ? newURL : prependHTTPS(newURL);
if (normalizedNewURL !== url) {
setAttributes({
blob: void 0,
url: normalizedNewURL,
id: void 0,
sizeSlug: getSettings().imageDefaultSize
});
setTemporaryURL();
}
}
useUploadMediaFromBlobURL({
url: temporaryURL,
allowedTypes: ALLOWED_MEDIA_TYPES,
onChange: onSelectImage,
onError: onUploadError
});
const src = isExternalImage(id, url) ? url : void 0;
const isSideloading = useSelect((select) => {
if (!window.__clientSideMediaProcessing || !id) return false;
return select(store$2).isUploadingById(id);
}, [id]);
const mediaPreview = !!url && /* @__PURE__ */ jsx("img", {
alt: __("Edit image"),
title: __("Edit image"),
className: "edit-image-preview",
src: url
});
const borderProps = __experimentalUseBorderProps(attributes);
const shadowProps = __experimentalGetShadowClassesAndStyles(attributes);
const classes = clsx(className, {
"is-transient": !!temporaryURL || isSideloading,
"is-resized": !!width || !!height,
[`size-${sizeSlug}`]: sizeSlug,
"has-custom-border": !!borderProps.className || borderProps.style && Object.keys(borderProps.style).length > 0
});
const blockProps = useBlockProps({
ref: containerRef,
className: classes
});
const { lockUrlControls = false, lockUrlControlsMessage } = useSelect((select) => {
if (!isSingleSelected) return {};
const blockBindingsSource = getBlockBindingsSource(metadata?.bindings?.url?.source);
return {
lockUrlControls: !!metadata?.bindings?.url && !blockBindingsSource?.canUserEditValue?.({
select,
context,
args: metadata?.bindings?.url?.args
}),
lockUrlControlsMessage: blockBindingsSource?.label ? sprintf(__("Connected to %s"), blockBindingsSource.label) : __("Connected to dynamic data")
};
}, [
context,
isSingleSelected,
metadata?.bindings?.url
]);
const pinnedHeight = height === "auto" ? void 0 : height;
const placeholder = (content) => {
return /* @__PURE__ */ jsxs(Placeholder, {
className: clsx("block-editor-media-placeholder", { [borderProps.className]: !!borderProps.className && !isSingleSelected }),
icon: !isSmallContainer && (lockUrlControls ? plugins : image),
withIllustration: !isSingleSelected || isSmallContainer,
label: !isSmallContainer && __("Image"),
instructions: !lockUrlControls && !isSmallContainer && __("Drag and drop an image, upload, or choose from your library."),
style: {
aspectRatio: !(width && pinnedHeight) && aspectRatio ? aspectRatio : void 0,
width: pinnedHeight && aspectRatio ? "100%" : width,
height: width && aspectRatio ? "100%" : pinnedHeight,
objectFit: scale,
...borderProps.style,
...shadowProps.style
},
children: [
lockUrlControls && !isSmallContainer && lockUrlControlsMessage,
!lockUrlControls && !isSmallContainer && content,
placeholderResizeListener
]
});
};
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsxs("figure", {
...blockProps,
children: [/* @__PURE__ */ jsx(Image, {
temporaryURL,
isSideloading,
attributes,
setAttributes,
isSingleSelected,
insertBlocksAfter,
onReplace,
onSelectImage,
onSelectURL,
onUploadError,
context,
clientId,
blockEditingMode,
parentLayoutType: layoutType,
maxContentWidth
}), /* @__PURE__ */ jsx(MediaPlaceholder, {
icon: /* @__PURE__ */ jsx(BlockIcon, { icon: image }),
onSelect: onSelectImage,
onSelectURL,
onFilesPreUpload,
onError: onUploadError,
placeholder,
allowedTypes: ALLOWED_MEDIA_TYPES,
handleUpload: (files) => files.length === 1,
value: {
id,
src
},
mediaPreview,
disableMediaButtons: temporaryURL || url
})]
}), isSingleSelected && isMaxWidthContainerWidth && maxWidthObserver] });
}
export { ImageEdit, ImageEdit as default, isExternalImage, pickRelevantMediaFiles };
//# sourceMappingURL=edit.mjs.map