@cloudbase/cals 0.4.3 → 0.4.4

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,2 +1,12 @@
1
+ import { Properties } from 'csstype';
1
2
  export declare function removeInvalidStyleFormValue(styleForm?: any): any;
3
+ export declare function processCommonStyle2CSSProperties(commonStyle?: any, options?: {
4
+ toRem: boolean;
5
+ ignoreSelf?: boolean;
6
+ addPXUnit?: boolean;
7
+ toRpx?: boolean;
8
+ }): Properties<string | number>;
9
+ export declare function processCSSProperties2Rem(style?: Properties<string | number>, options?: {
10
+ looseError: boolean;
11
+ }): Properties<string | number>;
2
12
  //# sourceMappingURL=style.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"style.d.ts","sourceRoot":"","sources":["../../../../src/parser/cals/utils/style.ts"],"names":[],"mappings":"AAEA,wBAAgB,2BAA2B,CAAC,SAAS,GAAE,GAA2B,GAAG,GAAG,CAUvF"}
1
+ {"version":3,"file":"style.d.ts","sourceRoot":"","sources":["../../../../src/parser/cals/utils/style.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAKrC,wBAAgB,2BAA2B,CAAC,SAAS,GAAE,GAA2B,GAAG,GAAG,CAUvF;AAuID,wBAAgB,gCAAgC,CAC9C,WAAW,GAAE,GAA2B,EACxC,OAAO,GAAE;IACP,KAAK,EAAE,OAAO,CAAC;IACf,UAAU,CAAC,EAAE,OAAO,CAAC;IACrB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,KAAK,CAAC,EAAE,OAAO,CAAC;CACoD,GACrE,UAAU,CAAC,MAAM,GAAG,MAAM,CAAC,CAmN7B;AASD,wBAAgB,wBAAwB,CACtC,KAAK,GAAE,UAAU,CAAC,MAAM,GAAG,MAAM,CAAM,EACvC,OAAO;;CAAwB,GAC9B,UAAU,CAAC,MAAM,GAAG,MAAM,CAAC,CAE7B"}
@@ -1,7 +1,21 @@
1
1
  "use strict";
2
+ var __rest = (this && this.__rest) || function (s, e) {
3
+ var t = {};
4
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
+ t[p] = s[p];
6
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
+ t[p[i]] = s[p[i]];
10
+ }
11
+ return t;
12
+ };
2
13
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.removeInvalidStyleFormValue = void 0;
14
+ exports.processCSSProperties2Rem = exports.processCommonStyle2CSSProperties = exports.removeInvalidStyleFormValue = void 0;
4
15
  const common_1 = require("./common");
