Skip to content

Commit

Permalink
Get rid of the isGrouped context as not really needed now image doesn…
Browse files Browse the repository at this point in the history
…'t need to add any markup based on location
  • Loading branch information
Glen Davies committed Dec 17, 2020
1 parent 36c2a01 commit 0550bcc
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 @@ -85,13 +85,20 @@ 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 = useSelect(
( select ) => {
const { getMedia } = select( 'core' );
return id && ( isSelected || isGrouped ) ? getMedia( id ) : null;
return id &&
( isSelected || parentLinkDestination || parentSizeSlug )
? getMedia( id )
: null;
},
[ id, isSelected, isGrouped ]
[ id, isSelected, parentLinkDestination ]
);
const {
imageEditing,
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 0550bcc

Please sign in to comment.