@@ -29,7 +29,8 @@ import { ProgressBarState } from "$lib/components/progress-bar/index.svelte";
2929import { Keybinds } from "./keybinds.svelte" ;
3030import { LayoutState , type PersistentLayoutState } from "./layout.svelte" ;
3131import { page } from "$app/state" ;
32- import { goto } from "$app/navigation" ;
32+ import { afterNavigate , goto } from "$app/navigation" ;
33+ import { type AfterNavigate } from "@sveltejs/kit" ;
3334
3435export const GITHUB_URL_PARAM = "github_url" ;
3536export const PATCH_URL_PARAM = "patch_url" ;
@@ -320,12 +321,43 @@ export class MultiFileDiffViewerState {
320321 }
321322 } ) ;
322323
324+ afterNavigate ( ( nav ) => {
325+ this . afterNavigate ( nav ) ;
326+ } ) ;
327+
328+ this . registerKeybinds ( ) ;
329+ }
330+
331+ private registerKeybinds ( ) {
323332 const keybinds = new Keybinds ( ) ;
324333 keybinds . registerModifierBind ( "o" , ( ) => this . openOpenDiffDialog ( ) ) ;
325334 keybinds . registerModifierBind ( "," , ( ) => this . openSettingsDialog ( ) ) ;
326335 keybinds . registerModifierBind ( "b" , ( ) => this . layoutState . toggleSidebar ( ) ) ;
327336 }
328337
338+ private afterNavigate ( nav : AfterNavigate ) {
339+ if ( ! this . vlist ) return ;
340+
341+ if ( nav . type === "popstate" ) {
342+ const selection = page . state . selection ;
343+ const file = selection ? this . fileDetails [ selection . fileIdx ] : undefined ;
344+ if ( selection && file ) {
345+ this . selection = {
346+ file,
347+ lines : selection . lines ,
348+ unresolvedLines : selection . unresolvedLines ,
349+ } ;
350+ } else {
351+ this . selection = undefined ;
352+ }
353+
354+ const scrollOffset = page . state . scrollOffset ;
355+ if ( scrollOffset !== undefined ) {
356+ this . vlist . scrollTo ( scrollOffset ) ;
357+ }
358+ }
359+ }
360+
329361 openOpenDiffDialog ( ) {
330362 this . openDiffDialogOpen = true ;
331363 this . settingsDialogOpen = false ;
@@ -378,11 +410,25 @@ export class MultiFileDiffViewerState {
378410 return null ;
379411 }
380412
413+ private createPageState ( ) : App . PageState {
414+ return {
415+ scrollOffset : this . vlist ?. getScrollOffset ( ) ,
416+ selection : this . selection
417+ ? {
418+ fileIdx : this . selection . file . index ,
419+ lines : $state . snapshot ( this . selection . lines ) ,
420+ unresolvedLines : $state . snapshot ( this . selection . unresolvedLines ) ,
421+ }
422+ : undefined ,
423+ } ;
424+ }
425+
381426 setSelection ( file : FileDetails , lines : LineSelection | undefined ) {
382427 this . selection = { file, lines } ;
383428
384429 goto ( `?${ page . url . searchParams } #${ makeUrlHashValue ( this . selection ) } ` , {
385430 keepFocus : true ,
431+ state : this . createPageState ( ) ,
386432 } ) ;
387433 }
388434
@@ -391,6 +437,7 @@ export class MultiFileDiffViewerState {
391437
392438 goto ( `?${ page . url . searchParams } ` , {
393439 keepFocus : true ,
440+ state : this . createPageState ( ) ,
394441 } ) ;
395442 }
396443
@@ -567,12 +614,14 @@ export class MultiFileDiffViewerState {
567614 file,
568615 unresolvedLines : urlSelection . lines ,
569616 } ;
570- await goto ( `?${ page . url . searchParams } #${ makeUrlHashValue ( this . selection ) } ` , {
571- keepFocus : true ,
572- } ) ;
573617 this . scrollToFile ( file . index , {
574618 focus : ! urlSelection . lines ,
575619 } ) ;
620+ await animationFramePromise ( ) ;
621+ await goto ( `?${ page . url . searchParams } #${ makeUrlHashValue ( this . selection ) } ` , {
622+ keepFocus : true ,
623+ state : this . createPageState ( ) ,
624+ } ) ;
576625 } else {
577626 await goto ( `?${ page . url . searchParams } ` , {
578627 keepFocus : true ,
0 commit comments