Skip to content

Commit

Permalink
Fix: AI - HTML thumbnail is not scaled and translated correctly [ED-1…
Browse files Browse the repository at this point in the history
…2735] (elementor#24267)
  • Loading branch information
matipojo authored Nov 14, 2023
1 parent 4e7d6c8 commit 5bd0275
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Thumbnail } from './thumbnail';
import { Thumbnail, THUMBNAIL_SIZE } from './thumbnail';
import { __ } from '@wordpress/i18n';
import PropTypes from 'prop-types';
import { Box, IconButton } from '@elementor/ui';
Expand All @@ -18,6 +18,8 @@ export const ThumbnailUrl = ( props ) => {
position: 'relative',
borderRadius: 1,
overflow: 'hidden',
minWidth: THUMBNAIL_SIZE,

'&:hover::before': {
content: '""',
position: 'absolute',
Expand All @@ -34,7 +36,7 @@ export const ThumbnailUrl = ( props ) => {
<IconButton
className="remove-attachment"
size="small"
aria-label={ __( 'Remove' ) }
aria-label={ __( 'Remove', 'elementor' ) }
disabled={ props.disabled }
onClick={ ( event ) => {
event.stopPropagation();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,25 @@ import { useEffect, useRef } from 'react';
import { Box } from '@elementor/ui';
import PropTypes from 'prop-types';

const THUMBNAIL_SIZE = 64;
export const THUMBNAIL_SIZE = 64;

export const Thumbnail = ( props ) => {
const previewRef = useRef( null );

useEffect( () => {
if ( previewRef.current ) {
const previewRoot = previewRef.current.firstElementChild;

const width = previewRoot?.offsetWidth || THUMBNAIL_SIZE;
const height = previewRoot?.offsetHeight || THUMBNAIL_SIZE;

// Keep the aspect ratio
previewRoot.style.width = `${ width }px`;
previewRoot.style.height = `${ height }px`;

previewRef.current.style.width = `${ THUMBNAIL_SIZE }px`;
previewRef.current.style.height = `${ THUMBNAIL_SIZE }px`;

const scaleFactor = Math.min( height, width );
const scale = THUMBNAIL_SIZE / scaleFactor;

Expand All @@ -32,12 +40,6 @@ export const Thumbnail = ( props ) => {

return (
<Box sx={ {
width: THUMBNAIL_SIZE,
height: THUMBNAIL_SIZE,
minWidth: THUMBNAIL_SIZE,
minHeight: THUMBNAIL_SIZE,
maxWidth: THUMBNAIL_SIZE,
maxHeight: THUMBNAIL_SIZE,
border: '1px solid',
borderColor: 'grey.300',
position: 'relative',
Expand All @@ -57,8 +59,6 @@ export const Thumbnail = ( props ) => {
sx={ {
pointerEvents: 'none',
transformOrigin: 'center',
width: THUMBNAIL_SIZE,
height: THUMBNAIL_SIZE,
} }
dangerouslySetInnerHTML={ {
__html: props.html,
Expand Down

0 comments on commit 5bd0275

Please sign in to comment.