16
+ const lodash_1 = require("lodash");
17
+ const CSSProperty_1 = require("../../../utils/CSSProperty");
18
+ const DISTANCE_KEY_LIST = ['top', 'right', 'bottom', 'left'];
5
19
  function removeInvalidStyleFormValue(styleForm = {}) {
6
20
  return Object.keys(styleForm).reduce((result, key) => {
7
21
  const propStyleFormData = styleForm[key];
@@ -28,3 +42,272 @@ function setStyleValue(object, key, value) {
28
42
  }
29
43
  object[(0, common_1.camelcase)(key)] = value;
30
44
  }
45
+ function _handleStyleNumValue(styleVal, addPXUnit) {
46
+ if (addPXUnit) {
47
+ const value = `${styleVal}`;
48
+ if (value.search(/^\d+$/) >= 0) {
49
+ return `${value}px`;
50
+ }
51
+ }
52
+ return styleVal;
53
+ }
54
+ function setDistanceStyle(style, distance, attr, addPXUnit) {
55
+ if (Object.keys(distance).length === 4) {
56
+ if ((0, lodash_1.uniq)((0, lodash_1.map)(distance, (val) => val)).length === 1) {
57
+ // 4个值全相等的情况
58
+ setStyleValue(style, (0, common_1.camelcase)(`${attr}`), _handleStyleNumValue(distance.top, addPXUnit));
59
+ }
60
+ else if (distance.top === distance.bottom && distance.left === distance.right) {
61
+ // 俩俩相等的情况
62
+ setStyleValue(style, (0, common_1.camelcase)(`${attr}`), `${_handleStyleNumValue(distance.top, addPXUnit)} ${_handleStyleNumValue(distance.right, addPXUnit)}`);
63
+ }
64
+ else if (distance.left === distance.right) {
65
+ setStyleValue(style, (0, common_1.camelcase)(`${attr}`), `${_handleStyleNumValue(distance.top, addPXUnit)} ${_handleStyleNumValue(distance.right, addPXUnit)} ${_handleStyleNumValue(distance.bottom, addPXUnit)}`);
66
+ }
67
+ else {
68
+ setStyleValue(style, (0, common_1.camelcase)(`${attr}`), `${_handleStyleNumValue(distance.top, addPXUnit)} ${_handleStyleNumValue(distance.right, addPXUnit)} ${_handleStyleNumValue(distance.bottom, addPXUnit)} ${_handleStyleNumValue(distance.left, addPXUnit)}`);
69
+ }
70
+ }
71
+ else {
72
+ DISTANCE_KEY_LIST.forEach((key) => {
73
+ if (distance[key] !== undefined)
74
+ setStyleValue(style, (0, common_1.camelcase)(`${attr}_${key}`), _handleStyleNumValue(distance[key], addPXUnit));
75
+ });
76
+ }
77
+ }
78
+ function translateStyleByHandler(style = {}, handler, options = { looseError: false }) {
79
+ return Object.keys(style).reduce((result, key) => {
80
+ const value = style[key];
81
+ if (CSSProperty_1.isUnitlessNumber[key]) {
82
+ setStyleValue(result, key, value);
83
+ }
84
+ else if (value !== undefined && value !== null) {
85
+ try {
86
+ setStyleValue(result, key, handler(value));
87
+ }
88
+ catch (e) {
89
+ if (options.looseError) {
90
+ console.warn('translate style error, key:value', key, value);
91
+ }
92
+ else {
93
+ throw e;
94
+ }
95
+ }
96
+ }
97
+ return result;
98
+ }, {});
99
+ }
100
+ function calPxToREM(px) {
101
+ if (Number.isNaN(px / 28))
102
+ return px.toString();
103
+ if (+px === 0) {
104
+ return '0';
105
+ }
106
+ return `${(px / 28).toFixed(4)}rem`;
107
+ }
108
+ function toREM(cssLen) {
109
+ if (typeof cssLen === 'string') {
110
+ const cssLenArr = cssLen.split(' ');
111
+ return cssLenArr
112
+ .map((attr) => {
113
+ const matchResult = attr.match(/^(-?\d+)(px)?$/);
114
+ if (matchResult === null || matchResult === void 0 ? void 0 : matchResult[1]) {
115
+ return calPxToREM(+matchResult[1]);
116
+ }
117
+ return attr;
118
+ })
119
+ .join(' ');
120
+ }
121
+ else if (typeof cssLen === 'number') {
122
+ return calPxToREM(cssLen);
123
+ }
124
+ else {
125
+ throw new Error('cssLen type error');
126
+ }
127
+ }
128
+ function toRPX(cssLen) {
129
+ if (typeof cssLen === 'string') {
130
+ const cssLenArr = cssLen.split(' ');
131
+ return cssLenArr
132
+ .map((attr) => {
133
+ const matchResult = attr.match(/^(-?\d+)(px)?$/);
134
+ if (matchResult === null || matchResult === void 0 ? void 0 : matchResult[1]) {
135
+ return `${+matchResult[1]}rpx`;
136
+ }
137
+ return attr;
138
+ })
139
+ .join(' ');
140
+ }
141
+ return `${cssLen}rpx`;
142
+ }
143
+ // Convert WeApps common style to css styles(React.CSSProperties)
144
+ function processCommonStyle2CSSProperties(commonStyle = {}, options = { toRem: true, ignoreSelf: false, addPXUnit: false, toRpx: false }) {
145
+ const { size,
146
+ // transform,
147
+ text, border, background, margin, padding, zIndex, position, display, flexConfig, custom, self, } = commonStyle;
148
+ const style = {};
149
+ if (size) {
150
+ setStyleValue(style, 'width', _handleStyleNumValue(size.width, !!options.addPXUnit));
151
+ setStyleValue(style, 'height', _handleStyleNumValue(size.height, !!options.addPXUnit));
152
+ }
153
+ // if (transform) {
154
+ // const { rotate, opacity, scale, radius } = transform
155
+ // if ((rotate && rotate !== 0) || (scale && scale !== 1)) {
156
+ // style.transform = `${rotate ? `rotate(${rotate}deg) ` : ''}${scale ? `scale(${scale})` : ''}`
157
+ // }
158
+ // setStyleValue(style, 'opacity', opacity)
159
+ // setStyleValue(style, 'borderRadius', radius)
160
+ // }
161
+ if (margin) {
162
+ setDistanceStyle(style, margin, 'margin', !!options.addPXUnit);
163
+ }
164
+ if (padding) {
165
+ setDistanceStyle(style, padding, 'padding', !!options.addPXUnit);
166
+ }
167
+ if (display) {
168
+ setStyleValue(style, 'display', display);
169
+ }
170
+ if (display === 'flex' && flexConfig) {
171
+ if (flexConfig.justifyContent)
172
+ setStyleValue(style, 'justifyContent', flexConfig.justifyContent);
173
+ if (flexConfig.alignItems)
174
+ setStyleValue(style, 'alignItems', flexConfig.alignItems);
175
+ if (flexConfig.flexWrap && flexConfig.flexDirection) {
176
+ setStyleValue(style, 'flexFlow', `${flexConfig.flexDirection} ${flexConfig.flexWrap}`);
177
+ }
178
+ else {
179
+ if (flexConfig.flexWrap)
180
+ setStyleValue(style, 'flexWrap', flexConfig.flexWrap);
181
+ if (flexConfig.flexDirection)
182
+ setStyleValue(style, 'flexDirection', flexConfig.flexDirection);
183
+ }
184
+ }
185
+ if (typeof zIndex === 'number' || `${zIndex}`.search(/^\d+$/) === 0) {
186
+ style.zIndex = zIndex;
187
+ }
188
+ if (text) {
189
+ setStyleValue(style, 'color', text.color);
190
+ setStyleValue(style, 'fontSize', _handleStyleNumValue(text.fontSize, !!options.addPXUnit));
191
+ setStyleValue(style, 'lineHeight', text.lineHeight);
192
+ setStyleValue(style, 'textAlign', text.textAlign);
193
+ setStyleValue(style, 'fontWeight', text.weight);
194
+ if (text.opacity !== undefined) {
195
+ setStyleValue(style, 'opacity', text.opacity / 100);
196
+ }
197
+ }
198
+ if (border) {
199
+ const { type, color, width, radius, radiusInfo } = border;
200
+ if (width !== undefined) {
201
+ if (type && type !== 'none') {
202
+ setStyleValue(style, 'border', `${_handleStyleNumValue(width, !!options.addPXUnit)} ${type} ${color || ''}`);
203
+ }
204
+ else {
205
+ setStyleValue(style, 'borderWidth', _handleStyleNumValue(width, !!options.addPXUnit));
206
+ if (color)
207
+ setStyleValue(style, 'borderColor', color);
208
+ }
209
+ }
210
+ if (radius !== undefined) {
211
+ setStyleValue(style, 'borderRadius', _handleStyleNumValue(radius, !!options.addPXUnit));
212
+ }
213
+ if (radiusInfo && !(0, common_1.isEmptyObj)(radiusInfo)) {
214
+ if (Object.keys(radiusInfo).length === 4) {
215
+ if ((0, lodash_1.uniq)((0, lodash_1.map)(radiusInfo, (val) => val)).length === 1) {
216
+ // 4个值全相等的情况
217
+ setStyleValue(style, 'borderRadius', _handleStyleNumValue(radiusInfo.topLeft, !!options.addPXUnit));
218
+ }
219
+ else if (radiusInfo.topLeft === radiusInfo.bottomRight && radiusInfo.topRight === radiusInfo.bottomLeft) {
220
+ // 俩俩相等的情况
221
+ setStyleValue(style, 'borderRadius', `${_handleStyleNumValue(radiusInfo.topLeft, !!options.addPXUnit)} ${_handleStyleNumValue(radiusInfo.topRight, !!options.addPXUnit)}`);
222
+ }
223
+ else if (radiusInfo.topRight === radiusInfo.bottomLeft) {
224
+ setStyleValue(style, 'borderRadius', `${_handleStyleNumValue(radiusInfo.topLeft, !!options.addPXUnit)} ${_handleStyleNumValue(radiusInfo.topRight, !!options.addPXUnit)} ${_handleStyleNumValue(radiusInfo.bottomRight, !!options.addPXUnit)}`);
225
+ }
226
+ else {
227
+ setStyleValue(style, 'borderRadius', `${_handleStyleNumValue(radiusInfo.topLeft, !!options.addPXUnit)} ${_handleStyleNumValue(radiusInfo.topRight, !!options.addPXUnit)} ${_handleStyleNumValue(radiusInfo.bottomRight, !!options.addPXUnit)} ${_handleStyleNumValue(radiusInfo.bottomLeft, !!options.addPXUnit)}`);
228
+ }
229
+ }
230
+ else {
231
+ setStyleValue(style, 'borderTopLeftRadius', _handleStyleNumValue(radiusInfo.topLeft, !!options.addPXUnit));
232
+ setStyleValue(style, 'borderTopRightRadius', _handleStyleNumValue(radiusInfo.topRight, !!options.addPXUnit));
233
+ setStyleValue(style, 'borderBottomRightRadius', _handleStyleNumValue(radiusInfo.bottomRight, !!options.addPXUnit));
234
+ setStyleValue(style, 'borderBottomLeftRadius', _handleStyleNumValue(radiusInfo.bottomLeft, !!options.addPXUnit));
235
+ }
236
+ }
237
+ if (type === 'none') {
238
+ setStyleValue(style, 'border', `none`);
239
+ }
240
+ }
241
+ if (background) {
242
+ const { bgType, color, image, size, repeat, position, positionObj } = background;
243
+ if (bgType === 'color') {
244
+ setStyleValue(style, 'background', color);
245
+ }
246
+ else if (bgType === 'image') {
247
+ // 如 radial-gradient(crimson, skyblue);
248
+ if (image != null) {
249
+ if (image.search(/[()]/) >= 0) {
250
+ style.background = image;
251
+ }
252
+ else {
253
+ style.background = `url(${image})`;
254
+ }
255
+ }
256
+ if (repeat)
257
+ style.background += ` ${repeat}`;
258
+ if (size) {
259
+ setStyleValue(style, 'backgroundSize', _handleStyleNumValue(size, !!options.addPXUnit));
260
+ }
261
+ setStyleValue(style, 'backgroundPosition', position);
262
+ if (positionObj && !(0, common_1.isEmptyObj)(positionObj)) {
263
+ style.background += ` ${_handleStyleNumValue(positionObj.left, !!options.addPXUnit)} ${_handleStyleNumValue(positionObj.top, !!options.addPXUnit)}`;
264
+ }
265
+ }
266
+ // FIXME: 这里兼容原有应用的数据,后面应去掉
267
+ if (bgType === undefined) {
268
+ setStyleValue(style, 'backgroundColor', color);
269
+ if (image != null) {
270
+ style.backgroundImage = `url(${image})`;
271
+ setStyleValue(style, 'backgroundRepeat', repeat);
272
+ setStyleValue(style, 'backgroundSize', _handleStyleNumValue(size, !!options.addPXUnit));
273
+ setStyleValue(style, 'backgroundPosition', position);
274
+ }
275
+ }
276
+ }
277
+ if (position) {
278
+ setStyleValue(style, 'position', position.position);
279
+ if (position.left !== undefined) {
280
+ setStyleValue(style, 'left', _handleStyleNumValue(position.left, !!options.addPXUnit));
281
+ }
282
+ if (position.right !== undefined) {
283
+ setStyleValue(style, 'right', _handleStyleNumValue(position.right, !!options.addPXUnit));
284
+ }
285
+ if (position.top !== undefined) {
286
+ setStyleValue(style, 'top', _handleStyleNumValue(position.top, !!options.addPXUnit));
287
+ }
288
+ if (position.bottom !== undefined) {
289
+ setStyleValue(style, 'bottom', _handleStyleNumValue(position.bottom, !!options.addPXUnit));
290
+ }
291
+ }
292
+ if (custom && custom.length > 0) {
293
+ custom.map((item) => {
294
+ setStyleValue(style, item.key, item.value);
295
+ });
296
+ }
297
+ if (self && !options.ignoreSelf) {
298
+ const _a = self, { cssFloat } = _a, restSelf = __rest(_a, ["cssFloat"]);
299
+ Object.assign(style, restSelf, { float: cssFloat });
300
+ }
301
+ if (options.toRpx) {
302
+ return translateStyleToRpx(style);
303
+ }
304
+ return options.toRem ? processCSSProperties2Rem(style) : style;
305
+ }
306
+ exports.processCommonStyle2CSSProperties = processCommonStyle2CSSProperties;
307
+ function translateStyleToRpx(style = {}, options = { looseError: false }) {
308
+ return translateStyleByHandler(style, toRPX, options);
309
+ }
310
+ function processCSSProperties2Rem(style = {}, options = { looseError: false }) {
311
+ return translateStyleByHandler(style, toREM, options);
312
+ }
313
+ exports.processCSSProperties2Rem = processCSSProperties2Rem;
@@ -1,7 +1,8 @@
1
1
  export * from './cals/index';
2
2
  export { upgrageToVersion2 } from './cals/utils/version';
3
3
  export { processComponentModule } from './cals/utils/spinoff';
4
- export { calsToCode, codeToCals, ICodeItem, checkForConflicts, autoResolveConflicts, } from './cals/utils/code';
4
+ export { calsToCode, codeToCals, ICodeItem, checkForConflicts, autoResolveConflicts } from './cals/utils/code';
5
5
  export { parseComponentCals } from './cals/utils/block';
6
6
  export { ActionType, IDataBind, PropBindType } from './expression/index';
7
+ export { processCommonStyle2CSSProperties, processCSSProperties2Rem } from './cals/utils/style';
7
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/parser/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AAC9D,OAAO,EACL,UAAU,EACV,UAAU,EACV,SAAS,EACT,iBAAiB,EACjB,oBAAoB,GACrB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/parser/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAAE,sBAAsB,EAAE,MAAM,sBAAsB,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,SAAS,EAAE,iBAAiB,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAC/G,OAAO,EAAE,kBAAkB,EAAE,MAAM,oBAAoB,CAAC;AACxD,OAAO,EAAE,UAAU,EAAE,SAAS,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AACzE,OAAO,EAAE,gCAAgC,EAAE,wBAAwB,EAAE,MAAM,oBAAoB,CAAC"}
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.PropBindType = exports.ActionType = exports.parseComponentCals = exports.autoResolveConflicts = exports.checkForConflicts = exports.codeToCals = exports.calsToCode = exports.processComponentModule = exports.upgrageToVersion2 = void 0;
17
+ exports.processCSSProperties2Rem = exports.processCommonStyle2CSSProperties = exports.PropBindType = exports.ActionType = exports.parseComponentCals = exports.autoResolveConflicts = exports.checkForConflicts = exports.codeToCals = exports.calsToCode = exports.processComponentModule = exports.upgrageToVersion2 = void 0;
18
18
  __exportStar(require("./cals/index"), exports);
19
19
  var version_1 = require("./cals/utils/version");
20
20
  Object.defineProperty(exports, "upgrageToVersion2", { enumerable: true, get: function () { return version_1.upgrageToVersion2; } });
@@ -30,3 +30,6 @@ Object.defineProperty(exports, "parseComponentCals", { enumerable: true, get: fu
30
30
  var index_1 = require("./expression/index");
31
31
  Object.defineProperty(exports, "ActionType", { enumerable: true, get: function () { return index_1.ActionType; } });
32
32
  Object.defineProperty(exports, "PropBindType", { enumerable: true, get: function () { return index_1.PropBindType; } });
33
+ var style_1 = require("./cals/utils/style");
34
+ Object.defineProperty(exports, "processCommonStyle2CSSProperties", { enumerable: true, get: function () { return style_1.processCommonStyle2CSSProperties; } });
35
+ Object.defineProperty(exports, "processCSSProperties2Rem", { enumerable: true, get: function () { return style_1.processCSSProperties2Rem; } });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudbase/cals",
3
- "version": "0.4.3",
3
+ "version": "0.4.4",
4
4
  "description": "Common application specifications",
5
5
  "main": "lib/utils/index.js",
6
6
  "source": "src/utils/index.ts",
@@ -31,6 +31,7 @@
31
31
  "@types/jest": "^27.4.0",
32
32
  "@types/lodash": "^4.14.168",
33
33
  "@types/node": "^14.14.6",
34
+ "@types/react": "^18.0.17",
34
35
  "ajv": "^8.8.2",
35
36
  "csstype": "^3.0.11",
36
37
  "jest": "^27.1.0",