util-helpers 4.20.8 → 4.21.1

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.8";
1
+ var VERSION = "4.21.1";
2
2
  var VERSION$1 = VERSION;
3
3
 
4
4
  export { VERSION$1 as default };
@@ -44,7 +44,13 @@ function compressImage(img, options) {
44
44
  }
45
45
  var outOffset = typeof offset === 'function' ? offset(info, options) : offset;
46
46
  beforeDraw === null || beforeDraw === void 0 ? void 0 : beforeDraw(info, options);
47
- ctx.drawImage(image, internalOffset[0] + toNumber(outOffset[0]), internalOffset[1] + toNumber(outOffset[1]), image.width, image.height);
47
+ var dx = internalOffset[0] + toNumber(outOffset[0]);
48
+ var dy = internalOffset[1] + toNumber(outOffset[1]);
49
+ ctx.drawImage(image, dx, dy, image.width, image.height);
50
+ if (type === 'image/png') {
51
+ ctx.globalCompositeOperation = 'destination-in';
52
+ ctx.drawImage(image, dx, dy, image.width, image.height);
53
+ }
48
54
  afterDraw === null || afterDraw === void 0 ? void 0 : afterDraw(info, options);
49
55
  if (format === 'blob') {
50
56
  canvasToBlob(canvas, type, numQuality).then(resolve).catch(reject);
@@ -0,0 +1,32 @@
1
+ import { __assign } from 'tslib';
2
+ import { isBlob } from 'ut2';
3
+ import ajax from './ajax.js';
4
+
5
+ var SuccessResponseStatus = [200, 304];
6
+ function getFileBlob(file, ajaxOptions) {
7
+ return new Promise(function (resolve, reject) {
8
+ if (isBlob(file)) {
9
+ resolve(file);
10
+ }
11
+ else {
12
+ ajax(file, __assign({ responseType: 'blob' }, ajaxOptions))
13
+ .then(function (ev) {
14
+ var responseStatus = ev.target.status;
15
+ if (SuccessResponseStatus.indexOf(responseStatus) !== -1) {
16
+ resolve(ev.target.response);
17
+ }
18
+ else {
19
+ var err = new Error("The file does not support get requests, responseStatus ".concat(responseStatus, ", '").concat(file, "'."));
20
+ console.error(err);
21
+ reject(err);
22
+ }
23
+ })
24
+ .catch(function (err) {
25
+ console.error(new Error("Failed to request file. ".concat(err)));
26
+ reject(err);
27
+ });
28
+ }
29
+ });
30
+ }
31
+
32
+ export { getFileBlob as default };
package/esm/index.js CHANGED
@@ -46,6 +46,7 @@ export { default as ajax } from './ajax.js';
46
46
  export { default as calculateCursorPosition } from './calculateCursorPosition.js';
47
47
  export { default as compressImage } from './compressImage.js';
48
48
  export { default as download } from './download.js';
49
+ export { default as getFileBlob } from './getFileBlob.js';
49
50
  export { default as getImageInfo } from './getImageInfo.js';
50
51
  export { default as loadImage } from './loadImage.js';
51
52
  export { default as loadImageWithBlob } from './loadImageWithBlob.js';
@@ -63,6 +64,6 @@ export { default as findTreeSelect } from './findTreeSelect.js';
63
64
  export { setDisableWarning } from './utils/config.js';
64
65
  export { default as VERSION } from './VERSION.js';
65
66
 
66
- var version = "4.20.8";
67
+ var version = "4.21.1";
67
68
 
68
69
  export { version };
package/esm/loadImage.js CHANGED
@@ -17,9 +17,6 @@ function loadImage(img, useCache) {
17
17
  image_1.crossOrigin = 'anonymous';
18
18
  }
19
19
  image_1.onload = function () {
20
- if (imgIsBlob_1) {
21
- revokeObjectURL(url_1);
22
- }
23
20
  if (useCache) {
24
21
  cacheImage = img;
25
22
  cacheResult = image_1;
@@ -1,34 +1,6 @@
1
- import { __assign } from 'tslib';
2
- import { isBlob } from 'ut2';
3
- import ajax from './ajax.js';
4
1
  import { createObjectURL, revokeObjectURL } from './utils/native.js';
2
+ import getFileBlob from './getFileBlob.js';
5
3
 
6
- var SuccessResponseStatus = [200, 304];
7
- function getBlob(img, ajaxOptions) {
8
- return new Promise(function (resolve, reject) {
9
- if (isBlob(img)) {
10
- resolve(img);
11
- }
12
- else {
13
- ajax(img, __assign({ responseType: 'blob' }, ajaxOptions))
14
- .then(function (ev) {
15
- var responseStatus = ev.target.status;
16
- if (SuccessResponseStatus.indexOf(responseStatus) !== -1) {
17
- resolve(ev.target.response);
18
- }
19
- else {
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);
23
- }
24
- })
25
- .catch(function (err) {
26
- console.error(new Error("[loadImageWithBlob] Failed to request image. ".concat(err)));
27
- reject(err);
28
- });
29
- }
30
- });
31
- }
32
4
  var cacheImage;
33
5
  var cacheResult;
34
6
  function loadImageWithBlob(img, useCache, ajaxOptions) {
@@ -38,12 +10,11 @@ function loadImageWithBlob(img, useCache, ajaxOptions) {
38
10
  resolve(cacheResult);
39
11
  }
40
12
  else {
41
- getBlob(img, ajaxOptions)
13
+ getFileBlob(img, ajaxOptions)
42
14
  .then(function (blob) {
43
15
  var url = createObjectURL(blob);
44
16
  var image = new Image();
45
17
  image.onload = function () {
46
- revokeObjectURL(url);
47
18
  var result = { blob: blob, image: image };
48
19
  if (useCache) {
49
20
  cacheImage = img;
package/lib/VERSION.js CHANGED
@@ -1,6 +1,6 @@
1
1
  'use strict';
2
2
 
3
- var VERSION = "4.20.8";
3
+ var VERSION = "4.21.1";
4
4
  var VERSION$1 = VERSION;
5
5
 
6
6
  module.exports = VERSION$1;
@@ -46,7 +46,13 @@ function compressImage(img, options) {
46
46
  }
47
47
  var outOffset = typeof offset === 'function' ? offset(info, options) : offset;
48
48
  beforeDraw === null || beforeDraw === void 0 ? void 0 : beforeDraw(info, options);
49
- ctx.drawImage(image, internalOffset[0] + ut2.toNumber(outOffset[0]), internalOffset[1] + ut2.toNumber(outOffset[1]), image.width, image.height);
49
+ var dx = internalOffset[0] + ut2.toNumber(outOffset[0]);
50
+ var dy = internalOffset[1] + ut2.toNumber(outOffset[1]);
51
+ ctx.drawImage(image, dx, dy, image.width, image.height);
52
+ if (type === 'image/png') {
53
+ ctx.globalCompositeOperation = 'destination-in';
54
+ ctx.drawImage(image, dx, dy, image.width, image.height);
55
+ }
50
56
  afterDraw === null || afterDraw === void 0 ? void 0 : afterDraw(info, options);
51
57
  if (format === 'blob') {
52
58
  canvasToBlob(canvas, type, numQuality).then(resolve).catch(reject);
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ var tslib = require('tslib');
4
+ var ut2 = require('ut2');
5
+ var ajax = require('./ajax.js');
6
+
7
+ var SuccessResponseStatus = [200, 304];
8
+ function getFileBlob(file, ajaxOptions) {
9
+ return new Promise(function (resolve, reject) {
10
+ if (ut2.isBlob(file)) {
11
+ resolve(file);
12
+ }
13
+ else {
14
+ ajax(file, tslib.__assign({ responseType: 'blob' }, ajaxOptions))
15
+ .then(function (ev) {
16
+ var responseStatus = ev.target.status;
17
+ if (SuccessResponseStatus.indexOf(responseStatus) !== -1) {
18
+ resolve(ev.target.response);
19
+ }
20
+ else {
21
+ var err = new Error("The file does not support get requests, responseStatus ".concat(responseStatus, ", '").concat(file, "'."));
22
+ console.error(err);
23
+ reject(err);
24
+ }
25
+ })
26
+ .catch(function (err) {
27
+ console.error(new Error("Failed to request file. ".concat(err)));
28
+ reject(err);
29
+ });
30
+ }
31
+ });
32
+ }
33
+
34
+ module.exports = getFileBlob;
package/lib/index.js CHANGED
@@ -48,6 +48,7 @@ var ajax = require('./ajax.js');
48
48
  var calculateCursorPosition = require('./calculateCursorPosition.js');
49
49
  var compressImage = require('./compressImage.js');
50
50
  var download = require('./download.js');
51
+ var getFileBlob = require('./getFileBlob.js');
51
52
  var getImageInfo = require('./getImageInfo.js');
52
53
  var loadImage = require('./loadImage.js');
53
54
  var loadImageWithBlob = require('./loadImageWithBlob.js');
@@ -65,7 +66,7 @@ var findTreeSelect = require('./findTreeSelect.js');
65
66
  var config = require('./utils/config.js');
66
67
  var VERSION = require('./VERSION.js');
67
68
 
68
- exports.version = "4.20.8";
69
+ exports.version = "4.21.1";
69
70
 
70
71
  exports.isMobile = isMobile;
71
72
  exports.isTelephone = isTelephone;
@@ -115,6 +116,7 @@ exports.ajax = ajax;
115
116
  exports.calculateCursorPosition = calculateCursorPosition;
116
117
  exports.compressImage = compressImage;
117
118
  exports.download = download;
119
+ exports.getFileBlob = getFileBlob;
118
120
  exports.getImageInfo = getImageInfo;
119
121
  exports.loadImage = loadImage;
120
122
  exports.loadImageWithBlob = loadImageWithBlob;
package/lib/loadImage.js CHANGED
@@ -19,9 +19,6 @@ function loadImage(img, useCache) {
19
19
  image_1.crossOrigin = 'anonymous';
20
20
  }
21
21
  image_1.onload = function () {
22
- if (imgIsBlob_1) {
23
- native.revokeObjectURL(url_1);
24
- }
25
22
  if (useCache) {
26
23
  cacheImage = img;
27
24
  cacheResult = image_1;
@@ -1,36 +1,8 @@
1
1
  'use strict';
2
2
 
3
- var tslib = require('tslib');
4
- var ut2 = require('ut2');
5
- var ajax = require('./ajax.js');
6
3
  var native = require('./utils/native.js');
4
+ var getFileBlob = require('./getFileBlob.js');
7
5
 
8
- var SuccessResponseStatus = [200, 304];
9
- function getBlob(img, ajaxOptions) {
10
- return new Promise(function (resolve, reject) {
11
- if (ut2.isBlob(img)) {
12
- resolve(img);
13
- }
14
- else {
15
- ajax(img, tslib.__assign({ responseType: 'blob' }, ajaxOptions))
16
- .then(function (ev) {
17
- var responseStatus = ev.target.status;
18
- if (SuccessResponseStatus.indexOf(responseStatus) !== -1) {
19
- resolve(ev.target.response);
20
- }
21
- else {
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);
25
- }
26
- })
27
- .catch(function (err) {
28
- console.error(new Error("[loadImageWithBlob] Failed to request image. ".concat(err)));
29
- reject(err);
30
- });
31
- }
32
- });
33
- }
34
6
  var cacheImage;
35
7
  var cacheResult;
36
8
  function loadImageWithBlob(img, useCache, ajaxOptions) {
@@ -40,12 +12,11 @@ function loadImageWithBlob(img, useCache, ajaxOptions) {
40
12
  resolve(cacheResult);
41
13
  }
42
14
  else {
43
- getBlob(img, ajaxOptions)
15
+ getFileBlob(img, ajaxOptions)
44
16
  .then(function (blob) {
45
17
  var url = native.createObjectURL(blob);
46
18
  var image = new Image();
47
19
  image.onload = function () {
48
- native.revokeObjectURL(url);
49
20
  var result = { blob: blob, image: image };
50
21
  if (useCache) {
51
22
  cacheImage = img;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "util-helpers",
3
- "version": "4.20.8",
3
+ "version": "4.21.1",
4
4
  "description": "一个基于业务场景的工具方法库",
5
5
  "main": "lib/index.js",
6
6
  "module": "esm/index.js",
@@ -92,5 +92,8 @@
92
92
  "dependencies": {
93
93
  "tslib": "^2.6.2",
94
94
  "ut2": "^1.5.2"
95
+ },
96
+ "publishConfig": {
97
+ "registry": "https://registry.npmjs.org/"
95
98
  }
96
99
  }
@@ -0,0 +1,22 @@
1
+ import ajax from './ajax';
2
+ /**
3
+ * 获取文件 Blob 。
4
+ *
5
+ * @static
6
+ * @alias module:Other.getFileBlob
7
+ * @since 4.21.0
8
+ * @param {string | Blob} file 文件地址或对象。
9
+ * @param {AjaxOptions} [ajaxOptions] ajax 请求配置项,当传入的图片为字符串时才会触发请求。
10
+ * @returns {Promise<Blob>} 文件 Blob 。
11
+ * @example
12
+ *
13
+ * getFileBlob(file).then((blob)=>{
14
+ * // do something
15
+ * });
16
+ *
17
+ * getFileBlob('https://dummyimage.com/200x300').then((blob)=>{
18
+ * // do something
19
+ * });
20
+ */
21
+ declare function getFileBlob(file: string | Blob, ajaxOptions?: Parameters<typeof ajax>[1]): Promise<Blob>;
22
+ export default getFileBlob;
@@ -4,6 +4,8 @@ import loadImageWithBlob from './loadImageWithBlob';
4
4
  *
5
5
  * <em style="font-weight: bold;">注意:该方法仅适用于浏览器端。</em>
6
6
  *
7
+ * <em style="font-weight: bold;">如果不需要该图片,建议手动调用 `URL.revokeObjectURL(image.src)` 释放缓存。</em>
8
+ *
7
9
  * @static
8
10
  * @alias module:Other.getImageInfo
9
11
  * @since 4.20.0
package/types/index.d.ts CHANGED
@@ -103,6 +103,7 @@ export { default as ajax } from './ajax';
103
103
  export { default as calculateCursorPosition } from './calculateCursorPosition';
104
104
  export { default as compressImage } from './compressImage';
105
105
  export { default as download } from './download';
106
+ export { default as getFileBlob } from './getFileBlob';
106
107
  export { default as getImageInfo } from './getImageInfo';
107
108
  export { default as loadImage } from './loadImage';
108
109
  export { default as loadImageWithBlob } from './loadImageWithBlob';
@@ -3,6 +3,8 @@
3
3
  *
4
4
  * <em style="font-weight: bold;">注意:该方法仅适用于浏览器端。</em>
5
5
  *
6
+ * <em style="font-weight: bold;">如果不需要该图片,建议手动调用 `URL.revokeObjectURL(image.src)` 释放缓存。</em>
7
+ *
6
8
  * @static
7
9
  * @alias module:Other.loadImage
8
10
  * @since 4.20.0
@@ -1,4 +1,4 @@
1
- import ajax from './ajax';
1
+ import getFileBlob from './getFileBlob';
2
2
  /**
3
3
  * @typedef {Object} ImageWithBlob HTML图片元素和 blob 对象
4
4
  * @property {HTMLImageElement} image HTML图片元素
@@ -9,6 +9,8 @@ import ajax from './ajax';
9
9
  *
10
10
  * <em style="font-weight: bold;">注意:该方法仅适用于浏览器端。</em>
11
11
  *
12
+ * <em style="font-weight: bold;">如果不需要该图片,建议手动调用 `URL.revokeObjectURL(image.src)` 释放缓存。</em>
13
+ *
12
14
  * @method
13
15
  * @alias module:Other.loadImageWithBlob
14
16
  * @since 4.20.0
@@ -31,7 +33,7 @@ import ajax from './ajax';
31
33
  * });
32
34
  *
33
35
  */
34
- declare function loadImageWithBlob(img: string | Blob, useCache?: boolean, ajaxOptions?: Parameters<typeof ajax>[1]): Promise<{
36
+ declare function loadImageWithBlob(img: string | Blob, useCache?: boolean, ajaxOptions?: Parameters<typeof getFileBlob>[1]): Promise<{
35
37
  image: HTMLImageElement;
36
38
  blob: Blob;
37
39
  }>;