@whitesev/domutils 1.1.5 → 1.3.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.
@@ -0,0 +1,111 @@
1
+ import { UtilsWindowApiOption } from "./WindowApi";
2
+
3
+ /** 通用工具类 */
4
+ const DOMUtilsCommonUtils = {
5
+ windowApi: {
6
+ window: window,
7
+ document: document,
8
+ } as UtilsWindowApiOption,
9
+ /**
10
+ * 判断元素是否已显示或已连接
11
+ * @param element
12
+ */
13
+ isShow(element: HTMLElement) {
14
+ return Boolean(element.getClientRects().length);
15
+ },
16
+ /**
17
+ * 用于显示元素并获取它的高度宽度等其它属性
18
+ * @param element
19
+ */
20
+ showElement(element: HTMLElement) {
21
+ let dupNode = element.cloneNode(true) as HTMLElement;
22
+ dupNode.setAttribute(
23
+ "style",
24
+ "visibility: hidden !important;display:block !important;"
25
+ );
26
+ this.windowApi.document.documentElement.appendChild(dupNode);
27
+ return {
28
+ /**
29
+ * 恢复修改的style
30
+ */
31
+ recovery() {
32
+ dupNode.remove();
33
+ },
34
+ };
35
+ },
36
+ /**
37
+ * 获取元素上的Float格式的属性px
38
+ * @param element
39
+ * @param styleName style名
40
+ */
41
+ getStyleValue(element: HTMLElement | CSSStyleDeclaration, styleName: string) {
42
+ let view = null;
43
+ let styles = null;
44
+ if (element instanceof CSSStyleDeclaration) {
45
+ /* 直接就获取了style属性 */
46
+ styles = element;
47
+ } else {
48
+ view = element.ownerDocument.defaultView;
49
+ if (!view || !view.opener) {
50
+ view = window;
51
+ }
52
+ styles = view.getComputedStyle(element);
53
+ }
54
+ let value = parseFloat(styles[styleName as any]);
55
+ if (isNaN(value)) {
56
+ return 0;
57
+ } else {
58
+ return value;
59
+ }
60
+ },
61
+ /**
62
+ * 判断是否是window,例如window、self、globalThis
63
+ * @param target
64
+ */
65
+ isWin(target: any) {
66
+ if (typeof target !== "object") {
67
+ return false;
68
+ }
69
+ if (target instanceof Node) {
70
+ return false;
71
+ }
72
+ if (target === globalThis) {
73
+ return true;
74
+ }
75
+ if (target === window) {
76
+ return true;
77
+ }
78
+ if (target === self) {
79
+ return true;
80
+ }
81
+ if (target === globalThis) {
82
+ return true;
83
+ }
84
+ if (target === window) {
85
+ return true;
86
+ }
87
+ if (target === self) {
88
+ return true;
89
+ }
90
+ if (typeof unsafeWindow !== "undefined" && target === unsafeWindow) {
91
+ return true;
92
+ }
93
+ if (target?.Math?.toString() !== "[object Math]") {
94
+ return false;
95
+ }
96
+ return true;
97
+ },
98
+ /**
99
+ * 删除对象上的属性
100
+ * @param target
101
+ * @param propName
102
+ */
103
+ delete(target: any, propName: any) {
104
+ if (typeof Reflect === "object" && Reflect.deleteProperty) {
105
+ Reflect.deleteProperty(target, propName);
106
+ } else {
107
+ delete target[propName];
108
+ }
109
+ },
110
+ };
111
+ export { DOMUtilsCommonUtils };
@@ -0,0 +1,9 @@
1
+ /* 数据 */
2
+ const DOMUtilsData = {
3
+ /** .on绑定的事件 */
4
+ SymbolEvents: Symbol(
5
+ "events_" + (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
6
+ ),
7
+ };
8
+
9
+ export { DOMUtilsData };