Alleinzellgaenger commited on
Commit
aa7e9a7
·
1 Parent(s): 045b303

Add zoom button

Browse files
frontend/src/components/DocumentViewer.jsx CHANGED
@@ -71,18 +71,57 @@ const DocumentViewer = ({ selectedFile, documentData, onPageChange, preloadedHig
71
  scrollToChunk(0);
72
  };
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  const zoomIn = () => {
75
  if (zoom < 4) {
76
- setZoom(prev => prev + 0.1);
77
  }
78
  };
79
 
80
  const zoomOut = () => {
81
  if (zoom > 0.2) {
82
- setZoom(prev => prev - 0.1);
83
  }
84
  };
85
 
 
 
 
 
86
  // Call onDocumentReady only once when utils become available
87
  const callOnDocumentReady = () => {
88
  if (onDocumentReady && !documentReadyCalledRef.current && highlighterUtilsRef.current) {
@@ -208,9 +247,14 @@ const DocumentViewer = ({ selectedFile, documentData, onPageChange, preloadedHig
208
  >
209
  -
210
  </button>
211
- <span className="text-sm text-gray-600 min-w-12 text-center">
 
 
 
 
 
212
  {(zoom * 100).toFixed(0)}%
213
- </span>
214
  <button
215
  title="Zoom in"
216
  onClick={zoomIn}
 
71
  scrollToChunk(0);
72
  };
73
 
74
+ const zoomWithCenter = (zoomDelta) => {
75
+ const container = document.querySelector('.PdfHighlighter');
76
+ if (!container) {
77
+ setZoom(prev => prev + zoomDelta);
78
+ return;
79
+ }
80
+
81
+ const scrollLeft = container.scrollLeft;
82
+ const scrollTop = container.scrollTop;
83
+ const containerWidth = container.clientWidth;
84
+ const containerHeight = container.clientHeight;
85
+
86
+ // Calculate center point before zoom
87
+ const centerX = scrollLeft + containerWidth / 2;
88
+ const centerY = scrollTop + containerHeight / 2;
89
+
90
+ setZoom(prev => {
91
+ const newZoom = prev + zoomDelta;
92
+ const zoomRatio = newZoom / prev;
93
+
94
+ // Use requestAnimationFrame for smoother transition
95
+ requestAnimationFrame(() => {
96
+ const newScrollLeft = centerX * zoomRatio - containerWidth / 2;
97
+ const newScrollTop = centerY * zoomRatio - containerHeight / 2;
98
+ container.scrollTo({
99
+ left: newScrollLeft,
100
+ top: newScrollTop,
101
+ behavior: 'auto'
102
+ });
103
+ });
104
+
105
+ return newZoom;
106
+ });
107
+ };
108
+
109
  const zoomIn = () => {
110
  if (zoom < 4) {
111
+ zoomWithCenter(0.1);
112
  }
113
  };
114
 
115
  const zoomOut = () => {
116
  if (zoom > 0.2) {
117
+ zoomWithCenter(-0.1);
118
  }
119
  };
120
 
121
+ const resetZoom = () => {
122
+ setZoom(1);
123
+ };
124
+
125
  // Call onDocumentReady only once when utils become available
126
  const callOnDocumentReady = () => {
127
  if (onDocumentReady && !documentReadyCalledRef.current && highlighterUtilsRef.current) {
 
247
  >
248
  -
249
  </button>
250
+ <button
251
+ title="Reset zoom"
252
+ onClick={resetZoom}
253
+ className="px-2 py-1 border rounded hover:bg-gray-100 text-sm"
254
+ disabled={zoom === 1}
255
+ >
256
  {(zoom * 100).toFixed(0)}%
257
+ </button>
258
  <button
259
  title="Zoom in"
260
  onClick={zoomIn}