@minilo/utils 0.0.4 → 0.0.5

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.
@@ -1,21 +1,21 @@
1
1
  export const config = {
2
- theme: {
3
- colorDestructive: 'hsl(348 100% 61%)',
4
- colorPrimary: 'hsl(212 100% 45%)',
5
- colorSuccess: 'hsl(144 57% 58%)',
6
- colorWarning: 'hsl(42 84% 61%)'
7
- },
8
- sidebar: {
9
- collapse: false
10
- },
11
- app: {
12
- locale: 'zh-CN',
13
- name: 'Minilo Adminss',
14
- logo: '',
15
- defaultHomePath: '/'
16
- },
17
- transition: {
18
- progress: true
19
- }
20
- }
21
- //# sourceMappingURL=config.js.map
2
+ theme: {
3
+ colorDestructive: 'hsl(348 100% 61%)',
4
+ colorPrimary: 'hsl(212 100% 45%)',
5
+ colorSuccess: 'hsl(144 57% 58%)',
6
+ colorWarning: 'hsl(42 84% 61%)'
7
+ },
8
+ sidebar: {
9
+ collapse: false
10
+ },
11
+ app: {
12
+ locale: 'zh-CN',
13
+ name: 'Minilo Adminss',
14
+ logo: '',
15
+ defaultHomePath: '/'
16
+ },
17
+ transition: {
18
+ progress: true
19
+ }
20
+ };
21
+ //# sourceMappingURL=config.js.map
@@ -1,57 +1,60 @@
1
- import { markRaw, reactive, readonly } from 'vue'
2
- import { StorageManager } from './storageManager'
3
- import { config as defaultConfig } from './config'
4
- import { merge } from '../func'
5
- import { updateCSSVariables } from './updateCssVariables'
6
- const STORAGE_KEY = 'config'
1
+ import { markRaw, reactive, readonly } from 'vue';
2
+ import { StorageManager } from './storageManager';
3
+ import { config as defaultConfig } from './config';
4
+ import { merge } from '../func';
5
+ import { updateCSSVariables } from './updateCssVariables';
6
+ const STORAGE_KEY = 'config';
7
7
  // 用户配置引导类
