suna/frontend/src/components/file-renderers/pdf-renderer.tsx

125 lines
3.3 KiB
TypeScript

"use client";
import React, { useState } from "react";
import { cn } from "@/lib/utils";
import { Button } from "@/components/ui/button";
import {
ZoomIn,
ZoomOut,
RotateCw,
Download,
ArrowLeft,
ArrowRight,
Fullscreen
} from "lucide-react";
interface PdfRendererProps {
url: string;
className?: string;
}
export function PdfRenderer({ url, className }: PdfRendererProps) {
// State for zoom and rotation controls
const [zoom, setZoom] = useState(100);
const [rotation, setRotation] = useState(0);
// Handle zoom in/out
const handleZoomIn = () => setZoom(prev => Math.min(prev + 25, 200));
const handleZoomOut = () => setZoom(prev => Math.max(prev - 25, 50));
// Handle rotation
const handleRotate = () => setRotation(prev => (prev + 90) % 360);
// Handle download
const handleDownload = () => {
const link = document.createElement('a');
link.href = url;
link.download = url.split('/').pop() || 'document.pdf';
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
};
// Handle fullscreen
const handleFullscreen = () => {
const iframe = document.querySelector('iframe');
if (iframe) {
if (iframe.requestFullscreen) {
iframe.requestFullscreen();
}
}
};
return (
<div className={cn("flex flex-col w-full h-full", className)}>
{/* Controls */}
<div className="flex items-center justify-between py-2 px-4 bg-muted/30 border-b mb-2 rounded-t-md">
<div className="flex items-center space-x-2">
<Button
variant="ghost"
size="sm"
className="h-8 w-8 p-0"
onClick={handleZoomOut}
title="Zoom out"
>
<ZoomOut className="h-4 w-4" />
</Button>
<span className="text-xs font-medium">{zoom}%</span>
<Button
variant="ghost"
size="sm"
className="h-8 w-8 p-0"
onClick={handleZoomIn}
title="Zoom in"
>
<ZoomIn className="h-4 w-4" />
</Button>
<Button
variant="ghost"
size="sm"
className="h-8 w-8 p-0"
onClick={handleRotate}
title="Rotate"
>
<RotateCw className="h-4 w-4" />
</Button>
</div>
<div className="flex items-center space-x-2">
<Button
variant="ghost"
size="sm"
className="h-8 w-8 p-0"
onClick={handleFullscreen}
title="Fullscreen"
>
<Fullscreen className="h-4 w-4" />
</Button>
<Button
variant="ghost"
size="sm"
className="h-8 w-8 p-0"
onClick={handleDownload}
title="Download"
>
<Download className="h-4 w-4" />
</Button>
</div>
</div>
{/* PDF Viewer */}
<div className="flex-1 overflow-hidden rounded-b-md bg-white">
<iframe
src={url}
className="w-full h-full border-0"
style={{
transform: `scale(${zoom / 100}) rotate(${rotation}deg)`,
transformOrigin: 'center center',
transition: 'transform 0.2s ease'
}}
title="PDF Viewer"
/>
</div>
</div>
);
}