Allow for template inside Timeago component that shows unless the time string is 'just now'

This commit is contained in:
Sean King 2022-08-02 23:19:25 -06:00
parent b70d50407c
commit 04e62df377
No known key found for this signature in database
GPG Key ID: 510C52BACD6E7257
2 changed files with 25 additions and 19 deletions

View File

@ -336,16 +336,12 @@
tag="span" tag="span"
> >
<template #time> <template #time>
<i18n-t <Timeago
keypath="time.in_past" template-key="time.in_past"
tag="span" :time="status.edited_at"
> :auto-update="60"
<Timeago :long-format="true"
:time="status.edited_at" />
:auto-update="60"
:long-format="true"
/>
</i18n-t>
</template> </template>
</i18n-t> </i18n-t>
</div> </div>

View File

@ -3,7 +3,7 @@
:datetime="time" :datetime="time"
:title="localeDateString" :title="localeDateString"
> >
{{ $tc(relativeTime.key, relativeTime.num, [relativeTime.num]) }} {{ relativeTimeString }}
</time> </time>
</template> </template>
@ -13,7 +13,7 @@ import localeService from 'src/services/locale/locale.service.js'
export default { export default {
name: 'Timeago', name: 'Timeago',
props: ['time', 'autoUpdate', 'longFormat', 'nowThreshold'], props: ['time', 'autoUpdate', 'longFormat', 'nowThreshold', 'templateKey'],
data () { data () {
return { return {
relativeTime: { key: 'time.now', num: 0 }, relativeTime: { key: 'time.now', num: 0 },
@ -26,6 +26,23 @@ export default {
return typeof this.time === 'string' return typeof this.time === 'string'
? new Date(Date.parse(this.time)).toLocaleString(browserLocale) ? new Date(Date.parse(this.time)).toLocaleString(browserLocale)
: this.time.toLocaleString(browserLocale) : this.time.toLocaleString(browserLocale)
},
relativeTimeString () {
const timeString = this.$i18n.tc(this.relativeTime.key, this.relativeTime.num, [this.relativeTime.num])
if (typeof this.templateKey === 'string' && this.relativeTime.key !== 'time.now') {
return this.$i18n.t(this.templateKey, [timeString])
}
return timeString
}
},
watch: {
time (newVal, oldVal) {
if (oldVal !== newVal) {
clearTimeout(this.interval)
this.refreshRelativeTimeObject()
}
} }
}, },
created () { created () {
@ -34,13 +51,6 @@ export default {
unmounted () { unmounted () {
clearTimeout(this.interval) clearTimeout(this.interval)
}, },
watch: {
time (newVal, oldVal) {
if (oldVal !== newVal) {
this.refreshRelativeTimeObject()
}
}
},
methods: { methods: {
refreshRelativeTimeObject () { refreshRelativeTimeObject () {
const nowThreshold = typeof this.nowThreshold === 'number' ? this.nowThreshold : 1 const nowThreshold = typeof this.nowThreshold === 'number' ? this.nowThreshold : 1