Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

feat: update image viewer zoom

Marc Seitz9e1b8d5b7305
diff --git a/components/view/view-data.tsx b/components/view/view-data.tsx
index 284bcd34..48eb5d36 100644
--- a/components/view/view-data.tsx
+++ b/components/view/view-data.tsx
@@ -114,7 +114,6 @@ export default function ViewData({
showAccountCreationSlide={showAccountCreationSlide}
enableQuestion={link.enableQuestion}
feedback={link.feedback}
- isVertical={document.versions[0].isVertical}
viewerEmail={viewerEmail}
watermarkConfig={
link.enableWatermark ? (link.watermarkConfig as WatermarkConfig) : null
diff --git a/components/view/viewer/image-viewer.tsx b/components/view/viewer/image-viewer.tsx
index 6c18863f..97e3b623 100644
--- a/components/view/viewer/image-viewer.tsx
+++ b/components/view/viewer/image-viewer.tsx
@@ -4,11 +4,6 @@ import { useEffect, useRef, useState } from "react";
import React from "react";
import { Brand, DataroomBrand } from "@prisma/client";
-import {
- ReactZoomPanPinchContentRef,
- TransformComponent,
- TransformWrapper,
-} from "react-zoom-pan-pinch";
import { toast } from "sonner";
import { WatermarkConfig } from "@/lib/types";
@@ -63,7 +58,6 @@ export default function ImageViewer({
showAccountCreationSlide,
enableQuestion = false,
feedback,
- isVertical = false,
viewerEmail,
isPreview,
watermarkConfig,
@@ -91,7 +85,6 @@ export default function ImageViewer({
id: string;
data: { question: string; type: string };
} | null;
- isVertical?: boolean;
viewerEmail?: string;
isPreview?: boolean;
watermarkConfig?: WatermarkConfig | null;
@@ -110,7 +103,6 @@ export default function ImageViewer({
const startTimeRef = useRef(Date.now());
const visibilityRef = useRef<boolean>(true);
const containerRef = useRef<HTMLDivElement>(null);
- const pinchRefs = useRef<(ReactZoomPanPinchContentRef | null)[]>([]);
const imageRefs = useRef<HTMLImageElement | null>(null);
const [imageDimensions, setImageDimensions] = useState<{
@@ -120,6 +112,36 @@ export default function ImageViewer({
const { isMobile } = useMediaQuery();
+ // Add zoom handlers
+ const handleZoomIn = () => {
+ setScale((prev) => Math.min(prev + 0.25, 3)); // Max zoom 3x
+ };
+
+ const handleZoomOut = () => {
+ setScale((prev) => Math.max(prev - 0.25, 0.5)); // Min zoom 0.5x
+ };
+
+ // Add keyboard shortcuts for zooming
+ useEffect(() => {
+ const handleKeyDown = (e: KeyboardEvent) => {
+ if (e.metaKey || e.ctrlKey) {
+ if (e.key === "=" || e.key === "+") {
+ e.preventDefault();
+ handleZoomIn();
+ } else if (e.key === "-") {
+ e.preventDefault();
+ handleZoomOut();
+ } else if (e.key === "0") {
+ e.preventDefault();
+ setScale(1);
+ }
+ }
+ };
+
+ window.addEventListener("keydown", handleKeyDown);
+ return () => window.removeEventListener("keydown", handleKeyDown);
+ }, [containerRef.current, imageDimensions]);
+
useEffect(() => {
const updateImageDimensions = () => {
let newDimensions: { width: number; height: number } | null = null;
@@ -280,20 +302,21 @@ export default function ImageViewer({
linkId={linkId}
documentId={documentId}
documentName={documentName}
- isDataroom={dataroomId ? true : false}
+ isDataroom={!!dataroomId}
setDocumentData={setDocumentData}
- documentRefs={pinchRefs}
isMobile={isMobile}
isPreview={isPreview}
- hasWatermark={watermarkConfig ? true : false}
+ hasWatermark={!!watermarkConfig}
+ handleZoomIn={handleZoomIn}
+ handleZoomOut={handleZoomOut}
/>
<div
style={{ height: "calc(100dvh - 64px)" }}
- className="relative flex items-center"
+ className="relative flex items-center overflow-hidden"
>
<div
className={cn(
- "relative flex h-full w-full flex-row",
+ "relative h-full w-full",
!isWindowFocused &&
screenshotProtectionEnabled &&
"blur-xl transition-all duration-300",
@@ -301,87 +324,64 @@ export default function ImageViewer({
ref={containerRef}
>
<div
- className="flex w-full flex-row justify-center"
- onContextMenu={handleContextMenu}
+ className={cn(
+ "h-full w-full",
+ scale > 1 && "overflow-auto",
+ isMobile && "touch-zoom-container",
+ )}
>
- <TransformWrapper
- initialScale={scale}
- initialPositionX={0}
- initialPositionY={0}
- disabled={isMobile}
- panning={{
- lockAxisY: false,
- velocityDisabled: true,
- wheelPanning: false,
- }}
- wheel={{ disabled: true }}
- pinch={{ disabled: true }}
- doubleClick={{ disabled: true }}
- onZoom={(ref) => {
- setScale(ref.state.scale);
- }}
- ref={(ref) => {
- pinchRefs.current[0] = ref;
- }}
- customTransform={(x: number, y: number, scale: number) => {
- // Keep the translateY value constant
- if (isVertical) {
- const transform = `translate(${x}px, ${0 * y * -2}px) scale(${scale})`;
- return transform;
- }
- const transform = `translate(${x}px, ${y}px) scale(${scale})`;
- return transform;
+ <div
+ className={cn(
+ "flex min-h-full w-full items-center justify-center",
+ isMobile && "touch-action-pinch-zoom",
+ )}
+ style={{
+ transform: isMobile ? "none" : `scale(${scale})`,
+ transition: "transform 0.2s ease-out",
+ transformOrigin: scale <= 1 ? "center center" : "left top",
+ minWidth: scale > 1 ? `${100 * scale}%` : "100%",
}}
+ onContextMenu={handleContextMenu}
>
- <TransformComponent
- wrapperClass={cn(
- "!h-full",
- isMobile
- ? "!overflow-x-clip !overflow-y-clip"
- : "!overflow-x-visible !overflow-y-clip",
- )}
- contentClass="!h-full"
- >
- <div className="relative my-auto block w-full">
- <img
- className="!pointer-events-auto object-contain"
- style={{
- maxHeight: "calc(100dvh - 64px)",
- }}
- ref={(ref) => {
- imageRefs.current = ref;
- if (ref) {
- ref.onload = () =>
- setImageDimensions({
- width: ref.clientWidth,
- height: ref.clientHeight,
- });
- }
+ <div className="relative my-auto flex w-full justify-center">
+ <img
+ className={cn(
+ "!pointer-events-auto max-h-[calc(100dvh-64px)] object-contain",
+ isMobile && "touch-action-pinch-zoom",
+ )}
+ ref={(ref) => {
+ imageRefs.current = ref;
+ if (ref) {
+ ref.onload = () =>
+ setImageDimensions({
+ width: ref.clientWidth,
+ height: ref.clientHeight,
+ });
+ }
+ }}
+ src={file}
+ alt="Image 1"
+ />
+
+ {/* Add Watermark Component */}
+ {watermarkConfig ? (
+ <SVGWatermark
+ config={watermarkConfig}
+ viewerData={{
+ email: viewerEmail,
+ date: new Date().toLocaleDateString(),
+ time: new Date().toLocaleTimeString(),
+ link: linkName,
+ ipAddress: ipAddress,
}}
- src={file}
- alt="Image 1"
+ documentDimensions={
+ imageDimensions ?? { width: 0, height: 0 }
+ }
+ pageIndex={0}
/>
-
- {/* Add Watermark Component */}
- {watermarkConfig ? (
- <SVGWatermark
- config={watermarkConfig}
- viewerData={{
- email: viewerEmail,
- date: new Date().toLocaleDateString(),
- time: new Date().toLocaleTimeString(),
- link: linkName,
- ipAddress: ipAddress,
- }}
- documentDimensions={
- imageDimensions ?? { width: 0, height: 0 }
- }
- pageIndex={0}
- />
- ) : null}
- </div>
- </TransformComponent>
- </TransformWrapper>
+ ) : null}
+ </div>
+ </div>
</div>
</div>