Skip to content

Commit

Permalink
Hotfix printing italicized text in Chrome
Browse files Browse the repository at this point in the history
If browser is Chrome, and version < 82, then remove all italics on page before printing.
See #38 for further context.
  • Loading branch information
seshrs committed Feb 1, 2020
1 parent 915d64b commit 498699d
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.1.0
1.1.1
4 changes: 2 additions & 2 deletions assets/js/primer_spec_plugin.min.js

Large diffs are not rendered by default.

25 changes: 25 additions & 0 deletions src_js/NodeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,29 @@ export default class NodeManager {
});
}

/**
* HACK: Toggles italics in Chrome before printing.
* (See issue eecs485staff/primer-spec#38)
* @param isItalicsEnabled boolean indicating whether italics should be enabled
*/
_toggleItalicsInChrome(isItalicsEnabled: boolean) {
const chromeVersion = Utilities.getChromeVersion();
if (chromeVersion === false || chromeVersion >= 82) {
return;
}

const all_italic_els =
'em, dfn, .text-italic, dt, .highlight .cm, .highlight .c1, ' +
'.highlight .cs, .highlight .cd, .highlight .ge, .primer-spec-toc-h4';
const font_style = isItalicsEnabled ? 'italic' : 'inherit';

const nodes: NodeListOf<HTMLElement> =
document.querySelectorAll(all_italic_els);
Array.from(nodes).map(el => {
el.style.fontStyle = font_style;
});
}

/**
* Register handler when page is previewed for printing. The handler hides
* the sidebar and settings panes and restores them after the print preview
Expand All @@ -111,6 +134,7 @@ export default class NodeManager {
should_undo_settings_toggle = true;
this.settings.toggle();
}
this._toggleItalicsInChrome(false);
};
const afterPrint = () => {
if (should_undo_sidebar_toggle) {
Expand All @@ -121,6 +145,7 @@ export default class NodeManager {
should_undo_settings_toggle = false;
this.settings.toggle();
}
this._toggleItalicsInChrome(true);
};
// Safari doesn't support onbeforeprint, etc.
// So this is the "official" work-around for webkit.
Expand Down
10 changes: 10 additions & 0 deletions src_js/Utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,14 @@ export default class Utilities {
static isSmallScreen() {
return document.documentElement.clientWidth < 900;
}

/**
* Return a number indicating the Chrome major version. Returns false if
* not Chrome.
*/
static getChromeVersion () {
// Based on: https://stackoverflow.com/a/4900484/5868796
const raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
return raw ? parseInt(raw[2], 10) : false;
}
}

0 comments on commit 498699d

Please sign in to comment.