util-helpers 4.20.2 → 4.20.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.
package/esm/VERSION.js CHANGED
@@ -1,4 +1,4 @@
1
- var VERSION = "4.20.2";
1
+ var VERSION = "4.20.4";
2
2
  var VERSION$1 = VERSION;
3
3
 
4
4
  export { VERSION$1 as default };
@@ -11,8 +11,8 @@ function canvasToBlob(canvas, type, quality) {
11
11
  function compressImage(img, options) {
12
12
  if (options === void 0) { options = {}; }
13
13
  return new Promise(function (resolve, reject) {
14
- var width = options.width, height = options.height, rotate = options.rotate, _a = options.offset, offset = _a === void 0 ? [0, 0] : _a, _b = options.cacheImage, cacheImage = _b === void 0 ? true : _b, _c = options.background, background = _c === void 0 ? '#fff' : _c, canvasWidth = options.canvasWidth, canvasHeight = options.canvasHeight, _d = options.format, format = _d === void 0 ? 'blob' : _d, _e = options.type, type = _e === void 0 ? 'image/jpeg' : _e, _f = options.quality, quality = _f === void 0 ? 0.8 : _f, beforeCompress = options.beforeCompress, beforeDraw = options.beforeDraw, afterDraw = options.afterDraw;
15
- loadImageWithBlob(img, cacheImage)
14
+ var width = options.width, height = options.height, rotate = options.rotate, _a = options.offset, offset = _a === void 0 ? [0, 0] : _a, _b = options.cacheImage, cacheImage = _b === void 0 ? true : _b, _c = options.background, background = _c === void 0 ? '#fff' : _c, canvasWidth = options.canvasWidth, canvasHeight = options.canvasHeight, _d = options.format, format = _d === void 0 ? 'blob' : _d, _e = options.type, type = _e === void 0 ? 'image/jpeg' : _e, _f = options.quality, quality = _f === void 0 ? 0.8 : _f, beforeCompress = options.beforeCompress, beforeDraw = options.beforeDraw, afterDraw = options.afterDraw, ajaxOptions = options.ajaxOptions;
15
+ loadImageWithBlob(img, cacheImage, ajaxOptions)
16
16
  .then(function (_a) {
17
17
  var image = _a.image, blob = _a.blob;
18
18
  var numWidth = toNumber(width);
@@ -10,14 +10,14 @@ function calcContrast(w, h) {
10
10
  }
11
11
  var cacheImage;
12
12
  var cacheResult;
13
- function getImageInfo(img, useCache) {
13
+ function getImageInfo(img, useCache, ajaxOptions) {
14
14
  if (useCache === void 0) { useCache = true; }
15
15
  return new Promise(function (resolve, reject) {
16
16
  if (useCache && cacheImage === img && cacheResult) {
17
17
  resolve(cacheResult);
18
18
  }
19
19
  else {
20
- loadImageWithBlob(img, false)
20
+ loadImageWithBlob(img, false, ajaxOptions)
21
21
  .then(function (_a) {
22
22
  var image = _a.image, blob = _a.blob;
23
23
  var width = image.width, height = image.height;
package/esm/index.js CHANGED
@@ -63,6 +63,6 @@ export { default as findTreeSelect } from './findTreeSelect.js';
63
63
  export { setDisableWarning } from './utils/config.js';
64
64
  export { default as VERSION } from './VERSION.js';
65
65
 
66
- var version = "4.20.2";
66
+ var version = "4.20.4";
67
67
 
68
68
  export { version };
@@ -1,38 +1,44 @@
1
+ import { __assign } from 'tslib';
1
2
  import { isBlob } from 'ut2';
2
3
  import ajax from './ajax.js';
3
4
  import { createObjectURL, revokeObjectURL } from './utils/native.js';
4
5
 
5
6
  var SuccessResponseStatus = [200, 304];
6
- function getBlob(img) {
7
+ function getBlob(img, ajaxOptions) {
7
8
  return new Promise(function (resolve, reject) {
8
9
  if (isBlob(img)) {
9
10
  resolve(img);
10
11
  }
11
12
  else {
12
- ajax(img, { responseType: 'blob' })
13
+ ajax(img, __assign({ responseType: 'blob' }, ajaxOptions))
13
14
  .then(function (ev) {
14
15
  var responseStatus = ev.target.status;
15
16
  if (SuccessResponseStatus.indexOf(responseStatus) !== -1) {
16
17
  resolve(ev.target.response);
17
18
  }
18
19
  else {
19
- reject(new Error("[loadImageWithBlob] The image does not support get requests, responseStatus ".concat(responseStatus, ", '").concat(img, "'.")));
20
+ var err = new Error("[loadImageWithBlob] The image does not support get requests, responseStatus ".concat(responseStatus, ", '").concat(img, "'."));
21
+ console.error(err);
22
+ reject(err);
20
23
  }
21
24
  })
22
- .catch(reject);
25
+ .catch(function (err) {
26
+ console.error(new Error("[loadImageWithBlob] Failed to request image. ".concat(err)));
27
+ reject(err);
28
+ });
23
29
  }
24
30
  });
25
31
  }
26
32
  var cacheImage;
27
33
  var cacheResult;
28
- function loadImageWithBlob(img, useCache) {
34
+ function loadImageWithBlob(img, useCache, ajaxOptions) {
29
35
  if (useCache === void 0) { useCache = true; }
30
36
  return new Promise(function (resolve, reject) {
31
37
  if (useCache && cacheImage === img && cacheResult) {
32
38
  resolve(cacheResult);
33
39
  }
34
40
  else {
35
- getBlob(img)
41
+ getBlob(img, ajaxOptions)
36
42
  .then(function (blob) {
37
43
  var url = createObjectURL(blob);
38
44
  var image = new Image();
package/lib/VERSION.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var VERSION = "4.20.2";
3
+ var VERSION = "4.20.4";
4
4
  var VERSION$1 = VERSION;
5
5
 
6
6
  module.exports = VERSION$1;
@@ -13,8 +13,8 @@ function canvasToBlob(canvas, type, quality) {
13
13
  function compressImage(img, options) {
14
14
  if (options === void 0) { options = {}; }
15
15
  return new Promise(function (resolve, reject) {
16
- var width = options.width, height = options.height, rotate = options.rotate, _a = options.offset, offset = _a === void 0 ? [0, 0] : _a, _b = options.cacheImage, cacheImage = _b === void 0 ? true : _b, _c = options.background, background = _c === void 0 ? '#fff' : _c, canvasWidth = options.canvasWidth, canvasHeight = options.canvasHeight, _d = options.format, format = _d === void 0 ? 'blob' : _d, _e = options.type, type = _e === void 0 ? 'image/jpeg' : _e, _f = options.quality, quality = _f === void 0 ? 0.8 : _f, beforeCompress = options.beforeCompress, beforeDraw = options.beforeDraw, afterDraw = options.afterDraw;
17
- loadImageWithBlob(img, cacheImage)
16
+ var width = options.width, height = options.height, rotate = options.rotate, _a = options.offset, offset = _a === void 0 ? [0, 0] : _a, _b = options.cacheImage, cacheImage = _b === void 0 ? true : _b, _c = options.background, background = _c === void 0 ? '#fff' : _c, canvasWidth = options.canvasWidth, canvasHeight = options.canvasHeight, _d = options.format, format = _d === void 0 ? 'blob' : _d, _e = options.type, type = _e === void 0 ? 'image/jpeg' : _e, _f = options.quality, quality = _f === void 0 ? 0.8 : _f, beforeCompress = options.beforeCompress, beforeDraw = options.beforeDraw, afterDraw = options.afterDraw, ajaxOptions = options.ajaxOptions;
17
+ loadImageWithBlob(img, cacheImage, ajaxOptions)
18
18
  .then(function (_a) {
19
19
  var image = _a.image, blob = _a.blob;
20
20
  var numWidth = ut2.toNumber(width);
@@ -12,14 +12,14 @@ function calcContrast(w, h) {
12
12
  }
13
13
  var cacheImage;
14
14
  var cacheResult;
15
- function getImageInfo(img, useCache) {
15
+ function getImageInfo(img, useCache, ajaxOptions) {
16
16
  if (useCache === void 0) { useCache = true; }
17
17
  return new Promise(function (resolve, reject) {
18
18
  if (useCache && cacheImage === img && cacheResult) {
19
19
  resolve(cacheResult);
20
20
  }
21
21
  else {
22
- loadImageWithBlob(img, false)
22
+ loadImageWithBlob(img, false, ajaxOptions)
23
23
  .then(function (_a) {
24
24
  var image = _a.image, blob = _a.blob;
25
25
  var width = image.width, height = image.height;
package/lib/index.js CHANGED
@@ -65,7 +65,7 @@ var findTreeSelect = require('./findTreeSelect.js');
65
65
  var config = require('./utils/config.js');
66
66
  var VERSION = require('./VERSION.js');
67
67
 
68
- exports.version = "4.20.2";
68
+ exports.version = "4.20.4";
69
69
 
70
70
  exports.isMobile = isMobile;
71
71
  exports.isTelephone = isTelephone;
@@ -1,40 +1,46 @@
1
1
  'use strict';
2
2
 
3
+ var tslib = require('tslib');
3
4
  var ut2 = require('ut2');
4
5
  var ajax = require('./ajax.js');
5
6
  var native = require('./utils/native.js');
6
7
 
7
8
  var SuccessResponseStatus = [200, 304];
8
- function getBlob(img) {
9
+ function getBlob(img, ajaxOptions) {
9
10
  return new Promise(function (resolve, reject) {
10
11
  if (ut2.isBlob(img)) {
11
12
  resolve(img);
12
13
  }
13
14
  else {
14
- ajax(img, { responseType: 'blob' })
15
+ ajax(img, tslib.__assign({ responseType: 'blob' }, ajaxOptions))
15
16
  .then(function (ev) {
16
17
  var responseStatus = ev.target.status;
17
18
  if (SuccessResponseStatus.indexOf(responseStatus) !== -1) {
18
19
  resolve(ev.target.response);
19
20
  }
20
21
  else {
21
- reject(new Error("[loadImageWithBlob] The image does not support get requests, responseStatus ".concat(responseStatus, ", '").concat(img, "'.")));
22
+ var err = new Error("[loadImageWithBlob] The image does not support get requests, responseStatus ".concat(responseStatus, ", '").concat(img, "'."));
23
+ console.error(err);
24
+ reject(err);
22
25
  }
23
26
  })
24
- .catch(reject);
27
+ .catch(function (err) {
28
+ console.error(new Error("[loadImageWithBlob] Failed to request image. ".concat(err)));
29
+ reject(err);
30
+ });
25
31
  }
26
32
  });
27
33
  }
28
34
  var cacheImage;
29
35
  var cacheResult;
30
- function loadImageWithBlob(img, useCache) {
36
+ function loadImageWithBlob(img, useCache, ajaxOptions) {
31
37
  if (useCache === void 0) { useCache = true; }
32
38
  return new Promise(function (resolve, reject) {
33
39
  if (useCache && cacheImage === img && cacheResult) {
34
40
  resolve(cacheResult);
35
41
  }
36
42
  else {
37
- getBlob(img)
43
+ getBlob(img, ajaxOptions)
38
44
  .then(function (blob) {
39
45
  var url = native.createObjectURL(blob);
40
46
  var image = new Image();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "util-helpers",
3
- "version": "4.20.2",
3
+ "version": "4.20.4",
4
4
  "description": "一个基于业务场景的工具方法库",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -1,3 +1,4 @@
1
+ import loadImageWithBlob from './loadImageWithBlob';
1
2
  type Info = {
2
3
  image: HTMLImageElement;
3
4
  blob: Blob;
@@ -9,7 +10,6 @@ type Options = {
9
10
  height?: number;
10
11
  rotate?: number;
11
12
  offset?: [number, number] | ((info: Info, options: Options) => [number, number]);
12
- cacheImage?: boolean;
13
13
  background?: string;
14
14
  canvasWidth?: number | ((info: Info, options: Options) => number);
15
15
  canvasHeight?: number | ((info: Info, options: Options) => number);
@@ -19,6 +19,8 @@ type Options = {
19
19
  beforeCompress?: (imageWithBlob: Pick<Info, 'image' | 'blob'>, options: Options) => void;
20
20
  beforeDraw?: (info: Info, options: Options) => void;
21
21
  afterDraw?: (info: Info, options: Options) => void;
22
+ cacheImage?: boolean;
23
+ ajaxOptions?: Parameters<typeof loadImageWithBlob>[2];
22
24
  };
23
25
  declare function compressImage(img: string | Blob, options: Omit<Options, 'format'> & {
24
26
  format: 'dataURL';
@@ -1,3 +1,4 @@
1
+ import loadImageWithBlob from './loadImageWithBlob';
1
2
  /**
2
3
  * 获取图片信息。
3
4
  *
@@ -8,6 +9,7 @@
8
9
  * @since 4.20.0
9
10
  * @param {string | Blob} img 图片地址或 blob 对象
10
11
  * @param {boolean} [useCache=true] 缓存最近一次成功结果,当图片地址或 blob 对象一致时,直接返回该缓存。避免连续请求同一个图片资源,重复加载问题。
12
+ * @param {AjaxOptions} [ajaxOptions] ajax 请求配置项,当传入的图片为字符串时才会触发请求。
11
13
  * @returns {Promise<ImageInfo>} 图片信息
12
14
  * @example
13
15
  *
@@ -24,7 +26,7 @@
24
26
  * });
25
27
  *
26
28
  */
27
- declare function getImageInfo(img: string | Blob, useCache?: boolean): Promise<{
29
+ declare function getImageInfo(img: string | Blob, useCache?: boolean, ajaxOptions?: Parameters<typeof loadImageWithBlob>[2]): Promise<{
28
30
  width: number;
29
31
  height: number;
30
32
  contrast: string;
@@ -1,3 +1,4 @@
1
+ import ajax from './ajax';
1
2
  /**
2
3
  * @typedef {Object} ImageWithBlob HTML图片元素和 blob 对象
3
4
  * @property {HTMLImageElement} image HTML图片元素
@@ -13,6 +14,7 @@
13
14
  * @since 4.20.0
14
15
  * @param {string | Blob} img 图片地址或 blob 对象
15
16
  * @param {boolean} [useCache=true] 缓存最近一次成功结果,当图片地址或 blob 对象一致时,直接返回该缓存。避免连续请求同一个图片资源,重复加载问题。
17
+ * @param {AjaxOptions} [ajaxOptions] ajax 请求配置项,当传入的图片为字符串时才会触发请求。
16
18
  * @returns {Promise<ImageWithBlob>} HTML图片元素和 blob 对象
17
19
  * @example
18
20
  *
@@ -29,7 +31,7 @@
29
31
  * });
30
32
  *
31
33
  */
32
- declare function loadImageWithBlob(img: string | Blob, useCache?: boolean): Promise<{
34
+ declare function loadImageWithBlob(img: string | Blob, useCache?: boolean, ajaxOptions?: Parameters<typeof ajax>[1]): Promise<{
33
35
  image: HTMLImageElement;
34
36
  blob: Blob;
35
37
  }>;