-
Notifications
You must be signed in to change notification settings - Fork 1
feat: add export to PDF #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| import type { Editor } from "@tiptap/react"; | ||
| import html2pdf from "html2pdf.js"; | ||
|
Comment on lines
+1
to
+2
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Enterprise Quality] 🟠
|
||
|
|
||
| const EDITOR_STYLES = ` | ||
| .pdf-export-container { | ||
| font-family: Arial, sans-serif; | ||
| font-size: 11pt; | ||
| line-height: 1.5; | ||
| color: #000; | ||
| } | ||
| .pdf-export-container ul { | ||
| list-style-type: disc; | ||
| padding-left: 1.5em; | ||
| margin: 0.5em 0; | ||
| } | ||
| .pdf-export-container ol { | ||
| list-style-type: decimal; | ||
| padding-left: 1.5em; | ||
| margin: 0.5em 0; | ||
| } | ||
| .pdf-export-container ul ul { | ||
| list-style-type: circle; | ||
| } | ||
| .pdf-export-container ul ul ul { | ||
| list-style-type: square; | ||
| } | ||
| .pdf-export-container li { | ||
| margin: 0.2em 0; | ||
| } | ||
| .pdf-export-container a { | ||
| color: #1a73e8; | ||
| text-decoration: underline; | ||
| } | ||
| .pdf-export-container img { | ||
| max-width: 100%; | ||
| height: auto; | ||
| } | ||
| .pdf-export-container hr { | ||
| border: none; | ||
| border-top: 1px solid #dadce0; | ||
| margin: 1em 0; | ||
|
Comment on lines
+4
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Code Quality] 🟠
|
||
| } | ||
| `; | ||
|
|
||
| export function exportToPdf(editor: Editor): void { | ||
| const editorHtml = editor.getHTML(); | ||
|
|
||
| const wrapper = document.createElement("div"); | ||
|
|
||
| const styleElement = document.createElement("style"); | ||
| styleElement.textContent = EDITOR_STYLES; | ||
| wrapper.appendChild(styleElement); | ||
|
|
||
| const content = document.createElement("div"); | ||
| content.className = "pdf-export-container"; | ||
| content.innerHTML = editorHtml; | ||
| wrapper.appendChild(content); | ||
|
|
||
| const options = { | ||
| margin: [0.5, 1, 0.5, 1] as [number, number, number, number], | ||
| filename: "document.pdf", | ||
| image: { type: "jpeg" as const, quality: 0.98 }, | ||
| html2canvas: { scale: 2, useCORS: true }, | ||
| jsPDF: { unit: "in", format: "letter", orientation: "portrait" as const }, | ||
| }; | ||
|
|
||
| html2pdf().set(options).from(wrapper).save(); | ||
|
Comment on lines
+45
to
+67
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Correctness] 🟠 [Code Quality] 🟠
[Enterprise Quality] 🟠
[Correctness] 🟠 [Correctness] 🟠 [Enterprise Quality] 🟠 The PDF output is rasterized through |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Enterprise Quality] 🟠 The new export control inherits mouse-only activation from
ToolbarButton: the action is bound toonMouseDown, but keyboard activation of a native button dispatchesclick, notmousedown. That means Enter/Space cannot trigger export, and the icon-only button still relies ontitleinstead of a stable programmatic label; wire activation throughonClickand addaria-label={title}while keepingonMouseDownonly for focus preservation.