util-helpers 4.20.1 → 4.20.3

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.
Files changed (45) hide show
  1. package/dist/util-helpers.js +42 -38
  2. package/dist/util-helpers.js.map +1 -1
  3. package/dist/util-helpers.min.js +1 -1
  4. package/dist/util-helpers.min.js.map +1 -1
  5. package/esm/VERSION.js +1 -1
  6. package/esm/ajax.js +4 -2
  7. package/esm/calculateCursorPosition.js +2 -2
  8. package/esm/compressImage.js +2 -2
  9. package/esm/download.js +3 -2
  10. package/esm/filterTree.js +3 -3
  11. package/esm/findTreeNode.js +3 -3
  12. package/esm/findTreeNodes.js +3 -3
  13. package/esm/findTreeSelect.js +3 -3
  14. package/esm/getImageInfo.js +2 -2
  15. package/esm/index.js +1 -1
  16. package/esm/listToTree.js +2 -2
  17. package/esm/loadImage.js +4 -3
  18. package/esm/loadImageWithBlob.js +9 -7
  19. package/esm/loadScript.js +2 -1
  20. package/esm/transformFieldNames.js +5 -4
  21. package/esm/treeToList.js +3 -3
  22. package/esm/utils/native.js +5 -0
  23. package/lib/VERSION.js +1 -1
  24. package/lib/ajax.js +4 -2
  25. package/lib/calculateCursorPosition.js +1 -1
  26. package/lib/compressImage.js +2 -2
  27. package/lib/download.js +3 -2
  28. package/lib/filterTree.js +2 -2
  29. package/lib/findTreeNode.js +2 -2
  30. package/lib/findTreeNodes.js +2 -2
  31. package/lib/findTreeSelect.js +2 -2
  32. package/lib/getImageInfo.js +2 -2
  33. package/lib/index.js +1 -1
  34. package/lib/listToTree.js +1 -1
  35. package/lib/loadImage.js +4 -3
  36. package/lib/loadImageWithBlob.js +9 -7
  37. package/lib/loadScript.js +2 -1
  38. package/lib/transformFieldNames.js +4 -3
  39. package/lib/treeToList.js +2 -2
  40. package/lib/utils/native.js +5 -0
  41. package/package.json +2 -2
  42. package/types/compressImage.d.ts +3 -1
  43. package/types/getImageInfo.d.ts +3 -1
  44. package/types/loadImageWithBlob.d.ts +3 -1
  45. package/types/utils/native.d.ts +6 -0
@@ -1,16 +1,18 @@
1
1
  'use strict';
2
2
 
3
+ var tslib = require('tslib');
3
4
  var ut2 = require('ut2');
4
5
  var ajax = require('./ajax.js');
6
+ var native = require('./utils/native.js');
5
7
 
6
8
  var SuccessResponseStatus = [200, 304];
