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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
|
"use client";
import * as React from "react";
import { WebViewerInstance } from "@pdftron/webviewer";
import {
Dialog,
DialogTrigger,
DialogContent,
DialogHeader,
DialogTitle,
DialogDescription,
DialogFooter,
} from "@/components/ui/dialog";
import { Building2, FileIcon, Loader2 } from "lucide-react";
import { Button } from "@/components/ui/button";
import fs from "fs";
// 인터페이스
interface Attachment {
id: number;
fileName: string;
filePath: string;
fileType?: string;
}
interface Version {
id: number;
stage: string;
revision: string;
uploaderType: string;
uploaderName: string | null;
comment: string | null;
status: string | null;
planDate: string | null;
actualDate: string | null;
approvedDate: string | null;
DocumentSubmitDate: Date;
attachments: Attachment[];
selected: boolean;
}
type ViewDocumentDialogProps = {
versions: Version[];
};
export function ViewDocumentDialog({ versions }: ViewDocumentDialogProps) {
const [open, setOpen] = React.useState(false);
return (
<>
<Button
size="sm"
className="border-blue-200"
variant="outline"
onClick={() => setOpen((prev) => !prev)}
>
문서 보기
</Button>
{open && (
<DocumentViewer open={open} setOpen={setOpen} versions={versions} />
)}
</>
);
}
const DocumentViewer: React.FC<{
open: boolean;
setOpen: React.Dispatch<React.SetStateAction<boolean>>;
versions: Version[];
}> = ({ open, setOpen, versions }) => {
const [instance, setInstance] = React.useState<null | WebViewerInstance>(
null
);
const [viwerLoading, setViewerLoading] = React.useState<boolean>(true);
const [fileSetLoading, setFileSetLoading] = React.useState<boolean>(true);
const viewer = React.useRef<HTMLDivElement>(null);
const initialized = React.useRef(false);
const isCancelled = React.useRef(false); // 초기화 중단용 flag
const cleanupHtmlStyle = () => {
const htmlElement = document.documentElement;
// 기존 style 속성 가져오기
const originalStyle = htmlElement.getAttribute("style") || "";
// "color-scheme: light" 또는 "color-scheme: dark" 찾기
const colorSchemeStyle = originalStyle
.split(";")
.map((s) => s.trim())
.find((s) => s.startsWith("color-scheme:"));
// 새로운 스타일 적용 (color-scheme만 유지)
if (colorSchemeStyle) {
htmlElement.setAttribute("style", colorSchemeStyle + ";");
} else {
htmlElement.removeAttribute("style"); // color-scheme도 없으면 style 속성 자체 삭제
}
console.log("html style 삭제");
};
React.useEffect(() => {
if (open && !initialized.current) {
initialized.current = true;
isCancelled.current = false; // 다시 열릴 때는 false로 리셋
requestAnimationFrame(() => {
if (viewer.current) {
import("@pdftron/webviewer").then(({ default: WebViewer }) => {
console.log(isCancelled.current);
if (isCancelled.current) {
console.log("📛 WebViewer 초기화 취소됨 (Dialog 닫힘)");
return;
}
WebViewer(
{
path: "/pdftronWeb",
licenseKey: process.env.NEXT_PUBLIC_PDFTRON_WEBVIEW_KEY,
fullAPI: true,
css: "/globals.css",
},
viewer.current as HTMLDivElement
).then(async (instance: WebViewerInstance) => {
setInstance(instance);
instance.UI.enableFeatures([instance.UI.Feature.MultiTab]);
instance.UI.disableElements([
"addTabButton",
"multiTabsEmptyPage",
]);
setViewerLoading(false);
});
});
}
});
}
return () => {
// cleanup 시에는 중단 flag 세움
if (instance) {
instance.UI.dispose();
}
setTimeout(() => cleanupHtmlStyle(), 500);
};
}, [open]);
React.useEffect(() => {
const loadDocument = async () => {
if (instance && versions.length > 0) {
const { UI } = instance;
const optionsArray: any[] = [];
versions.forEach((c) => {
const { attachments } = c;
attachments.forEach((c2) => {
const { fileName, filePath, fileType } = c2;
const fileTypeCur = fileType ?? "";
const options = {
filename: fileName,
...(fileTypeCur.includes("xlsx") && {
officeOptions: {
formatOptions: {
applyPageBreaksToSheet: true,
},
},
}),
};
optionsArray.push({
filePath,
options,
});
});
});
const tabIds = [];
for (const option of optionsArray) {
const { filePath, options } = option;
const response = await fetch(filePath);
const blob = await response.blob();
const tab = await UI.TabManager.addTab(blob, options);
tabIds.push(tab); // 탭 ID 저장
}
if (tabIds.length > 0) {
await UI.TabManager.setActiveTab(tabIds[0]);
}
setFileSetLoading(false);
}
};
loadDocument();
}, [instance, versions]);
return (
<Dialog
open={open}
onOpenChange={async (val) => {
console.log({ val, fileSetLoading });
if (!val && fileSetLoading) {
return;
}
if (instance) {
try {
await instance.UI.dispose();
setInstance(null); // 상태도 초기화
} catch (e) {
console.warn("dispose error", e);
}
}
// cleanupHtmlStyle()
setViewerLoading(false);
setOpen((prev) => !prev);
await setTimeout(() => cleanupHtmlStyle(), 1000);
}}
>
<DialogContent className="w-[70vw] h-[90vh]" style={{ maxWidth: "none" }}>
<DialogHeader className="h-[38px]">
<DialogTitle>문서 미리보기</DialogTitle>
<DialogDescription>첨부파일 미리보기</DialogDescription>
</DialogHeader>
<div
ref={viewer}
style={{ height: "calc(90vh - 20px - 38px - 1rem - 48px)" }}
>
{viwerLoading && (
<div className="flex flex-col items-center justify-center py-12">
<Loader2 className="h-8 w-8 text-blue-500 animate-spin mb-4" />
<p className="text-sm text-muted-foreground">
문서 뷰어 로딩 중...
</p>
</div>
)}
</div>
</DialogContent>
</Dialog>
);
};
|