sculp-js 1.9.0 → 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 +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 +1 -1
  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 +20 -16
  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 +1 -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 +1 -1
  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 +20 -16
  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 +24 -21
  62. package/lib/umd/index.js +55 -43
  63. package/package.json +2 -1
package/lib/es/number.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -76,11 +76,17 @@ const numberAbbr = (num, units, options = { ratio: 1000, decimals: 0, separator:
76
76
  * ['Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
77
77
  * @returns
78
78
  */
79
- function humanFileSize(num, options) {
80
- const { decimals = 0, si = false, separator = ' ', maxUnit } = options;
81
- const units = si
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
82
  ? ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
83
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
+ }
84
90
  if (!isNullOrUnDef(maxUnit)) {
85
91
  const targetIndex = units.findIndex(el => el === maxUnit);
86
92
  if (targetIndex !== -1) {
package/lib/es/object.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
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.9.0
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.9.0
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.9.0
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.9.0
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.9.0
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.9.0
2
+ * sculp-js v1.10.0
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.9.0
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.9.0
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,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
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.9.0
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.9.0
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.9.0
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.9.0
2
+ * sculp-js v1.10.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/index.d.ts CHANGED
@@ -285,10 +285,10 @@ declare function getComputedCssVal(el: HTMLElement, property: string, reNumber?:
285
285
  * 字符串的像素宽度
286
286
  * @param {string} str 目标字符串
287
287
  * @param {number} fontSize 字符串字体大小
288
- * @param {boolean} isRemoveDom 计算后是否移除中间dom元素
288
+ * @param {boolean} isRemove 计算后是否移除创建的dom元素
289
289
  * @returns {*}
290
290
  */
291
- declare function getStrWidthPx(str: string, fontSize?: number, isRemoveDom?: boolean): number;
291
+ declare function getStrWidthPx(str: string, fontSize?: number, isRemove?: boolean): number;
292
292
 
293
293
  interface Params<T = string | number> {
294
294
  [key: string]: T | Array<T>;
@@ -596,23 +596,23 @@ interface ICompressOptions {
596
596
  declare function compressImg(file: File | FileList, options: ICompressOptions): Promise<object> | undefined;
597
597
 
598
598
  interface ICanvasWM {
599
- container: HTMLElement;
600
- width: string;
601
- height: string;
602
- textAlign: CanvasTextAlign;
603
- textBaseline: CanvasTextBaseline;
604
- font: string;
605
- fillStyle: string;
606
- content: string;
607
- rotate: number;
608
- zIndex: number;
599
+ rootContainer?: HTMLElement | string;
600
+ width?: string;
601
+ height?: string;
602
+ textAlign?: CanvasTextAlign;
603
+ textBaseline?: CanvasTextBaseline;
604
+ font?: string;
605
+ fillStyle?: string;
606
+ rotate?: number;
607
+ zIndex?: number;
608
+ watermarkId?: string;
609
609
  }
610
610
  /**
611
611
  * canvas 实现 水印, 具备防删除功能
612
612
  * @param {ICanvasWM} canvasWM
613
613
  * @example genCanvasWM({ content: 'QQMusicFE' })
614
614
  */
615
- declare function genCanvasWM(canvasWM: ICanvasWM): void;
615
+ declare function genCanvasWM(content?: string, canvasWM?: ICanvasWM): void;
616
616
 
617
617
  interface DebounceFunc<F extends AnyFunc> {
618
618
  (...args: Parameters<F>): void;
@@ -715,6 +715,7 @@ interface IHumanFileSizeOptions {
715
715
  decimals?: number;
716
716
  si?: boolean;
717
717
  separator?: string;
718
+ baseUnit?: string;
718
719
  maxUnit?: string;
719
720
  }
720
721
  /**
@@ -728,7 +729,7 @@ interface IHumanFileSizeOptions {
728
729
  * ['Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
729
730
  * @returns
730
731
  */
731
- declare function humanFileSize(num: number | string, options: IHumanFileSizeOptions): string;
732
+ declare function humanFileSize(num: number | string, options?: IHumanFileSizeOptions): string;
732
733
  /**
733
734
  * 将数字格式化成千位分隔符显示的字符串
734
735
  * @param {number|string} num 数字
@@ -763,26 +764,28 @@ declare const uniqueString: UniqueString;
763
764
  * @Desc 自定义的tooltip方法, 支持拖动悬浮提示
764
765
  * Created by chendeqiao on 2017/5/8.
765
766
  * @example
766
- * <span onmouseleave="handleMouseLeave('#root')" onmousemove="handleMouseEnter({rootElId: '#root', title: 'title content', event: event})"
767
- * onmouseenter="handleMouseEnter({'#root', title: 'title content', event: event})">title content </span>
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>
768
769
  */
769
770
  interface ITooltipParams {
770
- rootElId: string;
771
+ rootContainer: HTMLElement | string;
771
772
  title: string;
772
- event: PointerEvent;
773
+ event: PointerEvent | MouseEvent;
774
+ bgColor?: string;
775
+ color?: string;
773
776
  }
774
777
  /**
775
778
  * 自定义title提示功能的mouseenter事件句柄
776
779
  * @param {ITooltipParams} param1
777
780
  * @returns {*}
778
781
  */
779
- declare function handleMouseEnter({ rootElId, title, event }: ITooltipParams): void;
782
+ declare function handleMouseEnter({ rootContainer, title, event, bgColor, color }: ITooltipParams): void;
780
783
  /**
781
784
  * 移除提示文案dom的事件句柄
782
- * @param {string} rootElId
785
+ * @param {string} rootContainer
783
786
  * @returns {*}
784
787
  */
785
- declare function handleMouseLeave(rootElId?: string): void;
788
+ declare function handleMouseLeave(rootContainer?: HTMLElement | string): void;
786
789
  declare const tooltipEvent: {
787
790
  handleMouseEnter: typeof handleMouseEnter;
788
791
  handleMouseLeave: typeof handleMouseLeave;
package/lib/umd/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.9.0
2
+ * sculp-js v1.10.0
3
3
  * (c) 2023-present chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -964,28 +964,32 @@
964
964
  * 字符串的像素宽度
965
965
  * @param {string} str 目标字符串
966
966
  * @param {number} fontSize 字符串字体大小
967
- * @param {boolean} isRemoveDom 计算后是否移除中间dom元素
967
+ * @param {boolean} isRemove 计算后是否移除创建的dom元素
968
968
  * @returns {*}
969
969
  */
970
- function getStrWidthPx(str, fontSize = 14, isRemoveDom = false) {
970
+ function getStrWidthPx(str, fontSize = 14, isRemove = true) {
971
971
  let strWidth = 0;
972
972
  console.assert(isString(str), `${str} 不是有效的字符串`);
973
973
  if (isString(str) && str.length > 0) {
974
- let getEle = document.querySelector('#getStrWidth1494304949567');
974
+ const id = 'getStrWidth1494304949567';
975
+ let getEle = document.querySelector(`#${id}`);
975
976
  if (!getEle) {
976
977
  const _ele = document.createElement('span');
977
- _ele.id = 'getStrWidth1494304949567';
978
+ _ele.id = id;
978
979
  _ele.style.fontSize = fontSize + 'px';
979
980
  _ele.style.whiteSpace = 'nowrap';
980
981
  _ele.style.visibility = 'hidden';
982
+ _ele.style.position = 'absolute';
983
+ _ele.style.top = '-9999px';
984
+ _ele.style.left = '-9999px';
981
985
  _ele.textContent = str;
982
986
  document.body.appendChild(_ele);
983
987
  getEle = _ele;
984
988
  }
985
989
  getEle.textContent = str;
986
990
  strWidth = getEle.offsetWidth;
987
- if (isRemoveDom) {
988
- document.body.appendChild(getEle);
991
+ if (isRemove) {
992
+ getEle.remove();
989
993
  }
990
994
  }
991
995
  return strWidth;
@@ -1606,15 +1610,14 @@
1606
1610
  * @param {ICanvasWM} canvasWM
1607
1611
  * @example genCanvasWM({ content: 'QQMusicFE' })
1608
1612
  */
1609
- function genCanvasWM(canvasWM) {
1610
- const { container = document.body, width = '300px', height = '200px', textAlign = 'center', textBaseline = 'middle', font = '20px PingFangSC-Medium,PingFang SC',
1613
+ function genCanvasWM(content = '请勿外传', canvasWM) {
1614
+ const { rootContainer = document.body, width = '300px', height = '150px', textAlign = 'center', textBaseline = 'middle', font = '20px PingFangSC-Medium,PingFang SC',
1611
1615
  // fontWeight = 500,
1612
- fillStyle = 'rgba(189, 177, 167, .3)', content = '请勿外传', rotate = 30, zIndex = 2147483647 } = canvasWM;
1613
- // 仅限主页面添加水印
1614
- // if (!location.pathname.includes('/home')) {
1615
- // return;
1616
- // }
1617
- const args = canvasWM;
1616
+ fillStyle = 'rgba(189, 177, 167, .3)', rotate = -20, zIndex = 2147483647, watermarkId = '__wm' } = isNullOrUnDef(canvasWM) ? {} : canvasWM;
1617
+ const container = isString(rootContainer) ? document.querySelector(rootContainer) : rootContainer;
1618
+ if (!container) {
1619
+ throw new Error(`${rootContainer} is not valid Html Element or element selector`);
1620
+ }
1618
1621
  const canvas = document.createElement('canvas');
1619
1622
  canvas.setAttribute('width', width);
1620
1623
  canvas.setAttribute('height', height);
@@ -1627,36 +1630,36 @@
1627
1630
  ctx.rotate((Math.PI / 180) * rotate);
1628
1631
  ctx.fillText(content, parseFloat(width) / 4, parseFloat(height) / 2);
1629
1632
  const base64Url = canvas.toDataURL();
1630
- const __wm = document.querySelector('.__wm');
1633
+ const __wm = document.querySelector(`#${watermarkId}`);
1631
1634
  const watermarkDiv = __wm || document.createElement('div');
1632
1635
  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}')`;
1633
1636
  watermarkDiv.setAttribute('style', styleStr);
1634
- watermarkDiv.classList.add('__wm');
1637
+ watermarkDiv.setAttribute('id', watermarkId);
1635
1638
  watermarkDiv.classList.add('nav-height');
1636
1639
  if (!__wm) {
1637
1640
  container.style.position = 'relative';
1638
1641
  container.appendChild(watermarkDiv);
1639
1642
  }
1640
1643
  const getMutableStyle = (ele) => {
1641
- const computedStle = getComputedStyle(ele);
1644
+ const computedStyle = getComputedStyle(ele);
1642
1645
  return {
1643
- opacity: computedStle.getPropertyValue('opacity'),
1644
- zIndex: computedStle.getPropertyValue('z-index'),
1645
- display: computedStle.getPropertyValue('display'),
1646
- visibility: computedStle.getPropertyValue('visibility')
1646
+ opacity: computedStyle.getPropertyValue('opacity'),
1647
+ zIndex: computedStyle.getPropertyValue('z-index'),
1648
+ display: computedStyle.getPropertyValue('display'),
1649
+ visibility: computedStyle.getPropertyValue('visibility')
1647
1650
  };
1648
1651
  };
1649
1652
  //@ts-ignore
1650
1653
  const MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
1651
1654
  if (MutationObserver) {
1652
1655
  let mo = new MutationObserver(function () {
1653
- const __wm = document.querySelector('.__wm'); // 只在__wm元素变动才重新调用 __canvasWM
1656
+ const __wm = document.querySelector(`#${watermarkId}`); // 只在__wm元素变动才重新调用 __canvasWM
1654
1657
  if (!__wm) {
1655
1658
  // 避免一直触发
1656
1659
  // console.log('regenerate watermark by delete::')
1657
1660
  mo.disconnect();
1658
1661
  mo = null;
1659
- genCanvasWM(JSON.parse(JSON.stringify(args)));
1662
+ genCanvasWM(content, canvasWM);
1660
1663
  }
1661
1664
  else {
1662
1665
  const { opacity, zIndex, display, visibility } = getMutableStyle(__wm);
@@ -1668,7 +1671,7 @@
1668
1671
  mo.disconnect();
1669
1672
  mo = null;
1670
1673
  container.removeChild(__wm);
1671
- genCanvasWM(JSON.parse(JSON.stringify(args)));
1674
+ genCanvasWM(content, canvasWM);
1672
1675
  }
1673
1676
  }
1674
1677
  });
@@ -1921,11 +1924,17 @@
1921
1924
  * ['Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB']
1922
1925
  * @returns
1923
1926
  */
1924
- function humanFileSize(num, options) {
1925
- const { decimals = 0, si = false, separator = ' ', maxUnit } = options;
1926
- const units = si
1927
+ function humanFileSize(num, options = { decimals: 0, si: false, separator: ' ' }) {
1928
+ const { decimals = 0, si = false, separator = ' ', baseUnit, maxUnit } = options;
1929
+ let units = si
1927
1930
  ? ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
1928
1931
  : ['Byte', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB', 'ZiB', 'YiB'];
1932
+ if (!isNullOrUnDef(baseUnit)) {
1933
+ const targetIndex = units.findIndex(el => el === baseUnit);
1934
+ if (targetIndex !== -1) {
1935
+ units = units.slice(targetIndex);
1936
+ }
1937
+ }
1929
1938
  if (!isNullOrUnDef(maxUnit)) {
1930
1939
  const targetIndex = units.findIndex(el => el === maxUnit);
1931
1940
  if (targetIndex !== -1) {
@@ -2027,45 +2036,48 @@
2027
2036
  * @Desc 自定义的tooltip方法, 支持拖动悬浮提示
2028
2037
  * Created by chendeqiao on 2017/5/8.
2029
2038
  * @example
2030
- * <span onmouseleave="handleMouseLeave('#root')" onmousemove="handleMouseEnter({rootElId: '#root', title: 'title content', event: event})"
2031
- * onmouseenter="handleMouseEnter({'#root', title: 'title content', event: event})">title content </span>
2039
+ * <span onmouseleave="handleMouseLeave('#root')" onmousemove="handleMouseEnter({rootContainer: '#root', title: 'title content', event: event})"
2040
+ * onmouseenter="handleMouseEnter({rootContainer:'#root', title: 'title content', event: event})">title content </span>
2032
2041
  */
2033
2042
  /**
2034
2043
  * 自定义title提示功能的mouseenter事件句柄
2035
2044
  * @param {ITooltipParams} param1
2036
2045
  * @returns {*}
2037
2046
  */
2038
- function handleMouseEnter({ rootElId = '#root', title, event }) {
2047
+ function handleMouseEnter({ rootContainer = '#root', title, event, bgColor = '#000', color = '#fff' }) {
2039
2048
  try {
2040
- const $rootEl = document.querySelector(rootElId);
2041
- console.assert($rootEl !== null, `未找到id为 ${rootElId} 的dom元素`);
2049
+ const $rootEl = isString(rootContainer) ? document.querySelector(rootContainer) : rootContainer;
2050
+ if (!$rootEl) {
2051
+ throw new Error(`${rootContainer} is not valid Html Element or element selector`);
2052
+ }
2042
2053
  let $customTitle = null;
2054
+ const styleId = 'style-tooltip-inner1494304949567';
2043
2055
  // 动态创建class样式,并加入到head中
2044
- if (!document.querySelector('.tooltip-inner1494304949567')) {
2056
+ if (!document.querySelector(`#${styleId}`)) {
2045
2057
  const tooltipWrapperClass = document.createElement('style');
2046
2058
  tooltipWrapperClass.type = 'text/css';
2059
+ tooltipWrapperClass.id = styleId;
2047
2060
  tooltipWrapperClass.innerHTML = `
2048
2061
  .tooltip-inner1494304949567 {
2049
2062
  max-width: 250px;
2050
2063
  padding: 3px 8px;
2051
- color: #fff;
2064
+ color: ${color};
2052
2065
  text-decoration: none;
2053
2066
  border-radius: 4px;
2054
2067
  text-align: left;
2068
+ background-color: ${bgColor};
2055
2069
  }
2056
2070
  `;
2057
2071
  document.querySelector('head').appendChild(tooltipWrapperClass);
2058
2072
  }
2059
- if (document.querySelector('#customTitle1494304949567')) {
2060
- $customTitle = document.querySelector('#customTitle1494304949567');
2073
+ $customTitle = document.querySelector('#customTitle1494304949567');
2074
+ if ($customTitle) {
2061
2075
  mouseenter($customTitle, title, event);
2062
2076
  }
2063
2077
  else {
2064
2078
  const $contentContainer = document.createElement('div');
2065
- $contentContainer.className = 'customTitle';
2066
2079
  $contentContainer.id = 'customTitle1494304949567';
2067
- $contentContainer.className = 'tooltip';
2068
- $contentContainer.style.cssText = 'z-index: 99999999; visibility: hidden;';
2080
+ $contentContainer.style.cssText = 'z-index: 99999999; visibility: hidden; position: absolute;';
2069
2081
  $contentContainer.innerHTML =
2070
2082
  '<div class="tooltip-inner1494304949567" style="word-wrap: break-word; max-width: 44px;">皮肤</div>';
2071
2083
  $rootEl.appendChild($contentContainer);
@@ -2118,11 +2130,11 @@
2118
2130
  }
2119
2131
  /**
2120
2132
  * 移除提示文案dom的事件句柄
2121
- * @param {string} rootElId
2133
+ * @param {string} rootContainer
2122
2134
  * @returns {*}
2123
2135
  */
2124
- function handleMouseLeave(rootElId = '#root') {
2125
- const rootEl = document.querySelector(rootElId), titleEl = document.querySelector('#customTitle1494304949567');
2136
+ function handleMouseLeave(rootContainer = '#root') {
2137
+ const rootEl = isString(rootContainer) ? document.querySelector(rootContainer) : rootContainer, titleEl = document.querySelector('#customTitle1494304949567');
2126
2138
  if (rootEl && titleEl) {
2127
2139
  rootEl.removeChild(titleEl);
2128
2140
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sculp-js",
3
- "version": "1.9.0",
3
+ "version": "1.10.0",
4
4
  "packageManager": "npm@8.19.2",
5
5
  "description": "js utils library, includes function library、class library",
6
6
  "scripts": {
@@ -79,6 +79,7 @@
79
79
  "eslint-plugin-standard": "^5.0.0",
80
80
  "husky": "^8.0.3",
81
81
  "jest": "^29.7.0",
82
+ "jest-canvas-mock": "^2.5.2",
82
83
  "jest-environment-jsdom": "^29.7.0",
83
84
  "lint-staged": "^13.2.2",
84
85
  "prettier": "^3.0.3",