diff --git a/__tests__/history.spec.ts b/__tests__/history.spec.ts index 55e41be..a4ccd8d 100644 --- a/__tests__/history.spec.ts +++ b/__tests__/history.spec.ts @@ -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 }) diff --git a/lib/utils/history.ts b/lib/utils/history.ts index f1c688a..7700ee2 100644 --- a/lib/utils/history.ts +++ b/lib/utils/history.ts @@ -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) }