Skip to content

Commit

Permalink
Move setting of attributes to the child images
Browse files Browse the repository at this point in the history
  • Loading branch information
Glen Davies committed Jan 14, 2021
1 parent 2f4f9a1 commit 150d907
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 39 deletions.
8 changes: 1 addition & 7 deletions packages/block-library/src/image/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@
"apiVersion": 2,
"name": "core/image",
"category": "media",
"usesContext": [
"allowResize",
"isGrouped",
"linkTo",
"linkTarget",
"sizeSlug"
],
"usesContext": [ "allowResize", "linkTo", "linkTarget", "sizeSlug" ],
"attributes": {
"align": {
"type": "string"
Expand Down
12 changes: 5 additions & 7 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { get, omit, pick } from 'lodash';
import { get, omit, pick, isEmpty } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -94,7 +94,6 @@ export function ImageEdit( {
sizeSlug,
inhertedAttributes,
} = attributes;
const { isGrouped } = context;
const [ tempUrl, setTempUrl ] = useState();
const altRef = useRef();
useEffect( () => {
Expand Down Expand Up @@ -161,10 +160,9 @@ export function ImageEdit( {
}

// Check if default link setting, or the one inherited from parent block should be used.
let linkDestination =
isGrouped && context.linkTo
? context.linkTo
: attributes.linkDestination;
let linkDestination = context.linkTo
? context.linkTo
: attributes.linkDestination;

if ( ! linkDestination ) {
// Use the WordPress option to determine the proper default.
Expand Down Expand Up @@ -203,7 +201,7 @@ export function ImageEdit( {
}
mediaAttributes.href = href;

if ( isGrouped ) {
if ( ! isEmpty( context ) ) {
const parentSizeAttributes = getImageSizeAttributes(
media,
context.sizeSlug
Expand Down
13 changes: 10 additions & 3 deletions packages/block-library/src/image/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ export default function Image( {
} ) {
const captionRef = useRef();
const prevUrl = usePrevious( url );
const { allowResize = true, isGrouped = false } = context;
const {
allowResize = true,
linkTo: parentLinkDestination,
sizeSlug: parentSizeSlug,
} = context;
const { image, multiImageSelection } = useSelect(
( select ) => {
const { getMedia } = select( 'core' );
Expand All @@ -96,7 +100,10 @@ export default function Image( {
const multiSelectedClientIds = getMultiSelectedBlockClientIds();
return {
image:
id && ( isSelected || isGrouped ) ? getMedia( id ) : null,
id &&
( isSelected || parentLinkDestination || parentSizeSlug )
? getMedia( id )
: null,
multiImageSelection:
multiSelectedClientIds.length &&
multiSelectedClientIds.every(
Expand All @@ -105,7 +112,7 @@ export default function Image( {
),
};
},
[ id, isSelected, isGrouped ]
[ id, isSelected, parentLinkDestination ]
);
const { imageEditing, imageSizes, maxWidth, mediaUpload } = useSelect(
( select ) => {
Expand Down
37 changes: 15 additions & 22 deletions packages/block-library/src/image/use-parent-attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,50 +24,43 @@ export default function useParentAttributes(
setAttributes
) {
const {
isGrouped,
linkTo: parentLinkDestination,
linkTarget: parentLinkTarget,
sizeSlug: parentSizeSlug,
} = context;

useEffect( () => {
if ( ! isGrouped ) {
if ( ! inheritedAttributes.linkDestination ) {
return;
}
if ( inheritedAttributes.linkDestination && image ) {
if ( image ) {
const href = getUrl( image, parentLinkDestination );
setAttributes( {
href,
linkDestination: parentLinkDestination,
} );
}
}, [ image, parentLinkDestination, isGrouped ] );
}, [ image, parentLinkDestination ] );

useEffect( () => {
if ( ! isGrouped ) {
if ( ! inheritedAttributes.linkTarget ) {
return;
}
if ( inheritedAttributes.linkTarget ) {
setAttributes( {
linkTarget: parentLinkTarget,
} );
}
}, [ parentLinkTarget, isGrouped ] );

setAttributes( {
linkTarget: parentLinkTarget,
} );
}, [ parentLinkTarget ] );

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

if ( inheritedAttributes.sizeSlug ) {
const sizeAttributes = getImageSizeAttributes(
image,
parentSizeSlug
);
const sizeAttributes = getImageSizeAttributes( image, parentSizeSlug );

setAttributes( {
...sizeAttributes,
} );
}
}, [ parentSizeSlug, isGrouped ] );
setAttributes( {
...sizeAttributes,
} );
}, [ parentSizeSlug ] );
}

0 comments on commit 150d907

Please sign in to comment.