Add function to convert current page to SSML, with `<mark>`s inserted
using `Intl.segmenter`.

The actual speaking is up to the reader, who should call
`hightlightSpeechMark()` upon mark events.
This commit is contained in:
John Factotum
2023-09-27 02:18:40 +08:00
parent 3833ce5702
commit 403882e66d
2 changed files with 111 additions and 0 deletions
+14
View File
@@ -90,6 +90,7 @@ export class View extends HTMLElement {
#tocProgress
#pageProgress
#searchResults = new Map()
#speechRanges
isFixedLayout = false
lastLocation
history = new History()
@@ -408,6 +409,19 @@ export class View extends HTMLElement {
for (const item of list) this.deleteAnnotation(item)
this.#searchResults.clear()
}
async speak(granularity) {
const { insertMarks, toSSML } = await import('./tts.js')
const doc = this.renderer.getContents()[0].doc
const { doc: markedDoc, ranges } = insertMarks(textWalker, doc, granularity)
this.#speechRanges = new Map(ranges)
const ssml = toSSML(markedDoc)
return new XMLSerializer().serializeToString(ssml)
}
hightlightSpeechMark(name) {
const range = this.#speechRanges.get(name)
if (range) this.renderer.scrollToAnchor(range, true)
else console.warn('Mark not found')
}
}
customElements.define('foliate-view', View)