@whitesev/domutils 1.6.8 → 1.7.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 (55) hide show
  1. package/dist/index.amd.js +1928 -1047
  2. package/dist/index.amd.js.map +1 -1
  3. package/dist/index.amd.min.js +2 -0
  4. package/dist/index.amd.min.js.map +1 -0
  5. package/dist/index.cjs.js +1928 -1047
  6. package/dist/index.cjs.js.map +1 -1
  7. package/dist/index.cjs.min.js +2 -0
  8. package/dist/index.cjs.min.js.map +1 -0
  9. package/dist/index.esm.js +1928 -1047
  10. package/dist/index.esm.js.map +1 -1
  11. package/dist/index.esm.min.js +2 -0
  12. package/dist/index.esm.min.js.map +1 -0
  13. package/dist/index.iife.js +1928 -1047
  14. package/dist/index.iife.js.map +1 -1
  15. package/dist/index.iife.min.js +2 -0
  16. package/dist/index.iife.min.js.map +1 -0
  17. package/dist/index.system.js +1928 -1047
  18. package/dist/index.system.js.map +1 -1
  19. package/dist/index.system.min.js +2 -0
  20. package/dist/index.system.min.js.map +1 -0
  21. package/dist/index.umd.js +1928 -1047
  22. package/dist/index.umd.js.map +1 -1
  23. package/dist/index.umd.min.js +2 -0
  24. package/dist/index.umd.min.js.map +1 -0
  25. package/dist/types/index.d.ts +1 -1
  26. package/dist/types/src/{DOMUtilsCommonUtils.d.ts → CommonUtils.d.ts} +20 -8
  27. package/dist/types/src/ElementAnimate.d.ts +89 -0
  28. package/dist/types/src/{DOMUtilsEvent.d.ts → ElementEvent.d.ts} +19 -84
  29. package/dist/types/src/ElementHandler.d.ts +17 -0
  30. package/dist/types/src/ElementSelector.d.ts +96 -0
  31. package/dist/types/src/ElementWait.d.ts +278 -0
  32. package/dist/types/src/GlobalData.d.ts +4 -0
  33. package/dist/types/src/{DOMUtilsOriginPrototype.d.ts → OriginPrototype.d.ts} +1 -2
  34. package/dist/types/src/Utils.d.ts +68 -0
  35. package/dist/types/src/{DOMUtils.d.ts → index.d.ts} +157 -177
  36. package/dist/types/src/types/env.d.ts +9 -0
  37. package/dist/types/src/types/global.d.ts +0 -2
  38. package/dist/types/src/types/gm.d.ts +0 -4
  39. package/index.ts +1 -1
  40. package/package.json +6 -2
  41. package/src/{DOMUtilsCommonUtils.ts → CommonUtils.ts} +25 -11
  42. package/src/ElementAnimate.ts +290 -0
  43. package/src/{DOMUtilsEvent.ts → ElementEvent.ts} +166 -361
  44. package/src/ElementHandler.ts +43 -0
  45. package/src/ElementSelector.ts +260 -0
  46. package/src/ElementWait.ts +699 -0
  47. package/src/GlobalData.ts +5 -0
  48. package/src/{DOMUtilsOriginPrototype.ts → OriginPrototype.ts} +1 -3
  49. package/src/Utils.ts +386 -0
  50. package/src/{DOMUtils.ts → index.ts} +678 -757
  51. package/src/types/env.d.ts +9 -0
  52. package/src/types/global.d.ts +0 -2
  53. package/src/types/gm.d.ts +0 -4
  54. package/dist/types/src/DOMUtilsData.d.ts +0 -5
  55. package/src/DOMUtilsData.ts +0 -7
@@ -0,0 +1,9 @@
1
+ declare const jQuery: any;
2
+ declare interface Window {
3
+ webkitMutationObserver: MutationObserver;
4
+ MozMutationObserver: MutationObserver;
5
+ jQuery: any;
6
+ // eslint-disable-next-line @typescript-eslint/consistent-type-imports
7
+ DOMUtils: typeof import("./../index").DOMUtils;
8
+ trustedTypes: any;
9
+ }
@@ -1,3 +1 @@
1
- export type ParseHTMLReturnType<T1, T2> = T1 extends true ? (T2 extends true ? Document : HTMLElement) : HTMLElement;
2
-
3
1
  export type DOMUtilsTargetElementType = HTMLElement | string | NodeList | HTMLElement[];
