-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
175 lines (169 loc) · 4.86 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* @Author: junjie.lean
* @Date: 2021-04-23 11:29:21
* @Last Modified by: junjie.lean
* @Last Modified time: 2021-05-03 13:11:23
*/
const cssList = require("./cssList.json");
const cssSpecialList = require("./cssSpecialList.json");
const isDev = process.env.NODE_ENV === "development";
const base = {
version: "0.1.0",
author: "lean",
};
/**
* @description 关键原型对象
*/
const linkCSS = Object.create(
{},
{
//合并对象
cssObject: {
value: {},
// enumerable: true,
configurable: true,
},
//返回当前对象
next: {
value: function () {
return this;
},
enumerable: false,
configurable: false,
},
//合并当前对象
combineStyle: {
value: function (v) {
this.cssObject = this.cssObject ? Object.assign(this.cssObject, v) : v;
return this.next();
},
enumerable: false,
configurable: false,
},
//调试当前对象
logThis: {
value: function () {
console.log(this);
return this.next();
},
enumerable: false,
configurable: false,
},
//自定义样式
customStyle: {
value: function (customStyleObj) {
if (!customStyleObj) {
throw new Error("自定义样式必须设置相关参数!");
}
if (customStyleObj.type === "fn") {
Object.defineProperty(this, customStyleObj.callName, {
value: function (v) {
if (arguments.length === 0 || v === "") {
throw new Error(
`'${customStyleObj.callName}'没有设置默认值,该属性必须设置一个值!`
);
}
return this.combineStyle({
[customStyleObj.styleName]: v,
});
},
});
} else {
Object.defineProperty(this, customStyleObj.callName, {
get() {
return this.combineStyle(customStyleObj.styleValue);
},
});
}
},
enumerable: false,
configurable: false,
},
//结束链式调用并返回样式对象
end: {
get() {
let endStyle = { ...this.cssObject };
delete this.cssObject;
return endStyle;
},
enumerable: true,
configurable: false,
},
}
);
/**
* @description 根据数据生成相应的api
* @condition 引入的二者均为数组时:
* @example
* {
"type": "function",
"shortName": "",
"ReactStyleName": ["paddingTop"],
"defaultValue": ["0"]
},
{
"specialDisposeName": "displayF",
"specialDisposeValue": {
"display": "flex"
}
}
*
*/
Array.isArray(cssList) && Array.isArray(cssSpecialList)
? [].concat(cssList, cssSpecialList).map((item) => {
if (item.type === "fn") {
const ReactStyleName = item.ReactStyleName[0];
const shortName = item.shortName;
const defailtValue =
item.defaultValue[0] === "not specified"
? null
: item.defaultValue[0];
Object.defineProperties(linkCSS, {
[ReactStyleName]: {
//默认情况,全称必有的情况:
value: function (v = defailtValue) {
if (arguments.length === 0 || v === "") {
throw new Error(
`'${shortName}'/'${ReactStyleName}' 没有设置默认值,该属性必须设置一个值!`
);
}
return this.combineStyle({ [ReactStyleName]: v });
},
configurable: false,
// enumerable: isDev,
enumerable: false,
// enumerable: !isDev,
},
...(shortName
? {
//有短名称的情况下:
[shortName]: {
value: function (v = item.defaultValue) {
if (arguments.length === 0 || v === "") {
throw new Error(
`'${shortName}'/'${ReactStyleName}' 没有设置默认值,该属性必须设置一个值!`
);
}
return this.combineStyle({ [ReactStyleName]: v });
},
configurable: false,
// enumerable: isDev,
enumerable: false,
// enumerable: !isDev,
},
}
: {}),
});
} else {
//特殊处理的字段
//比如 displayF
Object.defineProperty(linkCSS, item.specialDisposeName, {
get() {
return this.combineStyle(item.specialDisposeValue);
},
enumerable: false,
});
}
})
: null;
module.exports = linkCSS;