-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
may download document with translation (#1220)
- Loading branch information
Showing
9 changed files
with
187 additions
and
46 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
69 changes: 69 additions & 0 deletions
69
enjoy/src/renderer/components/documents/document-actions-button.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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { | ||
Button, | ||
DropdownMenu, | ||
DropdownMenuItem, | ||
DropdownMenuContent, | ||
DropdownMenuTrigger, | ||
toast, | ||
} from "@renderer/components/ui"; | ||
import { MoreVerticalIcon } from "lucide-react"; | ||
import { t } from "i18next"; | ||
import { useContext } from "react"; | ||
import { | ||
AppSettingsProviderContext, | ||
DocumentProviderContext, | ||
} from "@renderer/context"; | ||
import template from "./document.template.html?raw"; | ||
|
||
export const DocumentActionsButton = (props: { document: DocumentEType }) => { | ||
const { document } = props; | ||
const { EnjoyApp } = useContext(AppSettingsProviderContext); | ||
const { ref, section } = useContext(DocumentProviderContext); | ||
|
||
const handlePrint = async () => { | ||
if (!ref.current) return; | ||
|
||
const content = template.replace("$title", document.title).replace( | ||
"$content", | ||
Array.from(ref.current.querySelectorAll(".segment, .translation")) | ||
.map((segment) => { | ||
const tagName = segment.tagName.toLowerCase(); | ||
if (segment.classList.contains("translation")) { | ||
return `<${tagName}>${segment.textContent}</${tagName}>`; | ||
} | ||
return `<${tagName}>${ | ||
segment.querySelector(".segment-content")?.textContent | ||
}</${tagName}>`; | ||
}) | ||
.join("") | ||
); | ||
|
||
try { | ||
const savePath = await EnjoyApp.dialog.showSaveDialog({ | ||
title: t("print"), | ||
defaultPath: `${document.title}(S${section}).pdf`, | ||
}); | ||
|
||
if (!savePath) return; | ||
|
||
await EnjoyApp.download.printAsPdf(content, savePath); | ||
|
||
toast.success(t("downloadedSuccessfully")); | ||
} catch (err) { | ||
toast.error(`${t("downloadFailed")}: ${err.message}`); | ||
} | ||
}; | ||
|
||
return ( | ||
<DropdownMenu> | ||
<DropdownMenuTrigger asChild> | ||
<Button variant="ghost" size="icon" className="w-6 h-6"> | ||
<MoreVerticalIcon className="size-4" /> | ||
</Button> | ||
</DropdownMenuTrigger> | ||
<DropdownMenuContent side="bottom" align="start"> | ||
<DropdownMenuItem onClick={handlePrint}>{t("print")}</DropdownMenuItem> | ||
</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
40 changes: 40 additions & 0 deletions
40
enjoy/src/renderer/components/documents/document.template.html
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,40 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width initial-scale=1.0" /> | ||
<title>$title</title> | ||
<style> | ||
body { | ||
font-family: ui-serif, Georgia, Cambria, "Times New Roman", Times, serif; | ||
} | ||
|
||
h1 { | ||
font-size: 24px; | ||
font-weight: 600; | ||
text-align: center; | ||
margin-bottom: 24px; | ||
} | ||
|
||
h2 { | ||
font-size: 20px; | ||
font-weight: 600; | ||
margin-bottom: 16px; | ||
} | ||
|
||
h3 { | ||
font-size: 18px; | ||
font-weight: 600; | ||
margin-bottom: 16px; | ||
} | ||
|
||
p { | ||
font-size: 16px; | ||
margin-bottom: 16px; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div class="content">$content</div> | ||
</body> | ||
</html> |
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.