@@ -1,6 +1,2 @@
1
1
  /** GM中的unsafeWindow */
2
2
  declare const unsafeWindow: Window & typeof globalThis;
3
-
4
- declare interface Window {
5
- trustedTypes: any;
6
- }
package/index.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { DOMUtils } from "./src/DOMUtils";
1
+ import { DOMUtils } from "./src";
2
2
 
3
3
  export default DOMUtils;
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "$schema": "https://json.schemastore.org/package.json",
3
3
  "name": "@whitesev/domutils",
4
- "version": "1.6.8",
4
+ "version": "1.7.0",
5
5
  "type": "module",
6
- "description": "使用js重新对jQuery的部分函数进行了仿写",
6
+ "description": "适合在浏览器中操作DOM的常用工具类",
7
7
  "main": "dist/index.cjs.js",
8
8
  "module": "dist/index.esm.js",
9
9
  "types": "dist/types/index.d.ts",
@@ -39,6 +39,7 @@
39
39
  "@rollup/plugin-commonjs": "^28.0.6",
40
40
  "@rollup/plugin-json": "^6.1.0",
41
41
  "@rollup/plugin-node-resolve": "^16.0.1",
42
+ "@rollup/plugin-terser": "^0.4.4",
42
43
  "@rollup/plugin-typescript": "^12.1.2",
43
44
  "browserslist": "^4.26.2",
44
45
  "caniuse-lite": "^1.0.30001743",
@@ -54,6 +55,9 @@
54
55
  "typescript-eslint": "^8.44.0"
55
56
  },
