Skip to content

Commit

Permalink
When dragging an image block into a gallery make sure we don't wipe a…
Browse files Browse the repository at this point in the history
…ny custom links
  • Loading branch information
Glen Davies committed Apr 11, 2021
1 parent e3a6c29 commit d34dcdb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
15 changes: 11 additions & 4 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,14 @@ export default function Image( {
}
}, [ isSelected ] );

useParentAttributes( image, context, inheritedAttributes, setAttributes );
const currentAttributes = { linkDestination, linkTarget, href };
useParentAttributes(
image,
currentAttributes,
context,
inheritedAttributes,
setAttributes
);

// If an image is externally hosted, try to fetch the image data. This may
// fail if the image host doesn't allow CORS with the domain. If it works,
Expand Down Expand Up @@ -209,8 +216,8 @@ export default function Image( {

function onSetHref( props ) {
if (
inheritedAttributes.linkDestination ||
inheritedAttributes.linkTarget
inheritedAttributes?.linkDestination ||
inheritedAttributes?.linkTarget
) {
setAttributes( {
inheritedAttributes: {
Expand Down Expand Up @@ -251,7 +258,7 @@ export default function Image( {
}

function updateImage( newSizeSlug ) {
if ( inheritedAttributes.sizeSlug ) {
if ( inheritedAttributes?.sizeSlug ) {
setAttributes( {
inheritedAttributes: {
...inheritedAttributes,
Expand Down
19 changes: 18 additions & 1 deletion packages/block-library/src/image/use-parent-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import { getUrl, getImageSizeAttributes } from './utils';
* from changed values supplied by parent block context.
*
* @param {Object} image Image.
* @param {Object} currentAttributes Current values for inheritable attributes
* @param {Object} context Parent block context.
* @param {Object} inheritedAttributes Image block attribute that indicate which attributes are inherited from parent.
* @param {Function} setAttributes Image block setAttributes prop.
*/
export default function useParentAttributes(
image,
currentAttributes,
context,
inheritedAttributes,
setAttributes
Expand All @@ -35,6 +37,21 @@ export default function useParentAttributes(
setAttributes( {
inheritedAttributes: undefined,
} );
return;
}
if ( isGrouped && ! inheritedAttributes ) {
// Check current Image attributes to make sure we don't overwrite an custom
// links and targets. from Image blocks dragged into a new parent block with inheritable
// attributes
const { linkDestination, linkTarget, href } = currentAttributes;
setAttributes( {
inheritedAttributes: {
linkDestination:
linkDestination !== 'none' || href ? false : true,
linkTarget: linkTarget ? false : true,
sizeSlug: true,
},
} );
}
}, [ isGrouped, inheritedAttributes ] );

Expand Down Expand Up @@ -62,7 +79,7 @@ export default function useParentAttributes(
}, [ parentLinkTarget ] );

useEffect( () => {
if ( ! isGrouped || ! inheritedAttributes.sizeSlug ) {
if ( ! isGrouped || ! inheritedAttributes?.sizeSlug ) {
return;
}

Expand Down

0 comments on commit d34dcdb

Please sign in to comment.