From e593b317e2724ee9592bb37d82d3397554a61b3c Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Tue, 27 Mar 2018 14:38:58 +1100 Subject: [PATCH 01/12] Add ability to manually set image dimensions Let users manually set the width and height of an image either using a text field or by a preset percentage amount. --- blocks/library/image/block.js | 79 ++++++++++++++++++++++++++++---- blocks/library/image/editor.scss | 20 ++++++++ components/button/index.js | 2 +- components/button/test/index.js | 2 +- components/text-control/index.js | 4 +- 5 files changed, 94 insertions(+), 13 deletions(-) diff --git a/blocks/library/image/block.js b/blocks/library/image/block.js index 7629ab5c58f0e..0360cf130b77d 100644 --- a/blocks/library/image/block.js +++ b/blocks/library/image/block.js @@ -4,11 +4,12 @@ import classnames from 'classnames'; import ResizableBox from 're-resizable'; import { - startCase, + find, + get, isEmpty, map, - get, pick, + startCase, } from 'lodash'; /** @@ -18,9 +19,12 @@ import { __ } from '@wordpress/i18n'; import { Component, compose } from '@wordpress/element'; import { getBlobByURL, revokeBlobURL, viewPort } from '@wordpress/utils'; import { + Button, + ButtonGroup, IconButton, PanelBody, SelectControl, + TextControl, TextareaControl, Toolbar, } from '@wordpress/components'; @@ -144,10 +148,6 @@ class ImageBlock extends Component { const { attributes, setAttributes, isSelected, className, settings, toggleSelection } = this.props; const { url, alt, caption, align, id, href, width, height } = attributes; - const availableSizes = this.getAvailableSizes(); - const figureStyle = width ? { width } : {}; - const isResizable = [ 'wide', 'full' ].indexOf( align ) === -1 && ( ! viewPort.isExtraSmall() ); - const controls = ( isSelected && ( @@ -176,7 +176,10 @@ class ImageBlock extends Component { ) ); - if ( ! url ) { + const availableSizes = this.getAvailableSizes(); + const selectedSize = find( availableSizes, ( size ) => size.source_url === url ); + + if ( ! url || ! selectedSize ) { return [ controls, { ! isEmpty( availableSizes ) && ( ( { value: size.source_url, @@ -220,6 +225,62 @@ class ImageBlock extends Component { onChange={ this.updateImageURL } /> ) } +
+
+ { + setAttributes( { width: parseInt( value, 10 ) } ); + } } + /> + { + setAttributes( { height: parseInt( value, 10 ) } ); + } } + /> +
+
+ + { [ 25, 50, 75, 100 ].map( ( scale ) => { + const scaledWidth = Math.round( selectedSize.width * ( scale / 100 ) ); + const scaledHeight = Math.round( selectedSize.height * ( scale / 100 ) ); + + const isCurrent = width === scaledWidth && height === scaledHeight; + + return ( + + ); + } ) } + + +
+
), diff --git a/blocks/library/image/editor.scss b/blocks/library/image/editor.scss index db51468bc901e..02e36a0acf37f 100644 --- a/blocks/library/image/editor.scss +++ b/blocks/library/image/editor.scss @@ -92,3 +92,23 @@ margin-right: auto; } } + +.blocks-image-dimensions { + .blocks-image-dimensions__row { + display: flex; + justify-content: space-between; + } + + .blocks-image-dimensions__width, + .blocks-image-dimensions__height { + margin-bottom: 0.5em; + } + + .blocks-image-dimensions__width { + margin-right: 5px; + } + + .blocks-image-dimensions__height { + margin-left: 5px; + } +} diff --git a/components/button/index.js b/components/button/index.js index 49713d398a824..26d576b78d47e 100644 --- a/components/button/index.js +++ b/components/button/index.js @@ -47,7 +47,7 @@ class Button extends Component { ...additionalProps } = this.props; const classes = classnames( 'components-button', className, { - button: ( isPrimary || isLarge ), + button: ( isPrimary || isLarge || isSmall ), 'button-primary': isPrimary, 'button-large': isLarge, 'button-small': isSmall, diff --git a/components/button/test/index.js b/components/button/test/index.js index 2d3582ae8b82c..089d40721ec98 100644 --- a/components/button/test/index.js +++ b/components/button/test/index.js @@ -38,7 +38,7 @@ describe( 'Button', () => { it( 'should render a button element with button-small class', () => { const button = shallow( @@ -275,9 +286,7 @@ class ImageBlock extends Component { From cfd6d55166f9e9e4f5c98302fde19f4a70893a87 Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Wed, 18 Apr 2018 11:17:56 +1000 Subject: [PATCH 11/12] Add className prop to TextControl README --- components/text-control/README.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/components/text-control/README.md b/components/text-control/README.md index 51f80c365883e..449c132c36f97 100644 --- a/components/text-control/README.md +++ b/components/text-control/README.md @@ -44,11 +44,19 @@ Type of the input element to render. Defaults to "text". ### value -The current value of the input +The current value of the input. - Type: `Number` - Required: Yes +### className + +The class that will be added with "components-base-control" to the classes of the wrapper div. +If no className is passed only components-base-control is used. + +- Type: `String` +- Required: No + ### onChange A function that receives the value of the input. From af073abe6d86c360631fbe90006d47e725de48fb Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Wed, 18 Apr 2018 11:21:13 +1000 Subject: [PATCH 12/12] Reset any set image dimensions when image is changed If the user selects a new image, forget any image dimensions that they added since it likely no longer makes any sense. --- blocks/library/image/block.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/blocks/library/image/block.js b/blocks/library/image/block.js index 4df1131cd40ad..78e436c48585b 100644 --- a/blocks/library/image/block.js +++ b/blocks/library/image/block.js @@ -105,7 +105,11 @@ class ImageBlock extends Component { } onSelectImage( media ) { - this.props.setAttributes( pick( media, [ 'alt', 'id', 'caption', 'url' ] ) ); + this.props.setAttributes( { + ...pick( media, [ 'alt', 'id', 'caption', 'url' ] ), + width: undefined, + height: undefined, + } ); } onSetHref( value ) {