2023-02-06 10:01:03 -08:00
|
|
|
import clsx from 'clsx';
|
2022-05-07 10:46:21 -07:00
|
|
|
import React, { useRef } from 'react';
|
|
|
|
|
2024-10-25 15:06:13 -07:00
|
|
|
import { useSettings } from 'pl-fe/hooks/use-settings';
|
2022-05-07 10:46:21 -07:00
|
|
|
|
2024-05-13 10:00:42 -07:00
|
|
|
interface IStillImage {
|
2022-05-07 10:46:21 -07:00
|
|
|
/** Image alt text. */
|
2023-10-02 11:54:02 -07:00
|
|
|
alt?: string;
|
2022-05-07 10:46:21 -07:00
|
|
|
/** Extra class names for the outer <div> container. */
|
2023-10-02 11:54:02 -07:00
|
|
|
className?: string;
|
2022-05-07 10:46:21 -07:00
|
|
|
/** URL to the image */
|
2023-10-02 11:54:02 -07:00
|
|
|
src: string;
|
2022-05-07 10:46:21 -07:00
|
|
|
/** Extra CSS styles on the outer <div> element. */
|
2023-10-02 11:54:02 -07:00
|
|
|
style?: React.CSSProperties;
|
2022-11-20 12:23:18 -08:00
|
|
|
/** Whether to display the image contained vs filled in its container. */
|
2023-10-02 11:54:02 -07:00
|
|
|
letterboxed?: boolean;
|
2022-11-20 12:53:24 -08:00
|
|
|
/** Whether to show the file extension in the corner. */
|
2023-10-02 11:54:02 -07:00
|
|
|
showExt?: boolean;
|
2023-04-24 05:17:51 -07:00
|
|
|
/** Callback function if the image fails to load */
|
2023-10-02 11:54:02 -07:00
|
|
|
onError?(): void;
|
2024-09-11 08:52:38 -07:00
|
|
|
/** Treat as animated, no matter the extension */
|
|
|
|
isGif?: boolean;
|
|
|
|
/** Specify that the group is defined by the parent */
|
|
|
|
noGroup?: boolean;
|
2022-05-07 10:46:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Renders images on a canvas, only playing GIFs if autoPlayGif is enabled. */
|
2024-09-11 08:52:38 -07:00
|
|
|
const StillImage: React.FC<IStillImage> = ({
|
|
|
|
alt, className, src, style, letterboxed = false, showExt = false, onError, isGif, noGroup,
|
|
|
|
}) => {
|
2024-02-13 21:20:18 -08:00
|
|
|
const { autoPlayGif } = useSettings();
|
2022-05-07 10:46:21 -07:00
|
|
|
|
|
|
|
const canvas = useRef<HTMLCanvasElement>(null);
|
2024-08-11 01:48:58 -07:00
|
|
|
const img = useRef<HTMLImageElement>(null);
|
2022-05-07 10:46:21 -07:00
|
|
|
|
|
|
|
const hoverToPlay = (
|
2024-09-13 14:15:37 -07:00
|
|
|
src && !autoPlayGif && ((isGif) || src.endsWith('.gif') || src.startsWith('blob:'))
|
2022-05-07 10:46:21 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
const handleImageLoad = () => {
|
|
|
|
if (hoverToPlay && canvas.current && img.current) {
|
2024-08-11 01:48:58 -07:00
|
|
|
canvas.current.width = img.current.naturalWidth;
|
2022-05-07 10:46:21 -07:00
|
|
|
canvas.current.height = img.current.naturalHeight;
|
|
|
|
canvas.current.getContext('2d')?.drawImage(img.current, 0, 0);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-11-20 12:23:18 -08:00
|
|
|
/** ClassNames shared between the `<img>` and `<canvas>` elements. */
|
2024-09-21 15:09:45 -07:00
|
|
|
const baseClassName = clsx('block size-full', {
|
2022-11-20 12:23:18 -08:00
|
|
|
'object-contain': letterboxed,
|
|
|
|
'object-cover': !letterboxed,
|
|
|
|
});
|
|
|
|
|
2022-05-07 10:46:21 -07:00
|
|
|
return (
|
2022-11-20 12:23:18 -08:00
|
|
|
<div
|
|
|
|
data-testid='still-image-container'
|
2024-09-11 08:52:38 -07:00
|
|
|
className={clsx(className, 'relative isolate overflow-hidden', { 'group': !noGroup })}
|
2022-11-20 12:23:18 -08:00
|
|
|
style={style}
|
|
|
|
>
|
2022-11-22 09:33:47 -08:00
|
|
|
<img
|
|
|
|
src={src}
|
|
|
|
alt={alt}
|
|
|
|
ref={img}
|
|
|
|
onLoad={handleImageLoad}
|
2023-04-24 05:17:51 -07:00
|
|
|
onError={onError}
|
2023-02-06 10:01:03 -08:00
|
|
|
className={clsx(baseClassName, {
|
2022-11-22 09:33:47 -08:00
|
|
|
'invisible group-hover:visible': hoverToPlay,
|
|
|
|
})}
|
|
|
|
/>
|
|
|
|
|
|
|
|
{hoverToPlay && (
|
|
|
|
<canvas
|
|
|
|
ref={canvas}
|
2023-02-06 10:01:03 -08:00
|
|
|
className={clsx(baseClassName, {
|
2023-07-05 12:39:50 -07:00
|
|
|
'absolute group-hover:invisible top-0': hoverToPlay,
|
2022-11-20 12:23:18 -08:00
|
|
|
})}
|
|
|
|
/>
|
2022-11-22 09:33:47 -08:00
|
|
|
)}
|
2022-11-20 12:23:18 -08:00
|
|
|
|
2022-11-22 09:33:47 -08:00
|
|
|
{(hoverToPlay && showExt) && (
|
2023-04-01 11:37:34 -07:00
|
|
|
<div className='pointer-events-none absolute bottom-2 left-2 opacity-90 group-hover:hidden'>
|
2022-11-22 09:33:47 -08:00
|
|
|
<ExtensionBadge ext='GIF' />
|
|
|
|
</div>
|
|
|
|
)}
|
2022-05-07 10:46:21 -07:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-11-20 12:53:24 -08:00
|
|
|
interface IExtensionBadge {
|
|
|
|
/** File extension. */
|
2023-10-02 11:54:02 -07:00
|
|
|
ext: string;
|
2022-11-20 12:53:24 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Badge displaying a file extension. */
|
2024-05-12 16:18:04 -07:00
|
|
|
const ExtensionBadge: React.FC<IExtensionBadge> = ({ ext }) => (
|
|
|
|
<div className='inline-flex items-center rounded bg-gray-100 px-2 py-0.5 text-sm font-medium text-gray-900 dark:bg-gray-800 dark:text-gray-100'>
|
|
|
|
{ext}
|
|
|
|
</div>
|
|
|
|
);
|
2022-11-20 12:53:24 -08:00
|
|
|
|
2024-05-13 10:00:42 -07:00
|
|
|
export {
|
|
|
|
type IStillImage,
|
|
|
|
StillImage as default,
|
|
|
|
};
|