@@ -4,6 +4,7 @@ import { NextStepIcon } from "../icon/NextStepIcon";
4
4
import { ResetIcon } from "../icon/ResetIcon" ;
5
5
import DropDown from "../DropDown/DropDown" ;
6
6
import Button from "../Button/Button" ;
7
+ import { useRef } from "react" ;
7
8
8
9
const Player = ( {
9
10
selectAlgorithm,
@@ -16,6 +17,32 @@ const Player = ({
16
17
handleAlgorithmRun,
17
18
selectedAlgorithm,
18
19
} ) => {
20
+
21
+ const intervalRef = useRef ( null )
22
+
23
+ const startInterval = ( action ) => {
24
+ if ( intervalRef . current === null ) {
25
+ intervalRef . current = setInterval ( ( ) => {
26
+ action ( )
27
+ } , 100 )
28
+ }
29
+ }
30
+
31
+ const clearTimer = ( ) => {
32
+ if ( intervalRef . current !== null ) {
33
+ clearInterval ( intervalRef . current )
34
+ }
35
+ intervalRef . current = null
36
+ }
37
+
38
+ const handleMouseDown = ( action ) => {
39
+ action ( )
40
+ startInterval ( action )
41
+ }
42
+ const handleMouseUpOrLeave = ( ) => {
43
+ clearTimer ( )
44
+ }
45
+
19
46
return (
20
47
< div className = "player-container" >
21
48
< div className = "controls" >
@@ -24,11 +51,26 @@ const Player = ({
24
51
selectedAlgorithm = { selectedAlgorithm }
25
52
/>
26
53
< div className = "step-buttons" >
27
- < Button onClick = { goToPreviousStep } >
54
+ < Button
55
+ onMouseDown = { ( ) => handleMouseDown ( goToPreviousStep ) }
56
+ onMouseUp = { handleMouseUpOrLeave }
57
+ onMouseLeave = { handleMouseUpOrLeave }
58
+ onTouchStart = { ( ) => handleMouseDown ( goToPreviousStep ) }
59
+ onTouchEnd = { handleMouseUpOrLeave }
60
+ onTouchCancel = { handleMouseUpOrLeave }
61
+
62
+ >
28
63
< PreviousStepIcon />
29
64
</ Button >
30
65
< Button onClick = { handleAlgorithmRun } > { getButtonText ( ) } </ Button >
31
- < Button onClick = { goToNextStep } >
66
+ < Button
67
+ onMouseDown = { ( ) => handleMouseDown ( goToNextStep ) }
68
+ onMouseUp = { handleMouseUpOrLeave }
69
+ onMouseLeave = { handleMouseUpOrLeave }
70
+ onTouchStart = { ( ) => handleMouseDown ( goToNextStep ) }
71
+ onTouchEnd = { handleMouseUpOrLeave }
72
+ onTouchCancel = { handleMouseUpOrLeave }
73
+ >
32
74
< NextStepIcon />
33
75
</ Button >
34
76
</ div >
@@ -39,7 +81,7 @@ const Player = ({
39
81
< div className = "controls-speed" >
40
82
< div >
41
83
< input
42
- style = { { width : "320px" } }
84
+ style = { { width : "320px" , opacity : '70%' } }
43
85
type = "range"
44
86
id = "speed"
45
87
value = { speed }
0 commit comments