feat: add expand functionality and modal support for image, PDF, and video components

This commit is contained in:
swve 2025-07-09 16:05:11 +02:00
parent 7ef7f9feee
commit 70ce4bcac6
3 changed files with 314 additions and 166 deletions

View file

@ -1,7 +1,7 @@
import { NodeViewWrapper } from '@tiptap/react' import { NodeViewWrapper } from '@tiptap/react'
import React, { useEffect } from 'react' import React, { useEffect } from 'react'
import { Resizable } from 're-resizable' import { Resizable } from 're-resizable'
import { AlertTriangle, Image, Download, AlignLeft, AlignCenter, AlignRight } from 'lucide-react' import { AlertTriangle, Image, Download, AlignLeft, AlignCenter, AlignRight, Expand } from 'lucide-react'
import { uploadNewImageFile } from '../../../../../services/blocks/Image/images' import { uploadNewImageFile } from '../../../../../services/blocks/Image/images'
import { getActivityBlockMediaDirectory } from '@services/media/media' import { getActivityBlockMediaDirectory } from '@services/media/media'
import { useOrg } from '@components/Contexts/OrgContext' import { useOrg } from '@components/Contexts/OrgContext'
@ -10,6 +10,7 @@ import { useEditorProvider } from '@components/Contexts/Editor/EditorContext'
import { useLHSession } from '@components/Contexts/LHSessionContext' import { useLHSession } from '@components/Contexts/LHSessionContext'
import { FileUploadBlock, FileUploadBlockButton, FileUploadBlockInput } from '../../FileUploadBlock' import { FileUploadBlock, FileUploadBlockButton, FileUploadBlockInput } from '../../FileUploadBlock'
import { constructAcceptValue } from '@/lib/constants'; import { constructAcceptValue } from '@/lib/constants';
import Modal from '@components/Objects/StyledElements/Modal/Modal'
const SUPPORTED_FILES = constructAcceptValue(['image']) const SUPPORTED_FILES = constructAcceptValue(['image'])
@ -30,6 +31,7 @@ function ImageBlockComponent(props: any) {
width: props.node.attrs.size ? props.node.attrs.size.width : 300, width: props.node.attrs.size ? props.node.attrs.size.width : 300,
}) })
const [alignment, setAlignment] = React.useState(props.node.attrs.alignment || 'center') const [alignment, setAlignment] = React.useState(props.node.attrs.alignment || 'center')
const [isModalOpen, setIsModalOpen] = React.useState(false)
const fileId = blockObject const fileId = blockObject
? `${blockObject.content.file_id}.${blockObject.content.file_format}` ? `${blockObject.content.file_id}.${blockObject.content.file_format}`
@ -78,6 +80,10 @@ function ImageBlockComponent(props: any) {
document.body.removeChild(link); document.body.removeChild(link);
}; };
const handleExpand = () => {
setIsModalOpen(true);
};
const handleAlignmentChange = (newAlignment: string) => { const handleAlignmentChange = (newAlignment: string) => {
setAlignment(newAlignment); setAlignment(newAlignment);
props.updateAttributes({ props.updateAttributes({
@ -85,6 +91,15 @@ function ImageBlockComponent(props: any) {
}); });
}; };
const imageUrl = blockObject ? getActivityBlockMediaDirectory(
org?.org_uuid,
course?.courseStructure.course_uuid,
props.extension.options.activity.activity_uuid,
blockObject.block_uuid,
fileId || '',
'imageBlock'
) : null;
useEffect(() => {}, [course, org]) useEffect(() => {}, [course, org])
const getAlignmentClass = () => { const getAlignmentClass = () => {
@ -99,126 +114,150 @@ function ImageBlockComponent(props: any) {
}; };
return ( return (
<NodeViewWrapper className="block-image w-full"> <>
<FileUploadBlock isEditable={isEditable} isLoading={isLoading} isEmpty={!blockObject} Icon={Image}> <NodeViewWrapper className="block-image w-full">
<FileUploadBlockInput onChange={handleImageChange} accept={SUPPORTED_FILES} /> <FileUploadBlock isEditable={isEditable} isLoading={isLoading} isEmpty={!blockObject} Icon={Image}>
<FileUploadBlockButton onClick={handleSubmit} disabled={!image}/> <FileUploadBlockInput onChange={handleImageChange} accept={SUPPORTED_FILES} />
</FileUploadBlock> <FileUploadBlockButton onClick={handleSubmit} disabled={!image}/>
</FileUploadBlock>
{blockObject && isEditable && (
<div className={`w-full flex ${getAlignmentClass()}`}> {blockObject && isEditable && (
<Resizable <div className={`w-full flex ${getAlignmentClass()}`}>
defaultSize={{ width: imageSize.width, height: '100%' }} <Resizable
handleStyles={{ defaultSize={{ width: imageSize.width, height: '100%' }}
right: { handleStyles={{
position: 'unset', right: {
width: 7, position: 'unset',
height: 30, width: 7,
borderRadius: 20, height: 30,
cursor: 'col-resize', borderRadius: 20,
backgroundColor: 'black', cursor: 'col-resize',
opacity: '0.3', backgroundColor: 'black',
margin: 'auto', opacity: '0.3',
marginLeft: 5, margin: 'auto',
}, marginLeft: 5,
}}
style={{
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
height: '100%',
maxWidth: '100%',
}}
maxWidth="100%"
minWidth={200}
enable={{ right: true }}
onResizeStop={(e, direction, ref, d) => {
const newWidth = Math.min(imageSize.width + d.width, ref.parentElement?.clientWidth || 1000);
props.updateAttributes({
size: {
width: newWidth,
}, },
}) }}
setImageSize({ style={{
width: newWidth, display: 'flex',
}) justifyContent: 'center',
}} alignItems: 'center',
> height: '100%',
maxWidth: '100%',
}}
maxWidth="100%"
minWidth={200}
enable={{ right: true }}
onResizeStop={(e, direction, ref, d) => {
const newWidth = Math.min(imageSize.width + d.width, ref.parentElement?.clientWidth || 1000);
props.updateAttributes({
size: {
width: newWidth,
},
})
setImageSize({
width: newWidth,
})
}}
>
<div className="relative">
<img
src={imageUrl || ''}
alt=""
className="rounded-lg shadow-sm max-w-full h-auto"
style={{ width: '100%' }}
/>
<div className="absolute top-2 right-2 flex items-center gap-1.5 bg-white bg-opacity-90 backdrop-blur-xs rounded-lg p-1 shadow-xs transition-opacity opacity-70 hover:opacity-100">
<button
onClick={() => handleAlignmentChange('left')}
className={`p-1.5 rounded-md hover:bg-gray-100 text-gray-600 ${alignment === 'left' ? 'bg-gray-100' : ''}`}
title="Align left"
>
<AlignLeft size={16} />
</button>
<button
onClick={() => handleAlignmentChange('center')}
className={`p-1.5 rounded-md hover:bg-gray-100 text-gray-600 ${alignment === 'center' ? 'bg-gray-100' : ''}`}
title="Center align"
>
<AlignCenter size={16} />
</button>
<button
onClick={() => handleAlignmentChange('right')}
className={`p-1.5 rounded-md hover:bg-gray-100 text-gray-600 ${alignment === 'right' ? 'bg-gray-100' : ''}`}
title="Align right"
>
<AlignRight size={16} />
</button>
<div className="w-px h-4 bg-gray-300"></div>
<button
onClick={handleExpand}
className="p-1.5 rounded-md hover:bg-gray-100 text-gray-600"
title="Expand image"
>
<Expand size={16} />
</button>
</div>
</div>
</Resizable>
</div>
)}
{blockObject && !isEditable && (
<div className={`w-full flex ${getAlignmentClass()}`}>
<div className="relative"> <div className="relative">
<img <img
src={`${getActivityBlockMediaDirectory( src={imageUrl || ''}
org?.org_uuid,
course?.courseStructure.course_uuid,
props.extension.options.activity.activity_uuid,
blockObject.block_uuid,
blockObject ? fileId : ' ',
'imageBlock'
)}`}
alt="" alt=""
className="rounded-lg shadow-sm max-w-full h-auto" className="rounded-lg shadow-sm max-w-full h-auto"
style={{ width: '100%' }} style={{ width: imageSize.width, maxWidth: '100%' }}
/> />
<div className="absolute top-2 right-2 flex items-center gap-1.5 bg-white bg-opacity-90 backdrop-blur-xs rounded-lg p-1 shadow-xs transition-opacity opacity-70 hover:opacity-100"> <div className="absolute top-2 right-2 flex gap-1">
<button <button
onClick={() => handleAlignmentChange('left')} onClick={handleExpand}
className={`p-1.5 rounded-md hover:bg-gray-100 text-gray-600 ${alignment === 'left' ? 'bg-gray-100' : ''}`} className="p-2 bg-black/50 hover:bg-black/70 rounded-full transition-colors"
title="Align left" title="Expand image"
> >
<AlignLeft size={16} /> <Expand className="w-4 h-4 text-white" />
</button> </button>
<button <button
onClick={() => handleAlignmentChange('center')} onClick={handleDownload}
className={`p-1.5 rounded-md hover:bg-gray-100 text-gray-600 ${alignment === 'center' ? 'bg-gray-100' : ''}`} className="p-2 bg-black/50 hover:bg-black/70 rounded-full transition-colors"
title="Center align" title="Download image"
> >
<AlignCenter size={16} /> <Download className="w-4 h-4 text-white" />
</button>
<button
onClick={() => handleAlignmentChange('right')}
className={`p-1.5 rounded-md hover:bg-gray-100 text-gray-600 ${alignment === 'right' ? 'bg-gray-100' : ''}`}
title="Align right"
>
<AlignRight size={16} />
</button> </button>
</div> </div>
</div> </div>
</Resizable>
</div>
)}
{blockObject && !isEditable && (
<div className={`w-full flex ${getAlignmentClass()}`}>
<div className="relative">
<img
src={`${getActivityBlockMediaDirectory(
org?.org_uuid,
course?.courseStructure.course_uuid,
props.extension.options.activity.activity_uuid,
blockObject.block_uuid,
blockObject ? fileId : ' ',
'imageBlock'
)}`}
alt=""
className="rounded-lg shadow-sm max-w-full h-auto"
style={{ width: imageSize.width, maxWidth: '100%' }}
/>
<button
onClick={handleDownload}
className="absolute top-2 right-2 p-2 bg-black/50 hover:bg-black/70 rounded-full transition-colors"
title="Download image"
>
<Download className="w-4 h-4 text-white" />
</button>
</div> </div>
</div> )}
)}
{isLoading && ( {isLoading && (
<div> <div>
<AlertTriangle color="#e1e0e0" size={50} /> <AlertTriangle color="#e1e0e0" size={50} />
</div> </div>
)}
</NodeViewWrapper>
{blockObject && imageUrl && (
<Modal
isDialogOpen={isModalOpen}
onOpenChange={setIsModalOpen}
dialogTitle="Image Viewer"
minWidth="lg"
minHeight="lg"
dialogContent={
<div className="w-full flex items-center justify-center">
<img
src={imageUrl}
alt=""
className="max-w-full max-h-[80vh] object-contain rounded-lg shadow-lg"
/>
</div>
}
/>
)} )}
</NodeViewWrapper> </>
) )
} }

