sculp-js 1.0.0 → 1.1.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 (49) hide show
  1. package/README.md +106 -9
  2. package/lib/cjs/array.js +116 -16
  3. package/lib/cjs/async.js +1 -1
  4. package/lib/cjs/clipboard.js +1 -1
  5. package/lib/cjs/cookie.js +1 -1
  6. package/lib/cjs/date.js +1 -1
  7. package/lib/cjs/dom.js +1 -1
  8. package/lib/cjs/download.js +1 -1
  9. package/lib/cjs/easing.js +1 -1
  10. package/lib/cjs/file.js +1 -1
  11. package/lib/cjs/func.js +1 -1
  12. package/lib/cjs/index.js +6 -3
  13. package/lib/cjs/number.js +1 -1
  14. package/lib/cjs/object.js +1 -1
  15. package/lib/cjs/path.js +1 -1
  16. package/lib/cjs/qs.js +1 -1
  17. package/lib/cjs/random.js +1 -1
  18. package/lib/cjs/string.js +1 -1
  19. package/lib/cjs/tooltip.js +118 -0
  20. package/lib/cjs/type.js +1 -1
  21. package/lib/cjs/unique.js +1 -1
  22. package/lib/cjs/url.js +1 -1
  23. package/lib/cjs/watermark.js +1 -1
  24. package/lib/es/array.js +114 -15
  25. package/lib/es/async.js +1 -1
  26. package/lib/es/clipboard.js +1 -1
  27. package/lib/es/cookie.js +1 -1
  28. package/lib/es/date.js +1 -1
  29. package/lib/es/dom.js +1 -1
  30. package/lib/es/download.js +1 -1
  31. package/lib/es/easing.js +1 -1
  32. package/lib/es/file.js +1 -1
  33. package/lib/es/func.js +1 -1
  34. package/lib/es/index.js +3 -2
  35. package/lib/es/number.js +1 -1
  36. package/lib/es/object.js +1 -1
  37. package/lib/es/path.js +1 -1
  38. package/lib/es/qs.js +1 -1
  39. package/lib/es/random.js +1 -1
  40. package/lib/es/string.js +1 -1
  41. package/lib/es/tooltip.js +116 -0
  42. package/lib/es/type.js +1 -1
  43. package/lib/es/unique.js +1 -1
  44. package/lib/es/url.js +1 -1
  45. package/lib/es/watermark.js +1 -1
  46. package/lib/index.d.ts +77 -8
  47. package/lib/tsdoc-metadata.json +11 -0
  48. package/lib/umd/index.js +224 -16
  49. package/package.json +7 -6
