Skip to content

Commit

Permalink
Merge pull request #2661 from nextcloud/fix/browser-back
Browse files Browse the repository at this point in the history
fix: Close viewer if we navigate back in browser history
  • Loading branch information
juliusknorr authored Jan 7, 2025
2 parents 70956fe + 18477e4 commit 8e984be
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
29 changes: 29 additions & 0 deletions cypress/e2e/navigation.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

describe('Browser navigation', function() {
before(function() {
// Init user
cy.createRandomUser().then(user => {
// Upload test files
cy.uploadFile(user, 'image.png', 'image/png', '/image1.png')

// Visit nextcloud
cy.login(user)
cy.visit('/apps/files')
})
})
after(function() {
cy.logout()
})

it('Navigating back to the files overview', function() {
cy.getFile('image1.png', { timeout: 10000 })
cy.openFile('image1.png')
cy.get('body > .viewer').should('be.visible')
cy.go('back')
cy.get('body > .viewer').should('not.exist')
})
})
2 changes: 1 addition & 1 deletion js/viewer-init.mjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion js/viewer-init.mjs.map

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/files_actions/viewerAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ function pushToHistory(node: Node, view: View, dir: string) {
)
}

const onPopState = () => {
if (window.OCP.Files.Router.query.openfile !== 'true') {
window.OCA.Viewer.close()
window.removeEventListener('popstate', onPopState)
}
}

/**
* Execute the viewer files action
* @param node The active node
Expand All @@ -36,6 +43,8 @@ async function execAction(node: Node, view: View, dir: string): Promise<boolean|
window.OCP.Files.Router.goToRoute(null, window.OCP.Files.Router.params, newQuery)
}

window.addEventListener('popstate', onPopState)

pushToHistory(node, view, dir)
window.OCA.Viewer.open({
path: node.path,
Expand Down

0 comments on commit 8e984be

Please sign in to comment.