Paginator: fix focus lost when loading new section

Also add method for setting focus on the content window
This commit is contained in:
John Factotum
2024-09-15 13:13:44 +08:00
parent c17b542b5c
commit e6937eed15
+5
View File
@@ -866,6 +866,7 @@ export class Paginator extends HTMLElement {
async #display(promise) { async #display(promise) {
const { index, src, anchor, onLoad, select } = await promise const { index, src, anchor, onLoad, select } = await promise
this.#index = index this.#index = index
const hasFocus = this.#view?.document?.hasFocus()
if (src) { if (src) {
const view = this.#createView() const view = this.#createView()
const afterLoad = doc => { const afterLoad = doc => {
@@ -890,6 +891,7 @@ export class Paginator extends HTMLElement {
} }
await this.scrollToAnchor((typeof anchor === 'function' await this.scrollToAnchor((typeof anchor === 'function'
? anchor(this.#view.document) : anchor) ?? 0, select) ? anchor(this.#view.document) : anchor) ?? 0, select)
if (hasFocus) this.focusView()
} }
#canGoToIndex(index) { #canGoToIndex(index) {
return index >= 0 && index <= this.sections.length - 1 return index >= 0 && index <= this.sections.length - 1
@@ -1006,6 +1008,9 @@ export class Paginator extends HTMLElement {
// needed because the resize observer doesn't work in Firefox // needed because the resize observer doesn't work in Firefox
this.#view?.document?.fonts?.ready?.then(() => this.#view.expand()) this.#view?.document?.fonts?.ready?.then(() => this.#view.expand())
} }
focusView() {
this.#view.document.defaultView.focus()
}
destroy() { destroy() {
this.#observer.unobserve(this) this.#observer.unobserve(this)
this.#view.destroy() this.#view.destroy()