-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Febase/feature/play-editor
feature: 에디터의 text를 활용해 커스텀 이벤트 생성&연동
- Loading branch information
Showing
5 changed files
with
87 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useState, useRef } from 'react' | ||
|
||
interface IUsePlayText { | ||
play: (text: string) => void | ||
stop: () => void | ||
isPlaying: boolean | ||
} | ||
|
||
const usePlayText = (): IUsePlayText => { | ||
const [isPlaying, setIsPlaying] = useState<boolean>(false) | ||
const intervalIdRef = useRef<NodeJS.Timeout | null>(null) | ||
|
||
const play = (text: string) => { | ||
if (text === '') { | ||
return | ||
} | ||
|
||
setIsPlaying(true) | ||
|
||
let index = 0 | ||
const textArray = text.split('') | ||
const id = setInterval(() => { | ||
const keyId = textArray[index].toLowerCase() | ||
const event = new CustomEvent('threekeyboardevent', { | ||
detail: { | ||
keyId, | ||
}, | ||
}) | ||
document.dispatchEvent(event) | ||
|
||
index++ | ||
|
||
if (index === textArray.length) { | ||
clearInterval(id) | ||
setIsPlaying(false) | ||
intervalIdRef.current = null | ||
} | ||
}, 200) | ||
|
||
intervalIdRef.current = id | ||
} | ||
|
||
const stop = () => { | ||
if (intervalIdRef.current) { | ||
clearInterval(intervalIdRef.current) | ||
|
||
intervalIdRef.current = null | ||
setIsPlaying(false) | ||
} | ||
} | ||
|
||
return { play, stop, isPlaying } | ||
} | ||
|
||
export default usePlayText |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export type ThreeEventType = { | ||
keyId?: string | ||
keyId?: string | null | ||
} | ||
|
||
interface ThreeCustomEvent { | ||
|