@live-change/vue3-components 0.8.9 → 0.8.10

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/index.js CHANGED
@@ -21,3 +21,6 @@ export { createReactiveObject }
21
21
  import { analytics, useAnalytics, installRouterAnalytics, synchronized, synchronizedList } from "./logic"
22
22
  export { analytics, useAnalytics, installRouterAnalytics, synchronized, synchronizedList }
23
23
 
24
+ export {
25
+ useLocale, watchLocale, getLocale, getLocaleObservable, updateLocale, captureLocale, localTime, getOtherOwnerLocale
26
+ } from './logic/locale'
@@ -0,0 +1,138 @@
1
+ import { useApi } from '@live-change/vue3-ssr'
2
+ import { ObservableValue } from '@live-change/dao'
3
+ import { ref, computed, onUnmounted, getCurrentInstance } from 'vue'
4
+
5
+ let localeObservable
6
+ let localeRef = ref()
7
+
8
+ export async function getOtherOwnerLocale(owner, context) {
9
+ if(localeObservable) localeObservable.unbindProperty(localeRef, 'value')
10
+ if(typeof window === 'undefined') {
11
+ const value = await api.get(api.views.localeSettings.localeSettings(owner))
12
+ localeObservable = new ObservableValue(value)
13
+ } else {
14
+ localeObservable = api.observable(api.views.localeSettings.localeSettings(owner))
15
+ }
16
+ localeObservable.bindProperty(localeRef, 'value')
17
+ localeObservable.wait()
18
+ return localeObservable.getValue()
19
+ }
20
+
21
+ export function getLocale(context) {
22
+ context = context ?? getCurrentInstance().appContext
23
+ return (async () => {
24
+ await getLocaleObservable(context)
25
+ await localeObservable.wait()
26
+ return localeObservable.getValue()
27
+ })()
28
+ }
29
+
30
+ export function getLocaleObservable(context) {
31
+ context = context ?? getCurrentInstance().appContext
32
+ return (async () => {
33
+ if(localeObservable) return localeObservable
34
+ const api = useApi(context)
35
+ if(typeof window === 'undefined') {
36
+ const value = await api.get(api.views.localeSettings.myLocaleSettings({}))
37
+ localeObservable = new ObservableValue(value)
38
+ } else {
39
+ localeObservable = api.observable(api.views.localeSettings.myLocaleSettings({}))
40
+ }
41
+ localeObservable.bindProperty(localeRef, 'value')
42
+ return localeObservable
43
+ })()
44
+ }
45
+
46
+ export function watchLocale(callback, context) {
47
+ context = context ?? getCurrentInstance().appContext
48
+ return (async () => {
49
+ const observable = await getLocaleObservable(context)
50
+ const observer = { set: callback }
51
+ observable.observe(observer)
52
+ onUnmounted(() => observable.unobserve(observer))
53
+ await observable.wait()
54
+ return observable.getValue()
55
+ })()
56
+ }
57
+
58
+ export function updateLocale(localSettingsUpdate, context) {
59
+ const api = useApi(context)
60
+ return api.actions.localeSettings.setOrUpdateMyLocaleSettings(localSettingsUpdate)
61
+ }
62
+ export function captureLocale(context) {
63
+ context = context || getCurrentInstance().appContext
64
+ if(typeof navigator === 'undefined') return
65
+ return (async () => {
66
+ const localeSettings = await getLocale(context)
67
+ console.log("LOCALE SETTINGS", localeSettings)
68
+
69
+ const dateTimeOptions = new Intl.DateTimeFormat().resolvedOptions()
70
+ const listOptions = new Intl.ListFormat().resolvedOptions()
71
+ const numberOptions = new Intl.NumberFormat().resolvedOptions()
72
+ const pluralOptions = new Intl.PluralRules().resolvedOptions()
73
+ const relativeTimeOptions = new Intl.RelativeTimeFormat().resolvedOptions()
74
+
75
+ if(!localeSettings
76
+ || localeSettings.capturedLanguage !== navigator.language
77
+ || JSON.stringify(localeSettings.capturedDateTime) !== JSON.stringify(dateTimeOptions)
78
+ || JSON.stringify(localeSettings.capturedList) !== JSON.stringify(listOptions)
79
+ || JSON.stringify(localeSettings.capturedNumber) !== JSON.stringify(numberOptions)
80
+ || JSON.stringify(localeSettings.capturedPlural) !== JSON.stringify(pluralOptions)
81
+ || JSON.stringify(localeSettings.capturedRelativeTime) !== JSON.stringify(relativeTimeOptions)
82
+ ) {
83
+ const update = {
84
+ capturedLanguage: navigator.language,
85
+ capturedDateTime: dateTimeOptions,
86
+ capturedList: listOptions,
87
+ capturedNumber: numberOptions,
88
+ capturedPlural: pluralOptions,
89
+ capturedRelativeTime: relativeTimeOptions
90
+ }
91
+ console.log("CAPTURED LOCALE SETTINGS", update)
92
+ await updateLocale(update, context)
93
+ }
94
+ })()
95
+ }
96
+
97
+ function getTimeZoneOffset(d, tz) {
98
+ const a = d.toLocaleString("ja", {timeZone: tz}).split(/[/\s:]/)
99
+ a[1]--
100
+ const t1 = Date.UTC.apply(null, a)
101
+ const t2 = new Date(d).setMilliseconds(0)
102
+ return (t2 - t1) / 60 / 1000
103
+ }
104
+
105
+ export const language = computed(() => localeRef.value?.language ?? localeRef.value?.capturedLanguage ??
106
+ (typeof navigator != 'undefined' && navigator.language))
107
+ export const currency = computed(() => localeRef.value?.currency ?? localeRef.value?.capturedCurrency ?? 'usd')
108
+ export const dateTime = computed(() => localeRef.value?.dateTime ?? localeRef.value?.capturedDateTime
109
+ ?? new Intl.DateTimeFormat().resolvedOptions())
110
+ export const list = computed(() => localeRef.value?.list ?? localeRef.value?.capturedList
111
+ ?? new Intl.ListFormat().resolvedOptions())
112
+ export const number = computed(() => localeRef.value?.number ?? localeRef.value?.capturedNumber
113
+ ?? new Intl.NumberFormat().resolvedOptions())
114
+ export const plural = computed(() => localeRef.value?.plural ?? localeRef.value?.capturedPlural
115
+ ?? new Intl.PluralRules().resolvedOptions())
116
+ export const relativeTime = computed(() => localeRef.value?.relativeTime ?? localeRef.value?.capturedRelativeTime
117
+ ?? new Intl.RelativeTimeFormat().resolvedOptions())
118
+
119
+ export const timezoneOffset = computed(() => getTimeZoneOffset(new Date(), dateTime.value?.timeZone))
120
+
121
+ export function localTime(date) {
122
+ return new Date(new Date(date).getTime() + timezoneOffset?.value)
123
+ }
124
+
125
+ export function useLocale(context) {
126
+ return {
127
+ language,
128
+ currency,
129
+ dateTime,
130
+ list,
131
+ number,
132
+ plural,
133
+ relativeTime,
134
+ timezoneOffset,
135
+ localTime
136
+ }
137
+ }
138
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/vue3-components",
3
- "version": "0.8.9",
3
+ "version": "0.8.10",
4
4
  "description": "Live Change Framework - vue components",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -21,10 +21,10 @@
21
21
  },
22
22
  "homepage": "https://github.com/live-change/live-change-framework-vue3",
23
23
  "dependencies": {
24
- "@live-change/vue3-ssr": "^0.8.9",
24
+ "@live-change/vue3-ssr": "^0.8.10",
25
25
  "debug": "^4.3.4",
26
26
  "mitt": "3.0.1",
27
27
  "vue": "^3.4.19"
28
28
  },
29
- "gitHead": "362dd55bd2773195e4cefd84080c10bd9fbaaf45"
29
+ "gitHead": "b6d6d60903abd25c8da4a55f653aafafb6cf04f7"
30
30
  }