Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/block-library/src/image/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,6 @@ export function ImageEdit( {
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 (
window?.wp?.media?.view?.settings?.defaultProps?.link ||
LINK_DESTINATION_NONE
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/media-text/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { _x } from '@wordpress/i18n';

export const DEFAULT_MEDIA_SIZE_SLUG = 'full';
export const WIDTH_CONSTRAINT_PERCENTAGE = 15;
export const LINK_DESTINATION_NONE = 'none';
export const LINK_DESTINATION_MEDIA = 'media';
export const LINK_DESTINATION_ATTACHMENT = 'attachment';
export const TEMPLATE = [
Expand Down
45 changes: 37 additions & 8 deletions packages/block-library/src/media-text/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ import MediaContainer from './media-container';
import {
DEFAULT_MEDIA_SIZE_SLUG,
WIDTH_CONSTRAINT_PERCENTAGE,
LINK_DESTINATION_NONE,
LINK_DESTINATION_MEDIA,
LINK_DESTINATION_ATTACHMENT,
TEMPLATE,
Expand Down Expand Up @@ -107,16 +108,43 @@ function attributesFromMedia( {
media.media_details?.sizes?.large?.source_url;
}

let newLinkDestination = linkDestination;
let newHref = href;
if ( linkDestination === LINK_DESTINATION_MEDIA ) {
// Update the media link.
newHref = media.url;
}

// Check if the image is linked to the attachment page.
if ( linkDestination === LINK_DESTINATION_ATTACHMENT ) {
// Update the media link.
newHref = media.link;
// Only apply default link behavior for images (not videos).
if ( mediaType === 'image' ) {
// Check if default link setting should be used.
if ( ! newLinkDestination ) {
// 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.
switch (
window?.wp?.media?.view?.settings?.defaultProps?.link ||
LINK_DESTINATION_NONE
) {
case 'file':
case LINK_DESTINATION_MEDIA:
newLinkDestination = LINK_DESTINATION_MEDIA;
break;
case 'post':
case LINK_DESTINATION_ATTACHMENT:
newLinkDestination = LINK_DESTINATION_ATTACHMENT;
break;
case LINK_DESTINATION_NONE:
default:
newLinkDestination = LINK_DESTINATION_NONE;
break;
}
}

// Set href based on linkDestination.
switch ( newLinkDestination ) {
case LINK_DESTINATION_MEDIA:
newHref = media.url;
break;
case LINK_DESTINATION_ATTACHMENT:
newHref = media.link;
break;
}
}

setAttributes( {
Expand All @@ -126,6 +154,7 @@ function attributesFromMedia( {
mediaUrl: src || media.url,
mediaLink: media.link || undefined,
href: newHref,
linkDestination: newLinkDestination,
focalPoint: undefined,
useFeaturedImage: false,
} );
Expand Down
Loading