Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions __tests__/history.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,34 @@ describe('openWithHistory', () => {
expect(removeSpy).toHaveBeenCalledWith('popstate', expect.any(Function))
})

it('drops the openfile flag before the jump, not after it', () => {
const router = setRouter()
const file = makeFile({ id: 1 })
openWithHistory([file], file, view, folder)
router.query.openfile = 'true'

const order: string[] = []
vi.mocked(router.goToRoute).mockImplementation(() => {
order.push('route')
})
goSpy.mockImplementation(() => {
order.push('go')
})

openOptions().onClose()

// history.go() lands on a later task. Until it does the URL still says
// openfile=true, and the Files list opens the file again if anything
// makes it re-read the route in that window.
expect(order).toEqual(['route', 'go'])
expect(router.goToRoute).toHaveBeenCalledWith(
'filelist',
router.params,
expect.not.objectContaining({ openfile: 'true' }),
true,
)
})

it('drops the openfile flag in place when closing a refresh-opened viewer', () => {
const router = setRouter({ openfile: 'true', dir: '/photos' })
const file = makeFile({ id: 1 })
Expand Down
19 changes: 14 additions & 5 deletions lib/utils/history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,28 @@ function closeHistory(): void {
return
}

const query = { ...router.query }
delete query.openfile
delete query.editing

const offset = currentOffset()
if (offset > 0) {
// Drop the flag on the entry being left before jumping. history.go() is
// asynchronous, and until it lands the URL still says openfile=true:
// anything that makes the Files list re-read the route in that window
// runs the default action again and opens a second viewer over the one
// that is closing.
router.goToRoute(routeName(router), router.params, query, true)

// Jump back past every entry the viewer added, in one step, so the back
// button returns to the opening page instead of a previously shown file.
window.history.go(-offset)
return
}

// Opened from an openfile URL with no pre-viewer entry to return to (refresh):
// just drop the openfile flag on the current entry.
const query = { ...router.query }
delete query.openfile
delete query.editing
// Opened from an openfile URL with no pre-viewer entry to return to
// (refresh): the flag comes off the current entry and there is nothing to
// unwind.
router.goToRoute(routeName(router), router.params, query, true)
}

Expand Down
Loading