Skip to content

Commit

Permalink
Use class methods instead of calling setAttributes() directly
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Apr 18, 2018
1 parent 9674199 commit 18e4e92
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions blocks/library/image/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ class ImageBlock extends Component {
this.onSelectImage = this.onSelectImage.bind( this );
this.onSetHref = this.onSetHref.bind( this );
this.updateImageURL = this.updateImageURL.bind( this );
this.updateWidth = this.updateWidth.bind( this );
this.updateHeight = this.updateHeight.bind( this );
this.updateDimensions = this.updateDimensions.bind( this );

this.state = {
captionFocused: false,
Expand Down Expand Up @@ -140,6 +143,20 @@ class ImageBlock extends Component {
this.props.setAttributes( { url, width: undefined, height: undefined } );
}

updateWidth( width ) {
this.props.setAttributes( { width: parseInt( width, 10 ) } );
}

updateHeight( height ) {
this.props.setAttributes( { height: parseInt( height, 10 ) } );
}

updateDimensions( width = undefined, height = undefined ) {
return () => {
this.props.setAttributes( { width, height } );
};
}

getAvailableSizes() {
return get( this.props.image, [ 'media_details', 'sizes' ], {} );
}
Expand Down Expand Up @@ -235,19 +252,15 @@ class ImageBlock extends Component {
label={ __( 'Width' ) }
value={ width !== undefined ? width : '' }
placeholder={ selectedSize.width }
onChange={ ( value ) => {
setAttributes( { width: parseInt( value, 10 ) } );
} }
onChange={ this.updateWidth }
/>
<TextControl
type="number"
className="blocks-image__dimensions__height"
label={ __( 'Height' ) }
value={ height !== undefined ? height : '' }
placeholder={ selectedSize.height }
onChange={ ( value ) => {
setAttributes( { height: parseInt( value, 10 ) } );
} }
onChange={ this.updateHeight }
/>
</div>
<div className="blocks-image__dimensions__row">
Expand All @@ -264,9 +277,7 @@ class ImageBlock extends Component {
isSmall
isPrimary={ isCurrent }
aria-pressed={ isCurrent }
onClick={ () => {
setAttributes( { width: scaledWidth, height: scaledHeight } );
} }
onClick={ this.updateDimensions( scaledWidth, scaledHeight ) }
>
{ scale }%
</Button>
Expand All @@ -275,9 +286,7 @@ class ImageBlock extends Component {
</ButtonGroup>
<Button
isSmall
onClick={ () => {
setAttributes( { width: undefined, height: undefined } );
} }
onClick={ this.updateDimensions() }
>
{ __( 'Reset' ) }
</Button>
Expand Down

0 comments on commit 18e4e92

Please sign in to comment.