-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* fix: automatic page count for small screens * fix: automatic footer positioning for print * feat: use paged.js for print layout
- Loading branch information
Showing
5 changed files
with
100 additions
and
92 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
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,51 @@ | ||
async function initializePagedJS() { | ||
await Promise.all([ | ||
import("https://unpkg.com/[email protected]/dist/paged.min.js"), | ||
new Promise((resolve) => { | ||
if (document.readyState !== "loading") resolve(); | ||
document.addEventListener("DOMContentLoaded", () => resolve()); | ||
}), | ||
]); | ||
|
||
// Workaround for using custom properties with @page rules. For background on why this is an | ||
// issue, see: https://stackoverflow.com/a/44738574 | ||
const referencePage = document.createElement("div"); | ||
referencePage.style.height = "var(--page-height)"; | ||
referencePage.style.width = "var(--page-width)"; | ||
referencePage.style.margin = "var(--page-margin)"; | ||
referencePage.style.position = "absolute"; | ||
referencePage.style.top = "-9999px"; | ||
document.body.appendChild(referencePage); | ||
const referencePageStyles = window.getComputedStyle(referencePage); | ||
const style = document.createElement("style"); | ||
style.innerHTML = `@page { | ||
${Object.entries({ | ||
size: `${referencePageStyles.width} ${referencePageStyles.height}`, | ||
margin: referencePageStyles.margin, | ||
}).reduce((acc, [key, value]) => `${acc}${key}: ${value};\n`, "")} | ||
}`; | ||
document.head.appendChild(style); | ||
document.body.removeChild(referencePage); | ||
|
||
const previewer = new PagedModule.Previewer(); | ||
await previewer.preview(); | ||
} | ||
|
||
function showPrintPage() { | ||
const url = new URL(window.location.href); | ||
url.searchParams.set("print", "true"); | ||
history.pushState({}, "", url); | ||
initializePagedJS(); | ||
} | ||
|
||
if (new URL(window.location.href).searchParams.has("print")) { | ||
initializePagedJS(); | ||
} else { | ||
window.onbeforeprint = showPrintPage; | ||
} | ||
|
||
window.onafterprint = () => { | ||
const url = new URL(window.location.href); | ||
url.searchParams.delete("print"); | ||
window.location.href = url.href; | ||
}; |
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