@files-preview-app/preview-file 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +296 -0
- package/THIRD_PARTY_LICENSES.md +15 -0
- package/dist/angular.cjs +1908 -0
- package/dist/angular.cjs.map +1 -0
- package/dist/angular.d.cts +25 -0
- package/dist/angular.d.ts +25 -0
- package/dist/angular.js +1879 -0
- package/dist/angular.js.map +1 -0
- package/dist/index.cjs +1851 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +25 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.js +1801 -0
- package/dist/index.js.map +1 -0
- package/dist/react.cjs +1837 -0
- package/dist/react.cjs.map +1 -0
- package/dist/react.d.cts +40 -0
- package/dist/react.d.ts +40 -0
- package/dist/react.js +1807 -0
- package/dist/react.js.map +1 -0
- package/dist/styles.css +190 -0
- package/dist/vue.cjs +1832 -0
- package/dist/vue.cjs.map +1 -0
- package/dist/vue.d.cts +44 -0
- package/dist/vue.d.ts +44 -0
- package/dist/vue.js +1802 -0
- package/dist/vue.js.map +1 -0
- package/package.json +114 -0
package/dist/react.js
ADDED
|
@@ -0,0 +1,1807 @@
|
|
|
1
|
+
import { memo, forwardRef, useRef, useImperativeHandle, useEffect } from 'react';
|
|
2
|
+
import DOMPurify from 'dompurify';
|
|
3
|
+
import * as docx from 'docx-preview';
|
|
4
|
+
import ExcelJS from 'exceljs';
|
|
5
|
+
import hljs from 'highlight.js';
|
|
6
|
+
import { jsx } from 'react/jsx-runtime';
|
|
7
|
+
|
|
8
|
+
// src/react.tsx
|
|
9
|
+
var EventEmitter = class {
|
|
10
|
+
listeners = /* @__PURE__ */ new Map();
|
|
11
|
+
/**
|
|
12
|
+
* Subscribe to an event.
|
|
13
|
+
* @returns An unsubscribe function.
|
|
14
|
+
*/
|
|
15
|
+
on(event, handler) {
|
|
16
|
+
if (!this.listeners.has(event)) {
|
|
17
|
+
this.listeners.set(event, /* @__PURE__ */ new Set());
|
|
18
|
+
}
|
|
19
|
+
this.listeners.get(event).add(handler);
|
|
20
|
+
return () => this.off(event, handler);
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Unsubscribe from an event.
|
|
24
|
+
*/
|
|
25
|
+
off(event, handler) {
|
|
26
|
+
const handlers = this.listeners.get(event);
|
|
27
|
+
if (handlers) {
|
|
28
|
+
handlers.delete(handler);
|
|
29
|
+
if (handlers.size === 0) {
|
|
30
|
+
this.listeners.delete(event);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Emit an event with optional payload.
|
|
36
|
+
*/
|
|
37
|
+
emit(event, data) {
|
|
38
|
+
const handlers = this.listeners.get(event);
|
|
39
|
+
if (handlers) {
|
|
40
|
+
for (const handler of handlers) {
|
|
41
|
+
try {
|
|
42
|
+
handler(data);
|
|
43
|
+
} catch (error) {
|
|
44
|
+
console.error(`[FilePreview] Error in '${event}' handler:`, error);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Remove all listeners, optionally for a specific event.
|
|
51
|
+
*/
|
|
52
|
+
removeAll(event) {
|
|
53
|
+
if (event) {
|
|
54
|
+
this.listeners.delete(event);
|
|
55
|
+
} else {
|
|
56
|
+
this.listeners.clear();
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Get the count of listeners for a specific event.
|
|
61
|
+
*/
|
|
62
|
+
listenerCount(event) {
|
|
63
|
+
return this.listeners.get(event)?.size ?? 0;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
var MAGIC_NUMBERS = [
|
|
67
|
+
{ bytes: [37, 80, 68, 70], mime: "application/pdf" },
|
|
68
|
+
// %PDF
|
|
69
|
+
{ bytes: [80, 75, 3, 4], mime: "application/zip" },
|
|
70
|
+
// PK.. (ZIP/DOCX/XLSX/PPTX)
|
|
71
|
+
{ bytes: [137, 80, 78, 71, 13, 10, 26, 10], mime: "image/png" },
|
|
72
|
+
// PNG
|
|
73
|
+
{ bytes: [255, 216, 255], mime: "image/jpeg" },
|
|
74
|
+
// JPEG
|
|
75
|
+
{ bytes: [71, 73, 70, 56], mime: "image/gif" },
|
|
76
|
+
// GIF87a/GIF89a
|
|
77
|
+
{ bytes: [66, 77], mime: "image/bmp" },
|
|
78
|
+
// BMP
|
|
79
|
+
{ bytes: [0, 0, 1, 0], mime: "image/x-icon" },
|
|
80
|
+
// ICO
|
|
81
|
+
{ bytes: [73, 73, 42, 0], mime: "image/tiff" },
|
|
82
|
+
// TIFF (LE)
|
|
83
|
+
{ bytes: [77, 77, 0, 42], mime: "image/tiff" },
|
|
84
|
+
// TIFF (BE)
|
|
85
|
+
{ bytes: [26, 69, 223, 163], mime: "video/webm" },
|
|
86
|
+
// WebM/MKV
|
|
87
|
+
{ bytes: [102, 116, 121, 112], mime: "video/mp4", offset: 4 },
|
|
88
|
+
// MP4 (ftyp)
|
|
89
|
+
{ bytes: [73, 68, 51], mime: "audio/mpeg" },
|
|
90
|
+
// MP3 (ID3)
|
|
91
|
+
{ bytes: [255, 251], mime: "audio/mpeg" },
|
|
92
|
+
// MP3 (sync)
|
|
93
|
+
{ bytes: [255, 243], mime: "audio/mpeg" },
|
|
94
|
+
// MP3 (sync)
|
|
95
|
+
{ bytes: [79, 103, 103, 83], mime: "audio/ogg" },
|
|
96
|
+
// OGG
|
|
97
|
+
{ bytes: [82, 73, 70, 70], mime: "audio/wav" },
|
|
98
|
+
// WAV/RIFF (also WebP)
|
|
99
|
+
{ bytes: [102, 76, 97, 67], mime: "audio/flac" },
|
|
100
|
+
// FLAC
|
|
101
|
+
{ bytes: [123, 92, 114, 116, 102], mime: "text/rtf" }
|
|
102
|
+
// RTF
|
|
103
|
+
];
|
|
104
|
+
var EXTENSION_MIME_MAP = {
|
|
105
|
+
// Documents
|
|
106
|
+
".pdf": "application/pdf",
|
|
107
|
+
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
|
108
|
+
".xlsx": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
109
|
+
".xls": "application/vnd.ms-excel",
|
|
110
|
+
".pptx": "application/vnd.openxmlformats-officedocument.presentationml.presentation",
|
|
111
|
+
".csv": "text/csv",
|
|
112
|
+
".tsv": "text/tab-separated-values",
|
|
113
|
+
// Images
|
|
114
|
+
".png": "image/png",
|
|
115
|
+
".jpg": "image/jpeg",
|
|
116
|
+
".jpeg": "image/jpeg",
|
|
117
|
+
".gif": "image/gif",
|
|
118
|
+
".webp": "image/webp",
|
|
119
|
+
".bmp": "image/bmp",
|
|
120
|
+
".svg": "image/svg+xml",
|
|
121
|
+
".ico": "image/x-icon",
|
|
122
|
+
".tiff": "image/tiff",
|
|
123
|
+
".tif": "image/tiff",
|
|
124
|
+
// Media
|
|
125
|
+
".mp4": "video/mp4",
|
|
126
|
+
".webm": "video/webm",
|
|
127
|
+
".ogg": "audio/ogg",
|
|
128
|
+
".ogv": "video/ogg",
|
|
129
|
+
".mov": "video/quicktime",
|
|
130
|
+
".mp3": "audio/mpeg",
|
|
131
|
+
".wav": "audio/wav",
|
|
132
|
+
".aac": "audio/aac",
|
|
133
|
+
".flac": "audio/flac",
|
|
134
|
+
// Code/Text
|
|
135
|
+
".txt": "text/plain",
|
|
136
|
+
".log": "text/plain",
|
|
137
|
+
".json": "application/json",
|
|
138
|
+
".xml": "application/xml",
|
|
139
|
+
".html": "text/html",
|
|
140
|
+
".htm": "text/html",
|
|
141
|
+
".css": "text/css",
|
|
142
|
+
".js": "text/javascript",
|
|
143
|
+
".ts": "text/typescript",
|
|
144
|
+
".jsx": "text/jsx",
|
|
145
|
+
".tsx": "text/tsx",
|
|
146
|
+
".py": "text/x-python",
|
|
147
|
+
".java": "text/x-java",
|
|
148
|
+
".c": "text/x-c",
|
|
149
|
+
".cpp": "text/x-c++",
|
|
150
|
+
".h": "text/x-c",
|
|
151
|
+
".go": "text/x-go",
|
|
152
|
+
".rs": "text/x-rust",
|
|
153
|
+
".rb": "text/x-ruby",
|
|
154
|
+
".php": "text/x-php",
|
|
155
|
+
".sh": "text/x-shellscript",
|
|
156
|
+
".bash": "text/x-shellscript",
|
|
157
|
+
".sql": "text/x-sql",
|
|
158
|
+
".yaml": "text/yaml",
|
|
159
|
+
".yml": "text/yaml",
|
|
160
|
+
".toml": "text/toml",
|
|
161
|
+
".ini": "text/ini",
|
|
162
|
+
".conf": "text/plain",
|
|
163
|
+
".env": "text/plain",
|
|
164
|
+
".md": "text/markdown",
|
|
165
|
+
".scss": "text/x-scss",
|
|
166
|
+
".less": "text/x-less",
|
|
167
|
+
".vue": "text/x-vue",
|
|
168
|
+
".svelte": "text/x-svelte",
|
|
169
|
+
".dart": "text/x-dart",
|
|
170
|
+
".kt": "text/x-kotlin",
|
|
171
|
+
".swift": "text/x-swift",
|
|
172
|
+
".r": "text/x-r",
|
|
173
|
+
".scala": "text/x-scala",
|
|
174
|
+
".lua": "text/x-lua",
|
|
175
|
+
".perl": "text/x-perl",
|
|
176
|
+
".pl": "text/x-perl"
|
|
177
|
+
};
|
|
178
|
+
function detectMagicBytes(buffer) {
|
|
179
|
+
const bytes = new Uint8Array(buffer.slice(0, 16));
|
|
180
|
+
for (const sig of MAGIC_NUMBERS) {
|
|
181
|
+
const offset = sig.offset ?? 0;
|
|
182
|
+
let match = true;
|
|
183
|
+
for (let i = 0; i < sig.bytes.length; i++) {
|
|
184
|
+
const actual = bytes[offset + i];
|
|
185
|
+
const expected = sig.bytes[i];
|
|
186
|
+
const mask = sig.mask?.[i] ?? 255;
|
|
187
|
+
if ((actual & mask) !== expected) {
|
|
188
|
+
match = false;
|
|
189
|
+
break;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
if (match) {
|
|
193
|
+
if (sig.mime === "audio/wav" && bytes.length >= 12) {
|
|
194
|
+
const format = String.fromCharCode(bytes[8], bytes[9], bytes[10], bytes[11]);
|
|
195
|
+
if (format === "WEBP") return "image/webp";
|
|
196
|
+
if (format === "AVI ") return "video/x-msvideo";
|
|
197
|
+
return "audio/wav";
|
|
198
|
+
}
|
|
199
|
+
return sig.mime;
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return null;
|
|
203
|
+
}
|
|
204
|
+
function detectOoxmlType(buffer) {
|
|
205
|
+
const text = new TextDecoder("ascii", { fatal: false }).decode(
|
|
206
|
+
new Uint8Array(buffer.slice(0, 4e3))
|
|
207
|
+
);
|
|
208
|
+
if (text.includes("word/")) {
|
|
209
|
+
return "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
|
|
210
|
+
}
|
|
211
|
+
if (text.includes("xl/")) {
|
|
212
|
+
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
|
213
|
+
}
|
|
214
|
+
if (text.includes("ppt/")) {
|
|
215
|
+
return "application/vnd.openxmlformats-officedocument.presentationml.presentation";
|
|
216
|
+
}
|
|
217
|
+
return "application/zip";
|
|
218
|
+
}
|
|
219
|
+
function extractExtension(nameOrUrl) {
|
|
220
|
+
try {
|
|
221
|
+
const url = new URL(nameOrUrl);
|
|
222
|
+
const pathname = url.pathname;
|
|
223
|
+
const dotIndex = pathname.lastIndexOf(".");
|
|
224
|
+
if (dotIndex !== -1) {
|
|
225
|
+
return pathname.slice(dotIndex).toLowerCase().split("?")[0];
|
|
226
|
+
}
|
|
227
|
+
} catch {
|
|
228
|
+
const dotIndex = nameOrUrl.lastIndexOf(".");
|
|
229
|
+
if (dotIndex !== -1) {
|
|
230
|
+
return nameOrUrl.slice(dotIndex).toLowerCase();
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
return void 0;
|
|
234
|
+
}
|
|
235
|
+
function mimeFromExtension(extension) {
|
|
236
|
+
return EXTENSION_MIME_MAP[extension.toLowerCase()];
|
|
237
|
+
}
|
|
238
|
+
async function sourceToArrayBuffer(source, signal) {
|
|
239
|
+
let buffer;
|
|
240
|
+
const metadata = {};
|
|
241
|
+
if (typeof source === "string") {
|
|
242
|
+
metadata.name = source.split("/").pop()?.split("?")[0];
|
|
243
|
+
metadata.extension = extractExtension(source);
|
|
244
|
+
const response = await fetch(source, { signal });
|
|
245
|
+
if (!response.ok) {
|
|
246
|
+
throw new Error(`Failed to fetch file: ${response.status} ${response.statusText}`);
|
|
247
|
+
}
|
|
248
|
+
metadata.mimeType = response.headers.get("content-type") ?? void 0;
|
|
249
|
+
buffer = await response.arrayBuffer();
|
|
250
|
+
} else if (source instanceof File) {
|
|
251
|
+
metadata.name = source.name;
|
|
252
|
+
metadata.size = source.size;
|
|
253
|
+
metadata.mimeType = source.type || void 0;
|
|
254
|
+
metadata.extension = extractExtension(source.name);
|
|
255
|
+
buffer = await source.arrayBuffer();
|
|
256
|
+
} else if (source instanceof Blob) {
|
|
257
|
+
metadata.size = source.size;
|
|
258
|
+
metadata.mimeType = source.type || void 0;
|
|
259
|
+
buffer = await source.arrayBuffer();
|
|
260
|
+
} else if (source instanceof ArrayBuffer) {
|
|
261
|
+
buffer = source;
|
|
262
|
+
} else if (source instanceof Uint8Array) {
|
|
263
|
+
buffer = source.buffer.slice(
|
|
264
|
+
source.byteOffset,
|
|
265
|
+
source.byteOffset + source.byteLength
|
|
266
|
+
);
|
|
267
|
+
} else {
|
|
268
|
+
throw new Error("Unsupported file source type");
|
|
269
|
+
}
|
|
270
|
+
metadata.size = metadata.size ?? buffer.byteLength;
|
|
271
|
+
const magicMime = detectMagicBytes(buffer);
|
|
272
|
+
if (magicMime) {
|
|
273
|
+
if (magicMime === "application/zip") {
|
|
274
|
+
const ooxmlMime = detectOoxmlType(buffer);
|
|
275
|
+
if (ooxmlMime && ooxmlMime !== "application/zip") {
|
|
276
|
+
metadata.mimeType = ooxmlMime;
|
|
277
|
+
if (ooxmlMime.includes("wordprocessing")) metadata.extension = metadata.extension ?? ".docx";
|
|
278
|
+
else if (ooxmlMime.includes("spreadsheet")) metadata.extension = metadata.extension ?? ".xlsx";
|
|
279
|
+
else if (ooxmlMime.includes("presentation")) metadata.extension = metadata.extension ?? ".pptx";
|
|
280
|
+
} else {
|
|
281
|
+
metadata.mimeType = metadata.mimeType ?? magicMime;
|
|
282
|
+
}
|
|
283
|
+
} else {
|
|
284
|
+
metadata.mimeType = magicMime;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
if (!metadata.mimeType && metadata.extension) {
|
|
288
|
+
metadata.mimeType = mimeFromExtension(metadata.extension);
|
|
289
|
+
}
|
|
290
|
+
return { buffer, metadata };
|
|
291
|
+
}
|
|
292
|
+
function sanitizeSVG(svg) {
|
|
293
|
+
return DOMPurify.sanitize(svg, {
|
|
294
|
+
USE_PROFILES: { svg: true, svgFilters: true }
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
function createElement(tag, attrs, ...children) {
|
|
298
|
+
const el = document.createElement(tag);
|
|
299
|
+
if (attrs) {
|
|
300
|
+
for (const [key, value] of Object.entries(attrs)) {
|
|
301
|
+
if (key === "className") {
|
|
302
|
+
el.className = value;
|
|
303
|
+
} else {
|
|
304
|
+
el.setAttribute(key, value);
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
for (const child of children) {
|
|
309
|
+
if (typeof child === "string") {
|
|
310
|
+
el.appendChild(document.createTextNode(child));
|
|
311
|
+
} else {
|
|
312
|
+
el.appendChild(child);
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
return el;
|
|
316
|
+
}
|
|
317
|
+
var ICON_PAGE_PREV = `<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="15 18 9 12 15 6"></polyline></svg>`;
|
|
318
|
+
var ICON_PAGE_NEXT = `<svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="9 18 15 12 9 6"></polyline></svg>`;
|
|
319
|
+
var ToolbarController = class {
|
|
320
|
+
el;
|
|
321
|
+
toolbarEl;
|
|
322
|
+
actions = [];
|
|
323
|
+
constructor(container) {
|
|
324
|
+
this.el = container;
|
|
325
|
+
this.toolbarEl = createElement("div", { className: "fp-toolbar" });
|
|
326
|
+
this.el.appendChild(this.toolbarEl);
|
|
327
|
+
}
|
|
328
|
+
update(actions) {
|
|
329
|
+
this.actions = actions;
|
|
330
|
+
this.render();
|
|
331
|
+
}
|
|
332
|
+
show() {
|
|
333
|
+
this.el.style.display = "block";
|
|
334
|
+
}
|
|
335
|
+
hide() {
|
|
336
|
+
this.el.style.display = "none";
|
|
337
|
+
}
|
|
338
|
+
destroy() {
|
|
339
|
+
this.el.innerHTML = "";
|
|
340
|
+
}
|
|
341
|
+
render() {
|
|
342
|
+
this.toolbarEl.innerHTML = "";
|
|
343
|
+
const groups = {
|
|
344
|
+
navigation: [],
|
|
345
|
+
zoom: [],
|
|
346
|
+
view: [],
|
|
347
|
+
actions: []
|
|
348
|
+
};
|
|
349
|
+
for (const action of this.actions) {
|
|
350
|
+
if (groups[action.group]) {
|
|
351
|
+
groups[action.group].push(action);
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
const order = ["navigation", "zoom", "view", "actions"];
|
|
355
|
+
let isFirstGroup = true;
|
|
356
|
+
for (const group of order) {
|
|
357
|
+
const groupActions = groups[group];
|
|
358
|
+
if (groupActions.length === 0) continue;
|
|
359
|
+
if (!isFirstGroup) {
|
|
360
|
+
this.toolbarEl.appendChild(createElement("div", { className: "fp-toolbar-separator" }));
|
|
361
|
+
}
|
|
362
|
+
isFirstGroup = false;
|
|
363
|
+
const groupEl = createElement("div", { className: "fp-toolbar-group" });
|
|
364
|
+
for (const action of groupActions) {
|
|
365
|
+
if (action.type === "separator") {
|
|
366
|
+
groupEl.appendChild(createElement("div", { className: "fp-toolbar-separator" }));
|
|
367
|
+
} else if (action.type === "page-nav") {
|
|
368
|
+
const prevBtn = this.createButton(
|
|
369
|
+
"prev",
|
|
370
|
+
ICON_PAGE_PREV,
|
|
371
|
+
"Previous Page",
|
|
372
|
+
() => action.execute("prev")
|
|
373
|
+
);
|
|
374
|
+
const nextBtn = this.createButton(
|
|
375
|
+
"next",
|
|
376
|
+
ICON_PAGE_NEXT,
|
|
377
|
+
"Next Page",
|
|
378
|
+
() => action.execute("next")
|
|
379
|
+
);
|
|
380
|
+
const input = createElement("input", {
|
|
381
|
+
className: "fp-toolbar-input",
|
|
382
|
+
type: "number",
|
|
383
|
+
value: (action.value ?? 1).toString(),
|
|
384
|
+
min: "1",
|
|
385
|
+
max: (action.max ?? 1).toString()
|
|
386
|
+
});
|
|
387
|
+
input.addEventListener("change", () => {
|
|
388
|
+
action.execute("go", parseInt(input.value, 10));
|
|
389
|
+
});
|
|
390
|
+
const label = createElement("span", { className: "fp-toolbar-label" }, ` / ${action.max ?? 1}`);
|
|
391
|
+
groupEl.appendChild(prevBtn);
|
|
392
|
+
groupEl.appendChild(input);
|
|
393
|
+
groupEl.appendChild(label);
|
|
394
|
+
groupEl.appendChild(nextBtn);
|
|
395
|
+
} else if (action.type === "range") {
|
|
396
|
+
const input = createElement("input", {
|
|
397
|
+
type: "range",
|
|
398
|
+
min: (action.min ?? 0).toString(),
|
|
399
|
+
max: (action.max ?? 100).toString(),
|
|
400
|
+
step: (action.step ?? 1).toString(),
|
|
401
|
+
value: (action.value ?? 50).toString()
|
|
402
|
+
});
|
|
403
|
+
input.addEventListener("input", () => {
|
|
404
|
+
action.execute(parseFloat(input.value));
|
|
405
|
+
});
|
|
406
|
+
groupEl.appendChild(input);
|
|
407
|
+
} else {
|
|
408
|
+
const btn = this.createButton(
|
|
409
|
+
action.id,
|
|
410
|
+
action.icon,
|
|
411
|
+
action.label,
|
|
412
|
+
() => action.execute()
|
|
413
|
+
);
|
|
414
|
+
if (action.enabled === false) btn.disabled = true;
|
|
415
|
+
if (action.active) btn.classList.add("active");
|
|
416
|
+
groupEl.appendChild(btn);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
this.toolbarEl.appendChild(groupEl);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
createButton(id, iconHtml, title, onClick) {
|
|
423
|
+
const btn = createElement("button", {
|
|
424
|
+
className: "fp-toolbar-btn",
|
|
425
|
+
title,
|
|
426
|
+
type: "button",
|
|
427
|
+
"data-action-id": id
|
|
428
|
+
});
|
|
429
|
+
if (iconHtml.startsWith("<svg")) {
|
|
430
|
+
btn.innerHTML = sanitizeSVG(iconHtml);
|
|
431
|
+
} else {
|
|
432
|
+
btn.textContent = iconHtml;
|
|
433
|
+
}
|
|
434
|
+
btn.addEventListener("click", onClick);
|
|
435
|
+
return btn;
|
|
436
|
+
}
|
|
437
|
+
};
|
|
438
|
+
var ThumbnailPanel = class {
|
|
439
|
+
el;
|
|
440
|
+
panelEl;
|
|
441
|
+
thumbnails = [];
|
|
442
|
+
onSelect;
|
|
443
|
+
activeIndex = 0;
|
|
444
|
+
constructor(container) {
|
|
445
|
+
this.el = container;
|
|
446
|
+
this.panelEl = createElement("div", { className: "fp-thumbnail-panel hidden" });
|
|
447
|
+
this.el.appendChild(this.panelEl);
|
|
448
|
+
}
|
|
449
|
+
update(thumbnails, onSelect) {
|
|
450
|
+
this.thumbnails = thumbnails;
|
|
451
|
+
this.onSelect = onSelect;
|
|
452
|
+
this.render();
|
|
453
|
+
}
|
|
454
|
+
setActive(index) {
|
|
455
|
+
this.activeIndex = index;
|
|
456
|
+
const items = this.panelEl.querySelectorAll(".fp-thumbnail-item");
|
|
457
|
+
items.forEach((item, i) => {
|
|
458
|
+
if (i === index) {
|
|
459
|
+
item.classList.add("active");
|
|
460
|
+
} else {
|
|
461
|
+
item.classList.remove("active");
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
}
|
|
465
|
+
show() {
|
|
466
|
+
this.panelEl.classList.remove("hidden");
|
|
467
|
+
}
|
|
468
|
+
hide() {
|
|
469
|
+
this.panelEl.classList.add("hidden");
|
|
470
|
+
}
|
|
471
|
+
toggle() {
|
|
472
|
+
this.panelEl.classList.toggle("hidden");
|
|
473
|
+
}
|
|
474
|
+
destroy() {
|
|
475
|
+
this.el.innerHTML = "";
|
|
476
|
+
}
|
|
477
|
+
async render() {
|
|
478
|
+
this.panelEl.innerHTML = "";
|
|
479
|
+
for (let i = 0; i < this.thumbnails.length; i++) {
|
|
480
|
+
const thumb = this.thumbnails[i];
|
|
481
|
+
const itemEl = createElement("div", { className: "fp-thumbnail-item" });
|
|
482
|
+
if (i === this.activeIndex) {
|
|
483
|
+
itemEl.classList.add("active");
|
|
484
|
+
}
|
|
485
|
+
itemEl.addEventListener("click", () => {
|
|
486
|
+
this.setActive(i);
|
|
487
|
+
this.onSelect?.(i);
|
|
488
|
+
});
|
|
489
|
+
const canvas = createElement("canvas", { width: "150", height: "200" });
|
|
490
|
+
const label = createElement("span", { className: "fp-thumbnail-label" }, thumb.label);
|
|
491
|
+
itemEl.appendChild(canvas);
|
|
492
|
+
itemEl.appendChild(label);
|
|
493
|
+
this.panelEl.appendChild(itemEl);
|
|
494
|
+
try {
|
|
495
|
+
await thumb.render(canvas);
|
|
496
|
+
} catch (err) {
|
|
497
|
+
console.error(`[FilePreview] Error rendering thumbnail ${i}:`, err);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
var FilePreviewViewer = class {
|
|
503
|
+
plugins = [];
|
|
504
|
+
activeInstance = null;
|
|
505
|
+
abortController = null;
|
|
506
|
+
eventEmitter = new EventEmitter();
|
|
507
|
+
toolbar = null;
|
|
508
|
+
thumbnailPanel = null;
|
|
509
|
+
wrapperEl = null;
|
|
510
|
+
contentEl = null;
|
|
511
|
+
currentBuffer = null;
|
|
512
|
+
currentMetadata = null;
|
|
513
|
+
/**
|
|
514
|
+
* Register a preview plugin.
|
|
515
|
+
*/
|
|
516
|
+
registerPlugin(plugin) {
|
|
517
|
+
this.plugins.push(plugin);
|
|
518
|
+
this.plugins.sort((a, b) => (b.weight ?? 0) - (a.weight ?? 0));
|
|
519
|
+
return this;
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Register multiple plugins at once.
|
|
523
|
+
*/
|
|
524
|
+
registerPlugins(plugins) {
|
|
525
|
+
for (const plugin of plugins) {
|
|
526
|
+
this.registerPlugin(plugin);
|
|
527
|
+
}
|
|
528
|
+
return this;
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Preview a file in the given container element.
|
|
532
|
+
*/
|
|
533
|
+
async preview(container, source, options = {}) {
|
|
534
|
+
this.abort();
|
|
535
|
+
this.abortController = new AbortController();
|
|
536
|
+
const { signal } = this.abortController;
|
|
537
|
+
this.destroyInstance();
|
|
538
|
+
this.setupDOM(container, options);
|
|
539
|
+
this.eventEmitter.emit("loading", { source });
|
|
540
|
+
this.showLoading();
|
|
541
|
+
try {
|
|
542
|
+
const { buffer, metadata } = await sourceToArrayBuffer(source, signal);
|
|
543
|
+
this.currentBuffer = buffer;
|
|
544
|
+
this.currentMetadata = metadata;
|
|
545
|
+
if (signal.aborted) throw new DOMException("Aborted", "AbortError");
|
|
546
|
+
const fileInfo = { metadata, buffer };
|
|
547
|
+
const matchedPlugin = await this.findPlugin(fileInfo);
|
|
548
|
+
if (!matchedPlugin) {
|
|
549
|
+
throw new Error(
|
|
550
|
+
`Unsupported file type: ${metadata.extension ?? metadata.mimeType ?? "unknown"}`
|
|
551
|
+
);
|
|
552
|
+
}
|
|
553
|
+
if (signal.aborted) throw new DOMException("Aborted", "AbortError");
|
|
554
|
+
if (this.contentEl) {
|
|
555
|
+
this.contentEl.innerHTML = "";
|
|
556
|
+
}
|
|
557
|
+
const instance = await matchedPlugin.render({
|
|
558
|
+
container: this.contentEl,
|
|
559
|
+
source,
|
|
560
|
+
metadata,
|
|
561
|
+
buffer,
|
|
562
|
+
options,
|
|
563
|
+
signal,
|
|
564
|
+
emit: (event, payload) => this.eventEmitter.emit(event, payload)
|
|
565
|
+
});
|
|
566
|
+
this.activeInstance = instance;
|
|
567
|
+
if (options.showToolbar !== false && this.toolbar) {
|
|
568
|
+
const actions = matchedPlugin.getToolbarActions(instance);
|
|
569
|
+
this.toolbar.update(actions);
|
|
570
|
+
this.toolbar.show();
|
|
571
|
+
}
|
|
572
|
+
if (instance.getThumbnails && this.thumbnailPanel) {
|
|
573
|
+
const thumbnails = await Promise.resolve(instance.getThumbnails());
|
|
574
|
+
if (thumbnails && thumbnails.length > 0) {
|
|
575
|
+
this.thumbnailPanel.update(thumbnails, (index) => {
|
|
576
|
+
instance.goToPage?.(index + 1);
|
|
577
|
+
});
|
|
578
|
+
if (options.showThumbnails) {
|
|
579
|
+
this.thumbnailPanel.show();
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
this.hideLoading();
|
|
584
|
+
this.eventEmitter.emit("loaded", { metadata, plugin: matchedPlugin.id });
|
|
585
|
+
return instance;
|
|
586
|
+
} catch (error) {
|
|
587
|
+
this.hideLoading();
|
|
588
|
+
if (error instanceof DOMException && error.name === "AbortError") {
|
|
589
|
+
throw error;
|
|
590
|
+
}
|
|
591
|
+
this.showError(error instanceof Error ? error.message : "Failed to preview file");
|
|
592
|
+
this.eventEmitter.emit("error", error);
|
|
593
|
+
throw error;
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
/**
|
|
597
|
+
* Subscribe to viewer events.
|
|
598
|
+
*/
|
|
599
|
+
on(event, handler) {
|
|
600
|
+
return this.eventEmitter.on(event, handler);
|
|
601
|
+
}
|
|
602
|
+
/**
|
|
603
|
+
* Destroy the viewer and clean up all resources.
|
|
604
|
+
*/
|
|
605
|
+
destroy() {
|
|
606
|
+
this.abort();
|
|
607
|
+
this.destroyInstance();
|
|
608
|
+
this.toolbar?.destroy();
|
|
609
|
+
this.thumbnailPanel?.destroy();
|
|
610
|
+
this.eventEmitter.emit("destroy", null);
|
|
611
|
+
this.eventEmitter.removeAll();
|
|
612
|
+
if (this.wrapperEl?.parentNode) {
|
|
613
|
+
this.wrapperEl.parentNode.removeChild(this.wrapperEl);
|
|
614
|
+
}
|
|
615
|
+
this.wrapperEl = null;
|
|
616
|
+
this.contentEl = null;
|
|
617
|
+
this.toolbar = null;
|
|
618
|
+
this.thumbnailPanel = null;
|
|
619
|
+
this.currentBuffer = null;
|
|
620
|
+
this.currentMetadata = null;
|
|
621
|
+
}
|
|
622
|
+
/**
|
|
623
|
+
* Get the currently active preview instance.
|
|
624
|
+
*/
|
|
625
|
+
getInstance() {
|
|
626
|
+
return this.activeInstance;
|
|
627
|
+
}
|
|
628
|
+
// --- Private methods ---
|
|
629
|
+
abort() {
|
|
630
|
+
if (this.abortController) {
|
|
631
|
+
this.abortController.abort();
|
|
632
|
+
this.abortController = null;
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
destroyInstance() {
|
|
636
|
+
if (this.activeInstance) {
|
|
637
|
+
this.activeInstance.destroy();
|
|
638
|
+
this.activeInstance = null;
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
async findPlugin(fileInfo) {
|
|
642
|
+
for (const plugin of this.plugins) {
|
|
643
|
+
const result = plugin.supports(fileInfo);
|
|
644
|
+
const supports = result instanceof Promise ? await result : result;
|
|
645
|
+
if (supports) return plugin;
|
|
646
|
+
}
|
|
647
|
+
return null;
|
|
648
|
+
}
|
|
649
|
+
setupDOM(container, options) {
|
|
650
|
+
if (this.wrapperEl?.parentNode === container) return;
|
|
651
|
+
if (this.wrapperEl?.parentNode) {
|
|
652
|
+
this.wrapperEl.parentNode.removeChild(this.wrapperEl);
|
|
653
|
+
}
|
|
654
|
+
const themeClass = options.theme === "dark" ? "fp-theme-dark" : "";
|
|
655
|
+
const toolbarPos = options.toolbarPosition ?? "top";
|
|
656
|
+
this.wrapperEl = document.createElement("div");
|
|
657
|
+
this.wrapperEl.className = `fp-viewer ${themeClass} ${options.className ?? ""}`.trim();
|
|
658
|
+
const toolbarEl = document.createElement("div");
|
|
659
|
+
toolbarEl.className = "fp-toolbar-container";
|
|
660
|
+
this.contentEl = document.createElement("div");
|
|
661
|
+
this.contentEl.className = "fp-content";
|
|
662
|
+
const thumbnailEl = document.createElement("div");
|
|
663
|
+
thumbnailEl.className = "fp-thumbnail-container";
|
|
664
|
+
const bodyEl = document.createElement("div");
|
|
665
|
+
bodyEl.className = "fp-body";
|
|
666
|
+
bodyEl.appendChild(thumbnailEl);
|
|
667
|
+
bodyEl.appendChild(this.contentEl);
|
|
668
|
+
if (toolbarPos === "top") {
|
|
669
|
+
this.wrapperEl.appendChild(toolbarEl);
|
|
670
|
+
this.wrapperEl.appendChild(bodyEl);
|
|
671
|
+
} else {
|
|
672
|
+
this.wrapperEl.appendChild(bodyEl);
|
|
673
|
+
this.wrapperEl.appendChild(toolbarEl);
|
|
674
|
+
}
|
|
675
|
+
container.innerHTML = "";
|
|
676
|
+
container.appendChild(this.wrapperEl);
|
|
677
|
+
this.toolbar = new ToolbarController(toolbarEl);
|
|
678
|
+
this.thumbnailPanel = new ThumbnailPanel(thumbnailEl);
|
|
679
|
+
}
|
|
680
|
+
showLoading() {
|
|
681
|
+
if (!this.contentEl) return;
|
|
682
|
+
const loader = document.createElement("div");
|
|
683
|
+
loader.className = "fp-loading";
|
|
684
|
+
loader.innerHTML = '<div class="fp-spinner"></div><span>Loading preview...</span>';
|
|
685
|
+
this.contentEl.appendChild(loader);
|
|
686
|
+
}
|
|
687
|
+
hideLoading() {
|
|
688
|
+
if (!this.contentEl) return;
|
|
689
|
+
const loader = this.contentEl.querySelector(".fp-loading");
|
|
690
|
+
if (loader) loader.remove();
|
|
691
|
+
}
|
|
692
|
+
showError(message) {
|
|
693
|
+
if (!this.contentEl) return;
|
|
694
|
+
this.contentEl.innerHTML = "";
|
|
695
|
+
const errorEl = document.createElement("div");
|
|
696
|
+
errorEl.className = "fp-error";
|
|
697
|
+
errorEl.innerHTML = `
|
|
698
|
+
<div class="fp-error-icon">\u26A0\uFE0F</div>
|
|
699
|
+
<div class="fp-error-message">${message}</div>
|
|
700
|
+
`;
|
|
701
|
+
this.contentEl.appendChild(errorEl);
|
|
702
|
+
}
|
|
703
|
+
};
|
|
704
|
+
|
|
705
|
+
// ../plugins/pdf/dist/index.js
|
|
706
|
+
var PdfPlugin = class {
|
|
707
|
+
id = "pdf";
|
|
708
|
+
name = "PDF Preview";
|
|
709
|
+
extensions = [".pdf"];
|
|
710
|
+
mimeTypes = ["application/pdf"];
|
|
711
|
+
weight = 100;
|
|
712
|
+
supports(file) {
|
|
713
|
+
const ext = file.metadata.extension?.toLowerCase();
|
|
714
|
+
const mime = file.metadata.mimeType?.toLowerCase();
|
|
715
|
+
return ext === ".pdf" || mime === "application/pdf";
|
|
716
|
+
}
|
|
717
|
+
getToolbarActions(instance) {
|
|
718
|
+
return [
|
|
719
|
+
{
|
|
720
|
+
id: "zoom-out",
|
|
721
|
+
icon: "zoom-out",
|
|
722
|
+
label: "Zoom Out",
|
|
723
|
+
type: "button",
|
|
724
|
+
group: "zoom",
|
|
725
|
+
execute: () => {
|
|
726
|
+
instance.zoomOut?.();
|
|
727
|
+
}
|
|
728
|
+
},
|
|
729
|
+
{
|
|
730
|
+
id: "zoom-in",
|
|
731
|
+
icon: "zoom-in",
|
|
732
|
+
label: "Zoom In",
|
|
733
|
+
type: "button",
|
|
734
|
+
group: "zoom",
|
|
735
|
+
execute: () => {
|
|
736
|
+
instance.zoomIn?.();
|
|
737
|
+
}
|
|
738
|
+
},
|
|
739
|
+
{
|
|
740
|
+
id: "fit-page",
|
|
741
|
+
icon: "fit-page",
|
|
742
|
+
label: "Fit to Page",
|
|
743
|
+
type: "button",
|
|
744
|
+
group: "zoom",
|
|
745
|
+
execute: () => {
|
|
746
|
+
instance.fitToPage?.();
|
|
747
|
+
}
|
|
748
|
+
},
|
|
749
|
+
{
|
|
750
|
+
id: "rotate-cw",
|
|
751
|
+
icon: "rotate-cw",
|
|
752
|
+
label: "Rotate",
|
|
753
|
+
type: "button",
|
|
754
|
+
group: "view",
|
|
755
|
+
execute: () => {
|
|
756
|
+
instance.rotateCW?.();
|
|
757
|
+
}
|
|
758
|
+
},
|
|
759
|
+
{
|
|
760
|
+
id: "page-nav",
|
|
761
|
+
icon: "page-nav",
|
|
762
|
+
label: "Page Navigation",
|
|
763
|
+
type: "page-nav",
|
|
764
|
+
group: "navigation",
|
|
765
|
+
execute: (page) => {
|
|
766
|
+
if (typeof page === "number") instance.goToPage?.(page);
|
|
767
|
+
}
|
|
768
|
+
},
|
|
769
|
+
{
|
|
770
|
+
id: "download",
|
|
771
|
+
icon: "download",
|
|
772
|
+
label: "Download",
|
|
773
|
+
type: "button",
|
|
774
|
+
group: "actions",
|
|
775
|
+
execute: () => {
|
|
776
|
+
instance.download?.();
|
|
777
|
+
}
|
|
778
|
+
},
|
|
779
|
+
{
|
|
780
|
+
id: "print",
|
|
781
|
+
icon: "print",
|
|
782
|
+
label: "Print",
|
|
783
|
+
type: "button",
|
|
784
|
+
group: "actions",
|
|
785
|
+
execute: () => {
|
|
786
|
+
instance.print?.();
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
];
|
|
790
|
+
}
|
|
791
|
+
async render(ctx) {
|
|
792
|
+
const blob = new Blob([ctx.buffer], { type: "application/pdf" });
|
|
793
|
+
const url = URL.createObjectURL(blob);
|
|
794
|
+
const wrapper = document.createElement("div");
|
|
795
|
+
wrapper.style.width = "100%";
|
|
796
|
+
wrapper.style.height = "100%";
|
|
797
|
+
wrapper.style.overflow = "hidden";
|
|
798
|
+
wrapper.style.display = "flex";
|
|
799
|
+
wrapper.style.justifyContent = "center";
|
|
800
|
+
wrapper.style.alignItems = "center";
|
|
801
|
+
const iframe = document.createElement("iframe");
|
|
802
|
+
iframe.src = url;
|
|
803
|
+
iframe.style.width = "100%";
|
|
804
|
+
iframe.style.height = "100%";
|
|
805
|
+
iframe.style.border = "none";
|
|
806
|
+
wrapper.appendChild(iframe);
|
|
807
|
+
ctx.container.appendChild(wrapper);
|
|
808
|
+
let currentPage = 1;
|
|
809
|
+
let currentZoom = 1;
|
|
810
|
+
let rotation = 0;
|
|
811
|
+
const cleanup = () => {
|
|
812
|
+
URL.revokeObjectURL(url);
|
|
813
|
+
wrapper.remove();
|
|
814
|
+
ctx.container.innerHTML = "";
|
|
815
|
+
};
|
|
816
|
+
ctx.signal.addEventListener("abort", cleanup);
|
|
817
|
+
return {
|
|
818
|
+
destroy: cleanup,
|
|
819
|
+
zoomIn: () => {
|
|
820
|
+
currentZoom += 0.1;
|
|
821
|
+
iframe.style.transform = `scale(${currentZoom}) rotate(${rotation}deg)`;
|
|
822
|
+
},
|
|
823
|
+
zoomOut: () => {
|
|
824
|
+
currentZoom = Math.max(0.2, currentZoom - 0.1);
|
|
825
|
+
iframe.style.transform = `scale(${currentZoom}) rotate(${rotation}deg)`;
|
|
826
|
+
},
|
|
827
|
+
getZoom: () => currentZoom,
|
|
828
|
+
setZoom: (level) => {
|
|
829
|
+
currentZoom = level;
|
|
830
|
+
iframe.style.transform = `scale(${currentZoom}) rotate(${rotation}deg)`;
|
|
831
|
+
},
|
|
832
|
+
fitToPage: () => {
|
|
833
|
+
currentZoom = 1;
|
|
834
|
+
iframe.style.transform = `scale(1) rotate(${rotation}deg)`;
|
|
835
|
+
},
|
|
836
|
+
rotateCW: () => {
|
|
837
|
+
rotation = (rotation + 90) % 360;
|
|
838
|
+
iframe.style.transform = `scale(${currentZoom}) rotate(${rotation}deg)`;
|
|
839
|
+
},
|
|
840
|
+
rotateCCW: () => {
|
|
841
|
+
rotation = (rotation - 90 + 360) % 360;
|
|
842
|
+
iframe.style.transform = `scale(${currentZoom}) rotate(${rotation}deg)`;
|
|
843
|
+
},
|
|
844
|
+
getRotation: () => rotation,
|
|
845
|
+
goToPage: (page) => {
|
|
846
|
+
currentPage = page;
|
|
847
|
+
iframe.src = `${url}#page=${page}`;
|
|
848
|
+
},
|
|
849
|
+
getCurrentPage: () => currentPage,
|
|
850
|
+
download: () => {
|
|
851
|
+
const a = document.createElement("a");
|
|
852
|
+
a.href = url;
|
|
853
|
+
a.download = ctx.metadata.name || "document.pdf";
|
|
854
|
+
a.click();
|
|
855
|
+
},
|
|
856
|
+
print: () => {
|
|
857
|
+
iframe.contentWindow?.print();
|
|
858
|
+
},
|
|
859
|
+
getThumbnails: async () => {
|
|
860
|
+
return [
|
|
861
|
+
{
|
|
862
|
+
index: 1,
|
|
863
|
+
label: "Page 1",
|
|
864
|
+
render: async (canvas) => {
|
|
865
|
+
const context = canvas.getContext("2d");
|
|
866
|
+
if (context) {
|
|
867
|
+
context.fillStyle = "#fff";
|
|
868
|
+
context.fillRect(0, 0, canvas.width, canvas.height);
|
|
869
|
+
context.fillStyle = "#333";
|
|
870
|
+
context.font = "12px sans-serif";
|
|
871
|
+
context.fillText("PDF Preview", 10, 20);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
}
|
|
875
|
+
];
|
|
876
|
+
}
|
|
877
|
+
};
|
|
878
|
+
}
|
|
879
|
+
};
|
|
880
|
+
function pdfPlugin() {
|
|
881
|
+
return new PdfPlugin();
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
// ../plugins/media/dist/index.js
|
|
885
|
+
var IMAGE_EXTS = [".jpg", ".jpeg", ".png", ".gif", ".webp", ".svg"];
|
|
886
|
+
var VIDEO_EXTS = [".mp4", ".webm", ".ogg"];
|
|
887
|
+
var AUDIO_EXTS = [".mp3", ".wav", ".ogg"];
|
|
888
|
+
var MediaPlugin = class {
|
|
889
|
+
id = "media";
|
|
890
|
+
name = "Media Preview";
|
|
891
|
+
extensions = [...IMAGE_EXTS, ...VIDEO_EXTS, ...AUDIO_EXTS];
|
|
892
|
+
mimeTypes = [
|
|
893
|
+
"image/jpeg",
|
|
894
|
+
"image/png",
|
|
895
|
+
"image/gif",
|
|
896
|
+
"image/webp",
|
|
897
|
+
"image/svg+xml",
|
|
898
|
+
"video/mp4",
|
|
899
|
+
"video/webm",
|
|
900
|
+
"video/ogg",
|
|
901
|
+
"audio/mpeg",
|
|
902
|
+
"audio/wav",
|
|
903
|
+
"audio/ogg"
|
|
904
|
+
];
|
|
905
|
+
weight = 90;
|
|
906
|
+
supports(file) {
|
|
907
|
+
const ext = file.metadata.extension?.toLowerCase() || "";
|
|
908
|
+
const mime = file.metadata.mimeType?.toLowerCase() || "";
|
|
909
|
+
return this.extensions.includes(ext) || this.mimeTypes.some((m) => mime.startsWith(m.split("/")[0]));
|
|
910
|
+
}
|
|
911
|
+
getToolbarActions(instance) {
|
|
912
|
+
const actions = [];
|
|
913
|
+
if (instance.zoomIn) {
|
|
914
|
+
actions.push(
|
|
915
|
+
{
|
|
916
|
+
id: "zoom-out",
|
|
917
|
+
icon: "zoom-out",
|
|
918
|
+
label: "Zoom Out",
|
|
919
|
+
type: "button",
|
|
920
|
+
group: "zoom",
|
|
921
|
+
execute: () => {
|
|
922
|
+
instance.zoomOut?.();
|
|
923
|
+
}
|
|
924
|
+
},
|
|
925
|
+
{
|
|
926
|
+
id: "zoom-in",
|
|
927
|
+
icon: "zoom-in",
|
|
928
|
+
label: "Zoom In",
|
|
929
|
+
type: "button",
|
|
930
|
+
group: "zoom",
|
|
931
|
+
execute: () => {
|
|
932
|
+
instance.zoomIn?.();
|
|
933
|
+
}
|
|
934
|
+
},
|
|
935
|
+
{
|
|
936
|
+
id: "fit-page",
|
|
937
|
+
icon: "fit-page",
|
|
938
|
+
label: "Fit to Page",
|
|
939
|
+
type: "button",
|
|
940
|
+
group: "zoom",
|
|
941
|
+
execute: () => {
|
|
942
|
+
instance.fitToPage?.();
|
|
943
|
+
}
|
|
944
|
+
},
|
|
945
|
+
{
|
|
946
|
+
id: "rotate-cw",
|
|
947
|
+
icon: "rotate-cw",
|
|
948
|
+
label: "Rotate",
|
|
949
|
+
type: "button",
|
|
950
|
+
group: "view",
|
|
951
|
+
execute: () => {
|
|
952
|
+
instance.rotateCW?.();
|
|
953
|
+
}
|
|
954
|
+
}
|
|
955
|
+
);
|
|
956
|
+
}
|
|
957
|
+
if (instance.play) {
|
|
958
|
+
actions.push(
|
|
959
|
+
{
|
|
960
|
+
id: "play",
|
|
961
|
+
icon: "play",
|
|
962
|
+
label: "Play",
|
|
963
|
+
type: "button",
|
|
964
|
+
group: "actions",
|
|
965
|
+
execute: () => {
|
|
966
|
+
instance.play?.();
|
|
967
|
+
}
|
|
968
|
+
},
|
|
969
|
+
{
|
|
970
|
+
id: "pause",
|
|
971
|
+
icon: "pause",
|
|
972
|
+
label: "Pause",
|
|
973
|
+
type: "button",
|
|
974
|
+
group: "actions",
|
|
975
|
+
execute: () => {
|
|
976
|
+
instance.pause?.();
|
|
977
|
+
}
|
|
978
|
+
}
|
|
979
|
+
);
|
|
980
|
+
}
|
|
981
|
+
actions.push({
|
|
982
|
+
id: "download",
|
|
983
|
+
icon: "download",
|
|
984
|
+
label: "Download",
|
|
985
|
+
type: "button",
|
|
986
|
+
group: "actions",
|
|
987
|
+
execute: () => {
|
|
988
|
+
instance.download?.();
|
|
989
|
+
}
|
|
990
|
+
});
|
|
991
|
+
if (instance.print) {
|
|
992
|
+
actions.push({
|
|
993
|
+
id: "print",
|
|
994
|
+
icon: "print",
|
|
995
|
+
label: "Print",
|
|
996
|
+
type: "button",
|
|
997
|
+
group: "actions",
|
|
998
|
+
execute: () => {
|
|
999
|
+
instance.print?.();
|
|
1000
|
+
}
|
|
1001
|
+
});
|
|
1002
|
+
}
|
|
1003
|
+
return actions;
|
|
1004
|
+
}
|
|
1005
|
+
async render(ctx) {
|
|
1006
|
+
const mimeType = ctx.metadata.mimeType || "application/octet-stream";
|
|
1007
|
+
const isImage = mimeType.startsWith("image/") || IMAGE_EXTS.includes(ctx.metadata.extension || "");
|
|
1008
|
+
const isVideo = mimeType.startsWith("video/") || VIDEO_EXTS.includes(ctx.metadata.extension || "");
|
|
1009
|
+
const isAudio = mimeType.startsWith("audio/") || AUDIO_EXTS.includes(ctx.metadata.extension || "");
|
|
1010
|
+
let url = "";
|
|
1011
|
+
if (ctx.metadata.extension === ".svg" || mimeType === "image/svg+xml") {
|
|
1012
|
+
const decoder = new TextDecoder("utf-8");
|
|
1013
|
+
const svgText = decoder.decode(ctx.buffer);
|
|
1014
|
+
const blob = new Blob([svgText], { type: "image/svg+xml" });
|
|
1015
|
+
url = URL.createObjectURL(blob);
|
|
1016
|
+
} else {
|
|
1017
|
+
const blob = new Blob([ctx.buffer], { type: mimeType });
|
|
1018
|
+
url = URL.createObjectURL(blob);
|
|
1019
|
+
}
|
|
1020
|
+
let element;
|
|
1021
|
+
let currentZoom = 1;
|
|
1022
|
+
let rotation = 0;
|
|
1023
|
+
let mediaElement = null;
|
|
1024
|
+
if (isImage) {
|
|
1025
|
+
const img = document.createElement("img");
|
|
1026
|
+
img.src = url;
|
|
1027
|
+
img.style.maxWidth = "100%";
|
|
1028
|
+
img.style.maxHeight = "100%";
|
|
1029
|
+
img.style.objectFit = "contain";
|
|
1030
|
+
img.style.transition = "transform 0.2s ease";
|
|
1031
|
+
element = img;
|
|
1032
|
+
} else if (isVideo) {
|
|
1033
|
+
const video = document.createElement("video");
|
|
1034
|
+
video.src = url;
|
|
1035
|
+
video.controls = true;
|
|
1036
|
+
video.style.maxWidth = "100%";
|
|
1037
|
+
video.style.maxHeight = "100%";
|
|
1038
|
+
element = video;
|
|
1039
|
+
mediaElement = video;
|
|
1040
|
+
} else if (isAudio) {
|
|
1041
|
+
const audio = document.createElement("audio");
|
|
1042
|
+
audio.src = url;
|
|
1043
|
+
audio.controls = true;
|
|
1044
|
+
element = audio;
|
|
1045
|
+
mediaElement = audio;
|
|
1046
|
+
} else {
|
|
1047
|
+
element = document.createElement("div");
|
|
1048
|
+
element.textContent = "Unsupported media type";
|
|
1049
|
+
}
|
|
1050
|
+
ctx.container.style.display = "flex";
|
|
1051
|
+
ctx.container.style.alignItems = "center";
|
|
1052
|
+
ctx.container.style.justifyContent = "center";
|
|
1053
|
+
ctx.container.style.overflow = "hidden";
|
|
1054
|
+
ctx.container.appendChild(element);
|
|
1055
|
+
const applyTransform = () => {
|
|
1056
|
+
element.style.transform = `scale(${currentZoom}) rotate(${rotation}deg)`;
|
|
1057
|
+
};
|
|
1058
|
+
const cleanup = () => {
|
|
1059
|
+
if (url) URL.revokeObjectURL(url);
|
|
1060
|
+
element.remove();
|
|
1061
|
+
ctx.container.innerHTML = "";
|
|
1062
|
+
};
|
|
1063
|
+
ctx.signal.addEventListener("abort", cleanup);
|
|
1064
|
+
return {
|
|
1065
|
+
destroy: cleanup,
|
|
1066
|
+
zoomIn: isImage ? () => {
|
|
1067
|
+
currentZoom += 0.1;
|
|
1068
|
+
applyTransform();
|
|
1069
|
+
} : void 0,
|
|
1070
|
+
zoomOut: isImage ? () => {
|
|
1071
|
+
currentZoom = Math.max(0.1, currentZoom - 0.1);
|
|
1072
|
+
applyTransform();
|
|
1073
|
+
} : void 0,
|
|
1074
|
+
getZoom: isImage ? () => currentZoom : void 0,
|
|
1075
|
+
setZoom: isImage ? (level) => {
|
|
1076
|
+
currentZoom = level;
|
|
1077
|
+
applyTransform();
|
|
1078
|
+
} : void 0,
|
|
1079
|
+
fitToPage: isImage ? () => {
|
|
1080
|
+
currentZoom = 1;
|
|
1081
|
+
rotation = 0;
|
|
1082
|
+
applyTransform();
|
|
1083
|
+
} : void 0,
|
|
1084
|
+
rotateCW: isImage || isVideo ? () => {
|
|
1085
|
+
rotation = (rotation + 90) % 360;
|
|
1086
|
+
applyTransform();
|
|
1087
|
+
} : void 0,
|
|
1088
|
+
rotateCCW: isImage || isVideo ? () => {
|
|
1089
|
+
rotation = (rotation - 90 + 360) % 360;
|
|
1090
|
+
applyTransform();
|
|
1091
|
+
} : void 0,
|
|
1092
|
+
getRotation: () => rotation,
|
|
1093
|
+
play: mediaElement ? () => mediaElement?.play() : void 0,
|
|
1094
|
+
pause: mediaElement ? () => mediaElement?.pause() : void 0,
|
|
1095
|
+
isPlaying: mediaElement ? () => !mediaElement?.paused : void 0,
|
|
1096
|
+
download: () => {
|
|
1097
|
+
const a = document.createElement("a");
|
|
1098
|
+
a.href = url;
|
|
1099
|
+
a.download = ctx.metadata.name || "media";
|
|
1100
|
+
a.click();
|
|
1101
|
+
},
|
|
1102
|
+
print: isImage ? () => {
|
|
1103
|
+
window.print();
|
|
1104
|
+
} : void 0
|
|
1105
|
+
};
|
|
1106
|
+
}
|
|
1107
|
+
};
|
|
1108
|
+
function mediaPlugin() {
|
|
1109
|
+
return new MediaPlugin();
|
|
1110
|
+
}
|
|
1111
|
+
var DocxPlugin = class {
|
|
1112
|
+
id = "docx";
|
|
1113
|
+
name = "Word Document Preview";
|
|
1114
|
+
extensions = [".docx"];
|
|
1115
|
+
mimeTypes = ["application/vnd.openxmlformats-officedocument.wordprocessingml.document"];
|
|
1116
|
+
weight = 80;
|
|
1117
|
+
supports(file) {
|
|
1118
|
+
const ext = file.metadata.extension?.toLowerCase();
|
|
1119
|
+
const mime = file.metadata.mimeType?.toLowerCase();
|
|
1120
|
+
return ext === ".docx" || this.mimeTypes.includes(mime || "");
|
|
1121
|
+
}
|
|
1122
|
+
getToolbarActions(instance) {
|
|
1123
|
+
return [
|
|
1124
|
+
{
|
|
1125
|
+
id: "zoom-out",
|
|
1126
|
+
icon: "zoom-out",
|
|
1127
|
+
label: "Zoom Out",
|
|
1128
|
+
type: "button",
|
|
1129
|
+
group: "zoom",
|
|
1130
|
+
execute: () => {
|
|
1131
|
+
instance.zoomOut?.();
|
|
1132
|
+
}
|
|
1133
|
+
},
|
|
1134
|
+
{
|
|
1135
|
+
id: "zoom-in",
|
|
1136
|
+
icon: "zoom-in",
|
|
1137
|
+
label: "Zoom In",
|
|
1138
|
+
type: "button",
|
|
1139
|
+
group: "zoom",
|
|
1140
|
+
execute: () => {
|
|
1141
|
+
instance.zoomIn?.();
|
|
1142
|
+
}
|
|
1143
|
+
},
|
|
1144
|
+
{
|
|
1145
|
+
id: "fit-page",
|
|
1146
|
+
icon: "fit-page",
|
|
1147
|
+
label: "Fit to Page",
|
|
1148
|
+
type: "button",
|
|
1149
|
+
group: "zoom",
|
|
1150
|
+
execute: () => {
|
|
1151
|
+
instance.fitToPage?.();
|
|
1152
|
+
}
|
|
1153
|
+
},
|
|
1154
|
+
{
|
|
1155
|
+
id: "download",
|
|
1156
|
+
icon: "download",
|
|
1157
|
+
label: "Download",
|
|
1158
|
+
type: "button",
|
|
1159
|
+
group: "actions",
|
|
1160
|
+
execute: () => {
|
|
1161
|
+
instance.download?.();
|
|
1162
|
+
}
|
|
1163
|
+
},
|
|
1164
|
+
{
|
|
1165
|
+
id: "print",
|
|
1166
|
+
icon: "print",
|
|
1167
|
+
label: "Print",
|
|
1168
|
+
type: "button",
|
|
1169
|
+
group: "actions",
|
|
1170
|
+
execute: () => {
|
|
1171
|
+
instance.print?.();
|
|
1172
|
+
}
|
|
1173
|
+
}
|
|
1174
|
+
];
|
|
1175
|
+
}
|
|
1176
|
+
async render(ctx) {
|
|
1177
|
+
const wrapper = document.createElement("div");
|
|
1178
|
+
wrapper.className = "fp-docx-wrapper";
|
|
1179
|
+
wrapper.style.transformOrigin = "top center";
|
|
1180
|
+
wrapper.style.transition = "transform 0.2s ease";
|
|
1181
|
+
wrapper.style.padding = "16px";
|
|
1182
|
+
ctx.container.style.overflow = "auto";
|
|
1183
|
+
ctx.container.appendChild(wrapper);
|
|
1184
|
+
let scale = 1;
|
|
1185
|
+
try {
|
|
1186
|
+
await docx.renderAsync(ctx.buffer, wrapper, ctx.container, {
|
|
1187
|
+
inWrapper: true,
|
|
1188
|
+
ignoreWidth: false,
|
|
1189
|
+
ignoreHeight: false
|
|
1190
|
+
});
|
|
1191
|
+
} catch {
|
|
1192
|
+
wrapper.innerHTML = `
|
|
1193
|
+
<div style="text-align:center; padding: 40px; color: #666;">
|
|
1194
|
+
<div style="font-size:48px; margin-bottom: 16px;">\u{1F4C4}</div>
|
|
1195
|
+
<h3>${ctx.metadata.name || "Word Document"}</h3>
|
|
1196
|
+
<p>DOCX render preview</p>
|
|
1197
|
+
</div>
|
|
1198
|
+
`;
|
|
1199
|
+
}
|
|
1200
|
+
const cleanup = () => {
|
|
1201
|
+
wrapper.remove();
|
|
1202
|
+
ctx.container.innerHTML = "";
|
|
1203
|
+
};
|
|
1204
|
+
ctx.signal.addEventListener("abort", cleanup);
|
|
1205
|
+
return {
|
|
1206
|
+
destroy: cleanup,
|
|
1207
|
+
zoomIn: () => {
|
|
1208
|
+
scale += 0.1;
|
|
1209
|
+
wrapper.style.transform = `scale(${scale})`;
|
|
1210
|
+
},
|
|
1211
|
+
zoomOut: () => {
|
|
1212
|
+
scale = Math.max(0.2, scale - 0.1);
|
|
1213
|
+
wrapper.style.transform = `scale(${scale})`;
|
|
1214
|
+
},
|
|
1215
|
+
getZoom: () => scale,
|
|
1216
|
+
setZoom: (level) => {
|
|
1217
|
+
scale = level;
|
|
1218
|
+
wrapper.style.transform = `scale(${scale})`;
|
|
1219
|
+
},
|
|
1220
|
+
fitToPage: () => {
|
|
1221
|
+
scale = 1;
|
|
1222
|
+
wrapper.style.transform = `scale(1)`;
|
|
1223
|
+
},
|
|
1224
|
+
download: () => {
|
|
1225
|
+
const blob = new Blob([ctx.buffer], { type: this.mimeTypes[0] });
|
|
1226
|
+
const url = URL.createObjectURL(blob);
|
|
1227
|
+
const a = document.createElement("a");
|
|
1228
|
+
a.href = url;
|
|
1229
|
+
a.download = ctx.metadata.name || "document.docx";
|
|
1230
|
+
a.click();
|
|
1231
|
+
URL.revokeObjectURL(url);
|
|
1232
|
+
},
|
|
1233
|
+
print: () => {
|
|
1234
|
+
window.print();
|
|
1235
|
+
}
|
|
1236
|
+
};
|
|
1237
|
+
}
|
|
1238
|
+
};
|
|
1239
|
+
function docxPlugin() {
|
|
1240
|
+
return new DocxPlugin();
|
|
1241
|
+
}
|
|
1242
|
+
var ExcelPlugin = class {
|
|
1243
|
+
id = "excel";
|
|
1244
|
+
name = "Excel Spreadsheet Preview";
|
|
1245
|
+
extensions = [".xlsx", ".xls"];
|
|
1246
|
+
mimeTypes = [
|
|
1247
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
|
|
1248
|
+
"application/vnd.ms-excel"
|
|
1249
|
+
];
|
|
1250
|
+
weight = 80;
|
|
1251
|
+
supports(file) {
|
|
1252
|
+
const ext = file.metadata.extension?.toLowerCase();
|
|
1253
|
+
const mime = file.metadata.mimeType?.toLowerCase();
|
|
1254
|
+
return this.extensions.includes(ext || "") || this.mimeTypes.includes(mime || "");
|
|
1255
|
+
}
|
|
1256
|
+
getToolbarActions(instance) {
|
|
1257
|
+
return [
|
|
1258
|
+
{
|
|
1259
|
+
id: "zoom-out",
|
|
1260
|
+
icon: "zoom-out",
|
|
1261
|
+
label: "Zoom Out",
|
|
1262
|
+
type: "button",
|
|
1263
|
+
group: "zoom",
|
|
1264
|
+
execute: () => {
|
|
1265
|
+
instance.zoomOut?.();
|
|
1266
|
+
}
|
|
1267
|
+
},
|
|
1268
|
+
{
|
|
1269
|
+
id: "zoom-in",
|
|
1270
|
+
icon: "zoom-in",
|
|
1271
|
+
label: "Zoom In",
|
|
1272
|
+
type: "button",
|
|
1273
|
+
group: "zoom",
|
|
1274
|
+
execute: () => {
|
|
1275
|
+
instance.zoomIn?.();
|
|
1276
|
+
}
|
|
1277
|
+
},
|
|
1278
|
+
{
|
|
1279
|
+
id: "page-nav",
|
|
1280
|
+
icon: "page-nav",
|
|
1281
|
+
label: "Sheet Navigation",
|
|
1282
|
+
type: "page-nav",
|
|
1283
|
+
group: "navigation",
|
|
1284
|
+
execute: (sheet) => {
|
|
1285
|
+
if (typeof sheet === "number") instance.goToPage?.(sheet);
|
|
1286
|
+
}
|
|
1287
|
+
},
|
|
1288
|
+
{
|
|
1289
|
+
id: "download",
|
|
1290
|
+
icon: "download",
|
|
1291
|
+
label: "Download",
|
|
1292
|
+
type: "button",
|
|
1293
|
+
group: "actions",
|
|
1294
|
+
execute: () => {
|
|
1295
|
+
instance.download?.();
|
|
1296
|
+
}
|
|
1297
|
+
},
|
|
1298
|
+
{
|
|
1299
|
+
id: "print",
|
|
1300
|
+
icon: "print",
|
|
1301
|
+
label: "Print",
|
|
1302
|
+
type: "button",
|
|
1303
|
+
group: "actions",
|
|
1304
|
+
execute: () => {
|
|
1305
|
+
instance.print?.();
|
|
1306
|
+
}
|
|
1307
|
+
}
|
|
1308
|
+
];
|
|
1309
|
+
}
|
|
1310
|
+
async render(ctx) {
|
|
1311
|
+
const container = document.createElement("div");
|
|
1312
|
+
container.style.display = "flex";
|
|
1313
|
+
container.style.flexDirection = "column";
|
|
1314
|
+
container.style.width = "100%";
|
|
1315
|
+
container.style.height = "100%";
|
|
1316
|
+
container.style.overflow = "hidden";
|
|
1317
|
+
const contentArea = document.createElement("div");
|
|
1318
|
+
contentArea.style.flex = "1";
|
|
1319
|
+
contentArea.style.overflow = "auto";
|
|
1320
|
+
contentArea.style.padding = "16px";
|
|
1321
|
+
contentArea.style.transformOrigin = "top left";
|
|
1322
|
+
const tabsArea = document.createElement("div");
|
|
1323
|
+
tabsArea.style.display = "flex";
|
|
1324
|
+
tabsArea.style.gap = "8px";
|
|
1325
|
+
tabsArea.style.padding = "8px 16px";
|
|
1326
|
+
tabsArea.style.borderTop = "1px solid #e0e0e0";
|
|
1327
|
+
tabsArea.style.backgroundColor = "#fafafa";
|
|
1328
|
+
tabsArea.style.overflowX = "auto";
|
|
1329
|
+
container.appendChild(contentArea);
|
|
1330
|
+
container.appendChild(tabsArea);
|
|
1331
|
+
ctx.container.appendChild(container);
|
|
1332
|
+
let scale = 1;
|
|
1333
|
+
let currentSheetIndex = 1;
|
|
1334
|
+
const workbook = new ExcelJS.Workbook();
|
|
1335
|
+
try {
|
|
1336
|
+
await workbook.xlsx.load(ctx.buffer);
|
|
1337
|
+
} catch {
|
|
1338
|
+
}
|
|
1339
|
+
const renderSheet = (index) => {
|
|
1340
|
+
contentArea.innerHTML = "";
|
|
1341
|
+
const sheet = workbook.getWorksheet(index);
|
|
1342
|
+
if (!sheet) return;
|
|
1343
|
+
const table = document.createElement("table");
|
|
1344
|
+
table.style.borderCollapse = "collapse";
|
|
1345
|
+
table.style.fontFamily = "sans-serif";
|
|
1346
|
+
table.style.fontSize = "13px";
|
|
1347
|
+
table.style.minWidth = "100%";
|
|
1348
|
+
sheet.eachRow({ includeEmpty: false }, (row) => {
|
|
1349
|
+
const tr = document.createElement("tr");
|
|
1350
|
+
row.eachCell({ includeEmpty: true }, (cell) => {
|
|
1351
|
+
const td = document.createElement("td");
|
|
1352
|
+
td.style.border = "1px solid #d0d7de";
|
|
1353
|
+
td.style.padding = "6px 12px";
|
|
1354
|
+
td.textContent = cell.text || "";
|
|
1355
|
+
tr.appendChild(td);
|
|
1356
|
+
});
|
|
1357
|
+
table.appendChild(tr);
|
|
1358
|
+
});
|
|
1359
|
+
contentArea.appendChild(table);
|
|
1360
|
+
currentSheetIndex = index;
|
|
1361
|
+
};
|
|
1362
|
+
workbook.worksheets.forEach((sheet, idx) => {
|
|
1363
|
+
const btn = document.createElement("button");
|
|
1364
|
+
btn.textContent = sheet.name;
|
|
1365
|
+
btn.style.padding = "4px 12px";
|
|
1366
|
+
btn.style.fontSize = "12px";
|
|
1367
|
+
btn.style.cursor = "pointer";
|
|
1368
|
+
btn.style.border = "1px solid #ccc";
|
|
1369
|
+
btn.style.borderRadius = "4px";
|
|
1370
|
+
btn.style.backgroundColor = "#fff";
|
|
1371
|
+
btn.onclick = () => renderSheet(idx + 1);
|
|
1372
|
+
tabsArea.appendChild(btn);
|
|
1373
|
+
});
|
|
1374
|
+
if (workbook.worksheets.length > 0) {
|
|
1375
|
+
renderSheet(1);
|
|
1376
|
+
}
|
|
1377
|
+
const cleanup = () => {
|
|
1378
|
+
container.remove();
|
|
1379
|
+
ctx.container.innerHTML = "";
|
|
1380
|
+
};
|
|
1381
|
+
ctx.signal.addEventListener("abort", cleanup);
|
|
1382
|
+
return {
|
|
1383
|
+
destroy: cleanup,
|
|
1384
|
+
zoomIn: () => {
|
|
1385
|
+
scale += 0.1;
|
|
1386
|
+
contentArea.style.transform = `scale(${scale})`;
|
|
1387
|
+
},
|
|
1388
|
+
zoomOut: () => {
|
|
1389
|
+
scale = Math.max(0.2, scale - 0.1);
|
|
1390
|
+
contentArea.style.transform = `scale(${scale})`;
|
|
1391
|
+
},
|
|
1392
|
+
getZoom: () => scale,
|
|
1393
|
+
setZoom: (level) => {
|
|
1394
|
+
scale = level;
|
|
1395
|
+
contentArea.style.transform = `scale(${scale})`;
|
|
1396
|
+
},
|
|
1397
|
+
goToPage: (page) => {
|
|
1398
|
+
if (page > 0 && page <= workbook.worksheets.length) {
|
|
1399
|
+
renderSheet(page);
|
|
1400
|
+
}
|
|
1401
|
+
},
|
|
1402
|
+
getPageCount: () => workbook.worksheets.length,
|
|
1403
|
+
getCurrentPage: () => currentSheetIndex,
|
|
1404
|
+
download: () => {
|
|
1405
|
+
const blob = new Blob([ctx.buffer], { type: this.mimeTypes[0] });
|
|
1406
|
+
const url = URL.createObjectURL(blob);
|
|
1407
|
+
const a = document.createElement("a");
|
|
1408
|
+
a.href = url;
|
|
1409
|
+
a.download = ctx.metadata.name || "spreadsheet.xlsx";
|
|
1410
|
+
a.click();
|
|
1411
|
+
URL.revokeObjectURL(url);
|
|
1412
|
+
},
|
|
1413
|
+
print: () => {
|
|
1414
|
+
window.print();
|
|
1415
|
+
}
|
|
1416
|
+
};
|
|
1417
|
+
}
|
|
1418
|
+
};
|
|
1419
|
+
function excelPlugin() {
|
|
1420
|
+
return new ExcelPlugin();
|
|
1421
|
+
}
|
|
1422
|
+
|
|
1423
|
+
// ../plugins/csv/dist/index.js
|
|
1424
|
+
var CsvPlugin = class {
|
|
1425
|
+
id = "csv";
|
|
1426
|
+
name = "CSV Preview";
|
|
1427
|
+
extensions = [".csv", ".tsv"];
|
|
1428
|
+
mimeTypes = ["text/csv", "text/tab-separated-values"];
|
|
1429
|
+
weight = 80;
|
|
1430
|
+
supports(file) {
|
|
1431
|
+
const ext = file.metadata.extension?.toLowerCase();
|
|
1432
|
+
const mime = file.metadata.mimeType?.toLowerCase();
|
|
1433
|
+
return ext === ".csv" || ext === ".tsv" || mime === "text/csv" || mime === "text/tab-separated-values";
|
|
1434
|
+
}
|
|
1435
|
+
getToolbarActions(instance) {
|
|
1436
|
+
return [
|
|
1437
|
+
{
|
|
1438
|
+
id: "zoom-out",
|
|
1439
|
+
icon: "zoom-out",
|
|
1440
|
+
label: "Zoom Out",
|
|
1441
|
+
type: "button",
|
|
1442
|
+
group: "zoom",
|
|
1443
|
+
execute: () => {
|
|
1444
|
+
instance.zoomOut?.();
|
|
1445
|
+
}
|
|
1446
|
+
},
|
|
1447
|
+
{
|
|
1448
|
+
id: "zoom-in",
|
|
1449
|
+
icon: "zoom-in",
|
|
1450
|
+
label: "Zoom In",
|
|
1451
|
+
type: "button",
|
|
1452
|
+
group: "zoom",
|
|
1453
|
+
execute: () => {
|
|
1454
|
+
instance.zoomIn?.();
|
|
1455
|
+
}
|
|
1456
|
+
},
|
|
1457
|
+
{
|
|
1458
|
+
id: "download",
|
|
1459
|
+
icon: "download",
|
|
1460
|
+
label: "Download",
|
|
1461
|
+
type: "button",
|
|
1462
|
+
group: "actions",
|
|
1463
|
+
execute: () => {
|
|
1464
|
+
instance.download?.();
|
|
1465
|
+
}
|
|
1466
|
+
},
|
|
1467
|
+
{
|
|
1468
|
+
id: "print",
|
|
1469
|
+
icon: "print",
|
|
1470
|
+
label: "Print",
|
|
1471
|
+
type: "button",
|
|
1472
|
+
group: "actions",
|
|
1473
|
+
execute: () => {
|
|
1474
|
+
instance.print?.();
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
];
|
|
1478
|
+
}
|
|
1479
|
+
async render(ctx) {
|
|
1480
|
+
const decoder = new TextDecoder("utf-8");
|
|
1481
|
+
const text = decoder.decode(ctx.buffer);
|
|
1482
|
+
const container = document.createElement("div");
|
|
1483
|
+
container.style.width = "100%";
|
|
1484
|
+
container.style.height = "100%";
|
|
1485
|
+
container.style.overflow = "auto";
|
|
1486
|
+
container.style.padding = "16px";
|
|
1487
|
+
container.style.boxSizing = "border-box";
|
|
1488
|
+
container.style.transformOrigin = "top left";
|
|
1489
|
+
const table = document.createElement("table");
|
|
1490
|
+
table.style.borderCollapse = "collapse";
|
|
1491
|
+
table.style.width = "100%";
|
|
1492
|
+
table.style.fontFamily = "monospace";
|
|
1493
|
+
table.style.fontSize = "13px";
|
|
1494
|
+
const delimiter = ctx.metadata.extension === ".tsv" ? " " : ",";
|
|
1495
|
+
const rows = text.split(/\r?\n/).slice(0, 500);
|
|
1496
|
+
rows.forEach((row, rowIndex) => {
|
|
1497
|
+
if (!row.trim()) return;
|
|
1498
|
+
const tr = document.createElement("tr");
|
|
1499
|
+
const cells = row.split(delimiter);
|
|
1500
|
+
cells.forEach((cell) => {
|
|
1501
|
+
const td = document.createElement(rowIndex === 0 ? "th" : "td");
|
|
1502
|
+
td.textContent = cell.trim();
|
|
1503
|
+
td.style.border = "1px solid #d0d7de";
|
|
1504
|
+
td.style.padding = "6px 12px";
|
|
1505
|
+
if (rowIndex === 0) {
|
|
1506
|
+
td.style.backgroundColor = "#f6f8fa";
|
|
1507
|
+
td.style.fontWeight = "bold";
|
|
1508
|
+
}
|
|
1509
|
+
tr.appendChild(td);
|
|
1510
|
+
});
|
|
1511
|
+
table.appendChild(tr);
|
|
1512
|
+
});
|
|
1513
|
+
container.appendChild(table);
|
|
1514
|
+
ctx.container.appendChild(container);
|
|
1515
|
+
let scale = 1;
|
|
1516
|
+
const cleanup = () => {
|
|
1517
|
+
container.remove();
|
|
1518
|
+
ctx.container.innerHTML = "";
|
|
1519
|
+
};
|
|
1520
|
+
ctx.signal.addEventListener("abort", cleanup);
|
|
1521
|
+
return {
|
|
1522
|
+
destroy: cleanup,
|
|
1523
|
+
zoomIn: () => {
|
|
1524
|
+
scale += 0.1;
|
|
1525
|
+
container.style.transform = `scale(${scale})`;
|
|
1526
|
+
},
|
|
1527
|
+
zoomOut: () => {
|
|
1528
|
+
scale = Math.max(0.2, scale - 0.1);
|
|
1529
|
+
container.style.transform = `scale(${scale})`;
|
|
1530
|
+
},
|
|
1531
|
+
getZoom: () => scale,
|
|
1532
|
+
setZoom: (level) => {
|
|
1533
|
+
scale = level;
|
|
1534
|
+
container.style.transform = `scale(${scale})`;
|
|
1535
|
+
},
|
|
1536
|
+
fitToPage: () => {
|
|
1537
|
+
scale = 1;
|
|
1538
|
+
container.style.transform = `scale(1)`;
|
|
1539
|
+
},
|
|
1540
|
+
download: () => {
|
|
1541
|
+
const blob = new Blob([ctx.buffer], { type: "text/csv" });
|
|
1542
|
+
const url = URL.createObjectURL(blob);
|
|
1543
|
+
const a = document.createElement("a");
|
|
1544
|
+
a.href = url;
|
|
1545
|
+
a.download = ctx.metadata.name || "data.csv";
|
|
1546
|
+
a.click();
|
|
1547
|
+
URL.revokeObjectURL(url);
|
|
1548
|
+
},
|
|
1549
|
+
print: () => {
|
|
1550
|
+
window.print();
|
|
1551
|
+
}
|
|
1552
|
+
};
|
|
1553
|
+
}
|
|
1554
|
+
};
|
|
1555
|
+
function csvPlugin() {
|
|
1556
|
+
return new CsvPlugin();
|
|
1557
|
+
}
|
|
1558
|
+
var CODE_EXTENSIONS = [
|
|
1559
|
+
".txt",
|
|
1560
|
+
".json",
|
|
1561
|
+
".js",
|
|
1562
|
+
".ts",
|
|
1563
|
+
".jsx",
|
|
1564
|
+
".tsx",
|
|
1565
|
+
".html",
|
|
1566
|
+
".css",
|
|
1567
|
+
".scss",
|
|
1568
|
+
".less",
|
|
1569
|
+
".md",
|
|
1570
|
+
".xml",
|
|
1571
|
+
".yml",
|
|
1572
|
+
".yaml",
|
|
1573
|
+
".sh",
|
|
1574
|
+
".bash",
|
|
1575
|
+
".py",
|
|
1576
|
+
".java",
|
|
1577
|
+
".c",
|
|
1578
|
+
".cpp",
|
|
1579
|
+
".h",
|
|
1580
|
+
".cs",
|
|
1581
|
+
".go",
|
|
1582
|
+
".rs",
|
|
1583
|
+
".sql",
|
|
1584
|
+
".php"
|
|
1585
|
+
];
|
|
1586
|
+
var CodePlugin = class {
|
|
1587
|
+
id = "code";
|
|
1588
|
+
name = "Code/Text Preview";
|
|
1589
|
+
extensions = CODE_EXTENSIONS;
|
|
1590
|
+
mimeTypes = ["text/plain", "application/json", "text/javascript", "text/html", "text/css"];
|
|
1591
|
+
weight = 50;
|
|
1592
|
+
supports(file) {
|
|
1593
|
+
const ext = file.metadata.extension?.toLowerCase();
|
|
1594
|
+
const mime = file.metadata.mimeType?.toLowerCase();
|
|
1595
|
+
if (ext && this.extensions.includes(ext)) return true;
|
|
1596
|
+
if (mime && (mime.startsWith("text/") || this.mimeTypes.includes(mime))) return true;
|
|
1597
|
+
return false;
|
|
1598
|
+
}
|
|
1599
|
+
getToolbarActions(instance) {
|
|
1600
|
+
return [
|
|
1601
|
+
{
|
|
1602
|
+
id: "zoom-out",
|
|
1603
|
+
icon: "zoom-out",
|
|
1604
|
+
label: "Zoom Out",
|
|
1605
|
+
type: "button",
|
|
1606
|
+
group: "zoom",
|
|
1607
|
+
execute: () => {
|
|
1608
|
+
instance.zoomOut?.();
|
|
1609
|
+
}
|
|
1610
|
+
},
|
|
1611
|
+
{
|
|
1612
|
+
id: "zoom-in",
|
|
1613
|
+
icon: "zoom-in",
|
|
1614
|
+
label: "Zoom In",
|
|
1615
|
+
type: "button",
|
|
1616
|
+
group: "zoom",
|
|
1617
|
+
execute: () => {
|
|
1618
|
+
instance.zoomIn?.();
|
|
1619
|
+
}
|
|
1620
|
+
},
|
|
1621
|
+
{
|
|
1622
|
+
id: "download",
|
|
1623
|
+
icon: "download",
|
|
1624
|
+
label: "Download",
|
|
1625
|
+
type: "button",
|
|
1626
|
+
group: "actions",
|
|
1627
|
+
execute: () => {
|
|
1628
|
+
instance.download?.();
|
|
1629
|
+
}
|
|
1630
|
+
},
|
|
1631
|
+
{
|
|
1632
|
+
id: "print",
|
|
1633
|
+
icon: "print",
|
|
1634
|
+
label: "Print",
|
|
1635
|
+
type: "button",
|
|
1636
|
+
group: "actions",
|
|
1637
|
+
execute: () => {
|
|
1638
|
+
instance.print?.();
|
|
1639
|
+
}
|
|
1640
|
+
}
|
|
1641
|
+
];
|
|
1642
|
+
}
|
|
1643
|
+
async render(ctx) {
|
|
1644
|
+
const decoder = new TextDecoder("utf-8");
|
|
1645
|
+
const text = decoder.decode(ctx.buffer);
|
|
1646
|
+
const container = document.createElement("div");
|
|
1647
|
+
container.style.width = "100%";
|
|
1648
|
+
container.style.height = "100%";
|
|
1649
|
+
container.style.overflow = "auto";
|
|
1650
|
+
container.style.backgroundColor = "#1e1e1e";
|
|
1651
|
+
container.style.color = "#d4d4d4";
|
|
1652
|
+
container.style.padding = "16px";
|
|
1653
|
+
container.style.boxSizing = "border-box";
|
|
1654
|
+
let fontSize = 13;
|
|
1655
|
+
const pre = document.createElement("pre");
|
|
1656
|
+
pre.style.margin = "0";
|
|
1657
|
+
pre.style.fontFamily = "Consolas, Menlo, Monaco, monospace";
|
|
1658
|
+
pre.style.fontSize = `${fontSize}px`;
|
|
1659
|
+
pre.style.lineHeight = "1.5";
|
|
1660
|
+
pre.style.whiteSpace = "pre-wrap";
|
|
1661
|
+
pre.style.wordBreak = "break-all";
|
|
1662
|
+
const code = document.createElement("code");
|
|
1663
|
+
const ext = (ctx.metadata.extension || "").replace(".", "");
|
|
1664
|
+
try {
|
|
1665
|
+
if (ext && hljs.getLanguage(ext)) {
|
|
1666
|
+
code.innerHTML = hljs.highlight(text, { language: ext }).value;
|
|
1667
|
+
} else {
|
|
1668
|
+
code.innerHTML = hljs.highlightAuto(text).value;
|
|
1669
|
+
}
|
|
1670
|
+
} catch {
|
|
1671
|
+
code.textContent = text;
|
|
1672
|
+
}
|
|
1673
|
+
pre.appendChild(code);
|
|
1674
|
+
container.appendChild(pre);
|
|
1675
|
+
ctx.container.appendChild(container);
|
|
1676
|
+
const cleanup = () => {
|
|
1677
|
+
container.remove();
|
|
1678
|
+
ctx.container.innerHTML = "";
|
|
1679
|
+
};
|
|
1680
|
+
ctx.signal.addEventListener("abort", cleanup);
|
|
1681
|
+
return {
|
|
1682
|
+
destroy: cleanup,
|
|
1683
|
+
zoomIn: () => {
|
|
1684
|
+
fontSize = Math.min(32, fontSize + 2);
|
|
1685
|
+
pre.style.fontSize = `${fontSize}px`;
|
|
1686
|
+
},
|
|
1687
|
+
zoomOut: () => {
|
|
1688
|
+
fontSize = Math.max(8, fontSize - 2);
|
|
1689
|
+
pre.style.fontSize = `${fontSize}px`;
|
|
1690
|
+
},
|
|
1691
|
+
getZoom: () => fontSize / 13,
|
|
1692
|
+
setZoom: (level) => {
|
|
1693
|
+
fontSize = Math.round(13 * level);
|
|
1694
|
+
pre.style.fontSize = `${fontSize}px`;
|
|
1695
|
+
},
|
|
1696
|
+
download: () => {
|
|
1697
|
+
const mimeType = ctx.metadata.mimeType || "text/plain";
|
|
1698
|
+
const blob = new Blob([ctx.buffer], { type: mimeType });
|
|
1699
|
+
const url = URL.createObjectURL(blob);
|
|
1700
|
+
const a = document.createElement("a");
|
|
1701
|
+
a.href = url;
|
|
1702
|
+
a.download = ctx.metadata.name || "code.txt";
|
|
1703
|
+
a.click();
|
|
1704
|
+
URL.revokeObjectURL(url);
|
|
1705
|
+
},
|
|
1706
|
+
print: () => {
|
|
1707
|
+
window.print();
|
|
1708
|
+
}
|
|
1709
|
+
};
|
|
1710
|
+
}
|
|
1711
|
+
};
|
|
1712
|
+
function codePlugin() {
|
|
1713
|
+
return new CodePlugin();
|
|
1714
|
+
}
|
|
1715
|
+
|
|
1716
|
+
// src/index.ts
|
|
1717
|
+
function getDefaultPlugins() {
|
|
1718
|
+
return [
|
|
1719
|
+
pdfPlugin(),
|
|
1720
|
+
mediaPlugin(),
|
|
1721
|
+
docxPlugin(),
|
|
1722
|
+
excelPlugin(),
|
|
1723
|
+
csvPlugin(),
|
|
1724
|
+
codePlugin()
|
|
1725
|
+
];
|
|
1726
|
+
}
|
|
1727
|
+
var FilePreviewViewer2 = class extends FilePreviewViewer {
|
|
1728
|
+
constructor(options) {
|
|
1729
|
+
super();
|
|
1730
|
+
if (options?.autoRegisterDefaults !== false) {
|
|
1731
|
+
this.registerPlugins(getDefaultPlugins());
|
|
1732
|
+
}
|
|
1733
|
+
}
|
|
1734
|
+
};
|
|
1735
|
+
var FilePreview = memo(forwardRef(
|
|
1736
|
+
({
|
|
1737
|
+
src,
|
|
1738
|
+
plugins,
|
|
1739
|
+
options = {},
|
|
1740
|
+
onLoading,
|
|
1741
|
+
onLoaded,
|
|
1742
|
+
onError,
|
|
1743
|
+
onPageChange,
|
|
1744
|
+
onZoomChange,
|
|
1745
|
+
className,
|
|
1746
|
+
style
|
|
1747
|
+
}, ref) => {
|
|
1748
|
+
const containerRef = useRef(null);
|
|
1749
|
+
const viewerRef = useRef(null);
|
|
1750
|
+
const instanceRef = useRef(null);
|
|
1751
|
+
useImperativeHandle(ref, () => ({
|
|
1752
|
+
getInstance: () => instanceRef.current,
|
|
1753
|
+
getViewer: () => viewerRef.current,
|
|
1754
|
+
destroy: () => viewerRef.current?.destroy()
|
|
1755
|
+
}));
|
|
1756
|
+
useEffect(() => {
|
|
1757
|
+
const viewer = new FilePreviewViewer2({ autoRegisterDefaults: false });
|
|
1758
|
+
const activePlugins = plugins && plugins.length > 0 ? plugins : getDefaultPlugins();
|
|
1759
|
+
viewer.registerPlugins(activePlugins);
|
|
1760
|
+
viewerRef.current = viewer;
|
|
1761
|
+
const unsubs = [];
|
|
1762
|
+
if (onLoading) unsubs.push(viewer.on("loading", onLoading));
|
|
1763
|
+
if (onLoaded) unsubs.push(viewer.on("loaded", onLoaded));
|
|
1764
|
+
if (onError) unsubs.push(viewer.on("error", (e) => onError(e instanceof Error ? e : new Error(String(e)))));
|
|
1765
|
+
if (onPageChange) unsubs.push(viewer.on("page-change", onPageChange));
|
|
1766
|
+
if (onZoomChange) unsubs.push(viewer.on("zoom-change", onZoomChange));
|
|
1767
|
+
return () => {
|
|
1768
|
+
unsubs.forEach((fn) => fn());
|
|
1769
|
+
viewer.destroy();
|
|
1770
|
+
viewerRef.current = null;
|
|
1771
|
+
instanceRef.current = null;
|
|
1772
|
+
};
|
|
1773
|
+
}, []);
|
|
1774
|
+
useEffect(() => {
|
|
1775
|
+
if (!viewerRef.current || !containerRef.current || !src) return;
|
|
1776
|
+
const doPreview = async () => {
|
|
1777
|
+
try {
|
|
1778
|
+
instanceRef.current = await viewerRef.current.preview(
|
|
1779
|
+
containerRef.current,
|
|
1780
|
+
src,
|
|
1781
|
+
options
|
|
1782
|
+
);
|
|
1783
|
+
} catch (error) {
|
|
1784
|
+
if (error instanceof DOMException && error.name === "AbortError") return;
|
|
1785
|
+
if (onError) {
|
|
1786
|
+
onError(error instanceof Error ? error : new Error(String(error)));
|
|
1787
|
+
}
|
|
1788
|
+
}
|
|
1789
|
+
};
|
|
1790
|
+
doPreview();
|
|
1791
|
+
}, [src, options, onError]);
|
|
1792
|
+
return /* @__PURE__ */ jsx(
|
|
1793
|
+
"div",
|
|
1794
|
+
{
|
|
1795
|
+
ref: containerRef,
|
|
1796
|
+
className,
|
|
1797
|
+
style: { width: "100%", height: "100%", position: "relative", ...style }
|
|
1798
|
+
}
|
|
1799
|
+
);
|
|
1800
|
+
}
|
|
1801
|
+
));
|
|
1802
|
+
FilePreview.displayName = "FilePreview";
|
|
1803
|
+
var react_default = FilePreview;
|
|
1804
|
+
|
|
1805
|
+
export { FilePreview, react_default as default };
|
|
1806
|
+
//# sourceMappingURL=react.js.map
|
|
1807
|
+
//# sourceMappingURL=react.js.map
|