sculp-js 1.9.0 → 1.10.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.
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 +11 -7
  9. package/lib/cjs/download.js +1 -1
  10. package/lib/cjs/easing.js +1 -1
  11. package/lib/cjs/file.js +76 -50
  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 +10 -4
  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 +25 -18
  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 +20 -19
  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 +11 -7
  39. package/lib/es/download.js +1 -1
  40. package/lib/es/easing.js +1 -1
  41. package/lib/es/file.js +76 -50
  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 +10 -4
  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 +25 -18
  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 +20 -19
  60. package/lib/es/we-decode.js +1 -1
  61. package/lib/index.d.ts +50 -26
  62. package/lib/umd/index.js +151 -94
  63. package/package.json +2 -1
package/lib/cjs/array.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -41,6 +41,23 @@ function arrayEach(array, iterator, reverse = false) {
41
41
  * @param {ArrayLike<V>} array 数组
42
42
  * @param {(val: V, idx: number) => Promise<any>} iterator 支持Promise类型的回调函数
43
43
  * @param {boolean} reverse 是否反向遍历
44
+ * @example
45
+ * 使用范例如下:
46
+ * const start = async () => {
47
+ * await arrayEachAsync(result, async (item) => {
48
+ * await request(item);
49
+ * count++;
50
+ * })
51
+ * console.log('发送次数', count);
52
+ * }
53
+
54
+ * for await...of 使用范例如下
55
+ * const loadImages = async (images) => {
56
+ * for await (const item of images) {
57
+ * await request(item);
58
+ * count++;
59
+ * }
60
+ * }
44
61
  */
45
62
  async function arrayEachAsync(array, iterator, reverse = false) {
46
63
  if (reverse) {
package/lib/cjs/async.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/base64.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
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.9.0
2
+ * sculp-js v1.10.1
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.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/cookie.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/date.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/dom.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -133,28 +133,32 @@ function getComputedCssVal(el, property, reNumber = true) {
133
133
  * 字符串的像素宽度
134
134
  * @param {string} str 目标字符串
135
135
  * @param {number} fontSize 字符串字体大小
136
- * @param {boolean} isRemoveDom 计算后是否移除中间dom元素
136
+ * @param {boolean} isRemove 计算后是否移除创建的dom元素
137
137
  * @returns {*}
138
138
  */
139
- function getStrWidthPx(str, fontSize = 14, isRemoveDom = false) {
139
+ function getStrWidthPx(str, fontSize = 14, isRemove = true) {
140
140
  let strWidth = 0;
141
141
  console.assert(type.isString(str), `${str} 不是有效的字符串`);
142
142
  if (type.isString(str) && str.length > 0) {
143
- let getEle = document.querySelector('#getStrWidth1494304949567');
143
+ const id = 'getStrWidth1494304949567';
144
+ let getEle = document.querySelector(`#${id}`);
144
145
  if (!getEle) {
145
146
  const _ele = document.createElement('span');
146
- _ele.id = 'getStrWidth1494304949567';
147
+ _ele.id = id;
147
148
  _ele.style.fontSize = fontSize + 'px';
148
149
  _ele.style.whiteSpace = 'nowrap';
149
150
  _ele.style.visibility = 'hidden';
151
+ _ele.style.position = 'absolute';
152
+ _ele.style.top = '-9999px';
153
+ _ele.style.left = '-9999px';
150
154
  _ele.textContent = str;
151
155
  document.body.appendChild(_ele);
152
156
  getEle = _ele;
153
157
  }
154
158
  getEle.textContent = str;
155
159
  strWidth = getEle.offsetWidth;
156
- if (isRemoveDom) {
157
- document.body.appendChild(getEle);
160
+ if (isRemove) {
161
+ getEle.remove();
158
162
  }
159
163
  }
160
164
  return strWidth;
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/easing.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/file.js CHANGED
@@ -1,11 +1,12 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
7
7
  'use strict';
8
8
 
9
+ var type = require('./type.js');
9
10
  var weDecode = require('./we-decode.js');
10
11
 
11
12
  /**
@@ -36,16 +37,73 @@ function chooseLocalFile(accept, changeCb) {
36
37
  };
37
38
  return inputObj;
38
39
  }
40
+ /**
41
+ * 计算图片压缩后的尺寸
42
+ *
43
+ * @param {number} maxWidth
44
+ * @param {number} maxHeight
45
+ * @param {number} originWidth
46
+ * @param {number} originHeight
47
+ * @returns {*}
48
+ */
49
+ function calculateSize({ maxWidth, maxHeight, originWidth, originHeight }) {
50
+ let width = originWidth, height = originHeight;
51
+ // 图片尺寸超过限制
52
+ if (originWidth > maxWidth || originHeight > maxHeight) {
53
+ if (originWidth / originHeight > maxWidth / maxHeight) {
54
+ // 更宽,按照宽度限定尺寸
55
+ width = maxWidth;
56
+ height = Math.round(maxWidth * (originHeight / originWidth));
57
+ }
58
+ else {
59
+ height = maxHeight;
60
+ width = Math.round(maxHeight * (originWidth / originHeight));
61
+ }
62
+ }
63
+ return { width, height };
64
+ }
65
+ /**
66
+ * 根据原始图片的不同尺寸计算等比例缩放后的宽高尺寸
67
+ *
68
+ * @param {number} sizeKB
69
+ * @param {number} originWidth
70
+ * @param {number} originHeight
71
+ * @returns {*}
72
+ */
73
+ function scalingByAspectRatio({ sizeKB, originWidth, originHeight }) {
74
+ let targetWidth = originWidth, targetHeight = originHeight;
75
+ if (1 * 1024 <= sizeKB && sizeKB < 10 * 1024) {
76
+ // [1MB, 10MB)
77
+ const maxWidth = 1600, maxHeight = 1600;
78
+ const { width, height } = calculateSize({ maxWidth, maxHeight, originWidth, originHeight });
79
+ targetWidth = width;
80
+ targetHeight = height;
81
+ }
82
+ else if (10 * 1024 <= sizeKB) {
83
+ // [10MB, Infinity)
84
+ const maxWidth = originWidth > 15000 ? 8192 : originWidth > 10000 ? 4096 : 2000, maxHeight = originHeight > 15000 ? 8192 : originHeight > 10000 ? 4096 : 2000;
85
+ const { width, height } = calculateSize({ maxWidth, maxHeight, originWidth, originHeight });
86
+ targetWidth = width;
87
+ targetHeight = height;
88
+ }
89
+ return { width: targetWidth, height: targetHeight };
90
+ }
39
91
  /**
40
92
  * Web端:等比例压缩图片批量处理 (size小于200KB,不压缩)
41
- * @param {File | FileList} file 文件
42
- * @param {ICompressOptions} options
93
+ *
94
+ * @param {File | FileList} file 图片或图片数组
95
+ * @param {ICompressOptions} options 压缩图片配置项,default: {quality:0.52,mime:'image/jpeg'}
43
96
  * @returns {Promise<object> | undefined}
44
97
  */
45
- function compressImg(file, options) {
46
- console.assert(file instanceof File || file instanceof FileList, `${file} 必须是File或FileList类型`);
47
- console.assert(supportCanvas(), `当前环境不支持 Canvas`);
48
- let targetQuality = 0.52;
98
+ function compressImg(file, options = { quality: 0.52, mime: 'image/jpeg' }) {
99
+ if (!(file instanceof File || file instanceof FileList)) {
100
+ throw new Error(`${file} require be File or FileList`);
101
+ }
102
+ else if (!supportCanvas()) {
103
+ throw new Error(`Current runtime environment not support Canvas`);
104
+ }
105
+ const { quality = 0.52, mime = 'image/jpeg' } = type.isObject(options) ? options : {};
106
+ let targetQuality = quality;
49
107
  if (file instanceof File) {
50
108
  const sizeKB = +parseInt((file.size / 1024).toFixed(2));
51
109
  if (sizeKB < 1 * 1024) {
@@ -54,8 +112,11 @@ function compressImg(file, options) {
54
112
  else if (sizeKB >= 1 * 1024 && sizeKB < 5 * 1024) {
55
113
  targetQuality = 0.62;
56
114
  }
57
- else if (sizeKB >= 5 * 1024) {
58
- targetQuality = 0.52;
115
+ else if (sizeKB >= 5 * 1024 && sizeKB < 10 * 1024) {
116
+ targetQuality = 0.75;
117
+ }
118
+ else if (sizeKB >= 10 * 1024) {
119
+ targetQuality = 0.92;
59
120
  }
60
121
  }
61
122
  if (options.quality) {
@@ -80,49 +141,14 @@ function compressImg(file, options) {
80
141
  image.onload = () => {
81
142
  const canvas = document.createElement('canvas'); // 创建 canvas 元素
82
143
  const context = canvas.getContext('2d');
83
- let targetWidth = image.width;
84
- let targetHeight = image.height;
85
144
  const originWidth = image.width;
86
145
  const originHeight = image.height;
87
- if (1 * 1024 <= sizeKB && sizeKB < 10 * 1024) {
88
- const maxWidth = 1600, maxHeight = 1600;
89
- targetWidth = originWidth;
90
- targetHeight = originHeight;
91
- // 图片尺寸超过的限制
92
- if (originWidth > maxWidth || originHeight > maxHeight) {
93
- if (originWidth / originHeight > maxWidth / maxHeight) {
94
- // 更宽,按照宽度限定尺寸
95
- targetWidth = maxWidth;
96
- targetHeight = Math.round(maxWidth * (originHeight / originWidth));
97
- }
98
- else {
99
- targetHeight = maxHeight;
100
- targetWidth = Math.round(maxHeight * (originWidth / originHeight));
101
- }
102
- }
103
- }
104
- if (10 * 1024 <= sizeKB && sizeKB <= 20 * 1024) {
105
- const maxWidth = 1400, maxHeight = 1400;
106
- targetWidth = originWidth;
107
- targetHeight = originHeight;
108
- // 图片尺寸超过的限制
109
- if (originWidth > maxWidth || originHeight > maxHeight) {
110
- if (originWidth / originHeight > maxWidth / maxHeight) {
111
- // 更宽,按照宽度限定尺寸
112
- targetWidth = maxWidth;
113
- targetHeight = Math.round(maxWidth * (originHeight / originWidth));
114
- }
115
- else {
116
- targetHeight = maxHeight;
117
- targetWidth = Math.round(maxHeight * (originWidth / originHeight));
118
- }
119
- }
120
- }
121
- canvas.width = targetWidth;
122
- canvas.height = targetHeight;
123
- context.clearRect(0, 0, targetWidth, targetHeight);
124
- context.drawImage(image, 0, 0, targetWidth, targetHeight); // 绘制 canvas
125
- const canvasURL = canvas.toDataURL(options.mime, targetQuality);
146
+ const { width, height } = scalingByAspectRatio({ sizeKB, originWidth, originHeight });
147
+ canvas.width = width;
148
+ canvas.height = height;
149
+ context.clearRect(0, 0, width, height);
150
+ context.drawImage(image, 0, 0, width, height); // 绘制 canvas
151
+ const canvasURL = canvas.toDataURL(mime, targetQuality);
126
152
  const buffer = weDecode.weAtob(canvasURL.split(',')[1]);
127
153
  let length = buffer.length;
128
154
  const bufferArray = new Uint8Array(new ArrayBuffer(length));
package/lib/cjs/func.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
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.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/math.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/number.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -78,11 +78,17 @@ const numberAbbr = (num, units, options = { ratio: 1000, decimals: 0, separator:
78
78
  * ['Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
79
79
  * @returns
80
80
  */
81
- function humanFileSize(num, options) {
82
- const { decimals = 0, si = false, separator = ' ', maxUnit } = options;
83
- const units = si
81
+ function humanFileSize(num, options = { decimals: 0, si: false, separator: ' ' }) {
82
+ const { decimals = 0, si = false, separator = ' ', baseUnit, maxUnit } = options;
83
+ let units = si
84
84
  ? ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
85
85
  : ['Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
86
+ if (!type.isNullOrUnDef(baseUnit)) {
87
+ const targetIndex = units.findIndex(el => el === baseUnit);
88
+ if (targetIndex !== -1) {
89
+ units = units.slice(targetIndex);
90
+ }
91
+ }
86
92
  if (!type.isNullOrUnDef(maxUnit)) {
87
93
  const targetIndex = units.findIndex(el => el === maxUnit);
88
94
  if (targetIndex !== -1) {
package/lib/cjs/object.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/path.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/qs.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/random.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/string.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
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.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -7,51 +7,58 @@
7
7
  'use strict';
8
8
 
9
9
  var dom = require('./dom.js');
10
+ var type = require('./type.js');
10
11
 
11
12
  /**
12
- * @title tooltip
13
+ * 自定义的 tooltip, 支持鼠标移动动悬浮提示
13
14
  * @Desc 自定义的tooltip方法, 支持拖动悬浮提示
14
15
  * Created by chendeqiao on 2017/5/8.
15
16
  * @example
16
- * <span onmouseleave="handleMouseLeave('#root')" onmousemove="handleMouseEnter({rootElId: '#root', title: 'title content', event: event})"
17
- * onmouseenter="handleMouseEnter({'#root', title: 'title content', event: event})">title content </span>
17
+ * <span onmouseleave="handleMouseLeave('#root')"
18
+ * onmousemove="handleMouseEnter({rootContainer: '#root', title: 'title content', event: event})"
19
+ * onmouseenter="handleMouseEnter({rootContainer:'#root', title: 'title content', event: event})">
20
+ * title content
21
+ * </span>
18
22
  */
19
23
  /**
20
24
  * 自定义title提示功能的mouseenter事件句柄
21
- * @param {ITooltipParams} param1
25
+ * @param {ITooltipParams} param
22
26
  * @returns {*}
23
27
  */
24
- function handleMouseEnter({ rootElId = '#root', title, event }) {
28
+ function handleMouseEnter({ rootContainer = '#root', title, event, bgColor = '#000', color = '#fff' }) {
25
29
  try {
26
- const $rootEl = document.querySelector(rootElId);
27
- console.assert($rootEl !== null, `未找到id为 ${rootElId} 的dom元素`);
30
+ const $rootEl = type.isString(rootContainer) ? document.querySelector(rootContainer) : rootContainer;
31
+ if (!$rootEl) {
32
+ throw new Error(`${rootContainer} is not valid Html Element or element selector`);
33
+ }
28
34
  let $customTitle = null;
35
+ const styleId = 'style-tooltip-inner1494304949567';
29
36
  // 动态创建class样式,并加入到head中
30
- if (!document.querySelector('.tooltip-inner1494304949567')) {
37
+ if (!document.querySelector(`#${styleId}`)) {
31
38
  const tooltipWrapperClass = document.createElement('style');
32
39
  tooltipWrapperClass.type = 'text/css';
40
+ tooltipWrapperClass.id = styleId;
33
41
  tooltipWrapperClass.innerHTML = `
34
42
  .tooltip-inner1494304949567 {
35
43
  max-width: 250px;
36
44
  padding: 3px 8px;
37
- color: #fff;
45
+ color: ${color};
38
46
  text-decoration: none;
39
47
  border-radius: 4px;
40
48
  text-align: left;
49
+ background-color: ${bgColor};
41
50
  }
42
51
  `;
43
52
  document.querySelector('head').appendChild(tooltipWrapperClass);
44
53
  }
45
- if (document.querySelector('#customTitle1494304949567')) {
46
- $customTitle = document.querySelector('#customTitle1494304949567');
54
+ $customTitle = document.querySelector('#customTitle1494304949567');
55
+ if ($customTitle) {
47
56
  mouseenter($customTitle, title, event);
48
57
  }
49
58
  else {
50
59
  const $contentContainer = document.createElement('div');
51
- $contentContainer.className = 'customTitle';
52
60
  $contentContainer.id = 'customTitle1494304949567';
53
- $contentContainer.className = 'tooltip';
54
- $contentContainer.style.cssText = 'z-index: 99999999; visibility: hidden;';
61
+ $contentContainer.style.cssText = 'z-index: 99999999; visibility: hidden; position: absolute;';
55
62
  $contentContainer.innerHTML =
56
63
  '<div class="tooltip-inner1494304949567" style="word-wrap: break-word; max-width: 44px;">皮肤</div>';
57
64
  $rootEl.appendChild($contentContainer);
@@ -104,11 +111,11 @@ function mouseenter($customTitle, title, e) {
104
111
  }
105
112
  /**
106
113
  * 移除提示文案dom的事件句柄
107
- * @param {string} rootElId
114
+ * @param {string} rootContainer
108
115
  * @returns {*}
109
116
  */
110
- function handleMouseLeave(rootElId = '#root') {
111
- const rootEl = document.querySelector(rootElId), titleEl = document.querySelector('#customTitle1494304949567');
117
+ function handleMouseLeave(rootContainer = '#root') {
118
+ const rootEl = type.isString(rootContainer) ? document.querySelector(rootContainer) : rootContainer, titleEl = document.querySelector('#customTitle1494304949567');
112
119
  if (rootEl && titleEl) {
113
120
  rootEl.removeChild(titleEl);
114
121
  }
package/lib/cjs/tree.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/type.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/unique.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/url.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
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.9.0
2
+ * sculp-js v1.10.1
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.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,11 +1,13 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
6
6
 
7
7
  'use strict';
8
8
 
9
+ var type = require('./type.js');
10
+
9
11
  /*
10
12
  * @created: Saturday, 2020-04-18 14:38:23
11
13
  * @author: chendq
@@ -17,15 +19,14 @@
17
19
  * @param {ICanvasWM} canvasWM
18
20
  * @example genCanvasWM({ content: 'QQMusicFE' })
19
21
  */
20
- function genCanvasWM(canvasWM) {
21
- const { container = document.body, width = '300px', height = '200px', textAlign = 'center', textBaseline = 'middle', font = '20px PingFangSC-Medium,PingFang SC',
22
+ function genCanvasWM(content = '请勿外传', canvasWM) {
23
+ const { rootContainer = document.body, width = '300px', height = '150px', textAlign = 'center', textBaseline = 'middle', font = '20px PingFangSC-Medium,PingFang SC',
22
24
  // fontWeight = 500,
23
- fillStyle = 'rgba(189, 177, 167, .3)', content = '请勿外传', rotate = 30, zIndex = 2147483647 } = canvasWM;
24
- // 仅限主页面添加水印
25
- // if (!location.pathname.includes('/home')) {
26
- // return;
27
- // }
28
- const args = canvasWM;
25
+ fillStyle = 'rgba(189, 177, 167, .3)', rotate = -20, zIndex = 2147483647, watermarkId = '__wm' } = type.isNullOrUnDef(canvasWM) ? {} : canvasWM;
26
+ const container = type.isString(rootContainer) ? document.querySelector(rootContainer) : rootContainer;
27
+ if (!container) {
28
+ throw new Error(`${rootContainer} is not valid Html Element or element selector`);
29
+ }
29
30
  const canvas = document.createElement('canvas');
30
31
  canvas.setAttribute('width', width);
31
32
  canvas.setAttribute('height', height);
@@ -38,36 +39,36 @@ function genCanvasWM(canvasWM) {
38
39
  ctx.rotate((Math.PI / 180) * rotate);
39
40
  ctx.fillText(content, parseFloat(width) / 4, parseFloat(height) / 2);
40
41
  const base64Url = canvas.toDataURL();
41
- const __wm = document.querySelector('.__wm');
42
+ const __wm = document.querySelector(`#${watermarkId}`);
42
43
  const watermarkDiv = __wm || document.createElement('div');
43
44
  const styleStr = `opacity: 1 !important; display: block !important; visibility: visible !important; position:absolute; left:0; top:0; width:100%; height:100%; z-index:${zIndex}; pointer-events:none; background-repeat:repeat; background-image:url('${base64Url}')`;
44
45
  watermarkDiv.setAttribute('style', styleStr);
45
- watermarkDiv.classList.add('__wm');
46
+ watermarkDiv.setAttribute('id', watermarkId);
46
47
  watermarkDiv.classList.add('nav-height');
47
48
  if (!__wm) {
48
49
  container.style.position = 'relative';
49
50
  container.appendChild(watermarkDiv);
50
51
  }
51
52
  const getMutableStyle = (ele) => {
52
- const computedStle = getComputedStyle(ele);
53
+ const computedStyle = getComputedStyle(ele);
53
54
  return {
54
- opacity: computedStle.getPropertyValue('opacity'),
55
- zIndex: computedStle.getPropertyValue('z-index'),
56
- display: computedStle.getPropertyValue('display'),
57
- visibility: computedStle.getPropertyValue('visibility')
55
+ opacity: computedStyle.getPropertyValue('opacity'),
56
+ zIndex: computedStyle.getPropertyValue('z-index'),
57
+ display: computedStyle.getPropertyValue('display'),
58
+ visibility: computedStyle.getPropertyValue('visibility')
58
59
  };
59
60
  };
60
61
  //@ts-ignore
61
62
  const MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
62
63
  if (MutationObserver) {
63
64
  let mo = new MutationObserver(function () {
64
- const __wm = document.querySelector('.__wm'); // 只在__wm元素变动才重新调用 __canvasWM
65
+ const __wm = document.querySelector(`#${watermarkId}`); // 只在__wm元素变动才重新调用 __canvasWM
65
66
  if (!__wm) {
66
67
  // 避免一直触发
67
68
  // console.log('regenerate watermark by delete::')
68
69
  mo.disconnect();
69
70
  mo = null;
70
- genCanvasWM(JSON.parse(JSON.stringify(args)));
71
+ genCanvasWM(content, canvasWM);
71
72
  }
72
73
  else {
73
74
  const { opacity, zIndex, display, visibility } = getMutableStyle(__wm);
@@ -79,7 +80,7 @@ function genCanvasWM(canvasWM) {
79
80
  mo.disconnect();
80
81
  mo = null;
81
82
  container.removeChild(__wm);
82
- genCanvasWM(JSON.parse(JSON.stringify(args)));
83
+ genCanvasWM(content, canvasWM);
83
84
  }
84
85
  }
85
86
  });
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.1
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */