@dargmuesli/nuxt-vio 20.0.0-beta.6 → 20.0.0-beta.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app/components/vio/_/VioTime.vue +6 -4
- package/app/composables/dateTime.ts +23 -0
- package/package.json +1 -1
- package/shared/utils/constants.ts +1 -0
- package/shared/utils/dateTime.ts +25 -0
- package/app/composables/timeZone.ts +0 -10
- /package/app/utils/{timezone.ts → dateTime.ts} +0 -0
- /package/server/middleware/{timezone.ts → dateTime.ts} +0 -0
- /package/server/utils/{timezone.ts → dateTime.ts} +0 -0
|
@@ -1,14 +1,12 @@
|
|
|
1
1
|
<template>
|
|
2
2
|
<NuxtTime
|
|
3
|
-
v-bind="
|
|
3
|
+
v-bind="forwardedProps"
|
|
4
4
|
:locale="props.options.locale || defaultLocale"
|
|
5
5
|
:time-zone="props.options.timeZone || defaultTimeZone"
|
|
6
6
|
/>
|
|
7
7
|
</template>
|
|
8
8
|
|
|
9
9
|
<script setup lang="ts">
|
|
10
|
-
import { reactiveOmit } from '@vueuse/core'
|
|
11
|
-
|
|
12
10
|
import type { NuxtTimeProps } from 'nuxt/app'
|
|
13
11
|
|
|
14
12
|
const { locale: defaultLocale } = useI18n()
|
|
@@ -27,5 +25,9 @@ const props = withDefaults(
|
|
|
27
25
|
},
|
|
28
26
|
)
|
|
29
27
|
|
|
30
|
-
const
|
|
28
|
+
const forwardedProps = computed(() => {
|
|
29
|
+
const { locale, timeZone, ...delegated } = props.options
|
|
30
|
+
|
|
31
|
+
return { datetime: props.datetime, ...delegated }
|
|
32
|
+
})
|
|
31
33
|
</script>
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export const useTimeZone = () =>
|
|
2
|
+
useNuxtApp().ssrContext?.event.context.$timeZone ||
|
|
3
|
+
useCookie(TIMEZONE_COOKIE_NAME, {
|
|
4
|
+
httpOnly: false,
|
|
5
|
+
sameSite: 'strict',
|
|
6
|
+
secure: true,
|
|
7
|
+
}).value ||
|
|
8
|
+
(import.meta.client
|
|
9
|
+
? Intl.DateTimeFormat().resolvedOptions().timeZone
|
|
10
|
+
: undefined)
|
|
11
|
+
|
|
12
|
+
export const useNow = () => useState(STATE_KEY_NOW, () => new Date())
|
|
13
|
+
|
|
14
|
+
export const useFromNow = () => {
|
|
15
|
+
const { locale } = useI18n()
|
|
16
|
+
const now = useNow()
|
|
17
|
+
|
|
18
|
+
const formatter = new Intl.RelativeTimeFormat(locale.value, {
|
|
19
|
+
numeric: 'auto',
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
return (to: Date) => getFromTo(now.value, to, formatter)
|
|
23
|
+
}
|
package/package.json
CHANGED
|
@@ -156,6 +156,7 @@ export const POLYFILLS = [
|
|
|
156
156
|
]
|
|
157
157
|
export const REGEX_UUID =
|
|
158
158
|
/^[a-z0-9]{8}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{4}-[a-z0-9]{12}$/
|
|
159
|
+
export const STATE_KEY_NOW = 'dateTimeNow'
|
|
159
160
|
export const TESTING_COOKIE_NAME = 'vio_is-testing'
|
|
160
161
|
export const TIMEZONE_COOKIE_NAME = [COOKIE_PREFIX, 'tz'].join(COOKIE_SEPARATOR)
|
|
161
162
|
export const TIMEZONE_HEADER_KEY = `X-${VIO_SITE_NAME}-Timezone`
|
package/shared/utils/dateTime.ts
CHANGED
|
@@ -116,3 +116,28 @@ export const getEmailDateTimeFormatter = (
|
|
|
116
116
|
...dateTimeFormatOptions,
|
|
117
117
|
timeZone: 'UTC',
|
|
118
118
|
})
|
|
119
|
+
|
|
120
|
+
export const getFromTo = (
|
|
121
|
+
from: Date,
|
|
122
|
+
to: Date,
|
|
123
|
+
formatter: Intl.RelativeTimeFormat,
|
|
124
|
+
) => {
|
|
125
|
+
const difference = (to.getTime() - from.getTime()) / 1000
|
|
126
|
+
const units = [
|
|
127
|
+
{ unit: 'year' as const, seconds: 365 * 24 * 3600 },
|
|
128
|
+
{ unit: 'month' as const, seconds: 30 * 24 * 3600 },
|
|
129
|
+
{ unit: 'week' as const, seconds: 7 * 24 * 3600 },
|
|
130
|
+
{ unit: 'day' as const, seconds: 24 * 3600 },
|
|
131
|
+
{ unit: 'hour' as const, seconds: 3600 },
|
|
132
|
+
{ unit: 'minute' as const, seconds: 60 },
|
|
133
|
+
{ unit: 'second' as const, seconds: 1 },
|
|
134
|
+
]
|
|
135
|
+
|
|
136
|
+
for (const { unit, seconds } of units) {
|
|
137
|
+
const delta = difference / seconds
|
|
138
|
+
|
|
139
|
+
if (Math.abs(delta) >= 1 || unit === 'second') {
|
|
140
|
+
return formatter.format(Math.round(delta), unit)
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
export const useTimeZone = () =>
|
|
2
|
-
useNuxtApp().ssrContext?.event.context.$timeZone ||
|
|
3
|
-
useCookie(TIMEZONE_COOKIE_NAME, {
|
|
4
|
-
httpOnly: false,
|
|
5
|
-
sameSite: 'strict',
|
|
6
|
-
secure: true,
|
|
7
|
-
}).value ||
|
|
8
|
-
(import.meta.client
|
|
9
|
-
? Intl.DateTimeFormat().resolvedOptions().timeZone
|
|
10
|
-
: undefined)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|