sculp-js 1.8.4 → 1.10.0

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 +1 -1
  2. package/lib/cjs/async.js +1 -1
  3. package/lib/cjs/base64.js +1 -1
  4. package/lib/cjs/clipboard.js +7 -11
  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 +1 -1
  12. package/lib/cjs/func.js +1 -1
  13. package/lib/cjs/index.js +3 -1
  14. package/lib/cjs/isEqual.js +1 -1
  15. package/lib/cjs/math.js +1 -1
  16. package/lib/cjs/number.js +55 -12
  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 +20 -16
  23. package/lib/cjs/tree.js +2 -2
  24. package/lib/cjs/type.js +1 -1
  25. package/lib/cjs/unique.js +1 -1
  26. package/lib/cjs/url.js +15 -5
  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 +1 -1
  32. package/lib/es/async.js +1 -1
  33. package/lib/es/base64.js +1 -1
  34. package/lib/es/clipboard.js +7 -11
  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 +1 -1
  42. package/lib/es/func.js +1 -1
  43. package/lib/es/index.js +2 -2
  44. package/lib/es/isEqual.js +1 -1
  45. package/lib/es/math.js +1 -1
  46. package/lib/es/number.js +54 -13
  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 +20 -16
  53. package/lib/es/tree.js +2 -2
  54. package/lib/es/type.js +1 -1
  55. package/lib/es/unique.js +1 -1
  56. package/lib/es/url.js +15 -5
  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 +55 -29
  62. package/lib/umd/index.js +607 -552
  63. package/package.json +2 -1
package/lib/cjs/array.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.10.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/async.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.10.0
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.8.4
2
+ * sculp-js v1.10.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,32 +1,28 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.10.0
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 dom = require('./dom.js');
10
-
11
- const textEl = document.createElement('textarea');
12
- dom.setStyle(textEl, {
13
- position: 'absolute',
14
- top: '-9999px',
15
- left: '-9999px',
16
- opacity: 0
17
- });
18
- document.body.appendChild(textEl);
19
9
  /**
20
10
  * 复制文本
21
11
  * @param {string} text
22
12
  */
