EditAssetDialog: Use original image as source for cropping

This commit is contained in:
Gustav Hansen 2022-03-17 13:35:47 +01:00
parent f9fca8a8fe
commit f8582f2916
2 changed files with 29 additions and 9 deletions

View File

@ -13,16 +13,16 @@ const CardAsset = styled(Flex)`
background: linear-gradient(180deg, #ffffff 0%, #f6f6f9 121.48%);
`;
export const AssetPreview = forwardRef(({ mime, url, name }, ref) => {
export const AssetPreview = forwardRef(({ mime, url, name, ...props }, ref) => {
const [lang] = usePersistentState('strapi-admin-language', 'en');
if (mime.includes(AssetType.Image)) {
return <img ref={ref} src={url} alt={name} />;
return <img ref={ref} src={url} alt={name} {...props} />;
}
if (mime.includes(AssetType.Video)) {
return (
<video controls src={url} ref={ref}>
<video controls src={url} ref={ref} {...props}>
<track label={name} default kind="captions" srcLang={lang} src="" />
</video>
);
@ -30,7 +30,7 @@ export const AssetPreview = forwardRef(({ mime, url, name }, ref) => {
if (mime.includes(AssetType.Audio)) {
return (
<audio controls src={url} ref={ref}>
<audio controls src={url} ref={ref} {...props}>
{name}
</audio>
);
@ -38,14 +38,14 @@ export const AssetPreview = forwardRef(({ mime, url, name }, ref) => {
if (mime.includes('pdf')) {
return (
<CardAsset justifyContent="center">
<CardAsset justifyContent="center" {...props}>
<FilePdfIcon aria-label={name} />
</CardAsset>
);
}
return (
<CardAsset justifyContent="center">
<CardAsset justifyContent="center" {...props}>
<FileIcon aria-label={name} />
</CardAsset>
);

View File

@ -41,6 +41,8 @@ export const PreviewBox = ({
}) => {
const { trackUsage } = useTracking();
const previewRef = useRef(null);
const [cropImageReady, setCropImageReady] = useState(false);
const [cropIntent, setCropIntent] = useState(false);
const [assetUrl, setAssetUrl] = useState(createAssetUrl(asset, false));
const [thumbnailUrl, setThumbnailUrl] = useState(createAssetUrl(asset, true));
const { formatMessage } = useIntl();
@ -123,15 +125,23 @@ export const PreviewBox = ({
};
const handleCropCancel = () => {
setCropIntent(false);
setCropImageReady(false);
stopCropping();
onCropCancel();
};
const handleCropStart = () => {
crop(previewRef.current);
onCropStart();
setCropIntent(true);
};
useEffect(() => {
if (cropIntent && cropImageReady) {
crop(previewRef.current);
onCropStart();
}
}, [cropImageReady, cropIntent, onCropStart, crop]);
return (
<>
<RelativeBox hasRadius background="neutral150" borderColor="neutral200">
@ -198,7 +208,17 @@ export const PreviewBox = ({
</UploadProgressWrapper>
)}
<AssetPreview ref={previewRef} mime={asset.mime} name={asset.name} url={thumbnailUrl} />
<AssetPreview
ref={previewRef}
mime={asset.mime}
name={asset.name}
url={cropIntent ? assetUrl : thumbnailUrl}
onLoad={() => {
if (cropIntent) {
setCropImageReady(true);
}
}}
/>
</Wrapper>
<ActionRow