package/README.md CHANGED
@@ -1,24 +1,121 @@
1
1
  [![Node.js CI](https://github.com/chandq/sculp-js/actions/workflows/node.js.yml/badge.svg)](https://github.com/chandq/sculp-js/actions/workflows/node.js.yml)
2
2
  [![sculp-js](https://img.shields.io/github/package-json/v/chandq/sculp-js?style=flat-square)](https://github.com/chandq/sculp-js)
3
- [![node](https://img.shields.io/badge/node-v12.0.0-blue)](https://nodejs.org/download/release/v12.0.0/)
3
+ [![node](https://img.shields.io/badge/node-v16.0.0-blue)](https://nodejs.org/download/release/v16.0.0/)
4
4
  [![node](https://img.shields.io/badge/language-typescript-orange.svg)](https://nodejs.org/download/release/v12.0.0/)
5
5
  [![license:MIT](https://img.shields.io/npm/l/vue.svg?sanitize=true)](https://github.com/chandq/sculp-js/blob/main/LICENSE.md)
6
6
  [![Downloads:?](https://img.shields.io/npm/dm/sculp-js.svg?sanitize=true)](https://npmcharts.com/compare/sculp-js?minimal=true)
7
+ [![codecov](https://codecov.io/gh/chandq/sculp-js/graph/badge.svg?token=VZ6TERPGI9)](https://codecov.io/gh/chandq/sculp-js)
7
8
 
8
9
  # sculp-js
9
10
 
10
- > TS + Rollup, 原生实现,不依赖任何第三方库,输出 esm、cjs、umd三种模块方式的产物
11
+ [API文档](https://chandq.github.io/sculp-js/)
11
12
 
12
- js 工具函数库, 包含类型判断模块:type, 数据处理模块:`array`、`object`、`string`、`number`,功能性模块:下载`download`、复制`clipboard`、`cookie`、日期`date`、qs、水印`watermark`, 文件处理模块:`file`,自定义悬浮提示模块: `tooltip`, dom处理模块:`dom`;
13
+ > TS + Rollup, native implementation, without relying on any third-party libraries, outputs products of three module modes: ESM, CJS, and UMD
14
+
15
+ ## Features
16
+
17
+ - Type
18
+
19
+ - isString
20
+ - isBoolean
21
+ - isNumber
22
+ - isUndefined
23
+ - isNull
24
+ - isPrimitive
25
+ - isObject
26
+ - typeIs
13
27
 
14
28
  - Array
15
29
 
16
30
  - arrayLike 判断类数组
17
- - arrayEach 可中断的数组遍历, 支持倒序
18
31
  - arrayEachAsync 异步遍历数组,可中断,支持倒序
19
- - arrayInsertBefore 改变数组元素位置
20
- - arrayRemove 数组删除指定元素
21
- - deepTraversal 深度优先遍历函数, 支持continue、break,可定制id、children
22
- - getTreeIds 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
32
+ - forEachDeep 深度优先遍历函数, 支持continue、break,可定制id、children
33
+ - searchTreeById 在树中找到 id 为某个值的节点,并返回上游的所有父级节点
34
+ - buildTree 根据 id 与 parentId 从对象数组中构建对应的树
35
+
36
+ - Object
37
+
38
+ - isPlainObject
39
+ - objectHas
40
+ - objectAssign 合并
41
+ - objectEach
42
+ - objectEachAsync
43
+ - objectGet
44
+
45
+ - Number
46
+
47
+ - formatNumber
48
+
49
+ - String
50
+
51
+ - stringCamelCase
52
+ - stringKebabCase
53
+ - getStrWidthPx
54
+
55
+ - Unique
56
+
57
+ - uniqueString
58
+ - uniqueNumber
59
+
60
+ - Date
61
+
62
+ - formatDate
63
+ - dateToStart
64
+ - dateToEnd
65
+ - calculateDate
66
+ - calculateDateTime
67
+
68
+ - Download
69
+
70
+ - downloadURL
71
+ - downloadHref
72
+ - downloadBlob
73
+ - downloadData
74
+
75
+ - File
76
+
77
+ - chooseLocalFile
78
+
79
+ - Dom
80
+
81
+ - hasClass
82
+ - addClass
83
+ - removeClass
84
+ - setStyle
85
+ - getStyle
86
+ - getComputedCssVal
87
+
88
+ - Watermark
89
+
90
+ - genCanvasWM
91
+
92
+ - Clipboard
93
+ - copyText
94
+
95
+ ## Install
96
+
97
+ ```js
98
+ npm i sculp-js
99
+ ```
100
+
101
+ ## Usage
102
+
103
+ ```js
104
+ import { forEachDeep } from 'sculp-js';
105
+
106
+ const tree = [
107
+ { id: 1, name: 'row1' },
108
+ {
109
+ id: 2,
110
+ name: 'row2',
111
+ children: [{ id: 21, name: 'row2-1' }]
112
+ },
113
+ { id: 3, name: 'row3' }
114
+ ];
23
115
 
24
- ## [API文档详情](https://chandq.github.io/sculp-js/)
116
+ const arr = [];
117
+ forEachDeep(tree, ({ id, name }) => {
118
+ arr.push(name);
119
+ });
120
+ // arr will be: ['row1', 'row2', 'row2-1', 'row3'];
121
+ ```
package/lib/cjs/array.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.0.1
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -25,26 +25,32 @@ function arrayLike(any) {
25
25
  return object.objectHas(any, 'length');
26
26
  }
27
27
  /**
28
- * 遍历数组,返回 false 中断遍历
28
+ * 遍历数组,返回 false 中断遍历(支持continue和break操作)
29
29
  *
30
30
  * @param {ArrayLike<V>} array
31
- * @param {(val: V, idx: number) => any} iterator
32
- * @param reverse {boolean} 是否倒序
31
+ * @param {(val: V, idx: number) => any} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
32
+ * @param {boolean} reverse 是否倒序
33
33
  * @returns {*}
34
34
  */
35
35
  function arrayEach(array, iterator, reverse = false) {
36
36
  if (reverse) {
37
37
  for (let idx = array.length - 1; idx >= 0; idx--) {
38
38
  const val = array[idx];
39
- if (iterator(val, idx, array) === false)
39
+ const re = iterator(val, idx, array);
40
+ if (re === false)
40
41
  break;
42
+ else if (re === true)
43
+ continue;
41
44
  }
42
45
  }
43
46
  else {
44
47
  for (let idx = 0; idx < array.length; idx++) {
45
48
  const val = array[idx];
46
- if (iterator(val, idx, array) === false)
49
+ const re = iterator(val, idx, array);
50
+ if (re === false)
47
51
  break;
52
+ else if (re === true)
53
+ continue;
48
54
  }
49
55
  }
50
56
  }
@@ -106,21 +112,25 @@ function arrayRemove(array, expect) {
106
112
  /**
107
113
  * 自定义深度优先遍历函数(支持continue和break操作)
108
114
  * @param {ArrayLike<V>} tree 树形数据
109
- * @param {Function} iterator 迭代函数
115
+ * @param {Function} iterator 迭代函数, 返回值为true时continue, 返回值为false时break
110
116
  * @param {string} children 定制子元素的key
111
117
  * @param {boolean} isReverse 是否反向遍历
112
118
  * @returns {*}
113
119
  */
114
- function deepTraversal(tree, iterator, children = 'children', isReverse = false) {
115
- let level = 0;
120
+ function forEachDeep(tree, iterator, children = 'children', isReverse = false) {
121
+ let level = 0, isBreak = false;
116
122
  const walk = (arr, parent) => {
117
123
  if (isReverse) {
118
124
  for (let i = arr.length - 1; i >= 0; i--) {
125
+ if (isBreak) {
126
+ break;
127
+ }
119
128
  const re = iterator(arr[i], i, tree, parent, level);
120
- if (re === 'break') {
129
+ if (re === false) {
130
+ isBreak = true;
121
131
  break;
122
132
  }
123
- else if (re === 'continue') {
133
+ else if (re === true) {
124
134
  continue;
125
135
  }
126
136
  // @ts-ignore
@@ -133,11 +143,15 @@ function deepTraversal(tree, iterator, children = 'children', isReverse = false)
133
143
  }
134
144
  else {
135
145
  for (let i = 0; i < arr.length; i++) {
146
+ if (isBreak) {
147
+ break;
148
+ }
136
149
  const re = iterator(arr[i], i, tree, parent, level);
137
- if (re === 'break') {
150
+ if (re === false) {
151
+ isBreak = true;
138
152
  break;
139
153
  }
140
- else if (re === 'continue') {
154
+ else if (re === true) {
141
155
  continue;
142
156
  }
143
157
  // @ts-ignore
@@ -159,7 +173,7 @@ function deepTraversal(tree, iterator, children = 'children', isReverse = false)
159
173
  * @param {ITreeConf} config - 迭代配置项
160
174
  * @returns {[IdLike[], ITreeItem<V>[]]} - 由parentId...childId, parentObject-childObject组成的二维数组
161
175
  */
162
- function getTreeIds(tree, nodeId, config) {
176
+ function searchTreeById(tree, nodeId, config) {
163
177
  const { children = 'children', id = 'id' } = config || {};
164
178
  const toFlatArray = (tree, parentId, parent) => {
165
179
  return tree.reduce((t, _) => {
@@ -184,11 +198,97 @@ function getTreeIds(tree, nodeId, config) {
184
198
  };
185
199
  return getIds(toFlatArray(tree));
186
200
  }
201
+ /**
202
+ * 使用迭代函数转换数组
203
+ * @param {T} array
204
+ * @param {Function} callback 迭代函数
205
+ * @return {Array}
206
+ */
207
+ function flatMap(array, callback) {
208
+ const result = [];
209
+ array.forEach((value, index) => {
210
+ result.push(...callback(value, index, array));
211
+ });
212
+ return result;
213
+ }
214
+ /**
215
+ * 根据 idProp 与 parentIdProp 从对象数组中构建对应的树
216
+ * 当 A[parentIdProp] === B[idProp] 时,对象A会被移动到对象B的children。
217
+ * 当一个对象的 parentIdProp 不与其他对象的 idProp 字段相等时,该对象被作为树的顶层节点
218
+ * @param {string} idProp 元素ID
219
+ * @param {string} parentIdProp 父元素ID
220
+ * @param {object[]} items 一维数组
221
+ * @returns {WithChildren<T>[]} 树
222
+ * @example
223
+ * const array = [
224
+ * { id: 'node-1', parent: 'root' },
225
+ * { id: 'node-2', parent: 'root' },
226
+ * { id: 'node-3', parent: 'node-2' },
227
+ * { id: 'node-4', parent: 'node-2' },
228
+ * { id: 'node-5', parent: 'node-4' },
229
+ * ]
230
+ * const tree = buildTree('id', 'parent', array)
231
+ * expect(tree).toEqual([
232
+ * { id: 'node-1', parent: 'root' },
233
+ * {
234
+ * id: 'node-2',
235
+ * parent: 'root',
236
+ * children: [
237
+ * { id: 'node-3', parent: 'node-2' },
238
+ * {
239
+ * id: 'node-4',
240
+ * parent: 'node-2',
241
+ * children: [{ id: 'node-5', parent: 'node-4' }],
242
+ * },
243
+ * ],
244
+ * },
245
+ * ])
246
+ */
247
+ function buildTree(idProp, parentIdProp, items) {
248
+ const wrapperMap = new Map();
249
+ const ensure = (id) => {
250
+ if (wrapperMap.has(id)) {
251
+ return wrapperMap.get(id);
252
+ }
253
+ //@ts-ignore
254
+ const wrapper = { id, parent: null, item: null, children: [] };
255
+ wrapperMap.set(id, wrapper);
256
+ return wrapper;
257
+ };
258
+ for (const item of items) {
259
+ const parentWrapper = ensure(item[parentIdProp]);
260
+ const itemWrapper = ensure(item[idProp]);
261
+ //@ts-ignore
262
+ itemWrapper.parent = parentWrapper;
263
+ //@ts-ignore
264
+ parentWrapper.children.push(itemWrapper);
265
+ //@ts-ignore
266
+ itemWrapper.item = item;
267
+ }
268
+ const topLevelWrappers = flatMap(Array.from(wrapperMap.values()).filter(wrapper => wrapper.parent === null), wrapper => wrapper.children);
269
+ return unwrapRecursively(topLevelWrappers);
270
+ function unwrapRecursively(wrapperArray) {
271
+ const result = [];
272
+ for (const wrapper of wrapperArray) {
273
+ if (wrapper.children.length === 0) {
274
+ result.push(wrapper.item);
275
+ }
276
+ else {
277
+ result.push({
278
+ ...wrapper.item,
279
+ children: unwrapRecursively(wrapper.children)
280
+ });
281
+ }
282
+ }
283
+ return result;
284
+ }
285
+ }
187
286
 
188
287
  exports.arrayEach = arrayEach;
189
288
  exports.arrayEachAsync = arrayEachAsync;
190
289
  exports.arrayInsertBefore = arrayInsertBefore;
191
290
  exports.arrayLike = arrayLike;
192
291
  exports.arrayRemove = arrayRemove;
193
- exports.deepTraversal = deepTraversal;
194
- exports.getTreeIds = getTreeIds;
292
+ exports.buildTree = buildTree;
293
+ exports.forEachDeep = forEachDeep;
294
+ exports.searchTreeById = searchTreeById;
package/lib/cjs/async.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.0.1
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.0.1
3
3
  * (c) 2023-2023 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.0.0
2
+ * sculp-js v1.0.1
3
3
  * (c) 2023-2023 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.0.0
2
+ * sculp-js v1.0.1
3
3
  * (c) 2023-2023 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.0.0
2
+ * sculp-js v1.0.1
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.0.1
3
3
  * (c) 2023-2023 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.0.0
2
+ * sculp-js v1.0.1
3
3
  * (c) 2023-2023 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.0.0
2
+ * sculp-js v1.0.1
3
3
  * (c) 2023-2023 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.0.0
2
+ * sculp-js v1.0.1
3
3
  * (c) 2023-2023 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.0.0
2
+ * sculp-js v1.0.1
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -25,6 +25,7 @@ var func = require('./func.js');
25
25
  var random = require('./random.js');
26
26
  var number = require('./number.js');
27
27
  var unique = require('./unique.js');
28
+ var tooltip = require('./tooltip.js');
28
29
 
29
30
 
30
31
 
@@ -33,8 +34,9 @@ exports.arrayEachAsync = array.arrayEachAsync;
33
34
  exports.arrayInsertBefore = array.arrayInsertBefore;
34
35
  exports.arrayLike = array.arrayLike;
35
36
  exports.arrayRemove = array.arrayRemove;
36
- exports.deepTraversal = array.deepTraversal;
37
- exports.getTreeIds = array.getTreeIds;
37
+ exports.buildTree = array.buildTree;
38
+ exports.forEachDeep = array.forEachDeep;
39
+ exports.searchTreeById = array.searchTreeById;
38
40
  exports.copyText = clipboard.copyText;
39
41
  exports.cookieDel = cookie.cookieDel;
40
42
  exports.cookieGet = cookie.cookieGet;
@@ -125,3 +127,4 @@ exports.numberToHex = number.numberToHex;
125
127
  exports.UNIQUE_NUMBER_SAFE_LENGTH = unique.UNIQUE_NUMBER_SAFE_LENGTH;
126
128
  exports.uniqueNumber = unique.uniqueNumber;
127
129
  exports.uniqueString = unique.uniqueString;
130
+ exports.tooltipEvent = tooltip.tooltipEvent;
package/lib/cjs/number.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.0.1
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
package/lib/cjs/object.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.0.1
3
3
  * (c) 2023-2023 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.0.0
2
+ * sculp-js v1.0.1
3
3
  * (c) 2023-2023 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.0.0
2
+ * sculp-js v1.0.1
3
3
  * (c) 2023-2023 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.0.0
2
+ * sculp-js v1.0.1
3
3
  * (c) 2023-2023 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.0.0
2
+ * sculp-js v1.0.1
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -0,0 +1,118 @@
1
+ /*!
2
+ * sculp-js v1.0.1
3
+ * (c) 2023-2023 chandq
4
+ * Released under the MIT License.
5
+ */
6
+
7
+ 'use strict';
8
+
9
+ var string = require('./string.js');
10
+
11
+ /**
12
+ * @title tooltip
13
+ * @Desc 自定义的tooltip方法, 支持拖动悬浮提示
14
+ * Created by chendeqiao on 2017/5/8.
15
+ * @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>
18
+ */
19
+ /**
20
+ * 自定义title提示功能的mouseenter事件句柄
21
+ * @param {ITooltipParams} param1
22
+ * @returns {*}
23
+ */
24
+ function handleMouseEnter({ rootElId = '#root', title, event }) {
25
+ try {
26
+ const $rootEl = document.querySelector(rootElId);
27
+ console.assert($rootEl !== null, `未找到id为 ${rootElId} 的dom元素`);
28
+ let $customTitle = null;
29
+ // 动态创建class样式,并加入到head中
30
+ if (!document.querySelector('.tooltip-inner1494304949567')) {
31
+ const tooltipWrapperClass = document.createElement('style');
32
+ tooltipWrapperClass.type = 'text/css';
33
+ tooltipWrapperClass.innerHTML = `
34
+ .tooltip-inner1494304949567 {
35
+ max-width: 250px;
36
+ padding: 3px 8px;
37
+ color: #fff;
38
+ text-decoration: none;
39
+ border-radius: 4px;
40
+ text-align: left;
41
+ }
42
+ `;
43
+ document.querySelector('head').appendChild(tooltipWrapperClass);
44
+ }
45
+ if (document.querySelector('#customTitle1494304949567')) {
46
+ $customTitle = document.querySelector('#customTitle1494304949567');
47
+ mouseenter($customTitle, title, event);
48
+ }
49
+ else {
50
+ const $contentContainer = document.createElement('div');
51
+ $contentContainer.className = 'customTitle';
52
+ $contentContainer.id = 'customTitle1494304949567';
53
+ $contentContainer.className = 'tooltip';
54
+ $contentContainer.style.cssText = 'z-index: 99999999; visibility: hidden;';
55
+ $contentContainer.innerHTML =
56
+ '<div class="tooltip-inner1494304949567" style="word-wrap: break-word; max-width: 44px;">皮肤</div>';
57
+ $rootEl.appendChild($contentContainer);
58
+ $customTitle = document.querySelector('#customTitle1494304949567');
59
+ if (title) {
60
+ //判断div显示的内容是否为空
61
+ mouseenter($customTitle, title, event);
62
+ $customTitle.style.visibility = 'visible';
63
+ }
64
+ }
65
+ }
66
+ catch (e) {
67
+ console.error(e.message);
68
+ }
69
+ }
70
+ /**
71
+ * 提示文案dom渲染的处理函数
72
+ * @param {HTMLDivElement} customTitle
73
+ * @param {string} title 提示的字符串
74
+ * @param {PointerEvent} e 事件对象
75
+ * @returns {*}
76
+ */
77
+ function mouseenter($customTitle, title, e) {
78
+ let diffValueX = 200 + 50; //默认设置弹出div的宽度为250px
79
+ let x = 13;
80
+ const y = 23;
81
+ const $contentEle = $customTitle.children[0];
82
+ if (string.getStrWidthPx(title, 12) < 180 + 50) {
83
+ //【弹出div自适应字符串宽度】若显示的字符串占用宽度小于180,则设置弹出div的宽度为“符串占用宽度”+20
84
+ $contentEle.style.maxWidth = string.getStrWidthPx(title, 12) + 20 + 50 + 'px';
85
+ diffValueX = e.clientX + (string.getStrWidthPx(title, 12) + 50) - document.body.offsetWidth;
86
+ }
87
+ else {
88
+ $contentEle.style.maxWidth = '250px';
89
+ diffValueX = e.clientX + 230 - document.body.offsetWidth; //计算div水平方向显示的内容超出屏幕多少宽度
90
+ }
91
+ $contentEle.innerHTML = title; //html方法可解析内容中换行标签,text方法不能
92
+ if (diffValueX > 0) {
93
+ //水平方向超出可见区域时
94
+ x -= diffValueX;
95
+ }
96
+ $customTitle.style.top = e.clientY + y + 'px';
97
+ $customTitle.style.left = e.clientX + x + 'px';
98
+ $customTitle.style.maxWidth = '250px';
99
+ const diffValueY = $customTitle.getBoundingClientRect().top + $contentEle.offsetHeight - document.body.offsetHeight;
100
+ if (diffValueY > 0) {
101
+ //垂直方向超出可见区域时
102
+ $customTitle.style.top = e.clientY - diffValueY + 'px';
103
+ }
104
+ }
105
+ /**
106
+ * 移除提示文案dom的事件句柄
107
+ * @param {string} rootElId
108
+ * @returns {*}
109
+ */
110
+ function handleMouseLeave(rootElId = '#root') {
111
+ const rootEl = document.querySelector(rootElId), titleEl = document.querySelector('#customTitle1494304949567');
112
+ if (rootEl && titleEl) {
113
+ rootEl.removeChild(titleEl);
114
+ }
115
+ }
116
+ const tooltipEvent = { handleMouseEnter, handleMouseLeave };
117
+
118
+ exports.tooltipEvent = tooltipEvent;
package/lib/cjs/type.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.0.1
3
3
  * (c) 2023-2023 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.0.0
2
+ * sculp-js v1.0.1
3
3
  * (c) 2023-2023 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.0.0
2
+ * sculp-js v1.0.1
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * sculp-js v1.0.0
2
+ * sculp-js v1.0.1
3
3
  * (c) 2023-2023 chandq
4
4
  * Released under the MIT License.
5
5
  */