@eternalheart/react-file-preview 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 +22 -0
- package/README.md +384 -0
- package/README.zh-CN.md +383 -0
- package/lib/FilePreviewModal.d.ts +11 -0
- package/lib/FilePreviewModal.d.ts.map +1 -0
- package/lib/index.cjs +2 -0
- package/lib/index.cjs.map +1 -0
- package/lib/index.css +1 -0
- package/lib/index.d.ts +4 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.mjs +1478 -0
- package/lib/index.mjs.map +1 -0
- package/lib/renderers/AudioRenderer.d.ts +7 -0
- package/lib/renderers/AudioRenderer.d.ts.map +1 -0
- package/lib/renderers/DocxRenderer.d.ts +6 -0
- package/lib/renderers/DocxRenderer.d.ts.map +1 -0
- package/lib/renderers/ImageRenderer.d.ts +9 -0
- package/lib/renderers/ImageRenderer.d.ts.map +1 -0
- package/lib/renderers/MarkdownRenderer.d.ts +6 -0
- package/lib/renderers/MarkdownRenderer.d.ts.map +1 -0
- package/lib/renderers/PdfRenderer.d.ts +13 -0
- package/lib/renderers/PdfRenderer.d.ts.map +1 -0
- package/lib/renderers/PptxRenderer.d.ts +6 -0
- package/lib/renderers/PptxRenderer.d.ts.map +1 -0
- package/lib/renderers/TextRenderer.d.ts +7 -0
- package/lib/renderers/TextRenderer.d.ts.map +1 -0
- package/lib/renderers/UnsupportedRenderer.d.ts +8 -0
- package/lib/renderers/UnsupportedRenderer.d.ts.map +1 -0
- package/lib/renderers/VideoRenderer.d.ts +7 -0
- package/lib/renderers/VideoRenderer.d.ts.map +1 -0
- package/lib/renderers/XlsxRenderer.d.ts +6 -0
- package/lib/renderers/XlsxRenderer.d.ts.map +1 -0
- package/lib/types.d.ts +29 -0
- package/lib/types.d.ts.map +1 -0
- package/lib/utils/fileNormalizer.d.ts +14 -0
- package/lib/utils/fileNormalizer.d.ts.map +1 -0
- package/lib/utils/pdfConfig.d.ts +3 -0
- package/lib/utils/pdfConfig.d.ts.map +1 -0
- package/package.json +113 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 EternalHeart
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
# React File Preview [](https://www.npmjs.com/package/@eternalheart/react-file-preview)[](https://github.com/wh131462/react-file-preview/blob/master/LICENSE)[](https://www.npmjs.com/package/@eternalheart/react-file-preview)
|
|
2
|
+
|
|
3
|
+
English | [简体中文](./README.zh-CN.md)
|
|
4
|
+
|
|
5
|
+
A modern, feature-rich file preview component for React with support for images, videos, audio, PDFs, Office documents (Word, Excel, PowerPoint), Markdown, and code files.
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
## ✨ Features
|
|
10
|
+
|
|
11
|
+
- 🎨 **Modern UI** - Apple-inspired minimalist design with glassmorphism effects
|
|
12
|
+
- 📁 **Multi-format Support** - Supports 20+ file formats
|
|
13
|
+
- 🖼️ **Powerful Image Viewer** - Zoom, rotate, drag, mouse wheel zoom
|
|
14
|
+
- 🎬 **Custom Video Player** - Built on Video.js, supports multiple video formats
|
|
15
|
+
- 🎵 **Custom Audio Player** - Beautiful audio control interface
|
|
16
|
+
- 📄 **PDF Viewer** - Pagination support
|
|
17
|
+
- 📊 **Office Documents Support** - Word, Excel, PowerPoint file preview
|
|
18
|
+
- 📝 **Markdown Rendering** - GitHub Flavored Markdown support
|
|
19
|
+
- 💻 **Code Highlighting** - Supports 40+ programming languages
|
|
20
|
+
- 🎭 **Smooth Animations** - Powered by Framer Motion
|
|
21
|
+
- 📱 **Responsive Design** - Adapts to all screen sizes
|
|
22
|
+
- ⌨️ **Keyboard Navigation** - Arrow keys and ESC support
|
|
23
|
+
- 🎯 **Drag & Drop** - File upload via drag and drop
|
|
24
|
+
|
|
25
|
+
## 📦 Installation
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
# Using npm
|
|
29
|
+
npm install @eternalheart/react-file-preview
|
|
30
|
+
|
|
31
|
+
# Using yarn
|
|
32
|
+
yarn add @eternalheart/react-file-preview
|
|
33
|
+
|
|
34
|
+
# Using pnpm
|
|
35
|
+
pnpm add @eternalheart/react-file-preview
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
**Important:** You also need to import the CSS file:
|
|
39
|
+
|
|
40
|
+
```tsx
|
|
41
|
+
import '@eternalheart/react-file-preview/style.css';
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## 🚀 Quick Start
|
|
45
|
+
|
|
46
|
+
📖 **New to this library?** Check out the [Quick Start Guide](./QUICK_START.md) for a 5-minute introduction!
|
|
47
|
+
|
|
48
|
+
### Basic Usage
|
|
49
|
+
|
|
50
|
+
```tsx
|
|
51
|
+
import { FilePreviewModal } from '@eternalheart/react-file-preview';
|
|
52
|
+
import '@eternalheart/react-file-preview/style.css';
|
|
53
|
+
import { useState } from 'react';
|
|
54
|
+
|
|
55
|
+
function App() {
|
|
56
|
+
const [files, setFiles] = useState([]);
|
|
57
|
+
const [currentIndex, setCurrentIndex] = useState(0);
|
|
58
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
59
|
+
|
|
60
|
+
const handleFileSelect = (file: File) => {
|
|
61
|
+
// Method 1: Directly pass File object (recommended)
|
|
62
|
+
setFiles([file]);
|
|
63
|
+
setCurrentIndex(0);
|
|
64
|
+
setIsOpen(true);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<>
|
|
69
|
+
<input
|
|
70
|
+
type="file"
|
|
71
|
+
onChange={(e) => e.target.files?.[0] && handleFileSelect(e.target.files[0])}
|
|
72
|
+
/>
|
|
73
|
+
|
|
74
|
+
<FilePreviewModal
|
|
75
|
+
files={files}
|
|
76
|
+
currentIndex={currentIndex}
|
|
77
|
+
isOpen={isOpen}
|
|
78
|
+
onClose={() => setIsOpen(false)}
|
|
79
|
+
onNavigate={setCurrentIndex}
|
|
80
|
+
/>
|
|
81
|
+
</>
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
### Multiple Input Types
|
|
87
|
+
|
|
88
|
+
The component supports three types of file inputs:
|
|
89
|
+
|
|
90
|
+
```tsx
|
|
91
|
+
import { FilePreviewModal, PreviewFileInput } from '@eternalheart/react-file-preview';
|
|
92
|
+
import '@eternalheart/react-file-preview/style.css';
|
|
93
|
+
|
|
94
|
+
function App() {
|
|
95
|
+
const files: PreviewFileInput[] = [
|
|
96
|
+
// 1. Native File object
|
|
97
|
+
file1,
|
|
98
|
+
|
|
99
|
+
// 2. HTTP URL string
|
|
100
|
+
'https://example.com/image.jpg',
|
|
101
|
+
|
|
102
|
+
// 3. File object with metadata
|
|
103
|
+
{
|
|
104
|
+
name: 'document.pdf',
|
|
105
|
+
type: 'application/pdf',
|
|
106
|
+
url: '/path/to/document.pdf',
|
|
107
|
+
size: 1024,
|
|
108
|
+
},
|
|
109
|
+
];
|
|
110
|
+
|
|
111
|
+
return (
|
|
112
|
+
<FilePreviewModal
|
|
113
|
+
files={files}
|
|
114
|
+
currentIndex={0}
|
|
115
|
+
isOpen={true}
|
|
116
|
+
onClose={() => {}}
|
|
117
|
+
/>
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
## 💡 Usage Examples
|
|
123
|
+
|
|
124
|
+
### Preview PowerPoint Files
|
|
125
|
+
|
|
126
|
+
```tsx
|
|
127
|
+
import { FilePreviewModal } from '@eternalheart/react-file-preview';
|
|
128
|
+
import { useState } from 'react';
|
|
129
|
+
|
|
130
|
+
function PptPreview() {
|
|
131
|
+
const [isOpen, setIsOpen] = useState(false);
|
|
132
|
+
|
|
133
|
+
const pptFile = {
|
|
134
|
+
name: 'presentation.pptx',
|
|
135
|
+
type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
136
|
+
url: '/path/to/your/presentation.pptx',
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
return (
|
|
140
|
+
<>
|
|
141
|
+
<button onClick={() => setIsOpen(true)}>
|
|
142
|
+
Preview PPT
|
|
143
|
+
</button>
|
|
144
|
+
|
|
145
|
+
<FilePreviewModal
|
|
146
|
+
files={[pptFile]}
|
|
147
|
+
currentIndex={0}
|
|
148
|
+
isOpen={isOpen}
|
|
149
|
+
onClose={() => setIsOpen(false)}
|
|
150
|
+
/>
|
|
151
|
+
</>
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
### Preview Multiple Files
|
|
157
|
+
|
|
158
|
+
```tsx
|
|
159
|
+
const files = [
|
|
160
|
+
{ name: 'image.jpg', type: 'image/jpeg', url: '/path/to/image.jpg' },
|
|
161
|
+
{ name: 'document.pdf', type: 'application/pdf', url: '/path/to/document.pdf' },
|
|
162
|
+
{ name: 'presentation.pptx', type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation', url: '/path/to/presentation.pptx' },
|
|
163
|
+
{ name: 'spreadsheet.xlsx', type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', url: '/path/to/spreadsheet.xlsx' },
|
|
164
|
+
];
|
|
165
|
+
|
|
166
|
+
<FilePreviewModal
|
|
167
|
+
files={files}
|
|
168
|
+
currentIndex={0}
|
|
169
|
+
isOpen={isOpen}
|
|
170
|
+
onClose={() => setIsOpen(false)}
|
|
171
|
+
onNavigate={setCurrentIndex}
|
|
172
|
+
/>
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## 📖 Supported File Formats
|
|
176
|
+
|
|
177
|
+
### Images
|
|
178
|
+
- **Formats**: JPG, PNG, GIF, WebP, SVG, BMP, ICO
|
|
179
|
+
- **Features**: Zoom (0.5x - 5x), rotate, drag, mouse wheel zoom, double-click reset
|
|
180
|
+
|
|
181
|
+
### Videos
|
|
182
|
+
- **Formats**: MP4, WebM, OGG, MOV, AVI, MKV, M4V, 3GP, FLV
|
|
183
|
+
- **Features**: Custom player, progress control, volume adjustment, fullscreen
|
|
184
|
+
|
|
185
|
+
### Audio
|
|
186
|
+
- **Formats**: MP3, WAV, OGG, M4A, AAC, FLAC
|
|
187
|
+
- **Features**: Custom player, progress bar, volume control, skip forward/backward
|
|
188
|
+
|
|
189
|
+
### Documents
|
|
190
|
+
- **PDF**: Pagination, zoom
|
|
191
|
+
- **Word**: DOCX format support
|
|
192
|
+
- **Excel**: XLSX format support
|
|
193
|
+
- **PowerPoint**: PPTX/PPT format support, slide preview
|
|
194
|
+
|
|
195
|
+
### Code & Text
|
|
196
|
+
- **Markdown**: GitHub Flavored Markdown, code highlighting
|
|
197
|
+
- **Code Files**: JS, TS, Python, Java, C++, Go, Rust, and 40+ languages
|
|
198
|
+
- **Text Files**: TXT, LOG, CSV, JSON, YAML, XML, etc.
|
|
199
|
+
|
|
200
|
+
## 🎮 API Reference
|
|
201
|
+
|
|
202
|
+
### FilePreviewModal Props
|
|
203
|
+
|
|
204
|
+
| Prop | Type | Required | Description |
|
|
205
|
+
|------|------|----------|-------------|
|
|
206
|
+
| `files` | `PreviewFileInput[]` | ✅ | Array of files (supports File objects, file objects, or URL strings) |
|
|
207
|
+
| `currentIndex` | `number` | ✅ | Current file index |
|
|
208
|
+
| `isOpen` | `boolean` | ✅ | Whether the modal is open |
|
|
209
|
+
| `onClose` | `() => void` | ✅ | Close callback |
|
|
210
|
+
| `onNavigate` | `(index: number) => void` | ❌ | Navigation callback |
|
|
211
|
+
|
|
212
|
+
### File Type Definitions
|
|
213
|
+
|
|
214
|
+
```typescript
|
|
215
|
+
// Supports three types of file input
|
|
216
|
+
type PreviewFileInput = File | PreviewFileLink | string;
|
|
217
|
+
|
|
218
|
+
// 1. Native File object (Browser File API)
|
|
219
|
+
const file: File = ...;
|
|
220
|
+
|
|
221
|
+
// 2. File object
|
|
222
|
+
interface PreviewFileLink {
|
|
223
|
+
id?: string; // Optional unique identifier
|
|
224
|
+
name: string; // File name
|
|
225
|
+
type: string; // MIME type
|
|
226
|
+
url: string; // File URL (supports blob URLs and HTTP URLs)
|
|
227
|
+
size?: number; // File size in bytes
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// 3. HTTP URL string
|
|
231
|
+
const url: string = 'https://example.com/file.pdf';
|
|
232
|
+
```
|
|
233
|
+
|
|
234
|
+
### Usage Examples
|
|
235
|
+
|
|
236
|
+
```typescript
|
|
237
|
+
// Method 1: Using native File objects
|
|
238
|
+
const files = [file1, file2]; // Array of File objects
|
|
239
|
+
|
|
240
|
+
// Method 2: Using HTTP URL strings
|
|
241
|
+
const files = [
|
|
242
|
+
'https://example.com/image.jpg',
|
|
243
|
+
'https://example.com/document.pdf',
|
|
244
|
+
];
|
|
245
|
+
|
|
246
|
+
// Method 3: Using file objects
|
|
247
|
+
const files = [
|
|
248
|
+
{
|
|
249
|
+
name: 'presentation.pptx',
|
|
250
|
+
type: 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
|
|
251
|
+
url: '/path/to/presentation.pptx',
|
|
252
|
+
},
|
|
253
|
+
];
|
|
254
|
+
|
|
255
|
+
// Method 4: Mixed usage
|
|
256
|
+
const files = [
|
|
257
|
+
file1, // File object
|
|
258
|
+
'https://example.com/image.jpg', // URL string
|
|
259
|
+
{ name: 'doc.pdf', type: 'application/pdf', url: '/doc.pdf' }, // File object
|
|
260
|
+
];
|
|
261
|
+
```
|
|
262
|
+
|
|
263
|
+
### Supported MIME Types
|
|
264
|
+
|
|
265
|
+
#### Office Documents
|
|
266
|
+
- **Word**: `application/vnd.openxmlformats-officedocument.wordprocessingml.document` (.docx)
|
|
267
|
+
- **Excel**: `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` (.xlsx)
|
|
268
|
+
- **PowerPoint**: `application/vnd.openxmlformats-officedocument.presentationml.presentation` (.pptx)
|
|
269
|
+
- **PowerPoint (Legacy)**: `application/vnd.ms-powerpoint` (.ppt)
|
|
270
|
+
|
|
271
|
+
#### Other Documents
|
|
272
|
+
- **PDF**: `application/pdf`
|
|
273
|
+
|
|
274
|
+
#### Media Files
|
|
275
|
+
- **Images**: `image/jpeg`, `image/png`, `image/gif`, `image/webp`, `image/svg+xml`, etc.
|
|
276
|
+
- **Videos**: `video/mp4`, `video/webm`, `video/ogg`, etc.
|
|
277
|
+
- **Audio**: `audio/mpeg`, `audio/wav`, `audio/ogg`, etc.
|
|
278
|
+
|
|
279
|
+
#### Text Files
|
|
280
|
+
- **Markdown**: File extensions `.md` or `.markdown`
|
|
281
|
+
- **Code**: Auto-detected by file extension (`.js`, `.ts`, `.py`, `.java`, etc.)
|
|
282
|
+
- **Plain Text**: `text/plain`, `text/csv`, etc.
|
|
283
|
+
|
|
284
|
+
## 🎨 Custom Styling
|
|
285
|
+
|
|
286
|
+
The component is built with Tailwind CSS. You can customize styles by overriding CSS variables:
|
|
287
|
+
|
|
288
|
+
```css
|
|
289
|
+
/* Custom theme colors */
|
|
290
|
+
:root {
|
|
291
|
+
--primary-color: #8b5cf6;
|
|
292
|
+
--secondary-color: #ec4899;
|
|
293
|
+
}
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
## ⌨️ Keyboard Shortcuts
|
|
297
|
+
|
|
298
|
+
- `ESC` - Close preview
|
|
299
|
+
- `←` - Previous file
|
|
300
|
+
- `→` - Next file
|
|
301
|
+
- `Mouse Wheel` - Zoom image (image preview only)
|
|
302
|
+
|
|
303
|
+
## 📚 Documentation
|
|
304
|
+
|
|
305
|
+
- [Online Demo](https://wh131462.github.io/react-file-preview) - Live demo
|
|
306
|
+
|
|
307
|
+
## 📦 Package Information
|
|
308
|
+
|
|
309
|
+
### Bundle Size
|
|
310
|
+
|
|
311
|
+
- **ESM**: ~54 KB (gzipped: ~12 KB)
|
|
312
|
+
- **CJS**: ~37 KB (gzipped: ~11 KB)
|
|
313
|
+
- **CSS**: ~56 KB (gzipped: ~14 KB)
|
|
314
|
+
|
|
315
|
+
### Peer Dependencies
|
|
316
|
+
|
|
317
|
+
- `react`: ^18.0.0
|
|
318
|
+
- `react-dom`: ^18.0.0
|
|
319
|
+
|
|
320
|
+
### Exports
|
|
321
|
+
|
|
322
|
+
```json
|
|
323
|
+
{
|
|
324
|
+
".": {
|
|
325
|
+
"types": "./lib/index.d.ts",
|
|
326
|
+
"import": "./lib/index.mjs",
|
|
327
|
+
"require": "./lib/index.cjs"
|
|
328
|
+
},
|
|
329
|
+
"./style.css": "./lib/index.css"
|
|
330
|
+
}
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
## 🛠️ Development
|
|
334
|
+
|
|
335
|
+
### For Library Development
|
|
336
|
+
|
|
337
|
+
```bash
|
|
338
|
+
# Clone repository
|
|
339
|
+
git clone https://github.com/wh131462/react-file-preview.git
|
|
340
|
+
|
|
341
|
+
# Install dependencies
|
|
342
|
+
pnpm install
|
|
343
|
+
|
|
344
|
+
# Start dev server (demo app)
|
|
345
|
+
pnpm dev
|
|
346
|
+
|
|
347
|
+
# Build library (for npm)
|
|
348
|
+
pnpm build:lib
|
|
349
|
+
|
|
350
|
+
# Build demo app (for GitHub Pages)
|
|
351
|
+
pnpm build:demo
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
### Project Structure
|
|
355
|
+
|
|
356
|
+
```
|
|
357
|
+
react-file-preview/
|
|
358
|
+
├── src/
|
|
359
|
+
│ ├── index.ts # Library entry point
|
|
360
|
+
│ ├── FilePreviewModal.tsx # Main component
|
|
361
|
+
│ ├── types.ts # Type definitions
|
|
362
|
+
│ ├── utils/ # Utility functions
|
|
363
|
+
│ ├── renderers/ # File type renderers
|
|
364
|
+
│ ├── App.tsx # Demo app
|
|
365
|
+
│ └── main.tsx # Demo app entry
|
|
366
|
+
├── lib/ # Built library (npm package)
|
|
367
|
+
├── dist/ # Built demo app (GitHub Pages)
|
|
368
|
+
└── vite.config.lib.ts # Library build config
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
## 📄 License
|
|
372
|
+
|
|
373
|
+
[MIT](./LICENSE) © [EternalHeart](https://github.com/wh131462)
|
|
374
|
+
|
|
375
|
+
## 🤝 Contributing
|
|
376
|
+
|
|
377
|
+
Issues and Pull Requests are welcome!
|
|
378
|
+
|
|
379
|
+
## 🔗 Links
|
|
380
|
+
|
|
381
|
+
- [GitHub](https://github.com/wh131462/react-file-preview)
|
|
382
|
+
- [npm](https://www.npmjs.com/package/@eternalheart/react-file-preview)
|
|
383
|
+
- [Online Demo](https://wh131462.github.io/react-file-preview)
|
|
384
|
+
- [Issue Tracker](https://github.com/wh131462/react-file-preview/issues)
|