sculp-js 1.10.0 → 1.10.2

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 (63) hide show
  1. package/lib/cjs/array.js +18 -1
  2. package/lib/cjs/async.js +1 -1
  3. package/lib/cjs/base64.js +1 -1
  4. package/lib/cjs/clipboard.js +1 -1
  5. package/lib/cjs/cloneDeep.js +1 -1
  6. package/lib/cjs/cookie.js +1 -1
  7. package/lib/cjs/date.js +1 -1
  8. package/lib/cjs/dom.js +1 -1
  9. package/lib/cjs/download.js +1 -1
  10. package/lib/cjs/easing.js +1 -1
  11. package/lib/cjs/file.js +101 -63
  12. package/lib/cjs/func.js +1 -1
  13. package/lib/cjs/index.js +1 -1
  14. package/lib/cjs/isEqual.js +1 -1
  15. package/lib/cjs/math.js +1 -1
  16. package/lib/cjs/number.js +1 -1
  17. package/lib/cjs/object.js +1 -1
  18. package/lib/cjs/path.js +1 -1
  19. package/lib/cjs/qs.js +1 -1
  20. package/lib/cjs/random.js +1 -1
  21. package/lib/cjs/string.js +1 -1
  22. package/lib/cjs/tooltip.js +8 -5
  23. package/lib/cjs/tree.js +1 -1
  24. package/lib/cjs/type.js +1 -1
  25. package/lib/cjs/unique.js +1 -1
  26. package/lib/cjs/url.js +1 -1
  27. package/lib/cjs/validator.js +1 -1
  28. package/lib/cjs/variable.js +1 -1
  29. package/lib/cjs/watermark.js +1 -1
  30. package/lib/cjs/we-decode.js +1 -1
  31. package/lib/es/array.js +18 -1
  32. package/lib/es/async.js +1 -1
  33. package/lib/es/base64.js +1 -1
  34. package/lib/es/clipboard.js +1 -1
  35. package/lib/es/cloneDeep.js +1 -1
  36. package/lib/es/cookie.js +1 -1
  37. package/lib/es/date.js +1 -1
  38. package/lib/es/dom.js +1 -1
  39. package/lib/es/download.js +1 -1
  40. package/lib/es/easing.js +1 -1
  41. package/lib/es/file.js +101 -63
  42. package/lib/es/func.js +1 -1
  43. package/lib/es/index.js +1 -1
  44. package/lib/es/isEqual.js +1 -1
  45. package/lib/es/math.js +1 -1
  46. package/lib/es/number.js +1 -1
  47. package/lib/es/object.js +1 -1
  48. package/lib/es/path.js +1 -1
  49. package/lib/es/qs.js +1 -1
  50. package/lib/es/random.js +1 -1
  51. package/lib/es/string.js +1 -1
  52. package/lib/es/tooltip.js +8 -5
  53. package/lib/es/tree.js +1 -1
  54. package/lib/es/type.js +1 -1
  55. package/lib/es/unique.js +1 -1
  56. package/lib/es/url.js +1 -1
  57. package/lib/es/validator.js +1 -1
  58. package/lib/es/variable.js +1 -1
  59. package/lib/es/watermark.js +1 -1
  60. package/lib/es/we-decode.js +1 -1
  61. package/lib/index.d.ts +28 -7
  62. package/lib/umd/index.js +221 -163
  63. package/package.json +1 -1
