Skip to content

Commit

Permalink
Add align support to the image block - alternative (#55954)
Browse files Browse the repository at this point in the history
* only remove the alignment control

* use an effect to reset width, height, aspect ratio and cover when align is wide or full

* try a filter on block attributes when align control updates attributes

* new fixtures for the default align attribute of core/image

* updated snapshots

* modify the filter to always return the same shape of object, update naming

* remove filter, add undo skip

* update the react native component

* Remove align from custom image attributes

This allows the block supports to add the attribute instead keeping things cleaner.

* Fix tests

* Fix another fixture

---------

Co-authored-by: Alex Lende <[email protected]>
  • Loading branch information
draganescu and ajlende authored Nov 22, 2023
1 parent 3b380b0 commit 6e532f4
Show file tree
Hide file tree
Showing 14 changed files with 52 additions and 81 deletions.
4 changes: 2 additions & 2 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -378,8 +378,8 @@ Insert an image to make a visual statement. ([Source](https://github.com/WordPre

- **Name:** core/image
- **Category:** media
- **Supports:** anchor, color (~~background~~, ~~text~~), filter (duotone)
- **Attributes:** align, alt, aspectRatio, caption, height, href, id, lightbox, linkClass, linkDestination, linkTarget, rel, scale, sizeSlug, title, url, width
- **Supports:** align (center, full, left, right, wide), anchor, color (~~background~~, ~~text~~), filter (duotone)
- **Attributes:** alt, aspectRatio, caption, height, href, id, lightbox, linkClass, linkDestination, linkTarget, rel, scale, sizeSlug, title, url, width

## Latest Comments

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,25 @@ exports[`Align options for group block sets Wide width option 1`] = `
`;

exports[`Align options for media block sets Align center option 1`] = `
"<!-- wp:image {"align":"center","id":1,"sizeSlug":"large","linkDestination":"none"} -->
"<!-- wp:image {"id":1,"sizeSlug":"large","linkDestination":"none","align":"center"} -->
<figure class="wp-block-image aligncenter size-large"><img src="https://test-site.files.wordpress.com/local-image-1.jpeg" alt="" class="wp-image-1"/></figure>
<!-- /wp:image -->"
`;

exports[`Align options for media block sets Align left option 1`] = `
"<!-- wp:image {"align":"left","id":1,"sizeSlug":"large","linkDestination":"none"} -->
"<!-- wp:image {"id":1,"sizeSlug":"large","linkDestination":"none","align":"left"} -->
<figure class="wp-block-image alignleft size-large"><img src="https://test-site.files.wordpress.com/local-image-1.jpeg" alt="" class="wp-image-1"/></figure>
<!-- /wp:image -->"
`;

exports[`Align options for media block sets Align right option 1`] = `
"<!-- wp:image {"align":"right","id":1,"sizeSlug":"large","linkDestination":"none"} -->
"<!-- wp:image {"id":1,"sizeSlug":"large","linkDestination":"none","align":"right"} -->
<figure class="wp-block-image alignright size-large"><img src="https://test-site.files.wordpress.com/local-image-1.jpeg" alt="" class="wp-image-1"/></figure>
<!-- /wp:image -->"
`;

exports[`Align options for media block sets Full width option 1`] = `
"<!-- wp:image {"align":"full","id":1,"sizeSlug":"large","linkDestination":"none"} -->
"<!-- wp:image {"id":1,"sizeSlug":"large","linkDestination":"none","align":"full"} -->
<figure class="wp-block-image alignfull size-large"><img src="https://test-site.files.wordpress.com/local-image-1.jpeg" alt="" class="wp-image-1"/></figure>
<!-- /wp:image -->"
`;
Expand All @@ -49,7 +49,7 @@ exports[`Align options for media block sets None option 1`] = `
`;

exports[`Align options for media block sets Wide width option 1`] = `
"<!-- wp:image {"align":"wide","id":1,"sizeSlug":"large","linkDestination":"none"} -->
"<!-- wp:image {"id":1,"sizeSlug":"large","linkDestination":"none","align":"wide"} -->
<figure class="wp-block-image alignwide size-large"><img src="https://test-site.files.wordpress.com/local-image-1.jpeg" alt="" class="wp-image-1"/></figure>
<!-- /wp:image -->"
`;
Expand Down
4 changes: 1 addition & 3 deletions packages/block-library/src/image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@
"keywords": [ "img", "photo", "picture" ],
"textdomain": "default",
"attributes": {
"align": {
"type": "string"
},
"url": {
"type": "string",
"source": "attribute",
Expand Down Expand Up @@ -95,6 +92,7 @@
}
},
"supports": {
"align": [ "left", "center", "right", "wide", "full" ],
"anchor": true,
"color": {
"text": false,
Expand Down
37 changes: 16 additions & 21 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { getBlobByURL, isBlobURL, revokeBlobURL } from '@wordpress/blob';
import { Placeholder } from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import {
BlockAlignmentControl,
BlockControls,
BlockIcon,
MediaPlaceholder,
useBlockProps,
Expand Down Expand Up @@ -106,13 +104,13 @@ export function ImageEdit( {
url = '',
alt,
caption,
align,
id,
width,
height,
sizeSlug,
aspectRatio,
scale,
align,
} = attributes;
const [ temporaryURL, setTemporaryURL ] = useState();

Expand All @@ -126,6 +124,21 @@ export function ImageEdit( {
captionRef.current = caption;
}, [ caption ] );

const { __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );

useEffect( () => {
if ( [ 'wide', 'full' ].includes( align ) ) {
__unstableMarkNextChangeAsNotPersistent();
setAttributes( {
width: undefined,
height: undefined,
aspectRatio: undefined,
scale: undefined,
} );
}
}, [ align ] );

const ref = useRef();
const { imageDefaultSize, mediaUpload } = useSelect( ( select ) => {
const { getSettings } = select( blockEditorStore );
Expand Down Expand Up @@ -255,16 +268,6 @@ export function ImageEdit( {
}
}

function updateAlignment( nextAlign ) {
const extraUpdatedAttributes = [ 'wide', 'full' ].includes( nextAlign )
? { width: undefined, height: undefined }
: {};
setAttributes( {
...extraUpdatedAttributes,
align: nextAlign,
} );
}

let isTemp = isTemporaryImage( id, url );

// Upload a temporary image on mount.
Expand Down Expand Up @@ -375,14 +378,6 @@ export function ImageEdit( {
clientId={ clientId }
blockEditingMode={ blockEditingMode }
/>
{ ! url && blockEditingMode === 'default' && (
<BlockControls group="block">
<BlockAlignmentControl
value={ align }
onChange={ updateAlignment }
/>
</BlockControls>
) }
<MediaPlaceholder
icon={ <BlockIcon icon={ icon } /> }
onSelect={ onSelectImage }
Expand Down
35 changes: 17 additions & 18 deletions packages/block-library/src/image/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ import {
MEDIA_TYPE_IMAGE,
BlockControls,
InspectorControls,
BlockAlignmentToolbar,
BlockStyles,
store as blockEditorStore,
blockSettingsScreens,
Expand Down Expand Up @@ -212,7 +211,6 @@ export class ImageEdit extends Component {
this.onSetFeatured = this.onSetFeatured.bind( this );
this.onFocusCaption = this.onFocusCaption.bind( this );
this.onSelectURL = this.onSelectURL.bind( this );
this.updateAlignment = this.updateAlignment.bind( this );
this.accessibilityLabelCreator =
this.accessibilityLabelCreator.bind( this );
this.setMappedAttributes = this.setMappedAttributes.bind( this );
Expand Down Expand Up @@ -305,6 +303,20 @@ export class ImageEdit extends Component {
this.replacedFeaturedImage = false;
setFeaturedImage( id );
}

const { align } = attributes;
const { __unstableMarkNextChangeAsNotPersistent } = this.props;

// Update the attributes if the align is wide or full
if ( [ 'wide', 'full' ].includes( align ) ) {
__unstableMarkNextChangeAsNotPersistent();
setAttributes( {
width: undefined,
height: undefined,
aspectRatio: undefined,
scale: undefined,
} );
}
}

static getDerivedStateFromProps( props, state ) {
Expand Down Expand Up @@ -391,18 +403,6 @@ export class ImageEdit extends Component {
} );
}

updateAlignment( nextAlign ) {
const extraUpdatedAttributes = Object.values(
WIDE_ALIGNMENTS.alignments
).includes( nextAlign )
? { width: undefined, height: undefined }
: {};
this.props.setAttributes( {
...extraUpdatedAttributes,
align: nextAlign,
} );
}

onSetNewTab( value ) {
const updatedLinkTarget = getUpdatedLinkTargetSettings(
value,
Expand Down Expand Up @@ -711,10 +711,6 @@ export class ImageEdit extends Component {
onClick={ open }
/>
</ToolbarGroup>
<BlockAlignmentToolbar
value={ align }
onChange={ this.updateAlignment }
/>
</BlockControls>
);

Expand Down Expand Up @@ -941,8 +937,11 @@ export default compose( [
} ),
withDispatch( ( dispatch ) => {
const { createErrorNotice } = dispatch( noticesStore );
const { __unstableMarkNextChangeAsNotPersistent } =
dispatch( blockEditorStore );

return {
__unstableMarkNextChangeAsNotPersistent,
createErrorNotice,
closeSettingsBottomSheet() {
dispatch( editPostStore ).closeGeneralSidebar();
Expand Down
22 changes: 0 additions & 22 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
MediaReplaceFlow,
store as blockEditorStore,
useSettings,
BlockAlignmentControl,
__experimentalImageEditor as ImageEditor,
__experimentalGetElementClassName,
__experimentalUseBorderProps as useBorderProps,
Expand Down Expand Up @@ -353,21 +352,6 @@ export default function Image( {
} );
}

function updateAlignment( nextAlign ) {
const extraUpdatedAttributes = [ 'wide', 'full' ].includes( nextAlign )
? {
width: undefined,
height: undefined,
aspectRatio: undefined,
scale: undefined,
}
: {};
setAttributes( {
...extraUpdatedAttributes,
align: nextAlign,
} );
}

useEffect( () => {
if ( ! isSelected ) {
setIsEditingImage( false );
Expand Down Expand Up @@ -455,12 +439,6 @@ export default function Image( {
const controls = (
<>
<BlockControls group="block">
{ hasNonContentControls && (
<BlockAlignmentControl
value={ align }
onChange={ updateAlignment }
/>
) }
{ hasNonContentControls && (
<ToolbarButton
onClick={ () => {
Expand Down
1 change: 1 addition & 0 deletions packages/components/src/mobile/utils/alignments.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const WIDE_ALIGNMENTS = {
},
excludeBlocks: [ 'core/heading' ],
notInnerContainers: [
'core/image',
'core/separator',
'core/media-text',
'core/pullquote',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,19 @@ exports[`rawHandler should convert HTML post to blocks with minimal content chan
`;

exports[`rawHandler should convert a caption shortcode 1`] = `
"<!-- wp:image {"align":"none","id":122,"className":"size-medium wp-image-122"} -->
"<!-- wp:image {"id":122,"align":"none","className":"size-medium wp-image-122"} -->
<figure class="wp-block-image alignnone size-medium wp-image-122"><img src="image.png" alt="" class="wp-image-122"/><figcaption class="wp-element-caption">test</figcaption></figure>
<!-- /wp:image -->"
`;

exports[`rawHandler should convert a caption shortcode with caption 1`] = `
"<!-- wp:image {"align":"none","id":122,"className":"size-medium wp-image-122"} -->
"<!-- wp:image {"id":122,"align":"none","className":"size-medium wp-image-122"} -->
<figure class="wp-block-image alignnone size-medium wp-image-122"><img src="image.png" alt="" class="wp-image-122"/><figcaption class="wp-element-caption"><a href="https://w.org">test</a></figcaption></figure>
<!-- /wp:image -->"
`;

exports[`rawHandler should convert a caption shortcode with link 1`] = `
"<!-- wp:image {"align":"none","id":754} -->
"<!-- wp:image {"id":754,"align":"none"} -->
<figure class="wp-block-image alignnone"><a href="http://build.wordpress-develop.test/wp-content/uploads/2011/07/100_5478.jpg"><img src="http://build.wordpress-develop.test/wp-content/uploads/2011/07/100_5478.jpg?w=604" alt="Bell on Wharf" class="wp-image-754"/></a><figcaption class="wp-element-caption">Bell on wharf in San Francisco</figcaption></figure>
<!-- /wp:image -->"
`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"name": "core/image",
"isValid": true,
"attributes": {
"align": "center",
"url": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==",
"alt": "",
"caption": "Give it a try. Press the \"really wide\" button on the image toolbar."
"caption": "Give it a try. Press the \"really wide\" button on the image toolbar.",
"align": "center"
},
"innerBlocks": []
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:image {"align":"left","width":100,"height":100} -->
<!-- wp:image {"width":100,"height":100,"align":"left"} -->
<figure class="wp-block-image alignleft is-resized"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="" style="width:100px;height:100px"/></figure>
<!-- /wp:image -->
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:image {"align":"left","width":100,"height":100} -->
<!-- wp:image {"width":100,"height":100,"align":"left"} -->
<figure class="wp-block-image alignleft is-resized"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="" style="width:100px;height:100px"/></figure>
<!-- /wp:image -->
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:image {"align":"left","id":13,"width":358,"height":164,"sizeSlug":"large","linkDestination":"none"} -->
<!-- wp:image {"id":13,"width":358,"height":164,"sizeSlug":"large","linkDestination":"none","align":"left"} -->
<figure class="wp-block-image alignleft size-large is-resized"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="" class="wp-image-13" style="width:358px;height:164px"/></figure>
<!-- /wp:image -->
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<!-- wp:image {"align":"left","width":164,"height":164,"sizeSlug":"large","className":"is-style-rounded","style":{"border":{"radius":"100%"}}} -->
<!-- wp:image {"width":164,"height":164,"sizeSlug":"large","align":"left","className":"is-style-rounded","style":{"border":{"radius":"100%"}}} -->
<figure class="wp-block-image alignleft size-large is-resized has-custom-border is-style-rounded"><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" alt="" style="border-radius:100%;width:164px;height:164px"/></figure>
<!-- /wp:image -->
2 changes: 1 addition & 1 deletion test/integration/fixtures/documents/wordpress-out.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ <h3 class="wp-block-heading">Shortcode</h3>
<figure class="wp-block-image"><img src="block.png" alt="" class="wp-image-5114"/></figure>
<!-- /wp:image -->

<!-- wp:image {"align":"right","id":5114} -->
<!-- wp:image {"id":5114,"align":"right"} -->
<figure class="wp-block-image alignright"><img src="aligned.png" alt="" class="wp-image-5114"/></figure>
<!-- /wp:image -->

Expand Down

0 comments on commit 6e532f4

Please sign in to comment.