Skip to content
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

support a separate print stylesheet #574

Merged
merged 4 commits into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions css/elements.css
Original file line number Diff line number Diff line change
Expand Up @@ -1368,12 +1368,6 @@ li.menu-search-result-term:before {
padding-right: 5px;
}

@media print {
#menu-toggle {
display: none;
}
}

[normative-optional],
[deprecated],
[legacy] {
Expand Down
3 changes: 3 additions & 0 deletions css/print.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#menu-toggle {
display: none;
}
12 changes: 11 additions & 1 deletion src/Spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1035,6 +1035,7 @@ ${await utils.readFile(path.join(__dirname, '../js/multipage.js'))}
}

let cssContents = await utils.readFile(path.join(__dirname, '../css/elements.css'));
const printCssContents = await utils.readFile(path.join(__dirname, '../css/print.css'));

const FONT_FILE_CONTENTS = new Map(
zip(
Expand Down Expand Up @@ -1072,9 +1073,11 @@ ${await utils.readFile(path.join(__dirname, '../js/multipage.js'))}

const scriptLocationOnDisk = path.join(this.assets.directory, 'ecmarkup.js');
const styleLocationOnDisk = path.join(this.assets.directory, 'ecmarkup.css');
const printStyleLocationOnDisk = path.join(this.assets.directory, 'print.css');

this.generatedFiles.set(scriptLocationOnDisk, jsContents);
this.generatedFiles.set(styleLocationOnDisk, cssContents);
this.generatedFiles.set(printStyleLocationOnDisk, printCssContents);
for (const [, fontFile] of FONT_FILES) {
this.generatedFiles.set(
path.join(this.assets.directory, fontFile),
Expand All @@ -1087,6 +1090,7 @@ ${await utils.readFile(path.join(__dirname, '../js/multipage.js'))}
script.setAttribute('defer', '');
this.doc.head.appendChild(script);

this.addStyle(this.doc.head, path.relative(outDir, printStyleLocationOnDisk), 'print');
this.addStyle(this.doc.head, path.relative(outDir, styleLocationOnDisk));
} else {
// i.e. assets.type === 'inline'
Expand All @@ -1099,6 +1103,9 @@ ${await utils.readFile(path.join(__dirname, '../js/multipage.js'))}
const style = this.doc.createElement('style');
style.textContent = cssContents;
this.doc.head.appendChild(style);
const printStyle = this.doc.createElement('style');
printStyle.textContent = `@media print {\n${printCssContents}\n}`;
this.doc.head.appendChild(printStyle);
}
this.addStyle(
this.doc.head,
Expand All @@ -1108,10 +1115,13 @@ ${await utils.readFile(path.join(__dirname, '../js/multipage.js'))}
);
}

private addStyle(head: HTMLHeadElement, href: string) {
private addStyle(head: HTMLHeadElement, href: string, media?: 'all' | 'print' | 'screen') {
const style = this.doc.createElement('link');
style.setAttribute('rel', 'stylesheet');
style.setAttribute('href', href);
if (media != null) {
style.setAttribute('media', media);
}

// insert early so that the document's own stylesheets can override
const firstLink = head.querySelector('link[rel=stylesheet], style');
Expand Down
Loading