Skip to content
代码片段 群组 项目
未验证 提交 cd14c6ec 编辑于 作者: Paulina Sedlak-Jakubowska's avatar Paulina Sedlak-Jakubowska 提交者: GitLab
浏览文件

WIP: Update repository router to be compatible with Vue3 mode

上级 63a30cb4
No related branches found
No related tags found
无相关合并请求
...@@ -92,6 +92,16 @@ const transformOptions = (options = {}) => { ...@@ -92,6 +92,16 @@ const transformOptions = (options = {}) => {
const installed = new WeakMap(); const installed = new WeakMap();
export const getMatchedComponents = (instance, path) => {
if (instance.getMatchedComponents) {
return instance.getMatchedComponents(path);
}
const route = path ? instance.resolve(path) : instance.currentRoute.value;
return route.matched.flatMap((record) => Object.values(record.components));
};
export default class VueRouterCompat { export default class VueRouterCompat {
constructor(options) { constructor(options) {
// eslint-disable-next-line no-constructor-return // eslint-disable-next-line no-constructor-return
......
...@@ -9,11 +9,21 @@ import { getRefType } from './utils/ref_type'; ...@@ -9,11 +9,21 @@ import { getRefType } from './utils/ref_type';
Vue.use(VueRouter); Vue.use(VueRouter);
const normalizePathParam = (pathParam) => {
// Vue Router 4 when there's more than one `:path` segment
if (Array.isArray(pathParam)) {
return joinPaths(...pathParam);
}
// Vue Router 3, or when there's zero or one `:path` segments.
return pathParam?.replace(/^\//, '') || '/';
};
export default function createRouter(base, baseRef) { export default function createRouter(base, baseRef) {
const treePathRoute = { const treePathRoute = {
component: TreePage, component: TreePage,
props: (route) => ({ props: (route) => ({
path: route.params.path?.replace(/^\//, '') || '/', path: normalizePathParam(route.params.path),
refType: getRefType(route.query.ref_type || null), refType: getRefType(route.query.ref_type || null),
}), }),
}; };
...@@ -36,25 +46,25 @@ export default function createRouter(base, baseRef) { ...@@ -36,25 +46,25 @@ export default function createRouter(base, baseRef) {
{ {
name: 'treePathDecoded', name: 'treePathDecoded',
// Sometimes the ref needs decoding depending on how the backend sends it to us // Sometimes the ref needs decoding depending on how the backend sends it to us
path: `(/-)?/tree/${decodeURI(baseRef)}/:path*`, path: `/:dash(-)?/tree/${decodeURI(baseRef)}/:path*`,
...treePathRoute, ...treePathRoute,
}, },
{ {
name: 'treePath', name: 'treePath',
// Support without decoding as well just in case the ref doesn't need to be decoded // Support without decoding as well just in case the ref doesn't need to be decoded
path: `(/-)?/tree/${escapeRegExp(baseRef)}/:path*`, path: `/:dash(-)?/tree/${escapeRegExp(baseRef)}/:path*`,
...treePathRoute, ...treePathRoute,
}, },
{ {
name: 'blobPathDecoded', name: 'blobPathDecoded',
// Sometimes the ref needs decoding depending on how the backend sends it to us // Sometimes the ref needs decoding depending on how the backend sends it to us
path: `(/-)?/blob/${decodeURI(baseRef)}/:path*`, path: `/:dash(-)?/blob/${decodeURI(baseRef)}/:path*`,
...blobPathRoute, ...blobPathRoute,
}, },
{ {
name: 'blobPath', name: 'blobPath',
// Support without decoding as well just in case the ref doesn't need to be decoded // Support without decoding as well just in case the ref doesn't need to be decoded
path: `(/-)?/blob/${escapeRegExp(baseRef)}/:path*`, path: `/:dash(-)?/blob/${escapeRegExp(baseRef)}/:path*`,
...blobPathRoute, ...blobPathRoute,
}, },
{ {
...@@ -80,7 +90,7 @@ export default function createRouter(base, baseRef) { ...@@ -80,7 +90,7 @@ export default function createRouter(base, baseRef) {
'edit', 'edit',
decodeURI(baseRef), decodeURI(baseRef),
'-', '-',
to.params.path || '', normalizePathParam(to.params.path),
needsClosingSlash && '/', needsClosingSlash && '/',
), ),
); );
......
...@@ -233,10 +233,8 @@ spec/frontend/ref/init_ambiguous_ref_modal_spec.js ...@@ -233,10 +233,8 @@ spec/frontend/ref/init_ambiguous_ref_modal_spec.js
spec/frontend/releases/components/app_edit_new_spec.js spec/frontend/releases/components/app_edit_new_spec.js
spec/frontend/releases/components/asset_links_form_spec.js spec/frontend/releases/components/asset_links_form_spec.js
spec/frontend/repository/components/header_area/blob_controls_spec.js spec/frontend/repository/components/header_area/blob_controls_spec.js
spec/frontend/repository/components/header_area/blob_overflow_menu_spec.js
spec/frontend/repository/components/table/index_spec.js spec/frontend/repository/components/table/index_spec.js
spec/frontend/repository/components/table/row_spec.js spec/frontend/repository/components/table/row_spec.js
spec/frontend/repository/router_spec.js
spec/frontend/search/sidebar/components/checkbox_filter_spec.js spec/frontend/search/sidebar/components/checkbox_filter_spec.js
spec/frontend/search/topbar/components/app_spec.js spec/frontend/search/topbar/components/app_spec.js
spec/frontend/sessions/new/components/email_verification_spec.js spec/frontend/sessions/new/components/email_verification_spec.js
......
...@@ -2,6 +2,7 @@ import BlobPage from '~/repository/pages/blob.vue'; ...@@ -2,6 +2,7 @@ import BlobPage from '~/repository/pages/blob.vue';
import IndexPage from '~/repository/pages/index.vue'; import IndexPage from '~/repository/pages/index.vue';
import TreePage from '~/repository/pages/tree.vue'; import TreePage from '~/repository/pages/tree.vue';
import createRouter from '~/repository/router'; import createRouter from '~/repository/router';
import { getMatchedComponents } from '~/lib/utils/vue3compat/vue_router';
describe('Repository router spec', () => { describe('Repository router spec', () => {
it.each` it.each`
...@@ -11,18 +12,13 @@ describe('Repository router spec', () => { ...@@ -11,18 +12,13 @@ describe('Repository router spec', () => {
${'/tree/feat(test)'} | ${'feat(test)'} | ${TreePage} | ${'TreePage'} ${'/tree/feat(test)'} | ${'feat(test)'} | ${TreePage} | ${'TreePage'}
${'/-/tree/main'} | ${'main'} | ${TreePage} | ${'TreePage'} ${'/-/tree/main'} | ${'main'} | ${TreePage} | ${'TreePage'}
${'/-/tree/main/app/assets'} | ${'main'} | ${TreePage} | ${'TreePage'} ${'/-/tree/main/app/assets'} | ${'main'} | ${TreePage} | ${'TreePage'}
${'/-/tree/123/app/assets'} | ${'main'} | ${null} | ${'null'}
${'/-/blob/main/file.md'} | ${'main'} | ${BlobPage} | ${'BlobPage'} ${'/-/blob/main/file.md'} | ${'main'} | ${BlobPage} | ${'BlobPage'}
`('sets component as $componentName for path "$path"', ({ path, component, branch }) => { `('sets component as $componentName for path "$path"', ({ path, component, branch }) => {
const router = createRouter('', branch); const router = createRouter('', branch);
const componentsForRoute = router.getMatchedComponents(path); const componentsForRoute = getMatchedComponents(router, path);
expect(componentsForRoute.length).toBe(component ? 1 : 0); expect(componentsForRoute).toEqual([component]);
if (component) {
expect(componentsForRoute).toContain(component);
}
}); });
describe('Storing Web IDE path globally', () => { describe('Storing Web IDE path globally', () => {
...@@ -45,11 +41,14 @@ describe('Repository router spec', () => { ...@@ -45,11 +41,14 @@ describe('Repository router spec', () => {
${'/-/tree/main'} | ${'main'} | ${`/-/ide/project/${proj}/edit/main/-/`} ${'/-/tree/main'} | ${'main'} | ${`/-/ide/project/${proj}/edit/main/-/`}
${'/-/tree/main/app/assets'} | ${'main'} | ${`/-/ide/project/${proj}/edit/main/-/app/assets/`} ${'/-/tree/main/app/assets'} | ${'main'} | ${`/-/ide/project/${proj}/edit/main/-/app/assets/`}
${'/-/blob/main/file.md'} | ${'main'} | ${`/-/ide/project/${proj}/edit/main/-/file.md`} ${'/-/blob/main/file.md'} | ${'main'} | ${`/-/ide/project/${proj}/edit/main/-/file.md`}
`('generates the correct Web IDE url for $path', ({ path, branch, expectedPath } = {}) => { `(
const router = createRouter(proj, branch); 'generates the correct Web IDE url for $path',
async ({ path, branch, expectedPath } = {}) => {
router.push(path); const router = createRouter(proj, branch);
expect(window.gl.webIDEPath).toBe(expectedPath);
}); await router.push(path);
expect(window.gl.webIDEPath).toBe(expectedPath);
},
);
}); });
}); });
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册