zcw-shared 1.28.0 → 1.29.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/dist/functions/algorithm/lcsVisual.d.ts +6 -0
- package/dist/functions/algorithm/lcsVisual.js +88 -0
- package/dist/functions/algorithm/lcsVisual.js.map +1 -0
- package/dist/functions/algorithm/mergeSortVisual.d.ts +5 -0
- package/dist/functions/algorithm/mergeSortVisual.js +103 -0
- package/dist/functions/algorithm/mergeSortVisual.js.map +1 -0
- package/dist/functions/algorithm/nQueensVisual.d.ts +14 -0
- package/dist/functions/algorithm/nQueensVisual.js +94 -0
- package/dist/functions/algorithm/nQueensVisual.js.map +1 -0
- package/dist/functions/algorithm/numIslandsVisual.d.ts +17 -0
- package/dist/functions/algorithm/numIslandsVisual.js +102 -0
- package/dist/functions/algorithm/numIslandsVisual.js.map +1 -0
- package/dist/functions/algorithm/quickSortVisual.d.ts +18 -0
- package/dist/functions/algorithm/quickSortVisual.js +104 -0
- package/dist/functions/algorithm/quickSortVisual.js.map +1 -0
- package/dist/functions/browser/detectDevice.d.ts +10 -0
- package/dist/functions/browser/detectDevice.js +49 -0
- package/dist/functions/browser/detectDevice.js.map +1 -0
- package/dist/functions/cookie/getCookie.d.ts +5 -0
- package/dist/functions/cookie/getCookie.js +14 -0
- package/dist/functions/cookie/getCookie.js.map +1 -0
- package/dist/functions/cookie/removeCookie.d.ts +8 -0
- package/dist/functions/cookie/removeCookie.js +12 -0
- package/dist/functions/cookie/removeCookie.js.map +1 -0
- package/dist/functions/cookie/setCookie.d.ts +11 -0
- package/dist/functions/cookie/setCookie.js +23 -0
- package/dist/functions/cookie/setCookie.js.map +1 -0
- package/dist/functions/data-structures/EventEmitter.d.ts +32 -0
- package/dist/functions/data-structures/EventEmitter.js +108 -0
- package/dist/functions/data-structures/EventEmitter.js.map +1 -0
- package/dist/functions/data-structures/MyPromise.d.ts +32 -0
- package/dist/functions/data-structures/MyPromise.js +254 -0
- package/dist/functions/data-structures/MyPromise.js.map +1 -0
- package/dist/functions/image/generatePlaceholderImage.d.ts +43 -0
- package/dist/functions/image/generatePlaceholderImage.js +331 -0
- package/dist/functions/image/generatePlaceholderImage.js.map +1 -0
- package/dist/functions/reactive/useReactiveCookie.d.ts +19 -0
- package/dist/functions/reactive/useReactiveCookie.js +84 -0
- package/dist/functions/reactive/useReactiveCookie.js.map +1 -0
- package/dist/hooks/useClickOutside.d.ts +12 -0
- package/dist/hooks/useClickOutside.js +16 -0
- package/dist/hooks/useClickOutside.js.map +1 -0
- package/dist/hooks/useClipboard.d.ts +20 -0
- package/dist/hooks/useClipboard.js +33 -0
- package/dist/hooks/useClipboard.js.map +1 -0
- package/dist/hooks/useCounter.d.ts +19 -0
- package/dist/hooks/useCounter.js +44 -0
- package/dist/hooks/useCounter.js.map +1 -0
- package/dist/hooks/useEventListener.d.ts +14 -0
- package/dist/hooks/useEventListener.js +18 -0
- package/dist/hooks/useEventListener.js.map +1 -0
- package/dist/hooks/useInterval.d.ts +19 -0
- package/dist/hooks/useInterval.js +33 -0
- package/dist/hooks/useInterval.js.map +1 -0
- package/dist/hooks/useTimeout.d.ts +20 -0
- package/dist/hooks/useTimeout.js +40 -0
- package/dist/hooks/useTimeout.js.map +1 -0
- package/dist/hooks/useToggle.d.ts +15 -0
- package/dist/hooks/useToggle.js +23 -0
- package/dist/hooks/useToggle.js.map +1 -0
- package/dist/reactive/createGlobalState.d.ts +2 -0
- package/dist/reactive/createGlobalState.js +11 -0
- package/dist/reactive/createGlobalState.js.map +1 -0
- package/dist/reactive/createSharedComposable.d.ts +2 -0
- package/dist/reactive/createSharedComposable.js +13 -0
- package/dist/reactive/createSharedComposable.js.map +1 -0
- package/dist/reactive/errorManager.d.ts +63 -0
- package/dist/reactive/errorManager.js +199 -0
- package/dist/reactive/errorManager.js.map +1 -0
- package/dist/reactive/i18n.d.ts +27 -0
- package/dist/reactive/i18n.js +119 -0
- package/dist/reactive/i18n.js.map +1 -0
- package/dist/reactive/permission.d.ts +40 -0
- package/dist/reactive/permission.js +189 -0
- package/dist/reactive/permission.js.map +1 -0
- package/dist/reactive/pinia.d.ts +52 -0
- package/dist/reactive/pinia.js +183 -0
- package/dist/reactive/pinia.js.map +1 -0
- package/dist/reactive/useAltool.d.ts +39 -0
- package/dist/reactive/useAltool.js +350 -0
- package/dist/reactive/useAltool.js.map +1 -0
- package/dist/reactive/useCache.d.ts +18 -0
- package/dist/reactive/useCache.js +82 -0
- package/dist/reactive/useCache.js.map +1 -0
- package/dist/reactive/useLocalStorage.d.ts +8 -0
- package/dist/reactive/useLocalStorage.js +101 -0
- package/dist/reactive/useLocalStorage.js.map +1 -0
- package/dist/reactive/usePluginSystem.d.ts +11 -0
- package/dist/reactive/usePluginSystem.js +34 -0
- package/dist/reactive/usePluginSystem.js.map +1 -0
- package/dist/reactive/useSessionStorage.d.ts +8 -0
- package/dist/reactive/useSessionStorage.js +101 -0
- package/dist/reactive/useSessionStorage.js.map +1 -0
- package/dist/reactive/useStorage.d.ts +13 -0
- package/dist/reactive/useStorage.js +62 -0
- package/dist/reactive/useStorage.js.map +1 -0
- package/dist/reactive/useStorageWithIndexedDB.d.ts +8 -0
- package/dist/reactive/useStorageWithIndexedDB.js +132 -0
- package/dist/reactive/useStorageWithIndexedDB.js.map +1 -0
- package/dist/vue-hooks/browser/useFileUpload.d.ts +25 -0
- package/dist/vue-hooks/browser/useFileUpload.js +81 -0
- package/dist/vue-hooks/browser/useFileUpload.js.map +1 -0
- package/package.json +4 -2
- package/dist/functions/platform/tencent-cloud/deploy.tcb.d.ts +0 -13
- package/dist/functions/platform/tencent-cloud/deploy.tcb.js +0 -46
- package/dist/functions/platform/tencent-cloud/deploy.tcb.js.map +0 -1
- package/dist/functions/platform/tencent-cloud/getSecret.d.ts +0 -4
- package/dist/functions/platform/tencent-cloud/getSecret.js +0 -19
- package/dist/functions/platform/tencent-cloud/getSecret.js.map +0 -1
- package/dist/functions/platform/uniapp/app-plus/buildAndroidApp.d.ts +0 -27
- package/dist/functions/platform/uniapp/app-plus/buildAndroidApp.js +0 -287
- package/dist/functions/platform/uniapp/app-plus/buildAndroidApp.js.map +0 -1
- package/dist/functions/platform/uniapp/build.d.ts +0 -21
- package/dist/functions/platform/uniapp/build.js +0 -83
- package/dist/functions/platform/uniapp/build.js.map +0 -1
- package/dist/functions/platform/uniapp/detectAndroidProjectStructure.d.ts +0 -11
- package/dist/functions/platform/uniapp/detectAndroidProjectStructure.js +0 -30
- package/dist/functions/platform/uniapp/detectAndroidProjectStructure.js.map +0 -1
- package/dist/functions/platform/uniapp/detectProjectType.d.ts +0 -20
- package/dist/functions/platform/uniapp/detectProjectType.js +0 -68
- package/dist/functions/platform/uniapp/detectProjectType.js.map +0 -1
- package/dist/functions/platform/uniapp/parseManifest.d.ts +0 -18
- package/dist/functions/platform/uniapp/parseManifest.js +0 -49
- package/dist/functions/platform/uniapp/parseManifest.js.map +0 -1
- package/dist/functions/platform/uniapp/waitForPages.d.ts +0 -7
- package/dist/functions/platform/uniapp/waitForPages.js +0 -20
- package/dist/functions/platform/uniapp/waitForPages.js.map +0 -1
- package/dist/functions/string/visualizeRegexSteps.d.ts +0 -39
- package/dist/functions/string/visualizeRegexSteps.js +0 -446
- package/dist/functions/string/visualizeRegexSteps.js.map +0 -1
- package/dist/functions/utils/createI18n.d.ts +0 -2
- package/dist/functions/utils/createI18n.js +0 -97
- package/dist/functions/utils/createI18n.js.map +0 -1
- package/dist/functions/utils/formatString.d.ts +0 -5
- package/dist/functions/utils/formatString.js +0 -9
- package/dist/functions/utils/formatString.js.map +0 -1
- package/dist/hooks/usePermission.d.ts +0 -7
- package/dist/hooks/usePermission.js +0 -123
- package/dist/hooks/usePermission.js.map +0 -1
- package/dist/vue-hooks/dom/useKeyboard.d.ts +0 -29
- package/dist/vue-hooks/dom/useKeyboard.js +0 -129
- package/dist/vue-hooks/dom/useKeyboard.js.map +0 -1
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export function useEventListener(target, event, handler, options = {}, deps) {
|
|
2
|
+
const cleanup = () => {
|
|
3
|
+
const el = 'value' in target ? target.value : target;
|
|
4
|
+
if (el) {
|
|
5
|
+
el.removeEventListener(event, handler, options);
|
|
6
|
+
}
|
|
7
|
+
};
|
|
8
|
+
deps.vue.onMounted(() => {
|
|
9
|
+
const el = 'value' in target ? target.value : target;
|
|
10
|
+
if (el) {
|
|
11
|
+
el.addEventListener(event, handler, options);
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
deps.vue.onUnmounted(() => {
|
|
15
|
+
cleanup();
|
|
16
|
+
});
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=useEventListener.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useEventListener.js","sourceRoot":"","sources":["../../src/hooks/useEventListener.ts"],"names":[],"mappings":"AAkEA,MAAM,UAAU,gBAAgB,CAC9B,MAAkD,EAClD,KAAa,EACb,OAAsB,EACtB,UAA6C,EAAE,EAC/C,IAA0B;IAE1B,MAAM,OAAO,GAAG,GAAG,EAAE;QACnB,MAAM,EAAE,GAAG,OAAO,IAAI,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAA;QACpD,IAAI,EAAE,EAAE,CAAC;YACP,EAAE,CAAC,mBAAmB,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,CAAA;QACjD,CAAC;IACH,CAAC,CAAA;IAED,IAAI,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,EAAE;QACtB,MAAM,EAAE,GAAG,OAAO,IAAI,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAA;QACpD,IAAI,EAAE,EAAE,CAAC;YACP,EAAE,CAAC,gBAAgB,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,CAAA;QAC9C,CAAC;IACH,CAAC,CAAC,CAAA;IAEF,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC,GAAG,EAAE;QACxB,OAAO,EAAE,CAAA;IACX,CAAC,CAAC,CAAA;AACJ,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Ref } from '../../references/vue.d';
|
|
2
|
+
import type { setInterval, clearInterval } from '../../references/timer.d';
|
|
3
|
+
export interface VueCompositionAPI {
|
|
4
|
+
ref: <T>(value: T) => Ref<T>;
|
|
5
|
+
onUnmounted: (fn: () => void) => void;
|
|
6
|
+
}
|
|
7
|
+
export interface UseIntervalDeps {
|
|
8
|
+
vue: VueCompositionAPI;
|
|
9
|
+
setInterval: typeof setInterval;
|
|
10
|
+
clearInterval: typeof clearInterval;
|
|
11
|
+
}
|
|
12
|
+
export interface UseIntervalReturn {
|
|
13
|
+
isActive: Ref<boolean>;
|
|
14
|
+
pause: () => void;
|
|
15
|
+
resume: () => void;
|
|
16
|
+
}
|
|
17
|
+
export declare function useInterval(callback: () => void, interval: number, options: {
|
|
18
|
+
immediate?: boolean;
|
|
19
|
+
} | undefined, deps: UseIntervalDeps): UseIntervalReturn;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
export function useInterval(callback, interval, options = {}, deps) {
|
|
2
|
+
const { immediate = true } = options;
|
|
3
|
+
const isActive = deps.vue.ref(false);
|
|
4
|
+
let intervalId = null;
|
|
5
|
+
const clear = () => {
|
|
6
|
+
if (intervalId) {
|
|
7
|
+
deps.clearInterval(intervalId);
|
|
8
|
+
intervalId = null;
|
|
9
|
+
}
|
|
10
|
+
isActive.value = false;
|
|
11
|
+
};
|
|
12
|
+
const pause = () => {
|
|
13
|
+
clear();
|
|
14
|
+
};
|
|
15
|
+
const resume = () => {
|
|
16
|
+
if (isActive.value)
|
|
17
|
+
return;
|
|
18
|
+
intervalId = deps.setInterval(callback, interval);
|
|
19
|
+
isActive.value = true;
|
|
20
|
+
};
|
|
21
|
+
if (immediate) {
|
|
22
|
+
resume();
|
|
23
|
+
}
|
|
24
|
+
deps.vue.onUnmounted(() => {
|
|
25
|
+
clear();
|
|
26
|
+
});
|
|
27
|
+
return {
|
|
28
|
+
isActive,
|
|
29
|
+
pause,
|
|
30
|
+
resume
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
//# sourceMappingURL=useInterval.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useInterval.js","sourceRoot":"","sources":["../../src/hooks/useInterval.ts"],"names":[],"mappings":"AAqEA,MAAM,UAAU,WAAW,CACzB,QAAoB,EACpB,QAAgB,EAChB,UAAmC,EAAE,EACrC,IAAqB;IAErB,MAAM,EAAE,SAAS,GAAG,IAAI,EAAE,GAAG,OAAO,CAAA;IACpC,MAAM,QAAQ,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAU,KAAK,CAAC,CAAA;IAC7C,IAAI,UAAU,GAAQ,IAAI,CAAA;IAE1B,MAAM,KAAK,GAAG,GAAG,EAAE;QACjB,IAAI,UAAU,EAAE,CAAC;YACf,IAAI,CAAC,aAAa,CAAC,UAAU,CAAC,CAAA;YAC9B,UAAU,GAAG,IAAI,CAAA;QACnB,CAAC;QACD,QAAQ,CAAC,KAAK,GAAG,KAAK,CAAA;IACxB,CAAC,CAAA;IAED,MAAM,KAAK,GAAG,GAAG,EAAE;QACjB,KAAK,EAAE,CAAA;IACT,CAAC,CAAA;IAED,MAAM,MAAM,GAAG,GAAG,EAAE;QAClB,IAAI,QAAQ,CAAC,KAAK;YAAE,OAAM;QAE1B,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAA;QACjD,QAAQ,CAAC,KAAK,GAAG,IAAI,CAAA;IACvB,CAAC,CAAA;IAED,IAAI,SAAS,EAAE,CAAC;QACd,MAAM,EAAE,CAAA;IACV,CAAC;IAED,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC,GAAG,EAAE;QACxB,KAAK,EAAE,CAAA;IACT,CAAC,CAAC,CAAA;IAEF,OAAO;QACL,QAAQ;QACR,KAAK;QACL,MAAM;KACP,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Ref } from '../../references/vue.d';
|
|
2
|
+
import type { setTimeout, clearTimeout } from '../../references/timer.d';
|
|
3
|
+
export interface VueCompositionAPI {
|
|
4
|
+
ref: <T>(value: T) => Ref<T>;
|
|
5
|
+
onUnmounted: (fn: () => void) => void;
|
|
6
|
+
}
|
|
7
|
+
export interface UseTimeoutDeps {
|
|
8
|
+
vue: VueCompositionAPI;
|
|
9
|
+
setTimeout: typeof setTimeout;
|
|
10
|
+
clearTimeout: typeof clearTimeout;
|
|
11
|
+
}
|
|
12
|
+
export interface UseTimeoutReturn {
|
|
13
|
+
isPending: Ref<boolean>;
|
|
14
|
+
isReady: Ref<boolean>;
|
|
15
|
+
start: () => void;
|
|
16
|
+
stop: () => void;
|
|
17
|
+
}
|
|
18
|
+
export declare function useTimeout(callback: () => void, delay: number, options: {
|
|
19
|
+
immediate?: boolean;
|
|
20
|
+
} | undefined, deps: UseTimeoutDeps): UseTimeoutReturn;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export function useTimeout(callback, delay, options = {}, deps) {
|
|
2
|
+
const { immediate = true } = options;
|
|
3
|
+
const isPending = deps.vue.ref(false);
|
|
4
|
+
const isReady = deps.vue.ref(false);
|
|
5
|
+
let timeoutId = null;
|
|
6
|
+
const clear = () => {
|
|
7
|
+
if (timeoutId) {
|
|
8
|
+
deps.clearTimeout(timeoutId);
|
|
9
|
+
timeoutId = null;
|
|
10
|
+
}
|
|
11
|
+
isPending.value = false;
|
|
12
|
+
};
|
|
13
|
+
const stop = () => {
|
|
14
|
+
clear();
|
|
15
|
+
isReady.value = false;
|
|
16
|
+
};
|
|
17
|
+
const start = () => {
|
|
18
|
+
clear();
|
|
19
|
+
isReady.value = false;
|
|
20
|
+
isPending.value = true;
|
|
21
|
+
timeoutId = deps.setTimeout(() => {
|
|
22
|
+
isPending.value = false;
|
|
23
|
+
isReady.value = true;
|
|
24
|
+
callback();
|
|
25
|
+
}, delay);
|
|
26
|
+
};
|
|
27
|
+
if (immediate) {
|
|
28
|
+
start();
|
|
29
|
+
}
|
|
30
|
+
deps.vue.onUnmounted(() => {
|
|
31
|
+
clear();
|
|
32
|
+
});
|
|
33
|
+
return {
|
|
34
|
+
isPending,
|
|
35
|
+
isReady,
|
|
36
|
+
start,
|
|
37
|
+
stop
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=useTimeout.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useTimeout.js","sourceRoot":"","sources":["../../src/hooks/useTimeout.ts"],"names":[],"mappings":"AAuEA,MAAM,UAAU,UAAU,CACxB,QAAoB,EACpB,KAAa,EACb,UAAmC,EAAE,EACrC,IAAoB;IAEpB,MAAM,EAAE,SAAS,GAAG,IAAI,EAAE,GAAG,OAAO,CAAA;IACpC,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAU,KAAK,CAAC,CAAA;IAC9C,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAU,KAAK,CAAC,CAAA;IAC5C,IAAI,SAAS,GAAQ,IAAI,CAAA;IAEzB,MAAM,KAAK,GAAG,GAAG,EAAE;QACjB,IAAI,SAAS,EAAE,CAAC;YACd,IAAI,CAAC,YAAY,CAAC,SAAS,CAAC,CAAA;YAC5B,SAAS,GAAG,IAAI,CAAA;QAClB,CAAC;QACD,SAAS,CAAC,KAAK,GAAG,KAAK,CAAA;IACzB,CAAC,CAAA;IAED,MAAM,IAAI,GAAG,GAAG,EAAE;QAChB,KAAK,EAAE,CAAA;QACP,OAAO,CAAC,KAAK,GAAG,KAAK,CAAA;IACvB,CAAC,CAAA;IAED,MAAM,KAAK,GAAG,GAAG,EAAE;QACjB,KAAK,EAAE,CAAA;QACP,OAAO,CAAC,KAAK,GAAG,KAAK,CAAA;QACrB,SAAS,CAAC,KAAK,GAAG,IAAI,CAAA;QAEtB,SAAS,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,EAAE;YAC/B,SAAS,CAAC,KAAK,GAAG,KAAK,CAAA;YACvB,OAAO,CAAC,KAAK,GAAG,IAAI,CAAA;YACpB,QAAQ,EAAE,CAAA;QACZ,CAAC,EAAE,KAAK,CAAC,CAAA;IACX,CAAC,CAAA;IAED,IAAI,SAAS,EAAE,CAAC;QACd,KAAK,EAAE,CAAA;IACT,CAAC;IAED,IAAI,CAAC,GAAG,CAAC,WAAW,CAAC,GAAG,EAAE;QACxB,KAAK,EAAE,CAAA;IACT,CAAC,CAAC,CAAA;IAEF,OAAO;QACL,SAAS;QACT,OAAO;QACP,KAAK;QACL,IAAI;KACL,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Ref } from '../../references/vue.d';
|
|
2
|
+
export interface VueCompositionAPI {
|
|
3
|
+
ref: <T>(value: T) => Ref<T>;
|
|
4
|
+
}
|
|
5
|
+
export interface UseToggleDeps {
|
|
6
|
+
vue: VueCompositionAPI;
|
|
7
|
+
}
|
|
8
|
+
export interface UseToggleReturn {
|
|
9
|
+
value: Ref<boolean>;
|
|
10
|
+
toggle: () => void;
|
|
11
|
+
setTrue: () => void;
|
|
12
|
+
setFalse: () => void;
|
|
13
|
+
setValue: (val: boolean) => void;
|
|
14
|
+
}
|
|
15
|
+
export declare function useToggle(initialValue: boolean | undefined, deps: UseToggleDeps): UseToggleReturn;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export function useToggle(initialValue = false, deps) {
|
|
2
|
+
const value = deps.vue.ref(initialValue);
|
|
3
|
+
const toggle = () => {
|
|
4
|
+
value.value = !value.value;
|
|
5
|
+
};
|
|
6
|
+
const setTrue = () => {
|
|
7
|
+
value.value = true;
|
|
8
|
+
};
|
|
9
|
+
const setFalse = () => {
|
|
10
|
+
value.value = false;
|
|
11
|
+
};
|
|
12
|
+
const setValue = (val) => {
|
|
13
|
+
value.value = val;
|
|
14
|
+
};
|
|
15
|
+
return {
|
|
16
|
+
value,
|
|
17
|
+
toggle,
|
|
18
|
+
setTrue,
|
|
19
|
+
setFalse,
|
|
20
|
+
setValue
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
//# sourceMappingURL=useToggle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useToggle.js","sourceRoot":"","sources":["../../src/hooks/useToggle.ts"],"names":[],"mappings":"AAwDA,MAAM,UAAU,SAAS,CACvB,eAAwB,KAAK,EAC7B,IAAmB;IAEnB,MAAM,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,GAAG,CAAU,YAAY,CAAC,CAAA;IAEjD,MAAM,MAAM,GAAG,GAAG,EAAE;QAClB,KAAK,CAAC,KAAK,GAAG,CAAC,KAAK,CAAC,KAAK,CAAA;IAC5B,CAAC,CAAA;IAED,MAAM,OAAO,GAAG,GAAG,EAAE;QACnB,KAAK,CAAC,KAAK,GAAG,IAAI,CAAA;IACpB,CAAC,CAAA;IAED,MAAM,QAAQ,GAAG,GAAG,EAAE;QACpB,KAAK,CAAC,KAAK,GAAG,KAAK,CAAA;IACrB,CAAC,CAAA;IAED,MAAM,QAAQ,GAAG,CAAC,GAAY,EAAE,EAAE;QAChC,KAAK,CAAC,KAAK,GAAG,GAAG,CAAA;IACnB,CAAC,CAAA;IAED,OAAO;QACL,KAAK;QACL,MAAM;QACN,OAAO;QACP,QAAQ;QACR,QAAQ;KACT,CAAA;AACH,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createGlobalState.js","sourceRoot":"","sources":["../../src/reactive/createGlobalState.ts"],"names":[],"mappings":"AAwBA,MAAM,UAAU,iBAAiB,CAC/B,YAAqB;IAErB,IAAI,KAAoB,CAAA;IAExB,OAAO,GAAG,EAAE;QACV,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACxB,KAAK,GAAG,YAAY,EAAE,CAAA;QACxB,CAAC;QACD,OAAO,KAAK,CAAA;IACd,CAAC,CAAA;AACH,CAAC;AAED,eAAe,iBAAiB,CAAA"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export function createSharedComposable(composable) {
|
|
2
|
+
let state;
|
|
3
|
+
let subscriberCount = 0;
|
|
4
|
+
return () => {
|
|
5
|
+
subscriberCount++;
|
|
6
|
+
if (!state) {
|
|
7
|
+
state = composable();
|
|
8
|
+
}
|
|
9
|
+
return state;
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export default createSharedComposable;
|
|
13
|
+
//# sourceMappingURL=createSharedComposable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createSharedComposable.js","sourceRoot":"","sources":["../../src/reactive/createSharedComposable.ts"],"names":[],"mappings":"AAsCA,MAAM,UAAU,sBAAsB,CACpC,UAAmB;IAEnB,IAAI,KAAoB,CAAA;IACxB,IAAI,eAAe,GAAG,CAAC,CAAA;IAEvB,OAAO,GAAG,EAAE;QACV,eAAe,EAAE,CAAA;QAEjB,IAAI,CAAC,KAAK,EAAE,CAAC;YACX,KAAK,GAAG,UAAU,EAAE,CAAA;QACtB,CAAC;QAED,OAAO,KAAK,CAAA;IACd,CAAC,CAAA;AACH,CAAC;AAED,eAAe,sBAAsB,CAAA"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import type { CreateReactive } from '../../types/reactive';
|
|
2
|
+
export interface ErrorCode {
|
|
3
|
+
code: string;
|
|
4
|
+
message: Record<string, string>;
|
|
5
|
+
httpStatus?: number;
|
|
6
|
+
level?: 'info' | 'warning' | 'error' | 'fatal';
|
|
7
|
+
metadata?: Record<string, any>;
|
|
8
|
+
}
|
|
9
|
+
export interface ErrorCategory {
|
|
10
|
+
name: string;
|
|
11
|
+
prefix: string;
|
|
12
|
+
errors: ErrorCode[];
|
|
13
|
+
}
|
|
14
|
+
export interface ErrorManagerConfig {
|
|
15
|
+
defaultLocale: string;
|
|
16
|
+
fallbackLocale?: string;
|
|
17
|
+
categories: ErrorCategory[];
|
|
18
|
+
}
|
|
19
|
+
export interface ErrorManagerDeps {
|
|
20
|
+
createReactive: CreateReactive;
|
|
21
|
+
log: (...args: any[]) => void;
|
|
22
|
+
warn: (...args: any[]) => void;
|
|
23
|
+
}
|
|
24
|
+
export declare class BusinessError extends Error {
|
|
25
|
+
readonly code: string;
|
|
26
|
+
readonly httpStatus: number;
|
|
27
|
+
readonly level: 'info' | 'warning' | 'error' | 'fatal';
|
|
28
|
+
readonly metadata: Record<string, any>;
|
|
29
|
+
readonly params?: Record<string, any>;
|
|
30
|
+
constructor(code: string, message: string, options?: {
|
|
31
|
+
httpStatus?: number;
|
|
32
|
+
level?: 'info' | 'warning' | 'error' | 'fatal';
|
|
33
|
+
metadata?: Record<string, any>;
|
|
34
|
+
params?: Record<string, any>;
|
|
35
|
+
});
|
|
36
|
+
toJSON(): {
|
|
37
|
+
name: string;
|
|
38
|
+
code: string;
|
|
39
|
+
message: string;
|
|
40
|
+
httpStatus: number;
|
|
41
|
+
level: "error" | "info" | "warning" | "fatal";
|
|
42
|
+
metadata: Record<string, any>;
|
|
43
|
+
params: Record<string, any> | undefined;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
export interface ErrorManagerInstance {
|
|
47
|
+
locale: string;
|
|
48
|
+
getMessage: (code: string, params?: Record<string, any>) => string;
|
|
49
|
+
createError: (code: string, params?: Record<string, any>) => BusinessError;
|
|
50
|
+
throwError: (code: string, params?: Record<string, any>) => never;
|
|
51
|
+
hasError: (code: string) => boolean;
|
|
52
|
+
getErrorDefinition: (code: string) => ErrorCode | undefined;
|
|
53
|
+
getAllErrors: () => ErrorCode[];
|
|
54
|
+
getCategoryErrors: (prefix: string) => ErrorCode[];
|
|
55
|
+
getCategories: () => ErrorCategory[];
|
|
56
|
+
setLocale: (locale: string) => void;
|
|
57
|
+
addError: (categoryPrefix: string, error: ErrorCode) => void;
|
|
58
|
+
addCategory: (category: ErrorCategory) => void;
|
|
59
|
+
subscribe: (listener: () => void) => () => void;
|
|
60
|
+
dispose: () => void;
|
|
61
|
+
}
|
|
62
|
+
export declare function createErrorManager(config: ErrorManagerConfig, deps: ErrorManagerDeps): ErrorManagerInstance;
|
|
63
|
+
export declare function defineErrorCodes<T extends Record<string, string>>(codes: T): T;
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
export class BusinessError extends Error {
|
|
2
|
+
code;
|
|
3
|
+
httpStatus;
|
|
4
|
+
level;
|
|
5
|
+
metadata;
|
|
6
|
+
params;
|
|
7
|
+
constructor(code, message, options) {
|
|
8
|
+
super(message);
|
|
9
|
+
this.name = 'BusinessError';
|
|
10
|
+
this.code = code;
|
|
11
|
+
this.httpStatus = options?.httpStatus ?? 500;
|
|
12
|
+
this.level = options?.level ?? 'error';
|
|
13
|
+
this.metadata = options?.metadata ?? {};
|
|
14
|
+
this.params = options?.params;
|
|
15
|
+
}
|
|
16
|
+
toJSON() {
|
|
17
|
+
return {
|
|
18
|
+
name: this.name,
|
|
19
|
+
code: this.code,
|
|
20
|
+
message: this.message,
|
|
21
|
+
httpStatus: this.httpStatus,
|
|
22
|
+
level: this.level,
|
|
23
|
+
metadata: this.metadata,
|
|
24
|
+
params: this.params
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
export function createErrorManager(config, deps) {
|
|
29
|
+
if (!config.defaultLocale) {
|
|
30
|
+
throw new Error('defaultLocale is required');
|
|
31
|
+
}
|
|
32
|
+
if (!config.categories || config.categories.length === 0) {
|
|
33
|
+
throw new Error('categories is required and cannot be empty');
|
|
34
|
+
}
|
|
35
|
+
const errorMap = new Map();
|
|
36
|
+
const categoryMap = new Map();
|
|
37
|
+
config.categories.forEach(category => {
|
|
38
|
+
categoryMap.set(category.prefix, category);
|
|
39
|
+
category.errors.forEach(error => {
|
|
40
|
+
errorMap.set(error.code, error);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
const state = {
|
|
44
|
+
locale: config.defaultLocale,
|
|
45
|
+
fallbackLocale: config.fallbackLocale || config.defaultLocale,
|
|
46
|
+
categories: config.categories
|
|
47
|
+
};
|
|
48
|
+
const reactiveState = deps.createReactive(state, {
|
|
49
|
+
subscribers: new Set(),
|
|
50
|
+
notify: () => {
|
|
51
|
+
},
|
|
52
|
+
subscribe: (listener) => {
|
|
53
|
+
reactiveState.subscribers.add(listener);
|
|
54
|
+
return () => reactiveState.subscribers.delete(listener);
|
|
55
|
+
},
|
|
56
|
+
dispose: () => reactiveState.subscribers.clear(),
|
|
57
|
+
createProxy: (target, reactiveDeps) => new Proxy(target, {
|
|
58
|
+
get(target, property) {
|
|
59
|
+
if (property === 'subscribe' || property === 'notify' ||
|
|
60
|
+
property === 'dispose' || property === 'subscribers') {
|
|
61
|
+
return reactiveDeps[property];
|
|
62
|
+
}
|
|
63
|
+
return target[property];
|
|
64
|
+
},
|
|
65
|
+
set(target, property, value) {
|
|
66
|
+
;
|
|
67
|
+
target[property] = value;
|
|
68
|
+
if (property === 'locale') {
|
|
69
|
+
reactiveDeps.notify();
|
|
70
|
+
deps.log(`语言已切换到: ${value}`);
|
|
71
|
+
}
|
|
72
|
+
return true;
|
|
73
|
+
},
|
|
74
|
+
deleteProperty(target, property) {
|
|
75
|
+
if (property in target) {
|
|
76
|
+
delete target[property];
|
|
77
|
+
reactiveDeps.notify();
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
return false;
|
|
81
|
+
}
|
|
82
|
+
}),
|
|
83
|
+
getChangePath: (target, property) => [String(property)]
|
|
84
|
+
});
|
|
85
|
+
function replaceParams(message, params) {
|
|
86
|
+
if (!params) {
|
|
87
|
+
return message;
|
|
88
|
+
}
|
|
89
|
+
return message.replace(/\{(\w+)\}/g, (match, key) => {
|
|
90
|
+
return params[key] !== undefined ? String(params[key]) : match;
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
function getMessage(code, params) {
|
|
94
|
+
const errorDef = errorMap.get(code);
|
|
95
|
+
if (!errorDef) {
|
|
96
|
+
deps.warn(`Error code "${code}" not found`);
|
|
97
|
+
return code;
|
|
98
|
+
}
|
|
99
|
+
let message = errorDef.message[reactiveState.locale];
|
|
100
|
+
if (!message && reactiveState.fallbackLocale !== reactiveState.locale) {
|
|
101
|
+
message = errorDef.message[reactiveState.fallbackLocale];
|
|
102
|
+
}
|
|
103
|
+
if (!message) {
|
|
104
|
+
deps.warn(`Message for error code "${code}" not found in locale "${reactiveState.locale}"`);
|
|
105
|
+
return code;
|
|
106
|
+
}
|
|
107
|
+
return replaceParams(message, params);
|
|
108
|
+
}
|
|
109
|
+
function createError(code, params) {
|
|
110
|
+
const errorDef = errorMap.get(code);
|
|
111
|
+
if (!errorDef) {
|
|
112
|
+
deps.warn(`Error code "${code}" not found`);
|
|
113
|
+
return new BusinessError(code, code, {
|
|
114
|
+
httpStatus: 500,
|
|
115
|
+
level: 'error'
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
const message = getMessage(code, params);
|
|
119
|
+
return new BusinessError(code, message, {
|
|
120
|
+
httpStatus: errorDef.httpStatus,
|
|
121
|
+
level: errorDef.level,
|
|
122
|
+
metadata: errorDef.metadata,
|
|
123
|
+
params
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
function throwError(code, params) {
|
|
127
|
+
throw createError(code, params);
|
|
128
|
+
}
|
|
129
|
+
function hasError(code) {
|
|
130
|
+
return errorMap.has(code);
|
|
131
|
+
}
|
|
132
|
+
function getErrorDefinition(code) {
|
|
133
|
+
return errorMap.get(code);
|
|
134
|
+
}
|
|
135
|
+
function getAllErrors() {
|
|
136
|
+
return Array.from(errorMap.values());
|
|
137
|
+
}
|
|
138
|
+
function getCategoryErrors(prefix) {
|
|
139
|
+
const category = categoryMap.get(prefix);
|
|
140
|
+
return category ? category.errors : [];
|
|
141
|
+
}
|
|
142
|
+
function getCategories() {
|
|
143
|
+
return reactiveState.categories;
|
|
144
|
+
}
|
|
145
|
+
function setLocale(locale) {
|
|
146
|
+
reactiveState.locale = locale;
|
|
147
|
+
}
|
|
148
|
+
function addError(categoryPrefix, error) {
|
|
149
|
+
const category = categoryMap.get(categoryPrefix);
|
|
150
|
+
if (!category) {
|
|
151
|
+
deps.warn(`Category "${categoryPrefix}" not found`);
|
|
152
|
+
return;
|
|
153
|
+
}
|
|
154
|
+
if (errorMap.has(error.code)) {
|
|
155
|
+
deps.warn(`Error code "${error.code}" already exists`);
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
category.errors.push(error);
|
|
159
|
+
errorMap.set(error.code, error);
|
|
160
|
+
deps.log(`已添加错误码: ${error.code}`);
|
|
161
|
+
}
|
|
162
|
+
function addCategory(category) {
|
|
163
|
+
if (categoryMap.has(category.prefix)) {
|
|
164
|
+
deps.warn(`Category "${category.prefix}" already exists`);
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
reactiveState.categories.push(category);
|
|
168
|
+
categoryMap.set(category.prefix, category);
|
|
169
|
+
category.errors.forEach(error => {
|
|
170
|
+
errorMap.set(error.code, error);
|
|
171
|
+
});
|
|
172
|
+
deps.log(`已添加错误分类: ${category.name} (${category.prefix})`);
|
|
173
|
+
}
|
|
174
|
+
return {
|
|
175
|
+
get locale() {
|
|
176
|
+
return reactiveState.locale;
|
|
177
|
+
},
|
|
178
|
+
set locale(value) {
|
|
179
|
+
setLocale(value);
|
|
180
|
+
},
|
|
181
|
+
getMessage,
|
|
182
|
+
createError,
|
|
183
|
+
throwError,
|
|
184
|
+
hasError,
|
|
185
|
+
getErrorDefinition,
|
|
186
|
+
getAllErrors,
|
|
187
|
+
getCategoryErrors,
|
|
188
|
+
getCategories,
|
|
189
|
+
setLocale,
|
|
190
|
+
addError,
|
|
191
|
+
addCategory,
|
|
192
|
+
subscribe: reactiveState.subscribe,
|
|
193
|
+
dispose: reactiveState.dispose
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
export function defineErrorCodes(codes) {
|
|
197
|
+
return codes;
|
|
198
|
+
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AAW;QACX,SAAS,EAAE,aAAa,CAAC,SAAS;QAClC,OAAO,EAAE,aAAa,CAAC,OAAO;KAC/B,CAAA;AACH,CAAC;AAiBD,MAAM,UAAU,gBAAgB,CAAmC,KAAQ;IACzE,OAAO,KAAK,CAAA;AACd,CAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { CreateReactive } from '../../types/reactive';
|
|
2
|
+
export interface I18nTranslation {
|
|
3
|
+
[key: string]: string | I18nTranslation;
|
|
4
|
+
}
|
|
5
|
+
export interface I18nMessages {
|
|
6
|
+
[locale: string]: I18nTranslation;
|
|
7
|
+
}
|
|
8
|
+
export interface I18nConfig {
|
|
9
|
+
defaultLocale: string;
|
|
10
|
+
fallbackLocale?: string;
|
|
11
|
+
messages: I18nMessages;
|
|
12
|
+
}
|
|
13
|
+
export interface I18nDeps {
|
|
14
|
+
createReactive: CreateReactive;
|
|
15
|
+
log: (...args: any[]) => void;
|
|
16
|
+
warn: (...args: any[]) => void;
|
|
17
|
+
}
|
|
18
|
+
export interface I18nInstance {
|
|
19
|
+
locale: string;
|
|
20
|
+
t: (key: string, params?: Record<string, any>) => string;
|
|
21
|
+
setLocale: (locale: string) => void;
|
|
22
|
+
getLocales: () => string[];
|
|
23
|
+
hasLocale: (locale: string) => boolean;
|
|
24
|
+
addMessages: (locale: string, messages: I18nTranslation) => void;
|
|
25
|
+
getMessages: () => I18nTranslation;
|
|
26
|
+
}
|
|
27
|
+
export declare function createI18n(config: I18nConfig, deps: I18nDeps): I18nInstance;
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
export function createI18n(config, deps) {
|
|
2
|
+
if (!config.defaultLocale) {
|
|
3
|
+
throw new Error('defaultLocale is required');
|
|
4
|
+
}
|
|
5
|
+
if (!config.messages || Object.keys(config.messages).length === 0) {
|
|
6
|
+
throw new Error('messages is required and cannot be empty');
|
|
7
|
+
}
|
|
8
|
+
if (!config.messages[config.defaultLocale]) {
|
|
9
|
+
throw new Error(`Default locale "${config.defaultLocale}" not found in messages`);
|
|
10
|
+
}
|
|
11
|
+
const state = {
|
|
12
|
+
locale: config.defaultLocale,
|
|
13
|
+
fallbackLocale: config.fallbackLocale || config.defaultLocale,
|
|
14
|
+
messages: config.messages
|
|
15
|
+
};
|
|
16
|
+
const reactiveState = deps.createReactive(state, {
|
|
17
|
+
subscribers: new Set(),
|
|
18
|
+
notify: () => {
|
|
19
|
+
},
|
|
20
|
+
subscribe: (listener) => {
|
|
21
|
+
reactiveState.subscribers.add(listener);
|
|
22
|
+
return () => reactiveState.subscribers.delete(listener);
|
|
23
|
+
},
|
|
24
|
+
dispose: () => reactiveState.subscribers.clear(),
|
|
25
|
+
createProxy: (target, reactiveDeps) => new Proxy(target, {
|
|
26
|
+
get(target, property) {
|
|
27
|
+
if (property === 'subscribe' || property === 'notify' ||
|
|
28
|
+
property === 'dispose' || property === 'subscribers') {
|
|
29
|
+
return reactiveDeps[property];
|
|
30
|
+
}
|
|
31
|
+
return target[property];
|
|
32
|
+
},
|
|
33
|
+
set(target, property, value) {
|
|
34
|
+
const oldValue = target[property];
|
|
35
|
+
target[property] = value;
|
|
36
|
+
if (property === 'locale') {
|
|
37
|
+
reactiveDeps.notify();
|
|
38
|
+
deps.log(`语言已切换到: ${value}`);
|
|
39
|
+
}
|
|
40
|
+
return true;
|
|
41
|
+
},
|
|
42
|
+
deleteProperty(target, property) {
|
|
43
|
+
if (property in target) {
|
|
44
|
+
delete target[property];
|
|
45
|
+
reactiveDeps.notify();
|
|
46
|
+
return true;
|
|
47
|
+
}
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}),
|
|
51
|
+
getChangePath: (target, property) => [String(property)]
|
|
52
|
+
});
|
|
53
|
+
function getNestedValue(obj, path) {
|
|
54
|
+
return path.split('.').reduce((current, key) => {
|
|
55
|
+
return current && current[key] !== undefined ? current[key] : undefined;
|
|
56
|
+
}, obj);
|
|
57
|
+
}
|
|
58
|
+
function t(key, params) {
|
|
59
|
+
let message = getNestedValue(reactiveState.messages[reactiveState.locale], key);
|
|
60
|
+
if (message === undefined && reactiveState.fallbackLocale !== reactiveState.locale) {
|
|
61
|
+
message = getNestedValue(reactiveState.messages[reactiveState.fallbackLocale], key);
|
|
62
|
+
}
|
|
63
|
+
if (message === undefined) {
|
|
64
|
+
deps.warn(`Translation key "${key}" not found for locale "${reactiveState.locale}"`);
|
|
65
|
+
return key;
|
|
66
|
+
}
|
|
67
|
+
if (params && typeof message === 'string') {
|
|
68
|
+
return message.replace(/\{(\w+)\}/g, (match, paramKey) => {
|
|
69
|
+
return params[paramKey] !== undefined ? String(params[paramKey]) : match;
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
return String(message);
|
|
73
|
+
}
|
|
74
|
+
function setLocale(locale) {
|
|
75
|
+
if (!reactiveState.messages[locale]) {
|
|
76
|
+
deps.warn(`Locale "${locale}" not found in messages`);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
reactiveState.locale = locale;
|
|
80
|
+
}
|
|
81
|
+
function getLocales() {
|
|
82
|
+
return Object.keys(reactiveState.messages);
|
|
83
|
+
}
|
|
84
|
+
function hasLocale(locale) {
|
|
85
|
+
return locale in reactiveState.messages;
|
|
86
|
+
}
|
|
87
|
+
function addMessages(locale, messages) {
|
|
88
|
+
if (reactiveState.messages[locale]) {
|
|
89
|
+
reactiveState.messages[locale] = {
|
|
90
|
+
...reactiveState.messages[locale],
|
|
91
|
+
...messages
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
reactiveState.messages[locale] = messages;
|
|
96
|
+
}
|
|
97
|
+
deps.log(`已添加语言包: ${locale}`);
|
|
98
|
+
}
|
|
99
|
+
function getMessages() {
|
|
100
|
+
return reactiveState.messages[reactiveState.locale] || {};
|
|
101
|
+
}
|
|
102
|
+
return {
|
|
103
|
+
get locale() {
|
|
104
|
+
return reactiveState.locale;
|
|
105
|
+
},
|
|
106
|
+
set locale(value) {
|
|
107
|
+
setLocale(value);
|
|
108
|
+
},
|
|
109
|
+
t,
|
|
110
|
+
setLocale,
|
|
111
|
+
getLocales,
|
|
112
|
+
hasLocale,
|
|
113
|
+
addMessages,
|
|
114
|
+
getMessages,
|
|
115
|
+
subscribe: reactiveState.subscribe,
|
|
116
|
+
dispose: reactiveState.dispose
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
//# sourceMappingURL=i18n.js.map
|