import { observer } from "mobx-react"; import { forwardRef, useCallback, useMemo } from "react"; import { cn } from "../../utils/bem"; import messages from "../../utils/messages"; import { ErrorMessage } from "../ErrorMessage/ErrorMessage"; import "./Image.prefix.css"; /** * Coordinates in relative mode belong to a data domain consisting of percentages in the range from 0 to 100 */ export const RELATIVE_STAGE_WIDTH = 100; /** * Coordinates in relative mode belong to a data domain consisting of percentages in the range from 0 to 100 */ export const RELATIVE_STAGE_HEIGHT = 100; /** * Mode of snapping to pixel */ export const SNAP_TO_PIXEL_MODE = { EDGE: "edge", CENTER: "center", }; export const Image = observer( forwardRef(({ imageEntity, imageTransform, updateImageSize, usedValue, size, overlay }, ref) => { const imageSize = useMemo(() => { return { width: size.width === 1 ? "100%" : size.width, height: size.height === 1 ? "auto" : size.height, }; }, [size]); const onLoad = useCallback( (event) => { updateImageSize(event); imageEntity.setImageLoaded(true); }, [updateImageSize, imageEntity], ); const onError = useCallback(() => { // Handle image load failure (e.g., revoked blob URL, empty blob, network error) // setError(true) also resets imageLoaded state imageEntity.setError(true); }, [imageEntity]); return (
{overlay} {imageEntity.downloaded ? ( ) : null}
); }), ); const ImageProgress = observer(({ downloading, progress, error, src, usedValue }) => { return downloading ? (
Downloading image
) : error ? ( ) : null; }); const imgDefaultProps = { crossOrigin: "anonymous" }; const ImageRenderer = observer( forwardRef(({ src, onLoad, onError, imageTransform, isLoaded }, ref) => { const imageStyles = useMemo(() => { // We can't just skip rendering the image because we need its dimensions to be set // so we just hide it with 0x0 dimensions. // // Real dimension will still be available via `naturalWidth` and `naturalHeight` const style = { // For now, we can't fully hide it as it is used by the Magic Wand tool, so this will hide it visually, but allow using it on the canvas. // It is still possible that there is another way to get the right image data in the tool, so it's a temporary quick fix ...imageTransform, clip: "rect(1px, 1px, 1px, 1px)", }; return { ...style, maxWidth: "unset", visibility: isLoaded ? "visible" : "hidden", }; }, [imageTransform, isLoaded]); return ( // biome-ignore lint/a11y/noRedundantAlt: alt="image" is intentional for accessibility image ); }), ); const ImageLoadingError = ({ src, value }) => { const error = useMemo(() => { return messages.ERR_LOADING_HTTP({ url: src, error: "", attr: value, }); }, [src]); return ; };