@@ -17,7 +17,8 @@ class MainContainer extends Component {
17
17
18
18
this . state = {
19
19
snapshots : [ ] ,
20
- snapshotIndex : 0 ,
20
+ sliderIndex : 0 ,
21
+ viewIndex : 0 ,
21
22
port : null ,
22
23
playing : false ,
23
24
mode : {
@@ -39,15 +40,17 @@ class MainContainer extends Component {
39
40
const { action, payload } = message ;
40
41
switch ( action ) {
41
42
case 'sendSnapshots' : {
42
- const snapshotIndex = payload . length - 1 ;
43
+ const viewIndex = payload . length - 1 ;
44
+ const sliderIndex = viewIndex ;
43
45
// set state with the information received from the background script
44
- this . setState ( { snapshots : payload , snapshotIndex } ) ;
46
+ this . setState ( { snapshots : payload , viewIndex , sliderIndex } ) ;
45
47
break ;
46
48
}
47
49
case 'initialConnectSnapshots' : {
48
50
const { snapshots, mode } = payload ;
49
- const snapshotIndex = snapshots . length - 1 ;
50
- this . setState ( { snapshots, snapshotIndex, mode } ) ;
51
+ const viewIndex = snapshots . length - 1 ;
52
+ const sliderIndex = viewIndex ;
53
+ this . setState ( { snapshots, mode, viewIndex, sliderIndex } ) ;
51
54
break ;
52
55
}
53
56
default :
@@ -64,21 +67,21 @@ class MainContainer extends Component {
64
67
}
65
68
66
69
moveBackward ( ) {
67
- const { snapshots, snapshotIndex } = this . state ;
70
+ const { snapshots, sliderIndex } = this . state ;
68
71
this . pause ( ) ;
69
- if ( snapshots . length > 0 && snapshotIndex > 0 ) {
70
- const newIndex = snapshotIndex - 1 ;
72
+ if ( snapshots . length > 0 && sliderIndex > 0 ) {
73
+ const newIndex = sliderIndex - 1 ;
71
74
// second callback parameter of setState to invoke handleJumpSnapshot
72
- this . setState ( { snapshotIndex : newIndex } , this . handleJumpSnapshot ( newIndex ) ) ;
75
+ this . setState ( { sliderIndex : newIndex } , this . handleJumpSnapshot ( newIndex ) ) ;
73
76
}
74
77
}
75
78
76
79
moveForward ( ) {
77
- const { snapshots, snapshotIndex } = this . state ;
80
+ const { snapshots, sliderIndex } = this . state ;
78
81
this . pause ( ) ;
79
- if ( snapshotIndex < snapshots . length - 1 ) {
80
- const newIndex = snapshotIndex + 1 ;
81
- this . setState ( { snapshotIndex : newIndex } , this . handleJumpSnapshot ( newIndex ) ) ;
82
+ if ( sliderIndex < snapshots . length - 1 ) {
83
+ const newIndex = sliderIndex + 1 ;
84
+ this . setState ( { sliderIndex : newIndex } , this . handleJumpSnapshot ( newIndex ) ) ;
82
85
}
83
86
}
84
87
@@ -88,10 +91,10 @@ class MainContainer extends Component {
88
91
const { playing } = this . state ;
89
92
if ( playing ) {
90
93
intervalId = setInterval ( ( ) => {
91
- const { snapshots, snapshotIndex } = this . state ;
92
- if ( snapshotIndex < snapshots . length - 1 ) {
93
- const newIndex = snapshotIndex + 1 ;
94
- this . setState ( { snapshotIndex : newIndex } , this . handleJumpSnapshot ( newIndex ) ) ;
94
+ const { snapshots, sliderIndex } = this . state ;
95
+ if ( sliderIndex < snapshots . length - 1 ) {
96
+ const newIndex = sliderIndex + 1 ;
97
+ this . setState ( { sliderIndex : newIndex } , this . handleJumpSnapshot ( newIndex ) ) ;
95
98
} else {
96
99
// clear interval when play reaches the end
97
100
globalPlaying = false ;
@@ -111,22 +114,22 @@ class MainContainer extends Component {
111
114
112
115
emptySnapshot ( ) {
113
116
const { port, snapshots } = this . state ;
114
- this . setState ( { snapshots : [ snapshots [ 0 ] ] , snapshotIndex : 0 } ) ;
117
+ this . setState ( { snapshots : [ snapshots [ 0 ] ] , sliderIndex : 0 , viewIndex : 0 } ) ;
115
118
port . postMessage ( { action : 'emptySnap' } ) ;
116
119
}
117
120
118
- // change the snapshot index
121
+ // change the view index
119
122
// this will change the state shown in the state container but won't change the DOM
120
- handleChangeSnapshot ( snapshotIndex ) {
121
- // snapshotIndex
122
- // --> 1. affects the action that is highlighted
123
- // --> 2. moves the slider
124
- this . setState ( { snapshotIndex } ) ;
123
+ handleChangeSnapshot ( viewIndex ) {
124
+ this . setState ( { viewIndex } ) ;
125
125
}
126
126
127
- handleJumpSnapshot ( snapshotIndex ) {
127
+ // changes the slider index
128
+ // this will change the dom but not the state shown in the state container
129
+ handleJumpSnapshot ( sliderIndex ) {
128
130
const { snapshots, port } = this . state ;
129
- port . postMessage ( { action : 'jumpToSnap' , payload : snapshots [ snapshotIndex ] } ) ;
131
+ port . postMessage ( { action : 'jumpToSnap' , payload : snapshots [ sliderIndex ] } ) ;
132
+ this . setState ( { sliderIndex } ) ;
130
133
}
131
134
132
135
importSnapshots ( ) {
@@ -189,23 +192,23 @@ class MainContainer extends Component {
189
192
190
193
render ( ) {
191
194
const {
192
- snapshots, snapshotIndex , mode, playing, playSpeed,
195
+ snapshots, viewIndex , sliderIndex , mode, playing, playSpeed,
193
196
} = this . state ;
194
197
return (
195
198
< div className = "main-container" >
196
199
< HeadContainer />
197
200
< div className = "body-container" >
198
201
< ActionContainer
199
202
snapshots = { snapshots }
200
- snapshotIndex = { snapshotIndex }
203
+ viewIndex = { viewIndex }
201
204
handleChangeSnapshot = { this . handleChangeSnapshot }
202
205
handleJumpSnapshot = { this . handleJumpSnapshot }
203
206
emptySnapshot = { this . emptySnapshot }
204
207
/>
205
- { ( snapshots . length ) ? < StateContainer snapshot = { snapshots [ snapshotIndex ] } /> : null }
208
+ { ( snapshots . length ) ? < StateContainer snapshot = { snapshots [ viewIndex ] } /> : null }
206
209
< TravelContainer
207
210
snapshotsLength = { snapshots . length }
208
- snapshotIndex = { snapshotIndex }
211
+ sliderIndex = { sliderIndex }
209
212
handleChangeSnapshot = { this . handleChangeSnapshot }
210
213
handleJumpSnapshot = { this . handleJumpSnapshot }
211
214
moveBackward = { this . moveBackward }
0 commit comments