@aspire-ui/element-component-pro 1.0.13 → 1.0.14

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.
@@ -3,33 +3,88 @@ import { reactive } from 'vue'
3
3
  /** 组件默认配置存储:组件名 -> 默认 props/配置 */
4
4
  const componentSettings = reactive<Record<string, Record<string, unknown>>>({})
5
5
 
6
+ /**
7
+ * 深度合并两个对象
8
+ * - source 的值会覆盖 target 的值
9
+ * - 对于嵌套对象,会递归合并
10
+ * - 数组直接替换,不合并
11
+ */
12
+ function deepMerge(target: Record<string, unknown>, source?: Record<string, unknown>): Record<string, unknown> {
13
+ if (!source) return target
14
+
15
+ for (const key in source) {
16
+ if (Object.prototype.hasOwnProperty.call(source, key)) {
17
+ const targetValue = target[key]
18
+ const sourceValue = source[key]
19
+
20
+ if (isPlainObject(sourceValue)) {
21
+ if (!isPlainObject(targetValue)) {
22
+ target[key] = { ...(sourceValue as Record<string, unknown>) }
23
+ } else {
24
+ target[key] = deepMerge(
25
+ targetValue as Record<string, unknown>,
26
+ sourceValue as Record<string, unknown>
27
+ )
28
+ }
29
+ } else {
30
+ target[key] = sourceValue
31
+ }
32
+ }
33
+ }
34
+
35
+ return target
36
+ }
37
+
38
+ /** 判断是否为普通对象(非数组、非 null) */
39
+ function isPlainObject(value: unknown): value is Record<string, unknown> {
40
+ if (!value || typeof value !== 'object') return false
41
+ const proto = Object.getPrototypeOf(value)
42
+ return proto === Object.prototype || proto === null
43
+ }
44
+
6
45
  export interface UseComponentSettingReturn {
7
46
  /** 获取组件默认配置;不传参时返回全部组件的配置 */
8
- getSetting: (componentName?: string) => Record<string, unknown>
9
- /** 设置组件默认配置(与已有配置浅合并) */
47
+ getSetting: <T extends Record<string, unknown> = Record<string, unknown>>(componentName?: string) => T
48
+ /** 设置组件默认配置(与已有配置深度合并) */
10
49
  setSetting: (componentName: string, config: Record<string, unknown>) => void
50
+ /** 合并组件配置:全局配置与组件 props 合并,返回合并后的结果 */
51
+ mergeSettings: <T extends Record<string, unknown> = Record<string, unknown>>(componentName: string, props: Record<string, unknown>) => T
11
52
  }
12
53
 
13
54
  /**
14
55
  * 组件默认配置:供所有组件统一获取/设置默认配置
15
- * - getSetting:获取组件默认配置,用于初始化或合并 props
16
- * - setSetting:设置组件默认配置,可在应用入口或按需调用
56
+ * - getSetting:获取组件默认配置
57
+ * - setSetting:设置组件默认配置(深度合并),可在应用入口或按需调用
58
+ * - mergeSettings:合并全局配置与组件 props,props 优先级更高
59
+ *
60
+ * 合并优先级:组件默认值 < 全局配置 < 组件 props
17
61
  */
18
62
  export function useComponentSetting(): UseComponentSettingReturn {
19
- const getSetting = (componentName?: string): Record<string, unknown> => {
63
+ const getSetting = <T extends Record<string, unknown> = Record<string, unknown>>(componentName?: string): T => {
20
64
  if (componentName === undefined) {
21
- return { ...componentSettings }
65
+ return JSON.parse(JSON.stringify(componentSettings)) as T
22
66
  }
23
- return { ...(componentSettings[componentName] ?? {}) }
67
+ return JSON.parse(JSON.stringify(componentSettings[componentName] ?? {})) as T
24
68
  }
25
69
 
26
70
  const setSetting = (componentName: string, config: Record<string, unknown>): void => {
27
71
  const current = componentSettings[componentName]
28
- componentSettings[componentName] = current ? { ...current, ...config } : { ...config }
72
+ if (current) {
73
+ componentSettings[componentName] = deepMerge({ ...current }, config)
74
+ } else {
75
+ componentSettings[componentName] = { ...config }
76
+ }
77
+ }
78
+
79
+ /** 合并全局配置与组件 props,props 优先级更高 */
80
+ const mergeSettings = <T extends Record<string, unknown> = Record<string, unknown>>(componentName: string, props: Record<string, unknown>): T => {
81
+ const globalSetting = componentSettings[componentName] ?? {}
82
+ return deepMerge({ ...globalSetting }, props) as T
29
83
  }
30
84
 
31
85
  return {
32
86
  getSetting,
33
87
  setSetting,
88
+ mergeSettings,
34
89
  }
35
90
  }