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:
parent
f142a0b24a
commit
a7cd4f6791
2 changed files with 39 additions and 11 deletions
|
@ -676,6 +676,31 @@ class Contents {
|
||||||
}.bind(this));
|
}.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
|
* Append stylesheet rules to a generate stylesheet
|
||||||
* Array: https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/insertRule
|
* Array: https://developer.mozilla.org/en-US/docs/Web/API/CSSStyleSheet/insertRule
|
||||||
|
@ -685,19 +710,10 @@ class Contents {
|
||||||
addStylesheetRules(rules) {
|
addStylesheetRules(rules) {
|
||||||
var styleEl;
|
var styleEl;
|
||||||
var styleSheet;
|
var styleSheet;
|
||||||
var key = "epubjs-inserted-css";
|
|
||||||
|
|
||||||
if(!this.document || !rules || rules.length === 0) return;
|
if(!this.document || !rules || rules.length === 0) return;
|
||||||
|
|
||||||
// Check if link already exists
|
styleEl = this._getStylesheetNode();
|
||||||
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);
|
|
||||||
|
|
||||||
// Grab style sheet
|
// Grab style sheet
|
||||||
styleSheet = styleEl.sheet;
|
styleSheet = styleEl.sheet;
|
||||||
|
|
|
@ -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
|
* Register a url
|
||||||
* @param {string} name
|
* @param {string} name
|
||||||
|
@ -176,7 +188,7 @@ class Themes {
|
||||||
if (theme.url) {
|
if (theme.url) {
|
||||||
contents.addStylesheet(theme.url);
|
contents.addStylesheet(theme.url);
|
||||||
} else if (theme.serialized) {
|
} else if (theme.serialized) {
|
||||||
// TODO: handle serialized
|
contents.addStylesheetSerialized(theme.serialized);
|
||||||
} else if (theme.rules) {
|
} else if (theme.rules) {
|
||||||
contents.addStylesheetRules(theme.rules);
|
contents.addStylesheetRules(theme.rules);
|
||||||
theme.injected = true;
|
theme.injected = true;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue