Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

尝试增加单词发音功能 #50

Merged
merged 6 commits into from
Feb 8, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/components/Word/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Letter, { LetterState } from './Letter'
import { isLegal, isChineseSymbol } from '../../utils/utils'
import useSounds from 'hooks/useSounds'
import style from './index.module.css'
import usepronunciationSound from 'hooks/usePronouncation'
import usePronunciationSound from 'hooks/usePronouncation'

const Word: React.FC<WordProps> = ({ word = 'defaultWord', onFinish, isStart, wordVisible = true }) => {
word = word.replace(new RegExp(' ', 'g'), '_')
Expand All @@ -13,7 +13,7 @@ const Word: React.FC<WordProps> = ({ word = 'defaultWord', onFinish, isStart, wo
const [isFinish, setIsFinish] = useState(false)
const [hasWrong, setHasWrong] = useState(false)
const [playKeySound, playBeepSound, playHintSound] = useSounds()
const [playPronounce] = usepronunciationSound(word)
const playPronounce = usePronunciationSound(word)

const onKeydown = useCallback(
(e) => {
Expand Down Expand Up @@ -62,12 +62,12 @@ const Word: React.FC<WordProps> = ({ word = 'defaultWord', onFinish, isStart, wo
}, [hasWrong, playBeepSound])

useLayoutEffect(() => {
if (inputWord.length === 0) {
if (isStart && inputWord.length === 0) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里不应该使用 useLayoutEffect,useLayoutEffect是与dom更新同步,而useEffect是在dom更新后进行。盲目的使用useLayoutEffect可能会引发其他问题。可以参考链接:https://zh-hans.reactjs.org/docs/hooks-reference.html#uselayouteffect

以及,对 useEffect类函数的依赖可能理解不对,这里 hooks 内部没有使用 hasWrong,可以在依赖项中移除。

playPronounce()
}
// SAFETY: Don't depend on `playPronounce`! It will cost audio play again and again.
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [inputWord])
}, [isStart, hasWrong, word])

useLayoutEffect(() => {
let hasWrong = false,
Expand Down
16 changes: 10 additions & 6 deletions src/hooks/usePronouncation.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import { useAppSettings } from 'components/AppSettings'
import { noop } from 'lodash'
import { useEffect, useState } from 'react'

declare type PronounceFunction = () => void

const pronunciationApi = 'http://dict.youdao.com/dictvoice?audio='

export default function usepronunciationSound(word: string): [PronounceFunction] {
// SAFETY: We are trying to build an hook here.
// eslint-disable-next-line react-hooks/rules-of-hooks
export default function usePronunciationSound(word: string): PronounceFunction {
const [audio, setAudio] = useState<HTMLAudioElement | null>(null)
const { pronunciation } = useAppSettings()
const pronounceFunction = () => {
var audio = new Audio(pronunciationApi + word)
audio.play()
audio?.pause()
setAudio(new Audio(pronunciationApi + word))
}
return pronunciation ? [pronounceFunction] : [noop]

useEffect(() => {
audio?.play()
}, [audio])
return pronunciation ? pronounceFunction : noop
}
5 changes: 5 additions & 0 deletions src/pages/Typing/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ const App: React.FC = () => {
(dictName: string) => {
setOrder(0)
setIsLoading(true)
// Need to stop the game, in order to prevent pronounce wrong word.
// Besides, it makes sense because users are about to stop when they change dict/chapter.
// Otherwise, introduce a new parameter to allow pronunciation begin.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这种方案可能确实很是合理的。
Need more comments @chengluyu

setIsStart(false)
wordListDispatch('setDictName', dictName, () => {
setIsLoading(false)
})
Expand All @@ -172,6 +176,7 @@ const App: React.FC = () => {
const changeChapter = useCallback(
(chapter: number) => {
setOrder(0)
setIsStart(false) // Same story as above.
wordListDispatch('setChapter', chapter)
},
[wordListDispatch],
Expand Down