View file

@ -1,7 +1,7 @@
import { NodeViewWrapper } from '@tiptap/react' import { NodeViewWrapper } from '@tiptap/react'
import React, { useEffect } from 'react' import React, { useEffect } from 'react'
import styled from 'styled-components' import styled from 'styled-components'
import { AlertTriangle, FileText, Download } from 'lucide-react' import { AlertTriangle, FileText, Download, Expand } from 'lucide-react'
import { uploadNewPDFFile } from '../../../../../services/blocks/Pdf/pdf' import { uploadNewPDFFile } from '../../../../../services/blocks/Pdf/pdf'
import { getActivityBlockMediaDirectory } from '@services/media/media' import { getActivityBlockMediaDirectory } from '@services/media/media'
import { useOrg } from '@components/Contexts/OrgContext' import { useOrg } from '@components/Contexts/OrgContext'
@ -10,6 +10,7 @@ import { useEditorProvider } from '@components/Contexts/Editor/EditorContext'
import { useLHSession } from '@components/Contexts/LHSessionContext' import { useLHSession } from '@components/Contexts/LHSessionContext'
import { FileUploadBlock, FileUploadBlockButton, FileUploadBlockInput } from '../../FileUploadBlock' import { FileUploadBlock, FileUploadBlockButton, FileUploadBlockInput } from '../../FileUploadBlock'
import { constructAcceptValue } from '@/lib/constants'; import { constructAcceptValue } from '@/lib/constants';
import Modal from '@components/Objects/StyledElements/Modal/Modal'
const SUPPORTED_FILES = constructAcceptValue(['pdf']) const SUPPORTED_FILES = constructAcceptValue(['pdf'])
@ -23,6 +24,7 @@ function PDFBlockComponent(props: any) {
const [blockObject, setblockObject] = React.useState( const [blockObject, setblockObject] = React.useState(
props.node.attrs.blockObject props.node.attrs.blockObject
) )
const [isModalOpen, setIsModalOpen] = React.useState(false)
const fileId = blockObject const fileId = blockObject
? `${blockObject.content.file_id}.${blockObject.content.file_format}` ? `${blockObject.content.file_id}.${blockObject.content.file_format}`
: null : null
@ -70,47 +72,83 @@ function PDFBlockComponent(props: any) {
document.body.removeChild(link); document.body.removeChild(link);
}; };
const handleExpand = () => {
setIsModalOpen(true);
};
const pdfUrl = blockObject ? getActivityBlockMediaDirectory(
org?.org_uuid,
course?.courseStructure.course_uuid,
props.extension.options.activity.activity_uuid,
blockObject.block_uuid,
fileId || '',
'pdfBlock'
) : null;
useEffect(() => { }, [course, org]) useEffect(() => { }, [course, org])
return ( return (
<NodeViewWrapper className="block-pdf"> <>
<FileUploadBlock isEditable={isEditable} isLoading={isLoading} isEmpty={!blockObject} Icon={FileText}> <NodeViewWrapper className="block-pdf">
<FileUploadBlockInput onChange={handlePDFChange} accept={SUPPORTED_FILES} /> <FileUploadBlock isEditable={isEditable} isLoading={isLoading} isEmpty={!blockObject} Icon={FileText}>
<FileUploadBlockButton onClick={handleSubmit} disabled={!pdf}/> <FileUploadBlockInput onChange={handlePDFChange} accept={SUPPORTED_FILES} />
</FileUploadBlock> <FileUploadBlockButton onClick={handleSubmit} disabled={!pdf}/>
</FileUploadBlock>
{blockObject && (
<BlockPDF> {blockObject && (
<div className="relative"> <BlockPDF>
<iframe <div className="relative">
className="shadow-sm rounded-lg h-96 w-full object-scale-down bg-black" <iframe
src={`${getActivityBlockMediaDirectory( className="shadow-sm rounded-lg h-96 w-full object-scale-down bg-black"
org?.org_uuid, src={pdfUrl || ''}
course?.courseStructure.course_uuid, />
props.extension.options.activity.activity_uuid, <div className="absolute top-2 right-2 flex gap-1">
blockObject.block_uuid, <button
blockObject ? fileId : ' ', onClick={handleExpand}
'pdfBlock' className="p-2 bg-black/50 hover:bg-black/70 rounded-full transition-colors"
)}`} title="Expand PDF"
/> >
{!isEditable && ( <Expand className="w-4 h-4 text-white" />
<button </button>
onClick={handleDownload} {!isEditable && (
className="absolute top-2 right-2 p-2 bg-black/50 hover:bg-black/70 rounded-full transition-colors" <button
title="Download PDF" onClick={handleDownload}
> className="p-2 bg-black/50 hover:bg-black/70 rounded-full transition-colors"
<Download className="w-4 h-4 text-white" /> title="Download PDF"
</button> >
)} <Download className="w-4 h-4 text-white" />
</button>
)}
</div>
</div>
</BlockPDF>
)}
{isLoading && (
<div>
<AlertTriangle color="#e1e0e0" size={50} />
</div> </div>
</BlockPDF> )}
</NodeViewWrapper>
{blockObject && pdfUrl && (
<Modal
isDialogOpen={isModalOpen}
onOpenChange={setIsModalOpen}
dialogTitle="PDF Document"
minWidth="xl"
minHeight="xl"
dialogContent={
<div className="w-full h-[80vh]">
<iframe
className="w-full h-full rounded-lg shadow-lg border"
src={pdfUrl}
title="PDF Document"
/>
</div>
}
/>
)} )}
{isLoading && ( </>
<div>
<AlertTriangle color="#e1e0e0" size={50} />
</div>
)}
</NodeViewWrapper>
) )
} }

