-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
58 lines (55 loc) · 1.54 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env node
import cursor from "https://unpkg.com/ipad-cursor@latest"
/**
* @param {*} config tag style config
* @param {IpadCursorConfig} cursorConfig cursor normal config
* @param {Function} effect other effect
*/
function init(config, cursorConfig, effect) {
if (effect) {
effect();
}
document.querySelectorAll('*').forEach(_ => _.style.cursor = 'none');
if (config) {
Object.keys(config).forEach(query => {
bindAttr(query, config[query]);
})
}
cursor.initCursor(cursorConfig);
}
const setAttr = (item, type, style) => {
item?.setAttribute('data-cursor', type);
if (style) {
item?.setAttribute('data-cursor-style', style);
}
}
const bindAttr = (query, cfg) => {
const { type, style } = cfg;
const selPath = query.split('>');
if (selPath.length > 2) {
return;
}
if (cfg.children) {
bindAttrNested(query, cfg);
} else if (selPath.length == 2) {
document.querySelectorAll(selPath[0])?.forEach(p => {
p.querySelectorAll(selPath[1])?.forEach(c => {
setAttr(c, type, style);
})
})
} else {
document.querySelectorAll(query)?.forEach(_ => {
setAttr(_, type, style);
})
}
}
const bindAttrNested = (query, cfg) => {
const { type, style } = cfg;
cfg = cfg.children;
query = Object.keys(cfg)[0];
document.querySelectorAll(query)?.forEach(p => {
setAttr(p, type, style);
bindAttr(query, cfg[query]);
})
}
export default init;