|
3 | 3 | * SPDX-License-Identifier: AGPL-3.0-or-later |
4 | 4 | */ |
5 | 5 | import type { RawLocation, Route } from 'vue-router' |
| 6 | + |
6 | 7 | import { generateUrl } from '@nextcloud/router' |
| 8 | +import { relative } from 'path' |
7 | 9 | import queryString from 'query-string' |
8 | 10 | import Router, { isNavigationFailure, NavigationFailureType } from 'vue-router' |
9 | 11 | import Vue from 'vue' |
| 12 | + |
| 13 | +import { useFilesStore } from '../store/files' |
| 14 | +import { useNavigation } from '../composables/useNavigation' |
| 15 | +import { usePathsStore } from '../store/paths' |
10 | 16 | import logger from '../logger' |
11 | 17 |
|
12 | 18 | Vue.use(Router) |
@@ -68,4 +74,60 @@ const router = new Router({ |
68 | 74 | }, |
69 | 75 | }) |
70 | 76 |
|
| 77 | +// If navigating back from a folder to a parent folder, |
| 78 | +// we need to keep the current dir fileid so it's highlighted |
| 79 | +// and scrolled into view. |
| 80 | +router.beforeEach((to, from, next) => { |
| 81 | + if (to.params?.parentIntercept) { |
| 82 | + delete to.params.parentIntercept |
| 83 | + next() |
| 84 | + return |
| 85 | + } |
| 86 | + |
| 87 | + const fromDir = (from.query?.dir || '/') as string |
| 88 | + const toDir = (to.query?.dir || '/') as string |
| 89 | + |
| 90 | + // We are going back to a parent directory |
| 91 | + if (relative(fromDir, toDir) === '..') { |
| 92 | + const { currentView } = useNavigation() |
| 93 | + const { getNode } = useFilesStore() |
| 94 | + const { getPath } = usePathsStore() |
| 95 | + |
| 96 | + if (!currentView.value?.id) { |
| 97 | + logger.error('No current view id found, cannot navigate to parent directory', { fromDir, toDir }) |
| 98 | + return next() |
| 99 | + } |
| 100 | + |
| 101 | + // Get the previous parent's file id |
| 102 | + const fromSource = getPath(currentView.value?.id, fromDir) |
| 103 | + if (!fromSource) { |
| 104 | + logger.error('No source found for the parent directory', { fromDir, toDir }) |
| 105 | + return next() |
| 106 | + } |
| 107 | + |
| 108 | + const fileId = getNode(fromSource)?.fileid |
| 109 | + if (!fileId) { |
| 110 | + logger.error('No fileid found for the parent directory', { fromDir, toDir, fromSource }) |
| 111 | + return next() |
| 112 | + } |
| 113 | + |
| 114 | + logger.debug('Navigating back to parent directory', { fromDir, toDir, fileId }) |
| 115 | + next({ |
| 116 | + name: 'filelist', |
| 117 | + query: to.query, |
| 118 | + params: { |
| 119 | + ...to.params, |
| 120 | + fileid: String(fileId), |
| 121 | + // Prevents the beforeEach from being called again |
| 122 | + parentIntercept: 'true', |
| 123 | + }, |
| 124 | + // Replace the current history entry |
| 125 | + replace: true, |
| 126 | + }) |
| 127 | + } |
| 128 | + |
| 129 | + // else, we just continue |
| 130 | + next() |
| 131 | +}) |
| 132 | + |
71 | 133 | export default router |
0 commit comments