package/lib/es/file.js CHANGED
@@ -1,10 +1,10 @@
1
1
  /*!
2
- * sculp-js v1.10.0
2
+ * sculp-js v1.10.2
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
7
- import { weAtob } from './we-decode.js';
7
+ import { isObject } from './type.js';
8
8
 
9
9
  /**
10
10
  * 判断是否支持canvas
@@ -34,38 +34,111 @@ function chooseLocalFile(accept, changeCb) {
34
34
  };
35
35
  return inputObj;
36
36
  }
37
+ /**
38
+ * 计算图片压缩后的尺寸
39
+ *
40
+ * @param {number} maxWidth
41
+ * @param {number} maxHeight
42
+ * @param {number} originWidth
43
+ * @param {number} originHeight
44
+ * @returns {*}
45
+ */
46
+ function calculateSize({ maxWidth, maxHeight, originWidth, originHeight }) {
47
+ let width = originWidth, height = originHeight;
48
+ // 图片尺寸超过限制
49
+ if (originWidth > maxWidth || originHeight > maxHeight) {
50
+ if (originWidth / originHeight > maxWidth / maxHeight) {
51
+ // 更宽,按照宽度限定尺寸
52
+ width = maxWidth;
53
+ height = Math.round(maxWidth * (originHeight / originWidth));
54
+ }
55
+ else {
56
+ height = maxHeight;
57
+ width = Math.round(maxHeight * (originWidth / originHeight));
58
+ }
59
+ }
60
+ return { width, height };
61
+ }
62
+ /**
63
+ * 根据原始图片的不同尺寸计算等比例缩放后的宽高尺寸
64
+ *
65
+ * @param {number} sizeKB
66
+ * @param {number} originWidth
67
+ * @param {number} originHeight
68
+ * @returns {*}
69
+ */
70
+ function scalingByAspectRatio({ sizeKB, originWidth, originHeight }) {
71
+ let targetWidth = originWidth, targetHeight = originHeight;
72
+ if (sizeKB <= 500) {
73
+ // [50KB, 500KB]
74
+ const maxWidth = 1200, maxHeight = 1200;
75
+ const { width, height } = calculateSize({ maxWidth, maxHeight, originWidth, originHeight });
76
+ targetWidth = width;
77
+ targetHeight = height;
78
+ }
79
+ else if (sizeKB < 10 * 1024) {
80
+ // (500KB, 10MB)
81
+ const maxWidth = 1600, maxHeight = 1600;
82
+ const { width, height } = calculateSize({ maxWidth, maxHeight, originWidth, originHeight });
83
+ targetWidth = width;
84
+ targetHeight = height;
85
+ }
86
+ else if (10 * 1024 <= sizeKB) {
87
+ // [10MB, Infinity)
88
+ const maxWidth = originWidth > 15000 ? 8192 : originWidth > 10000 ? 4096 : 2048, maxHeight = originHeight > 15000 ? 8192 : originHeight > 10000 ? 4096 : 2048;
89
+ const { width, height } = calculateSize({ maxWidth, maxHeight, originWidth, originHeight });
90
+ targetWidth = width;
91
+ targetHeight = height;
92
+ }
93
+ return { width: targetWidth, height: targetHeight };
94
+ }
37
95
  /**
38
96
  * Web端:等比例压缩图片批量处理 (size小于200KB,不压缩)
39
- * @param {File | FileList} file 文件
40
- * @param {ICompressOptions} options
97
+ *
98
+ * @param {File | FileList} file 图片或图片数组
99
+ * @param {ICompressOptions} options 压缩图片配置项,default: {mime:'image/jpeg'}
41
100
  * @returns {Promise<object> | undefined}
42
101
  */
