Skip to content

Commit

Permalink
fix: refresh paragragh
Browse files Browse the repository at this point in the history
  • Loading branch information
IronKinoko committed Jan 11, 2024
1 parent 915b798 commit 61e4d83
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/sour-ears-run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'novel-speech-synthesis': patch
---

fix: refresh paragragh
32 changes: 31 additions & 1 deletion packages/novel-speech-synthesis/src/adapter/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Route, RouterOptions } from 'shared'
import { SpeechOptions } from '../speech'
import Speech, { SpeechOptions } from '../speech'

type Adapter = Omit<RouterOptions, 'routes'> & {
routes: (Partial<Route> & { speech?: SpeechOptions })[]
Expand Down Expand Up @@ -54,6 +54,21 @@ export const adapters: Adapter[] = [
?.click()
},
},
run() {
const speech = new Speech(this.speech!)

const ob = new MutationObserver(() => {
if (
speech.paragraphList.length !== speech.opts.getParagraph().length
) {
speech.setupParagraph()
}
})

ob.observe(document.querySelector('.read-content')!, {
childList: true,
})
},
},
],
},
Expand All @@ -77,6 +92,21 @@ export const adapters: Adapter[] = [
?.click()
},
},
run() {
const speech = new Speech(this.speech!)

const ob = new MutationObserver(() => {
if (
speech.paragraphList.length !== speech.opts.getParagraph().length
) {
speech.setupParagraph()
}
})

ob.observe(document.querySelector('#acontentz')!, {
childList: true,
})
},
},
],
},
Expand Down
5 changes: 3 additions & 2 deletions packages/novel-speech-synthesis/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ adapters.forEach((adapter) => {
return {
...route,
run: () => {
route.run?.()
if (route.speech) new Speech(route.speech)
if (route.run) {
route.run()
} else if (route.speech) new Speech(route.speech)
},
}
}),
Expand Down
29 changes: 21 additions & 8 deletions packages/novel-speech-synthesis/src/speech/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,29 @@ export default class Speech {
disabled: false,
}
private voices: SpeechSynthesisVoice[] = []
private paragraphList = [] as HTMLElement[]
paragraphList: HTMLElement[] = []
private paragraphDisposeList: (() => void)[] = []

private speakDispose: (() => void) | null = null
drag!: Drag
private drag!: Drag

constructor(private opts: SpeechOptions) {
constructor(public opts: SpeechOptions) {
this.loadUtterance()
this.washContent()
this.setupParagraph()
this.createUI()
}

private washContent() {
setupParagraph() {
if (this.paragraphDisposeList.length) {
this.paragraphDisposeList.forEach((fn) => fn())
this.paragraphDisposeList = []
}

this.paragraphList = this.opts.getParagraph()
this.paragraphList.forEach((p, idx) => {
this.paragraphDisposeList = this.paragraphList.map((p, idx) => {
p.setAttribute('data-speech-idx', idx.toString())

p.addEventListener('click', () => {
const fn = () => {
if (this.utterance.disabled) return

let current = idx
Expand All @@ -66,7 +72,14 @@ export default class Speech {
cancel = true
}
speak()
})
}

p.addEventListener('click', fn)

return () => {
p.removeAttribute('data-speech-idx')
p.removeEventListener('click', fn)
}
})
}

Expand Down

0 comments on commit 61e4d83

Please sign in to comment.