Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update/link control show preview of attachment in suggestion list #57824

Open
wants to merge 3 commits into
base: trunk
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const ICONS_MAP = {

function SearchItemIcon( { isURL, suggestion } ) {
let icon = null;
let imgSrc = null;

if ( isURL ) {
icon = globe;
Expand All @@ -40,9 +41,27 @@ function SearchItemIcon( { isURL, suggestion } ) {
if ( suggestion.isBlogHome ) {
icon = verse;
}
} else if (
suggestion.type === 'attachment' &&
suggestion.mimeType &&
suggestion.mimeType.startsWith( 'image/' )
) {
imgSrc = suggestion.url;
}
}

if ( imgSrc ) {
return (
<img
className="block-editor-link-control__search-item-img"
src={ imgSrc }
alt={ suggestion.title }
width={ 24 }
height={ 24 }
/>
);
}

if ( icon ) {
return (
<Icon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ $preview-image-height: 140px;
}
}

.block-editor-link-control__search-item-img {
aspect-ratio: 1/1;
margin-right: 8px;
}

&.is-error .block-editor-link-control__search-item-icon {
top: 0;
width: 32px;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,12 @@ import { __ } from '@wordpress/i18n';
/**
* @typedef WPLinkSearchResult
*
* @property {number} id Post or term id.
* @property {string} url Link url.
* @property {string} title Title of the link.
* @property {string} type The taxonomy or post type slug or type URL.
* @property {WPKind} [kind] Link kind of post-type or taxonomy
* @property {number} id Post or term id.
* @property {string} url Link url.
* @property {string} title Title of the link.
* @property {string} type The taxonomy or post type slug or type URL.
* @property {WPKind} [kind] Link kind of post-type or taxonomy
* @property {string} [mimeType] attachment mime type
*/

/**
Expand Down Expand Up @@ -229,6 +230,7 @@ const fetchLinkSuggestions = async (
) || __( '(no title)' ),
type: result.subtype || result.type,
kind: result?.meta?.kind,
mimeType: result?.mime_type,
};
} );
} );
Expand Down
Loading