|
2 | 2 |
|
3 | 3 | import { memo, useCallback, useEffect, useRef, useState } from 'react' |
4 | 4 | import { createLogger } from '@sim/logger' |
| 5 | +import { ZoomIn, ZoomOut } from 'lucide-react' |
5 | 6 | import { Skeleton } from '@/components/emcn' |
6 | 7 | import { cn } from '@/lib/core/utils/cn' |
7 | 8 | import type { WorkspaceFileRecord } from '@/lib/uploads/contexts/workspace' |
@@ -432,17 +433,120 @@ const IframePreview = memo(function IframePreview({ file }: { file: WorkspaceFil |
432 | 433 | ) |
433 | 434 | }) |
434 | 435 |
|
| 436 | +const ZOOM_MIN = 0.25 |
| 437 | +const ZOOM_MAX = 4 |
| 438 | +const ZOOM_WHEEL_SENSITIVITY = 0.005 |
| 439 | +const ZOOM_BUTTON_FACTOR = 1.2 |
| 440 | + |
| 441 | +const clampZoom = (z: number) => Math.min(Math.max(z, ZOOM_MIN), ZOOM_MAX) |
| 442 | + |
435 | 443 | const ImagePreview = memo(function ImagePreview({ file }: { file: WorkspaceFileRecord }) { |
436 | 444 | const serveUrl = `/api/files/serve/${encodeURIComponent(file.key)}?context=workspace` |
| 445 | + const [zoom, setZoom] = useState(1) |
| 446 | + const [offset, setOffset] = useState({ x: 0, y: 0 }) |
| 447 | + const isDragging = useRef(false) |
| 448 | + const dragStart = useRef({ x: 0, y: 0 }) |
| 449 | + const offsetAtDragStart = useRef({ x: 0, y: 0 }) |
| 450 | + const offsetRef = useRef(offset) |
| 451 | + offsetRef.current = offset |
| 452 | + |
| 453 | + const containerRef = useRef<HTMLDivElement>(null) |
| 454 | + |
| 455 | + const zoomIn = useCallback(() => setZoom((z) => clampZoom(z * ZOOM_BUTTON_FACTOR)), []) |
| 456 | + const zoomOut = useCallback(() => setZoom((z) => clampZoom(z / ZOOM_BUTTON_FACTOR)), []) |
| 457 | + |
| 458 | + useEffect(() => { |
| 459 | + const el = containerRef.current |
| 460 | + if (!el) return |
| 461 | + const onWheel = (e: WheelEvent) => { |
| 462 | + e.preventDefault() |
| 463 | + if (e.ctrlKey || e.metaKey) { |
| 464 | + setZoom((z) => clampZoom(z * Math.exp(-e.deltaY * ZOOM_WHEEL_SENSITIVITY))) |
| 465 | + } else { |
| 466 | + setOffset((o) => ({ x: o.x - e.deltaX, y: o.y - e.deltaY })) |
| 467 | + } |
| 468 | + } |
| 469 | + el.addEventListener('wheel', onWheel, { passive: false }) |
| 470 | + return () => el.removeEventListener('wheel', onWheel) |
| 471 | + }, []) |
| 472 | + |
| 473 | + const handleMouseDown = useCallback((e: React.MouseEvent) => { |
| 474 | + if (e.button !== 0) return |
| 475 | + isDragging.current = true |
| 476 | + dragStart.current = { x: e.clientX, y: e.clientY } |
| 477 | + offsetAtDragStart.current = offsetRef.current |
| 478 | + if (containerRef.current) containerRef.current.style.cursor = 'grabbing' |
| 479 | + e.preventDefault() |
| 480 | + }, []) |
| 481 | + |
| 482 | + const handleMouseMove = useCallback((e: React.MouseEvent) => { |
| 483 | + if (!isDragging.current) return |
| 484 | + setOffset({ |
| 485 | + x: offsetAtDragStart.current.x + (e.clientX - dragStart.current.x), |
| 486 | + y: offsetAtDragStart.current.y + (e.clientY - dragStart.current.y), |
| 487 | + }) |
| 488 | + }, []) |
| 489 | + |
| 490 | + const handleMouseUp = useCallback(() => { |
| 491 | + isDragging.current = false |
| 492 | + if (containerRef.current) containerRef.current.style.cursor = 'grab' |
| 493 | + }, []) |
| 494 | + |
| 495 | + useEffect(() => { |
| 496 | + setZoom(1) |
| 497 | + setOffset({ x: 0, y: 0 }) |
| 498 | + }, [file.key]) |
437 | 499 |
|
438 | 500 | return ( |
439 | | - <div className='flex flex-1 items-center justify-center overflow-auto bg-[var(--surface-1)] p-6'> |
440 | | - <img |
441 | | - src={serveUrl} |
442 | | - alt={file.name} |
443 | | - className='max-h-full max-w-full rounded-md object-contain' |
444 | | - loading='eager' |
445 | | - /> |
| 501 | + <div |
| 502 | + ref={containerRef} |
| 503 | + className='relative flex flex-1 cursor-grab overflow-hidden bg-[var(--surface-1)]' |
| 504 | + onMouseDown={handleMouseDown} |
| 505 | + onMouseMove={handleMouseMove} |
| 506 | + onMouseUp={handleMouseUp} |
| 507 | + onMouseLeave={handleMouseUp} |
| 508 | + > |
| 509 | + <div |
| 510 | + className='pointer-events-none absolute inset-0 flex items-center justify-center' |
| 511 | + style={{ |
| 512 | + transform: `translate(${offset.x}px, ${offset.y}px) scale(${zoom})`, |
| 513 | + transformOrigin: 'center center', |
| 514 | + }} |
| 515 | + > |
| 516 | + <img |
| 517 | + src={serveUrl} |
| 518 | + alt={file.name} |
| 519 | + className='max-h-full max-w-full select-none rounded-md object-contain' |
| 520 | + draggable={false} |
| 521 | + loading='eager' |
| 522 | + /> |
| 523 | + </div> |
| 524 | + <div |
| 525 | + className='absolute right-4 bottom-4 flex items-center gap-1 rounded-md border border-[var(--border)] bg-[var(--surface-2)] px-2 py-1 shadow-sm' |
| 526 | + onMouseDown={(e) => e.stopPropagation()} |
| 527 | + > |
| 528 | + <button |
| 529 | + type='button' |
| 530 | + onClick={zoomOut} |
| 531 | + disabled={zoom <= ZOOM_MIN} |
| 532 | + className='flex h-6 w-6 items-center justify-center rounded text-[var(--text-secondary)] transition-colors hover:bg-[var(--surface-3)] hover:text-[var(--text-primary)] disabled:cursor-not-allowed disabled:opacity-40' |
| 533 | + aria-label='Zoom out' |
| 534 | + > |
| 535 | + <ZoomOut className='h-3.5 w-3.5' /> |
| 536 | + </button> |
| 537 | + <span className='min-w-[3rem] text-center text-[11px] text-[var(--text-secondary)]'> |
| 538 | + {Math.round(zoom * 100)}% |
| 539 | + </span> |
| 540 | + <button |
| 541 | + type='button' |
| 542 | + onClick={zoomIn} |
| 543 | + disabled={zoom >= ZOOM_MAX} |
| 544 | + className='flex h-6 w-6 items-center justify-center rounded text-[var(--text-secondary)] transition-colors hover:bg-[var(--surface-3)] hover:text-[var(--text-primary)] disabled:cursor-not-allowed disabled:opacity-40' |
| 545 | + aria-label='Zoom in' |
| 546 | + > |
| 547 | + <ZoomIn className='h-3.5 w-3.5' /> |
| 548 | + </button> |
| 549 | + </div> |
446 | 550 | </div> |
447 | 551 | ) |
448 | 552 | }) |
|
0 commit comments