From c173f5b523d6c0491d73f714183c3af0916a1155 Mon Sep 17 00:00:00 2001 From: John Factotum <50942278+johnfactotum@users.noreply.github.com> Date: Sat, 1 Apr 2023 23:33:10 +0800 Subject: [PATCH] Paginator: allow injecting CSS *before* the book's stylesheet Before this commit: `setStyle()` takes a single string argument, which is the stylesheet it will append to head. After this commit: it optionally accepts two stylesheet strings in an array, the first of which will be prepended, allowing the reading system to set default styles that can be overridden by the book. --- paginator.js | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/paginator.js b/paginator.js index 14860a1..143dde9 100644 --- a/paginator.js +++ b/paginator.js @@ -623,9 +623,11 @@ export class Paginator { const view = this.#createView() const afterLoad = doc => { if (doc.head) { + const $styleBefore = doc.createElement('style') + doc.head.prepend($styleBefore) const $style = doc.createElement('style') doc.head.append($style) - this.#styleMap.set(doc, $style) + this.#styleMap.set(doc, [$styleBefore, $style]) } onLoad?.(doc, index) } @@ -735,9 +737,15 @@ export class Paginator { doc: this.#view.document, } } - setStyle(style) { - const $style = this.#styleMap.get(this.#view?.document) - if ($style) $style.textContent = style + setStyle(styles) { + const $$styles = this.#styleMap.get(this.#view?.document) + if (!$$styles) return + const [$beforeStyle, $style] = $$styles + if (Array.isArray(styles)) { + const [beforeStyle, style] = styles + $beforeStyle.textContent = beforeStyle + $style.textContent = style + } else $style.textContent = styles } deselect() { const sel = this.#view.document.defaultView.getSelection()