2023-01-29 07:23:53 +00:00
|
|
|
import { getBlurHashAverageColor } from 'fast-blurhash';
|
2023-09-14 12:41:03 +00:00
|
|
|
import { Fragment } from 'preact';
|
2023-06-13 16:09:26 +00:00
|
|
|
import {
|
|
|
|
useCallback,
|
|
|
|
useLayoutEffect,
|
|
|
|
useMemo,
|
|
|
|
useRef,
|
|
|
|
useState,
|
|
|
|
} from 'preact/hooks';
|
2023-03-27 16:29:01 +00:00
|
|
|
import QuickPinchZoom, { make3dTransformValue } from 'react-quick-pinch-zoom';
|
2023-01-29 07:23:53 +00:00
|
|
|
|
2024-03-31 12:53:08 +00:00
|
|
|
import formatDuration from '../utils/format-duration';
|
2023-10-14 12:33:40 +00:00
|
|
|
import mem from '../utils/mem';
|
2023-09-28 07:48:32 +00:00
|
|
|
import states from '../utils/states';
|
|
|
|
|
2023-03-13 15:40:08 +00:00
|
|
|
import Icon from './icon';
|
2023-04-14 07:30:04 +00:00
|
|
|
import Link from './link';
|
2023-01-29 07:23:53 +00:00
|
|
|
|
2023-06-28 15:35:22 +00:00
|
|
|
const isSafari = /^((?!chrome|android).)*safari/i.test(navigator.userAgent); // https://stackoverflow.com/a/23522755
|
|
|
|
|
2023-01-29 07:23:53 +00:00
|
|
|
/*
|
|
|
|
Media type
|
|
|
|
===
|
|
|
|
unknown = unsupported or unrecognized file type
|
|
|
|
image = Static image
|
|
|
|
gifv = Looping, soundless animation
|
|
|
|
video = Video clip
|
|
|
|
audio = Audio track
|
|
|
|
*/
|
|
|
|
|
2023-09-28 07:48:32 +00:00
|
|
|
const dataAltLabel = 'ALT';
|
|
|
|
const AltBadge = (props) => {
|
2023-10-02 04:21:26 +00:00
|
|
|
const { alt, lang, index, ...rest } = props;
|
2023-09-28 07:48:32 +00:00
|
|
|
if (!alt || !alt.trim()) return null;
|
|
|
|
return (
|
|
|
|
<button
|
|
|
|
type="button"
|
2023-09-28 15:48:01 +00:00
|
|
|
class="alt-badge clickable"
|
2023-09-28 07:48:32 +00:00
|
|
|
{...rest}
|
|
|
|
onClick={(e) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
e.preventDefault();
|
2023-09-30 15:23:34 +00:00
|
|
|
states.showMediaAlt = {
|
|
|
|
alt,
|
|
|
|
lang,
|
|
|
|
};
|
2023-09-28 07:48:32 +00:00
|
|
|
}}
|
|
|
|
title="Media description"
|
|
|
|
>
|
|
|
|
{dataAltLabel}
|
2023-10-02 04:21:26 +00:00
|
|
|
{!!index && <sup>{index}</sup>}
|
2023-09-28 07:48:32 +00:00
|
|
|
</button>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-09-28 11:46:44 +00:00
|
|
|
const MEDIA_CAPTION_LIMIT = 140;
|
2024-02-29 05:25:30 +00:00
|
|
|
const MEDIA_CAPTION_LIMIT_LONGER = 280;
|
2023-10-02 10:58:42 +00:00
|
|
|
export const isMediaCaptionLong = mem((caption) =>
|
|
|
|
caption?.length
|
|
|
|
? caption.length > MEDIA_CAPTION_LIMIT ||
|
|
|
|
/[\n\r].*[\n\r]/.test(caption.trim())
|
|
|
|
: false,
|
|
|
|
);
|
2023-09-28 11:46:44 +00:00
|
|
|
|
2023-09-30 15:23:34 +00:00
|
|
|
function Media({
|
2023-10-29 13:41:03 +00:00
|
|
|
class: className = '',
|
2023-09-30 15:23:34 +00:00
|
|
|
media,
|
|
|
|
to,
|
|
|
|
lang,
|
|
|
|
showOriginal,
|
|
|
|
autoAnimate,
|
2023-10-01 05:18:31 +00:00
|
|
|
showCaption,
|
2024-02-29 05:25:30 +00:00
|
|
|
allowLongerCaption,
|
2023-10-02 04:21:26 +00:00
|
|
|
altIndex,
|
2023-09-30 15:23:34 +00:00
|
|
|
onClick = () => {},
|
|
|
|
}) {
|
2024-04-09 15:35:17 +00:00
|
|
|
let {
|
2023-04-28 11:28:36 +00:00
|
|
|
blurhash,
|
|
|
|
description,
|
|
|
|
meta,
|
|
|
|
previewRemoteUrl,
|
|
|
|
previewUrl,
|
|
|
|
remoteUrl,
|
|
|
|
url,
|
|
|
|
type,
|
|
|
|
} = media;
|
2024-04-09 15:35:17 +00:00
|
|
|
if (/no\-preview\./i.test(previewUrl)) {
|
|
|
|
previewUrl = null;
|
|
|
|
}
|
2023-02-23 09:01:33 +00:00
|
|
|
const { original = {}, small, focus } = meta || {};
|
2023-01-29 07:23:53 +00:00
|
|
|
|
2024-04-09 15:35:17 +00:00
|
|
|
const width = showOriginal
|
|
|
|
? original?.width
|
|
|
|
: small?.width || original?.width;
|
|
|
|
const height = showOriginal
|
|
|
|
? original?.height
|
|
|
|
: small?.height || original?.height;
|
2023-04-28 11:28:36 +00:00
|
|
|
const mediaURL = showOriginal ? url : previewUrl || url;
|
|
|
|
const remoteMediaURL = showOriginal
|
|
|
|
? remoteUrl
|
|
|
|
: previewRemoteUrl || remoteUrl;
|
2024-04-11 09:15:16 +00:00
|
|
|
const hasDimensions = width && height;
|
|
|
|
const orientation = hasDimensions
|
|
|
|
? width > height
|
|
|
|
? 'landscape'
|
|
|
|
: 'portrait'
|
|
|
|
: null;
|
2023-01-29 07:23:53 +00:00
|
|
|
|
|
|
|
const rgbAverageColor = blurhash ? getBlurHashAverageColor(blurhash) : null;
|
|
|
|
|
|
|
|
const videoRef = useRef();
|
|
|
|
|
2023-12-26 09:06:52 +00:00
|
|
|
let focalPosition;
|
2023-01-29 07:23:53 +00:00
|
|
|
if (focus) {
|
|
|
|
// Convert focal point to CSS background position
|
|
|
|
// Formula from jquery-focuspoint
|
|
|
|
// x = -1, y = 1 => 0% 0%
|
|
|
|
// x = 0, y = 0 => 50% 50%
|
|
|
|
// x = 1, y = -1 => 100% 100%
|
|
|
|
const x = ((focus.x + 1) / 2) * 100;
|
|
|
|
const y = ((1 - focus.y) / 2) * 100;
|
2023-12-26 09:06:52 +00:00
|
|
|
focalPosition = `${x.toFixed(0)}% ${y.toFixed(0)}%`;
|
2023-01-29 07:23:53 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 15:24:43 +00:00
|
|
|
const mediaRef = useRef();
|
2023-03-27 16:29:01 +00:00
|
|
|
const onUpdate = useCallback(({ x, y, scale }) => {
|
2023-03-28 15:24:43 +00:00
|
|
|
const { current: media } = mediaRef;
|
2023-03-27 16:29:01 +00:00
|
|
|
|
2023-03-28 15:24:43 +00:00
|
|
|
if (media) {
|
2023-03-27 16:29:01 +00:00
|
|
|
const value = make3dTransformValue({ x, y, scale });
|
|
|
|
|
2023-04-14 17:30:20 +00:00
|
|
|
if (scale === 1) {
|
|
|
|
media.style.removeProperty('transform');
|
|
|
|
} else {
|
|
|
|
media.style.setProperty('transform', value);
|
|
|
|
}
|
2023-03-28 02:26:27 +00:00
|
|
|
|
2023-03-28 15:24:43 +00:00
|
|
|
media.closest('.media-zoom').style.touchAction =
|
2023-04-15 03:45:34 +00:00
|
|
|
scale <= 1.01 ? 'pan-x' : '';
|
2023-03-27 16:29:01 +00:00
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
2023-04-09 11:46:49 +00:00
|
|
|
const [pinchZoomEnabled, setPinchZoomEnabled] = useState(false);
|
2023-03-28 15:24:43 +00:00
|
|
|
const quickPinchZoomProps = {
|
2023-04-09 11:46:49 +00:00
|
|
|
enabled: pinchZoomEnabled,
|
2023-03-28 15:24:43 +00:00
|
|
|
draggableUnZoomed: false,
|
|
|
|
inertiaFriction: 0.9,
|
2024-04-10 07:03:02 +00:00
|
|
|
tapZoomFactor: 2,
|
|
|
|
doubleTapToggleZoom: true,
|
2023-03-28 15:24:43 +00:00
|
|
|
containerProps: {
|
|
|
|
className: 'media-zoom',
|
|
|
|
style: {
|
2023-03-29 12:53:48 +00:00
|
|
|
overflow: 'visible',
|
|
|
|
// width: 'inherit',
|
|
|
|
// height: 'inherit',
|
|
|
|
// justifyContent: 'inherit',
|
|
|
|
// alignItems: 'inherit',
|
|
|
|
// display: 'inherit',
|
2023-03-28 15:24:43 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
onUpdate,
|
|
|
|
};
|
|
|
|
|
2023-04-24 11:27:12 +00:00
|
|
|
const Parent = useMemo(
|
|
|
|
() => (to ? (props) => <Link to={to} {...props} /> : 'div'),
|
|
|
|
[to],
|
|
|
|
);
|
2023-04-14 07:30:04 +00:00
|
|
|
|
2024-01-24 05:08:54 +00:00
|
|
|
const remoteMediaURLObj = remoteMediaURL ? new URL(remoteMediaURL) : null;
|
2023-09-13 10:10:20 +00:00
|
|
|
const isVideoMaybe =
|
|
|
|
type === 'unknown' &&
|
2024-01-24 05:08:54 +00:00
|
|
|
remoteMediaURLObj &&
|
|
|
|
/\.(mp4|m4r|m4v|mov|webm)$/i.test(remoteMediaURLObj.pathname);
|
|
|
|
const isAudioMaybe =
|
|
|
|
type === 'unknown' &&
|
|
|
|
remoteMediaURLObj &&
|
|
|
|
/\.(mp3|ogg|wav|m4a|m4p|m4b)$/i.test(remoteMediaURLObj.pathname);
|
2023-09-13 10:10:20 +00:00
|
|
|
const isImage =
|
2024-01-24 05:08:54 +00:00
|
|
|
type === 'image' ||
|
|
|
|
(type === 'unknown' && previewUrl && !isVideoMaybe && !isAudioMaybe);
|
2023-06-13 16:09:26 +00:00
|
|
|
|
|
|
|
const parentRef = useRef();
|
|
|
|
const [imageSmallerThanParent, setImageSmallerThanParent] = useState(false);
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
if (!isImage) return;
|
|
|
|
if (!showOriginal) return;
|
|
|
|
if (!parentRef.current) return;
|
|
|
|
const { offsetWidth, offsetHeight } = parentRef.current;
|
|
|
|
const smaller = width < offsetWidth && height < offsetHeight;
|
|
|
|
if (smaller) setImageSmallerThanParent(smaller);
|
|
|
|
}, [width, height]);
|
|
|
|
|
2023-08-02 09:41:00 +00:00
|
|
|
const maxAspectHeight =
|
|
|
|
window.innerHeight * (orientation === 'portrait' ? 0.45 : 0.33);
|
|
|
|
const maxHeight = orientation === 'portrait' ? 0 : 160;
|
2023-10-29 13:41:03 +00:00
|
|
|
const averageColorStyle = {
|
|
|
|
'--average-color': rgbAverageColor && `rgb(${rgbAverageColor.join(',')})`,
|
|
|
|
};
|
2023-10-25 12:22:58 +00:00
|
|
|
const mediaStyles =
|
|
|
|
width && height
|
|
|
|
? {
|
|
|
|
'--width': `${width}px`,
|
|
|
|
'--height': `${height}px`,
|
|
|
|
// Calculate '--aspectWidth' based on aspect ratio calculated from '--width' and '--height', max height has to be 160px
|
|
|
|
'--aspectWidth': `${
|
|
|
|
(width / height) * Math.max(maxHeight, maxAspectHeight)
|
|
|
|
}px`,
|
|
|
|
aspectRatio: `${width} / ${height}`,
|
2023-10-29 13:41:03 +00:00
|
|
|
...averageColorStyle,
|
2023-10-25 12:22:58 +00:00
|
|
|
}
|
2023-10-29 13:41:03 +00:00
|
|
|
: {
|
|
|
|
...averageColorStyle,
|
|
|
|
};
|
2023-07-30 13:28:17 +00:00
|
|
|
|
2023-10-02 10:58:42 +00:00
|
|
|
const longDesc = isMediaCaptionLong(description);
|
2024-02-29 05:25:30 +00:00
|
|
|
let showInlineDesc =
|
2023-10-02 10:58:42 +00:00
|
|
|
!!showCaption && !showOriginal && !!description && !longDesc;
|
2024-02-29 05:25:30 +00:00
|
|
|
if (
|
|
|
|
allowLongerCaption &&
|
|
|
|
!showInlineDesc &&
|
|
|
|
description?.length <= MEDIA_CAPTION_LIMIT_LONGER
|
|
|
|
) {
|
|
|
|
showInlineDesc = true;
|
|
|
|
}
|
2023-10-01 05:18:31 +00:00
|
|
|
const Figure = !showInlineDesc
|
|
|
|
? Fragment
|
|
|
|
: (props) => {
|
|
|
|
const { children, ...restProps } = props;
|
|
|
|
return (
|
|
|
|
<figure {...restProps}>
|
|
|
|
{children}
|
|
|
|
<figcaption
|
2023-10-02 10:58:42 +00:00
|
|
|
class="media-caption"
|
2023-10-01 05:18:31 +00:00
|
|
|
lang={lang}
|
|
|
|
dir="auto"
|
|
|
|
onClick={(e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
states.showMediaAlt = {
|
|
|
|
alt: description,
|
|
|
|
lang,
|
|
|
|
};
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{description}
|
|
|
|
</figcaption>
|
|
|
|
</figure>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-06-13 16:09:26 +00:00
|
|
|
if (isImage) {
|
2023-01-29 07:23:53 +00:00
|
|
|
// Note: type: unknown might not have width/height
|
2023-03-28 15:24:43 +00:00
|
|
|
quickPinchZoomProps.containerProps.style.display = 'inherit';
|
2023-06-28 15:35:22 +00:00
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
if (!isSafari) return;
|
2023-06-29 10:55:17 +00:00
|
|
|
if (!showOriginal) return;
|
2023-06-28 15:35:22 +00:00
|
|
|
(async () => {
|
|
|
|
try {
|
2023-06-28 16:27:15 +00:00
|
|
|
await fetch(mediaURL, { mode: 'no-cors' });
|
2023-06-28 15:35:22 +00:00
|
|
|
mediaRef.current.src = mediaURL;
|
|
|
|
} catch (e) {
|
|
|
|
// Ignore
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}, [mediaURL]);
|
|
|
|
|
2023-01-29 07:23:53 +00:00
|
|
|
return (
|
2023-10-01 05:18:31 +00:00
|
|
|
<Figure>
|
|
|
|
<Parent
|
|
|
|
ref={parentRef}
|
2023-10-29 13:41:03 +00:00
|
|
|
class={`media media-image ${className}`}
|
2023-10-01 05:18:31 +00:00
|
|
|
onClick={onClick}
|
|
|
|
data-orientation={orientation}
|
|
|
|
data-has-alt={!showInlineDesc}
|
|
|
|
style={
|
|
|
|
showOriginal
|
|
|
|
? {
|
|
|
|
backgroundImage: `url(${previewUrl})`,
|
|
|
|
backgroundSize: imageSmallerThanParent
|
|
|
|
? `${width}px ${height}px`
|
|
|
|
: undefined,
|
2023-10-29 13:41:03 +00:00
|
|
|
...averageColorStyle,
|
2023-09-28 07:48:32 +00:00
|
|
|
}
|
2023-10-01 05:18:31 +00:00
|
|
|
: mediaStyles
|
|
|
|
}
|
|
|
|
>
|
|
|
|
{showOriginal ? (
|
|
|
|
<QuickPinchZoom {...quickPinchZoomProps}>
|
|
|
|
<img
|
|
|
|
ref={mediaRef}
|
|
|
|
src={mediaURL}
|
|
|
|
alt={description}
|
|
|
|
width={width}
|
|
|
|
height={height}
|
|
|
|
data-orientation={orientation}
|
|
|
|
loading="eager"
|
|
|
|
decoding="sync"
|
|
|
|
onLoad={(e) => {
|
|
|
|
e.target.closest('.media-image').style.backgroundImage = '';
|
|
|
|
e.target.closest('.media-zoom').style.display = '';
|
|
|
|
setPinchZoomEnabled(true);
|
|
|
|
}}
|
|
|
|
onError={(e) => {
|
|
|
|
const { src } = e.target;
|
2024-04-09 15:35:17 +00:00
|
|
|
if (
|
|
|
|
src === mediaURL &&
|
|
|
|
remoteMediaURL &&
|
|
|
|
mediaURL !== remoteMediaURL
|
|
|
|
) {
|
2023-10-01 05:18:31 +00:00
|
|
|
e.target.src = remoteMediaURL;
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</QuickPinchZoom>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
<img
|
|
|
|
src={mediaURL}
|
|
|
|
alt={showInlineDesc ? '' : description}
|
|
|
|
width={width}
|
|
|
|
height={height}
|
|
|
|
data-orientation={orientation}
|
|
|
|
loading="lazy"
|
|
|
|
style={{
|
2023-12-26 09:06:52 +00:00
|
|
|
// backgroundColor:
|
|
|
|
// rgbAverageColor && `rgb(${rgbAverageColor.join(',')})`,
|
|
|
|
// backgroundPosition: focalBackgroundPosition || 'center',
|
2023-10-01 05:18:31 +00:00
|
|
|
// Duration based on width or height in pixels
|
2023-12-26 09:06:52 +00:00
|
|
|
objectPosition: focalPosition || 'center',
|
2023-10-01 05:18:31 +00:00
|
|
|
// 100px per second (rough estimate)
|
|
|
|
// Clamp between 5s and 120s
|
|
|
|
'--anim-duration': `${Math.min(
|
|
|
|
Math.max(Math.max(width, height) / 100, 5),
|
|
|
|
120,
|
|
|
|
)}s`,
|
|
|
|
}}
|
|
|
|
onLoad={(e) => {
|
2023-12-26 09:06:52 +00:00
|
|
|
// e.target.closest('.media-image').style.backgroundImage = '';
|
2023-10-01 05:18:31 +00:00
|
|
|
e.target.dataset.loaded = true;
|
2024-04-11 09:15:16 +00:00
|
|
|
if (!hasDimensions) {
|
|
|
|
const $media = e.target.closest('.media');
|
|
|
|
if ($media) {
|
|
|
|
$media.dataset.orientation =
|
|
|
|
e.target.naturalWidth > e.target.naturalHeight
|
|
|
|
? 'landscape'
|
|
|
|
: 'portrait';
|
|
|
|
$media.style['--width'] = `${e.target.naturalWidth}px`;
|
|
|
|
$media.style['--height'] = `${e.target.naturalHeight}px`;
|
|
|
|
$media.style.aspectRatio = `${e.target.naturalWidth}/${e.target.naturalHeight}`;
|
|
|
|
}
|
|
|
|
}
|
2023-10-01 05:18:31 +00:00
|
|
|
}}
|
|
|
|
onError={(e) => {
|
|
|
|
const { src } = e.target;
|
2023-12-10 19:06:09 +00:00
|
|
|
if (src === mediaURL && mediaURL !== remoteMediaURL) {
|
2023-10-01 05:18:31 +00:00
|
|
|
e.target.src = remoteMediaURL;
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
2023-10-02 04:21:26 +00:00
|
|
|
{!showInlineDesc && (
|
|
|
|
<AltBadge alt={description} lang={lang} index={altIndex} />
|
|
|
|
)}
|
2023-10-01 05:18:31 +00:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Parent>
|
|
|
|
</Figure>
|
2023-01-29 07:23:53 +00:00
|
|
|
);
|
2023-09-13 10:10:20 +00:00
|
|
|
} else if (type === 'gifv' || type === 'video' || isVideoMaybe) {
|
2024-04-11 09:15:16 +00:00
|
|
|
const hasDuration = original.duration > 0;
|
2023-01-29 07:23:53 +00:00
|
|
|
const shortDuration = original.duration < 31;
|
|
|
|
const isGIF = type === 'gifv' && shortDuration;
|
|
|
|
// If GIF is too long, treat it as a video
|
|
|
|
const loopable = original.duration < 61;
|
|
|
|
const formattedDuration = formatDuration(original.duration);
|
|
|
|
const hoverAnimate = !showOriginal && !autoAnimate && isGIF;
|
|
|
|
const autoGIFAnimate = !showOriginal && autoAnimate && isGIF;
|
2023-11-23 14:59:27 +00:00
|
|
|
const showProgress = original.duration > 5;
|
2023-03-28 15:24:43 +00:00
|
|
|
|
|
|
|
const videoHTML = `
|
|
|
|
<video
|
|
|
|
src="${url}"
|
|
|
|
poster="${previewUrl}"
|
|
|
|
width="${width}"
|
|
|
|
height="${height}"
|
2023-04-14 13:14:08 +00:00
|
|
|
data-orientation="${orientation}"
|
2023-03-28 15:24:43 +00:00
|
|
|
preload="auto"
|
|
|
|
autoplay
|
|
|
|
muted="${isGIF}"
|
|
|
|
${isGIF ? '' : 'controls'}
|
|
|
|
playsinline
|
|
|
|
loop="${loopable}"
|
|
|
|
${isGIF ? 'ondblclick="this.paused ? this.play() : this.pause()"' : ''}
|
2023-11-23 14:59:27 +00:00
|
|
|
${
|
|
|
|
isGIF && showProgress
|
|
|
|
? "ontimeupdate=\"this.closest('.media-gif') && this.closest('.media-gif').style.setProperty('--progress', `${~~((this.currentTime / this.duration) * 100)}%`)\""
|
|
|
|
: ''
|
|
|
|
}
|
2023-03-28 15:24:43 +00:00
|
|
|
></video>
|
|
|
|
`;
|
|
|
|
|
2023-01-29 07:23:53 +00:00
|
|
|
return (
|
2023-10-01 05:18:31 +00:00
|
|
|
<Figure>
|
2023-09-14 12:41:03 +00:00
|
|
|
<Parent
|
2023-10-29 13:41:03 +00:00
|
|
|
class={`media ${className} media-${isGIF ? 'gif' : 'video'} ${
|
2023-09-14 12:41:03 +00:00
|
|
|
autoGIFAnimate ? 'media-contain' : ''
|
2023-12-30 12:29:21 +00:00
|
|
|
} ${hoverAnimate ? 'media-hover-animate' : ''}`}
|
2023-09-14 12:41:03 +00:00
|
|
|
data-orientation={orientation}
|
2023-10-29 13:41:03 +00:00
|
|
|
data-formatted-duration={
|
|
|
|
!showOriginal ? formattedDuration : undefined
|
|
|
|
}
|
2023-09-14 12:41:03 +00:00
|
|
|
data-label={isGIF && !showOriginal && !autoGIFAnimate ? 'GIF' : ''}
|
2023-10-01 05:18:31 +00:00
|
|
|
data-has-alt={!showInlineDesc}
|
2023-09-14 12:41:03 +00:00
|
|
|
// style={{
|
|
|
|
// backgroundColor:
|
|
|
|
// rgbAverageColor && `rgb(${rgbAverageColor.join(',')})`,
|
|
|
|
// }}
|
|
|
|
style={!showOriginal && mediaStyles}
|
|
|
|
onClick={(e) => {
|
|
|
|
if (hoverAnimate) {
|
|
|
|
try {
|
|
|
|
videoRef.current.pause();
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
onClick(e);
|
|
|
|
}}
|
|
|
|
onMouseEnter={() => {
|
|
|
|
if (hoverAnimate) {
|
|
|
|
try {
|
|
|
|
videoRef.current.play();
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onMouseLeave={() => {
|
|
|
|
if (hoverAnimate) {
|
|
|
|
try {
|
|
|
|
videoRef.current.pause();
|
2023-09-29 13:02:29 +00:00
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onFocus={() => {
|
|
|
|
if (hoverAnimate) {
|
|
|
|
try {
|
|
|
|
videoRef.current.play();
|
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onBlur={() => {
|
|
|
|
if (hoverAnimate) {
|
|
|
|
try {
|
|
|
|
videoRef.current.pause();
|
2023-09-14 12:41:03 +00:00
|
|
|
} catch (e) {}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{showOriginal || autoGIFAnimate ? (
|
|
|
|
isGIF && showOriginal ? (
|
|
|
|
<QuickPinchZoom {...quickPinchZoomProps} enabled>
|
|
|
|
<div
|
|
|
|
ref={mediaRef}
|
|
|
|
dangerouslySetInnerHTML={{
|
|
|
|
__html: videoHTML,
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</QuickPinchZoom>
|
|
|
|
) : (
|
2023-03-28 15:24:43 +00:00
|
|
|
<div
|
2023-09-14 12:41:03 +00:00
|
|
|
class="video-container"
|
2023-03-28 15:24:43 +00:00
|
|
|
dangerouslySetInnerHTML={{
|
|
|
|
__html: videoHTML,
|
|
|
|
}}
|
|
|
|
/>
|
2023-09-14 12:41:03 +00:00
|
|
|
)
|
|
|
|
) : isGIF ? (
|
|
|
|
<video
|
|
|
|
ref={videoRef}
|
|
|
|
src={url}
|
|
|
|
poster={previewUrl}
|
2023-03-13 15:40:08 +00:00
|
|
|
width={width}
|
|
|
|
height={height}
|
2023-04-14 13:14:08 +00:00
|
|
|
data-orientation={orientation}
|
2023-09-14 12:41:03 +00:00
|
|
|
preload="auto"
|
|
|
|
// controls
|
|
|
|
playsinline
|
|
|
|
loop
|
|
|
|
muted
|
2023-11-23 14:59:27 +00:00
|
|
|
onTimeUpdate={
|
|
|
|
showProgress
|
|
|
|
? (e) => {
|
|
|
|
const { target } = e;
|
|
|
|
const container = target?.closest('.media-gif');
|
|
|
|
if (container) {
|
|
|
|
const percentage =
|
|
|
|
(target.currentTime / target.duration) * 100;
|
|
|
|
container.style.setProperty(
|
|
|
|
'--progress',
|
|
|
|
`${percentage}%`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
: undefined
|
|
|
|
}
|
2023-03-13 15:40:08 +00:00
|
|
|
/>
|
2023-09-14 12:41:03 +00:00
|
|
|
) : (
|
|
|
|
<>
|
2024-04-09 15:35:17 +00:00
|
|
|
{previewUrl ? (
|
|
|
|
<img
|
|
|
|
src={previewUrl}
|
|
|
|
alt={showInlineDesc ? '' : description}
|
|
|
|
width={width}
|
|
|
|
height={height}
|
|
|
|
data-orientation={orientation}
|
|
|
|
loading="lazy"
|
2024-04-11 09:45:19 +00:00
|
|
|
onLoad={(e) => {
|
|
|
|
if (!hasDimensions) {
|
|
|
|
const $media = e.target.closest('.media');
|
|
|
|
if ($media) {
|
|
|
|
$media.dataset.orientation =
|
|
|
|
e.target.naturalWidth > e.target.naturalHeight
|
|
|
|
? 'landscape'
|
|
|
|
: 'portrait';
|
|
|
|
$media.style['--width'] = `${e.target.naturalWidth}px`;
|
|
|
|
$media.style[
|
|
|
|
'--height'
|
|
|
|
] = `${e.target.naturalHeight}px`;
|
|
|
|
$media.style.aspectRatio = `${e.target.naturalWidth}/${e.target.naturalHeight}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
2024-04-09 15:35:17 +00:00
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<video
|
2024-04-11 10:24:51 +00:00
|
|
|
src={url + '#t=0.1'} // Make Safari show 1st-frame preview
|
2024-04-09 15:35:17 +00:00
|
|
|
width={width}
|
|
|
|
height={height}
|
|
|
|
data-orientation={orientation}
|
|
|
|
preload="metadata"
|
|
|
|
muted
|
|
|
|
disablePictureInPicture
|
2024-04-11 09:15:16 +00:00
|
|
|
onLoadedMetadata={(e) => {
|
|
|
|
if (!hasDuration) {
|
|
|
|
const { duration } = e.target;
|
|
|
|
if (duration) {
|
|
|
|
const formattedDuration = formatDuration(duration);
|
|
|
|
const container = e.target.closest('.media-video');
|
|
|
|
if (container) {
|
|
|
|
container.dataset.formattedDuration =
|
|
|
|
formattedDuration;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
2024-04-09 15:35:17 +00:00
|
|
|
/>
|
|
|
|
)}
|
2023-09-14 12:41:03 +00:00
|
|
|
<div class="media-play">
|
|
|
|
<Icon icon="play" size="xl" />
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)}
|
2023-09-30 15:23:34 +00:00
|
|
|
{!showOriginal && !showInlineDesc && (
|
2023-10-02 04:21:26 +00:00
|
|
|
<AltBadge alt={description} lang={lang} index={altIndex} />
|
2023-09-30 15:23:34 +00:00
|
|
|
)}
|
2023-09-14 12:41:03 +00:00
|
|
|
</Parent>
|
2023-10-01 05:18:31 +00:00
|
|
|
</Figure>
|
2023-01-29 07:23:53 +00:00
|
|
|
);
|
2024-01-24 05:08:54 +00:00
|
|
|
} else if (type === 'audio' || isAudioMaybe) {
|
2023-01-29 07:23:53 +00:00
|
|
|
const formattedDuration = formatDuration(original.duration);
|
|
|
|
return (
|
2023-10-01 05:18:31 +00:00
|
|
|
<Figure>
|
|
|
|
<Parent
|
2023-10-29 13:41:03 +00:00
|
|
|
class={`media media-audio ${className}`}
|
|
|
|
data-formatted-duration={
|
|
|
|
!showOriginal ? formattedDuration : undefined
|
|
|
|
}
|
2023-10-01 05:18:31 +00:00
|
|
|
data-has-alt={!showInlineDesc}
|
|
|
|
onClick={onClick}
|
|
|
|
style={!showOriginal && mediaStyles}
|
|
|
|
>
|
|
|
|
{showOriginal ? (
|
|
|
|
<audio src={remoteUrl || url} preload="none" controls autoplay />
|
|
|
|
) : previewUrl ? (
|
|
|
|
<img
|
|
|
|
src={previewUrl}
|
|
|
|
alt={showInlineDesc ? '' : description}
|
|
|
|
width={width}
|
|
|
|
height={height}
|
|
|
|
data-orientation={orientation}
|
|
|
|
loading="lazy"
|
2024-01-24 05:08:54 +00:00
|
|
|
onError={(e) => {
|
|
|
|
try {
|
|
|
|
// Remove self if broken
|
|
|
|
e.target?.remove?.();
|
|
|
|
} catch (e) {}
|
|
|
|
}}
|
2023-10-01 05:18:31 +00:00
|
|
|
/>
|
|
|
|
) : null}
|
|
|
|
{!showOriginal && (
|
|
|
|
<>
|
|
|
|
<div class="media-play">
|
|
|
|
<Icon icon="play" size="xl" />
|
|
|
|
</div>
|
2023-10-02 04:21:26 +00:00
|
|
|
{!showInlineDesc && (
|
|
|
|
<AltBadge alt={description} lang={lang} index={altIndex} />
|
|
|
|
)}
|
2023-10-01 05:18:31 +00:00
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</Parent>
|
|
|
|
</Figure>
|
2023-01-29 07:23:53 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Media;
|