import { NEW_TAB_REL, ALLOWED_MEDIA_TYPES, LINK_DESTINATION_ATTACHMENT, LINK_DESTINATION_CUSTOM, LINK_DESTINATION_MEDIA, LINK_DESTINATION_NONE, } from './constants'; import type { ImageMediaDetails, WindowWithWp } from './types'; /** * Returns the site's default link destination for newly added images, from the * `image_default_link_type` option. * * @return Block link destination. */ export function getDefaultLinkDestination(): string { switch ( ( window as unknown as WindowWithWp ).wp?.media?.view?.settings ?.defaultProps?.link ) { case 'file': return LINK_DESTINATION_MEDIA; case 'post': return LINK_DESTINATION_ATTACHMENT; case LINK_DESTINATION_CUSTOM: return LINK_DESTINATION_CUSTOM; default: return LINK_DESTINATION_NONE; } } /** * Evaluates a CSS aspect-ratio property value as a number. * * Degenerate or invalid ratios behave as 'auto'. And 'auto' ratios return NaN. * * @see https://drafts.csswg.org/css-sizing-4/#aspect-ratio * * @param value CSS aspect-ratio property value. * @return Numerical aspect ratio or NaN if invalid. */ export function evalAspectRatio( value: string ): number { const [ width, height = 1 ] = value.split( '/' ).map( Number ); const aspectRatio = width / height; return aspectRatio === Infinity || aspectRatio === 0 ? NaN : aspectRatio; } export function removeNewTabRel( currentRel?: string ): string | undefined { let newRel = currentRel; if ( currentRel !== undefined && newRel ) { NEW_TAB_REL.forEach( ( relVal ) => { const regExp = new RegExp( '\\b' + relVal + '\\b', 'gi' ); newRel = ( newRel as string ).replace( regExp, '' ); } ); // Only trim if NEW_TAB_REL values was replaced. if ( newRel !== currentRel ) { newRel = newRel.trim(); } if ( ! newRel ) { newRel = undefined; } } return newRel; } /** * Helper to get the link target settings to be stored. * * @param value The new link target value. * @param attributes Block attributes. * @param attributes.rel Image block's rel attribute. * * @return Updated link target settings. */ export function getUpdatedLinkTargetSettings( value: boolean | string | undefined, { rel }: { rel?: string } ): { linkTarget: '_blank' | undefined; rel: string | undefined } { const linkTarget = value ? '_blank' : undefined; let updatedRel; if ( ! linkTarget && ! rel ) { updatedRel = undefined; } else { updatedRel = removeNewTabRel( rel ); } return { linkTarget, rel: updatedRel, }; } /** * Determines new Image block attributes size selection. * * @param image Media file object for gallery image. * @param size Selected size slug to apply. */ export function getImageSizeAttributes( image: ImageMediaDetails | undefined, size: string ) { const url = image?.media_details?.sizes?.[ size ]?.source_url; if ( url ) { return { url, width: undefined, height: undefined, sizeSlug: size }; } return {}; } /** * Checks if the file has a valid file type. * * @param file - The file to check. * @return - Returns true if the file has a valid file type, otherwise false. */ export function isValidFileType( file: File ): boolean { return ALLOWED_MEDIA_TYPES.some( ( mediaType ) => file.type.indexOf( mediaType ) === 0 ); } export function mediaPosition( { x, y }: { x: number; y: number } = { x: 0.5, y: 0.5 } ): string { return `${ Math.round( x * 100 ) }% ${ Math.round( y * 100 ) }%`; }