@@ -10,23 +10,23 @@ class MainContainer extends Component {
1010 this . state = {
1111 snapshots : [ ] ,
1212 snapshotIndex : 0 ,
13+ currentIndex : null ,
1314 port : null ,
1415 } ;
1516
1617 this . handleChangeSnapshot = this . handleChangeSnapshot . bind ( this ) ;
1718 this . handleSendSnapshot = this . handleSendSnapshot . bind ( this ) ;
18- this . emptySnapshot = this . emptySnapshot . bind ( this )
19+ this . emptySnapshot = this . emptySnapshot . bind ( this ) ;
1920 }
2021
21-
22-
2322 componentDidMount ( ) {
2423 // open connection with background script
2524 const port = chrome . runtime . connect ( ) ;
2625
2726 // listen for a message containing snapshots from the background script
2827 port . onMessage . addListener ( ( snapshots ) => {
2928 console . log ( 'message from background script' , snapshots ) ;
29+ console . log ( 'snapshots' , this . state . snapshots ) ;
3030 const snapshotIndex = snapshots . length - 1 ;
3131
3232 // set state with the information received from the background script
@@ -41,13 +41,11 @@ class MainContainer extends Component {
4141 // assign port to state so it could be used by other components
4242 this . setState ( { port } ) ;
4343 }
44-
45- emptySnapshot ( ) {
46- const { port } = this . state ;
4744
48- this . setState ( { snapshots : [ ] } )
49-
50- port . postMessage ( { action : 'emptySnap' , payload : [ ] } ) ;
45+ emptySnapshot ( ) {
46+ const { port } = this . state ;
47+ this . setState ( { snapshots : [ ] } ) ;
48+ port . postMessage ( { action : 'emptySnap' , payload : [ ] } ) ;
5149 }
5250
5351 // change the snapshot index
@@ -60,7 +58,26 @@ class MainContainer extends Component {
6058 // when the jump button is clicked, send a message to npm package with the selected snapshot
6159 handleSendSnapshot ( snapshotIndex ) {
6260 const { snapshots, port } = this . state ;
63- port . postMessage ( { action : 'jumpToSnap' , payload : snapshots [ snapshotIndex ] } ) ;
61+ let { currentIndex } = this . state ;
62+ const payload = [ ] ;
63+
64+ currentIndex = currentIndex === null ? snapshots . length - 1 : currentIndex ;
65+
66+ if ( currentIndex > snapshotIndex ) {
67+ for ( let i = currentIndex - 1 ; i >= snapshotIndex ; i -= 1 ) {
68+ payload . push ( snapshots [ i ] ) ;
69+ }
70+ } else {
71+ for ( let i = currentIndex + 1 ; i <= snapshotIndex ; i += 1 ) {
72+ payload . push ( snapshots [ i ] ) ;
73+ }
74+ }
75+
76+ this . setState ( { currentIndex : snapshotIndex } ) ;
77+
78+ console . log ( 'payload' , payload ) ;
79+ port . postMessage ( { action : 'stepToSnap' , payload } ) ;
80+ // port.postMessage({ action: 'jumpToSnap', payload: snapshots[snapshotIndex] });
6481 }
6582
6683 render ( ) {
@@ -76,7 +93,7 @@ class MainContainer extends Component {
7693 snapshotIndex = { snapshotIndex }
7794 handleChangeSnapshot = { this . handleChangeSnapshot }
7895 handleSendSnapshot = { this . handleSendSnapshot }
79- emptySnapshot = { this . emptySnapshot }
96+ emptySnapshot = { this . emptySnapshot }
8097 />
8198 < StateContainer snapshot = { snapshots [ snapshotIndex ] } />
8299 </ div >
0 commit comments