-
-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update output attachment extention
Co-authored-by: hunghg255 <[email protected]>
- Loading branch information
Showing
3 changed files
with
106 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 52 additions & 23 deletions
75
src/extensions/Attachment/components/NodeViewAttachment/FileIcon.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,67 @@ | ||
import { LucideAudioLines, LucideFile, LucideImage, LucideSheet, LucideTableProperties, LucideVideo } from 'lucide-react' | ||
import ReactDOMServer from 'react-dom/server' | ||
import { normalizeFileType } from '@/utils/file' | ||
import { ExportPdf } from '@/components/icons/ExportPdf' | ||
import ExportWord from '@/components/icons/ExportWord' | ||
|
||
export function getFileTypeIcon(fileType: string) { | ||
const type = normalizeFileType(fileType) | ||
function iconToProseMirror(icon: JSX.Element) { | ||
// Render SVG as a static string | ||
const svgString = ReactDOMServer.renderToStaticMarkup(icon) | ||
|
||
switch (type) { | ||
case 'audio': | ||
return <LucideAudioLines /> | ||
// Parse the string into ProseMirror-compatible structure | ||
const parser = new DOMParser() | ||
const svgDocument = parser.parseFromString(svgString, 'image/svg+xml') | ||
const svgElement = svgDocument.documentElement | ||
|
||
case 'video': | ||
return <LucideVideo /> | ||
const iconToReturn = [ | ||
'svg', | ||
{ | ||
...Array.from(svgElement.attributes).reduce((acc: any, attr: any) => { | ||
acc[attr.name] = attr.value | ||
return acc | ||
}, {}), | ||
}, | ||
] | ||
|
||
case 'file': | ||
return <LucideFile /> | ||
Array.from(svgElement.childNodes).forEach((child: any) => { | ||
if (child.nodeType === 1) { | ||
// Element node | ||
const childElement = [ | ||
child.tagName.toLowerCase(), | ||
Array.from(child.attributes).reduce((acc: any, attr: any) => { | ||
acc[attr.name] = attr.value | ||
return acc | ||
}, {}), | ||
] | ||
|
||
case 'image': | ||
return <LucideImage /> | ||
if (child.textContent) { | ||
childElement.push(child.textContent) | ||
} | ||
|
||
case 'pdf': | ||
return <ExportPdf /> | ||
iconToReturn.push(childElement) | ||
} | ||
}) | ||
|
||
case 'word': | ||
return <ExportWord /> | ||
return iconToReturn | ||
} | ||
|
||
case 'excel': | ||
return <LucideSheet /> | ||
// React components for rendering directly in JSX | ||
const icons = { | ||
audio: <LucideAudioLines />, | ||
video: <LucideVideo />, | ||
file: <LucideFile />, | ||
image: <LucideImage />, | ||
pdf: <ExportPdf />, | ||
word: <ExportWord />, | ||
excel: <LucideSheet />, | ||
ppt: <LucideTableProperties />, | ||
} | ||
|
||
case 'ppt': | ||
return <LucideTableProperties /> | ||
export function getFileTypeIcon(fileType: string, forProseMirror = false) { | ||
const type = normalizeFileType(fileType) | ||
|
||
default: { | ||
return <></> | ||
} | ||
} | ||
const icon = icons[type] || <></> | ||
|
||
// Return ProseMirror-compatible structure or React component | ||
return forProseMirror ? iconToProseMirror(icon) : icon | ||
} |
16 changes: 15 additions & 1 deletion
16
src/extensions/Attachment/components/NodeViewAttachment/index.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,23 @@ | ||
.wrap { | ||
.attachment, /* for browser ready HTML */ | ||
.wrap { /* for NodeView */ | ||
border-width: 1px !important; | ||
border-radius: 4px; | ||
padding: 10px; | ||
display: flex; | ||
align-items: center; | ||
justify-content: space-between; | ||
margin: 10px 0; | ||
|
||
:global { | ||
.attachment__icon { | ||
width: 32px; | ||
text-align: center; | ||
} | ||
|
||
.attachment__icon svg { | ||
width: 32px; | ||
display: inline-block; | ||
} | ||
} | ||
} | ||
|