@tarojs/api 4.0.0-canary.7 → 4.0.0-canary.9

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.
package/dist/taro.js CHANGED
@@ -1,356 +1,246 @@
1
1
  (function (global, factory) {
2
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@babel/runtime/helpers/typeof'), require('@tarojs/runtime'), require('@tarojs/shared'), require('@babel/runtime/helpers/objectSpread2'), require('@babel/runtime/helpers/classCallCheck'), require('@babel/runtime/helpers/createClass'), require('@babel/runtime/helpers/defineProperty')) :
3
- typeof define === 'function' && define.amd ? define(['exports', '@babel/runtime/helpers/typeof', '@tarojs/runtime', '@tarojs/shared', '@babel/runtime/helpers/objectSpread2', '@babel/runtime/helpers/classCallCheck', '@babel/runtime/helpers/createClass', '@babel/runtime/helpers/defineProperty'], factory) :
4
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Taro = {}, global._typeof, global.runtime, global.shared, global._objectSpread, global._classCallCheck, global._createClass, global._defineProperty));
5
- })(this, (function (exports, _typeof, runtime, shared, _objectSpread, _classCallCheck, _createClass, _defineProperty) { 'use strict';
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('@tarojs/runtime'), require('@tarojs/shared')) :
3
+ typeof define === 'function' && define.amd ? define(['@tarojs/runtime', '@tarojs/shared'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, global.Taro = factory(global.runtime, global.shared));
5
+ })(this, (function (runtime, shared) { 'use strict';
6
6
 
7
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
8
-
9
- var _typeof__default = /*#__PURE__*/_interopDefaultLegacy(_typeof);
10
- var _objectSpread__default = /*#__PURE__*/_interopDefaultLegacy(_objectSpread);
11
- var _classCallCheck__default = /*#__PURE__*/_interopDefaultLegacy(_classCallCheck);
12
- var _createClass__default = /*#__PURE__*/_interopDefaultLegacy(_createClass);
13
- var _defineProperty__default = /*#__PURE__*/_interopDefaultLegacy(_defineProperty);
14
-
15
- function isFunction(x) {
16
- return typeof x === 'function';
17
- }
18
- function isUndefined(x) {
19
- return typeof x === 'undefined';
20
- }
21
- function isObject(x) {
22
- return x && _typeof__default["default"](x) === 'object';
23
- }
24
-
25
- var isBadObj = function isBadObj(x) {
26
- return !isObject(x);
7
+ const ENV_TYPE = {
8
+ WEAPP: 'WEAPP',
9
+ SWAN: 'SWAN',
10
+ ALIPAY: 'ALIPAY',
11
+ TT: 'TT',
12
+ QQ: 'QQ',
13
+ JD: 'JD',
14
+ WEB: 'WEB',
15
+ RN: 'RN',
16
+ HARMONY: 'HARMONY',
17
+ QUICKAPP: 'QUICKAPP',
18
+ HARMONYHYBRID: 'HARMONYHYBRID',
27
19
  };
28
- function throwTypeError(s) {
29
- throw new TypeError(s);
30
- }
31
- if (!isFunction(Object.assign)) {
32
- // Must be writable: true, enumerable: false, configurable: true
33
- Object.assign = function (target) {
34
- // .length of function is 2
35
- if (target == null) {
36
- // TypeError if undefined or null
37
- throwTypeError('Cannot convert undefined or null to object');
20
+ function getEnv() {
21
+ if (process.env.TARO_ENV === 'weapp') {
22
+ return ENV_TYPE.WEAPP;
38
23
  }
39
- var to = Object(target);
40
- for (var index = 1; index < arguments.length; index++) {
41
- var nextSource = arguments[index];
42
- if (nextSource != null) {
43
- // Skip over if undefined or null
44
- for (var nextKey in nextSource) {
45
- // Avoid bugs when hasOwnProperty is shadowed
46
- if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
47
- to[nextKey] = nextSource[nextKey];
48
- }
49
- }
50
- }
24
+ else if (process.env.TARO_ENV === 'alipay') {
25
+ return ENV_TYPE.ALIPAY;
51
26
  }
52
- return to;
53
- };
54
- }
55
- if (!isFunction(Object.defineProperties)) {
56
- Object.defineProperties = function (obj, properties) {
57
- function convertToDescriptor(desc) {
58
- function hasProperty(obj, prop) {
59
- return Object.prototype.hasOwnProperty.call(obj, prop);
60
- }
61
- if (isBadObj(desc)) {
62
- throwTypeError('bad desc');
63
- }
64
- var d = {};
65
- if (hasProperty(desc, 'enumerable')) d.enumerable = !!desc.enumerable;
66
- if (hasProperty(desc, 'configurable')) {
67
- d.configurable = !!desc.configurable;
68
- }
69
- if (hasProperty(desc, 'value')) d.value = desc.value;
70
- if (hasProperty(desc, 'writable')) d.writable = !!desc.writable;
71
- if (hasProperty(desc, 'get')) {
72
- var g = desc.get;
73
- if (!isFunction(g) && !isUndefined(g)) {
74
- throwTypeError('bad get');
75
- }
76
- d.get = g;
77
- }
78
- if (hasProperty(desc, 'set')) {
79
- var s = desc.set;
80
- if (!isFunction(s) && !isUndefined(s)) {
81
- throwTypeError('bad set');
82
- }
83
- d.set = s;
84
- }
85
- if (('get' in d || 'set' in d) && ('value' in d || 'writable' in d)) {
86
- throwTypeError('identity-confused descriptor');
87
- }
88
- return d;
27
+ else if (process.env.TARO_ENV === 'swan') {
28
+ return ENV_TYPE.SWAN;
89
29
  }
90
- if (isBadObj(obj)) throwTypeError('bad obj');
91
- properties = Object(properties);
92
- var keys = Object.keys(properties);
93
- var descs = [];
94
- for (var i = 0; i < keys.length; i++) {
95
- descs.push([keys[i], convertToDescriptor(properties[keys[i]])]);
30
+ else if (process.env.TARO_ENV === 'tt') {
31
+ return ENV_TYPE.TT;
96
32
  }
97
- for (var _i = 0; _i < descs.length; _i++) {
98
- Object.defineProperty(obj, descs[_i][0], descs[_i][1]);
33
+ else if (process.env.TARO_ENV === 'jd') {
34
+ return ENV_TYPE.JD;
35
+ }
36
+ else if (process.env.TARO_ENV === 'qq') {
37
+ return ENV_TYPE.QQ;
38
+ }
39
+ else if (process.env.TARO_ENV === 'harmony-hybrid') {
40
+ return ENV_TYPE.HARMONYHYBRID;
41
+ }
42
+ else if (process.env.TARO_ENV === 'h5' || process.env.TARO_PLATFORM === 'web') {
43
+ return ENV_TYPE.WEB;
44
+ }
45
+ else if (process.env.TARO_ENV === 'rn') {
46
+ return ENV_TYPE.RN;
47
+ }
48
+ else if (process.env.TARO_ENV === 'harmony' || process.env.TARO_PLATFORM === 'harmony') {
49
+ return ENV_TYPE.HARMONY;
50
+ }
51
+ else if (process.env.TARO_ENV === 'quickapp') {
52
+ return ENV_TYPE.QUICKAPP;
53
+ }
54
+ else {
55
+ return process.env.TARO_ENV || 'Unknown';
99
56
  }
100
- return obj;
101
- };
102
- }
103
-
104
- var ENV_TYPE = {
105
- WEAPP: 'WEAPP',
106
- SWAN: 'SWAN',
107
- ALIPAY: 'ALIPAY',
108
- TT: 'TT',
109
- QQ: 'QQ',
110
- JD: 'JD',
111
- WEB: 'WEB',
112
- RN: 'RN',
113
- HARMONY: 'HARMONY',
114
- QUICKAPP: 'QUICKAPP'
115
- };
116
- var isWeb = shared.isWebPlatform();
117
- function getEnv() {
118
- if (process.env.TARO_ENV === 'weapp') {
119
- return ENV_TYPE.WEAPP;
120
- } else if (process.env.TARO_ENV === 'alipay') {
121
- return ENV_TYPE.ALIPAY;
122
- } else if (process.env.TARO_ENV === 'swan') {
123
- return ENV_TYPE.SWAN;
124
- } else if (process.env.TARO_ENV === 'tt') {
125
- return ENV_TYPE.TT;
126
- } else if (process.env.TARO_ENV === 'jd') {
127
- return ENV_TYPE.JD;
128
- } else if (process.env.TARO_ENV === 'qq') {
129
- return ENV_TYPE.QQ;
130
- } else if (isWeb) {
131
- return ENV_TYPE.WEB;
132
- } else if (process.env.TARO_ENV === 'rn') {
133
- return ENV_TYPE.RN;
134
- } else if (process.env.TARO_ENV === 'harmony') {
135
- return ENV_TYPE.HARMONY;
136
- } else if (process.env.TARO_ENV === 'quickapp') {
137
- return ENV_TYPE.QUICKAPP;
138
- } else {
139
- return process.env.TARO_ENV || 'Unknown';
140
- }
141
57
  }
142
58
 
143
- var Chain = /*#__PURE__*/function () {
144
- function Chain(requestParams, interceptors, index) {
145
- _classCallCheck__default["default"](this, Chain);
146
- this.index = index || 0;
147
- this.requestParams = requestParams;
148
- this.interceptors = interceptors || [];
149
- }
150
- _createClass__default["default"](Chain, [{
151
- key: "proceed",
152
- value: function proceed(requestParams) {
153
- this.requestParams = requestParams;
154
- if (this.index >= this.interceptors.length) {
155
- throw new Error('chain 参数错误, 请勿直接修改 request.chain');
156
- }
157
- var nextInterceptor = this._getNextInterceptor();
158
- var nextChain = this._getNextChain();
159
- var p = nextInterceptor(nextChain);
160
- var res = p.catch(function (err) {
161
- return Promise.reject(err);
162
- });
163
- Object.keys(p).forEach(function (k) {
164
- return isFunction(p[k]) && (res[k] = p[k]);
165
- });
166
- return res;
59
+ class Chain {
60
+ constructor(requestParams, interceptors, index) {
61
+ this.index = index || 0;
62
+ this.requestParams = requestParams || {};
63
+ this.interceptors = interceptors || [];
64
+ }
65
+ proceed(requestParams = {}) {
66
+ this.requestParams = requestParams;
67
+ if (this.index >= this.interceptors.length) {
68
+ throw new Error('chain 参数错误, 请勿直接修改 request.chain');
69
+ }
70
+ const nextInterceptor = this._getNextInterceptor();
71
+ const nextChain = this._getNextChain();
72
+ const p = nextInterceptor(nextChain);
73
+ const res = p.catch(err => Promise.reject(err));
74
+ Object.keys(p).forEach(k => shared.isFunction(p[k]) && (res[k] = p[k]));
75
+ return res;
167
76
  }
168
- }, {
169
- key: "_getNextInterceptor",
170
- value: function _getNextInterceptor() {
171
- return this.interceptors[this.index];
77
+ _getNextInterceptor() {
78
+ return this.interceptors[this.index];
172
79
  }
173
- }, {
174
- key: "_getNextChain",
175
- value: function _getNextChain() {
176
- return new Chain(this.requestParams, this.interceptors, this.index + 1);
80
+ _getNextChain() {
81
+ return new Chain(this.requestParams, this.interceptors, this.index + 1);
177
82
  }
178
- }]);
179
- return Chain;
180
- }();
83
+ }
181
84
 
182
- var Link = /*#__PURE__*/function () {
183
- function Link(interceptor) {
184
- _classCallCheck__default["default"](this, Link);
185
- this.taroInterceptor = interceptor;
186
- this.chain = new Chain();
187
- }
188
- _createClass__default["default"](Link, [{
189
- key: "request",
190
- value: function request(requestParams) {
191
- var chain = this.chain;
192
- var taroInterceptor = this.taroInterceptor;
193
- chain.interceptors = chain.interceptors.filter(function (interceptor) {
194
- return interceptor !== taroInterceptor;
195
- }).concat(taroInterceptor);
196
- return chain.proceed(_objectSpread__default["default"]({}, requestParams));
85
+ class Link {
86
+ constructor(interceptor) {
87
+ this.taroInterceptor = interceptor;
88
+ this.chain = new Chain();
89
+ }
90
+ request(requestParams) {
91
+ const chain = this.chain;
92
+ const taroInterceptor = this.taroInterceptor;
93
+ chain.interceptors = chain.interceptors
94
+ .filter(interceptor => interceptor !== taroInterceptor)
95
+ .concat(taroInterceptor);
96
+ return chain.proceed(Object.assign({}, requestParams));
197
97
  }
198
- }, {
199
- key: "addInterceptor",
200
- value: function addInterceptor(interceptor) {
201
- this.chain.interceptors.push(interceptor);
98
+ addInterceptor(interceptor) {
99
+ this.chain.interceptors.push(interceptor);
202
100
  }
203
- }, {
204
- key: "cleanInterceptors",
205
- value: function cleanInterceptors() {
206
- this.chain = new Chain();
101
+ cleanInterceptors() {
102
+ this.chain = new Chain();
207
103
  }
208
- }]);
209
- return Link;
210
- }();
104
+ }
211
105
  function interceptorify(promiseifyApi) {
212
- return new Link(function (chain) {
213
- return promiseifyApi(chain.requestParams);
214
- });
106
+ return new Link(function (chain) {
107
+ return promiseifyApi(chain.requestParams);
108
+ });
215
109
  }
216
110
 
217
111
  function timeoutInterceptor(chain) {
218
- var requestParams = chain.requestParams;
219
- var p;
220
- var res = new Promise(function (resolve, reject) {
221
- var timeout = setTimeout(function () {
222
- timeout = null;
223
- reject(new Error('网络链接超时,请稍后再试!'));
224
- }, requestParams && requestParams.timeout || 60000);
225
- p = chain.proceed(requestParams);
226
- p.then(function (res) {
227
- if (!timeout) return;
228
- clearTimeout(timeout);
229
- resolve(res);
230
- }).catch(function (err) {
231
- timeout && clearTimeout(timeout);
232
- reject(err);
112
+ const requestParams = chain.requestParams;
113
+ let p;
114
+ const res = new Promise((resolve, reject) => {
115
+ const timeout = setTimeout(() => {
116
+ clearTimeout(timeout);
117
+ reject(new Error('网络链接超时,请稍后再试!'));
118
+ }, (requestParams && requestParams.timeout) || 60000);
119
+ p = chain.proceed(requestParams);
120
+ p
121
+ .then(res => {
122
+ if (!timeout)
123
+ return;
124
+ clearTimeout(timeout);
125
+ resolve(res);
126
+ })
127
+ .catch(err => {
128
+ timeout && clearTimeout(timeout);
129
+ reject(err);
130
+ });
233
131
  });
234
- });
235
- if (!isUndefined(p) && isFunction(p.abort)) res.abort = p.abort;
236
- return res;
132
+ // @ts-ignore
133
+ if (!shared.isUndefined(p) && shared.isFunction(p.abort))
134
+ res.abort = p.abort;
135
+ return res;
237
136
  }
238
137
  function logInterceptor(chain) {
239
- var requestParams = chain.requestParams;
240
- var method = requestParams.method,
241
- data = requestParams.data,
242
- url = requestParams.url;
243
-
244
- // eslint-disable-next-line no-console
245
- console.log("http ".concat(method || 'GET', " --> ").concat(url, " data: "), data);
246
- var p = chain.proceed(requestParams);
247
- var res = p.then(function (res) {
138
+ const requestParams = chain.requestParams;
139
+ const { method, data, url } = requestParams;
248
140
  // eslint-disable-next-line no-console
249
- console.log("http <-- ".concat(url, " result:"), res);
141
+ console.log(`http ${method || 'GET'} --> ${url} data: `, data);
142
+ const p = chain.proceed(requestParams);
143
+ const res = p
144
+ .then(res => {
145
+ // eslint-disable-next-line no-console
146
+ console.log(`http <-- ${url} result:`, res);
147
+ return res;
148
+ });
149
+ // @ts-ignore
150
+ if (shared.isFunction(p.abort))
151
+ res.abort = p.abort;
250
152
  return res;
251
- });
252
- if (isFunction(p.abort)) res.abort = p.abort;
253
- return res;
254
153
  }
255
154
 
256
155
  var interceptors = /*#__PURE__*/Object.freeze({
257
156
  __proto__: null,
258
- timeoutInterceptor: timeoutInterceptor,
259
- logInterceptor: logInterceptor
157
+ logInterceptor: logInterceptor,
158
+ timeoutInterceptor: timeoutInterceptor
260
159
  });
261
160
 
262
161
  function Behavior(options) {
263
- return options;
162
+ return options;
264
163
  }
265
164
  function getPreload(current) {
266
- return function (key, val) {
267
- current.preloadData = isObject(key) ? key : _defineProperty__default["default"]({}, key, val);
268
- };
165
+ return function (key, val) {
166
+ current.preloadData = shared.isObject(key)
167
+ ? key
168
+ : {
169
+ [key]: val
170
+ };
171
+ };
269
172
  }
270
- var defaultDesignWidth = 750;
271
- var defaultDesignRatio = {
272
- 640: 2.34 / 2,
273
- 750: 1,
274
- 828: 1.81 / 2
173
+ const defaultDesignWidth = 750;
174
+ const defaultDesignRatio = {
175
+ 640: 2.34 / 2,
176
+ 750: 1,
177
+ 828: 1.81 / 2
275
178
  };
276
- var defaultBaseFontSize = 20;
277
- var defaultUnitPrecision = 5;
278
- var defaultTargetUnit = 'rpx';
179
+ const defaultBaseFontSize = 20;
180
+ const defaultUnitPrecision = 5;
181
+ const defaultTargetUnit = 'rpx';
279
182
  function getInitPxTransform(taro) {
280
- return function (config) {
281
- var _config$designWidth = config.designWidth,
282
- designWidth = _config$designWidth === void 0 ? defaultDesignWidth : _config$designWidth,
283
- _config$deviceRatio = config.deviceRatio,
284
- deviceRatio = _config$deviceRatio === void 0 ? defaultDesignRatio : _config$deviceRatio,
285
- _config$baseFontSize = config.baseFontSize,
286
- baseFontSize = _config$baseFontSize === void 0 ? defaultBaseFontSize : _config$baseFontSize,
287
- _config$targetUnit = config.targetUnit,
288
- targetUnit = _config$targetUnit === void 0 ? defaultTargetUnit : _config$targetUnit,
289
- _config$unitPrecision = config.unitPrecision,
290
- unitPrecision = _config$unitPrecision === void 0 ? defaultUnitPrecision : _config$unitPrecision;
291
- taro.config = taro.config || {};
292
- taro.config.designWidth = designWidth;
293
- taro.config.deviceRatio = deviceRatio;
294
- taro.config.baseFontSize = baseFontSize;
295
- taro.config.targetUnit = targetUnit;
296
- taro.config.unitPrecision = unitPrecision;
297
- };
183
+ return function (config) {
184
+ const { designWidth = defaultDesignWidth, deviceRatio = defaultDesignRatio, baseFontSize = defaultBaseFontSize, targetUnit = defaultTargetUnit, unitPrecision = defaultUnitPrecision, } = config;
185
+ taro.config = taro.config || {};
186
+ taro.config.designWidth = designWidth;
187
+ taro.config.deviceRatio = deviceRatio;
188
+ taro.config.baseFontSize = baseFontSize;
189
+ taro.config.targetUnit = targetUnit;
190
+ taro.config.unitPrecision = unitPrecision;
191
+ };
298
192
  }
299
193
  function getPxTransform(taro) {
300
- return function (size) {
301
- var config = taro.config || {};
302
- var baseFontSize = config.baseFontSize;
303
- var deviceRatio = config.deviceRatio || defaultDesignRatio;
304
- var designWidth = function () {
305
- var input = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;
306
- return isFunction(config.designWidth) ? config.designWidth(input) : config.designWidth || defaultDesignWidth;
307
- }(size);
308
- if (!(designWidth in deviceRatio)) {
309
- throw new Error("deviceRatio \u914D\u7F6E\u4E2D\u4E0D\u5B58\u5728 ".concat(designWidth, " \u7684\u8BBE\u7F6E\uFF01"));
310
- }
311
- var targetUnit = config.targetUnit || defaultTargetUnit;
312
- var unitPrecision = config.unitPrecision || defaultUnitPrecision;
313
- var formatSize = ~~size;
314
- var rootValue = 1 / deviceRatio[designWidth];
315
- switch (targetUnit) {
316
- case 'rem':
317
- rootValue *= baseFontSize * 2;
318
- break;
319
- case 'px':
320
- rootValue *= 2;
321
- break;
322
- }
323
- var val = formatSize / rootValue;
324
- if (unitPrecision >= 0 && unitPrecision <= 100) {
325
- val = Number(val.toFixed(unitPrecision));
326
- }
327
- return val + targetUnit;
328
- };
194
+ return function (size) {
195
+ const config = taro.config || {};
196
+ const baseFontSize = config.baseFontSize;
197
+ const deviceRatio = config.deviceRatio || defaultDesignRatio;
198
+ const designWidth = (((input = 0) => shared.isFunction(config.designWidth)
199
+ ? config.designWidth(input)
200
+ : config.designWidth || defaultDesignWidth))(size);
201
+ if (!(designWidth in deviceRatio)) {
202
+ throw new Error(`deviceRatio 配置中不存在 ${designWidth} 的设置!`);
203
+ }
204
+ const targetUnit = config.targetUnit || defaultTargetUnit;
205
+ const unitPrecision = config.unitPrecision || defaultUnitPrecision;
206
+ const formatSize = ~~size;
207
+ let rootValue = 1 / deviceRatio[designWidth];
208
+ switch (targetUnit) {
209
+ case 'rem':
210
+ rootValue *= baseFontSize * 2;
211
+ break;
212
+ case 'px':
213
+ rootValue *= 2;
214
+ break;
215
+ }
216
+ let val = formatSize / rootValue;
217
+ if (unitPrecision >= 0 && unitPrecision <= 100) {
218
+ val = Number(val.toFixed(unitPrecision));
219
+ }
220
+ return val + targetUnit;
221
+ };
329
222
  }
330
223
 
331
224
  /* eslint-disable camelcase */
332
- var Taro = {
333
- Behavior: Behavior,
334
- getEnv: getEnv,
335
- ENV_TYPE: ENV_TYPE,
336
- Link: Link,
337
- interceptors: interceptors,
338
- Current: runtime.Current,
339
- getCurrentInstance: runtime.getCurrentInstance,
340
- options: runtime.options,
341
- nextTick: runtime.nextTick,
342
- eventCenter: runtime.eventCenter,
343
- Events: runtime.Events,
344
- getInitPxTransform: getInitPxTransform,
345
- interceptorify: interceptorify
225
+ const Taro = {
226
+ Behavior,
227
+ getEnv,
228
+ ENV_TYPE,
229
+ Link,
230
+ interceptors,
231
+ Current: runtime.Current,
232
+ getCurrentInstance: runtime.getCurrentInstance,
233
+ options: runtime.options,
234
+ nextTick: runtime.nextTick,
235
+ eventCenter: runtime.eventCenter,
236
+ Events: runtime.Events,
237
+ getInitPxTransform,
238
+ interceptorify
346
239
  };
347
240
  Taro.initPxTransform = getInitPxTransform(Taro);
348
241
  Taro.preload = getPreload(runtime.Current);
349
242
  Taro.pxTransform = getPxTransform(Taro);
350
243
 
351
- exports["default"] = Taro;
352
-
353
- Object.defineProperty(exports, '__esModule', { value: true });
244
+ return Taro;
354
245
 
355
246
  }));
356
- //# sourceMappingURL=taro.js.map
@@ -0,0 +1,5 @@
1
+ declare function Behavior(options: any): any;
2
+ declare function getPreload(current: any): (key: string | Record<string, unknown>, val: unknown) => void;
3
+ declare function getInitPxTransform(taro: any): (config: any) => void;
4
+ declare function getPxTransform(taro: any): (size: any) => any;
5
+ export { Behavior, getPreload, getInitPxTransform, getPxTransform };
package/dist/tools.js ADDED
@@ -0,0 +1,66 @@
1
+ import { isObject, isFunction } from '@tarojs/shared';
2
+
3
+ function Behavior(options) {
4
+ return options;
5
+ }
6
+ function getPreload(current) {
7
+ return function (key, val) {
8
+ current.preloadData = isObject(key)
9
+ ? key
10
+ : {
11
+ [key]: val
12
+ };
13
+ };
14
+ }
15
+ const defaultDesignWidth = 750;
16
+ const defaultDesignRatio = {
17
+ 640: 2.34 / 2,
18
+ 750: 1,
19
+ 828: 1.81 / 2
20
+ };
21
+ const defaultBaseFontSize = 20;
22
+ const defaultUnitPrecision = 5;
23
+ const defaultTargetUnit = 'rpx';
24
+ function getInitPxTransform(taro) {
25
+ return function (config) {
26
+ const { designWidth = defaultDesignWidth, deviceRatio = defaultDesignRatio, baseFontSize = defaultBaseFontSize, targetUnit = defaultTargetUnit, unitPrecision = defaultUnitPrecision, } = config;
27
+ taro.config = taro.config || {};
28
+ taro.config.designWidth = designWidth;
29
+ taro.config.deviceRatio = deviceRatio;
30
+ taro.config.baseFontSize = baseFontSize;
31
+ taro.config.targetUnit = targetUnit;
32
+ taro.config.unitPrecision = unitPrecision;
33
+ };
34
+ }
35
+ function getPxTransform(taro) {
36
+ return function (size) {
37
+ const config = taro.config || {};
38
+ const baseFontSize = config.baseFontSize;
39
+ const deviceRatio = config.deviceRatio || defaultDesignRatio;
40
+ const designWidth = (((input = 0) => isFunction(config.designWidth)
41
+ ? config.designWidth(input)
42
+ : config.designWidth || defaultDesignWidth))(size);
43
+ if (!(designWidth in deviceRatio)) {
44
+ throw new Error(`deviceRatio 配置中不存在 ${designWidth} 的设置!`);
45
+ }
46
+ const targetUnit = config.targetUnit || defaultTargetUnit;
47
+ const unitPrecision = config.unitPrecision || defaultUnitPrecision;
48
+ const formatSize = ~~size;
49
+ let rootValue = 1 / deviceRatio[designWidth];
50
+ switch (targetUnit) {
51
+ case 'rem':
52
+ rootValue *= baseFontSize * 2;
53
+ break;
54
+ case 'px':
55
+ rootValue *= 2;
56
+ break;
57
+ }
58
+ let val = formatSize / rootValue;
59
+ if (unitPrecision >= 0 && unitPrecision <= 100) {
60
+ val = Number(val.toFixed(unitPrecision));
61
+ }
62
+ return val + targetUnit;
63
+ };
64
+ }
65
+
66
+ export { Behavior, getInitPxTransform, getPreload, getPxTransform };