@hlw-uni-mp/use 1.0.4 → 1.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/dist/index.d.mts CHANGED
@@ -1,10 +1,11 @@
1
1
  import * as pinia from 'pinia';
2
+ import { PiniaPluginContext } from 'pinia';
2
3
  import { App, Ref, ComputedRef } from 'vue';
3
4
 
4
5
  /**
5
6
  * 具有消息提示属性的响应体对象
6
7
  */
7
- interface ToastResponseLike {
8
+ interface ToastRes {
8
9
  /** 提示文案 */
9
10
  msg?: string;
10
11
  /** 是否为模态弹窗 */
@@ -23,7 +24,7 @@ type ToastCallback = (confirm: boolean) => void;
23
24
  */
24
25
  interface HlwMsg {
25
26
  /** 显示轻提示或模态确认框 (支持回调与 Promise) */
26
- toast(opts: UniApp.ShowToastOptions | ToastResponseLike | string, callback?: ToastCallback): Promise<boolean>;
27
+ toast(opts: UniApp.ShowToastOptions | ToastRes | string, callback?: ToastCallback): Promise<boolean>;
27
28
  /** 成功提示框 */
28
29
  success(msg: string): void;
29
30
  /** 失败提示框 */
@@ -62,6 +63,28 @@ declare const hlw: {
62
63
  };
63
64
  type HlwInstance = typeof hlw;
64
65
 
66
+ /**
67
+ * 持久化配置项
68
+ */
69
+ interface PersistOptions {
70
+ /** 自定义本地存储键名,默认为 store.$id */
71
+ key?: string;
72
+ /** 持久化白名单字段列表,默认持久化全部 state */
73
+ paths?: string[];
74
+ }
75
+
76
+ declare module "pinia" {
77
+ interface DefineStoreOptionsBase<S, Store> {
78
+ /** 本地持久化配置 */
79
+ unistorage?: boolean | PersistOptions;
80
+ }
81
+ }
82
+
83
+ /**
84
+ * 创建 Pinia 本地持久化存储插件
85
+ */
86
+ declare function storage(): (context: PiniaPluginContext) => void;
87
+
65
88
  /**
66
89
  * 外观模式型
67
90
  */
@@ -249,4 +272,4 @@ interface UseRefsReturn<T = unknown> {
249
272
  */
250
273
  declare function useRefs<T = unknown>(): UseRefsReturn<T>;
251
274
 
252
- export { DEFAULT_THEME, type FontFamily, type FontFamilyPreset, type FontSize, type FontSizePreset, type HlwInstance, type HlwMsg, type InitThemeOptions, type ShareConfig, type ShareHandlers, THEME_COLORS, type ThemeColor, type ThemeMode, type ThemeState, type ToastCallback, type ToastResponseLike, type UseRefsReturn, applyTheme, fontFamilyPresets, fontSizePresets, getTheme, hlw, initTheme, showShareMenu, useMsg, useRefs, useShare, useTheme, useThemeStore };
275
+ export { DEFAULT_THEME, type FontFamily, type FontFamilyPreset, type FontSize, type FontSizePreset, type HlwInstance, type HlwMsg, type InitThemeOptions, type PersistOptions, type ShareConfig, type ShareHandlers, THEME_COLORS, type ThemeColor, type ThemeMode, type ThemeState, type ToastCallback, type ToastRes, type UseRefsReturn, applyTheme, fontFamilyPresets, fontSizePresets, getTheme, hlw, initTheme, showShareMenu, storage, useMsg, useRefs, useShare, useTheme, useThemeStore };
package/dist/index.d.ts CHANGED
@@ -1,10 +1,11 @@
1
1
  import * as pinia from 'pinia';
2
+ import { PiniaPluginContext } from 'pinia';
2
3
  import { App, Ref, ComputedRef } from 'vue';
3
4
 
4
5
  /**
5
6
  * 具有消息提示属性的响应体对象
6
7
  */
7
- interface ToastResponseLike {
8
+ interface ToastRes {
8
9
  /** 提示文案 */
9
10
  msg?: string;
10
11
  /** 是否为模态弹窗 */
@@ -23,7 +24,7 @@ type ToastCallback = (confirm: boolean) => void;
23
24
  */
24
25
  interface HlwMsg {
25
26
  /** 显示轻提示或模态确认框 (支持回调与 Promise) */
26
- toast(opts: UniApp.ShowToastOptions | ToastResponseLike | string, callback?: ToastCallback): Promise<boolean>;
27
+ toast(opts: UniApp.ShowToastOptions | ToastRes | string, callback?: ToastCallback): Promise<boolean>;
27
28
  /** 成功提示框 */
28
29
  success(msg: string): void;
29
30
  /** 失败提示框 */
@@ -62,6 +63,28 @@ declare const hlw: {
62
63
  };
63
64
  type HlwInstance = typeof hlw;
64
65
 
66
+ /**
67
+ * 持久化配置项
68
+ */
69
+ interface PersistOptions {
70
+ /** 自定义本地存储键名,默认为 store.$id */
71
+ key?: string;
72
+ /** 持久化白名单字段列表,默认持久化全部 state */
73
+ paths?: string[];
74
+ }
75
+
76
+ declare module "pinia" {
77
+ interface DefineStoreOptionsBase<S, Store> {
78
+ /** 本地持久化配置 */
79
+ unistorage?: boolean | PersistOptions;
80
+ }
81
+ }
82
+
83
+ /**
84
+ * 创建 Pinia 本地持久化存储插件
85
+ */
86
+ declare function storage(): (context: PiniaPluginContext) => void;
87
+
65
88
  /**
66
89
  * 外观模式型
67
90
  */
@@ -249,4 +272,4 @@ interface UseRefsReturn<T = unknown> {
249
272
  */
250
273
  declare function useRefs<T = unknown>(): UseRefsReturn<T>;
251
274
 
252
- export { DEFAULT_THEME, type FontFamily, type FontFamilyPreset, type FontSize, type FontSizePreset, type HlwInstance, type HlwMsg, type InitThemeOptions, type ShareConfig, type ShareHandlers, THEME_COLORS, type ThemeColor, type ThemeMode, type ThemeState, type ToastCallback, type ToastResponseLike, type UseRefsReturn, applyTheme, fontFamilyPresets, fontSizePresets, getTheme, hlw, initTheme, showShareMenu, useMsg, useRefs, useShare, useTheme, useThemeStore };
275
+ export { DEFAULT_THEME, type FontFamily, type FontFamilyPreset, type FontSize, type FontSizePreset, type HlwInstance, type HlwMsg, type InitThemeOptions, type PersistOptions, type ShareConfig, type ShareHandlers, THEME_COLORS, type ThemeColor, type ThemeMode, type ThemeState, type ToastCallback, type ToastRes, type UseRefsReturn, applyTheme, fontFamilyPresets, fontSizePresets, getTheme, hlw, initTheme, showShareMenu, storage, useMsg, useRefs, useShare, useTheme, useThemeStore };
package/dist/index.js CHANGED
@@ -29,6 +29,7 @@ __export(index_exports, {
29
29
  hlw: () => hlw,
30
30
  initTheme: () => initTheme,
31
31
  showShareMenu: () => showShareMenu,
32
+ storage: () => storage,
32
33
  useMsg: () => useMsg,
33
34
  useRefs: () => useRefs,
34
35
  useShare: () => useShare,
@@ -145,8 +146,51 @@ var hlw = {
145
146
  var import_vue = require("vue");
146
147
 
147
148
  // src/theme/store.ts
149
+ var import_pinia2 = require("pinia");
150
+
151
+ // src/storage/types.d.ts
148
152
  var import_pinia = require("pinia");
149
- var import_pinia_plugin_storage = require("@hlw-uni-mp/pinia-plugin-storage");
153
+
154
+ // src/storage/index.ts
155
+ function storage() {
156
+ return (context) => {
157
+ const { store, options } = context;
158
+ const config = options.unistorage;
159
+ if (!config) return;
160
+ const storageKey = typeof config === "object" && config?.key || store.$id;
161
+ const paths = typeof config === "object" ? config?.paths : void 0;
162
+ try {
163
+ const cached = uni.getStorageSync(storageKey);
164
+ if (cached) {
165
+ const data = typeof cached === "string" ? JSON.parse(cached) : cached;
166
+ if (typeof data === "object" && data !== null) {
167
+ store.$patch(data);
168
+ }
169
+ }
170
+ } catch (error) {
171
+ console.error(`[Storage] \u6062\u590D ${store.$id} \u5931\u8D25:`, error);
172
+ }
173
+ store.$subscribe(
174
+ (_, state) => {
175
+ try {
176
+ let toSave = state;
177
+ if (paths && Array.isArray(paths)) {
178
+ toSave = paths.reduce((acc, key) => {
179
+ if (key in state) acc[key] = state[key];
180
+ return acc;
181
+ }, {});
182
+ }
183
+ uni.setStorageSync(storageKey, toSave);
184
+ } catch (error) {
185
+ console.error(`[Storage] \u4FDD\u5B58 ${store.$id} \u5931\u8D25:`, error);
186
+ }
187
+ },
188
+ { detached: true }
189
+ );
190
+ };
191
+ }
192
+
193
+ // src/theme/store.ts
150
194
  var fontSizePresets = [
151
195
  {
152
196
  id: "small",
@@ -191,7 +235,7 @@ var fontFamilyPresets = [
191
235
  class: "font-family-kaiti"
192
236
  }
193
237
  ];
194
- var useThemeStore = (0, import_pinia.defineStore)("theme", {
238
+ var useThemeStore = (0, import_pinia2.defineStore)("theme", {
195
239
  state: () => ({
196
240
  mode: "light",
197
241
  color: "#08c060",
@@ -352,6 +396,7 @@ function useRefs() {
352
396
  hlw,
353
397
  initTheme,
354
398
  showShareMenu,
399
+ storage,
355
400
  useMsg,
356
401
  useRefs,
357
402
  useShare,
package/dist/index.mjs CHANGED
@@ -107,7 +107,50 @@ import { computed, toRef } from "vue";
107
107
 
108
108
  // src/theme/store.ts
109
109
  import { defineStore } from "pinia";
110
- import "@hlw-uni-mp/pinia-plugin-storage";
110
+
111
+ // src/storage/types.d.ts
112
+ import "pinia";
113
+
114
+ // src/storage/index.ts
115
+ function storage() {
116
+ return (context) => {
117
+ const { store, options } = context;
118
+ const config = options.unistorage;
119
+ if (!config) return;
120
+ const storageKey = typeof config === "object" && config?.key || store.$id;
121
+ const paths = typeof config === "object" ? config?.paths : void 0;
122
+ try {
123
+ const cached = uni.getStorageSync(storageKey);
124
+ if (cached) {
125
+ const data = typeof cached === "string" ? JSON.parse(cached) : cached;
126
+ if (typeof data === "object" && data !== null) {
127
+ store.$patch(data);
128
+ }
129
+ }
130
+ } catch (error) {
131
+ console.error(`[Storage] \u6062\u590D ${store.$id} \u5931\u8D25:`, error);
132
+ }
133
+ store.$subscribe(
134
+ (_, state) => {
135
+ try {
136
+ let toSave = state;
137
+ if (paths && Array.isArray(paths)) {
138
+ toSave = paths.reduce((acc, key) => {
139
+ if (key in state) acc[key] = state[key];
140
+ return acc;
141
+ }, {});
142
+ }
143
+ uni.setStorageSync(storageKey, toSave);
144
+ } catch (error) {
145
+ console.error(`[Storage] \u4FDD\u5B58 ${store.$id} \u5931\u8D25:`, error);
146
+ }
147
+ },
148
+ { detached: true }
149
+ );
150
+ };
151
+ }
152
+
153
+ // src/theme/store.ts
111
154
  var fontSizePresets = [
112
155
  {
113
156
  id: "small",
@@ -312,6 +355,7 @@ export {
312
355
  hlw,
313
356
  initTheme,
314
357
  showShareMenu,
358
+ storage,
315
359
  useMsg,
316
360
  useRefs,
317
361
  useShare,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hlw-uni-mp/use",
3
- "version": "1.0.4",
3
+ "version": "1.1.0",
4
4
  "description": "Universal Uni-App Vue 3 stateful composables library",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -32,9 +32,6 @@
32
32
  "optional": true
33
33
  }
34
34
  },
35
- "dependencies": {
36
- "@hlw-uni-mp/pinia-plugin-storage": "^1.0.1"
37
- },
38
35
  "devDependencies": {
39
36
  "@dcloudio/types": "^3.4.30",
40
37
  "@dcloudio/uni-app": "3.0.0-5000720260410001",