Skip to content

Commit 815972b

Browse files
committed
feat: 添加 utils 函数,更新 image 组件的 props 和样式
1 parent c1ad286 commit 815972b

File tree

9 files changed

+221
-436
lines changed

9 files changed

+221
-436
lines changed

components/_utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ export * from "./props";
77
export * from "./px-check";
88
export * from "./raf";
99
export * from "./style";
10+
export * from "./utils";

components/_utils/is.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,12 @@ export function isUrl(path: string): boolean {
9191
const reg = /^http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- ./?%&=]*)?/;
9292
return reg.test(path);
9393
}
94+
95+
/**
96+
* @description 判断target是否对象
97+
* @param value
98+
* @return {boolean}
99+
*/
100+
export function isObj(value: any): value is object {
101+
return Object.prototype.toString.call(value) === '[object Object]' || typeof value === 'object'
102+
}

components/_utils/utils.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { isArray, isObj, isString } from './is'
2+
/**
3+
* @description 对num自动填充px
4+
* @param {number} num
5+
* @return {string} num+px
6+
*/
7+
export function addUnit(num: number | string) {
8+
return Number.isNaN(Number(num)) ? `${num}` : `${num}px`
9+
}
10+
11+
/**
12+
* 将驼峰命名转换为短横线命名。
13+
* @param {string} word 待转换的词条
14+
* @returns {string} 转换后的结果
15+
*/
16+
export function kebabCase(word: string): string {
17+
// 使用正则表达式匹配所有大写字母,并在前面加上短横线,然后转换为小写
18+
const newWord: string = word
19+
.replace(/[A-Z]/g, (match) => {
20+
return `-${match}`
21+
})
22+
.toLowerCase()
23+
24+
return newWord
25+
}
26+
27+
28+
/**
29+
* 将外部传入的样式格式化为可读的 CSS 样式。
30+
* @param {object | object[]} styles 外部传入的样式对象或数组
31+
* @returns {string} 格式化后的 CSS 样式字符串
32+
*/
33+
export function objToStyle(styles: Record<string, any> | Record<string, any>[]): string {
34+
// 如果 styles 是数组类型
35+
if (isArray(styles)) {
36+
// 使用过滤函数去除空值和 null 值的元素
37+
// 对每个非空元素递归调用 objToStyle,然后通过分号连接
38+
const result = styles
39+
.filter((item) => {
40+
// eslint-disable-next-line unicorn/no-null
41+
return item != null && item !== ''
42+
})
43+
.map((item) => {
44+
return objToStyle(item)
45+
})
46+
.join(';')
47+
48+
// 如果结果不为空,确保末尾有分号
49+
return result ? (result.endsWith(';') ? result : `${result};`) : ''
50+
}
51+
52+
if (isString(styles)) {
53+
// 如果是字符串且不为空,确保末尾有分号
54+
return styles ? (styles.endsWith(';') ? styles : `${styles};`) : ''
55+
}
56+
57+
// 如果 styles 是对象类型
58+
if (isObj(styles)) {
59+
// 使用 Object.keys 获取所有属性名
60+
// 使用过滤函数去除值为 null 或空字符串的属性
61+
// 对每个属性名和属性值进行格式化,通过分号连接
62+
const result = Object.keys(styles)
63+
.filter((key) => {
64+
// eslint-disable-next-line unicorn/no-null
65+
return styles[key] != null && styles[key] !== ''
66+
})
67+
.map((key) => {
68+
// 使用 kebabCase 函数将属性名转换为 kebab-case 格式
69+
// 将属性名和属性值格式化为 CSS 样式的键值对
70+
return [kebabCase(key), styles[key]].join(':')
71+
})
72+
.join(';')
73+
74+
// 如果结果不为空,确保末尾有分号
75+
return result ? (result.endsWith(';') ? result : `${result};`) : ''
76+
}
77+
// 如果 styles 不是对象也不是数组,则直接返回
78+
return ''
79+
}

components/image/image.ts

Lines changed: 29 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,56 @@
1-
import type { ImageProps as ImagePropsType } from "@uni-helper/uni-app-types";
1+
import type { ImageMode } from "@uni-helper/uni-app-types";
22
import type { ExtractPropTypes, PropType } from "vue";
3-
import type { LoadingMode } from "./type";
4-
import { makeStringProp } from "../_utils";
5-
6-
interface ImgProps {
7-
file: string;
8-
fileType: string;
9-
thumbnail: string;
10-
videoSrc: string;
11-
blurhash?: string | undefined;
12-
cover?: string;
13-
preSrc?: string;
14-
alt?: string;
15-
src?: string;
16-
baseSrc?: string;
17-
}
18-
19-
interface ErrorConfig {
20-
open: boolean;
21-
errSrc: string;
22-
}
3+
import { makeNumericProp } from "../_utils";
234

245
// 定义 props
256
export const imageProps = {
26-
/**
27-
* @description 表单类型,可选值为 `LoadingMode` 类型中定义的值,默认值为 'default'
28-
*/
29-
loadingMode: makeStringProp<LoadingMode>("blurhash"),
30-
/**
31-
* @description 是否禁用刷新功能
32-
* @default false
33-
*/
34-
isDisabledRefresh: {
35-
type: Boolean,
36-
default: false,
37-
},
38-
39-
/**
40-
* @description
41-
*/
42-
imageProps: {
43-
type: Object as PropType<
44-
Partial<Omit<ImagePropsType, "loading" | "finished" | "onLoad">>
45-
>,
46-
default: () => ({
47-
lazyLoad: true,
48-
mode: "widthFix",
49-
}),
50-
},
51-
data: {
52-
type: Object as PropType<ImgProps>,
53-
default: () => ({}),
54-
},
7+
mode: {
8+
type: String as PropType<ImageMode>,
9+
default: "scaleToFill",
10+
} as const,
5511
width: {
56-
// type: [String, Number],
57-
type: String,
58-
default: "100%",
12+
type: [String, Number],
5913
},
6014
height: {
61-
// type: [String, Number],
62-
type: String,
63-
default: "100%",
64-
},
65-
isShowBase: {
66-
type: Boolean,
67-
default: false,
68-
},
69-
resize: {
70-
type: Number,
71-
default: 0,
15+
type: [String, Number],
7216
},
7317
delay: {
7418
type: Number,
75-
default: 500,
19+
default: 0,
7620
},
77-
borderRadius: {
78-
// type: [String, Number],
21+
radius: {
7922
type: Number,
8023
default: 0,
8124
},
82-
minHeight: {
83-
// type: [String, Number],
25+
minHeight: makeNumericProp("200rpx"),
26+
lazyLoad: {
27+
type: Boolean,
28+
default: true,
29+
},
30+
customStyle: {
8431
type: String,
85-
default: "200rpx",
32+
default: "",
8633
},
87-
errConfig: {
88-
type: Object as PropType<ErrorConfig>,
89-
default: () => ({
90-
open: false,
91-
errSrc: "",
92-
}),
34+
round: {
35+
type: Boolean,
36+
default: false,
9337
},
94-
placeholder: {
38+
customClass: {
9539
type: String,
9640
default: "",
9741
},
42+
9843
src: {
9944
type: String,
10045
default: "",
10146
},
102-
isLpPreset: {
103-
type: Boolean,
104-
default: false,
47+
placeholderSrc: {
48+
type: String,
49+
default: "",
50+
},
51+
filter: {
52+
type: String,
53+
default: "",
10554
},
10655
} as const;
10756

0 commit comments

Comments
 (0)