56
57
  "scripts": {
58
+ "install": "pnpm install --ignore-workspace",
59
+ "outdated": "pnpm outdated --ignore-workspace",
60
+ "update": "pnpm update --latest --ignore-workspace",
57
61
  "lint": "eslint .",
58
62
  "lint:fix": "eslint . --fix",
59
63
  "format": "prettier . --write",
@@ -7,7 +7,7 @@ import {
7
7
  } from "worker-timers";
8
8
 
9
9
  /** 通用工具类 */
10
- export const DOMUtilsCommonUtils = {
10
+ export const CommonUtils = {
11
11
  windowApi: new WindowApi({
12
12
  document: document,
13
13
  window: window,
@@ -19,15 +19,16 @@ export const DOMUtilsCommonUtils = {
19
19
  }),
20
20
  /**
21
21
  * 判断元素是否已显示或已连接
22
- * @param element
22
+ * @param $el
23
23
  */
24
- isShow(element: HTMLElement) {
25
- return Boolean(element.getClientRects().length);
24
+ isShow($el: HTMLElement) {
25
+ return Boolean($el.getClientRects().length);
26
26
  },
27
27
  /**
28
- * 获取安全的html
28
+ * 创建安全的html
29
+ * @param text 字符串
29
30
  */
30
- getSafeHTML(text: string) {
31
+ createSafeHTML(text: string) {
31
32
  if (window.trustedTypes) {
32
33
  const policy = window.trustedTypes.createPolicy("safe-innerHTML", {
33
34
  createHTML: (html: string) => html,
@@ -44,14 +45,14 @@ export const DOMUtilsCommonUtils = {
44
45
  */
45
46
  setSafeHTML($el: HTMLElement, text: string) {
46
47
  // 创建 TrustedHTML 策略(需 CSP 允许)
47
- $el.innerHTML = this.getSafeHTML(text);
48
+ $el.innerHTML = this.createSafeHTML(text);
48
49
  },
49
50
  /**
50
- * 用于显示元素并获取它的高度宽度等其它属性
51
- * @param element
51
+ * 用于强制显示元素并获取它的高度宽度等其它属性
52
+ * @param $el
52
53
  */
53
- showElement(element: HTMLElement) {
54
- const dupNode = element.cloneNode(true) as HTMLElement;
54
+ forceShow($el: HTMLElement) {
55
+ const dupNode = $el.cloneNode(true) as HTMLElement;
55
56
  dupNode.setAttribute("style", "visibility: hidden !important;display:block !important;");
56
57
  this.windowApi.document.documentElement.appendChild(dupNode);
57
58
  return {
@@ -125,6 +126,19 @@ export const DOMUtilsCommonUtils = {
125
126
  }
126
127
  return true;
127
128
  },
129
+ /**
130
+ * 判断对象是否是元素
131
+ * @param $el
132
+ * @returns
133
+ * + true 是元素
134
+ * + false 不是元素
135
+ * @example
136
+ * DOMUtilsCommonUtils.isDOM(document.querySelector("a"))
137
+ * > true
138
+ */
139
+ isDOM($el: any): boolean {
140
+ return $el instanceof Node;
141
+ },
128
142
  /**
129
143
  * 删除对象上的属性
130
144
  * @param target
@@ -0,0 +1,290 @@
1
+ import { CommonUtils } from "./CommonUtils";
2
+ import { elementSelector } from "./ElementSelector";
3
+ import type { DOMUtilsTargetElementType } from "./types/global";
4
+ import type { WindowApiOption } from "./types/WindowApi";
5
+ import { ElementWait } from "./ElementWait";
6
+ import { WindowApi } from "./WindowApi";
7
+
8
+ class ElementAnimate extends ElementWait {
9
+ windowApi: typeof WindowApi.prototype;
10
+ constructor(windowApiOption?: WindowApiOption) {
11
+ super(windowApiOption);
12
+ this.windowApi = new WindowApi(windowApiOption);
13
+ }
14
+ /**
15
+ * 在一定时间内改变元素的样式属性,实现动画效果
16
+ * @param element 需要进行动画的元素
17
+ * @param styles 动画结束时元素的样式属性
18
+ * @param duration 动画持续时间,单位为毫秒
19
+ * @param callback 动画结束后执行的函数
20
+ * @example
21
+ * // 监听元素a.xx的从显示变为隐藏
22
+ * DOMUtils.animate(document.querySelector("a.xx"),{ top:100},1000,function(){
23
+ * console.log("已往上位移100px")
24
+ * })
25
+ */
26
+ animate(
27
+ element: DOMUtilsTargetElementType,
28
+ styles: CSSStyleDeclaration,
29
+ duration: number = 1000,
30
+ callback: (() => void) | undefined | null = null
31
+ ) {
32
+ const context = this;
33
+ if (typeof element === "string") {
34
+ element = elementSelector.selectorAll(element);
35
+ }
36
+ if (element == null) {
37
+ return;
38
+ }
39
+ if (CommonUtils.isNodeList(element)) {
40
+ // 设置
41
+ element.forEach(($ele) => {
42
+ context.animate($ele as HTMLElement, styles, duration, callback);
43
+ });
44
+ return;
45
+ }
46
+ if (typeof duration !== "number" || duration <= 0) {
47
+ throw new TypeError("duration must be a positive number");
48
+ }
49
+ if (typeof callback !== "function" && callback !== void 0) {
50
+ throw new TypeError("callback must be a function or null");
51
+ }
52
+ if (typeof styles !== "object" || styles === void 0) {
53
+ throw new TypeError("styles must be an object");
54
+ }
55
+ if (Object.keys(styles).length === 0) {
56
+ throw new Error("styles must contain at least one property");
57
+ }
58
+ const start = performance.now();
59
+ const from: {
60
+ [prop: string]: any;
61
+ } = {};
62
+ const to: {
63
+ [prop: string]: any;
64
+ } = {};
65
+ for (const prop in styles) {
66
+ from[prop] = element.style[prop] || context.windowApi.globalThis.getComputedStyle(element)[prop];
67
+ to[prop] = styles[prop];
68
+ }
69
+ const timer = CommonUtils.setInterval(function () {
70
+ const timePassed = performance.now() - start;
71
+ let progress = timePassed / duration;
72
+ if (progress > 1) {
73
+ progress = 1;
74
+ }
75
+ for (const prop in styles) {
76
+ element.style[prop] = from[prop] + (to[prop] - from[prop]) * progress + "px";
77
+ }
78
+ if (progress === 1) {
79
+ CommonUtils.clearInterval(timer);
80
+ if (callback) {
81
+ callback();
82
+ }
83
+ }
84
+ }, 10);
85
+ }
86
+ /**
87
+ * 显示元素
88
+ * @param target 当前元素
89
+ * @param checkVisiblie 是否检测元素是否显示
90
+ * + true (默认)如果检测到还未显示,则强制使用display: unset !important;
91
+ * + false 不检测,直接设置display属性为空
92
+ * @example
93
+ * // 显示a.xx元素
94
+ * DOMUtils.show(document.querySelector("a.xx"))
95
+ * DOMUtils.show(document.querySelectorAll("a.xx"))
96
+ * DOMUtils.show("a.xx")
97
+ */
98
+ show(target: DOMUtilsTargetElementType, checkVisiblie: boolean = true) {
99
+ const context = this;
100
+ if (target == null) {
101
+ return;
102
+ }
103
+ if (typeof target === "string") {
104
+ target = elementSelector.selectorAll(target);
105
+ }
106
+ if (target instanceof NodeList || target instanceof Array) {
107
+ target = target as HTMLElement[];
108
+ for (const element of target) {
109
+ context.show(element, checkVisiblie);
110
+ }
111
+ } else {
112
+ target = target as HTMLElement;
113
+ target.style.display = "";
114
+ if (checkVisiblie) {
115
+ if (!CommonUtils.isShow(target)) {
116
+ /* 仍然是不显示,尝试使用强覆盖 */
117
+ target.style.setProperty("display", "unset", "important");
118
+ }
119
+ }
120
+ }
121
+ }
122
+ /**
123
+ * 隐藏元素
124
+ * @param target 当前元素
125
+ * @param checkVisiblie 是否检测元素是否显示
126
+ * + true (默认)如果检测到显示,则强制使用display: none !important;
127
+ * + false 不检测,直接设置display属性为none
128
+ * @example
129
+ * // 隐藏a.xx元素
130
+ * DOMUtils.hide(document.querySelector("a.xx"))
131
+ * DOMUtils.hide(document.querySelectorAll("a.xx"))
132
+ * DOMUtils.hide("a.xx")
133
+ */
134
+ hide(target: DOMUtilsTargetElementType, checkVisiblie: boolean = true) {
135
+ const context = this;
136
+ if (target == null) {
137
+ return;
138
+ }
139
+ if (typeof target === "string") {
140
+ target = elementSelector.selectorAll(target);
141
+ }
142
+ if (target instanceof NodeList || target instanceof Array) {
143
+ target = target as HTMLElement[];
144
+ for (const element of target) {
145
+ context.hide(element, checkVisiblie);
146
+ }
147
+ } else {
148
+ target = target as HTMLElement;
149
+ target.style.display = "none";
150
+ if (checkVisiblie) {
151
+ if (CommonUtils.isShow(target)) {
152
+ /* 仍然是显示,尝试使用强覆盖 */
153
+ target.style.setProperty("display", "none", "important");
154
+ }
155
+ }
156
+ }
157
+ }
158
+ /**
159
+ * 淡入元素
160
+ * @param element 当前元素
161
+ * @param duration 动画持续时间(毫秒),默认400毫秒
162
+ * @param callback 动画结束的回调
163
+ * @example
164
+ * // 元素a.xx淡入
165
+ * DOMUtils.fadeIn(document.querySelector("a.xx"),2500,()=>{
166
+ * console.log("淡入完毕");
167
+ * })
168
+ * DOMUtils.fadeIn("a.xx",undefined,()=>{
169
+ * console.log("淡入完毕");
170
+ * })
171
+ */
172
+ fadeIn(element: DOMUtilsTargetElementType, duration: number = 400, callback?: () => void) {
173
+ if (element == null) {
174
+ return;
175
+ }
176
+ const context = this;
177
+ if (typeof element === "string") {
178
+ element = elementSelector.selectorAll(element);
179
+ }
180
+ if (CommonUtils.isNodeList(element)) {
181
+ // 设置
182
+ element.forEach(($ele) => {
183
+ context.fadeIn($ele as HTMLElement, duration, callback);
184
+ });
185
+ return;
186
+ }
187
+ element.style.opacity = "0";
188
+ element.style.display = "";
189
+ let start: number = null as any;
190
+ let timer: number = null as any;
191
+ function step(timestamp: number) {
192
+ if (!start) start = timestamp;
193
+ const progress = timestamp - start;
194
+ element = element as HTMLElement;
195
+ element.style.opacity = Math.min(progress / duration, 1).toString();
196
+ if (progress < duration) {
197
+ context.windowApi.window.requestAnimationFrame(step);
198
+ } else {
199
+ if (callback && typeof callback === "function") {
200
+ callback();
201
+ }
202
+ context.windowApi.window.cancelAnimationFrame(timer);
203
+ }
204
+ }
205
+ timer = context.windowApi.window.requestAnimationFrame(step);
206
+ }
207
+ /**
208
+ * 淡出元素
209
+ * @param element 当前元素
210
+ * @param duration 动画持续时间(毫秒),默认400毫秒
211
+ * @param callback 动画结束的回调
212
+ * @example
213
+ * // 元素a.xx淡出
214
+ * DOMUtils.fadeOut(document.querySelector("a.xx"),2500,()=>{
215
+ * console.log("淡出完毕");
216
+ * })
217
+ * DOMUtils.fadeOut("a.xx",undefined,()=>{
218
+ * console.log("淡出完毕");
219
+ * })
220
+ */
221
+ fadeOut(element: DOMUtilsTargetElementType, duration: number = 400, callback?: () => void) {
222
+ const context = this;
223
+ if (element == null) {
224
+ return;
225
+ }
226
+ if (typeof element === "string") {
227
+ element = elementSelector.selectorAll(element);
228
+ }
229
+ if (CommonUtils.isNodeList(element)) {
230
+ // 设置
231
+ element.forEach(($ele) => {
232
+ context.fadeOut($ele as HTMLElement, duration, callback);
233
+ });
234
+ return;
235
+ }
236
+ element.style.opacity = "1";
237
+ let start: number = null as any;
238
+ let timer: number = null as any;
239
+ function step(timestamp: number) {
240
+ if (!start) start = timestamp;
241
+ const progress = timestamp - start;
242
+ element = element as HTMLElement;
243
+ element.style.opacity = Math.max(1 - progress / duration, 0).toString();
244
+ if (progress < duration) {
245
+ context.windowApi.window.requestAnimationFrame(step);
246
+ } else {
247
+ element.style.display = "none";
248
+ if (typeof callback === "function") {
249
+ callback();
250
+ }
251
+ context.windowApi.window.cancelAnimationFrame(timer);
252
+ }
253
+ }
254
+ timer = context.windowApi.window.requestAnimationFrame(step);
255
+ }
256
+ /**
257
+ * 切换元素的显示和隐藏状态
258
+ * @param element 当前元素
259
+ * @param checkVisiblie 是否检测元素是否显示
260
+ * @example
261
+ * // 如果元素a.xx当前是隐藏,则显示,如果是显示,则隐藏
262
+ * DOMUtils.toggle(document.querySelector("a.xx"))
263
+ * DOMUtils.toggle("a.xx")
264
+ */
265
+ toggle(element: DOMUtilsTargetElementType, checkVisiblie?: boolean) {
266
+ const context = this;
267
+ if (typeof element === "string") {
268
+ element = elementSelector.selectorAll(element);
269
+ }
270
+ if (element == null) {
271
+ return;
272
+ }
273
+ if (CommonUtils.isNodeList(element)) {
274
+ // 设置
275
+ element.forEach(($ele) => {
276
+ context.toggle($ele as HTMLElement);
277
+ });
278
+ return;
279
+ }
280
+ if (context.windowApi.globalThis.getComputedStyle(element).getPropertyValue("display") === "none") {
281
+ context.show(element, checkVisiblie);
282
+ } else {
283
+ context.hide(element, checkVisiblie);
284
+ }
285
+ }
286
+ }
287
+
288
+ const elementAnimate = new ElementAnimate();
289
+
290
+ export { elementAnimate, ElementAnimate };