Skip to content

Commit

Permalink
Merge pull request #9 from Febase/feature/play-editor
Browse files Browse the repository at this point in the history
feature: 에디터의 text를 활용해 커스텀 이벤트 생성&연동
  • Loading branch information
BonhaengLee authored Feb 27, 2023
2 parents 5492b34 + 474641a commit 69f00c3
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/components/Panel/Items.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React, { useState } from 'react'
import styled from '@emotion/styled'
import PlayButton from './PlayButton'
import usePlayText from '../../hooks/usePlayText'

export default function Items() {
return <Item />
Expand All @@ -27,7 +28,12 @@ function Item() {
onKeyDown={handleKeyDown}
onChange={handleChange}
/>
<PlayButton />
{
<PlayButton
isPlaying={isPlaying}
onClick={isPlaying ? stop : () => play(value)}
/>
}
</ItemContainer>
)
}
Expand Down
19 changes: 16 additions & 3 deletions src/components/Panel/PlayButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ import styled from '@emotion/styled'
import { useState } from 'react'
import { buttonStyle } from './styles'
import { ReactComponent as PlayIcon } from '../../res/play.svg'
import { ReactComponent as StopIcon } from '../../res/stop.svg'

export default function PlayButton() {
interface IPlayButtonProps {
isPlaying: boolean
onClick: () => void
}

export default function PlayButton({ isPlaying, onClick }: IPlayButtonProps) {
const [isHovered, setIsHovered] = useState(false)
const handleMouseEnter = () => {
setIsHovered(true)
Expand All @@ -13,13 +19,20 @@ export default function PlayButton() {
}

return (
<Button onMouseEnter={handleMouseEnter} onMouseLeave={handleMouseLeave}>
<PlayIcon fill={isHovered ? '#d65a31' : '#ffffff'} />
<Button role="button" onMouseEnter={handleMouseEnter} onMouseLeave={handleMouseLeave}>
{isPlaying ? (
<StopIcon onClick={onClick} />
) : (
<PlayIcon fill={isHovered ? '#d65a31' : '#ffffff'} onClick={onClick} />
)}
</Button>
)
}

const Button = styled.div`
width: 30px;
height: 30px;
cursor: pointer;
${buttonStyle}
margin-top: 0.5rem;
`
55 changes: 55 additions & 0 deletions src/hooks/usePlayText.ts
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
8 changes: 8 additions & 0 deletions src/res/stop.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/types/ThreeEvent.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export type ThreeEventType = {
keyId?: string
keyId?: string | null
}

interface ThreeCustomEvent {
Expand Down

0 comments on commit 69f00c3

Please # to comment.