UNPKG

@nextcloud/vue

Version:
50 lines (49 loc) 2.04 kB
import { formatRelativeTime, getCanonicalLocale } from "@nextcloud/l10n"; import { computed, toValue, ref, watchEffect, onUnmounted, readonly } from "vue"; import { r as register, b as t2, a as t } from "../../chunks/_l10n-DrTiip5c.mjs"; register(t2); const FEW_SECONDS_AGO = { long: t("a few seconds ago"), short: t("seconds ago"), // FOR TRANSLATORS: Shorter version of 'a few seconds ago' narrow: t("sec. ago") // FOR TRANSLATORS: If possible in your language an even shorter version of 'a few seconds ago' }; function useFormatRelativeTime(timestamp = Date.now(), opts = {}) { let timeoutId; const date = computed(() => new Date(toValue(timestamp))); const options = computed(() => { const { language, relativeTime: relativeTime2, ignoreSeconds } = toValue(opts); return { ...language && { language }, ...relativeTime2 && { relativeTime: relativeTime2 }, ignoreSeconds: ignoreSeconds ? FEW_SECONDS_AGO[relativeTime2 || "long"] : false }; }); const relativeTime = ref(""); watchEffect(() => updateRelativeTime()); function updateRelativeTime() { relativeTime.value = formatRelativeTime(date.value, options.value); if (toValue(opts).update !== false) { const diff = Math.abs(Date.now() - new Date(toValue(timestamp)).getTime()); const interval = diff > 12e4 || options.value.ignoreSeconds ? Math.min(diff / 60, 18e5) : 1e3; timeoutId = window.setTimeout(updateRelativeTime, interval); } } onUnmounted(() => timeoutId && window.clearTimeout(timeoutId)); return readonly(relativeTime); } function useFormatTime(timestamp, opts) { const options = computed(() => ({ locale: getCanonicalLocale(), format: { dateStyle: "short", timeStyle: "medium" }, ...toValue(opts) })); const formatter = computed(() => new Intl.DateTimeFormat(options.value.locale, options.value.format)); return computed(() => formatter.value.format(toValue(timestamp))); } export { useFormatRelativeTime, useFormatTime }; //# sourceMappingURL=index.mjs.map