Merge branch 'fix/MkTime' into 'develop'
refactor MkTime: replace the awful ?: chain with if-else; fix: force update ticker when props.time changed Co-authored-by: Lhcfl <Lhcfl@outlook.com> See merge request firefish/firefish!10797
This commit is contained in:
commit
b3d1be457b
1 changed files with 61 additions and 40 deletions
|
@ -25,15 +25,21 @@ const props = withDefaults(
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
|
function getDateSafe(n: Date | string | number) {
|
||||||
|
try {
|
||||||
|
if (n instanceof Date) {
|
||||||
|
return n;
|
||||||
|
}
|
||||||
|
return new Date(n);
|
||||||
|
} catch (err) {
|
||||||
|
return {
|
||||||
|
getTime: () => Number.NaN,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
const _time = computed(() =>
|
const _time = computed(() =>
|
||||||
props.time == null
|
props.time == null ? Number.NaN : getDateSafe(props.time).getTime(),
|
||||||
? Number.NaN
|
|
||||||
: typeof props.time === "number"
|
|
||||||
? props.time
|
|
||||||
: (props.time instanceof Date
|
|
||||||
? props.time
|
|
||||||
: new Date(props.time)
|
|
||||||
).getTime(),
|
|
||||||
);
|
);
|
||||||
const invalid = computed(() => Number.isNaN(_time.value));
|
const invalid = computed(() => Number.isNaN(_time.value));
|
||||||
const absolute = computed(() =>
|
const absolute = computed(() =>
|
||||||
|
@ -41,45 +47,57 @@ const absolute = computed(() =>
|
||||||
);
|
);
|
||||||
|
|
||||||
const now = ref(props.origin?.getTime() ?? Date.now());
|
const now = ref(props.origin?.getTime() ?? Date.now());
|
||||||
|
|
||||||
const relative = computed<string>(() => {
|
const relative = computed<string>(() => {
|
||||||
if (props.mode === "absolute") return ""; // absoluteではrelativeを使わないので計算しない
|
if (props.mode === "absolute") return ""; // absoluteではrelativeを使わないので計算しない
|
||||||
if (invalid.value) return i18n.ts._ago.invalid;
|
if (invalid.value) return i18n.ts._ago.invalid;
|
||||||
|
|
||||||
const ago = (now.value - _time.value) / 1000; /* ms */
|
const ago = (now.value - _time.value) / 1000; /* ms */
|
||||||
return ago >= 31536000
|
|
||||||
? i18n.t("_ago.yearsAgo", { n: Math.floor(ago / 31536000).toString() })
|
if (ago >= 31536000) {
|
||||||
: ago >= 2592000
|
return i18n.t("_ago.yearsAgo", {
|
||||||
? i18n.t("_ago.monthsAgo", {
|
n: Math.floor(ago / 31536000).toString(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (ago >= 2592000) {
|
||||||
|
return i18n.t("_ago.monthsAgo", {
|
||||||
n: Math.floor(ago / 2592000).toString(),
|
n: Math.floor(ago / 2592000).toString(),
|
||||||
})
|
});
|
||||||
: ago >= 604800
|
}
|
||||||
? i18n.t("_ago.weeksAgo", {
|
if (ago >= 604800) {
|
||||||
|
return i18n.t("_ago.weeksAgo", {
|
||||||
n: Math.floor(ago / 604800).toString(),
|
n: Math.floor(ago / 604800).toString(),
|
||||||
})
|
});
|
||||||
: ago >= 86400
|
}
|
||||||
? i18n.t("_ago.daysAgo", {
|
if (ago >= 86400) {
|
||||||
|
return i18n.t("_ago.daysAgo", {
|
||||||
n: Math.floor(ago / 86400).toString(),
|
n: Math.floor(ago / 86400).toString(),
|
||||||
})
|
});
|
||||||
: ago >= 3600
|
}
|
||||||
? i18n.t("_ago.hoursAgo", {
|
if (ago >= 3600) {
|
||||||
|
return i18n.t("_ago.hoursAgo", {
|
||||||
n: Math.floor(ago / 3600).toString(),
|
n: Math.floor(ago / 3600).toString(),
|
||||||
})
|
});
|
||||||
: ago >= 60
|
}
|
||||||
? i18n.t("_ago.minutesAgo", {
|
if (ago >= 60) {
|
||||||
|
return i18n.t("_ago.minutesAgo", {
|
||||||
n: (~~(ago / 60)).toString(),
|
n: (~~(ago / 60)).toString(),
|
||||||
})
|
});
|
||||||
: ago >= 10
|
}
|
||||||
? i18n.t("_ago.secondsAgo", {
|
if (ago >= 10) {
|
||||||
|
return i18n.t("_ago.secondsAgo", {
|
||||||
n: (~~(ago % 60)).toString(),
|
n: (~~(ago % 60)).toString(),
|
||||||
})
|
});
|
||||||
: ago >= -1
|
}
|
||||||
? i18n.ts._ago.justNow
|
if (ago >= -1) {
|
||||||
: i18n.ts._ago.future;
|
return i18n.ts._ago.justNow;
|
||||||
|
}
|
||||||
|
return i18n.ts._ago.future;
|
||||||
});
|
});
|
||||||
|
|
||||||
let tickId: number | undefined;
|
let tickId: number | undefined;
|
||||||
|
|
||||||
function tick() {
|
function tick(forceUpdateTicker = false) {
|
||||||
if (
|
if (
|
||||||
invalid.value ||
|
invalid.value ||
|
||||||
props.origin ||
|
props.origin ||
|
||||||
|
@ -101,13 +119,16 @@ function tick() {
|
||||||
|
|
||||||
if (!tickId) {
|
if (!tickId) {
|
||||||
tickId = window.setInterval(tick, next);
|
tickId = window.setInterval(tick, next);
|
||||||
} else if (prev < next) {
|
} else if (prev < next || forceUpdateTicker) {
|
||||||
window.clearInterval(tickId);
|
window.clearInterval(tickId);
|
||||||
tickId = window.setInterval(tick, next);
|
tickId = window.setInterval(tick, next);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
watch(() => props.time, tick);
|
watch(
|
||||||
|
() => props.time,
|
||||||
|
() => tick(true),
|
||||||
|
);
|
||||||
|
|
||||||
onMounted(() => {
|
onMounted(() => {
|
||||||
tick();
|
tick();
|
||||||
|
|
Loading…
Reference in a new issue