Skip to content

Commit

Permalink
Fix apply-font for multiple font pickers (closes #7)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelmeuli committed May 11, 2019
1 parent ad62603 commit 9e440e3
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions src/font-manager/styles/declarations.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import getFontId from "../../shared/fontId";
import { Font } from "../../shared/types";

const activeFontStylesheet = document.createElement("style");
const previewFontsStylesheet = document.createElement("style");
document.head.appendChild(activeFontStylesheet);
document.head.appendChild(previewFontsStylesheet);

/**
Expand All @@ -19,6 +17,20 @@ export function applyFontPreview(previewFont: Font, selectorSuffix: string): voi
previewFontsStylesheet.appendChild(document.createTextNode(style));
}

/**
* Create/find and return the apply-font stylesheet for the provided selectorSuffix
*/
function getActiveFontStylesheet(selectorSuffix: string) {
const stylesheetId = `active-font-${selectorSuffix}`;
let activeFontStylesheet = document.getElementById(stylesheetId);
if (!activeFontStylesheet) {
activeFontStylesheet = document.createElement("style");
activeFontStylesheet.id = stylesheetId;
document.head.appendChild(activeFontStylesheet);
}
return activeFontStylesheet;
}

/**
* Add/update declaration for applying the current active font
*/
Expand All @@ -32,10 +44,6 @@ export function applyActiveFont(
font-family: "${activeFont.family}"${previousFontFamily ? `, "${previousFontFamily}"` : ""};
}
`;
const styleNode = document.createTextNode(style);
if (activeFontStylesheet.childNodes.length === 0) {
activeFontStylesheet.appendChild(styleNode);
} else {
activeFontStylesheet.replaceChild(styleNode, activeFontStylesheet.childNodes[0]);
}
const activeFontStylesheet = getActiveFontStylesheet(selectorSuffix);
activeFontStylesheet.innerHTML = style;
}

0 comments on commit 9e440e3

Please sign in to comment.