Skip to content

Commit

Permalink
Make some form of callback/event available when the TTS changes state…
Browse files Browse the repository at this point in the history
… - play, pause, resume, stop (manually stopped or reached the end).
  • Loading branch information
aferditamuriqi committed Sep 20, 2020
1 parent 0ef80fb commit 2c76942
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ export async function load(config: ReaderConfig): Promise<any> {
store: settingsStore,
initialTTSSettings: config.tts,
headerMenu: headerMenu,
api:config.api
api:config.tts.api
})

R2Navigator = await IFrameNavigator.create({
Expand Down
34 changes: 23 additions & 11 deletions src/modules/TTS/TTSModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,16 @@ export default class TTSModule implements ReaderModule {
}

cancel() {
this.userScrolled = false
window.speechSynthesis.cancel()
if (this.splittingResult && this.annotationModule.delegate.tts.enableSplitter) {
this.splittingResult.forEach(splittingWord => {
splittingWord.dataset.ttsCurrentWord = "false"
splittingWord.dataset.ttsCurrentLine = "false"
});
if (window.speechSynthesis.speaking) {
if (this.tts.api) this.tts.api.stopped()
this.userScrolled = false
window.speechSynthesis.cancel()
if (this.splittingResult && this.annotationModule.delegate.tts.enableSplitter) {
this.splittingResult.forEach(splittingWord => {
splittingWord.dataset.ttsCurrentWord = "false"
splittingWord.dataset.ttsCurrentLine = "false"
});
}
}
}

Expand All @@ -144,6 +147,8 @@ export default class TTSModule implements ReaderModule {

async speak(selectionInfo: ISelectionInfo | undefined, node: any, partial: boolean, callback: () => void): Promise<any> {

if (this.tts.api) this.tts.api.started()

this.userScrolled = false
var self = this

Expand Down Expand Up @@ -368,19 +373,26 @@ export default class TTSModule implements ReaderModule {
});

}
if (self.tts.api) self.tts.api.finished()
}
callback()

}

speakPause() {
this.userScrolled = false
window.speechSynthesis.pause()
if (window.speechSynthesis.speaking) {
if (this.tts.api) this.tts.api.paused()
this.userScrolled = false
window.speechSynthesis.pause()
}
}

speakResume() {
this.userScrolled = false
window.speechSynthesis.resume()
if (window.speechSynthesis.speaking) {
if (this.tts.api) this.tts.api.resumed()
this.userScrolled = false
window.speechSynthesis.resume()
}
}

public static async create(config: TTSModuleConfig) {
Expand Down

0 comments on commit 2c76942

Please sign in to comment.