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.
This commit is contained in:
John Factotum
2023-04-01 23:33:10 +08:00
parent 34973ac032
commit c173f5b523
+12 -4
View File
@@ -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()