43
- function compressImg(file, options) {
44
- console.assert(file instanceof File || file instanceof FileList, `${file} 必须是File或FileList类型`);
45
- console.assert(supportCanvas(), `当前环境不支持 Canvas`);
46
- let targetQuality = 0.52;
47
- if (file instanceof File) {
102
+ function compressImg(file, options = { mime: 'image/jpeg' }) {
103
+ if (!(file instanceof File || file instanceof FileList)) {
104
+ throw new Error(`${file} require be File or FileList`);
105
+ }
106
+ else if (!supportCanvas()) {
107
+ throw new Error(`Current runtime environment not support Canvas`);
108
+ }
109
+ const { quality, mime = 'image/jpeg' } = isObject(options) ? options : {};
110
+ let targetQuality = quality;
111
+ if (quality) {
112
+ targetQuality = quality;
113
+ }
114
+ else if (file instanceof File) {
48
115
  const sizeKB = +parseInt((file.size / 1024).toFixed(2));
49
- if (sizeKB < 1 * 1024) {
116
+ if (sizeKB < 1 * 50) {
117
+ targetQuality = 1;
118
+ }
119
+ else if (sizeKB < 1 * 1024) {
50
120
  targetQuality = 0.85;
51
121
  }
52
- else if (sizeKB >= 1 * 1024 && sizeKB < 5 * 1024) {
53
- targetQuality = 0.62;
122
+ else if (sizeKB < 5 * 1024) {
123
+ targetQuality = 0.8;
54
124
  }
55
- else if (sizeKB >= 5 * 1024) {
56
- targetQuality = 0.52;
125
+ else {
126
+ targetQuality = 0.75;
57
127
  }
58
128
  }
59
- if (options.quality) {
60
- targetQuality = options.quality;
61
- }
62
129
  if (file instanceof FileList) {
63
- return Promise.all(Array.from(file).map(el => compressImg(el, { mime: options.mime, quality: targetQuality }))); // 如果是 file 数组返回 Promise 数组
130
+ return Promise.all(Array.from(file).map(el => compressImg(el, { mime: mime, quality: targetQuality }))); // 如果是 file 数组返回 Promise 数组
64
131
  }
65
132
  else if (file instanceof File) {
66
133
  return new Promise(resolve => {
134
+ const ext = {
135
+ 'image/webp': 'webp',
136
+ 'image/jpeg': 'jpg',
137
+ 'image/png': 'png'
138
+ };
139
+ const fileName = [...file.name.split('.').slice(0, -1), ext[mime]].join('.');
67
140
  const sizeKB = +parseInt((file.size / 1024).toFixed(2));
68
- if (+(file.size / 1024).toFixed(2) < 200) {
141
+ if (+(file.size / 1024).toFixed(2) < 50) {
69
142
  resolve({
70
143
  file: file
71
144
  });
@@ -78,57 +151,22 @@ function compressImg(file, options) {
78
151
  image.onload = () => {
79
152
  const canvas = document.createElement('canvas'); // 创建 canvas 元素
80
153
  const context = canvas.getContext('2d');
81
- let targetWidth = image.width;
82
- let targetHeight = image.height;
83
154
  const originWidth = image.width;
84
155
  const originHeight = image.height;
85
- if (1 * 1024 <= sizeKB && sizeKB < 10 * 1024) {
86
- const maxWidth = 1600, maxHeight = 1600;
87
- targetWidth = originWidth;
88
- targetHeight = originHeight;
89
- // 图片尺寸超过的限制
90
- if (originWidth > maxWidth || originHeight > maxHeight) {
91
- if (originWidth / originHeight > maxWidth / maxHeight) {
92
- // 更宽,按照宽度限定尺寸
93
- targetWidth = maxWidth;
94
- targetHeight = Math.round(maxWidth * (originHeight / originWidth));
95
- }
96
- else {
97
- targetHeight = maxHeight;
98
- targetWidth = Math.round(maxHeight * (originWidth / originHeight));
99
- }
100
- }
101
- }
102
- if (10 * 1024 <= sizeKB && sizeKB <= 20 * 1024) {
103
- const maxWidth = 1400, maxHeight = 1400;
104
- targetWidth = originWidth;
105
- targetHeight = originHeight;
106
- // 图片尺寸超过的限制
107
- if (originWidth > maxWidth || originHeight > maxHeight) {
108
- if (originWidth / originHeight > maxWidth / maxHeight) {
109
- // 更宽,按照宽度限定尺寸
110
- targetWidth = maxWidth;
111
- targetHeight = Math.round(maxWidth * (originHeight / originWidth));
112
- }
113
- else {
114
- targetHeight = maxHeight;
115
- targetWidth = Math.round(maxHeight * (originWidth / originHeight));
116
- }
117
- }
118
- }
119
- canvas.width = targetWidth;
120
- canvas.height = targetHeight;
121
- context.clearRect(0, 0, targetWidth, targetHeight);
122
- context.drawImage(image, 0, 0, targetWidth, targetHeight); // 绘制 canvas
123
- const canvasURL = canvas.toDataURL(options.mime, targetQuality);
124
- const buffer = weAtob(canvasURL.split(',')[1]);
156
+ const { width, height } = scalingByAspectRatio({ sizeKB, originWidth, originHeight });
157
+ canvas.width = width;
158
+ canvas.height = height;
159
+ context.clearRect(0, 0, width, height);
160
+ context.drawImage(image, 0, 0, width, height); // 绘制 canvas
161
+ const canvasURL = canvas.toDataURL(mime, targetQuality);
162
+ const buffer = atob(canvasURL.split(',')[1]);
125
163
  let length = buffer.length;
126
164
  const bufferArray = new Uint8Array(new ArrayBuffer(length));
127
165
  while (length--) {
128
166
  bufferArray[length] = buffer.charCodeAt(length);
129
167
  }
130
- const miniFile = new File([bufferArray], file.name, {
131
- type: options.mime
168
+ const miniFile = new File([bufferArray], fileName, {
169
+ type: mime
132
170
  });
133
171
  resolve({
134
172
  file: miniFile,
package/lib/es/func.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.0
2
+ * sculp-js v1.10.2
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.0
2
+ * sculp-js v1.10.2
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/isEqual.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.0
2
+ * sculp-js v1.10.2
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/math.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.0
2
+ * sculp-js v1.10.2
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/number.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.0
2
+ * sculp-js v1.10.2
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/object.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.0
2
+ * sculp-js v1.10.2
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/path.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.0
2
+ * sculp-js v1.10.2
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/qs.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.0
2
+ * sculp-js v1.10.2
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/random.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.0
2
+ * sculp-js v1.10.2
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/string.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.0
2
+ * sculp-js v1.10.2
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/tooltip.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.0
2
+ * sculp-js v1.10.2
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -8,16 +8,19 @@ import { getStrWidthPx } from './dom.js';
8
8
  import { isString } from './type.js';
9
9
 
10
10
  /**
11
- * @title tooltip
11
+ * 自定义的 tooltip, 支持鼠标移动动悬浮提示
12
12
  * @Desc 自定义的tooltip方法, 支持拖动悬浮提示
13
13
  * Created by chendeqiao on 2017/5/8.
14
14
  * @example
15
- * <span onmouseleave="handleMouseLeave('#root')" onmousemove="handleMouseEnter({rootContainer: '#root', title: 'title content', event: event})"
16
- * onmouseenter="handleMouseEnter({rootContainer:'#root', title: 'title content', event: event})">title content </span>
15
+ * <span onmouseleave="handleMouseLeave('#root')"
16
+ * onmousemove="handleMouseEnter({rootContainer: '#root', title: 'title content', event: event})"
17
+ * onmouseenter="handleMouseEnter({rootContainer:'#root', title: 'title content', event: event})">
18
+ * title content
19
+ * </span>
17
20
  */
18
21
  /**
19
22
  * 自定义title提示功能的mouseenter事件句柄
20
- * @param {ITooltipParams} param1
23
+ * @param {ITooltipParams} param
21
24
  * @returns {*}
22
25
  */
23
26
  function handleMouseEnter({ rootContainer = '#root', title, event, bgColor = '#000', color = '#fff' }) {
package/lib/es/tree.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.0
2
+ * sculp-js v1.10.2
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/type.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.0
2
+ * sculp-js v1.10.2
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/unique.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.0
2
+ * sculp-js v1.10.2
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/url.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.0
2
+ * sculp-js v1.10.2
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.0
2
+ * sculp-js v1.10.2
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.0
2
+ * sculp-js v1.10.2
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.0
2
+ * sculp-js v1.10.2
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.10.0
2
+ * sculp-js v1.10.2
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/index.d.ts CHANGED
@@ -106,6 +106,23 @@ declare function arrayEach<V>(array: ArrayLike<V>, iterator: (val: V, idx: numbe
106
106
  * @param {ArrayLike<V>} array 数组
107
107
  * @param {(val: V, idx: number) => Promise<any>} iterator 支持Promise类型的回调函数
108
108
  * @param {boolean} reverse 是否反向遍历
109
+ * @example
110
+ * 使用范例如下:
111
+ * const start = async () => {
112
+ * await arrayEachAsync(result, async (item) => {
113
+ * await request(item);
114
+ * count++;
115
+ * })
116
+ * console.log('发送次数', count);
117
+ * }
118
+
119
+ * for await...of 使用范例如下
120
+ * const loadImages = async (images) => {
121
+ * for await (const item of images) {
122
+ * await request(item);
123
+ * count++;
124
+ * }
125
+ * }
109
126
  */
110
127
  declare function arrayEachAsync<V>(array: ArrayLike<V>, iterator: (val: V, idx: number) => Promise<any> | any, reverse?: boolean): Promise<void>;
111
128
  /**
@@ -589,11 +606,12 @@ interface ICompressOptions {
589
606
  }
590
607
  /**
591
608
  * Web端:等比例压缩图片批量处理 (size小于200KB,不压缩)
592
- * @param {File | FileList} file 文件
593
- * @param {ICompressOptions} options
609
+ *
610
+ * @param {File | FileList} file 图片或图片数组
611
+ * @param {ICompressOptions} options 压缩图片配置项,default: {mime:'image/jpeg'}
594
612
  * @returns {Promise<object> | undefined}
595
613
  */
596
- declare function compressImg(file: File | FileList, options: ICompressOptions): Promise<object> | undefined;
614
+ declare function compressImg(file: File | FileList, options?: ICompressOptions): Promise<object> | undefined;
597
615
 
598
616
  interface ICanvasWM {
599
617
  rootContainer?: HTMLElement | string;
@@ -760,12 +778,15 @@ interface UniqueString {
760
778
  declare const uniqueString: UniqueString;
761
779
 
762
780
  /**
763
- * @title tooltip
781
+ * 自定义的 tooltip, 支持鼠标移动动悬浮提示
764
782
  * @Desc 自定义的tooltip方法, 支持拖动悬浮提示
765
783
  * Created by chendeqiao on 2017/5/8.
766
784
  * @example
767
- * <span onmouseleave="handleMouseLeave('#root')" onmousemove="handleMouseEnter({rootContainer: '#root', title: 'title content', event: event})"
768
- * onmouseenter="handleMouseEnter({rootContainer:'#root', title: 'title content', event: event})">title content </span>
785
+ * <span onmouseleave="handleMouseLeave('#root')"
786
+ * onmousemove="handleMouseEnter({rootContainer: '#root', title: 'title content', event: event})"
787
+ * onmouseenter="handleMouseEnter({rootContainer:'#root', title: 'title content', event: event})">
788
+ * title content
789
+ * </span>
769
790
  */
770
791
  interface ITooltipParams {
771
792
  rootContainer: HTMLElement | string;
@@ -776,7 +797,7 @@ interface ITooltipParams {
776
797
  }
777
798
  /**
778
799
  * 自定义title提示功能的mouseenter事件句柄
779
- * @param {ITooltipParams} param1
800
+ * @param {ITooltipParams} param
780
801
  * @returns {*}
781
802
  */
782
803
  declare function handleMouseEnter({ rootContainer, title, event, bgColor, color }: ITooltipParams): void;