feat: use Intl.DateTimeFormat for gantt weekdays (#2766)
continuous-integration/drone/push Build is passing Details

Fixes #2728

Co-authored-by: Dominik Pschenitschni <mail@celement.de>
Reviewed-on: #2766
Reviewed-by: konrad <k@knt.li>
Co-authored-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
Co-committed-by: Dominik Pschenitschni <dpschen@noreply.kolaente.de>
This commit is contained in:
Dominik Pschenitschni 2022-11-30 15:58:04 +00:00 committed by konrad
parent 963f3bfb07
commit 3b95824f58
2 changed files with 31 additions and 24 deletions

View File

@ -1,6 +1,6 @@
<template> <template>
<Loading <Loading
v-if="props.isLoading && tasks.size || dayjsLanguageLoading" v-if="props.isLoading && !ganttBars.length || dayjsLanguageLoading"
class="gantt-container" class="gantt-container"
/> />
<div class="gantt-container" v-else> <div class="gantt-container" v-else>
@ -16,14 +16,14 @@
@dblclick-bar="openTask" @dblclick-bar="openTask"
:width="ganttChartWidth + 'px'" :width="ganttChartWidth + 'px'"
> >
<template #timeunit="{label, value}"> <template #timeunit="{value, date}">
<div <div
class="timeunit-wrapper" class="timeunit-wrapper"
:class="{'today': dayIsToday(label)}" :class="{'today': dateIsToday(date)}"
> >
<span>{{ value }}</span> <span>{{ value }}</span>
<span class="weekday"> <span class="weekday">
{{ weekdayFromTimeLabel(label) }} {{ weekDayFromDate(date) }}
</span> </span>
</div> </div>
</template> </template>
@ -40,9 +40,7 @@
<script setup lang="ts"> <script setup lang="ts">
import {computed, ref, watch, toRefs} from 'vue' import {computed, ref, watch, toRefs} from 'vue'
import {useRouter} from 'vue-router' import {useRouter} from 'vue-router'
import {format, parse} from 'date-fns' import {useNow} from '@vueuse/core'
import dayjs from 'dayjs'
import isToday from 'dayjs/plugin/isToday'
import {getHexColor} from '@/models/task' import {getHexColor} from '@/models/task'
@ -63,6 +61,7 @@ import {
import Loading from '@/components/misc/loading.vue' import Loading from '@/components/misc/loading.vue'
import {MILLISECONDS_A_DAY} from '@/constants/date' import {MILLISECONDS_A_DAY} from '@/constants/date'
import {useWeekDayFromDate} from '@/helpers/time/formatDate'
export interface GanttChartProps { export interface GanttChartProps {
isLoading: boolean, isLoading: boolean,
@ -85,7 +84,6 @@ const {tasks, filters} = toRefs(props)
// setup dayjs for vue-ganttastic // setup dayjs for vue-ganttastic
const dayjsLanguageLoading = ref(false) const dayjsLanguageLoading = ref(false)
// const dayjsLanguageLoading = useDayjsLanguageSync(dayjs) // const dayjsLanguageLoading = useDayjsLanguageSync(dayjs)
dayjs.extend(isToday)
extendDayjs() extendDayjs()
const router = useRouter() const router = useRouter()
@ -157,23 +155,16 @@ function openTask(e: {
}) })
} }
function parseTimeLabel(label: string) { const weekDayFromDate = useWeekDayFromDate()
return parse(label, 'dd.MMM', dateFromDate.value)
}
function weekdayFromTimeLabel(label: string): string { const today = useNow()
const parsed = parseTimeLabel(label) const dateIsToday = computed(() => (date: Date) => {
return format(parsed, 'E') return (
} date.getDate() === today.value.getDate() &&
date.getMonth() === today.value.getMonth() &&
function dayIsToday(label: string): boolean { date.getFullYear() === today.value.getFullYear()
const parsed = parseTimeLabel(label) )
})
const today = new Date()
return parsed.getDate() === today.getDate() &&
parsed.getMonth() === today.getMonth() &&
parsed.getFullYear() === today.getFullYear()
}
</script> </script>
<style scoped lang="scss"> <style scoped lang="scss">

View File

@ -5,6 +5,8 @@ import {format, formatDistanceToNow, formatISO as formatISOfns} from 'date-fns'
import {enGB, de, fr, ru} from 'date-fns/locale' import {enGB, de, fr, ru} from 'date-fns/locale'
import {i18n} from '@/i18n' import {i18n} from '@/i18n'
import { createSharedComposable, type MaybeRef } from '@vueuse/core'
import { computed, unref } from 'vue'
const locales = {en: enGB, de, ch: de, fr, ru} const locales = {en: enGB, de, ch: de, fr, ru}
@ -50,3 +52,17 @@ export const formatDateSince = (date) => {
export function formatISO(date) { export function formatISO(date) {
return date ? formatISOfns(date) : '' return date ? formatISOfns(date) : ''
} }
/**
* Because `Intl.DateTimeFormat` is expensive to instatiate we try to reuse it as often as possible,
* by creating a shared composable.
*/
export const useDateTimeFormatter = createSharedComposable((options?: MaybeRef<Intl.DateTimeFormatOptions>) => {
return computed(() => new Intl.DateTimeFormat(i18n.global.locale, unref(options)))
})
export function useWeekDayFromDate() {
const dateTimeFormatter = useDateTimeFormatter({ weekday: 'short' })
return computed(() => (date: Date) => dateTimeFormatter.value.format(date))
}