Skip to content

Commit

Permalink
Simplify logic, clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
mkaz committed Sep 23, 2020
1 parent 45ccae9 commit 44c3e7b
Showing 1 changed file with 32 additions and 23 deletions.
55 changes: 32 additions & 23 deletions packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ import Image from './image';
* Module constants
*/
import {
LINK_DESTINATION_MEDIA,
LINK_DESTINATION_ATTACHMENT,
LINK_DESTINATION_CUSTOM,
LINK_DESTINATION_MEDIA,
LINK_DESTINATION_NONE,
ALLOWED_MEDIA_TYPES,
DEFAULT_SIZE_SLUG,
} from './constants';
Expand Down Expand Up @@ -155,33 +157,40 @@ export function ImageEdit( {

// Check if default link setting should be used.
let linkDestination = attributes.linkDestination;

if ( ! linkDestination && wp.media.view.settings.defaultProps.link ) {
// The constants used in Gutenberg do not match WP options.
// TODO: fix this
if ( wp.media.view.settings.defaultProps.link === 'file' ) {
linkDestination = LINK_DESTINATION_MEDIA;
} else if ( wp.media.view.settings.defaultProps.link === 'post' ) {
linkDestination = LINK_DESTINATION_ATTACHMENT;
} else {
linkDestination = wp.media.view.settings.defaultProps.link;
if ( ! linkDestination ) {
// Use the WordPress option to determine the proper default.
// The constants used in Gutenberg do not match WP options so a little more complicated than ideal.
// TODO: fix this in a follow up PR, requires updating media-text and ui component.
switch ( wp.media.view.settings.defaultProps.link ) {
case 'file':
case LINK_DESTINATION_MEDIA:
linkDestination = LINK_DESTINATION_MEDIA;
break;
case 'post':
case LINK_DESTINATION_ATTACHMENT:
linkDestination = LINK_DESTINATION_ATTACHMENT;
break;
case LINK_DESTINATION_CUSTOM:
linkDestination = LINK_DESTINATION_CUSTOM;
break;
case LINK_DESTINATION_NONE:
default:
linkDestination = LINK_DESTINATION_NONE;
break;
}
}

// If still not set, set to "none" for back compat.
linkDestination = linkDestination ? linkDestination : 'none';

// Check if the image is linked to it's media.
if ( linkDestination === LINK_DESTINATION_MEDIA ) {
// Update the media link.
mediaAttributes.href = media.url;
}

// Check if the image is linked to the attachment page.
if ( linkDestination === LINK_DESTINATION_ATTACHMENT ) {
// Update the media link.
mediaAttributes.href = media.link;
let href;
switch ( linkDestination ) {
case LINK_DESTINATION_MEDIA:
href = media.url;
break;
case LINK_DESTINATION_ATTACHMENT:
href = media.link;
break;
}
mediaAttributes.href = href;

setAttributes( {
...mediaAttributes,
Expand Down

0 comments on commit 44c3e7b

Please sign in to comment.