import * as React from 'react' import {__, _x} from '@wordpress/i18n' import { useState, } from '@wordpress/element' import { SelectControl, __experimentalVStack as VStack, __experimentalHStack as HStack, BaseControl, TextControl, ToggleControl, Button, } from '@wordpress/components' import { addQueryArgs, } from '@wordpress/url' import { useSelect, useDispatch, } from '@wordpress/data' import { store as coreDataStore, type Attachment, } from '@wordpress/core-data' import apiFetch from '@wordpress/api-fetch' import { useMediaSizeOptions, } from './hooks' import { RESIZABLE_MIME_TYPES, } from './constants' import { getAttachmentSizeSlugByUrl, type APIResponse, } from '@ska/shared' import './style.scss' export * from './types' export * from './constants' export * from './hooks' export * from './util' export interface ImageSizeControlProps { label?: string /** Current image size slug, alternatively derived from `src` when `id` is specified. */ slug?: string /** Media ID. */ id?: number /** Media file source URL. */ src?: string /** Fired when different size is selected. */ onChange: (nextValue: {slug: string, src?: string, width?: number, height?: number}) => void /** Is resizing supported (default: `true`). */ resizable?: boolean /** Supports selecting "custom" size. */ custom?: boolean } const ImageSizeControl: React.FC = ({ label = __('Source image', 'ska-blocks'), slug, id, src = '', onChange, resizable = true, custom = false, }) => { const media = useSelect(select => select(coreDataStore).getEntityRecord('postType', 'attachment', id), [id]) const [width, setWidth] = useState(0) const [height, setHeight] = useState(0) const [crop, setCrop] = useState(src ? !src.includes('-no-crop') : true) const [isResizing, setIsResizing] = useState(false) // Whether to show UI for resizing const [isLoading, setIsLoading] = useState(false) // @ts-expect-error const {invalidateResolution} = useDispatch(coreDataStore) const supportsSizes = slug || (media && !src.startsWith('data:image')) const supportsResize = media && media?.mime_type && RESIZABLE_MIME_TYPES.includes(media.mime_type) const canResize = resizable && supportsResize const OPTIONS = useMediaSizeOptions(media, custom) const value = slug ? slug : getAttachmentSizeSlugByUrl(media, src) if(!supportsSizes) { return null } return (
{ const { width = 0, height = 0, } = OPTIONS.find(({value}) => value === slug) || {} const src = media && slug in (media?.media_details?.sizes || {}) ? media.media_details.sizes[slug].source_url : undefined onChange({ slug, src, width, height, }) }} __nextHasNoMarginBottom __next40pxDefaultSize /> {canResize && <>
{isResizing && ( setWidth(Number(value) || 0)} __nextHasNoMarginBottom __next40pxDefaultSize /> setHeight(Number(value) || 0)} __nextHasNoMarginBottom __next40pxDefaultSize /> setCrop(!crop)} __nextHasNoMarginBottom />