@webc.site/web 0.0.0-stage → 0.1.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/package.json +20 -4
- package/src/clip.js +3 -0
- package/src/const.js +1 -0
- package/src/dom.js +66 -0
- package/src/scroll.js +132 -0
- package/README.md +0 -3
package/package.json
CHANGED
|
@@ -1,6 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webc.site/web",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Web native DOM primitives for webc components",
|
|
5
|
+
"license": "MulanPSL-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://atomgit.com/i18n-site/webc.git"
|
|
9
|
+
},
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"type": "module",
|
|
14
|
+
"sideEffects": false,
|
|
15
|
+
"exports": {
|
|
16
|
+
".": "./src/dom.js",
|
|
17
|
+
"./*": "./src/*"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"src"
|
|
21
|
+
]
|
|
22
|
+
}
|
package/src/clip.js
ADDED
package/src/const.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const EVT_MAP = { tap: 'click' }
|
package/src/dom.js
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { EVT_MAP } from './const.js'
|
|
2
|
+
|
|
3
|
+
const doc = () => globalThis.document,
|
|
4
|
+
create = (tag, cls) => {
|
|
5
|
+
const el = doc().createElement(tag)
|
|
6
|
+
if (cls) el.className = cls
|
|
7
|
+
return el
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const $new = create,
|
|
11
|
+
$div = (cls) => create('div', cls),
|
|
12
|
+
$b = (cls) => create('b', cls),
|
|
13
|
+
$span = (cls) => create('span', cls),
|
|
14
|
+
$a = (cls) => create('a', cls),
|
|
15
|
+
$img = (cls) => create('img', cls),
|
|
16
|
+
$scroll = (cls) => create('div', cls),
|
|
17
|
+
$txt = (el, text) => {
|
|
18
|
+
el.textContent = text == null ? '' : String(text)
|
|
19
|
+
},
|
|
20
|
+
$cls = (el, cls) => {
|
|
21
|
+
el.className = cls || ''
|
|
22
|
+
},
|
|
23
|
+
$attr = (el, key, val) => {
|
|
24
|
+
if (val === undefined || val === null || val === false) {
|
|
25
|
+
el.removeAttribute(key)
|
|
26
|
+
return
|
|
27
|
+
}
|
|
28
|
+
el.setAttribute(key, val === true ? '' : String(val))
|
|
29
|
+
},
|
|
30
|
+
$style = (el, style_obj) => {
|
|
31
|
+
if (typeof style_obj === 'string') {
|
|
32
|
+
el.setAttribute('style', style_obj)
|
|
33
|
+
return
|
|
34
|
+
}
|
|
35
|
+
Object.assign(el.style, style_obj)
|
|
36
|
+
},
|
|
37
|
+
$add = (parent, ...child_li) => {
|
|
38
|
+
parent.append(...child_li)
|
|
39
|
+
},
|
|
40
|
+
$ins = (parent, child, ref) => {
|
|
41
|
+
parent.insertBefore(child, ref ?? null)
|
|
42
|
+
},
|
|
43
|
+
$rm = (parent, child) => {
|
|
44
|
+
parent.removeChild(child)
|
|
45
|
+
},
|
|
46
|
+
$on = (el, evt, fn) => {
|
|
47
|
+
const name = EVT_MAP[evt] ?? evt
|
|
48
|
+
el.addEventListener(name, fn)
|
|
49
|
+
return () => el.removeEventListener(name, fn)
|
|
50
|
+
},
|
|
51
|
+
$root = () => doc().body,
|
|
52
|
+
$esc = (fn) => {
|
|
53
|
+
const bd = doc().body,
|
|
54
|
+
onKey = (e) => {
|
|
55
|
+
if (e.key === 'Escape' || e.keyCode === 27) {
|
|
56
|
+
const tag = e.target?.tagName
|
|
57
|
+
if (tag === 'INPUT' || tag === 'TEXTAREA') {
|
|
58
|
+
e.target.blur()
|
|
59
|
+
return
|
|
60
|
+
}
|
|
61
|
+
fn()
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
bd.addEventListener('keyup', onKey)
|
|
65
|
+
return () => bd.removeEventListener('keyup', onKey)
|
|
66
|
+
}
|
package/src/scroll.js
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
const MIN_THUMB = 16,
|
|
2
|
+
PAD = 3,
|
|
3
|
+
HIDE_MS = 1000,
|
|
4
|
+
RESIZE_MS = 200,
|
|
5
|
+
DRAG_CLS = 'Drag',
|
|
6
|
+
BODY_DRAG = 'ScrollDrag',
|
|
7
|
+
{ max, min, round } = Math
|
|
8
|
+
|
|
9
|
+
// Bind custom virtual overlay scrollbar onto Web scroll nodes
|
|
10
|
+
export const scrollBind = (root, view, inner, bar, si, is_h = false) => {
|
|
11
|
+
const can_hide =
|
|
12
|
+
typeof globalThis.CSS?.supports === 'function'
|
|
13
|
+
? globalThis.CSS.supports('scrollbar-width', 'none')
|
|
14
|
+
: true
|
|
15
|
+
if (!can_hide) return [() => {}, () => {}]
|
|
16
|
+
|
|
17
|
+
const size = is_h ? 'Width' : 'Height',
|
|
18
|
+
pos = is_h ? 'Left' : 'Top',
|
|
19
|
+
axis = is_h ? 'X' : 'Y',
|
|
20
|
+
style_size = size.toLowerCase(),
|
|
21
|
+
style_pos = pos.toLowerCase(),
|
|
22
|
+
client_size = 'client' + size,
|
|
23
|
+
scroll_size = 'scroll' + size,
|
|
24
|
+
scroll_pos = 'scroll' + pos,
|
|
25
|
+
client_pos = 'client' + axis
|
|
26
|
+
|
|
27
|
+
let timer_bar,
|
|
28
|
+
timer_resize,
|
|
29
|
+
ptr_unbind = null,
|
|
30
|
+
pre_st = -1
|
|
31
|
+
|
|
32
|
+
const geo = (sih = si[client_size]) => {
|
|
33
|
+
const ch = view[client_size] || 0,
|
|
34
|
+
sh = max(inner[scroll_size] || 0, view[scroll_size] || 0),
|
|
35
|
+
thumb = sih || max(MIN_THUMB, round((ch * ch) / max(1, sh)))
|
|
36
|
+
return [sh - ch, max(1, ch - thumb - PAD * 2), thumb, ch, sh]
|
|
37
|
+
},
|
|
38
|
+
update = (forced_h) => {
|
|
39
|
+
const [ds, db, , ch, sh] = geo(forced_h)
|
|
40
|
+
if (ds <= 0) {
|
|
41
|
+
if (bar.parentNode === root) root.removeChild(bar)
|
|
42
|
+
pre_st = -1
|
|
43
|
+
return
|
|
44
|
+
}
|
|
45
|
+
if (bar.parentNode !== root) root.appendChild(bar)
|
|
46
|
+
const h = forced_h ?? max(MIN_THUMB, round((ch * ch) / sh))
|
|
47
|
+
si.style[style_size] = h + 'px'
|
|
48
|
+
const st = max(0, min(view[scroll_pos] || 0, ds))
|
|
49
|
+
if (pre_st !== -1 && pre_st !== st) {
|
|
50
|
+
bar.style.opacity = '1'
|
|
51
|
+
clearTimeout(timer_bar)
|
|
52
|
+
timer_bar = setTimeout(() => {
|
|
53
|
+
bar.style.opacity = '0'
|
|
54
|
+
}, HIDE_MS)
|
|
55
|
+
}
|
|
56
|
+
pre_st = st
|
|
57
|
+
si.style[style_pos] = PAD + round((db * st) / ds) + 'px'
|
|
58
|
+
},
|
|
59
|
+
onDown = (e) => {
|
|
60
|
+
if (ptr_unbind) return
|
|
61
|
+
const bd = globalThis.document.body
|
|
62
|
+
bd.setPointerCapture?.(e.pointerId)
|
|
63
|
+
bd.classList?.add(BODY_DRAG)
|
|
64
|
+
bar.classList?.add(DRAG_CLS)
|
|
65
|
+
let pre = e[client_pos]
|
|
66
|
+
const onMove = (evt) => {
|
|
67
|
+
const [ds, db] = geo()
|
|
68
|
+
view[scroll_pos] += round((ds * (evt[client_pos] - pre)) / db)
|
|
69
|
+
pre = evt[client_pos]
|
|
70
|
+
update()
|
|
71
|
+
},
|
|
72
|
+
detach = () => {
|
|
73
|
+
bd.classList?.remove(BODY_DRAG)
|
|
74
|
+
bar.classList?.remove(DRAG_CLS)
|
|
75
|
+
bd.removeEventListener('pointermove', onMove)
|
|
76
|
+
bd.removeEventListener('pointerup', detach)
|
|
77
|
+
bd.removeEventListener('lostpointercapture', detach)
|
|
78
|
+
ptr_unbind = null
|
|
79
|
+
}
|
|
80
|
+
bd.addEventListener('pointermove', onMove)
|
|
81
|
+
bd.addEventListener('pointerup', detach)
|
|
82
|
+
bd.addEventListener('lostpointercapture', detach)
|
|
83
|
+
ptr_unbind = detach
|
|
84
|
+
},
|
|
85
|
+
onTrack = (e) => {
|
|
86
|
+
const rect = bar.getBoundingClientRect(),
|
|
87
|
+
[ds, db, sih] = geo(),
|
|
88
|
+
ratio = max(
|
|
89
|
+
min((e[client_pos] - rect[style_pos] - PAD - sih / 2) / db, 1),
|
|
90
|
+
0
|
|
91
|
+
)
|
|
92
|
+
view[scroll_pos] = round(ds * ratio)
|
|
93
|
+
update()
|
|
94
|
+
onDown(e)
|
|
95
|
+
},
|
|
96
|
+
onThumb = (e) => {
|
|
97
|
+
e.stopPropagation?.()
|
|
98
|
+
onDown(e)
|
|
99
|
+
},
|
|
100
|
+
onScroll = () => update(),
|
|
101
|
+
sync = () => update()
|
|
102
|
+
|
|
103
|
+
bar.addEventListener('pointerdown', onTrack)
|
|
104
|
+
si.addEventListener('pointerdown', onThumb)
|
|
105
|
+
view.addEventListener('scroll', onScroll)
|
|
106
|
+
|
|
107
|
+
let ob = null
|
|
108
|
+
if (typeof globalThis.ResizeObserver === 'function') {
|
|
109
|
+
ob = new globalThis.ResizeObserver(() => {
|
|
110
|
+
clearTimeout(timer_resize)
|
|
111
|
+
timer_resize = setTimeout(sync, RESIZE_MS)
|
|
112
|
+
})
|
|
113
|
+
ob.observe(view)
|
|
114
|
+
ob.observe(inner)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
bar.style.opacity = '0'
|
|
118
|
+
queueMicrotask(sync)
|
|
119
|
+
|
|
120
|
+
const unmount = () => {
|
|
121
|
+
clearTimeout(timer_bar)
|
|
122
|
+
clearTimeout(timer_resize)
|
|
123
|
+
bar.removeEventListener('pointerdown', onTrack)
|
|
124
|
+
si.removeEventListener('pointerdown', onThumb)
|
|
125
|
+
view.removeEventListener('scroll', onScroll)
|
|
126
|
+
ptr_unbind?.()
|
|
127
|
+
ob?.disconnect()
|
|
128
|
+
if (bar.parentNode === root) root.removeChild(bar)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
return [sync, unmount]
|
|
132
|
+
}
|
package/README.md
DELETED