mirror of
https://github.com/mediacms-io/mediacms.git
synced 2025-06-26 06:51:47 +02:00
feat: fullscreen images slideshow (#1120)
This commit is contained in:
parent
53d8215346
commit
6db01932e1
53
frontend/src/static/js/components/_shared/ToolTip.jsx
Normal file
53
frontend/src/static/js/components/_shared/ToolTip.jsx
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
import React, { useEffect, useRef, useState } from 'react';
|
||||||
|
import './ToolTip.scss';
|
||||||
|
|
||||||
|
function Tooltip({ children, content, title, position = 'right', classNames = '' }) {
|
||||||
|
const [active, setActive] = useState(false);
|
||||||
|
const [tooltipDimensions, setTooltipDimensions] = useState({
|
||||||
|
height: 0,
|
||||||
|
width: 0,
|
||||||
|
});
|
||||||
|
|
||||||
|
const popUpRef = useRef(null);
|
||||||
|
|
||||||
|
const showTip = () => {
|
||||||
|
setActive(true);
|
||||||
|
};
|
||||||
|
|
||||||
|
const hideTip = () => {
|
||||||
|
setActive(false);
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (popUpRef.current) {
|
||||||
|
setTooltipDimensions({
|
||||||
|
height: popUpRef.current.clientHeight || 0,
|
||||||
|
width: popUpRef.current.clientWidth || 0,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}, [active]);
|
||||||
|
|
||||||
|
const tooltipPositionStyles = {
|
||||||
|
right: { left: '100%', marginLeft: '10px', top: '-50%' },
|
||||||
|
left: { right: '100%', marginRight: '10px', top: '-50%' },
|
||||||
|
top: { left: '50%', top: `-${tooltipDimensions.height + 10}px`, transform: 'translateX(-50%)' },
|
||||||
|
center: { top: '50%', left: '50%', translate: 'x-[-50%]' },
|
||||||
|
'bottom-left': { left: `-${tooltipDimensions.width - 20}px`, top: '100%', marginTop: '10px' },
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div onMouseEnter={showTip} onMouseLeave={hideTip}>
|
||||||
|
<div
|
||||||
|
ref={popUpRef}
|
||||||
|
className={`tooltip-box ${active ? 'show' : 'hide'} ${classNames}`}
|
||||||
|
style={tooltipPositionStyles[position]}
|
||||||
|
>
|
||||||
|
{title && <div className="tooltip-title">{title}</div>}
|
||||||
|
<div className="tooltip-content">{content}</div>
|
||||||
|
</div>
|
||||||
|
{children}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Tooltip;
|
31
frontend/src/static/js/components/_shared/ToolTip.scss
Normal file
31
frontend/src/static/js/components/_shared/ToolTip.scss
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
.tooltip-box {
|
||||||
|
position: absolute;
|
||||||
|
padding: 10px;
|
||||||
|
z-index: 100;
|
||||||
|
background: #fff;
|
||||||
|
border-radius: 8px;
|
||||||
|
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
|
||||||
|
opacity: 0;
|
||||||
|
transform: translateY(-10px);
|
||||||
|
transition: opacity 0.3s ease, transform 0.3s ease;
|
||||||
|
}
|
||||||
|
.tooltip-box.show {
|
||||||
|
opacity: 1;
|
||||||
|
transform: translateY(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
.tooltip-box.hide {
|
||||||
|
opacity: 0;
|
||||||
|
transform: translateY(-10px);
|
||||||
|
}
|
||||||
|
.tooltip-title {
|
||||||
|
color: #333;
|
||||||
|
font-size: 14px;
|
||||||
|
margin-bottom: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.tooltip-content {
|
||||||
|
color: #666;
|
||||||
|
font-size: 12px;
|
||||||
|
}
|
||||||
|
|
@ -522,6 +522,7 @@
|
|||||||
display: block;
|
display: block;
|
||||||
|
|
||||||
img {
|
img {
|
||||||
|
cursor: pointer;
|
||||||
position: relative;
|
position: relative;
|
||||||
display: block;
|
display: block;
|
||||||
max-width: 100%;
|
max-width: 100%;
|
||||||
@ -530,6 +531,135 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.modal-overlay {
|
||||||
|
position: fixed;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
width: 100%;
|
||||||
|
height: 100%;
|
||||||
|
background: rgba(0, 0, 0, 0.8);
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
z-index: 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
.slideshow-container {
|
||||||
|
position: relative;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
width: auto;
|
||||||
|
max-width: 90%;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
.slideshow-image img {
|
||||||
|
display: block;
|
||||||
|
width: auto;
|
||||||
|
height: auto;
|
||||||
|
max-width: 100%; /* Ensure image doesn't exceed container width */
|
||||||
|
max-height: 90vh;
|
||||||
|
border-radius: 0;
|
||||||
|
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
|
||||||
|
transition: transform 60s ease-in-out, opacity 60 ease-in-out;
|
||||||
|
}
|
||||||
|
|
||||||
|
.slideshow-title {
|
||||||
|
margin-top: 10px;
|
||||||
|
text-align: start;
|
||||||
|
font-size: 16px;
|
||||||
|
font-weight: bold;
|
||||||
|
color: #bdb6b6;
|
||||||
|
z-index: 1200;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
.arrow {
|
||||||
|
position: absolute;
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
align-items: center;
|
||||||
|
width: 40px;
|
||||||
|
height: 40px;
|
||||||
|
top: 50%;
|
||||||
|
transform: translateY(-50%);
|
||||||
|
border: none;
|
||||||
|
color: white;
|
||||||
|
font-size: 2rem;
|
||||||
|
background-color: rgba(0, 0, 0, 0.2);
|
||||||
|
cursor: pointer;
|
||||||
|
padding: 10px;
|
||||||
|
border-radius: 50%;
|
||||||
|
z-index: 1000;
|
||||||
|
transition: background-color 0.2s ease, transform 0.2s ease;
|
||||||
|
text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.8);
|
||||||
|
}
|
||||||
|
|
||||||
|
.arrow:hover {
|
||||||
|
background: rgba(92, 78, 78, 0.6);
|
||||||
|
transform: translateY(-50%) scale(1.1);
|
||||||
|
}
|
||||||
|
|
||||||
|
.arrow.left {
|
||||||
|
left: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.arrow.right {
|
||||||
|
right: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.thumbnail-navigation {
|
||||||
|
position: fixed;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
margin-top: 20px;
|
||||||
|
gap: 10px;
|
||||||
|
bottom: 10%;
|
||||||
|
left: 50%;
|
||||||
|
transform: translateX(-50%);
|
||||||
|
}
|
||||||
|
|
||||||
|
.thumbnail-container {
|
||||||
|
display: flex;
|
||||||
|
gap: 10px;
|
||||||
|
overflow-x: auto;
|
||||||
|
scroll-behavior: smooth;
|
||||||
|
max-width: 80%;
|
||||||
|
padding: 10px 0;
|
||||||
|
scrollbar-width: none; /* Hide scrollbar for Firefox */
|
||||||
|
}
|
||||||
|
|
||||||
|
.thumbnail-container.center-thumbnails {
|
||||||
|
display: flex;
|
||||||
|
justify-content: center;
|
||||||
|
overflow: visible; /* No scrollbars for fewer thumbnails */
|
||||||
|
}
|
||||||
|
|
||||||
|
.thumbnail-container::-webkit-scrollbar {
|
||||||
|
display: none; /* Hide scrollbar for Chrome/Safari */
|
||||||
|
}
|
||||||
|
|
||||||
|
.thumbnail {
|
||||||
|
width: 60px;
|
||||||
|
height: 60px;
|
||||||
|
object-fit: cover;
|
||||||
|
border: 2px solid transparent;
|
||||||
|
border-radius: 5px;
|
||||||
|
cursor: pointer;
|
||||||
|
transition: transform 0.3s ease;
|
||||||
|
}
|
||||||
|
|
||||||
|
.thumbnail.active {
|
||||||
|
border-color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
.thumbnail:hover {
|
||||||
|
transform: scale(1.1);
|
||||||
|
}
|
||||||
|
|
||||||
.viewer-container .player-container {
|
.viewer-container .player-container {
|
||||||
@media screen and (min-width: 480px) {
|
@media screen and (min-width: 480px) {
|
||||||
border-radius: 10px;
|
border-radius: 10px;
|
||||||
|
@ -1,8 +1,10 @@
|
|||||||
import React, { useContext, useEffect, useState } from 'react';
|
import React, { useContext, useEffect, useState } from 'react';
|
||||||
import { SiteContext } from '../../utils/contexts/';
|
import { SiteContext } from '../../utils/contexts/';
|
||||||
import { MediaPageStore } from '../../utils/stores/';
|
import { MediaPageStore } from '../../utils/stores/';
|
||||||
|
import { SpinnerLoader } from '../_shared';
|
||||||
|
import Tooltip from '../_shared/ToolTip';
|
||||||
|
|
||||||
export default function ImageViewer(props) {
|
export default function ImageViewer() {
|
||||||
const site = useContext(SiteContext);
|
const site = useContext(SiteContext);
|
||||||
|
|
||||||
let initialImage = getImageUrl();
|
let initialImage = getImageUrl();
|
||||||
@ -11,6 +13,12 @@ export default function ImageViewer(props) {
|
|||||||
initialImage = initialImage ? initialImage : '';
|
initialImage = initialImage ? initialImage : '';
|
||||||
|
|
||||||
const [image, setImage] = useState(initialImage);
|
const [image, setImage] = useState(initialImage);
|
||||||
|
const [slideshowItems, setSlideshowItems] = useState([]);
|
||||||
|
const [isModalOpen, setIsModalOpen] = useState(false);
|
||||||
|
const [currentIndex, setCurrentIndex] = useState(0);
|
||||||
|
const [isImgLoading, setIsImgLoading] = useState(true);
|
||||||
|
|
||||||
|
const thumbnailRef = React.useRef();
|
||||||
|
|
||||||
function onImageLoad() {
|
function onImageLoad() {
|
||||||
setImage(getImageUrl());
|
setImage(getImageUrl());
|
||||||
@ -19,34 +27,142 @@ export default function ImageViewer(props) {
|
|||||||
function getImageUrl() {
|
function getImageUrl() {
|
||||||
const media_data = MediaPageStore.get('media-data');
|
const media_data = MediaPageStore.get('media-data');
|
||||||
|
|
||||||
let imgUrl = 'string' === typeof media_data.poster_url ? media_data.poster_url.trim() : '';
|
let imgUrl =
|
||||||
|
media_data.poster_url?.trim() ||
|
||||||
if ('' === imgUrl) {
|
media_data.thumbnail_url?.trim() ||
|
||||||
imgUrl = 'string' === typeof media_data.thumbnail_url ? media_data.thumbnail_url.trim() : '';
|
MediaPageStore.get('media-original-url')?.trim() ||
|
||||||
}
|
'#';
|
||||||
|
|
||||||
if ('' === imgUrl) {
|
|
||||||
imgUrl =
|
|
||||||
'string' === typeof MediaPageStore.get('media-original-url')
|
|
||||||
? MediaPageStore.get('media-original-url').trim()
|
|
||||||
: '';
|
|
||||||
}
|
|
||||||
|
|
||||||
if ('' === imgUrl) {
|
|
||||||
return '#';
|
|
||||||
}
|
|
||||||
|
|
||||||
return site.url + '/' + imgUrl.replace(/^\//g, '');
|
return site.url + '/' + imgUrl.replace(/^\//g, '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fetchSlideShowItems = () => {
|
||||||
|
const media_data = MediaPageStore.get('media-data');
|
||||||
|
const items = media_data.slideshow_items;
|
||||||
|
if (Array.isArray(items)) {
|
||||||
|
setSlideshowItems(items);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (image) {
|
||||||
|
fetchSlideShowItems();
|
||||||
|
}
|
||||||
|
}, [image]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
MediaPageStore.on('loaded_image_data', onImageLoad);
|
MediaPageStore.on('loaded_image_data', onImageLoad);
|
||||||
return () => MediaPageStore.removeListener('loaded_image_data', onImageLoad);
|
return () => MediaPageStore.removeListener('loaded_image_data', onImageLoad);
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isModalOpen) return;
|
||||||
|
|
||||||
|
document.addEventListener('keydown', handleKeyDown);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
document.removeEventListener('keydown', handleKeyDown);
|
||||||
|
};
|
||||||
|
}, [isModalOpen, slideshowItems]);
|
||||||
|
|
||||||
|
const handleKeyDown = (event) => {
|
||||||
|
if (event.key === 'ArrowRight') handleNext();
|
||||||
|
if (event.key === 'ArrowLeft') handlePrevious();
|
||||||
|
if (event.key === 'Escape') onClose();
|
||||||
|
};
|
||||||
|
|
||||||
|
const onClose = () => setIsModalOpen(false);
|
||||||
|
|
||||||
|
const handleNext = () => {
|
||||||
|
setIsImgLoading(true);
|
||||||
|
setCurrentIndex((prevIndex) => (prevIndex + 1) % slideshowItems.length);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handlePrevious = () => {
|
||||||
|
setIsImgLoading(true);
|
||||||
|
setCurrentIndex((prevIndex) => (prevIndex - 1 + slideshowItems.length) % slideshowItems.length);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDotClick = (index) => {
|
||||||
|
setIsImgLoading(true);
|
||||||
|
setCurrentIndex(index);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleImageClick = (index) => {
|
||||||
|
const mediaPageUrl = site.url + slideshowItems[index]?.url;
|
||||||
|
window.location.href = mediaPageUrl;
|
||||||
|
};
|
||||||
|
|
||||||
|
const scrollThumbnails = (direction) => {
|
||||||
|
if (thumbnailRef.current) {
|
||||||
|
const scrollAmount = 10;
|
||||||
|
if (direction === 'left') {
|
||||||
|
thumbnailRef.current.scrollBy({ left: -scrollAmount, behavior: 'smooth' });
|
||||||
|
} else if (direction === 'right') {
|
||||||
|
thumbnailRef.current.scrollBy({ left: scrollAmount, behavior: 'smooth' });
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
return !image ? null : (
|
return !image ? null : (
|
||||||
<div className="viewer-image-container">
|
<div className="viewer-image-container">
|
||||||
<img src={image} alt={MediaPageStore.get('media-data').title || null} />
|
<Tooltip content={'load full-image'} position="center">
|
||||||
|
<img src={image} alt={MediaPageStore.get('media-data').title || null} onClick={() => setIsModalOpen(true)} />
|
||||||
|
</Tooltip>
|
||||||
|
{isModalOpen && slideshowItems && (
|
||||||
|
<div className="modal-overlay" onClick={() => setIsModalOpen(false)}>
|
||||||
|
<div className="slideshow-container" onClick={(e) => e.stopPropagation()}>
|
||||||
|
{!isImgLoading && (
|
||||||
|
<button className="arrow left" onClick={handlePrevious} aria-label="Previous slide">
|
||||||
|
‹
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
<div className="slideshow-image">
|
||||||
|
{isImgLoading && <SpinnerLoader size="large" />}
|
||||||
|
<img
|
||||||
|
src={site.url + '/' + slideshowItems[currentIndex]?.original_media_url}
|
||||||
|
alt={`Slide ${currentIndex + 1}`}
|
||||||
|
onClick={() => handleImageClick(currentIndex)}
|
||||||
|
onLoad={() => setIsImgLoading(false)}
|
||||||
|
onError={() => setIsImgLoading(false)}
|
||||||
|
style={{ display: isImgLoading ? 'none' : 'block' }}
|
||||||
|
/>
|
||||||
|
{!isImgLoading && <div className="slideshow-title">{slideshowItems[currentIndex]?.title}</div>}
|
||||||
|
</div>
|
||||||
|
{!isImgLoading && (
|
||||||
|
<button className="arrow right" onClick={handleNext} aria-label="Next slide">
|
||||||
|
›
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
<div className="thumbnail-navigation">
|
||||||
|
{slideshowItems.length > 5 && (
|
||||||
|
<button className="arrow left" onClick={() => scrollThumbnails('left')} aria-label="Scroll left">
|
||||||
|
‹
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
<div
|
||||||
|
className={`thumbnail-container ${slideshowItems.length <= 5 ? 'center-thumbnails' : ''}`}
|
||||||
|
ref={thumbnailRef}
|
||||||
|
>
|
||||||
|
{slideshowItems.map((item, index) => (
|
||||||
|
<img
|
||||||
|
key={index}
|
||||||
|
src={site.url + '/' + item.thumbnail_url}
|
||||||
|
alt={`Thumbnail ${index + 1}`}
|
||||||
|
className={`thumbnail ${currentIndex === index ? 'active' : ''}`}
|
||||||
|
onClick={() => handleDotClick(index)}
|
||||||
|
/>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
{slideshowItems.length > 5 && (
|
||||||
|
<button className="arrow right" onClick={() => scrollThumbnails('right')} aria-label="Scroll right">
|
||||||
|
›
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user