@@ -48,10 +48,12 @@ const EXTENSION = ".time-travel";
48
48
49
49
export interface TimeTravelState extends CodeEditorState {
50
50
versions : List < Date > ;
51
+ git_versions : List < Date > ;
51
52
loading : boolean ;
52
53
has_full_history : boolean ;
53
54
docpath : string ;
54
55
docext : string ;
56
+ // true if in a git repo
55
57
git ?: boolean ;
56
58
//frame_states: Map<string, any>; // todo: really map from frame_id to FrameState as immutable map.
57
59
}
@@ -115,31 +117,54 @@ export class TimeTravelActions extends CodeEditorActions<TimeTravelState> {
115
117
} ) ;
116
118
}
117
119
118
- public init_frame_tree ( versions ?: List < Date > ) : void {
120
+ init_frame_tree = ( ) => {
121
+ this . ensureSelectedVersionsAreConsistent ( ) ;
122
+ } ;
123
+
124
+ ensureSelectedVersionsAreConsistent = ( {
125
+ versions,
126
+ git_versions,
127
+ } : {
128
+ versions ?;
129
+ git_versions ?;
130
+ } = { } ) : void => {
119
131
if ( versions == null ) {
120
132
if ( this . syncdoc == null || this . syncdoc . get_state ( ) != "ready" ) return ;
121
- versions = List < Date > ( this . syncdoc . all_versions ( ) ) ;
133
+ versions =
134
+ this . store . get ( "versions" ) ?? List < Date > ( this . syncdoc . all_versions ( ) ) ;
135
+ }
136
+ if ( git_versions == null ) {
137
+ git_versions = this . store . get ( "git_versions" ) ;
122
138
}
123
139
// make sure all the version and version ranges are valid...
124
140
const max = versions . size - 1 ;
141
+ const max_git = git_versions != null ? git_versions . size - 1 : Infinity ;
125
142
for ( const actions of [ this . ambient_actions , this ] ) {
126
143
if ( actions == null ) continue ;
127
144
for ( const id in actions . _get_leaf_ids ( ) ) {
128
145
const node = actions . _get_frame_node ( id ) ;
129
- if ( node == null || node . get ( "type" ) != "time_travel" ) continue ;
146
+ if ( node ?. get ( "type" ) != "time_travel" ) {
147
+ continue ;
148
+ }
149
+ const m = node . get ( "git_mode" ) ? max_git : max ;
130
150
for ( const x of [ "version" , "version0" , "version1" ] ) {
131
151
let n : number | undefined = node . get ( x ) ;
132
- if ( n == null || n > max || n < 0 ) {
133
- // make it max except in the case of "version0"
152
+ if ( n == null || n > m || n < 0 ) {
153
+ // make it m except in the case of "version0"
134
154
// when we want it to be one less than version1, which
135
- // will be max.
136
- n = x == "version0" ? Math . max ( 0 , max - 1 ) : max ;
155
+ // will be m.
156
+ // Also for git mode when m=Infinity, use 0 since there is no other option.
157
+ if ( m == Infinity ) {
158
+ n = 0 ;
159
+ } else {
160
+ n = x == "version0" ? Math . max ( 0 , m - 1 ) : m ;
161
+ }
137
162
actions . set_frame_tree ( { id, [ x ] : n } ) ;
138
163
}
139
164
}
140
165
}
141
166
}
142
- }
167
+ } ;
143
168
144
169
public async load_full_history ( ) : Promise < void > {
145
170
if (
@@ -170,7 +195,7 @@ export class TimeTravelActions extends CodeEditorActions<TimeTravelState> {
170
195
this . setState ( { versions } ) ;
171
196
if ( this . first_load ) {
172
197
this . first_load = false ;
173
- this . init_frame_tree ( versions ) ;
198
+ this . ensureSelectedVersionsAreConsistent ( { versions } ) ;
174
199
}
175
200
}
176
201
@@ -220,14 +245,20 @@ export class TimeTravelActions extends CodeEditorActions<TimeTravelState> {
220
245
throw Error ( `BUG -- no node with id ${ id } ` ) ;
221
246
}
222
247
223
- public set_version ( id : string , version : number ) : void {
248
+ set_version = ( id : string , version : number ) : void => {
224
249
for ( const actions of [ this , this . ambient_actions ] ) {
225
250
if ( actions == null || actions . _get_frame_node ( id ) == null ) continue ;
226
251
if ( typeof version != "number" ) {
227
252
// be extra careful
228
253
throw Error ( "version must be a number" ) ;
229
254
}
230
- const versions = this . store . get ( "versions" ) ;
255
+ const node = actions . _get_frame_node ( id ) ;
256
+ if ( node == null ) {
257
+ return ;
258
+ }
259
+ const versions = node . get ( "git_mode" )
260
+ ? this . store . get ( "git_versions" )
261
+ : this . store . get ( "versions" ) ;
231
262
if ( versions == null || versions . size == 0 ) return ;
232
263
if ( version == - 1 || version >= versions . size ) {
233
264
version = versions . size - 1 ;
@@ -237,7 +268,7 @@ export class TimeTravelActions extends CodeEditorActions<TimeTravelState> {
237
268
actions . set_frame_tree ( { id, version } ) ;
238
269
return ;
239
270
}
240
- }
271
+ } ;
241
272
242
273
public step ( id : string , delta : number ) : void {
243
274
const node = this . get_frame_node_global ( id ) ;
@@ -367,13 +398,6 @@ export class TimeTravelActions extends CodeEditorActions<TimeTravelState> {
367
398
368
399
private gitCommand = async ( args : string [ ] , commit ?: string ) => {
369
400
const { head, tail } = path_split ( this . docpath ) ;
370
- console . log ( {
371
- command : "git" ,
372
- args : args . concat ( [ `${ commit ? commit + ":./" : "" } ${ tail } ` ] ) ,
373
- path : head ,
374
- project_id : this . project_id ,
375
- err_on_exit : true ,
376
- } ) ;
377
401
return await exec ( {
378
402
command : "git" ,
379
403
args : args . concat ( [ `${ commit ? commit + ":./" : "" } ${ tail } ` ] ) ,
@@ -403,10 +427,12 @@ export class TimeTravelActions extends CodeEditorActions<TimeTravelState> {
403
427
versions . push ( new Date ( t ) ) ;
404
428
}
405
429
versions . reverse ( ) ;
430
+ const git_versions = List < Date > ( versions ) ;
406
431
this . setState ( {
407
432
git : versions . length > 0 ,
408
- versions : List < Date > ( versions ) ,
433
+ git_versions ,
409
434
} ) ;
435
+ this . ensureSelectedVersionsAreConsistent ( { git_versions } ) ;
410
436
} catch ( err ) {
411
437
this . set_error ( `${ err } ` ) ;
412
438
this . setState ( { git : false } ) ;
0 commit comments