jointrashposs/app.vue

212 lines
7.0 KiB
Vue
Raw Normal View History

2023-07-08 10:36:02 +02:00
<script setup lang="ts">
2023-12-20 23:52:08 +01:00
import TopIco from 'bi/chevron-up.svg';
import { locales } from '@/assets/data/locales';
2023-10-29 14:02:27 +01:00
import NProgress from 'nprogress';
2023-07-08 10:36:02 +02:00
import type { Graph, Thing } from 'schema-dts';
2023-12-23 09:43:51 +01:00
import { cleanDoubleSlashes, joinURL, parseURL, stringifyParsedURL, withTrailingSlash } from 'ufo';
2023-07-08 10:36:02 +02:00
const nuxtApp = useNuxtApp();
const { t, locale } = useI18n();
2023-07-08 10:36:02 +02:00
const route = useRoute();
const router = useRouter();
2023-07-08 19:23:27 +02:00
const colorMode = useColorMode();
2023-07-10 19:54:13 +02:00
const baseUrl = useRuntimeConfig().public.baseUrl as string;
2023-07-08 19:23:27 +02:00
router.beforeEach((to, from) => {
if (to.path === from.path) return;
if (!NProgress.isStarted()) {
NProgress.start();
}
})
nuxtApp.hook('page:loading:end', () => {
2023-10-29 14:18:36 +01:00
nextTick(() => {
setTimeout(() => {
NProgress.done();
}, 100);
});
2023-10-29 14:02:27 +01:00
});
2023-07-08 10:36:02 +02:00
const getDescription = (): string => {
if (route.meta.description != null && route.meta.description != "") {
return route.meta.description;
} else {
return t('_seo.defaultDescription');
}
}
2023-12-02 05:49:50 +01:00
const getTitle = () => route.meta.title ? `${route.meta.title} | ${t('_seo.siteName')}` : t('_seo.siteNameLong');
2023-07-08 10:36:02 +02:00
const getLdJson = (additionalGraphes: Thing[] = []): string => {
const ldJson: Graph = {
"@context": "https://schema.org",
"@graph": [
{
"@type": "Organization",
2023-07-10 19:54:13 +02:00
"@id": `${baseUrl}/#Organization`,
2023-07-08 10:36:02 +02:00
"name": "Misskey",
2023-07-10 19:54:13 +02:00
"url": `${baseUrl}/`,
2023-07-08 10:36:02 +02:00
"sameAs": [
2023-10-29 14:18:36 +01:00
"https://ja.wikipedia.org/wiki/Misskey",
2023-07-08 10:36:02 +02:00
],
"logo": {
"@type": "ImageObject",
2023-12-03 10:14:27 +01:00
"url": `${baseUrl}/img/icons/icon-256x256.png`
2023-07-08 10:36:02 +02:00
}
},
{
"@type": "WebSite",
2023-07-10 19:54:13 +02:00
"@id": `${baseUrl}/#WebPage`,
2023-07-08 10:36:02 +02:00
"name": t('_seo.siteName'),
"inLanguage": locale.value,
2023-07-10 19:54:13 +02:00
"url": `${baseUrl}${route.path}`,
2023-07-08 10:36:02 +02:00
"publisher": {
"@type": "Organization",
2023-07-10 19:54:13 +02:00
"@id": `${baseUrl}/#Organization`
2023-07-08 10:36:02 +02:00
},
"headline": getTitle(),
"description": getDescription()
},
]
};
ldJson['@graph'] = ldJson['@graph'].concat(additionalGraphes);
return JSON.stringify(ldJson);
};
const currentLocaleIso = computed(() => locales.find((e) => e?.code === locale.value)?.iso);
2023-07-08 10:36:02 +02:00
2023-07-10 19:54:13 +02:00
const head = useLocaleHead({
addSeoAttributes: true,
2023-07-10 19:54:13 +02:00
});
2023-12-23 09:43:51 +01:00
const i18nLinks = computed(() => head.value.link?.map((e) => {
if (e.rel === 'alternate') {
let href = e.href;
2024-02-09 13:19:47 +01:00
if (typeof e.hreflang === 'string' && (e.hreflang.includes('ja') || e.hreflang === 'x-default') && e.hreflang !== 'ja-KS') {
2023-12-23 09:43:51 +01:00
const url = parseURL(href);
url.pathname = joinURL('/ja/', url.pathname);
href = cleanDoubleSlashes(withTrailingSlash(stringifyParsedURL(url)));
} else {
href = cleanDoubleSlashes(withTrailingSlash(href));
}
return { ...e, rel: e.rel, href, hreflang: e.hreflang };
} else if (e.rel === 'canonical' && locale.value === 'ja') {
let href = e.href;
const url = parseURL(href);
url.pathname = joinURL('/ja/', url.pathname);
href = cleanDoubleSlashes(withTrailingSlash(stringifyParsedURL(url)));
return { ...e, rel: e.rel, href, hreflang: e.hreflang };
}
return e;
}));
2023-10-29 15:30:59 +01:00
/**
* 中国大陸で Google Fonts を使う裏技
* fonts.googleapis.com fonts.googleapis.cn
**/
const cnHead = (locale.value === 'cn') ? [
2023-11-03 08:55:33 +01:00
{ rel: 'preconnect', href: 'https://fonts.googleapis.cn' },
{ rel: 'preconnect', href: 'https://fonts.gstatic.cn' },
2023-10-29 15:30:59 +01:00
{ rel: 'stylesheet', href: 'https://fonts.googleapis.cn/css2?family=Capriola&family=Nunito:ital,wght@0,400;0,700;1,400;1,700&display=swap' }
2023-11-03 08:55:33 +01:00
] : [
{ rel: 'preconnect', href: 'https://fonts.googleapis.com' },
{ rel: 'preconnect', href: 'https://fonts.gstatic.com' },
{ rel: 'stylesheet', href: 'https://fonts.googleapis.com/css2?family=Capriola&family=Nunito:ital,wght@0,400;0,700;1,400;1,700&display=swap' },
];
2023-10-29 15:30:59 +01:00
2023-07-08 10:36:02 +02:00
useHead((): Record<string, any> => ({
htmlAttrs: {
lang: currentLocaleIso.value,
2023-07-09 20:09:50 +02:00
'data-bs-theme': colorMode.value,
2023-07-08 10:36:02 +02:00
},
title: getTitle(),
meta: [
{
name: "description",
content: getDescription(),
},
{
property: "og:title",
content: getTitle(),
},
2023-07-15 10:59:23 +02:00
{
property: "og:site_name",
content: t('_seo.siteName'),
},
2023-07-08 10:36:02 +02:00
{
property: "og:description",
content: getDescription(),
},
{
property: "og:image",
// TODO
2023-12-01 17:20:08 +01:00
content: () => route.meta.thumbnail ? route.meta.thumbnail : `${baseUrl}/img/og/misskey-hub-screenshot-l.png`,
2023-07-10 19:54:13 +02:00
},
...(head.value.meta?.map((e) => ({ property: e.property, content: e.content, })) || []),
2023-07-08 10:36:02 +02:00
],
link: [
2023-12-23 09:43:51 +01:00
...(i18nLinks.value || []),
2023-10-29 15:30:59 +01:00
...cnHead,
2023-07-08 10:36:02 +02:00
],
script: [
{ type: "application/ld+json", children: getLdJson(route.meta.graph) }
],
}));
2023-12-20 11:39:08 +01:00
/** サイト全体でひとつのScroll Posiitionを使う */
const scrollPos = useState('miHub_global_scrollPos', () => 0);
async function updatePos() {
scrollPos.value = document.body.getBoundingClientRect().y;
}
if (process.client) {
window.addEventListener('scroll', updatePos);
window.addEventListener('resize', updatePos);
}
onUnmounted(() => {
if (process.client) {
window.removeEventListener('scroll', updatePos);
window.removeEventListener('resize', updatePos);
}
});
const hideFrom = computed(() => route.meta.scrollButton ? route.meta.scrollButton?.hideFrom ?? -45 : -45);
const sbPosition = computed(() => route.meta.scrollButton ? { x: route.meta.scrollButton?.customPosition?.x ?? '2.5rem', y: route.meta.scrollButton?.customPosition?.y ?? '2.5rem' } ?? { x: '2.5rem', y: '2.5rem' } : { x: '2.5rem', y: '2.5rem' });
function scrollToTop() {
if (!process.client) return;
window.scrollTo({
top: 0,
behavior: 'smooth',
});
}
2023-07-08 10:36:02 +02:00
</script>
<template>
2023-10-29 14:18:36 +01:00
<div class="text-slate-800 dark:text-slate-200 bg-slate-100 dark:bg-gray-900">
2023-11-15 13:23:19 +01:00
<NuxtIsland name="GNoScript" />
2023-10-29 14:18:36 +01:00
<NuxtLayout>
<NuxtPage />
</NuxtLayout>
2023-07-12 15:50:26 +02:00
<ClientOnly>
2023-07-16 13:23:03 +02:00
<LazyGAiChan />
2023-07-12 15:50:26 +02:00
</ClientOnly>
2023-12-20 11:39:08 +01:00
<button
v-if="$route.meta.scrollButton !== false"
:class="[
'fixed h-14 w-14 p-[1.125rem] rounded-full bg-accent-600 text-white shadow-lg transition-opacity',
(hideFrom >= scrollPos) ? 'opacity-75 hover:opacity-100' : 'opacity-0 pointer-events-none',
$route.meta.scrollButton?.customClass ?? '',
$style.scrollToTopButton,
]"
@click="scrollToTop"
><TopIco class="h-5 w-5 stroke-1 stroke-current" /></button>
2023-10-29 14:18:36 +01:00
</div>
2023-07-08 10:36:02 +02:00
</template>
2023-12-20 11:39:08 +01:00
<style module>
.scrollToTopButton {
bottom: v-bind(sbPosition.y);
right: v-bind(sbPosition.x);
}
</style>