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

Use actual image dimensions for preview image thumbnails #813

Draft
wants to merge 2 commits into
base: 1.x
Choose a base branch
from
Draft
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
51 changes: 41 additions & 10 deletions assets/js/src/core/components/image-preview/image-preview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @license https://github.com/pimcore/studio-ui-bundle/blob/1.x/LICENSE.md POCL and PCL
*/

import React, { type CSSProperties, forwardRef, type MutableRefObject, useEffect } from 'react'
import React, { type CSSProperties, forwardRef, type MutableRefObject, useEffect, useRef, useState } from 'react'
import { useStyle } from './image-preview.styles'
import cn from 'classnames'
import { toCssDimension } from '@Pimcore/utils/css'
Expand All @@ -35,11 +35,35 @@ interface ImagePreviewProps {
}

export const ImagePreview = forwardRef(function ImagePreview ({ src, assetId, width, height, className, style, dropdownItems, bordered = false }: ImagePreviewProps, ref: MutableRefObject<HTMLDivElement>): React.JSX.Element {
const [dimensions, setDimensions] = useState({ width: 0, height: 0 })
const previewImageWrapperRef = useRef<HTMLDivElement>(null)
const [key, setKey] = React.useState(0)
const { getStateClasses } = useDroppable()
const { styles } = useStyle()

const imageSrc = assetId !== undefined ? `${getPrefix()}/assets/${assetId}/image/stream/preview` : src
const createPreviewSrc = (): string | undefined => {
if (assetId !== undefined && dimensions.width > 0 && dimensions.height > 0) {
return `${getPrefix()}/assets/${assetId}/image/stream/custom?${new URLSearchParams({
mimeType: 'JPEG',
resizeMode: 'scaleByWidth',
contain: 'true',
width: dimensions.width.toString(),
height: dimensions.height.toString()
}).toString()}`
}
return undefined
}

const imageSrc: string | undefined = assetId !== undefined
? createPreviewSrc()
: src

useEffect(() => {
if (previewImageWrapperRef?.current !== null) {
const { width: previewWidth, height: previewHeight } = previewImageWrapperRef.current.getBoundingClientRect()
setDimensions({ width: previewWidth, height: previewHeight })
}
}, [previewImageWrapperRef.current])

useEffect(() => {
setKey(key + 1)
Expand All @@ -65,14 +89,21 @@ export const ImagePreview = forwardRef(function ImagePreview ({ src, assetId, wi
width: toCssDimension(width)
} }
>
<Image
className="w-full"
fallback="/bundles/pimcorestudioui/img/fallback-image.svg"
key={ key }
placeholder={ loadingSpinner }
preview={ false }
src={ imageSrc }
/>
<div
className="w-full h-full"
ref={ previewImageWrapperRef }
>
{ imageSrc !== undefined && (
<Image
className="w-full"
fallback="/bundles/pimcorestudioui/img/fallback-image.svg"
key={ key }
placeholder={ loadingSpinner }
preview={ false }
src={ imageSrc }
/>
)}
</div>

<ImagePreviewDropdown dropdownItems={ dropdownItems } />
</div>
Expand Down

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions public/build/00beb9d4-398f-44a9-8796-aaeaa3a61692/core-dll.js

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions public/build/00beb9d4-398f-44a9-8796-aaeaa3a61692/entrypoints.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"entrypoints": {
"core-dll": {
"css": [
"/bundles/pimcorestudioui/build/00beb9d4-398f-44a9-8796-aaeaa3a61692/core-dll.css"
],
"js": [
"/bundles/pimcorestudioui/build/00beb9d4-398f-44a9-8796-aaeaa3a61692/core-dll.js"
]
}
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"entrypoints": {
"vendor": {
"js": [
"/bundles/pimcorestudioui/build/580d7cea-1691-4dac-967e-8b990e09ec84/vendor.js"
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"bundles/pimcorestudioui/build/580d7cea-1691-4dac-967e-8b990e09ec84/vendor.js": "/bundles/pimcorestudioui/build/580d7cea-1691-4dac-967e-8b990e09ec84/vendor.js"
}

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"entrypoints": {
"main": {
"js": [
"/bundles/pimcorestudioui/build/6f3f870a-1ef7-4134-b750-83ab3529d9d0/main.js"
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"bundles/pimcorestudioui/build/6f3f870a-1ef7-4134-b750-83ab3529d9d0/main.js": "/bundles/pimcorestudioui/build/6f3f870a-1ef7-4134-b750-83ab3529d9d0/main.js"
}
2 changes: 0 additions & 2 deletions public/build/ba18a2e5-08c4-43f3-9f28-876514532044/core-dll.js

This file was deleted.

12 changes: 0 additions & 12 deletions public/build/ba18a2e5-08c4-43f3-9f28-876514532044/entrypoints.json

This file was deleted.

This file was deleted.

This file was deleted.