View file

@ -3,7 +3,7 @@ import { Node } from '@tiptap/core'
import { import {
Loader2, Video, Upload, X, HelpCircle, Loader2, Video, Upload, X, HelpCircle,
Maximize2, Minimize2, ArrowLeftRight, Maximize2, Minimize2, ArrowLeftRight,
CheckCircle2, AlertCircle, Download CheckCircle2, AlertCircle, Download, Expand
} from 'lucide-react' } from 'lucide-react'
import React from 'react' import React from 'react'
import { uploadNewVideoFile } from '../../../../../services/blocks/Video/video' import { uploadNewVideoFile } from '../../../../../services/blocks/Video/video'
@ -16,6 +16,7 @@ import { constructAcceptValue } from '@/lib/constants'
import { cn } from '@/lib/utils' import { cn } from '@/lib/utils'
import { motion, AnimatePresence } from 'framer-motion' import { motion, AnimatePresence } from 'framer-motion'
import styled from 'styled-components' import styled from 'styled-components'
import Modal from '@components/Objects/StyledElements/Modal/Modal'
const SUPPORTED_FILES = constructAcceptValue(['webm', 'mp4']) const SUPPORTED_FILES = constructAcceptValue(['webm', 'mp4'])
@ -189,6 +190,7 @@ function VideoBlockComponent(props: ExtendedNodeViewProps) {
const [uploadProgress, setUploadProgress] = React.useState(0) const [uploadProgress, setUploadProgress] = React.useState(0)
const [blockObject, setBlockObject] = React.useState<VideoBlockObject | null>(initialBlockObject) const [blockObject, setBlockObject] = React.useState<VideoBlockObject | null>(initialBlockObject)
const [selectedSize, setSelectedSize] = React.useState<VideoSize>(initialBlockObject?.size || 'medium') const [selectedSize, setSelectedSize] = React.useState<VideoSize>(initialBlockObject?.size || 'medium')
const [isModalOpen, setIsModalOpen] = React.useState(false)
// Update block object when size changes // Update block object when size changes
React.useEffect(() => { React.useEffect(() => {
@ -323,40 +325,73 @@ function VideoBlockComponent(props: ExtendedNodeViewProps) {
document.body.removeChild(link); document.body.removeChild(link);
}; };
const handleExpand = () => {
setIsModalOpen(true);
};
// If we're in preview mode and have a video, show only the video player // If we're in preview mode and have a video, show only the video player
if (!isEditable && blockObject && videoUrl) { if (!isEditable && blockObject && videoUrl) {
const width = VIDEO_SIZES[blockObject.size].width const width = VIDEO_SIZES[blockObject.size].width
return ( return (
<NodeViewWrapper className="block-video w-full"> <>
<motion.div <NodeViewWrapper className="block-video w-full">
initial={{ opacity: 0 }} <motion.div
animate={{ opacity: 1 }} initial={{ opacity: 0 }}
transition={{ duration: 0.3 }} animate={{ opacity: 1 }}
className="w-full flex justify-center relative" transition={{ duration: 0.3 }}
> className="w-full flex justify-center relative"
<div
style={{
maxWidth: typeof width === 'number' ? width : '100%',
width: '100%'
}}
> >
<div className="relative"> <div
style={{
maxWidth: typeof width === 'number' ? width : '100%',
width: '100%'
}}
>
<div className="relative">
<video
controls
className="w-full aspect-video object-contain rounded-lg shadow-sm"
src={videoUrl}
/>
<div className="absolute top-2 right-2 flex gap-1">
<button
onClick={handleExpand}
className="p-2 bg-black/50 hover:bg-black/70 rounded-full transition-colors"
title="Expand video"
>
<Expand className="w-4 h-4 text-white" />
</button>
<button
onClick={handleDownload}
className="p-2 bg-black/50 hover:bg-black/70 rounded-full transition-colors"
title="Download video"
>
<Download className="w-4 h-4 text-white" />
</button>
</div>
</div>
</div>
</motion.div>
</NodeViewWrapper>
<Modal
isDialogOpen={isModalOpen}
onOpenChange={setIsModalOpen}
dialogTitle="Video Player"
minWidth="lg"
minHeight="lg"
dialogContent={
<div className="w-full">
<video <video
controls controls
className="w-full aspect-video object-contain rounded-lg shadow-sm" autoPlay
className="w-full aspect-video object-contain rounded-lg shadow-lg bg-black"
src={videoUrl} src={videoUrl}
/> />
<button
onClick={handleDownload}
className="absolute top-2 right-2 p-2 bg-black/50 hover:bg-black/70 rounded-full transition-colors"
title="Download video"
>
<Download className="w-4 h-4 text-white" />
</button>
</div> </div>
</div> }
</motion.div> />
</NodeViewWrapper> </>
) )
} }
@ -524,12 +559,48 @@ function VideoBlockComponent(props: ExtendedNodeViewProps) {
)} )}
src={videoUrl} src={videoUrl}
/> />
<div className="absolute top-2 right-2 flex gap-1">
<button
onClick={handleExpand}
className="p-2 bg-black/50 hover:bg-black/70 rounded-full transition-colors"
title="Expand video"
>
<Expand className="w-4 h-4 text-white" />
</button>
<button
onClick={handleDownload}
className="p-2 bg-black/50 hover:bg-black/70 rounded-full transition-colors"
title="Download video"
>
<Download className="w-4 h-4 text-white" />
</button>
</div>
</div> </div>
</div> </div>
</VideoContainer> </VideoContainer>
</motion.div> </motion.div>
)} )}
</VideoWrapper> </VideoWrapper>
{blockObject && videoUrl && (
<Modal
isDialogOpen={isModalOpen}
onOpenChange={setIsModalOpen}
dialogTitle="Video Player"
minWidth="lg"
minHeight="lg"
dialogContent={
<div className="w-full">
<video
controls
autoPlay
className="w-full aspect-video object-contain rounded-lg shadow-lg bg-black"
src={videoUrl}
/>
</div>
}
/>
)}
</motion.div> </motion.div>
</NodeViewWrapper> </NodeViewWrapper>
) )