-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
185 additions
and
9 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
'use client'; | ||
|
||
import React from 'react'; | ||
|
||
import type { DropdownMenuProps } from '@radix-ui/react-dropdown-menu'; | ||
|
||
import { toDOMNode, useEditorRef } from '@udecode/plate-common/react'; | ||
import { ArrowDownToLineIcon } from 'lucide-react'; | ||
|
||
import { | ||
DropdownMenu, | ||
DropdownMenuContent, | ||
DropdownMenuGroup, | ||
DropdownMenuItem, | ||
DropdownMenuTrigger, | ||
useOpenState, | ||
} from './dropdown-menu'; | ||
import { ToolbarButton } from './toolbar'; | ||
|
||
export function ExportToolbarButton({ children, ...props }: DropdownMenuProps) { | ||
const editor = useEditorRef(); | ||
const openState = useOpenState(); | ||
|
||
const getCanvas = async () => { | ||
const { default: html2canvas } = await import('html2canvas'); | ||
|
||
const style = document.createElement('style'); | ||
document.head.append(style); | ||
style.sheet?.insertRule( | ||
'body > div:last-child img { display: inline-block !important; }' | ||
); | ||
|
||
const canvas = await html2canvas(toDOMNode(editor, editor)!); | ||
style.remove(); | ||
|
||
return canvas; | ||
}; | ||
|
||
const downloadFile = (href: string, filename: string) => { | ||
const element = document.createElement('a'); | ||
element.setAttribute('href', href); | ||
element.setAttribute('download', filename); | ||
element.style.display = 'none'; | ||
document.body.append(element); | ||
element.click(); | ||
element.remove(); | ||
}; | ||
|
||
const exportToPdf = async () => { | ||
const canvas = await getCanvas(); | ||
|
||
const PDFLib = await import('pdf-lib'); | ||
const pdfDoc = await PDFLib.PDFDocument.create(); | ||
const page = pdfDoc.addPage([canvas.width, canvas.height]); | ||
const imageEmbed = await pdfDoc.embedPng(canvas.toDataURL('PNG')); | ||
const { height, width } = imageEmbed.scale(1); | ||
page.drawImage(imageEmbed, { | ||
height, | ||
width, | ||
x: 0, | ||
y: 0, | ||
}); | ||
const pdfBase64 = await pdfDoc.saveAsBase64({ dataUri: true }); | ||
|
||
downloadFile(pdfBase64, 'plate.pdf'); | ||
}; | ||
|
||
const exportToImage = async () => { | ||
const canvas = await getCanvas(); | ||
downloadFile(canvas.toDataURL('image/png'), 'plate.png'); | ||
}; | ||
|
||
return ( | ||
<DropdownMenu modal={false} {...openState} {...props}> | ||
<DropdownMenuTrigger asChild> | ||
<ToolbarButton pressed={openState.open} tooltip="Export" isDropdown> | ||
<ArrowDownToLineIcon className="size-4" /> | ||
</ToolbarButton> | ||
</DropdownMenuTrigger> | ||
|
||
<DropdownMenuContent align="start"> | ||
<DropdownMenuGroup> | ||
<DropdownMenuItem onSelect={exportToPdf}> | ||
Export as PDF | ||
</DropdownMenuItem> | ||
<DropdownMenuItem onSelect={exportToImage}> | ||
Export as Image | ||
</DropdownMenuItem> | ||
</DropdownMenuGroup> | ||
</DropdownMenuContent> | ||
</DropdownMenu> | ||
); | ||
} |
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
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
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
Oops, something went wrong.