2018-02-05 06:25:19 +01:00
|
|
|
<template>
|
2023-04-08 02:01:42 +02:00
|
|
|
<time :title="absolute">
|
|
|
|
<template v-if="mode === 'relative'">{{ relative }}</template>
|
|
|
|
<template v-else-if="mode === 'absolute'">{{ absolute }}</template>
|
|
|
|
<template v-else-if="mode === 'detail'"
|
|
|
|
>{{ absolute }} ({{ relative }})</template
|
|
|
|
>
|
2023-04-30 18:29:50 +02:00
|
|
|
<slot></slot>
|
2023-04-08 02:01:42 +02:00
|
|
|
</time>
|
2018-02-05 06:25:19 +01:00
|
|
|
</template>
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2022-01-15 23:47:28 +01:00
|
|
|
<script lang="ts" setup>
|
2023-04-08 02:01:42 +02:00
|
|
|
import { onUnmounted } from "vue";
|
|
|
|
import { i18n } from "@/i18n";
|
|
|
|
|
|
|
|
const props = withDefaults(
|
|
|
|
defineProps<{
|
|
|
|
time: Date | string;
|
2023-04-30 18:29:50 +02:00
|
|
|
mode?: "relative" | "absolute" | "detail" | "none";
|
2023-04-08 02:01:42 +02:00
|
|
|
}>(),
|
|
|
|
{
|
|
|
|
mode: "relative",
|
|
|
|
}
|
|
|
|
);
|
2022-01-15 23:47:28 +01:00
|
|
|
|
2023-04-08 02:01:42 +02:00
|
|
|
const _time =
|
|
|
|
typeof props.time === "string" ? new Date(props.time) : props.time;
|
2022-01-15 23:47:28 +01:00
|
|
|
const absolute = _time.toLocaleString();
|
2018-06-10 01:03:02 +02:00
|
|
|
|
2022-08-06 12:20:53 +02:00
|
|
|
let now = $shallowRef(new Date());
|
2022-01-15 23:47:28 +01:00
|
|
|
const relative = $computed(() => {
|
2023-04-08 02:01:42 +02:00
|
|
|
const ago = (now.getTime() - _time.getTime()) / 1000; /*ms*/
|
|
|
|
return ago >= 31536000
|
|
|
|
? i18n.t("_ago.yearsAgo", { n: Math.round(ago / 31536000).toString() })
|
|
|
|
: ago >= 2592000
|
|
|
|
? i18n.t("_ago.monthsAgo", { n: Math.round(ago / 2592000).toString() })
|
|
|
|
: ago >= 604800
|
|
|
|
? i18n.t("_ago.weeksAgo", { n: Math.round(ago / 604800).toString() })
|
|
|
|
: ago >= 86400
|
|
|
|
? i18n.t("_ago.daysAgo", { n: Math.round(ago / 86400).toString() })
|
|
|
|
: ago >= 3600
|
|
|
|
? i18n.t("_ago.hoursAgo", { n: Math.round(ago / 3600).toString() })
|
|
|
|
: ago >= 60
|
|
|
|
? i18n.t("_ago.minutesAgo", { n: (~~(ago / 60)).toString() })
|
|
|
|
: ago >= 10
|
|
|
|
? i18n.t("_ago.secondsAgo", { n: (~~(ago % 60)).toString() })
|
|
|
|
: ago >= -1
|
|
|
|
? i18n.ts._ago.justNow
|
|
|
|
: i18n.ts._ago.future;
|
2018-02-13 05:49:48 +01:00
|
|
|
});
|
2022-01-15 23:47:28 +01:00
|
|
|
|
|
|
|
function tick() {
|
|
|
|
// TODO: パフォーマンス向上のため、このコンポーネントが画面内に表示されている場合のみ更新する
|
|
|
|
now = new Date();
|
|
|
|
|
|
|
|
tickId = window.setTimeout(() => {
|
|
|
|
window.requestAnimationFrame(tick);
|
|
|
|
}, 10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
let tickId: number;
|
|
|
|
|
2023-04-08 02:01:42 +02:00
|
|
|
if (props.mode === "relative" || props.mode === "detail") {
|
2022-01-15 23:47:28 +01:00
|
|
|
tickId = window.requestAnimationFrame(tick);
|
|
|
|
|
|
|
|
onUnmounted(() => {
|
2022-06-25 20:12:58 +02:00
|
|
|
window.cancelAnimationFrame(tickId);
|
2022-01-15 23:47:28 +01:00
|
|
|
});
|
|
|
|
}
|
2018-02-05 06:25:19 +01:00
|
|
|
</script>
|