|
1 | 1 | import { LucideAudioLines, LucideFile, LucideImage, LucideSheet, LucideTableProperties, LucideVideo } from 'lucide-react'
|
| 2 | +import ReactDOMServer from 'react-dom/server' |
2 | 3 | import { normalizeFileType } from '@/utils/file'
|
3 | 4 | import { ExportPdf } from '@/components/icons/ExportPdf'
|
4 | 5 | import ExportWord from '@/components/icons/ExportWord'
|
5 | 6 |
|
6 |
| -export function getFileTypeIcon(fileType: string) { |
7 |
| - const type = normalizeFileType(fileType) |
| 7 | +function iconToProseMirror(icon: JSX.Element) { |
| 8 | + // Render SVG as a static string |
| 9 | + const svgString = ReactDOMServer.renderToStaticMarkup(icon) |
8 | 10 |
|
9 |
| - switch (type) { |
10 |
| - case 'audio': |
11 |
| - return <LucideAudioLines /> |
| 11 | + // Parse the string into ProseMirror-compatible structure |
| 12 | + const parser = new DOMParser() |
| 13 | + const svgDocument = parser.parseFromString(svgString, 'image/svg+xml') |
| 14 | + const svgElement = svgDocument.documentElement |
12 | 15 |
|
13 |
| - case 'video': |
14 |
| - return <LucideVideo /> |
| 16 | + const iconToReturn = [ |
| 17 | + 'svg', |
| 18 | + { |
| 19 | + ...Array.from(svgElement.attributes).reduce((acc: any, attr: any) => { |
| 20 | + acc[attr.name] = attr.value |
| 21 | + return acc |
| 22 | + }, {}), |
| 23 | + }, |
| 24 | + ] |
15 | 25 |
|
16 |
| - case 'file': |
17 |
| - return <LucideFile /> |
| 26 | + Array.from(svgElement.childNodes).forEach((child: any) => { |
| 27 | + if (child.nodeType === 1) { |
| 28 | + // Element node |
| 29 | + const childElement = [ |
| 30 | + child.tagName.toLowerCase(), |
| 31 | + Array.from(child.attributes).reduce((acc: any, attr: any) => { |
| 32 | + acc[attr.name] = attr.value |
| 33 | + return acc |
| 34 | + }, {}), |
| 35 | + ] |
18 | 36 |
|
19 |
| - case 'image': |
20 |
| - return <LucideImage /> |
| 37 | + if (child.textContent) { |
| 38 | + childElement.push(child.textContent) |
| 39 | + } |
21 | 40 |
|
22 |
| - case 'pdf': |
23 |
| - return <ExportPdf /> |
| 41 | + iconToReturn.push(childElement) |
| 42 | + } |
| 43 | + }) |
24 | 44 |
|
25 |
| - case 'word': |
26 |
| - return <ExportWord /> |
| 45 | + return iconToReturn |
| 46 | +} |
27 | 47 |
|
28 |
| - case 'excel': |
29 |
| - return <LucideSheet /> |
| 48 | +// React components for rendering directly in JSX |
| 49 | +const icons = { |
| 50 | + audio: <LucideAudioLines />, |
| 51 | + video: <LucideVideo />, |
| 52 | + file: <LucideFile />, |
| 53 | + image: <LucideImage />, |
| 54 | + pdf: <ExportPdf />, |
| 55 | + word: <ExportWord />, |
| 56 | + excel: <LucideSheet />, |
| 57 | + ppt: <LucideTableProperties />, |
| 58 | +} |
30 | 59 |
|
31 |
| - case 'ppt': |
32 |
| - return <LucideTableProperties /> |
| 60 | +export function getFileTypeIcon(fileType: string, forProseMirror = false) { |
| 61 | + const type = normalizeFileType(fileType) |
33 | 62 |
|
34 |
| - default: { |
35 |
| - return <></> |
36 |
| - } |
37 |
| - } |
| 63 | + const icon = icons[type] || <></> |
| 64 | + |
| 65 | + // Return ProseMirror-compatible structure or React component |
| 66 | + return forProseMirror ? iconToProseMirror(icon) : icon |
38 | 67 | }
|
0 commit comments