8
8
  class Guider {
9
- constructor() {
10
- this.isInitialized = false
11
- this.state = reactive({
12
- ...this.loadConfig()
13
- })
14
- // SSR 兼容性检查:仅在浏览器中创建 StorageManager
15
- if (typeof window !== 'undefined') {
16
- this.cache = new StorageManager()
17
- } else {
18
- this.cache = null
9
+ constructor() {
10
+ this.isInitialized = false;
11
+ this.state = reactive({
12
+ ...this.loadConfig()
13
+ });
14
+ // SSR 兼容性检查:仅在浏览器中创建 StorageManager
15
+ if (typeof window !== 'undefined') {
16
+ this.cache = new StorageManager();
17
+ }
18
+ else {
19
+ this.cache = null;
20
+ }
19
21
  }
20
- }
21
- // 初始化配置
22
- initConfig({ config, namespace }) {
23
- if (this.isInitialized) {
24
- return
22
+ // 初始化配置
23
+ initConfig({ config, namespace }) {
24
+ if (this.isInitialized) {
25
+ return;
26
+ }
27
+ this.cache = new StorageManager({ prefix: namespace });
28
+ const mergeDefaultConfig = merge({}, config, defaultConfig);
29
+ const state = merge({}, this.loadConfig(), mergeDefaultConfig);
30
+ this.updateConfig(state);
31
+ this.isInitialized = true;
32
+ }
33
+ // 更新配置
34
+ updateConfig(config) {
35
+ const cacheState = merge({}, config, markRaw(this.state));
36
+ Object.assign(this.state, cacheState);
37
+ updateCSSVariables(this.state);
38
+ this.saveConfig(this.state);
39
+ }
40
+ // 保存配置
41
+ saveConfig(config) {
42
+ if (!this.cache)
43
+ return;
44
+ this.cache.setItem(STORAGE_KEY, config);
45
+ }
46
+ // 加载配置
47
+ loadConfig() {
48
+ if (!this.cache)
49
+ return undefined;
50
+ return this.cache.getItem(STORAGE_KEY);
51
+ }
52
+ // 获取配置
53
+ getConfig() {
54
+ return readonly(this.state);
25
55
  }
26
- this.cache = new StorageManager({ prefix: namespace })
27
- const mergeDefaultConfig = merge({}, config, defaultConfig)
28
- const state = merge({}, this.loadConfig(), mergeDefaultConfig)
29
- this.updateConfig(state)
30
- this.isInitialized = true
31
- }
32
- // 更新配置
33
- updateConfig(config) {
34
- const cacheState = merge({}, config, markRaw(this.state))
35
- Object.assign(this.state, cacheState)
36
- updateCSSVariables(this.state)
37
- this.saveConfig(this.state)
38
- }
39
- // 保存配置
40
- saveConfig(config) {
41
- if (!this.cache) return
42
- this.cache.setItem(STORAGE_KEY, config)
43
- }
44
- // 加载配置
45
- loadConfig() {
46
- if (!this.cache) return undefined
47
- return this.cache.getItem(STORAGE_KEY)
48
- }
49
- // 获取配置
50
- getConfig() {
51
- return readonly(this.state)
52
- }
53
56
  }
54
- const guider = new Guider()
55
- const userConfig = guider.getConfig()
56
- export { Guider, userConfig, guider }
57
- //# sourceMappingURL=guider.js.map
57
+ const guider = new Guider();
58
+ const userConfig = guider.getConfig();
59
+ export { Guider, userConfig, guider };
60
+ //# sourceMappingURL=guider.js.map
@@ -1,6 +1,6 @@
1
- export * from './config'
2
- export * from './guider'
3
- export * from './updateCssVariables'
4
- export * from './storageManager'
5
- export * from './types'
6
- //# sourceMappingURL=index.js.map
1
+ export * from './config';
2
+ export * from './guider';
3
+ export * from './updateCssVariables';
4
+ export * from './storageManager';
5
+ export * from './types';
6
+ //# sourceMappingURL=index.js.map
@@ -1,100 +1,108 @@
1
1
  class StorageManager {
2
- constructor({ prefix = '', storageType = 'localStorage' } = {}) {
3
- this.prefix = prefix
4
- // SSR 兼容性检查
5
- if (typeof window !== 'undefined') {
6
- this.storage = storageType === 'localStorage' ? window.localStorage : window.sessionStorage
7
- } else {
8
- this.storage = null
2
+ constructor({ prefix = '', storageType = 'localStorage' } = {}) {
3
+ this.prefix = prefix;
4
+ // SSR 兼容性检查
5
+ if (typeof window !== 'undefined') {
6
+ this.storage = storageType === 'localStorage' ? window.localStorage : window.sessionStorage;
7
+ }
8
+ else {
9
+ this.storage = null;
10
+ }
9
11
  }
10
- }
11
- /**
12
- * 清除所有带前缀的存储项
13
- */
14
- clear() {
15
- if (!this.storage) return
16
- const keysToRemove = []
17
- for (let i = 0; i < this.storage.length; i++) {
18
- const key = this.storage.key(i)
19
- if (key && key.startsWith(this.prefix)) {
20
- keysToRemove.push(key)
21
- }
12
+ /**
13
+ * 清除所有带前缀的存储项
14
+ */
15
+ clear() {
16
+ if (!this.storage)
17
+ return;
18
+ const keysToRemove = [];
19
+ for (let i = 0; i < this.storage.length; i++) {
20
+ const key = this.storage.key(i);
21
+ if (key && key.startsWith(this.prefix)) {
22
+ keysToRemove.push(key);
23
+ }
24
+ }
25
+ keysToRemove.forEach((key) => this.storage?.removeItem(key));
22
26
  }
23
- keysToRemove.forEach((key) => this.storage?.removeItem(key))
24
- }
25
- /**
26
- * 清除所有过期的存储项
27
- */
28
- clearExpiredItems() {
29
- if (!this.storage) return
30
- for (let i = 0; i < this.storage.length; i++) {
31
- const key = this.storage.key(i)
32
- if (key && key.startsWith(this.prefix)) {
33
- const shortKey = key.replace(this.prefix, '')
34
- this.getItem(shortKey) // 调用 getItem 方法检查并移除过期项
35
- }
27
+ /**
28
+ * 清除所有过期的存储项
29
+ */
30
+ clearExpiredItems() {
31
+ if (!this.storage)
32
+ return;
33
+ for (let i = 0; i < this.storage.length; i++) {
34
+ const key = this.storage.key(i);
35
+ if (key && key.startsWith(this.prefix)) {
36
+ const shortKey = key.replace(this.prefix, '');
37
+ this.getItem(shortKey); // 调用 getItem 方法检查并移除过期项
38
+ }
39
+ }
36
40
  }
37
- }
38
- /**
39
- * 获取存储项
40
- * @param key
41
- * @param defaultValue 当项不存在或已过期时返回的默认值
42
- * @returns 值,如果项已过期或解析错误则返回默认值
43
- */
44
- getItem(key, defaultValue = null) {
45
- if (!this.storage) return defaultValue
46
- const fullKey = this.getFullKey(key)
47
- const itemStr = this.storage.getItem(fullKey)
48
- if (!itemStr) {
49
- return defaultValue
41
+ /**
42
+ * 获取存储项
43
+ * @param key 键
44
+ * @param defaultValue 当项不存在或已过期时返回的默认值
45
+ * @returns 值,如果项已过期或解析错误则返回默认值
46
+ */
47
+ getItem(key, defaultValue = null) {
48
+ if (!this.storage)
49
+ return defaultValue;
50
+ const fullKey = this.getFullKey(key);
51
+ const itemStr = this.storage.getItem(fullKey);
52
+ if (!itemStr) {
53
+ return defaultValue;
54
+ }
55
+ try {
56
+ const item = JSON.parse(itemStr);
57
+ if (item.expiry && Date.now() > item.expiry) {
58
+ this.storage.removeItem(fullKey);
59
+ return defaultValue;
60
+ }
61
+ return item.value;
62
+ }
63
+ catch (error) {
64
+ console.error(`Error parsing item with key "${fullKey}":`, error);
65
+ this.storage.removeItem(fullKey); // 如果解析失败,删除该项
66
+ return defaultValue;
67
+ }
50
68
  }
51
- try {
52
- const item = JSON.parse(itemStr)
53
- if (item.expiry && Date.now() > item.expiry) {
54
- this.storage.removeItem(fullKey)
55
- return defaultValue
56
- }
57
- return item.value
58
- } catch (error) {
59
- console.error(`Error parsing item with key "${fullKey}":`, error)
60
- this.storage.removeItem(fullKey) // 如果解析失败,删除该项
61
- return defaultValue
69
+ /**
70
+ * 移除存储项
71
+ * @param key
72
+ */
73
+ removeItem(key) {
74
+ if (!this.storage)
75
+ return;
76
+ const fullKey = this.getFullKey(key);
77
+ this.storage.removeItem(fullKey);
62
78
  }
63
- }
64
- /**
65
- * 移除存储项
66
- * @param key
67
- */
68
- removeItem(key) {
69
- if (!this.storage) return
70
- const fullKey = this.getFullKey(key)
71
- this.storage.removeItem(fullKey)
72
- }
73
- /**
74
- * 设置存储项
75
- * @param key 键
76
- * @param value 值
77
- * @param ttl 存活时间(毫秒)
78
- */
79
- setItem(key, value, ttl) {
80
- if (!this.storage) return
81
- const fullKey = this.getFullKey(key)
82
- const expiry = ttl ? Date.now() + ttl : undefined
83
- const item = { expiry, value }
84
- try {
85
- this.storage.setItem(fullKey, JSON.stringify(item))
86
- } catch (error) {
87
- console.error(`Error setting item with key "${fullKey}":`, error)
79
+ /**
80
+ * 设置存储项
81
+ * @param key 键
82
+ * @param value
83
+ * @param ttl 存活时间(毫秒)
84
+ */
85
+ setItem(key, value, ttl) {
86
+ if (!this.storage)
87
+ return;
88
+ const fullKey = this.getFullKey(key);
89
+ const expiry = ttl ? Date.now() + ttl : undefined;
90
+ const item = { expiry, value };
91
+ try {
92
+ this.storage.setItem(fullKey, JSON.stringify(item));
93
+ }
94
+ catch (error) {
95
+ console.error(`Error setting item with key "${fullKey}":`, error);
96
+ }
97
+ }
98
+ /**
99
+ * 获取完整的存储键
100
+ * @param key 原始键
101
+ * @returns 带前缀的完整键
102
+ */
103
+ getFullKey(key) {
104
+ return `${this.prefix}-${key}`;
88
105
  }
89
- }
90
- /**
91
- * 获取完整的存储键
92
- * @param key 原始键
93
- * @returns 带前缀的完整键
94
- */
95
- getFullKey(key) {
96
- return `${this.prefix}-${key}`
97
- }
98
106
  }
99
- export { StorageManager }
100
- //# sourceMappingURL=storageManager.js.map
107
+ export { StorageManager };
108
+ //# sourceMappingURL=storageManager.js.map
@@ -1,2 +1,2 @@
1
- export {}
2
- //# sourceMappingURL=types.js.map
1
+ export {};
2
+ //# sourceMappingURL=types.js.map
@@ -1,91 +1,89 @@
1
- import { getColors } from 'theme-colors'
2
- import { convertToHsl, TinyColor } from '../func'
1
+ import { getColors } from 'theme-colors';
2
+ import { convertToHsl, TinyColor } from '../func';
3
3
  function executeUpdateCSSVariables(variables, id = '__minilo-styles__') {
4
- // 获取或创建内联样式表元素
5
- const styleElement = document.querySelector(`#${id}`) || document.createElement('style')
6
- styleElement.id = id
7
- // 构建要更新的 CSS 变量的样式文本
8
- let cssText = ':root {'
9
- for (const key in variables) {
10
- if (Object.prototype.hasOwnProperty.call(variables, key)) {
11
- cssText += `${key}: ${variables[key]};`
4
+ // 获取或创建内联样式表元素
5
+ const styleElement = document.querySelector(`#${id}`) || document.createElement('style');
6
+ styleElement.id = id;
7
+ // 构建要更新的 CSS 变量的样式文本
8
+ let cssText = ':root {';
9
+ for (const key in variables) {
10
+ if (Object.prototype.hasOwnProperty.call(variables, key)) {
11
+ cssText += `${key}: ${variables[key]};`;
12
+ }
13
+ }
14
+ cssText += '}';
15
+ // 将样式文本赋值给内联样式表
16
+ styleElement.textContent = cssText;
17
+ // 将内联样式表添加到文档头部
18
+ if (!document.querySelector(`#${id}`)) {
19
+ setTimeout(() => {
20
+ document.head.append(styleElement);
21
+ });
12
22
  }
13
- }
14
- cssText += '}'
15
- // 将样式文本赋值给内联样式表
16
- styleElement.textContent = cssText
17
- // 将内联样式表添加到文档头部
18
- if (!document.querySelector(`#${id}`)) {
19
- setTimeout(() => {
20
- document.head.append(styleElement)
21
- })
22
- }
23
23
  }
24
24
  // 根据某个色值生成色值阶梯对象,key=500时为其默认初始值,也就是默认参数
25
25
  function generatorColorVariables(colorItems) {
26
- const colorVariables = {}
27
- colorItems.forEach(({ alias, color, name }) => {
28
- if (color) {
29
- const colorsMap = getColors(new TinyColor(color).toHexString())
30
- let mainColor = colorsMap['500']
31
- const colorKeys = Object.keys(colorsMap)
32
- colorKeys.forEach((key) => {
33
- const colorValue = colorsMap[key]
34
- if (colorValue) {
35
- const hslColor = convertToHsl(colorValue)
36
- colorVariables[`--${name}-${key}`] = hslColor
37
- if (alias) {
38
- colorVariables[`--${alias}-${key}`] = hslColor
39
- }
40
- if (key === '500') {
41
- mainColor = hslColor
42
- }
26
+ const colorVariables = {};
27
+ colorItems.forEach(({ alias, color, name }) => {
28
+ if (color) {
29
+ const colorsMap = getColors(new TinyColor(color).toHexString());
30
+ let mainColor = colorsMap['500'];
31
+ const colorKeys = Object.keys(colorsMap);
32
+ colorKeys.forEach((key) => {
33
+ const colorValue = colorsMap[key];
34
+ if (colorValue) {
35
+ const hslColor = convertToHsl(colorValue);
36
+ colorVariables[`--${name}-${key}`] = hslColor;
37
+ if (alias) {
38
+ colorVariables[`--${alias}-${key}`] = hslColor;
39
+ }
40
+ if (key === '500') {
41
+ mainColor = hslColor;
42
+ }
43
+ }
44
+ });
45
+ if (alias && mainColor) {
46
+ colorVariables[`--${alias}`] = mainColor;
47
+ }
43
48
  }
44
- })
45
- if (alias && mainColor) {
46
- colorVariables[`--${alias}`] = mainColor
47
- }
48
- }
49
- })
50
- return colorVariables
49
+ });
50
+ return colorVariables;
51
51
  }
52
52
  function updateCSSVariables(config) {
53
- const theme = config?.theme ?? {}
54
- if (
55
- Reflect.has(theme, 'colorPrimary') ||
56
- Reflect.has(theme, 'colorDestructive') ||
57
- Reflect.has(theme, 'colorSuccess') ||
58
- Reflect.has(theme, 'colorWarning')
59
- ) {
60
- updateMainColorVariables(config)
61
- }
53
+ const theme = config?.theme ?? {};
54
+ if (Reflect.has(theme, 'colorPrimary') ||
55
+ Reflect.has(theme, 'colorDestructive') ||
56
+ Reflect.has(theme, 'colorSuccess') ||
57
+ Reflect.has(theme, 'colorWarning')) {
58
+ updateMainColorVariables(config);
59
+ }
62
60
  }
63
61
  function updateMainColorVariables(config) {
64
- if (!config.theme) {
65
- return
66
- }
67
- const { colorDestructive, colorPrimary, colorSuccess, colorWarning } = config.theme
68
- const colorVariables = generatorColorVariables([
69
- { color: colorPrimary, name: 'primary' },
70
- { alias: 'warning', color: colorWarning, name: 'yellow' },
71
- { alias: 'success', color: colorSuccess, name: 'green' },
72
- { alias: 'destructive', color: colorDestructive, name: 'red' }
73
- ])
74
- // 要设置的 CSS 变量映射
75
- const colorMappings = {
76
- '--green-500': '--success',
77
- '--primary-500': '--primary',
78
- '--red-500': '--destructive',
79
- '--yellow-500': '--warning'
80
- }
81
- // 统一处理颜色变量的更新
82
- Object.entries(colorMappings).forEach(([sourceVar, targetVar]) => {
83
- const colorValue = colorVariables[sourceVar]
84
- if (colorValue) {
85
- document.documentElement.style.setProperty(targetVar, colorValue)
62
+ if (!config.theme) {
63
+ return;
86
64
  }
87
- })
88
- executeUpdateCSSVariables(colorVariables)
65
+ const { colorDestructive, colorPrimary, colorSuccess, colorWarning } = config.theme;
66
+ const colorVariables = generatorColorVariables([
67
+ { color: colorPrimary, name: 'primary' },
68
+ { alias: 'warning', color: colorWarning, name: 'yellow' },
69
+ { alias: 'success', color: colorSuccess, name: 'green' },
70
+ { alias: 'destructive', color: colorDestructive, name: 'red' }
71
+ ]);
72
+ // 要设置的 CSS 变量映射
73
+ const colorMappings = {
74
+ '--green-500': '--success',
75
+ '--primary-500': '--primary',
76
+ '--red-500': '--destructive',
77
+ '--yellow-500': '--warning'
78
+ };
79
+ // 统一处理颜色变量的更新
80
+ Object.entries(colorMappings).forEach(([sourceVar, targetVar]) => {
81
+ const colorValue = colorVariables[sourceVar];
82
+ if (colorValue) {
83
+ document.documentElement.style.setProperty(targetVar, colorValue);
84
+ }
85
+ });
86
+ executeUpdateCSSVariables(colorVariables);
89
87
  }
90
- export { updateCSSVariables, executeUpdateCSSVariables, generatorColorVariables }
91
- //# sourceMappingURL=updateCssVariables.js.map
88
+ export { updateCSSVariables, executeUpdateCSSVariables, generatorColorVariables };
89
+ //# sourceMappingURL=updateCssVariables.js.map
@@ -1,18 +1,18 @@
1
1
  /**
2
2
  * @zh_CN 登录页面 url 地址
3
3
  */
4
- export const LOGIN_PATH = '/auth/login'
4
+ export const LOGIN_PATH = '/auth/login';
5
5
  /**
6
6
  * Supported languages
7
7
  */
8
8
  export const SUPPORT_LANGUAGES = [
9
- {
10
- label: '简体中文',
11
- value: 'zh-CN'
12
- },
13
- {
14
- label: 'English',
15
- value: 'en-US'
16
- }
17
- ]
18
- //# sourceMappingURL=index.js.map
9
+ {
10
+ label: '简体中文',
11
+ value: 'zh-CN'
12
+ },
13
+ {
14
+ label: 'English',
15
+ value: 'en-US'
16
+ }
17
+ ];
18
+ //# sourceMappingURL=index.js.map
@@ -1,12 +1,12 @@
1
- import { TinyColor } from '@ctrl/tinycolor'
1
+ import { TinyColor } from '@ctrl/tinycolor';
2
2
  // 将颜色转换为HSL格式
3
3
  function convertToHsl(color) {
4
- // 使用TinyColor库将颜色转换为HSL格式
5
- const { a, h, l, s } = new TinyColor(color).toHsl()
6
- // 将HSL格式转换为字符串
7
- const hsl = `hsl(${Math.round(h)} ${Math.round(s * 100)}% ${Math.round(l * 100)}%)`
8
- // 如果颜色的透明度小于1,则返回带有透明度的HSL格式
9
- return a < 1 ? `${hsl} ${a}` : hsl
4
+ // 使用TinyColor库将颜色转换为HSL格式
5
+ const { a, h, l, s } = new TinyColor(color).toHsl();
6
+ // 将HSL格式转换为字符串
7
+ const hsl = `hsl(${Math.round(h)} ${Math.round(s * 100)}% ${Math.round(l * 100)}%)`;
8
+ // 如果颜色的透明度小于1,则返回带有透明度的HSL格式
9
+ return a < 1 ? `${hsl} ${a}` : hsl;
10
10
  }
11
11
  /**
12
12
  * 将颜色转换为RGB颜色字符串
@@ -17,7 +17,7 @@ function convertToHsl(color) {
17
17
  * @returns 如果颜色值有效,则返回对应的RGB颜色字符串;如果无效,则返回rgb(0, 0, 0)
18
18
  */
19
19
  function convertToRgb(str) {
20
- return new TinyColor(str.replaceAll(/deg|grad|rad|turn/g, '')).toRgbString()
20
+ return new TinyColor(str.replaceAll(/deg|grad|rad|turn/g, '')).toRgbString();
21
21
  }
22
- export { convertToHsl, convertToRgb, TinyColor }
23
- //# sourceMappingURL=color.js.map
22
+ export { convertToHsl, convertToRgb, TinyColor };
23
+ //# sourceMappingURL=color.js.map