2023-07-27 05:31:52 +00:00
|
|
|
<!--
|
|
|
|
SPDX-FileCopyrightText: syuilo and other misskey contributors
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
-->
|
|
|
|
|
2022-06-20 08:38:49 +00:00
|
|
|
<template>
|
2022-06-30 01:53:40 +00:00
|
|
|
<KeepAlive :max="defaultStore.state.numberOfPageCache">
|
2022-12-31 11:14:24 +00:00
|
|
|
<Suspense :timeout="0">
|
2022-07-19 12:36:33 +00:00
|
|
|
<component :is="currentPageComponent" :key="key" v-bind="Object.fromEntries(currentPageProps)"/>
|
|
|
|
|
|
|
|
<template #fallback>
|
2022-08-27 07:26:14 +00:00
|
|
|
<MkLoading/>
|
2022-07-19 12:36:33 +00:00
|
|
|
</template>
|
|
|
|
</Suspense>
|
2022-06-20 08:38:49 +00:00
|
|
|
</KeepAlive>
|
|
|
|
</template>
|
|
|
|
|
|
|
|
<script lang="ts" setup>
|
2023-02-16 14:09:41 +00:00
|
|
|
import { inject, onBeforeUnmount, provide } from 'vue';
|
2022-07-20 10:59:27 +00:00
|
|
|
import { Resolved, Router } from '@/nirax';
|
2023-09-19 07:37:43 +00:00
|
|
|
import { defaultStore } from '@/store.js';
|
2022-06-20 08:38:49 +00:00
|
|
|
|
|
|
|
const props = defineProps<{
|
|
|
|
router?: Router;
|
|
|
|
}>();
|
|
|
|
|
|
|
|
const router = props.router ?? inject('router');
|
|
|
|
|
|
|
|
if (router == null) {
|
|
|
|
throw new Error('no router provided');
|
|
|
|
}
|
|
|
|
|
2022-07-20 10:59:27 +00:00
|
|
|
const currentDepth = inject('routerCurrentDepth', 0);
|
|
|
|
provide('routerCurrentDepth', currentDepth + 1);
|
|
|
|
|
|
|
|
function resolveNested(current: Resolved, d = 0): Resolved | null {
|
|
|
|
if (d === currentDepth) {
|
|
|
|
return current;
|
|
|
|
} else {
|
|
|
|
if (current.child) {
|
|
|
|
return resolveNested(current.child, d + 1);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-20 08:38:49 +00:00
|
|
|
|
2022-07-20 10:59:27 +00:00
|
|
|
const current = resolveNested(router.current)!;
|
|
|
|
let currentPageComponent = $shallowRef(current.route.component);
|
|
|
|
let currentPageProps = $ref(current.props);
|
|
|
|
let key = $ref(current.route.path + JSON.stringify(Object.fromEntries(current.props)));
|
|
|
|
|
|
|
|
function onChange({ resolved, key: newKey }) {
|
|
|
|
const current = resolveNested(resolved);
|
|
|
|
if (current == null) return;
|
|
|
|
currentPageComponent = current.route.component;
|
|
|
|
currentPageProps = current.props;
|
|
|
|
key = current.route.path + JSON.stringify(Object.fromEntries(current.props));
|
2022-06-20 08:38:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
router.addListener('change', onChange);
|
|
|
|
|
2022-07-20 10:59:27 +00:00
|
|
|
onBeforeUnmount(() => {
|
2022-06-20 08:38:49 +00:00
|
|
|
router.removeListener('change', onChange);
|
|
|
|
});
|
|
|
|
</script>
|