mirror of
https://github.com/misode/misode.github.io.git
synced 2026-04-23 23:27:09 +00:00
Switch to vite and preact
This commit is contained in:
36
src/app/schema/Mounter.ts
Normal file
36
src/app/schema/Mounter.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { hexId } from '../Utils'
|
||||
|
||||
export class Mounter {
|
||||
private registry: { [id: string]: (el: Element) => void } = {}
|
||||
|
||||
register(callback: (el: Element) => void): string {
|
||||
const id = hexId()
|
||||
this.registry[id] = callback
|
||||
return id
|
||||
}
|
||||
|
||||
on(type: string, callback: (el: Element) => void): string {
|
||||
return this.register(el => {
|
||||
el.addEventListener(type, evt => {
|
||||
callback(el)
|
||||
evt.stopPropagation()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
onChange(callback: (el: Element) => void): string {
|
||||
return this.on('change', callback)
|
||||
}
|
||||
|
||||
onClick(callback: (el: Element) => void): string {
|
||||
return this.on('click', callback)
|
||||
}
|
||||
|
||||
mounted(el: Element): void {
|
||||
el.querySelectorAll('[data-id]').forEach(el => {
|
||||
const id = el.getAttribute('data-id')!
|
||||
this.registry[id]?.(el)
|
||||
})
|
||||
this.registry = {}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user