2021-07-23 12:05:21 +02:00
|
|
|
import * as fs from 'fs-extra';
|
2019-12-13 02:40:58 +02:00
|
|
|
|
2021-07-23 12:05:21 +02:00
|
|
|
export const loadCustomCss = async (filePath: string) => {
|
2019-12-13 02:40:58 +02:00
|
|
|
let cssString = '';
|
|
|
|
if (await fs.pathExists(filePath)) {
|
|
|
|
try {
|
|
|
|
cssString = await fs.readFile(filePath, 'utf-8');
|
|
|
|
} catch (error) {
|
|
|
|
let msg = error.message ? error.message : '';
|
|
|
|
msg = `Could not load custom css from ${filePath}\n${msg}`;
|
|
|
|
error.message = msg;
|
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cssString;
|
|
|
|
};
|
|
|
|
|
2021-07-23 12:05:21 +02:00
|
|
|
export const injectCustomStyles = async (elementId: string, cssFilePath: string) => {
|
2019-12-13 02:40:58 +02:00
|
|
|
const css = await loadCustomCss(cssFilePath);
|
|
|
|
const styleTag = document.createElement('style');
|
2021-07-23 12:05:21 +02:00
|
|
|
styleTag.setAttribute('id', elementId);
|
|
|
|
styleTag.setAttribute('type', 'text/css');
|
2019-12-13 02:40:58 +02:00
|
|
|
styleTag.appendChild(document.createTextNode(css));
|
|
|
|
document.head.appendChild(styleTag);
|
|
|
|
};
|