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
|
import { createElement, type CreateElement } from "./html";
export default class NotiveElement extends HTMLElement {
static makeFactory<T extends NotiveElement>(this: {
new (): T;
}): CreateElement<T>;
static makeFactory(): any {
throw new Error(
"Missing makeFactory implementation. Did you forget to use @customElement?",
);
}
}
export function customElement(tagName: string) {
return function (_value: unknown, context: ClassDecoratorContext) {
context.addInitializer(function () {
window.customElements.define(tagName, this as typeof NotiveElement);
(this as typeof NotiveElement).makeFactory = () =>
((...args: any[]) => createElement(tagName, ...args)) as CreateElement<any>;
});
};
}
export function eventHandler(eventName: string) {
return function (_value: unknown, context: ClassFieldDecoratorContext) {
const privateKey = Symbol(context.name.toString());
context.addInitializer(function () {
Object.defineProperty(this, context.name, {
get() {
return this[privateKey];
},
set(handler) {
const oldHandler = this[privateKey];
if (oldHandler) this.removeEventListener(eventName, oldHandler);
this[privateKey] = handler;
if (handler) this.addEventListener(eventName, handler);
},
enumerable: true,
configurable: true,
});
});
};
}
|