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