-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathpreview-text-viewer.tsx
More file actions
201 lines (175 loc) · 5.82 KB
/
preview-text-viewer.tsx
File metadata and controls
201 lines (175 loc) · 5.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { useCallback, useEffect, useRef, useState } from "react";
import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react";
import { DocumentPreviewData } from "@/lib/types/document-preview";
import { paginateText, TEXT_PAGE_DEFAULTS } from "@/lib/utils/text-pagination";
import { Button } from "@/components/ui/button";
const CANVAS_WIDTH = 816;
const CANVAS_HEIGHT = 1056;
const CANVAS_PADDING = 48;
const FONT_SIZE = 14;
const LINE_HEIGHT = Math.floor(
(CANVAS_HEIGHT - CANVAS_PADDING * 2) / TEXT_PAGE_DEFAULTS.linesPerPage,
);
const FONT_FAMILY = '"Courier New", Courier, monospace';
function renderPageToCanvas(
canvas: HTMLCanvasElement,
lines: string[],
dpr: number,
) {
const ctx = canvas.getContext("2d");
if (!ctx) return;
canvas.width = CANVAS_WIDTH * dpr;
canvas.height = CANVAS_HEIGHT * dpr;
canvas.style.width = `${CANVAS_WIDTH}px`;
canvas.style.height = `${CANVAS_HEIGHT}px`;
ctx.scale(dpr, dpr);
ctx.fillStyle = "#ffffff";
ctx.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);
ctx.font = `${FONT_SIZE}px ${FONT_FAMILY}`;
ctx.fillStyle = "#1a1a1a";
ctx.textBaseline = "top";
for (let i = 0; i < lines.length; i++) {
ctx.fillText(lines[i], CANVAS_PADDING, CANVAS_PADDING + i * LINE_HEIGHT);
}
}
interface PreviewTextViewerProps {
documentData: DocumentPreviewData;
onClose: () => void;
}
export function PreviewTextViewer({
documentData,
onClose,
}: PreviewTextViewerProps) {
const [currentPage, setCurrentPage] = useState(1);
const [pages, setPages] = useState<string[][] | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const canvasRef = useRef<HTMLCanvasElement>(null);
const { file, documentName } = documentData;
const numPages = pages?.length ?? 0;
useEffect(() => {
if (!file) return;
let cancelled = false;
async function fetchText() {
try {
setLoading(true);
const response = await fetch(file!);
if (!response.ok) throw new Error("Failed to load text file");
const text = await response.text();
if (cancelled) return;
setPages(paginateText(text));
} catch (err) {
if (!cancelled) {
setError(err instanceof Error ? err.message : "Failed to load file");
}
} finally {
if (!cancelled) setLoading(false);
}
}
fetchText();
return () => {
cancelled = true;
};
}, [file]);
useEffect(() => {
if (!pages || !canvasRef.current) return;
const currentPageLines = pages[currentPage - 1];
if (!currentPageLines) return;
const dpr = window.devicePixelRatio || 1;
renderPageToCanvas(canvasRef.current, currentPageLines, dpr);
}, [pages, currentPage]);
const goToNextPage = useCallback(() => {
if (currentPage < numPages) {
setCurrentPage(currentPage + 1);
}
}, [currentPage, numPages]);
const goToPreviousPage = useCallback(() => {
if (currentPage > 1) {
setCurrentPage(currentPage - 1);
}
}, [currentPage]);
useEffect(() => {
const handleKeyDown = (e: KeyboardEvent) => {
switch (e.key) {
case "ArrowLeft":
goToPreviousPage();
break;
case "ArrowRight":
goToNextPage();
break;
case "Escape":
onClose();
break;
}
};
document.addEventListener("keydown", handleKeyDown);
return () => document.removeEventListener("keydown", handleKeyDown);
}, [goToPreviousPage, goToNextPage, onClose]);
if (!file) {
return (
<div className="flex h-full w-full items-center justify-center">
<p className="text-gray-400">Text file not available</p>
</div>
);
}
if (loading) {
return (
<div className="flex h-full w-full items-center justify-center">
<div className="h-8 w-8 animate-spin rounded-full border-2 border-white border-t-transparent" />
</div>
);
}
if (error || !pages) {
return (
<div className="flex h-full w-full items-center justify-center">
<p className="text-gray-400">{error ?? "Failed to load file"}</p>
</div>
);
}
return (
<div className="relative h-full w-full select-none overflow-hidden">
{/* Document Title & Page Counter */}
<div className="absolute left-1/2 top-4 z-50 -translate-x-1/2">
<div className="rounded-lg bg-black/20 px-3 py-2 text-white">
<span className="text-sm font-medium">
{documentName} - Page {currentPage} of {numPages}
</span>
</div>
</div>
{/* Canvas Content */}
<div className="flex h-full w-full items-center justify-center p-4">
<div className="relative max-h-full max-w-full">
<canvas
ref={canvasRef}
className="max-h-[calc(100vh-120px)] max-w-full rounded shadow-lg"
style={{ objectFit: "contain" }}
onContextMenu={(e) => e.preventDefault()}
/>
</div>
</div>
{/* Navigation */}
{numPages > 1 && (
<>
<Button
variant="ghost"
size="icon"
className="absolute left-2 top-1/2 z-50 h-10 w-10 -translate-y-1/2 rounded-full bg-black/20 text-white hover:bg-black/40 hover:text-white disabled:opacity-30"
onClick={goToPreviousPage}
disabled={currentPage <= 1}
>
<ChevronLeftIcon className="h-6 w-6" />
</Button>
<Button
variant="ghost"
size="icon"
className="absolute right-2 top-1/2 z-50 h-10 w-10 -translate-y-1/2 rounded-full bg-black/20 text-white hover:bg-black/40 hover:text-white disabled:opacity-30"
onClick={goToNextPage}
disabled={currentPage >= numPages}
>
<ChevronRightIcon className="h-6 w-6" />
</Button>
</>
)}
</div>
);
}