47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
/*
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
import { inject } from 'vue';
|
|
import { page } from '@/router.definition.js';
|
|
import { $i } from '@/i.js';
|
|
import { Nirax } from '@/lib/nirax.js';
|
|
import { ROUTE_DEF } from '@/router.definition.js';
|
|
import { analytics } from '@/analytics.js';
|
|
import { DI } from '@/di.js';
|
|
|
|
export type Router = Nirax<typeof ROUTE_DEF>;
|
|
|
|
export function createRouter(fullPath: string): Router {
|
|
return new Nirax(ROUTE_DEF, fullPath, !!$i, page(() => import('@/pages/not-found.vue')));
|
|
}
|
|
|
|
export const mainRouter = createRouter(window.location.pathname + window.location.search + window.location.hash);
|
|
|
|
window.addEventListener('popstate', (event) => {
|
|
mainRouter.replace(window.location.pathname + window.location.search + window.location.hash);
|
|
});
|
|
|
|
mainRouter.addListener('push', ctx => {
|
|
window.history.pushState({ }, '', ctx.fullPath);
|
|
});
|
|
|
|
mainRouter.addListener('replace', ctx => {
|
|
window.history.replaceState({ }, '', ctx.fullPath);
|
|
});
|
|
|
|
mainRouter.addListener('change', ctx => {
|
|
if (_DEV_) console.log('mainRouter: change', ctx.fullPath);
|
|
analytics.page({
|
|
path: ctx.fullPath,
|
|
title: ctx.fullPath,
|
|
});
|
|
});
|
|
|
|
mainRouter.init();
|
|
|
|
export function useRouter(): Router {
|
|
return inject(DI.router, null) ?? mainRouter;
|
|
}
|