Skip to content

Commit

Permalink
Change attribute name
Browse files Browse the repository at this point in the history
  • Loading branch information
t-hamano committed Dec 12, 2023
1 parent 929c01a commit 6fc2d47
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 13 deletions.
6 changes: 3 additions & 3 deletions enable-responsive-image.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ function enable_responsive_image_enqueue_block_editor_assets() {
add_action( 'enqueue_block_editor_assets', 'enable_responsive_image_enqueue_block_editor_assets' );

function enable_responsive_image_render_block_image( $block_content, $block ) {
if ( ! isset( $block['attrs']['sources'] ) ) {
if ( ! isset( $block['attrs']['enableResponsiveImageSources'] ) ) {
return $block_content;
}

if ( ! is_array( $block['attrs']['sources'] ) ) {
if ( ! is_array( $block['attrs']['enableResponsiveImageSources'] ) ) {
return $block_content;
}

$filtered_sources = array_filter(
$block['attrs']['sources'],
$block['attrs']['enableResponsiveImageSources'],
function ( $source ) {
return isset( $source['srcset'] ) && isset( $source['mediaType'] ) && isset( $source['mediaValue'] );
}
Expand Down
12 changes: 11 additions & 1 deletion src/block-edit-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,17 @@ import type { BlockEditProps } from '@wordpress/blocks';
import type { BlockAttributes } from './types';

export default function BlockEditPreview( { attributes }: BlockEditProps< BlockAttributes > ) {
const { url, alt, caption, align, width, height, aspectRatio, scale, sources } = attributes;
const {
url,
alt,
caption,
align,
width,
height,
aspectRatio,
scale,
enableResponsiveImageSources: sources,
} = attributes;

const borderProps = getBorderClassesAndStyles( attributes );

Expand Down
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const addImageSourceAttributes = ( settings: { [ key: string ]: any } ) => {
...settings,
attributes: {
...settings.attributes,
sources: {
enableResponsiveImageSources: {
type: 'array',
items: {
type: 'number',
Expand Down
10 changes: 5 additions & 5 deletions src/source-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import { MAX_SOURCES } from './constants';

export default function ImageList( props: BlockEditProps< BlockAttributes > ) {
const { attributes, setAttributes } = props;
const { sources } = attributes;
const { enableResponsiveImageSources: sources } = attributes;

function onChange( newSource: Source, index: number ) {
const newSources = [ ...sources ];
newSources[ index ] = newSource;
setAttributes( { sources: newSources } );
setAttributes( { enableResponsiveImageSources: newSources } );
}

function onAddSource() {
Expand All @@ -38,21 +38,21 @@ export default function ImageList( props: BlockEditProps< BlockAttributes > ) {
mediaType: undefined,
mediaValue: undefined,
} );
setAttributes( { sources: newSources } );
setAttributes( { enableResponsiveImageSources: newSources } );
}

function onChangeOrder( direction: number, index: number ) {
const newSources = [ ...sources ];
const newIndex = index + direction;
const movedSource = newSources.splice( index, 1 )[ 0 ];
newSources.splice( newIndex, 0, movedSource );
setAttributes( { sources: newSources } );
setAttributes( { enableResponsiveImageSources: newSources } );
}

function onRemoveSource( index: number ) {
const newSources = [ ...sources ];
newSources.splice( index, 1 );
setAttributes( { sources: newSources } );
setAttributes( { enableResponsiveImageSources: newSources } );
}

return (
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export interface BlockAttributes {
sources: Source[];
enableResponsiveImageSources: Source[];
[ key: string ]: any;
}

Expand Down
4 changes: 2 additions & 2 deletions test/e2e/test.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ test.describe( 'Block', () => {
{
name: 'core/image',
attributes: {
sources: [
enableResponsiveImageSources: [
{
slug: 'medium',
mediaType: 'max-width',
Expand All @@ -92,7 +92,7 @@ test.describe( 'Block', () => {
},
] );

const sources = blocks[ 0 ].attributes.sources;
const sources = blocks[ 0 ].attributes.enableResponsiveImageSources;
expect( sources[ 0 ].srcset.includes( firstSourceFilename ) ).toBe( true );
expect( sources[ 1 ].srcset.includes( secondSourceFilename ) ).toBe( true );
} );
Expand Down

0 comments on commit 6fc2d47

Please sign in to comment.