23
13
  function copyText(text) {
14
+ const textEl = document.createElement('textarea');
15
+ textEl.style.position = 'absolute';
16
+ textEl.style.top = '-9999px';
17
+ textEl.style.left = '-9999px';
24
18
  textEl.value = text;
19
+ document.body.appendChild(textEl);
25
20
  textEl.focus({ preventScroll: true });
26
21
  textEl.select();
27
22
  try {
28
23
  document.execCommand('copy');
29
24
  textEl.blur();
25
+ document.body.removeChild(textEl);
30
26
  }
31
27
  catch (err) {
32
28
  // ignore
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.10.0
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.8.4
2
+ * sculp-js v1.10.0
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.8.4
2
+ * sculp-js v1.10.0
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.8.4
2
+ * sculp-js v1.10.0
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.8.4
2
+ * sculp-js v1.10.0
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.8.4
2
+ * sculp-js v1.10.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/file.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.10.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/func.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.10.0
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.8.4
2
+ * sculp-js v1.10.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -131,7 +131,9 @@ exports.randomNumber = random.randomNumber;
131
131
  exports.randomString = random.randomString;
132
132
  exports.randomUuid = random.randomUuid;
133
133
  exports.HEX_POOL = number.HEX_POOL;
134
+ exports.formatMoney = number.formatNumber;
134
135
  exports.formatNumber = number.formatNumber;
136
+ exports.humanFileSize = number.humanFileSize;
135
137
  exports.numberAbbr = number.numberAbbr;
136
138
  exports.numberToHex = number.numberToHex;
137
139
  exports.UNIQUE_NUMBER_SAFE_LENGTH = unique.UNIQUE_NUMBER_SAFE_LENGTH;
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.10.0
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.8.4
2
+ * sculp-js v1.10.0
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.8.4
2
+ * sculp-js v1.10.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -8,6 +8,7 @@
8
8
 
9
9
  var func = require('./func.js');
10
10
  var string = require('./string.js');
11
+ var type = require('./type.js');
11
12
 
12
13
  const HEX_POOL = `${string.STRING_ARABIC_NUMERALS}${string.STRING_UPPERCASE_ALPHA}${string.STRING_LOWERCASE_ALPHA}`;
13
14
  const supportBigInt = typeof BigInt !== 'undefined';
@@ -45,38 +46,80 @@ function numberToHex(decimal, hexPool = HEX_POOL) {
45
46
  return ret.join('');
46
47
  }
47
48
  /**
48
- * 缩写
49
+ * 将数字转换为携带单位的字符串
49
50
  * @param {number | string} num
50
51
  * @param {Array<string>} units
51
- * @param {number} ratio
52
- * @param {number} exponent
52
+ * @param {INumberAbbr} options default: { ratio: 1000, decimals: 0, separator: ' ' }
53
53
  * @returns {string}
54
54
  */
55
- const numberAbbr = (num, units, ratio = 1000, exponent) => {
55
+ const numberAbbr = (num, units, options = { ratio: 1000, decimals: 0, separator: ' ' }) => {
56
+ const { ratio = 1000, decimals = 0, separator = ' ' } = options;
56
57
  const { length } = units;
57
58
  if (length === 0)
58
- throw new Error('至少需要一个单位');
59
+ throw new Error('At least one unit is required');
59
60
  let num2 = Number(num);
60
61
  let times = 0;
61
62
  while (num2 >= ratio && times < length - 1) {
62
63
  num2 = num2 / ratio;
63
64
  times++;
64
65
  }
65
- const value = num2.toFixed(exponent);
66
+ const value = num2.toFixed(decimals);
66
67
  const unit = units[times];
67
- return value.toString() + '' + unit;
68
+ return String(value) + separator + unit;
68
69
  };
70
+ /**
71
+ * Converting file size in bytes to human-readable string
72
+ * reference: https://zh.wikipedia.org/wiki/%E5%8D%83%E5%AD%97%E8%8A%82
73
+ * @param {number | string} num bytes Number in Bytes
74
+ * @param {IHumanFileSizeOptions} options default: { decimals = 0, si = false, separator = ' ' }
75
+ * si: True to use metric (SI) units, aka powers of 1000, the units is
76
+ * ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'].
77
+ * False to use binary (IEC), aka powers of 1024, the units is
78
+ * ['Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
79
+ * @returns
80
+ */
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
+ ? ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
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
+ }
92
+ if (!type.isNullOrUnDef(maxUnit)) {
93
+ const targetIndex = units.findIndex(el => el === maxUnit);
94
+ if (targetIndex !== -1) {
95
+ units.splice(targetIndex + 1);
96
+ }
97
+ }
98
+ return numberAbbr(num, units, { ratio: si ? 1000 : 1024, decimals, separator });
99
+ }
69
100
  /**
70
101
  * 将数字格式化成千位分隔符显示的字符串
71
- * @param {number} val 数字
72
- * @param {'int' | 'float'} type 展示分段显示的类型 int:整型 | float:浮点型
102
+ * @param {number|string} num 数字
103
+ * @param {number} decimals 格式化成指定小数位精度的参数
73
104
  * @returns {string}
74
105
  */
75
- function formatNumber(val, type = 'int') {
76
- return type === 'int' ? parseInt(String(val)).toLocaleString() : Number(val).toLocaleString('en-US');
106
+ function formatNumber(num, decimals) {
107
+ if (type.isNullOrUnDef(decimals)) {
108
+ return parseInt(String(num)).toLocaleString();
109
+ }
110
+ let prec = 0;
111
+ if (!type.isNumber(decimals)) {
112
+ throw new Error('Decimals must be a positive number not less than zero');
113
+ }
114
+ else if (decimals > 0) {
115
+ prec = decimals;
116
+ }
117
+ return Number(Number(num).toFixed(prec)).toLocaleString('en-US');
77
118
  }
78
119
 
79
120
  exports.HEX_POOL = HEX_POOL;
121
+ exports.formatMoney = formatNumber;
80
122
  exports.formatNumber = formatNumber;
123
+ exports.humanFileSize = humanFileSize;
81
124
  exports.numberAbbr = numberAbbr;
82
125
  exports.numberToHex = numberToHex;
package/lib/cjs/object.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.10.0
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.8.4
2
+ * sculp-js v1.10.0
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.8.4
2
+ * sculp-js v1.10.0
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.8.4
2
+ * sculp-js v1.10.0
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.8.4
2
+ * sculp-js v1.10.0
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.8.4
2
+ * sculp-js v1.10.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -7,51 +7,55 @@
7
7
  'use strict';
8
8
 
9
9
  var dom = require('./dom.js');
10
+ var type = require('./type.js');
10
11
 
11
12
  /**
12
13
  * @title 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')" onmousemove="handleMouseEnter({rootContainer: '#root', title: 'title content', event: event})"
18
+ * onmouseenter="handleMouseEnter({rootContainer:'#root', title: 'title content', event: event})">title content </span>
18
19
  */
19
20
  /**
20
21
  * 自定义title提示功能的mouseenter事件句柄
21
22
  * @param {ITooltipParams} param1
22
23
  * @returns {*}
23
24
  */
24
- function handleMouseEnter({ rootElId = '#root', title, event }) {
25
+ function handleMouseEnter({ rootContainer = '#root', title, event, bgColor = '#000', color = '#fff' }) {
25
26
  try {
26
- const $rootEl = document.querySelector(rootElId);
27
- console.assert($rootEl !== null, `未找到id为 ${rootElId} 的dom元素`);
27
+ const $rootEl = type.isString(rootContainer) ? document.querySelector(rootContainer) : rootContainer;
28
+ if (!$rootEl) {
29
+ throw new Error(`${rootContainer} is not valid Html Element or element selector`);
30
+ }
28
31
  let $customTitle = null;
32
+ const styleId = 'style-tooltip-inner1494304949567';
29
33
  // 动态创建class样式,并加入到head中
30
- if (!document.querySelector('.tooltip-inner1494304949567')) {
34
+ if (!document.querySelector(`#${styleId}`)) {
31
35
  const tooltipWrapperClass = document.createElement('style');
32
36
  tooltipWrapperClass.type = 'text/css';
37
+ tooltipWrapperClass.id = styleId;
33
38
  tooltipWrapperClass.innerHTML = `
34
39
  .tooltip-inner1494304949567 {
35
40
  max-width: 250px;
36
41
  padding: 3px 8px;
37
- color: #fff;
42
+ color: ${color};
38
43
  text-decoration: none;
39
44
  border-radius: 4px;
40
45
  text-align: left;
46
+ background-color: ${bgColor};
41
47
  }
42
48
  `;
43
49
  document.querySelector('head').appendChild(tooltipWrapperClass);
44
50
  }
45
- if (document.querySelector('#customTitle1494304949567')) {
46
- $customTitle = document.querySelector('#customTitle1494304949567');
51
+ $customTitle = document.querySelector('#customTitle1494304949567');
52
+ if ($customTitle) {
47
53
  mouseenter($customTitle, title, event);
48
54
  }
49
55
  else {
50
56
  const $contentContainer = document.createElement('div');
51
- $contentContainer.className = 'customTitle';
52
57
  $contentContainer.id = 'customTitle1494304949567';
53
- $contentContainer.className = 'tooltip';
54
- $contentContainer.style.cssText = 'z-index: 99999999; visibility: hidden;';
58
+ $contentContainer.style.cssText = 'z-index: 99999999; visibility: hidden; position: absolute;';
55
59
  $contentContainer.innerHTML =
56
60
  '<div class="tooltip-inner1494304949567" style="word-wrap: break-word; max-width: 44px;">皮肤</div>';
57
61
  $rootEl.appendChild($contentContainer);
@@ -104,11 +108,11 @@ function mouseenter($customTitle, title, e) {
104
108
  }
105
109
  /**
106
110
  * 移除提示文案dom的事件句柄
107
- * @param {string} rootElId
111
+ * @param {string} rootContainer
108
112
  * @returns {*}
109
113
  */
110
- function handleMouseLeave(rootElId = '#root') {
111
- const rootEl = document.querySelector(rootElId), titleEl = document.querySelector('#customTitle1494304949567');
114
+ function handleMouseLeave(rootContainer = '#root') {
115
+ const rootEl = type.isString(rootContainer) ? document.querySelector(rootContainer) : rootContainer, titleEl = document.querySelector('#customTitle1494304949567');
112
116
  if (rootEl && titleEl) {
113
117
  rootEl.removeChild(titleEl);
114
118
  }
package/lib/cjs/tree.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.10.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -212,7 +212,7 @@ function flatTree(treeList, options = defaultFieldOptions) {
212
212
  ...node,
213
213
  [childField]: [] // 清空子级
214
214
  };
215
- item.hasOwnProperty([childField]) && delete item[childField];
215
+ type.objectHas(item, childField) && delete item[childField];
216
216
  res.push(item);
217
217
  if (node[childField]) {
218
218
  const children = node[childField].map(item => ({
package/lib/cjs/type.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.10.0
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.8.4
2
+ * sculp-js v1.10.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/url.js CHANGED
@@ -1,29 +1,39 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.10.0
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 path = require('./path.js');
10
11
  var qs = require('./qs.js');
11
12
 
12
- const anchorEl = document.createElement('a');
13
13
  /**
14
14
  * url 解析
15
15
  * @param {string} url
16
+ * @param {boolean} isModernApi 使用现代API:URL, 默认true (对无效url解析会抛错), 否则使用a标签来解析(兼容性更强)
16
17
  * @returns {Url}
17
18
  */
18
- const urlParse = (url) => {
19
- anchorEl.href = url;
20
- const { protocol, username, password, host, port, hostname, hash, search, pathname: _pathname } = anchorEl;
19
+ const urlParse = (url, isModernApi = true) => {
20
+ // @ts-ignore
21
+ let urlObj = null;
22
+ if (type.isFunction(URL) && isModernApi) {
23
+ urlObj = new URL(url);
24
+ }
25
+ else {
26
+ urlObj = document.createElement('a');
27
+ urlObj.href = url;
28
+ }
29
+ const { protocol, username, password, host, port, hostname, hash, search, pathname: _pathname } = urlObj;
21
30
  // fix: ie 浏览器下,解析出来的 pathname 是没有 / 根的
22
31
  const pathname = path.pathJoin('/', _pathname);
23
32
  const auth = username && password ? `${username}:${password}` : '';
24
33
  const query = search.replace(/^\?/, '');
25
34
  const searchParams = qs.qsParse(query);
26
35
  const path$1 = `${pathname}${search}`;
36
+ urlObj = null;
27
37
  return {
28
38
  protocol,
29
39
  auth,
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.10.0
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.8.4
2
+ * sculp-js v1.10.0
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.8.4
2
+ * sculp-js v1.10.0
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.8.4
2
+ * sculp-js v1.10.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/array.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.10.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/es/async.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.8.4
2
+ * sculp-js v1.10.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */