From ac4e3962df96905c1598caa9adf391a89fbcd06d Mon Sep 17 00:00:00 2001 From: John Factotum <50942278+johnfactotum@users.noreply.github.com> Date: Tue, 7 Mar 2023 06:46:28 +0000 Subject: [PATCH] Add method for getting TOC item for any location --- view.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/view.js b/view.js index 44bfc3e..bcec969 100644 --- a/view.js +++ b/view.js @@ -280,6 +280,20 @@ export class View { console.error(`Could not go to ${target}`) } } + async getTOCItemOf(target) { + try { + const { index, anchor } = await this.resolveNavigation(target) + const doc = await this.book.sections[index].createDocument() + const frag = anchor(doc) + const isRange = frag instanceof Range + const range = isRange ? frag : doc.createRange() + if (!isRange) range.selectNodeContents(frag) + return this.#tocProgress.getProgress(index, range) + } catch(e) { + console.error(e) + console.error(`Could not get ${target}`) + } + } goLeft() { return this.book.dir === 'rtl' ? this.renderer.next() : this.renderer.prev() }