@whitesev/domutils 1.0.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.
- package/dist/index.cjs.js +1677 -0
- package/dist/index.cjs.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.esm.js +1675 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.umd.js +1683 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/src/CommonDOMUtils.d.ts +36 -0
- package/dist/src/Core.d.ts +197 -0
- package/dist/src/DOMUtils.d.ts +877 -0
- package/dist/src/Data.d.ts +5 -0
- package/dist/src/OriginPrototype.d.ts +6 -0
- package/global.d.ts +1 -0
- package/index.ts +3 -0
- package/package.json +34 -0
- package/rollup.config.js +28 -0
- package/src/CommonDOMUtils.ts +96 -0
- package/src/Core.ts +224 -0
- package/src/DOMUtils.ts +2504 -0
- package/src/Data.ts +9 -0
- package/src/OriginPrototype.ts +7 -0
- package/tsconfig.json +31 -0
package/global.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare var unsafeWindow: Window & typeof globalThis;
|
package/index.ts
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@whitesev/domutils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "使用js重新对jQuery的部分函数进行了仿写",
|
|
5
|
+
"main": "dist/index.cjs.js",
|
|
6
|
+
"module": "dist/node/index.esm.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"jsdelivr": "dist/index.umd.js",
|
|
9
|
+
"exports": {
|
|
10
|
+
"./package.json": "./package.json",
|
|
11
|
+
".": {
|
|
12
|
+
"import": "./dist/index.esm.js",
|
|
13
|
+
"require": "./dist/index.cjs.js",
|
|
14
|
+
"types": "./dist/index.d.ts"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"build": "rollup --config"
|
|
19
|
+
},
|
|
20
|
+
"keywords": [
|
|
21
|
+
"typescript"
|
|
22
|
+
],
|
|
23
|
+
"author": "WhiteSev",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"dependencies": {
|
|
26
|
+
"@rollup/plugin-commonjs": "^25.0.8",
|
|
27
|
+
"@rollup/plugin-node-resolve": "^15.2.3",
|
|
28
|
+
"@rollup/plugin-typescript": "^11.1.6",
|
|
29
|
+
"tslib": "^2.6.2"
|
|
30
|
+
},
|
|
31
|
+
"devDependencies": {
|
|
32
|
+
"typescript": "^5.4.5"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/rollup.config.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// 允许使用 node 或 umd 包
|
|
2
|
+
const commonjs = require("@rollup/plugin-commonjs");
|
|
3
|
+
const { nodeResolve } = require("@rollup/plugin-node-resolve");
|
|
4
|
+
// 编译 TS 代码
|
|
5
|
+
const typescript = require("@rollup/plugin-typescript");
|
|
6
|
+
|
|
7
|
+
module.exports = {
|
|
8
|
+
plugins: [nodeResolve(), commonjs(), typescript()],
|
|
9
|
+
input: "./index.ts", // 源文件入口
|
|
10
|
+
output: [
|
|
11
|
+
{
|
|
12
|
+
file: "dist/index.esm.js", // package.json 中 "module": "dist/index.esm.js"
|
|
13
|
+
format: "esm", // es module 形式的包, 用来import 导入, 可以tree shaking
|
|
14
|
+
sourcemap: true,
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
file: "dist/index.cjs.js", // package.json 中 "main": "dist/index.cjs.js",
|
|
18
|
+
format: "cjs", // commonjs 形式的包, require 导入
|
|
19
|
+
sourcemap: true,
|
|
20
|
+
},
|
|
21
|
+
{
|
|
22
|
+
file: "dist/index.umd.js",
|
|
23
|
+
name: "DOMUtils", // 模块名
|
|
24
|
+
format: "umd", // umd 兼容形式的包, 可以直接应用于网页 script
|
|
25
|
+
sourcemap: true,
|
|
26
|
+
},
|
|
27
|
+
],
|
|
28
|
+
};
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/** 通用工具类 */
|
|
2
|
+
const CommonDOMUtils = {
|
|
3
|
+
/**
|
|
4
|
+
* 判断元素是否已显示或已连接
|
|
5
|
+
* @param element
|
|
6
|
+
*/
|
|
7
|
+
isShow(element: HTMLElement) {
|
|
8
|
+
return Boolean(element.getClientRects().length);
|
|
9
|
+
},
|
|
10
|
+
/**
|
|
11
|
+
* 用于显示元素并获取它的高度宽度等其它属性
|
|
12
|
+
* @param element
|
|
13
|
+
*/
|
|
14
|
+
showElement(element: HTMLElement) {
|
|
15
|
+
let dupNode = element.cloneNode(true) as HTMLElement;
|
|
16
|
+
dupNode.setAttribute(
|
|
17
|
+
"style",
|
|
18
|
+
"visibility: hidden !important;display:block !important;"
|
|
19
|
+
);
|
|
20
|
+
document.documentElement.appendChild(dupNode);
|
|
21
|
+
return {
|
|
22
|
+
/**
|
|
23
|
+
* 恢复修改的style
|
|
24
|
+
*/
|
|
25
|
+
recovery() {
|
|
26
|
+
dupNode.remove();
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
},
|
|
30
|
+
/**
|
|
31
|
+
* 获取元素上的Float格式的属性px
|
|
32
|
+
* @param element
|
|
33
|
+
* @param styleName style名
|
|
34
|
+
*/
|
|
35
|
+
getStyleValue(element: HTMLElement | CSSStyleDeclaration, styleName: string) {
|
|
36
|
+
let view = null;
|
|
37
|
+
let styles = null;
|
|
38
|
+
if (element instanceof CSSStyleDeclaration) {
|
|
39
|
+
/* 直接就获取了style属性 */
|
|
40
|
+
styles = element;
|
|
41
|
+
} else {
|
|
42
|
+
view = element.ownerDocument.defaultView;
|
|
43
|
+
if (!view || !view.opener) {
|
|
44
|
+
view = window;
|
|
45
|
+
}
|
|
46
|
+
styles = view.getComputedStyle(element);
|
|
47
|
+
}
|
|
48
|
+
let value = parseFloat(styles[styleName as any]);
|
|
49
|
+
if (isNaN(value)) {
|
|
50
|
+
return 0;
|
|
51
|
+
} else {
|
|
52
|
+
return value;
|
|
53
|
+
}
|
|
54
|
+
},
|
|
55
|
+
/**
|
|
56
|
+
* 判断是否是window,例如window、self、globalThis
|
|
57
|
+
* @param target
|
|
58
|
+
*/
|
|
59
|
+
isWin(target: any) {
|
|
60
|
+
if (typeof target !== "object") {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
if (target instanceof Node) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
if (target === globalThis) {
|
|
67
|
+
return true;
|
|
68
|
+
}
|
|
69
|
+
if (target === window) {
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
if (target === self) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
if (typeof unsafeWindow !== "undefined" && target === unsafeWindow) {
|
|
76
|
+
return true;
|
|
77
|
+
}
|
|
78
|
+
if (target?.Math?.toString() !== "[object Math]") {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
return true;
|
|
82
|
+
},
|
|
83
|
+
/**
|
|
84
|
+
* 删除对象上的属性
|
|
85
|
+
* @param target
|
|
86
|
+
* @param propName
|
|
87
|
+
*/
|
|
88
|
+
delete(target: any, propName: any) {
|
|
89
|
+
if (typeof Reflect === "object" && Reflect.deleteProperty) {
|
|
90
|
+
Reflect.deleteProperty(target, propName);
|
|
91
|
+
} else {
|
|
92
|
+
delete target[propName];
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
export { CommonDOMUtils };
|
package/src/Core.ts
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
declare type DOMUtilsCreateElementAttributesMap = {
|
|
2
|
+
style?: string;
|
|
3
|
+
id?: string;
|
|
4
|
+
class?: string;
|
|
5
|
+
"data-"?: string;
|
|
6
|
+
type?: string;
|
|
7
|
+
[key: string]: any;
|
|
8
|
+
};
|
|
9
|
+
/**
|
|
10
|
+
* 鼠标事件
|
|
11
|
+
* + https://blog.csdn.net/weixin_68658847/article/details/126939879
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
interface DOMUtils_MouseEvent {
|
|
15
|
+
click: MouseEvent | PointerEvent;
|
|
16
|
+
contextmenu: MouseEvent | PointerEvent;
|
|
17
|
+
dblclick: MouseEvent | PointerEvent;
|
|
18
|
+
mousedown: MouseEvent | PointerEvent;
|
|
19
|
+
mouseenter: MouseEvent | PointerEvent;
|
|
20
|
+
mouseleave: MouseEvent | PointerEvent;
|
|
21
|
+
mousemove: MouseEvent | PointerEvent;
|
|
22
|
+
mouseover: MouseEvent | PointerEvent;
|
|
23
|
+
mouseout: MouseEvent | PointerEvent;
|
|
24
|
+
mouseup: MouseEvent | PointerEvent;
|
|
25
|
+
}
|
|
26
|
+
type DOMUtils_MouseEventType = keyof DOMUtils_MouseEvent;
|
|
27
|
+
/**
|
|
28
|
+
* 鼠标事件
|
|
29
|
+
*/
|
|
30
|
+
interface DOMUtils_KeyboardEvent {
|
|
31
|
+
keydown: KeyboardEvent;
|
|
32
|
+
keypress: KeyboardEvent;
|
|
33
|
+
keyup: KeyboardEvent;
|
|
34
|
+
}
|
|
35
|
+
type DOMUtils_KeyboardEventType = keyof DOMUtils_KeyboardEvent;
|
|
36
|
+
/**
|
|
37
|
+
* 框架/对象事件
|
|
38
|
+
*/
|
|
39
|
+
interface DOMUtils_Frame_Object_Event {
|
|
40
|
+
abort: Event;
|
|
41
|
+
beforeunload: Event;
|
|
42
|
+
error: Event;
|
|
43
|
+
hashchange: Event;
|
|
44
|
+
load: Event;
|
|
45
|
+
pageshow: Event;
|
|
46
|
+
pagehide: Event;
|
|
47
|
+
resize: Event;
|
|
48
|
+
scroll: Event;
|
|
49
|
+
unload: Event;
|
|
50
|
+
}
|
|
51
|
+
type DOMUtils_Frame_Object_EventType = keyof DOMUtils_Frame_Object_Event;
|
|
52
|
+
/**
|
|
53
|
+
* 表单事件
|
|
54
|
+
*/
|
|
55
|
+
interface DOMUtils_FormEvent {
|
|
56
|
+
blur: Event;
|
|
57
|
+
change: Event;
|
|
58
|
+
focus: Event;
|
|
59
|
+
focusin: Event;
|
|
60
|
+
focusout: Event;
|
|
61
|
+
input: Event;
|
|
62
|
+
reset: Event;
|
|
63
|
+
search: Event;
|
|
64
|
+
}
|
|
65
|
+
type DOMUtils_FormEventType = keyof DOMUtils_FormEvent;
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* 剪贴板事件
|
|
69
|
+
*/
|
|
70
|
+
interface DOMUtils_ClipboardEvent {
|
|
71
|
+
copy: ClipboardEvent;
|
|
72
|
+
cut: ClipboardEvent;
|
|
73
|
+
paste: ClipboardEvent;
|
|
74
|
+
}
|
|
75
|
+
type DOMUtils_ClipboardEventType = keyof DOMUtils_ClipboardEvent;
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* 打印事件
|
|
79
|
+
*/
|
|
80
|
+
interface DOMUtils_PrintEvent {
|
|
81
|
+
afterprint: Event;
|
|
82
|
+
beforeprint: Event;
|
|
83
|
+
}
|
|
84
|
+
type DOMUtils_PrintEventType = keyof DOMUtils_PrintEvent;
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* 拖动事件
|
|
88
|
+
*/
|
|
89
|
+
interface DOMUtils_DragEvent {
|
|
90
|
+
drag: DragEvent;
|
|
91
|
+
dragend: DragEvent;
|
|
92
|
+
dragenter: DragEvent;
|
|
93
|
+
dragleave: DragEvent;
|
|
94
|
+
dragover: DragEvent;
|
|
95
|
+
dragstart: DragEvent;
|
|
96
|
+
drop: DragEvent;
|
|
97
|
+
}
|
|
98
|
+
type DOMUtils_DragEventType = keyof DOMUtils_DragEvent;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* 多媒体(Media)事件
|
|
102
|
+
*/
|
|
103
|
+
interface DOMUtils_MediaEvent {
|
|
104
|
+
abort: Event;
|
|
105
|
+
canplay: Event;
|
|
106
|
+
canplaythrough: Event;
|
|
107
|
+
durationchange: Event;
|
|
108
|
+
emptied: Event;
|
|
109
|
+
ended: Event;
|
|
110
|
+
error: Event;
|
|
111
|
+
loadeddata: Event;
|
|
112
|
+
loadedmetadata: Event;
|
|
113
|
+
loadstart: Event;
|
|
114
|
+
pause: Event;
|
|
115
|
+
play: Event;
|
|
116
|
+
playing: Event;
|
|
117
|
+
progress: Event;
|
|
118
|
+
ratechange: Event;
|
|
119
|
+
seeked: Event;
|
|
120
|
+
seeking: Event;
|
|
121
|
+
stalled: Event;
|
|
122
|
+
suspend: Event;
|
|
123
|
+
timeupdate: Event;
|
|
124
|
+
volumechange: Event;
|
|
125
|
+
waiting: Event;
|
|
126
|
+
}
|
|
127
|
+
type DOMUtils_MediaEventType = keyof DOMUtils_MediaEvent;
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* 动画事件
|
|
131
|
+
*/
|
|
132
|
+
interface DOMUtils_AnimationEvent {
|
|
133
|
+
animationend: AnimationEvent;
|
|
134
|
+
animationiteration: AnimationEvent;
|
|
135
|
+
animationstart: AnimationEvent;
|
|
136
|
+
}
|
|
137
|
+
type DOMUtils_AnimationEventType = keyof DOMUtils_AnimationEvent;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* 过渡事件
|
|
141
|
+
*/
|
|
142
|
+
interface DOMUtils_TransitionEvent {
|
|
143
|
+
transitionend: TransitionEvent;
|
|
144
|
+
}
|
|
145
|
+
type DOMUtils_TransitionEventType = keyof DOMUtils_TransitionEvent;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* 触摸事件
|
|
149
|
+
*/
|
|
150
|
+
interface DOMUtils_TouchEvent {
|
|
151
|
+
touchstart: TouchEvent;
|
|
152
|
+
touchmove: TouchEvent;
|
|
153
|
+
touchend: TouchEvent;
|
|
154
|
+
touchcancel: TouchEvent;
|
|
155
|
+
touchenter: TouchEvent;
|
|
156
|
+
touchleave: TouchEvent;
|
|
157
|
+
}
|
|
158
|
+
type DOMUtils_TouchEventType = keyof DOMUtils_TouchEvent;
|
|
159
|
+
/**
|
|
160
|
+
* 其它事件
|
|
161
|
+
*/
|
|
162
|
+
interface DOMUtils_OtherEvent {
|
|
163
|
+
message: Event;
|
|
164
|
+
online: Event;
|
|
165
|
+
offline: Event;
|
|
166
|
+
popstate: Event;
|
|
167
|
+
show: Event;
|
|
168
|
+
storage: Event;
|
|
169
|
+
toggle: Event;
|
|
170
|
+
wheel: Event;
|
|
171
|
+
propertychange: Event;
|
|
172
|
+
fullscreenchange: Event;
|
|
173
|
+
DOMContentLoaded: Event;
|
|
174
|
+
}
|
|
175
|
+
type DOMUtils_OtherEventType = keyof DOMUtils_OtherEvent;
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* 全部事件
|
|
179
|
+
*/
|
|
180
|
+
declare type DOMUtils_Event = DOMUtils_MouseEvent &
|
|
181
|
+
DOMUtils_KeyboardEvent &
|
|
182
|
+
DOMUtils_Frame_Object_Event &
|
|
183
|
+
DOMUtils_FormEvent &
|
|
184
|
+
DOMUtils_ClipboardEvent &
|
|
185
|
+
DOMUtils_PrintEvent &
|
|
186
|
+
DOMUtils_DragEvent &
|
|
187
|
+
DOMUtils_MediaEvent &
|
|
188
|
+
DOMUtils_AnimationEvent &
|
|
189
|
+
DOMUtils_TransitionEvent &
|
|
190
|
+
DOMUtils_TouchEvent &
|
|
191
|
+
DOMUtils_OtherEvent;
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* 事件类型
|
|
195
|
+
*/
|
|
196
|
+
declare type DOMUtils_EventType = keyof DOMUtils_Event;
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* 元素上的events属性
|
|
200
|
+
*/
|
|
201
|
+
declare interface DOMUtilsEventListenerOptionsAttribute {
|
|
202
|
+
/**
|
|
203
|
+
* 自定义的ownCallBack
|
|
204
|
+
*/
|
|
205
|
+
callback: () => void;
|
|
206
|
+
/**
|
|
207
|
+
* 属性配置
|
|
208
|
+
*/
|
|
209
|
+
option: AddEventListenerOptions;
|
|
210
|
+
/**
|
|
211
|
+
* 用户添加的事件
|
|
212
|
+
*/
|
|
213
|
+
originCallBack: () => void;
|
|
214
|
+
/**
|
|
215
|
+
* 子元素选择器
|
|
216
|
+
*/
|
|
217
|
+
selector?: string;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
type ParseHTMLReturnType<T1, T2> = T1 extends true
|
|
221
|
+
? T2 extends true
|
|
222
|
+
? Document
|
|
223
|
+
: HTMLElement
|
|
224
|
+
: HTMLElement;
|