1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-04 15:09:16 +02:00

Implement serialized themes

This commit is contained in:
Benjamin Pick 2018-12-19 09:54:27 +01:00
parent f142a0b24a
commit a7cd4f6791
2 changed files with 39 additions and 11 deletions

View file

@ -676,6 +676,31 @@ class Contents {
}.bind(this));
}
_getStylesheetNode() {
var styleEl;
var key = "epubjs-inserted-css";
// Check if link already exists
styleEl = this.document.getElementById("#"+key);
if (!styleEl) {
styleEl = this.document.createElement("style");
styleEl.id = key;
// Append style element to head
this.document.head.appendChild(styleEl);
}
return styleEl;
}
/**
* Append stylesheet css
* @param {string} serializedCss
*/
addStylesheetSerialized(serializedCss) {
var styleEl;
styleEl = this._getStylesheetNode();
styleEl.innerText = serializedCss;
}
/**
* Append stylesheet rules to a generate stylesheet
* Array: https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/insertRule
@ -685,19 +710,10 @@ class Contents {
addStylesheetRules(rules) {
var styleEl;
var styleSheet;
var key = "epubjs-inserted-css";
if(!this.document || !rules || rules.length === 0) return;
// Check if link already exists
styleEl = this.document.getElementById("#"+key);
if (!styleEl) {
styleEl = this.document.createElement("style");
styleEl.id = key;
}
// Append style element to head
this.document.head.appendChild(styleEl);
styleEl = this._getStylesheetNode();
// Grab style sheet
styleSheet = styleEl.sheet;

View file

@ -82,6 +82,18 @@ class Themes {
}
}
/**
* Register a theme by passing its css as string
* @param {string} name
* @param {string} css
*/
registerCss (name, css) {
this._themes[name] = { "serialized" : css };
if (this._injected[name]) {
this.update(name);
}
}
/**
* Register a url
* @param {string} name
@ -176,7 +188,7 @@ class Themes {
if (theme.url) {
contents.addStylesheet(theme.url);
} else if (theme.serialized) {
// TODO: handle serialized
contents.addStylesheetSerialized(theme.serialized);
} else if (theme.rules) {
contents.addStylesheetRules(theme.rules);
theme.injected = true;