7
- function getBlob(img) {
9
+ function getBlob(img, ajaxOptions) {
8
10
  return new Promise(function (resolve, reject) {
9
11
  if (ut2.isBlob(img)) {
10
12
  resolve(img);
11
13
  }
12
14
  else {
13
- ajax(img, { responseType: 'blob' })
15
+ ajax(img, tslib.__assign({ responseType: 'blob', headers: { 'sec-fetch-site': 'none' } }, ajaxOptions))
14
16
  .then(function (ev) {
15
17
  var responseStatus = ev.target.status;
16
18
  if (SuccessResponseStatus.indexOf(responseStatus) !== -1) {
@@ -26,19 +28,19 @@ function getBlob(img) {
26
28
  }
27
29
  var cacheImage;
28
30
  var cacheResult;
29
- function loadImageWithBlob(img, useCache) {
31
+ function loadImageWithBlob(img, useCache, ajaxOptions) {
30
32
  if (useCache === void 0) { useCache = true; }
31
33
  return new Promise(function (resolve, reject) {
32
34
  if (useCache && cacheImage === img && cacheResult) {
33
35
  resolve(cacheResult);
34
36
  }
35
37
  else {
36
- getBlob(img)
38
+ getBlob(img, ajaxOptions)
37
39
  .then(function (blob) {
38
- var url = URL.createObjectURL(blob);
40
+ var url = native.createObjectURL(blob);
39
41
  var image = new Image();
40
42
  image.onload = function () {
41
- URL.revokeObjectURL(url);
43
+ native.revokeObjectURL(url);
42
44
  var result = { blob: blob, image: image };
43
45
  if (useCache) {
44
46
  cacheImage = img;
@@ -47,7 +49,7 @@ function loadImageWithBlob(img, useCache) {
47
49
  resolve(result);
48
50
  };
49
51
  image.onerror = function (err) {
50
- URL.revokeObjectURL(url);
52
+ native.revokeObjectURL(url);
51
53
  console.error("[loadImageWithBlob] The image load failed, '".concat(img, "'."));
52
54
  reject(err);
53
55
  };
package/lib/loadScript.js CHANGED
@@ -1,6 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  var tslib = require('tslib');
4
+ var native = require('./utils/native.js');
4
5
 
5
6
  function loadScript(src, options) {
6
7
  return new Promise(function (resolve, reject) {
@@ -15,7 +16,7 @@ function loadScript(src, options) {
15
16
  script[key] = props[key];
16
17
  }
17
18
  if (typeof attrs === 'object') {
18
- Object.keys(attrs).forEach(function (key) {
19
+ native.objectKeys(attrs).forEach(function (key) {
19
20
  script.setAttribute(key, attrs[key]);
20
21
  });
21
22
  }
@@ -2,10 +2,11 @@
2
2
 
3
3
  var tslib = require('tslib');
4
4
  var ut2 = require('ut2');
5
+ var native = require('./utils/native.js');
5
6
 
6
7
  function transformFieldNames(data, fieldNames, childrenField, nodeAssign) {
7
8
  if (nodeAssign === void 0) { nodeAssign = 'spread'; }
8
- if (!Array.isArray(data)) {
9
+ if (!ut2.isArray(data)) {
9
10
  return data;
10
11
  }
11
12
  if (data.length <= 0) {
@@ -18,10 +19,10 @@ function transformFieldNames(data, fieldNames, childrenField, nodeAssign) {
18
19
  }
19
20
  var newItem = nodeAssign === 'spread' ? tslib.__assign({}, item) : item;
20
21
  var delKeys = [];
21
- if (childrenField && Array.isArray(newItem[childrenField]) && newItem[childrenField].length > 0) {
22
+ if (childrenField && ut2.isArray(newItem[childrenField]) && newItem[childrenField].length > 0) {
22
23
  newItem[childrenField] = recusion(newItem[childrenField].slice());
23
24
  }
24
- Object.keys(fieldNames).forEach(function (newKey) {
25
+ native.objectKeys(fieldNames).forEach(function (newKey) {
25
26
  var oldKey = fieldNames[newKey];
26
27
  if (oldKey in newItem) {
27
28
  newItem[newKey] = newItem[oldKey];
package/lib/treeToList.js CHANGED
@@ -5,7 +5,7 @@ var ut2 = require('ut2');
5
5
 
6
6
  function treeToList(tree, childrenField) {
7
7
  var list = [];
8
- if (!Array.isArray(tree)) {
8
+ if (!ut2.isArray(tree)) {
9
9
  return list;
10
10
  }
11
11
  function recusion(arr) {
@@ -14,7 +14,7 @@ function treeToList(tree, childrenField) {
14
14
  var newItem = tslib.__assign({}, item);
15
15
  list.push(newItem);
16
16
  if (newItem[childrenField]) {
17
- if (Array.isArray(newItem[childrenField]) && newItem[childrenField].length > 0) {
17
+ if (ut2.isArray(newItem[childrenField]) && newItem[childrenField].length > 0) {
18
18
  recusion(newItem[childrenField]);
19
19
  }
20
20
  delete newItem[childrenField];
@@ -0,0 +1,5 @@
1
+ 'use strict';
2
+
3
+ exports.objectKeys = Object.keys;
4
+ exports.createObjectURL = URL.createObjectURL;
5
+ exports.revokeObjectURL = URL.revokeObjectURL;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "util-helpers",
3
- "version": "4.20.1",
3
+ "version": "4.20.3",
4
4
  "description": "一个基于业务场景的工具方法库",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -94,6 +94,6 @@
94
94
  },
95
95
  "dependencies": {
96
96
  "tslib": "^2.6.2",
97
- "ut2": "^1.4.6"
97
+ "ut2": "^1.4.8"
98
98
  }
99
99
  }
@@ -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
  }>;
@@ -0,0 +1,6 @@
1
+ export declare const objectKeys: {
2
+ (o: object): string[];
3
+ (o: {}): string[];
4
+ };
5
+ export declare const createObjectURL: (obj: Blob | MediaSource) => string;
6
+ export declare const revokeObjectURL: (url: string) => void;