@yh-ui/hooks 0.1.0
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/LICENSE +21 -0
- package/dist/index.cjs +585 -0
- package/dist/index.d.cts +259 -0
- package/dist/index.d.mts +259 -0
- package/dist/index.d.ts +259 -0
- package/dist/index.mjs +542 -0
- package/dist/use-cache/index.cjs +21 -0
- package/dist/use-cache/index.d.ts +10 -0
- package/dist/use-cache/index.mjs +15 -0
- package/dist/use-click-outside/index.cjs +20 -0
- package/dist/use-click-outside/index.d.ts +2 -0
- package/dist/use-click-outside/index.mjs +14 -0
- package/dist/use-config/index.cjs +30 -0
- package/dist/use-config/index.d.ts +22 -0
- package/dist/use-config/index.mjs +25 -0
- package/dist/use-event-listener/index.cjs +40 -0
- package/dist/use-event-listener/index.d.ts +2 -0
- package/dist/use-event-listener/index.mjs +34 -0
- package/dist/use-form-item/index.cjs +27 -0
- package/dist/use-form-item/index.d.ts +43 -0
- package/dist/use-form-item/index.mjs +20 -0
- package/dist/use-id/index.cjs +29 -0
- package/dist/use-id/index.d.ts +21 -0
- package/dist/use-id/index.mjs +21 -0
- package/dist/use-locale/dayjs-locale.cjs +129 -0
- package/dist/use-locale/dayjs-locale.d.ts +37 -0
- package/dist/use-locale/dayjs-locale.mjs +131 -0
- package/dist/use-locale/index.cjs +88 -0
- package/dist/use-locale/index.d.ts +16 -0
- package/dist/use-locale/index.mjs +63 -0
- package/dist/use-namespace/index.cjs +76 -0
- package/dist/use-namespace/index.d.ts +34 -0
- package/dist/use-namespace/index.mjs +68 -0
- package/dist/use-scroll-lock/index.cjs +73 -0
- package/dist/use-scroll-lock/index.d.ts +8 -0
- package/dist/use-scroll-lock/index.mjs +60 -0
- package/dist/use-virtual-scroll/index.cjs +64 -0
- package/dist/use-virtual-scroll/index.d.ts +35 -0
- package/dist/use-virtual-scroll/index.mjs +53 -0
- package/dist/use-z-index/index.cjs +57 -0
- package/dist/use-z-index/index.d.ts +30 -0
- package/dist/use-z-index/index.mjs +45 -0
- package/package.json +56 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.useEventListener = useEventListener;
|
|
7
|
+
var _vue = require("vue");
|
|
8
|
+
function useEventListener(target, event, handler, options) {
|
|
9
|
+
if (typeof window === "undefined") return;
|
|
10
|
+
const getTarget = () => {
|
|
11
|
+
if (typeof target === "function") {
|
|
12
|
+
return target();
|
|
13
|
+
}
|
|
14
|
+
return (0, _vue.unref)(target);
|
|
15
|
+
};
|
|
16
|
+
const add = () => {
|
|
17
|
+
const el = getTarget();
|
|
18
|
+
if (el) {
|
|
19
|
+
el.addEventListener(event, handler, options);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
const remove = () => {
|
|
23
|
+
const el = getTarget();
|
|
24
|
+
if (el) {
|
|
25
|
+
el.removeEventListener(event, handler, options);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
(0, _vue.onMounted)(add);
|
|
29
|
+
(0, _vue.onBeforeUnmount)(remove);
|
|
30
|
+
if ((0, _vue.isRef)(target)) {
|
|
31
|
+
(0, _vue.watch)(target, (newVal, oldVal) => {
|
|
32
|
+
if (oldVal) {
|
|
33
|
+
oldVal.removeEventListener(event, handler, options);
|
|
34
|
+
}
|
|
35
|
+
if (newVal) {
|
|
36
|
+
newVal.addEventListener(event, handler, options);
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { onMounted, onBeforeUnmount, isRef, watch, unref } from "vue";
|
|
2
|
+
export function useEventListener(target, event, handler, options) {
|
|
3
|
+
if (typeof window === "undefined") return;
|
|
4
|
+
const getTarget = () => {
|
|
5
|
+
if (typeof target === "function") {
|
|
6
|
+
return target();
|
|
7
|
+
}
|
|
8
|
+
return unref(target);
|
|
9
|
+
};
|
|
10
|
+
const add = () => {
|
|
11
|
+
const el = getTarget();
|
|
12
|
+
if (el) {
|
|
13
|
+
el.addEventListener(event, handler, options);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
const remove = () => {
|
|
17
|
+
const el = getTarget();
|
|
18
|
+
if (el) {
|
|
19
|
+
el.removeEventListener(event, handler, options);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
onMounted(add);
|
|
23
|
+
onBeforeUnmount(remove);
|
|
24
|
+
if (isRef(target)) {
|
|
25
|
+
watch(target, (newVal, oldVal) => {
|
|
26
|
+
if (oldVal) {
|
|
27
|
+
oldVal.removeEventListener(event, handler, options);
|
|
28
|
+
}
|
|
29
|
+
if (newVal) {
|
|
30
|
+
newVal.addEventListener(event, handler, options);
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.useFormItem = exports.FormItemContextKey = exports.FormContextKey = void 0;
|
|
7
|
+
var _vue = require("vue");
|
|
8
|
+
const FormContextKey = exports.FormContextKey = Symbol("FormContextKey");
|
|
9
|
+
const FormItemContextKey = exports.FormItemContextKey = Symbol("FormItemContextKey");
|
|
10
|
+
const useFormItem = () => {
|
|
11
|
+
const form = (0, _vue.inject)(FormContextKey, void 0);
|
|
12
|
+
const formItem = (0, _vue.inject)(FormItemContextKey, void 0);
|
|
13
|
+
return {
|
|
14
|
+
form,
|
|
15
|
+
formItem,
|
|
16
|
+
// 触发校验
|
|
17
|
+
validate: trigger => {
|
|
18
|
+
if (formItem) {
|
|
19
|
+
return formItem.validate(trigger).catch(() => {
|
|
20
|
+
return false;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
return Promise.resolve(true);
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
exports.useFormItem = useFormItem;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import type { InjectionKey } from 'vue';
|
|
2
|
+
export interface FormContext {
|
|
3
|
+
model: Record<string, unknown>;
|
|
4
|
+
rules?: Record<string, unknown>;
|
|
5
|
+
labelWidth?: string | number;
|
|
6
|
+
labelPosition?: string;
|
|
7
|
+
labelSuffix?: string;
|
|
8
|
+
showMessage?: boolean;
|
|
9
|
+
scrollToError?: boolean;
|
|
10
|
+
scrollIntoViewOptions?: boolean | ScrollIntoViewOptions;
|
|
11
|
+
disabled?: boolean;
|
|
12
|
+
size?: string;
|
|
13
|
+
statusIcon?: boolean;
|
|
14
|
+
layout?: string;
|
|
15
|
+
addField: (field: FormItemContext) => void;
|
|
16
|
+
removeField: (field: FormItemContext) => void;
|
|
17
|
+
themeOverrides?: Record<string, unknown>;
|
|
18
|
+
}
|
|
19
|
+
export interface FormItemContext {
|
|
20
|
+
prop: string;
|
|
21
|
+
validate: (trigger: string, callback?: (isValid: boolean) => void) => Promise<boolean | unknown>;
|
|
22
|
+
resetField: () => void;
|
|
23
|
+
clearValidate: () => void;
|
|
24
|
+
validateStatus: string;
|
|
25
|
+
validateMessage: string;
|
|
26
|
+
label: string;
|
|
27
|
+
errorId?: string;
|
|
28
|
+
inputId?: string;
|
|
29
|
+
size?: string;
|
|
30
|
+
disabled?: boolean;
|
|
31
|
+
}
|
|
32
|
+
export declare const FormContextKey: InjectionKey<FormContext>;
|
|
33
|
+
export declare const FormItemContextKey: InjectionKey<FormItemContext>;
|
|
34
|
+
/**
|
|
35
|
+
* useFormItem - 供组件内部使用的 Hook
|
|
36
|
+
* @description 获取表单项上下文。已优化内部触发校验逻辑,防止 Promise 拒绝导致控制台报错。
|
|
37
|
+
*/
|
|
38
|
+
export declare const useFormItem: () => {
|
|
39
|
+
form: FormContext | undefined;
|
|
40
|
+
formItem: FormItemContext | undefined;
|
|
41
|
+
validate: (trigger: string) => Promise<boolean | unknown>;
|
|
42
|
+
};
|
|
43
|
+
export type UseFormItemReturn = ReturnType<typeof useFormItem>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { inject } from "vue";
|
|
2
|
+
export const FormContextKey = Symbol("FormContextKey");
|
|
3
|
+
export const FormItemContextKey = Symbol("FormItemContextKey");
|
|
4
|
+
export const useFormItem = () => {
|
|
5
|
+
const form = inject(FormContextKey, void 0);
|
|
6
|
+
const formItem = inject(FormItemContextKey, void 0);
|
|
7
|
+
return {
|
|
8
|
+
form,
|
|
9
|
+
formItem,
|
|
10
|
+
// 触发校验
|
|
11
|
+
validate: (trigger) => {
|
|
12
|
+
if (formItem) {
|
|
13
|
+
return formItem.validate(trigger).catch(() => {
|
|
14
|
+
return false;
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return Promise.resolve(true);
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.useIdInjection = exports.useId = exports.idInjectionKey = void 0;
|
|
7
|
+
var _vue = require("vue");
|
|
8
|
+
const idInjectionKey = exports.idInjectionKey = Symbol("idInjectionKey");
|
|
9
|
+
const useId = idOverrides => {
|
|
10
|
+
const injectedId = (0, _vue.inject)(idInjectionKey, void 0);
|
|
11
|
+
const nativeId = (0, _vue.useId)();
|
|
12
|
+
const id = (0, _vue.computed)(() => {
|
|
13
|
+
const override = (0, _vue.unref)(idOverrides);
|
|
14
|
+
if (override) return override;
|
|
15
|
+
const injected = (0, _vue.unref)(injectedId);
|
|
16
|
+
if (injected) return injected;
|
|
17
|
+
return nativeId;
|
|
18
|
+
});
|
|
19
|
+
return id;
|
|
20
|
+
};
|
|
21
|
+
exports.useId = useId;
|
|
22
|
+
const useIdInjection = () => {
|
|
23
|
+
return {
|
|
24
|
+
prefix: (0, _vue.computed)(() => `yh-${Date.now()}`),
|
|
25
|
+
current: 0
|
|
26
|
+
// No longer using counter
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
exports.useIdInjection = useIdInjection;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { InjectionKey, Ref } from 'vue';
|
|
2
|
+
export declare const idInjectionKey: InjectionKey<Ref<string | undefined>>;
|
|
3
|
+
/**
|
|
4
|
+
* useId - 生成唯一 ID
|
|
5
|
+
* @param idOverrides - 可选的自定义 ID 前缀
|
|
6
|
+
* @returns 唯一 ID
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* const id = useId() // 'yh-id-1' (or vue native id)
|
|
10
|
+
* const customId = useId(ref('custom')) // 'custom'
|
|
11
|
+
*/
|
|
12
|
+
export declare const useId: (idOverrides?: Ref<string | undefined>) => Ref<string>;
|
|
13
|
+
/**
|
|
14
|
+
* useIdInjection - ID 注入工具
|
|
15
|
+
* @description 用于组件内部生成关联 ID
|
|
16
|
+
*/
|
|
17
|
+
export declare const useIdInjection: () => {
|
|
18
|
+
prefix: import("vue").ComputedRef<string>;
|
|
19
|
+
current: number;
|
|
20
|
+
};
|
|
21
|
+
export type UseIdReturn = ReturnType<typeof useId>;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { computed, inject, unref, useId as useVueId } from "vue";
|
|
2
|
+
export const idInjectionKey = Symbol("idInjectionKey");
|
|
3
|
+
export const useId = (idOverrides) => {
|
|
4
|
+
const injectedId = inject(idInjectionKey, void 0);
|
|
5
|
+
const nativeId = useVueId();
|
|
6
|
+
const id = computed(() => {
|
|
7
|
+
const override = unref(idOverrides);
|
|
8
|
+
if (override) return override;
|
|
9
|
+
const injected = unref(injectedId);
|
|
10
|
+
if (injected) return injected;
|
|
11
|
+
return nativeId;
|
|
12
|
+
});
|
|
13
|
+
return id;
|
|
14
|
+
};
|
|
15
|
+
export const useIdInjection = () => {
|
|
16
|
+
return {
|
|
17
|
+
prefix: computed(() => `yh-${Date.now()}`),
|
|
18
|
+
current: 0
|
|
19
|
+
// No longer using counter
|
|
20
|
+
};
|
|
21
|
+
};
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.updateDayjsMonths = exports.setDayjsLocaleSync = exports.setDayjsLocale = exports.getDayjsLocale = void 0;
|
|
7
|
+
var _dayjs = _interopRequireWildcard(require("dayjs"));
|
|
8
|
+
require("dayjs/locale/en");
|
|
9
|
+
require("dayjs/locale/zh-cn");
|
|
10
|
+
require("dayjs/locale/zh-tw");
|
|
11
|
+
require("dayjs/locale/ja");
|
|
12
|
+
require("dayjs/locale/ko");
|
|
13
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); }
|
|
14
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && {}.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
|
|
15
|
+
const dayjs = _dayjs.default || _dayjs;
|
|
16
|
+
const loadedLocales = /* @__PURE__ */new Set(["en", "zh-cn", "zh-tw", "ja", "ko"]);
|
|
17
|
+
const localeMapping = {
|
|
18
|
+
"zh-cn": "zh-cn",
|
|
19
|
+
"zh-tw": "zh-tw",
|
|
20
|
+
"zh-hk": "zh-hk",
|
|
21
|
+
"zh-mo": "zh-tw",
|
|
22
|
+
// 澳门使用繁体
|
|
23
|
+
en: "en",
|
|
24
|
+
ja: "ja",
|
|
25
|
+
ko: "ko",
|
|
26
|
+
de: "de",
|
|
27
|
+
fr: "fr",
|
|
28
|
+
es: "es",
|
|
29
|
+
pt: "pt",
|
|
30
|
+
"pt-br": "pt-br",
|
|
31
|
+
ru: "ru",
|
|
32
|
+
ar: "ar",
|
|
33
|
+
"ar-eg": "ar",
|
|
34
|
+
tr: "tr",
|
|
35
|
+
it: "it",
|
|
36
|
+
nl: "nl",
|
|
37
|
+
pl: "pl",
|
|
38
|
+
th: "th",
|
|
39
|
+
vi: "vi",
|
|
40
|
+
id: "id",
|
|
41
|
+
ms: "ms",
|
|
42
|
+
da: "da",
|
|
43
|
+
sv: "sv",
|
|
44
|
+
fi: "fi",
|
|
45
|
+
no: "nb",
|
|
46
|
+
"nb-NO": "nb",
|
|
47
|
+
cs: "cs",
|
|
48
|
+
sk: "sk",
|
|
49
|
+
uk: "uk",
|
|
50
|
+
hu: "hu",
|
|
51
|
+
ro: "ro",
|
|
52
|
+
bg: "bg",
|
|
53
|
+
az: "az",
|
|
54
|
+
fa: "fa",
|
|
55
|
+
hi: "hi",
|
|
56
|
+
pa: "pa-in",
|
|
57
|
+
el: "el",
|
|
58
|
+
ca: "ca",
|
|
59
|
+
tk: "tk",
|
|
60
|
+
ta: "ta",
|
|
61
|
+
lv: "lv",
|
|
62
|
+
af: "af",
|
|
63
|
+
et: "et",
|
|
64
|
+
sl: "sl",
|
|
65
|
+
he: "he",
|
|
66
|
+
lo: "lo",
|
|
67
|
+
lt: "lt",
|
|
68
|
+
mn: "mn",
|
|
69
|
+
kk: "kk",
|
|
70
|
+
ku: "ku",
|
|
71
|
+
ckb: "ku",
|
|
72
|
+
"ug-cn": "ug-cn",
|
|
73
|
+
km: "km",
|
|
74
|
+
sr: "sr",
|
|
75
|
+
eu: "eu",
|
|
76
|
+
ky: "ky",
|
|
77
|
+
"hy-am": "hy-am",
|
|
78
|
+
hr: "hr",
|
|
79
|
+
eo: "eo",
|
|
80
|
+
bn: "bn",
|
|
81
|
+
mg: "mg",
|
|
82
|
+
sw: "sw",
|
|
83
|
+
"uz-uz": "uz",
|
|
84
|
+
my: "my",
|
|
85
|
+
te: "te"
|
|
86
|
+
};
|
|
87
|
+
const getDayjsLocale = localeCode => {
|
|
88
|
+
return localeMapping[localeCode] || "en";
|
|
89
|
+
};
|
|
90
|
+
exports.getDayjsLocale = getDayjsLocale;
|
|
91
|
+
const setDayjsLocale = async localeCode => {
|
|
92
|
+
const dayjsLocale = getDayjsLocale(localeCode);
|
|
93
|
+
if (loadedLocales.has(dayjsLocale)) {
|
|
94
|
+
dayjs.locale(dayjsLocale);
|
|
95
|
+
return;
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
await Promise.resolve(`../../../../node_modules/dayjs/locale/${dayjsLocale}.js`).then(s => require(s));
|
|
99
|
+
loadedLocales.add(dayjsLocale);
|
|
100
|
+
dayjs.locale(dayjsLocale);
|
|
101
|
+
} catch {
|
|
102
|
+
console.warn(`[yh-ui] Failed to load dayjs locale: ${dayjsLocale}, falling back to 'en'`);
|
|
103
|
+
dayjs.locale("en");
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
exports.setDayjsLocale = setDayjsLocale;
|
|
107
|
+
const setDayjsLocaleSync = localeCode => {
|
|
108
|
+
const dayjsLocale = getDayjsLocale(localeCode);
|
|
109
|
+
if (loadedLocales.has(dayjsLocale)) {
|
|
110
|
+
dayjs.locale(dayjsLocale);
|
|
111
|
+
} else {
|
|
112
|
+
dayjs.locale("en");
|
|
113
|
+
setDayjsLocale(localeCode);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
exports.setDayjsLocaleSync = setDayjsLocaleSync;
|
|
117
|
+
const updateDayjsMonths = (localeCode, months) => {
|
|
118
|
+
const dayjsLocale = getDayjsLocale(localeCode);
|
|
119
|
+
const monthsArray = [months.jan, months.feb, months.mar, months.apr, months.may, months.jun, months.jul, months.aug, months.sep, months.oct, months.nov, months.dec];
|
|
120
|
+
try {
|
|
121
|
+
if (dayjs.updateLocale) {
|
|
122
|
+
dayjs.updateLocale(dayjsLocale, {
|
|
123
|
+
months: monthsArray,
|
|
124
|
+
monthsShort: monthsArray
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
} catch {}
|
|
128
|
+
};
|
|
129
|
+
exports.updateDayjsMonths = updateDayjsMonths;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import 'dayjs/locale/en';
|
|
2
|
+
import 'dayjs/locale/zh-cn';
|
|
3
|
+
import 'dayjs/locale/zh-tw';
|
|
4
|
+
import 'dayjs/locale/ja';
|
|
5
|
+
import 'dayjs/locale/ko';
|
|
6
|
+
/**
|
|
7
|
+
* 获取 dayjs locale code
|
|
8
|
+
*/
|
|
9
|
+
export declare const getDayjsLocale: (localeCode: string) => string;
|
|
10
|
+
/**
|
|
11
|
+
* 动态加载并设置 dayjs locale
|
|
12
|
+
* 使用动态导入来按需加载,避免打包所有语言
|
|
13
|
+
*/
|
|
14
|
+
export declare const setDayjsLocale: (localeCode: string) => Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* 同步设置 dayjs locale(不推荐,会阻塞)
|
|
17
|
+
* 用于需要立即同步的场景
|
|
18
|
+
*/
|
|
19
|
+
export declare const setDayjsLocaleSync: (localeCode: string) => void;
|
|
20
|
+
/**
|
|
21
|
+
* 使用自定义月份名称更新 dayjs locale
|
|
22
|
+
* 这样可以确保 dayjs 使用我们语言包中定义的月份名称
|
|
23
|
+
*/
|
|
24
|
+
export declare const updateDayjsMonths: (localeCode: string, months: {
|
|
25
|
+
jan: string;
|
|
26
|
+
feb: string;
|
|
27
|
+
mar: string;
|
|
28
|
+
apr: string;
|
|
29
|
+
may: string;
|
|
30
|
+
jun: string;
|
|
31
|
+
jul: string;
|
|
32
|
+
aug: string;
|
|
33
|
+
sep: string;
|
|
34
|
+
oct: string;
|
|
35
|
+
nov: string;
|
|
36
|
+
dec: string;
|
|
37
|
+
}) => void;
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
import * as _dayjs from "dayjs";
|
|
2
|
+
const dayjs = _dayjs.default || _dayjs;
|
|
3
|
+
import "dayjs/locale/en";
|
|
4
|
+
import "dayjs/locale/zh-cn";
|
|
5
|
+
import "dayjs/locale/zh-tw";
|
|
6
|
+
import "dayjs/locale/ja";
|
|
7
|
+
import "dayjs/locale/ko";
|
|
8
|
+
const loadedLocales = /* @__PURE__ */ new Set(["en", "zh-cn", "zh-tw", "ja", "ko"]);
|
|
9
|
+
const localeMapping = {
|
|
10
|
+
"zh-cn": "zh-cn",
|
|
11
|
+
"zh-tw": "zh-tw",
|
|
12
|
+
"zh-hk": "zh-hk",
|
|
13
|
+
"zh-mo": "zh-tw",
|
|
14
|
+
// 澳门使用繁体
|
|
15
|
+
en: "en",
|
|
16
|
+
ja: "ja",
|
|
17
|
+
ko: "ko",
|
|
18
|
+
de: "de",
|
|
19
|
+
fr: "fr",
|
|
20
|
+
es: "es",
|
|
21
|
+
pt: "pt",
|
|
22
|
+
"pt-br": "pt-br",
|
|
23
|
+
ru: "ru",
|
|
24
|
+
ar: "ar",
|
|
25
|
+
"ar-eg": "ar",
|
|
26
|
+
tr: "tr",
|
|
27
|
+
it: "it",
|
|
28
|
+
nl: "nl",
|
|
29
|
+
pl: "pl",
|
|
30
|
+
th: "th",
|
|
31
|
+
vi: "vi",
|
|
32
|
+
id: "id",
|
|
33
|
+
ms: "ms",
|
|
34
|
+
da: "da",
|
|
35
|
+
sv: "sv",
|
|
36
|
+
fi: "fi",
|
|
37
|
+
no: "nb",
|
|
38
|
+
"nb-NO": "nb",
|
|
39
|
+
cs: "cs",
|
|
40
|
+
sk: "sk",
|
|
41
|
+
uk: "uk",
|
|
42
|
+
hu: "hu",
|
|
43
|
+
ro: "ro",
|
|
44
|
+
bg: "bg",
|
|
45
|
+
az: "az",
|
|
46
|
+
fa: "fa",
|
|
47
|
+
hi: "hi",
|
|
48
|
+
pa: "pa-in",
|
|
49
|
+
el: "el",
|
|
50
|
+
ca: "ca",
|
|
51
|
+
tk: "tk",
|
|
52
|
+
ta: "ta",
|
|
53
|
+
lv: "lv",
|
|
54
|
+
af: "af",
|
|
55
|
+
et: "et",
|
|
56
|
+
sl: "sl",
|
|
57
|
+
he: "he",
|
|
58
|
+
lo: "lo",
|
|
59
|
+
lt: "lt",
|
|
60
|
+
mn: "mn",
|
|
61
|
+
kk: "kk",
|
|
62
|
+
ku: "ku",
|
|
63
|
+
ckb: "ku",
|
|
64
|
+
"ug-cn": "ug-cn",
|
|
65
|
+
km: "km",
|
|
66
|
+
sr: "sr",
|
|
67
|
+
eu: "eu",
|
|
68
|
+
ky: "ky",
|
|
69
|
+
"hy-am": "hy-am",
|
|
70
|
+
hr: "hr",
|
|
71
|
+
eo: "eo",
|
|
72
|
+
bn: "bn",
|
|
73
|
+
mg: "mg",
|
|
74
|
+
sw: "sw",
|
|
75
|
+
"uz-uz": "uz",
|
|
76
|
+
my: "my",
|
|
77
|
+
te: "te"
|
|
78
|
+
};
|
|
79
|
+
export const getDayjsLocale = (localeCode) => {
|
|
80
|
+
return localeMapping[localeCode] || "en";
|
|
81
|
+
};
|
|
82
|
+
export const setDayjsLocale = async (localeCode) => {
|
|
83
|
+
const dayjsLocale = getDayjsLocale(localeCode);
|
|
84
|
+
if (loadedLocales.has(dayjsLocale)) {
|
|
85
|
+
dayjs.locale(dayjsLocale);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
await import(`../../../../node_modules/dayjs/locale/${dayjsLocale}.js`);
|
|
90
|
+
loadedLocales.add(dayjsLocale);
|
|
91
|
+
dayjs.locale(dayjsLocale);
|
|
92
|
+
} catch {
|
|
93
|
+
console.warn(`[yh-ui] Failed to load dayjs locale: ${dayjsLocale}, falling back to 'en'`);
|
|
94
|
+
dayjs.locale("en");
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
export const setDayjsLocaleSync = (localeCode) => {
|
|
98
|
+
const dayjsLocale = getDayjsLocale(localeCode);
|
|
99
|
+
if (loadedLocales.has(dayjsLocale)) {
|
|
100
|
+
dayjs.locale(dayjsLocale);
|
|
101
|
+
} else {
|
|
102
|
+
dayjs.locale("en");
|
|
103
|
+
setDayjsLocale(localeCode);
|
|
104
|
+
}
|
|
105
|
+
};
|
|
106
|
+
export const updateDayjsMonths = (localeCode, months) => {
|
|
107
|
+
const dayjsLocale = getDayjsLocale(localeCode);
|
|
108
|
+
const monthsArray = [
|
|
109
|
+
months.jan,
|
|
110
|
+
months.feb,
|
|
111
|
+
months.mar,
|
|
112
|
+
months.apr,
|
|
113
|
+
months.may,
|
|
114
|
+
months.jun,
|
|
115
|
+
months.jul,
|
|
116
|
+
months.aug,
|
|
117
|
+
months.sep,
|
|
118
|
+
months.oct,
|
|
119
|
+
months.nov,
|
|
120
|
+
months.dec
|
|
121
|
+
];
|
|
122
|
+
try {
|
|
123
|
+
if (dayjs.updateLocale) {
|
|
124
|
+
dayjs.updateLocale(dayjsLocale, {
|
|
125
|
+
months: monthsArray,
|
|
126
|
+
monthsShort: monthsArray
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
} catch {
|
|
130
|
+
}
|
|
131
|
+
};
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
Object.defineProperty(exports, "getDayjsLocale", {
|
|
7
|
+
enumerable: true,
|
|
8
|
+
get: function () {
|
|
9
|
+
return _dayjsLocale.getDayjsLocale;
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
Object.defineProperty(exports, "setDayjsLocale", {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _dayjsLocale.setDayjsLocale;
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
Object.defineProperty(exports, "setDayjsLocaleSync", {
|
|
19
|
+
enumerable: true,
|
|
20
|
+
get: function () {
|
|
21
|
+
return _dayjsLocale.setDayjsLocaleSync;
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
Object.defineProperty(exports, "updateDayjsMonths", {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get: function () {
|
|
27
|
+
return _dayjsLocale.updateDayjsMonths;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
exports.useLocale = void 0;
|
|
31
|
+
var _vue = require("vue");
|
|
32
|
+
var _locale = require("@yh-ui/locale");
|
|
33
|
+
var _useConfig = require("../use-config/index.cjs");
|
|
34
|
+
var _dayjsLocale = require("./dayjs-locale.cjs");
|
|
35
|
+
const useLocale = localeOverrides => {
|
|
36
|
+
const {
|
|
37
|
+
globalLocale
|
|
38
|
+
} = (0, _useConfig.useConfig)();
|
|
39
|
+
const locale = (0, _vue.computed)(() => {
|
|
40
|
+
return (0, _vue.unref)(localeOverrides) ?? (0, _vue.unref)(globalLocale) ?? _locale.zhCn;
|
|
41
|
+
});
|
|
42
|
+
const lang = (0, _vue.computed)(() => locale.value.name);
|
|
43
|
+
(0, _vue.watch)(lang, newLang => {
|
|
44
|
+
(0, _dayjsLocale.setDayjsLocale)(newLang);
|
|
45
|
+
}, {
|
|
46
|
+
immediate: true
|
|
47
|
+
});
|
|
48
|
+
const t = (path, options) => {
|
|
49
|
+
const keys = path.split(".");
|
|
50
|
+
let result = locale.value.yh;
|
|
51
|
+
for (const key of keys) {
|
|
52
|
+
if (result && typeof result === "object") {
|
|
53
|
+
result = result[key];
|
|
54
|
+
} else {
|
|
55
|
+
result = void 0;
|
|
56
|
+
}
|
|
57
|
+
if (result === void 0) return path;
|
|
58
|
+
}
|
|
59
|
+
if (typeof result !== "string") return path;
|
|
60
|
+
if (options) {
|
|
61
|
+
return result.replace(/\{(\w+)\}/g, (_match, key) => {
|
|
62
|
+
const val = options[key];
|
|
63
|
+
return val !== void 0 ? String(val) : `{${key}}`;
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
67
|
+
};
|
|
68
|
+
const tRaw = path => {
|
|
69
|
+
const keys = path.split(".");
|
|
70
|
+
let result = locale.value.yh;
|
|
71
|
+
for (const key of keys) {
|
|
72
|
+
if (result && typeof result === "object") {
|
|
73
|
+
result = result[key];
|
|
74
|
+
} else {
|
|
75
|
+
result = void 0;
|
|
76
|
+
}
|
|
77
|
+
if (result === void 0) return path;
|
|
78
|
+
}
|
|
79
|
+
return result;
|
|
80
|
+
};
|
|
81
|
+
return {
|
|
82
|
+
locale,
|
|
83
|
+
lang,
|
|
84
|
+
t,
|
|
85
|
+
tRaw
|
|
86
|
+
};
|
|
87
|
+
};
|
|
88
|
+
exports.useLocale = useLocale;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { Ref } from 'vue';
|
|
2
|
+
import type { Language } from '@yh-ui/locale';
|
|
3
|
+
export type { Language };
|
|
4
|
+
export { setDayjsLocale, getDayjsLocale, setDayjsLocaleSync, updateDayjsMonths } from './dayjs-locale';
|
|
5
|
+
/**
|
|
6
|
+
* useLocale - 国际化
|
|
7
|
+
* @param localeOverrides - 可选的自定义语言包
|
|
8
|
+
* @returns 国际化相关方法
|
|
9
|
+
*/
|
|
10
|
+
export declare const useLocale: (localeOverrides?: Ref<Language>) => {
|
|
11
|
+
locale: import("vue").ComputedRef<Language>;
|
|
12
|
+
lang: import("vue").ComputedRef<string>;
|
|
13
|
+
t: (path: string, options?: Record<string, string | number>) => string;
|
|
14
|
+
tRaw: <T = unknown>(path: string) => T;
|
|
15
|
+
};
|
|
16
|
+
export type UseLocaleReturn = ReturnType<typeof useLocale>;
|