-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
206 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
RichText, | ||
useBlockProps, | ||
// @ts-ignore: has no exported member | ||
__experimentalGetElementClassName, | ||
// @ts-ignore: has no exported member | ||
__experimentalGetBorderClassesAndStyles as getBorderClassesAndStyles, | ||
} from '@wordpress/block-editor'; | ||
import { __ } from '@wordpress/i18n'; | ||
import type { BlockEditProps } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { BlockAttributes } from './types'; | ||
|
||
export default function BlockEditPreview( { attributes }: BlockEditProps< BlockAttributes > ) { | ||
const { url, alt, caption, align, width, height, aspectRatio, scale, id, sizeSlug, sources } = | ||
attributes; | ||
|
||
const borderProps = getBorderClassesAndStyles( attributes ); | ||
|
||
const filteredSources = sources.filter( ( { srcset, mediaType, mediaValue } ) => { | ||
return srcset && mediaType && mediaValue; | ||
} ); | ||
|
||
const classes = classnames( { | ||
[ `align${ align }` ]: align, | ||
[ `size-${ sizeSlug }` ]: sizeSlug, | ||
'is-resized': width || height, | ||
'has-custom-border': | ||
!! borderProps.className || | ||
( borderProps.style && Object.keys( borderProps.style ).length > 0 ), | ||
} ); | ||
|
||
const imageClasses = classnames( borderProps.className, { | ||
[ `wp-image-${ id }` ]: !! id, | ||
} ); | ||
|
||
const blockProps = useBlockProps( { | ||
className: classes, | ||
} ); | ||
|
||
return ( | ||
<figure { ...blockProps }> | ||
<picture> | ||
{ filteredSources.length > 0 && | ||
filteredSources.map( ( { srcset, mediaType, mediaValue }, index ) => ( | ||
<source | ||
key={ index } | ||
srcSet={ srcset } | ||
media={ `(${ mediaType }: ${ mediaValue }px)` } | ||
/> | ||
) ) } | ||
<img | ||
src={ url } | ||
className={ imageClasses || undefined } | ||
style={ { | ||
...borderProps.style, | ||
aspectRatio, | ||
objectFit: scale, | ||
width, | ||
height, | ||
} } | ||
alt={ alt } | ||
/> | ||
</picture> | ||
{ ! RichText.isEmpty( caption ) && ( | ||
<RichText.Content | ||
className={ __experimentalGetElementClassName( 'caption' ) } | ||
tagName="figcaption" | ||
value={ caption } | ||
/> | ||
) } | ||
</figure> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
export const DEFAULT_MEDIA_VALUE = isNaN( | ||
parseInt( window?.enableResponsiveImage?.defaultMediaValue ) | ||
) | ||
? 600 | ||
: parseInt( window?.enableResponsiveImage?.defaultMediaValue ); | ||
|
||
export const MEDIA_TYPES = [ | ||
{ | ||
label: __( 'max-width', 'enable-responsive-image' ), | ||
value: 'max-width', | ||
}, | ||
{ | ||
label: __( 'min-width', 'enable-responsive-image' ), | ||
value: 'min-width', | ||
}, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createReduxStore, register } from '@wordpress/data'; | ||
|
||
const DEFAULT_STATE = { | ||
isPreview: false, | ||
}; | ||
|
||
export const store = createReduxStore( 'enable-responsive-image', { | ||
reducer: ( state = DEFAULT_STATE, action ) => { | ||
if ( action.type === 'UPDATE_IS_PREVIEW' ) { | ||
return { | ||
...state, | ||
isPreview: ! state.isPreview, | ||
}; | ||
} | ||
|
||
return state; | ||
}, | ||
selectors: { | ||
getIsPreview( state: { isPreview: boolean } ) { | ||
return state.isPreview; | ||
}, | ||
}, | ||
actions: { | ||
setIsPreview( value ) { | ||
return { | ||
type: 'UPDATE_IS_PREVIEW', | ||
value, | ||
}; | ||
}, | ||
}, | ||
} ); | ||
|
||
register( store ); |