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/es/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,30 +1,26 @@
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
- import { setStyle } from './dom.js';
8
-
9
- const textEl = document.createElement('textarea');
10
- setStyle(textEl, {
11
- position: 'absolute',
12
- top: '-9999px',
13
- left: '-9999px',
14
- opacity: 0
15
- });
16
- document.body.appendChild(textEl);
17
7
  /**
18
8
  * 复制文本
19
9
  * @param {string} text
20
10
  */
21
11
  function copyText(text) {
12
+ const textEl = document.createElement('textarea');
13
+ textEl.style.position = 'absolute';
14
+ textEl.style.top = '-9999px';
15
+ textEl.style.left = '-9999px';
22
16
  textEl.value = text;
17
+ document.body.appendChild(textEl);
23
18
  textEl.focus({ preventScroll: true });
24
19
  textEl.select();
25
20
  try {
26
21
  document.execCommand('copy');
27
22
  textEl.blur();
23
+ document.body.removeChild(textEl);
28
24
  }
29
25
  catch (err) {
30
26
  // 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/es/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/es/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/es/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
  */
@@ -131,28 +131,32 @@ function getComputedCssVal(el, property, reNumber = true) {
131
131
  * 字符串的像素宽度
132
132
  * @param {string} str 目标字符串
133
133
  * @param {number} fontSize 字符串字体大小
134
- * @param {boolean} isRemoveDom 计算后是否移除中间dom元素
134
+ * @param {boolean} isRemove 计算后是否移除创建的dom元素
135
135
  * @returns {*}
136
136
  */
137
- function getStrWidthPx(str, fontSize = 14, isRemoveDom = false) {
137
+ function getStrWidthPx(str, fontSize = 14, isRemove = true) {
138
138
  let strWidth = 0;
139
139
  console.assert(isString(str), `${str} 不是有效的字符串`);
140
140
  if (isString(str) && str.length > 0) {
141
- let getEle = document.querySelector('#getStrWidth1494304949567');
141
+ const id = 'getStrWidth1494304949567';
142
+ let getEle = document.querySelector(`#${id}`);
142
143
  if (!getEle) {
143
144
  const _ele = document.createElement('span');
144
- _ele.id = 'getStrWidth1494304949567';
145
+ _ele.id = id;
145
146
  _ele.style.fontSize = fontSize + 'px';
146
147
  _ele.style.whiteSpace = 'nowrap';
147
148
  _ele.style.visibility = 'hidden';
149
+ _ele.style.position = 'absolute';
150
+ _ele.style.top = '-9999px';
151
+ _ele.style.left = '-9999px';
148
152
  _ele.textContent = str;
149
153
  document.body.appendChild(_ele);
150
154
  getEle = _ele;
151
155
  }
152
156
  getEle.textContent = str;
153
157
  strWidth = getEle.offsetWidth;
154
- if (isRemoveDom) {
155
- document.body.appendChild(getEle);
158
+ if (isRemove) {
159
+ getEle.remove();
156
160
  }
157
161
  }
158
162
  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/es/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/es/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/es/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/es/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
  */
@@ -21,7 +21,7 @@ export { chooseLocalFile, compressImg, supportCanvas } from './file.js';
21
21
  export { genCanvasWM } from './watermark.js';
22
22
  export { debounce, getGlobal, once, setGlobal, throttle } from './func.js';
23
23
  export { STRING_POOL, randomNumber, randomString, randomUuid } from './random.js';
24
- export { HEX_POOL, formatNumber, numberAbbr, numberToHex } from './number.js';
24
+ export { HEX_POOL, formatNumber as formatMoney, formatNumber, humanFileSize, numberAbbr, numberToHex } from './number.js';
25
25
  export { UNIQUE_NUMBER_SAFE_LENGTH, uniqueNumber, uniqueString } from './unique.js';
26
26
  export { tooltipEvent } from './tooltip.js';
27
27
  export { flatTree, forEachDeep, formatTree, fuzzySearchTree, mapDeep, searchTreeById } from './tree.js';
package/lib/es/isEqual.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/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/es/number.js CHANGED
@@ -1,11 +1,12 @@
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
  import { getGlobal } from './func.js';
8
8
  import { STRING_ARABIC_NUMERALS, STRING_UPPERCASE_ALPHA, STRING_LOWERCASE_ALPHA } from './string.js';
9
+ import { isNullOrUnDef, isNumber } from './type.js';
9
10
 
10
11
  const HEX_POOL = `${STRING_ARABIC_NUMERALS}${STRING_UPPERCASE_ALPHA}${STRING_LOWERCASE_ALPHA}`;
11
12
  const supportBigInt = typeof BigInt !== 'undefined';
@@ -43,35 +44,75 @@ function numberToHex(decimal, hexPool = HEX_POOL) {
43
44
  return ret.join('');
44
45
  }
45
46
  /**
46
- * 缩写
47
+ * 将数字转换为携带单位的字符串
47
48
  * @param {number | string} num
48
49
  * @param {Array<string>} units
49
- * @param {number} ratio
50
- * @param {number} exponent
50
+ * @param {INumberAbbr} options default: { ratio: 1000, decimals: 0, separator: ' ' }
51
51
  * @returns {string}
52
52
  */
53
- const numberAbbr = (num, units, ratio = 1000, exponent) => {
53
+ const numberAbbr = (num, units, options = { ratio: 1000, decimals: 0, separator: ' ' }) => {
54
+ const { ratio = 1000, decimals = 0, separator = ' ' } = options;
54
55
  const { length } = units;
55
56
  if (length === 0)
56
- throw new Error('至少需要一个单位');
57
+ throw new Error('At least one unit is required');
57
58
  let num2 = Number(num);
58
59
  let times = 0;
59
60
  while (num2 >= ratio && times < length - 1) {
60
61
  num2 = num2 / ratio;
61
62
  times++;
62
63
  }
63
- const value = num2.toFixed(exponent);
64
+ const value = num2.toFixed(decimals);
64
65
  const unit = units[times];
65
- return value.toString() + '' + unit;
66
+ return String(value) + separator + unit;
66
67
  };
68
+ /**
69
+ * Converting file size in bytes to human-readable string
70
+ * reference: https://zh.wikipedia.org/wiki/%E5%8D%83%E5%AD%97%E8%8A%82
71
+ * @param {number | string} num bytes Number in Bytes
72
+ * @param {IHumanFileSizeOptions} options default: { decimals = 0, si = false, separator = ' ' }
73
+ * si: True to use metric (SI) units, aka powers of 1000, the units is
74
+ * ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'].
75
+ * False to use binary (IEC), aka powers of 1024, the units is
76
+ * ['Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
77
+ * @returns
78
+ */
79
+ function humanFileSize(num, options = { decimals: 0, si: false, separator: ' ' }) {
80
+ const { decimals = 0, si = false, separator = ' ', baseUnit, maxUnit } = options;
81
+ let units = si
82
+ ? ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
83
+ : ['Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
84
+ if (!isNullOrUnDef(baseUnit)) {
85
+ const targetIndex = units.findIndex(el => el === baseUnit);
86
+ if (targetIndex !== -1) {
87
+ units = units.slice(targetIndex);
88
+ }
89
+ }
90
+ if (!isNullOrUnDef(maxUnit)) {
91
+ const targetIndex = units.findIndex(el => el === maxUnit);
92
+ if (targetIndex !== -1) {
93
+ units.splice(targetIndex + 1);
94
+ }
95
+ }
96
+ return numberAbbr(num, units, { ratio: si ? 1000 : 1024, decimals, separator });
97
+ }
67
98
  /**
68
99
  * 将数字格式化成千位分隔符显示的字符串
69
- * @param {number} val 数字
70
- * @param {'int' | 'float'} type 展示分段显示的类型 int:整型 | float:浮点型
100
+ * @param {number|string} num 数字
101
+ * @param {number} decimals 格式化成指定小数位精度的参数
71
102
  * @returns {string}
72
103
  */
73
- function formatNumber(val, type = 'int') {
74
- return type === 'int' ? parseInt(String(val)).toLocaleString() : Number(val).toLocaleString('en-US');
104
+ function formatNumber(num, decimals) {
105
+ if (isNullOrUnDef(decimals)) {
106
+ return parseInt(String(num)).toLocaleString();
107
+ }
108
+ let prec = 0;
109
+ if (!isNumber(decimals)) {
110
+ throw new Error('Decimals must be a positive number not less than zero');
111
+ }
112
+ else if (decimals > 0) {
113
+ prec = decimals;
114
+ }
115
+ return Number(Number(num).toFixed(prec)).toLocaleString('en-US');
75
116
  }
76
117
 
77
- export { HEX_POOL, formatNumber, numberAbbr, numberToHex };
118
+ export { HEX_POOL, formatNumber as formatMoney, formatNumber, humanFileSize, numberAbbr, numberToHex };
package/lib/es/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/es/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/es/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/es/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/es/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
  */
package/lib/es/tooltip.js CHANGED
@@ -1,55 +1,59 @@
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
  import { getStrWidthPx } from './dom.js';
8
+ import { isString } from './type.js';
8
9
 
9
10
  /**
10
11
  * @title tooltip
11
12
  * @Desc 自定义的tooltip方法, 支持拖动悬浮提示
12
13
  * Created by chendeqiao on 2017/5/8.
13
14
  * @example
14
- * <span onmouseleave="handleMouseLeave('#root')" onmousemove="handleMouseEnter({rootElId: '#root', title: 'title content', event: event})"
15
- * onmouseenter="handleMouseEnter({'#root', title: 'title content', event: event})">title content </span>
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>
16
17
  */
17
18
  /**
18
19
  * 自定义title提示功能的mouseenter事件句柄
19
20
  * @param {ITooltipParams} param1
20
21
  * @returns {*}
21
22
  */
22
- function handleMouseEnter({ rootElId = '#root', title, event }) {
23
+ function handleMouseEnter({ rootContainer = '#root', title, event, bgColor = '#000', color = '#fff' }) {
23
24
  try {
24
- const $rootEl = document.querySelector(rootElId);
25
- console.assert($rootEl !== null, `未找到id为 ${rootElId} 的dom元素`);
25
+ const $rootEl = isString(rootContainer) ? document.querySelector(rootContainer) : rootContainer;
26
+ if (!$rootEl) {
27
+ throw new Error(`${rootContainer} is not valid Html Element or element selector`);
28
+ }
26
29
  let $customTitle = null;
30
+ const styleId = 'style-tooltip-inner1494304949567';
27
31
  // 动态创建class样式,并加入到head中
28
- if (!document.querySelector('.tooltip-inner1494304949567')) {
32
+ if (!document.querySelector(`#${styleId}`)) {
29
33
  const tooltipWrapperClass = document.createElement('style');
30
34
  tooltipWrapperClass.type = 'text/css';
35
+ tooltipWrapperClass.id = styleId;
31
36
  tooltipWrapperClass.innerHTML = `
32
37
  .tooltip-inner1494304949567 {
33
38
  max-width: 250px;
34
39
  padding: 3px 8px;
35
- color: #fff;
40
+ color: ${color};
36
41
  text-decoration: none;
37
42
  border-radius: 4px;
38
43
  text-align: left;
44
+ background-color: ${bgColor};
39
45
  }
40
46
  `;
41
47
  document.querySelector('head').appendChild(tooltipWrapperClass);
42
48
  }
43
- if (document.querySelector('#customTitle1494304949567')) {
44
- $customTitle = document.querySelector('#customTitle1494304949567');
49
+ $customTitle = document.querySelector('#customTitle1494304949567');
50
+ if ($customTitle) {
45
51
  mouseenter($customTitle, title, event);
46
52
  }
47
53
  else {
48
54
  const $contentContainer = document.createElement('div');
49
- $contentContainer.className = 'customTitle';
50
55
  $contentContainer.id = 'customTitle1494304949567';
51
- $contentContainer.className = 'tooltip';
52
- $contentContainer.style.cssText = 'z-index: 99999999; visibility: hidden;';
56
+ $contentContainer.style.cssText = 'z-index: 99999999; visibility: hidden; position: absolute;';
53
57
  $contentContainer.innerHTML =
54
58
  '<div class="tooltip-inner1494304949567" style="word-wrap: break-word; max-width: 44px;">皮肤</div>';
55
59
  $rootEl.appendChild($contentContainer);
@@ -102,11 +106,11 @@ function mouseenter($customTitle, title, e) {
102
106
  }
103
107
  /**
104
108
  * 移除提示文案dom的事件句柄
105
- * @param {string} rootElId
109
+ * @param {string} rootContainer
106
110
  * @returns {*}
107
111
  */
108
- function handleMouseLeave(rootElId = '#root') {
109
- const rootEl = document.querySelector(rootElId), titleEl = document.querySelector('#customTitle1494304949567');
112
+ function handleMouseLeave(rootContainer = '#root') {
113
+ const rootEl = isString(rootContainer) ? document.querySelector(rootContainer) : rootContainer, titleEl = document.querySelector('#customTitle1494304949567');
110
114
  if (rootEl && titleEl) {
111
115
  rootEl.removeChild(titleEl);
112
116
  }
package/lib/es/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
  */
@@ -210,7 +210,7 @@ function flatTree(treeList, options = defaultFieldOptions) {
210
210
  ...node,
211
211
  [childField]: [] // 清空子级
212
212
  };
213
- item.hasOwnProperty([childField]) && delete item[childField];
213
+ objectHas(item, childField) && delete item[childField];
214
214
  res.push(item);
215
215
  if (node[childField]) {
216
216
  const children = node[childField].map(item => ({
package/lib/es/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/es/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/es/url.js CHANGED
@@ -1,27 +1,37 @@
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
+ import { isFunction } from './type.js';
7
8
  import { pathJoin } from './path.js';
8
9
  import { qsParse, qsStringify } from './qs.js';
9
10
 
10
- const anchorEl = document.createElement('a');
11
11
  /**
12
12
  * url 解析
13
13
  * @param {string} url
14
+ * @param {boolean} isModernApi 使用现代API:URL, 默认true (对无效url解析会抛错), 否则使用a标签来解析(兼容性更强)
14
15
  * @returns {Url}
15
16
  */
16
- const urlParse = (url) => {
17
- anchorEl.href = url;
18
- const { protocol, username, password, host, port, hostname, hash, search, pathname: _pathname } = anchorEl;
17
+ const urlParse = (url, isModernApi = true) => {
18
+ // @ts-ignore
19
+ let urlObj = null;
20
+ if (isFunction(URL) && isModernApi) {
21
+ urlObj = new URL(url);
22
+ }
23
+ else {
24
+ urlObj = document.createElement('a');
25
+ urlObj.href = url;
26
+ }
27
+ const { protocol, username, password, host, port, hostname, hash, search, pathname: _pathname } = urlObj;
19
28
  // fix: ie 浏览器下,解析出来的 pathname 是没有 / 根的
20
29
  const pathname = pathJoin('/', _pathname);
21
30
  const auth = username && password ? `${username}:${password}` : '';
22
31
  const query = search.replace(/^\?/, '');
23
32
  const searchParams = qsParse(query);
24
33
  const path = `${pathname}${search}`;
34
+ urlObj = null;
25
35
  return {
26
36
  protocol,
27
37
  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,9 +1,11 @@
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
+ import { isString, isNullOrUnDef } from './type.js';
8
+
7
9
  /*
8
10
  * @created: Saturday, 2020-04-18 14:38:23
9
11
  * @author: chendq
@@ -15,15 +17,14 @@
15
17
  * @param {ICanvasWM} canvasWM
16
18
  * @example genCanvasWM({ content: 'QQMusicFE' })
17
19
  */
18
- function genCanvasWM(canvasWM) {
19
- const { container = document.body, width = '300px', height = '200px', textAlign = 'center', textBaseline = 'middle', font = '20px PingFangSC-Medium,PingFang SC',
20
+ function genCanvasWM(content = '请勿外传', canvasWM) {
21
+ const { rootContainer = document.body, width = '300px', height = '150px', textAlign = 'center', textBaseline = 'middle', font = '20px PingFangSC-Medium,PingFang SC',
20
22
  // fontWeight = 500,
21
- fillStyle = 'rgba(189, 177, 167, .3)', content = '请勿外传', rotate = 30, zIndex = 2147483647 } = canvasWM;
22
- // 仅限主页面添加水印
23
- // if (!location.pathname.includes('/home')) {
24
- // return;
25
- // }
26
- const args = canvasWM;
23
+ fillStyle = 'rgba(189, 177, 167, .3)', rotate = -20, zIndex = 2147483647, watermarkId = '__wm' } = isNullOrUnDef(canvasWM) ? {} : canvasWM;
24
+ const container = isString(rootContainer) ? document.querySelector(rootContainer) : rootContainer;
25
+ if (!container) {
26
+ throw new Error(`${rootContainer} is not valid Html Element or element selector`);
27
+ }
27
28
  const canvas = document.createElement('canvas');
28
29
  canvas.setAttribute('width', width);
29
30
  canvas.setAttribute('height', height);
@@ -36,36 +37,36 @@ function genCanvasWM(canvasWM) {
36
37
  ctx.rotate((Math.PI / 180) * rotate);
37
38
  ctx.fillText(content, parseFloat(width) / 4, parseFloat(height) / 2);
38
39
  const base64Url = canvas.toDataURL();
39
- const __wm = document.querySelector('.__wm');
40
+ const __wm = document.querySelector(`#${watermarkId}`);
40
41
  const watermarkDiv = __wm || document.createElement('div');
41
42
  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}')`;
42
43
  watermarkDiv.setAttribute('style', styleStr);
43
- watermarkDiv.classList.add('__wm');
44
+ watermarkDiv.setAttribute('id', watermarkId);
44
45
  watermarkDiv.classList.add('nav-height');
45
46
  if (!__wm) {
46
47
  container.style.position = 'relative';
47
48
  container.appendChild(watermarkDiv);
48
49
  }
49
50
  const getMutableStyle = (ele) => {
50
- const computedStle = getComputedStyle(ele);
51
+ const computedStyle = getComputedStyle(ele);
51
52
  return {
52
- opacity: computedStle.getPropertyValue('opacity'),
53
- zIndex: computedStle.getPropertyValue('z-index'),
54
- display: computedStle.getPropertyValue('display'),
55
- visibility: computedStle.getPropertyValue('visibility')
53
+ opacity: computedStyle.getPropertyValue('opacity'),
54
+ zIndex: computedStyle.getPropertyValue('z-index'),
55
+ display: computedStyle.getPropertyValue('display'),
56
+ visibility: computedStyle.getPropertyValue('visibility')
56
57
  };
57
58
  };
58
59
  //@ts-ignore
59
60
  const MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
60
61
  if (MutationObserver) {
61
62
  let mo = new MutationObserver(function () {
62
- const __wm = document.querySelector('.__wm'); // 只在__wm元素变动才重新调用 __canvasWM
63
+ const __wm = document.querySelector(`#${watermarkId}`); // 只在__wm元素变动才重新调用 __canvasWM
63
64
  if (!__wm) {
64
65
  // 避免一直触发
65
66
  // console.log('regenerate watermark by delete::')
66
67
  mo.disconnect();
67
68
  mo = null;
68
- genCanvasWM(JSON.parse(JSON.stringify(args)));
69
+ genCanvasWM(content, canvasWM);
69
70
  }
70
71
  else {
71
72
  const { opacity, zIndex, display, visibility } = getMutableStyle(__wm);
@@ -77,7 +78,7 @@ function genCanvasWM(canvasWM) {
77
78
  mo.disconnect();
78
79
  mo = null;
79
80
  container.removeChild(__wm);
80
- genCanvasWM(JSON.parse(JSON.stringify(args)));
81
+ genCanvasWM(content, canvasWM);
81
82
  }
82
83
  }
83
84
  });
@@ -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
  */