@ecopages/radiant 0.1.4 → 0.1.6
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/README.md +2 -0
- package/dist/context/context-provider.js +282 -3
- package/dist/context/context-provider.js.map +5 -3
- package/dist/context/create-context.js +6 -3
- package/dist/context/create-context.js.map +2 -2
- package/dist/context/decorators/consume-context.js +64 -3
- package/dist/context/decorators/consume-context.js.map +4 -3
- package/dist/context/decorators/context-selector.js +71 -3
- package/dist/context/decorators/context-selector.js.map +5 -4
- package/dist/context/decorators/provide-context.js +293 -3
- package/dist/context/decorators/provide-context.js.map +6 -3
- package/dist/context/events.js +53 -3
- package/dist/context/events.js.map +2 -2
- package/dist/context/index.js +340 -2
- package/dist/context/index.js.map +10 -3
- package/dist/context/types.js +1 -1
- package/dist/context/types.js.map +1 -1
- package/dist/core/index.js +82 -2
- package/dist/core/index.js.map +4 -3
- package/dist/core/radiant-element.d.ts +21 -1
- package/dist/core/radiant-element.js +82 -3
- package/dist/core/radiant-element.js.map +3 -3
- package/dist/decorators/custom-element.js +14 -3
- package/dist/decorators/custom-element.js.map +2 -2
- package/dist/decorators/debounce.d.ts +1 -0
- package/dist/decorators/debounce.js +21 -0
- package/dist/decorators/debounce.js.map +10 -0
- package/dist/decorators/event.js +46 -3
- package/dist/decorators/event.js.map +4 -3
- package/dist/decorators/on-event.d.ts +4 -0
- package/dist/decorators/on-event.js +47 -3
- package/dist/decorators/on-event.js.map +3 -3
- package/dist/decorators/on-updated.d.ts +1 -1
- package/dist/decorators/on-updated.js +29 -3
- package/dist/decorators/on-updated.js.map +3 -3
- package/dist/decorators/query.js +36 -3
- package/dist/decorators/query.js.map +3 -3
- package/dist/decorators/reactive-field.js +26 -3
- package/dist/decorators/reactive-field.js.map +2 -2
- package/dist/decorators/reactive-prop.d.ts +1 -1
- package/dist/decorators/reactive-prop.js +214 -3
- package/dist/decorators/reactive-prop.js.map +5 -4
- package/dist/decorators.js +400 -2
- package/dist/decorators.js.map +12 -3
- package/dist/index.js +719 -2
- package/dist/index.js.map +21 -3
- package/dist/mixins/index.js +23 -2
- package/dist/mixins/index.js.map +4 -3
- package/dist/mixins/with-kita.js +23 -3
- package/dist/mixins/with-kita.js.map +2 -2
- package/dist/tools/event-emitter.js +22 -3
- package/dist/tools/event-emitter.js.map +2 -2
- package/dist/tools/index.js +28 -2
- package/dist/tools/index.js.map +5 -3
- package/dist/tools/stringify-attribute.js +8 -3
- package/dist/tools/stringify-attribute.js.map +2 -2
- package/dist/utils/attribute-utils.d.ts +2 -0
- package/dist/utils/attribute-utils.js +137 -3
- package/dist/utils/attribute-utils.js.map +3 -3
- package/dist/utils/index.js +137 -2
- package/dist/utils/index.js.map +4 -3
- package/package.json +9 -4
|
@@ -1,4 +1,83 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// src/core/radiant-element.ts
|
|
2
|
+
class RadiantElement extends HTMLElement {
|
|
3
|
+
eventSubscriptions = new Map;
|
|
4
|
+
elementReady = false;
|
|
5
|
+
connectedCallback() {
|
|
6
|
+
this.elementReady = true;
|
|
7
|
+
}
|
|
8
|
+
connectedContextCallback(_contextName) {
|
|
9
|
+
}
|
|
10
|
+
disconnectedCallback() {
|
|
11
|
+
this.removeAllSubscribedEvents();
|
|
12
|
+
}
|
|
13
|
+
updated(changedProperty, oldValue, value) {
|
|
14
|
+
if (!this.elementReady || !this.updatesRegistry || oldValue === value)
|
|
15
|
+
return;
|
|
16
|
+
const updates = this.updatesRegistry.get(changedProperty);
|
|
17
|
+
if (updates) {
|
|
18
|
+
for (const update of updates) {
|
|
19
|
+
this[update]();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
attributeChangedCallback(name, oldValue, newValue) {
|
|
24
|
+
if (oldValue === newValue || !this.elementReady)
|
|
25
|
+
return;
|
|
26
|
+
if (name in this) {
|
|
27
|
+
const config = this.propertyConfigMap.get(name);
|
|
28
|
+
const transformedValue = newValue ? config?.converter.fromAttribute(newValue) : newValue;
|
|
29
|
+
const transformedOldValue = oldValue ? config?.converter.fromAttribute(oldValue) : oldValue;
|
|
30
|
+
this[name] = transformedValue;
|
|
31
|
+
this.updated(name, transformedOldValue, transformedValue);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
renderTemplate({
|
|
35
|
+
target = this,
|
|
36
|
+
template,
|
|
37
|
+
insert = "replace"
|
|
38
|
+
}) {
|
|
39
|
+
switch (insert) {
|
|
40
|
+
case "replace":
|
|
41
|
+
target.innerHTML = template;
|
|
42
|
+
break;
|
|
43
|
+
case "beforeend":
|
|
44
|
+
target.insertAdjacentHTML("beforeend", template);
|
|
45
|
+
break;
|
|
46
|
+
case "afterbegin":
|
|
47
|
+
target.insertAdjacentHTML("afterbegin", template);
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
subscribeEvents(events) {
|
|
52
|
+
for (const event of events) {
|
|
53
|
+
this.subscribeEvent(event);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
subscribeEvent(eventConfig) {
|
|
57
|
+
const delegatedListener = (delegatedEvent) => {
|
|
58
|
+
if (delegatedEvent.target && delegatedEvent.target.matches(eventConfig.selector)) {
|
|
59
|
+
eventConfig.listener.call(this, delegatedEvent);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
this.addEventListener(eventConfig.type, delegatedListener, eventConfig.options);
|
|
63
|
+
this.eventSubscriptions.set(eventConfig.id, { ...eventConfig, listener: delegatedListener });
|
|
64
|
+
}
|
|
65
|
+
unsubscribeEvent(id) {
|
|
66
|
+
const eventSubscription = this.eventSubscriptions.get(id);
|
|
67
|
+
if (eventSubscription) {
|
|
68
|
+
this.removeEventListener(eventSubscription.type, eventSubscription.listener, eventSubscription.options);
|
|
69
|
+
this.eventSubscriptions.delete(id);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
removeAllSubscribedEvents() {
|
|
73
|
+
for (const eventSubscription of this.eventSubscriptions.values()) {
|
|
74
|
+
this.removeEventListener(eventSubscription.type, eventSubscription.listener, eventSubscription.options);
|
|
75
|
+
}
|
|
76
|
+
this.eventSubscriptions.clear();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
export {
|
|
80
|
+
RadiantElement
|
|
81
|
+
};
|
|
3
82
|
|
|
4
|
-
//# debugId=
|
|
83
|
+
//# debugId=494A682B938300F764756E2164756E21
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/core/radiant-element.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"import type { UnknownContext } from '@/context/types';\n\nexport type RenderInsertPosition = 'replace' | 'beforebegin' | 'afterbegin' | 'beforeend' | 'afterend';\n\n/**\n * Represents a Radiant element event listener.\n */\nexport type RadiantElementEventListener = {\n selector: string;\n type: string;\n listener: EventListener;\n id: string;\n options?: AddEventListenerOptions;\n};\n\n/**\n * Represents an interface for a Radiant element.\n */\nexport interface IRadiantElement {\n /**\n * Called when a property of the element is updated.\n * @param changedProperty - The name of the changed property.\n * @param oldValue - The old value of the property.\n * @param newValue - The new value of the property.\n */\n updated(changedProperty: string, oldValue: unknown, newValue: unknown): void;\n\n /**\n * Subscribes to a Radiant element event.\n * @param event - The event listener to subscribe to.\n */\n subscribeEvent(event: RadiantElementEventListener): void;\n\n /**\n * Subscribes to multiple Radiant element events.\n * @param events - The array of event listeners to subscribe to.\n */\n subscribeEvents(events: RadiantElementEventListener[]): void;\n\n /**\n * Unsubscribes from a Radiant element event.\n * @param id - The ID of the event listener to unsubscribe from.\n */\n unsubscribeEvent(id: string): void;\n\n /**\n * Removes all subscribed events from the Radiant element.\n */\n removeAllSubscribedEvents(): void;\n\n /**\n * Renders a template into the specified target element.\n * @param options - The rendering options.\n * @param options.target - The target element to render the template into.\n * @param options.template - The template string to render.\n * @param options.insert - The position to insert the rendered template. (optional)\n */\n renderTemplate(options: {\n target: HTMLElement;\n template: string;\n insert?: RenderInsertPosition;\n }): void;\n\n /**\n * Called when the Radiant element is connected to a context.\n * @param context - The connected context.\n */\n connectedContextCallback(context: UnknownContext): void;\n}\n\n/**\n * A base class for creating custom elements with reactive properties and event subscriptions.\n * @extends HTMLElement\n * @implements IRadiantElement\n */\nexport class RadiantElement extends HTMLElement implements IRadiantElement {\n private eventSubscriptions = new Map<string, RadiantElementEventListener>();\n\n connectedCallback() {}\n\n connectedContextCallback(_contextName: UnknownContext): void {}\n\n disconnectedCallback() {\n this.removeAllSubscribedEvents();\n }\n\n updated(
|
|
5
|
+
"import type { UnknownContext } from '@/context/types';\nimport type { AttributeTypeConstant, ReadAttributeValueReturnType, WriteAttributeValueReturnType } from '@/utils';\n\n/**\n * Possible positions to insert a rendered template.\n */\nexport type RenderInsertPosition = 'replace' | 'beforebegin' | 'afterbegin' | 'beforeend' | 'afterend';\n\n/**\n * Represents a Radiant element event listener.\n */\nexport type RadiantElementEventListener = {\n selector: string;\n type: string;\n listener: EventListener;\n id: string;\n options?: AddEventListenerOptions;\n};\n\n/**\n * Represents a property metadata object.\n */\nexport interface PropertyConfig {\n type: AttributeTypeConstant;\n propertyName: string;\n attributeKey: string;\n converter: {\n fromAttribute: (value: string) => ReadAttributeValueReturnType;\n toAttribute: (value: any) => WriteAttributeValueReturnType;\n };\n}\n\n/**\n * Represents an interface for a Radiant element.\n */\nexport interface IRadiantElement {\n /**\n * Called when a property of the element is updated.\n * @param changedProperty - The name of the changed property.\n * @param oldValue - The old value of the property.\n * @param newValue - The new value of the property.\n */\n updated(changedProperty: string, oldValue: unknown, newValue: unknown): void;\n\n /**\n * Subscribes to a Radiant element event.\n * @param event - The event listener to subscribe to.\n */\n subscribeEvent(event: RadiantElementEventListener): void;\n\n /**\n * Subscribes to multiple Radiant element events.\n * @param events - The array of event listeners to subscribe to.\n */\n subscribeEvents(events: RadiantElementEventListener[]): void;\n\n /**\n * Unsubscribes from a Radiant element event.\n * @param id - The ID of the event listener to unsubscribe from.\n */\n unsubscribeEvent(id: string): void;\n\n /**\n * Removes all subscribed events from the Radiant element.\n */\n removeAllSubscribedEvents(): void;\n\n /**\n * Renders a template into the specified target element.\n * @param options - The rendering options.\n * @param options.target - The target element to render the template into.\n * @param options.template - The template string to render.\n * @param options.insert - The position to insert the rendered template. (optional)\n */\n renderTemplate(options: {\n target: HTMLElement;\n template: string;\n insert?: RenderInsertPosition;\n }): void;\n\n /**\n * Called when the Radiant element is connected to a context.\n * @param context - The connected context.\n */\n connectedContextCallback(context: UnknownContext): void;\n}\n\n/**\n * A base class for creating custom elements with reactive properties and event subscriptions.\n * @extends HTMLElement\n * @implements IRadiantElement\n */\nexport class RadiantElement extends HTMLElement implements IRadiantElement {\n declare propertyConfigMap: Map<string, PropertyConfig>;\n declare updatesRegistry: Map<string, Set<string>>;\n private eventSubscriptions = new Map<string, RadiantElementEventListener>();\n private elementReady = false;\n\n connectedCallback() {\n this.elementReady = true;\n }\n\n connectedContextCallback(_contextName: UnknownContext): void {}\n\n disconnectedCallback() {\n this.removeAllSubscribedEvents();\n }\n\n updated(changedProperty: string, oldValue: unknown, value: unknown) {\n if (!this.elementReady || !this.updatesRegistry || oldValue === value) return;\n const updates = this.updatesRegistry.get(changedProperty);\n if (updates) {\n for (const update of updates) {\n (this as any)[update]();\n }\n }\n }\n\n attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null) {\n if (oldValue === newValue || !this.elementReady) return;\n\n if (name in this) {\n const config = this.propertyConfigMap.get(name);\n const transformedValue = newValue ? config?.converter.fromAttribute(newValue) : newValue;\n const transformedOldValue = oldValue ? config?.converter.fromAttribute(oldValue) : oldValue;\n (this as RadiantElement & { [key: string]: any })[name] = transformedValue;\n this.updated(name, transformedOldValue, transformedValue);\n }\n }\n\n renderTemplate({\n target = this,\n template,\n insert = 'replace',\n }: {\n target: HTMLElement;\n template: string;\n insert?: RenderInsertPosition;\n }) {\n switch (insert) {\n case 'replace':\n target.innerHTML = template;\n break;\n case 'beforeend':\n target.insertAdjacentHTML('beforeend', template);\n break;\n case 'afterbegin':\n target.insertAdjacentHTML('afterbegin', template);\n break;\n }\n }\n\n public subscribeEvents(events: RadiantElementEventListener[]): void {\n for (const event of events) {\n this.subscribeEvent(event);\n }\n }\n\n public subscribeEvent(eventConfig: RadiantElementEventListener): void {\n const delegatedListener = (delegatedEvent: Event) => {\n if (delegatedEvent.target && (delegatedEvent.target as Element).matches(eventConfig.selector)) {\n eventConfig.listener.call(this, delegatedEvent);\n }\n };\n\n this.addEventListener(eventConfig.type, delegatedListener, eventConfig.options);\n this.eventSubscriptions.set(eventConfig.id, { ...eventConfig, listener: delegatedListener });\n }\n\n public unsubscribeEvent(id: string): void {\n const eventSubscription = this.eventSubscriptions.get(id);\n if (eventSubscription) {\n this.removeEventListener(eventSubscription.type, eventSubscription.listener, eventSubscription.options);\n this.eventSubscriptions.delete(id);\n }\n }\n\n public removeAllSubscribedEvents(): void {\n for (const eventSubscription of this.eventSubscriptions.values()) {\n this.removeEventListener(eventSubscription.type, eventSubscription.listener, eventSubscription.options);\n }\n this.eventSubscriptions.clear();\n }\n}\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": "
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": ";AA4FO,MAAM,uBAAuB,YAAuC;AAAA,EAGjE,qBAAqB,IAAI;AAAA,EACzB,eAAe;AAAA,EAEvB,iBAAiB,GAAG;AAClB,SAAK,eAAe;AAAA;AAAA,EAGtB,wBAAwB,CAAC,cAAoC;AAAA;AAAA,EAE7D,oBAAoB,GAAG;AACrB,SAAK,0BAA0B;AAAA;AAAA,EAGjC,OAAO,CAAC,iBAAyB,UAAmB,OAAgB;AAClE,SAAK,KAAK,iBAAiB,KAAK,mBAAmB,aAAa;AAAO;AACvE,UAAM,UAAU,KAAK,gBAAgB,IAAI,eAAe;AACxD,QAAI,SAAS;AACX,iBAAW,UAAU,SAAS;AAC5B,QAAC,KAAa,QAAQ;AAAA,MACxB;AAAA,IACF;AAAA;AAAA,EAGF,wBAAwB,CAAC,MAAc,UAAyB,UAAyB;AACvF,QAAI,aAAa,aAAa,KAAK;AAAc;AAEjD,QAAI,QAAQ,MAAM;AAChB,YAAM,SAAS,KAAK,kBAAkB,IAAI,IAAI;AAC9C,YAAM,mBAAmB,WAAW,QAAQ,UAAU,cAAc,QAAQ,IAAI;AAChF,YAAM,sBAAsB,WAAW,QAAQ,UAAU,cAAc,QAAQ,IAAI;AACnF,MAAC,KAAiD,QAAQ;AAC1D,WAAK,QAAQ,MAAM,qBAAqB,gBAAgB;AAAA,IAC1D;AAAA;AAAA,EAGF,cAAc;AAAA,IACZ,SAAS;AAAA,IACT;AAAA,IACA,SAAS;AAAA,KAKR;AACD,YAAQ;AAAA,WACD;AACH,eAAO,YAAY;AACnB;AAAA,WACG;AACH,eAAO,mBAAmB,aAAa,QAAQ;AAC/C;AAAA,WACG;AACH,eAAO,mBAAmB,cAAc,QAAQ;AAChD;AAAA;AAAA;AAAA,EAIC,eAAe,CAAC,QAA6C;AAClE,eAAW,SAAS,QAAQ;AAC1B,WAAK,eAAe,KAAK;AAAA,IAC3B;AAAA;AAAA,EAGK,cAAc,CAAC,aAAgD;AACpE,UAAM,oBAAoB,CAAC,mBAA0B;AACnD,UAAI,eAAe,UAAW,eAAe,OAAmB,QAAQ,YAAY,QAAQ,GAAG;AAC7F,oBAAY,SAAS,KAAK,MAAM,cAAc;AAAA,MAChD;AAAA;AAGF,SAAK,iBAAiB,YAAY,MAAM,mBAAmB,YAAY,OAAO;AAC9E,SAAK,mBAAmB,IAAI,YAAY,IAAI,KAAK,aAAa,UAAU,kBAAkB,CAAC;AAAA;AAAA,EAGtF,gBAAgB,CAAC,IAAkB;AACxC,UAAM,oBAAoB,KAAK,mBAAmB,IAAI,EAAE;AACxD,QAAI,mBAAmB;AACrB,WAAK,oBAAoB,kBAAkB,MAAM,kBAAkB,UAAU,kBAAkB,OAAO;AACtG,WAAK,mBAAmB,OAAO,EAAE;AAAA,IACnC;AAAA;AAAA,EAGK,yBAAyB,GAAS;AACvC,eAAW,qBAAqB,KAAK,mBAAmB,OAAO,GAAG;AAChE,WAAK,oBAAoB,kBAAkB,MAAM,kBAAkB,UAAU,kBAAkB,OAAO;AAAA,IACxG;AACA,SAAK,mBAAmB,MAAM;AAAA;AAElC;",
|
|
8
|
+
"debugId": "494A682B938300F764756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
|
@@ -1,4 +1,15 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// src/decorators/custom-element.ts
|
|
2
|
+
function customElement(name) {
|
|
3
|
+
return (target) => {
|
|
4
|
+
if (!globalThis.window)
|
|
5
|
+
return;
|
|
6
|
+
if (!window.customElements.get(name)) {
|
|
7
|
+
window.customElements.define(name, target);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export {
|
|
12
|
+
customElement
|
|
13
|
+
};
|
|
3
14
|
|
|
4
|
-
//# debugId=
|
|
15
|
+
//# debugId=5286B58DE50049BA64756E2164756E21
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"sourcesContent": [
|
|
5
5
|
"/**\n * Registers a web component with the given name on the global `window.customElements` registry.\n * @param name selector name.\n */\nexport function customElement(name: string) {\n return (target: CustomElementConstructor) => {\n if (!globalThis.window) return;\n if (!window.customElements.get(name)) {\n window.customElements.define(name, target);\n }\n };\n}\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": "AAIO,SAAS,
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": ";AAIO,SAAS,aAAa,CAAC,MAAc;AAC1C,SAAO,CAAC,WAAqC;AAC3C,SAAK,WAAW;AAAQ;AACxB,SAAK,OAAO,eAAe,IAAI,IAAI,GAAG;AACpC,aAAO,eAAe,OAAO,MAAM,MAAM;AAAA,IAC3C;AAAA;AAAA;",
|
|
8
|
+
"debugId": "5286B58DE50049BA64756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function debounce(timeout: number): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// src/decorators/debounce.ts
|
|
2
|
+
function debounce(timeout) {
|
|
3
|
+
let timeoutRef = null;
|
|
4
|
+
return (_target, _propertyKey, descriptor) => {
|
|
5
|
+
const originalMethod = descriptor.value;
|
|
6
|
+
descriptor.value = function debounce(...args) {
|
|
7
|
+
if (timeoutRef !== null) {
|
|
8
|
+
clearTimeout(timeoutRef);
|
|
9
|
+
}
|
|
10
|
+
timeoutRef = setTimeout(() => {
|
|
11
|
+
originalMethod.apply(this, args);
|
|
12
|
+
}, timeout);
|
|
13
|
+
};
|
|
14
|
+
return descriptor;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export {
|
|
18
|
+
debounce
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
//# debugId=30A0CC4182AD504664756E2164756E21
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/decorators/debounce.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"export function debounce(\n timeout: number,\n): (target: any, propertyKey: string, descriptor: PropertyDescriptor) => PropertyDescriptor {\n let timeoutRef: ReturnType<typeof setTimeout> | null = null;\n\n return (_target: any, _propertyKey: string, descriptor: PropertyDescriptor): PropertyDescriptor => {\n const originalMethod = descriptor.value;\n\n descriptor.value = function debounce(...args: any[]) {\n if (timeoutRef !== null) {\n clearTimeout(timeoutRef);\n }\n\n timeoutRef = setTimeout(() => {\n originalMethod.apply(this, args);\n }, timeout);\n };\n\n return descriptor;\n };\n}\n"
|
|
6
|
+
],
|
|
7
|
+
"mappings": ";AAAO,SAAS,QAAQ,CACtB,SAC0F;AAC1F,MAAI,aAAmD;AAEvD,SAAO,CAAC,SAAc,cAAsB,eAAuD;AACjG,UAAM,iBAAiB,WAAW;AAElC,eAAW,iBAAiB,QAAQ,IAAI,MAAa;AACnD,UAAI,eAAe,MAAM;AACvB,qBAAa,UAAU;AAAA,MACzB;AAEA,mBAAa,WAAW,MAAM;AAC5B,uBAAe,MAAM,MAAM,IAAI;AAAA,SAC9B,OAAO;AAAA;AAGZ,WAAO;AAAA;AAAA;",
|
|
8
|
+
"debugId": "30A0CC4182AD504664756E2164756E21",
|
|
9
|
+
"names": []
|
|
10
|
+
}
|
package/dist/decorators/event.js
CHANGED
|
@@ -1,4 +1,47 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// src/tools/event-emitter.ts
|
|
2
|
+
class EventEmitter {
|
|
3
|
+
host;
|
|
4
|
+
eventConfig;
|
|
5
|
+
constructor(host, eventConfig) {
|
|
6
|
+
this.host = host;
|
|
7
|
+
this.eventConfig = eventConfig;
|
|
8
|
+
}
|
|
9
|
+
emit(detail) {
|
|
10
|
+
const event = new CustomEvent(this.eventConfig.name, {
|
|
11
|
+
detail,
|
|
12
|
+
bubbles: this.eventConfig.bubbles,
|
|
13
|
+
cancelable: this.eventConfig.cancelable,
|
|
14
|
+
composed: this.eventConfig.composed
|
|
15
|
+
});
|
|
16
|
+
this.host.dispatchEvent(event);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
3
19
|
|
|
4
|
-
|
|
20
|
+
// src/decorators/event.ts
|
|
21
|
+
function event(eventConfig) {
|
|
22
|
+
return (target, propertyKey) => {
|
|
23
|
+
if (!propertyKey) {
|
|
24
|
+
throw new Error("The propertyKey is missing for the event decorator.");
|
|
25
|
+
}
|
|
26
|
+
if (!eventConfig || !eventConfig.name) {
|
|
27
|
+
throw new Error("Invalid eventConfig provided.");
|
|
28
|
+
}
|
|
29
|
+
const uniqueKey = Symbol(eventConfig.name);
|
|
30
|
+
Object.defineProperty(target, propertyKey, {
|
|
31
|
+
get() {
|
|
32
|
+
const emittersMap = eventEmitters.get(this) || new Map;
|
|
33
|
+
if (!emittersMap.has(uniqueKey)) {
|
|
34
|
+
emittersMap.set(uniqueKey, new EventEmitter(this, eventConfig));
|
|
35
|
+
eventEmitters.set(this, emittersMap);
|
|
36
|
+
}
|
|
37
|
+
return emittersMap.get(uniqueKey);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
var eventEmitters = new WeakMap;
|
|
43
|
+
export {
|
|
44
|
+
event
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
//# debugId=64B9F802F680DEB764756E2164756E21
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"version": 3,
|
|
3
|
-
"sources": ["../src/decorators/event.ts"],
|
|
3
|
+
"sources": ["../src/tools/event-emitter.ts", "../src/decorators/event.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
+
"import type { RadiantElement } from '..';\n\nexport interface EventEmitterConfig {\n name: string;\n bubbles?: boolean;\n cancelable?: boolean;\n composed?: boolean;\n}\n\n/**\n * A generic event emitter class that allows emitting custom events.\n *\n * @template T - The type of the event detail.\n */\nexport class EventEmitter<T = unknown> {\n private host: RadiantElement;\n private eventConfig: EventEmitterConfig;\n\n /**\n * Constructs a new instance of the EventEmitter class.\n *\n * @param host - The host element on which the events will be dispatched.\n * @param eventConfig - The configuration for the event.\n */\n constructor(host: RadiantElement, eventConfig: EventEmitterConfig) {\n this.host = host;\n this.eventConfig = eventConfig;\n }\n\n /**\n * Emits a custom event with the specified detail.\n *\n * @param detail - The detail object to be passed along with the event.\n */\n emit(detail?: T) {\n const event = new CustomEvent(this.eventConfig.name, {\n detail: detail,\n bubbles: this.eventConfig.bubbles,\n cancelable: this.eventConfig.cancelable,\n composed: this.eventConfig.composed,\n });\n this.host.dispatchEvent(event);\n }\n}\n",
|
|
5
6
|
"import { EventEmitter, type EventEmitterConfig } from '@/tools/event-emitter';\n\nconst eventEmitters = new WeakMap();\n\n/**\n * Decorator that attaches an EventEmitter to the class field property.\n * The EventEmitter can be used to dispatch custom events from the target element.\n * @param eventConfig Configuration for the event emitter.\n * @see {@link EventEmitter} for more details about how the EventEmitter works.\n */\nexport function event(eventConfig: EventEmitterConfig) {\n return (target: any, propertyKey: string) => {\n if (!propertyKey) {\n throw new Error('The propertyKey is missing for the event decorator.');\n }\n\n if (!eventConfig || !eventConfig.name) {\n throw new Error('Invalid eventConfig provided.');\n }\n\n const uniqueKey = Symbol(eventConfig.name);\n\n Object.defineProperty(target, propertyKey, {\n get() {\n const emittersMap: Map<symbol, EventEmitter> = eventEmitters.get(this) || new Map();\n if (!emittersMap.has(uniqueKey)) {\n emittersMap.set(uniqueKey, new EventEmitter(this, eventConfig));\n eventEmitters.set(this, emittersMap);\n }\n\n return emittersMap.get(uniqueKey);\n },\n });\n };\n}\n"
|
|
6
7
|
],
|
|
7
|
-
"mappings": "
|
|
8
|
-
"debugId": "
|
|
8
|
+
"mappings": ";AAcO,MAAM,aAA0B;AAAA,EAC7B;AAAA,EACA;AAAA,EAQR,WAAW,CAAC,MAAsB,aAAiC;AACjE,SAAK,OAAO;AACZ,SAAK,cAAc;AAAA;AAAA,EAQrB,IAAI,CAAC,QAAY;AACf,UAAM,QAAQ,IAAI,YAAY,KAAK,YAAY,MAAM;AAAA,MACnD;AAAA,MACA,SAAS,KAAK,YAAY;AAAA,MAC1B,YAAY,KAAK,YAAY;AAAA,MAC7B,UAAU,KAAK,YAAY;AAAA,IAC7B,CAAC;AACD,SAAK,KAAK,cAAc,KAAK;AAAA;AAEjC;;;ACjCO,SAAS,KAAK,CAAC,aAAiC;AACrD,SAAO,CAAC,QAAa,gBAAwB;AAC3C,SAAK,aAAa;AAChB,YAAM,IAAI,MAAM,qDAAqD;AAAA,IACvE;AAEA,SAAK,gBAAgB,YAAY,MAAM;AACrC,YAAM,IAAI,MAAM,+BAA+B;AAAA,IACjD;AAEA,UAAM,YAAY,OAAO,YAAY,IAAI;AAEzC,WAAO,eAAe,QAAQ,aAAa;AAAA,MACzC,GAAG,GAAG;AACJ,cAAM,cAAyC,cAAc,IAAI,IAAI,KAAK,IAAI;AAC9E,aAAK,YAAY,IAAI,SAAS,GAAG;AAC/B,sBAAY,IAAI,WAAW,IAAI,aAAa,MAAM,WAAW,CAAC;AAC9D,wBAAc,IAAI,MAAM,WAAW;AAAA,QACrC;AAEA,eAAO,YAAY,IAAI,SAAS;AAAA;AAAA,IAEpC,CAAC;AAAA;AAAA;AA9BL,IAAM,gBAAgB,IAAI;",
|
|
9
|
+
"debugId": "64B9F802F680DEB764756E2164756E21",
|
|
9
10
|
"names": []
|
|
10
11
|
}
|
|
@@ -1,4 +1,48 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// src/decorators/on-event.ts
|
|
2
|
+
function onEvent(eventConfig) {
|
|
3
|
+
return (proto, _, descriptor) => {
|
|
4
|
+
const originalConnectedCallback = proto.connectedCallback;
|
|
5
|
+
const originalDisconnectedCallback = proto.disconnectedCallback;
|
|
6
|
+
if ("window" in eventConfig) {
|
|
7
|
+
proto.connectedCallback = function() {
|
|
8
|
+
window.addEventListener(eventConfig.type, descriptor.value.bind(this), eventConfig.options);
|
|
9
|
+
originalConnectedCallback.call(this);
|
|
10
|
+
};
|
|
11
|
+
proto.disconnectedCallback = function() {
|
|
12
|
+
window.removeEventListener(eventConfig.type, descriptor.value.bind(this), eventConfig.options);
|
|
13
|
+
originalDisconnectedCallback.call(this);
|
|
14
|
+
};
|
|
15
|
+
return descriptor;
|
|
16
|
+
}
|
|
17
|
+
if ("document" in eventConfig) {
|
|
18
|
+
proto.connectedCallback = function() {
|
|
19
|
+
document.addEventListener(eventConfig.type, descriptor.value.bind(this), eventConfig.options);
|
|
20
|
+
originalConnectedCallback.call(this);
|
|
21
|
+
};
|
|
22
|
+
proto.disconnectedCallback = function() {
|
|
23
|
+
document.removeEventListener(eventConfig.type, descriptor.value.bind(this), eventConfig.options);
|
|
24
|
+
originalDisconnectedCallback.call(this);
|
|
25
|
+
};
|
|
26
|
+
return descriptor;
|
|
27
|
+
}
|
|
28
|
+
const selector = "selector" in eventConfig ? eventConfig.selector : `[data-ref="${eventConfig.ref}"]`;
|
|
29
|
+
const originalMethod = descriptor.value;
|
|
30
|
+
const subscriptionId = `${eventConfig.type}-${selector}`;
|
|
31
|
+
proto.connectedCallback = function() {
|
|
32
|
+
this.subscribeEvent({
|
|
33
|
+
id: subscriptionId,
|
|
34
|
+
selector,
|
|
35
|
+
type: eventConfig.type,
|
|
36
|
+
listener: originalMethod.bind(this),
|
|
37
|
+
options: eventConfig?.options ?? undefined
|
|
38
|
+
});
|
|
39
|
+
originalConnectedCallback.call(this);
|
|
40
|
+
};
|
|
41
|
+
return descriptor;
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
onEvent
|
|
46
|
+
};
|
|
3
47
|
|
|
4
|
-
//# debugId=
|
|
48
|
+
//# debugId=49814E8BB4043A1364756E2164756E21
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/decorators/on-event.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"import type { RadiantElement, RadiantElementEventListener } from '@/core/radiant-element';\n\ntype OnEventConfig = Pick<RadiantElementEventListener, 'type' | 'options'> &\n (\n | {\n selector: string;\n }\n | {\n ref: string;\n }\n );\n\n/**\n * A decorator to subscribe to an event on the target element.\n * The event listener will be automatically unsubscribed when the element is disconnected.\n *\n * Note: This decorator uses event delegation, which means it relies on event bubbling.\n * Therefore, it will not work with events that do not bubble, such as `focus`, `blur`, `load`, `unload`, `scroll`, etc.\n * For focus and blur events, consider using `focusin` and `focusout` which are similar but do bubble.\n *\n * @param eventConfig The event configuration.\n * @param eventConfig.selectors The CSS selector(s) of the target element(s).\n * @param eventConfig.ref The data-ref attribute of the target element.\n * @param eventConfig.type The type of the event to listen for.\n * @param eventConfig.options Optional. An options object that specifies characteristics about the event listener.\n */\nexport function onEvent(eventConfig: OnEventConfig) {\n return (proto: RadiantElement, _: string, descriptor: PropertyDescriptor) => {\n const originalConnectedCallback = proto.connectedCallback;\n\n const selector = 'selector' in eventConfig ? eventConfig.selector : `[data-ref=\"${eventConfig.ref}\"]`;\n\n const originalMethod = descriptor.value;\n const subscriptionId = `${eventConfig.type}-${selector}`;\n\n proto.connectedCallback = function (this: RadiantElement) {\n
|
|
5
|
+
"import type { RadiantElement, RadiantElementEventListener } from '@/core/radiant-element';\n\ntype OnEventConfig = Pick<RadiantElementEventListener, 'type' | 'options'> &\n (\n | {\n selector: string;\n }\n | {\n ref: string;\n }\n | {\n window: boolean;\n }\n | {\n document: boolean;\n }\n );\n\n/**\n * A decorator to subscribe to an event on the target element.\n * The event listener will be automatically unsubscribed when the element is disconnected.\n *\n * Note: This decorator uses event delegation, which means it relies on event bubbling.\n * Therefore, it will not work with events that do not bubble, such as `focus`, `blur`, `load`, `unload`, `scroll`, etc.\n * For focus and blur events, consider using `focusin` and `focusout` which are similar but do bubble.\n *\n * @param eventConfig The event configuration.\n * @param eventConfig.selectors The CSS selector(s) of the target element(s).\n * @param eventConfig.ref The data-ref attribute of the target element.\n * @param eventConfig.type The type of the event to listen for.\n * @param eventConfig.options Optional. An options object that specifies characteristics about the event listener.\n */\nexport function onEvent(eventConfig: OnEventConfig) {\n return (proto: RadiantElement, _: string, descriptor: PropertyDescriptor) => {\n const originalConnectedCallback = proto.connectedCallback;\n const originalDisconnectedCallback = proto.disconnectedCallback;\n\n if ('window' in eventConfig) {\n proto.connectedCallback = function (this: RadiantElement) {\n window.addEventListener(eventConfig.type, descriptor.value.bind(this), eventConfig.options);\n originalConnectedCallback.call(this);\n };\n\n proto.disconnectedCallback = function (this: RadiantElement) {\n window.removeEventListener(eventConfig.type, descriptor.value.bind(this), eventConfig.options);\n originalDisconnectedCallback.call(this);\n };\n\n return descriptor;\n }\n\n if ('document' in eventConfig) {\n proto.connectedCallback = function (this: RadiantElement) {\n document.addEventListener(eventConfig.type, descriptor.value.bind(this), eventConfig.options);\n originalConnectedCallback.call(this);\n };\n\n proto.disconnectedCallback = function (this: RadiantElement) {\n document.removeEventListener(eventConfig.type, descriptor.value.bind(this), eventConfig.options);\n originalDisconnectedCallback.call(this);\n };\n\n return descriptor;\n }\n\n const selector = 'selector' in eventConfig ? eventConfig.selector : `[data-ref=\"${eventConfig.ref}\"]`;\n\n const originalMethod = descriptor.value;\n const subscriptionId = `${eventConfig.type}-${selector}`;\n\n proto.connectedCallback = function (this: RadiantElement) {\n this.subscribeEvent({\n id: subscriptionId,\n selector: selector,\n type: eventConfig.type,\n listener: originalMethod.bind(this),\n options: eventConfig?.options ?? undefined,\n });\n\n originalConnectedCallback.call(this);\n };\n\n return descriptor;\n };\n}\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": "
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": ";AAgCO,SAAS,OAAO,CAAC,aAA4B;AAClD,SAAO,CAAC,OAAuB,GAAW,eAAmC;AAC3E,UAAM,4BAA4B,MAAM;AACxC,UAAM,+BAA+B,MAAM;AAE3C,QAAI,YAAY,aAAa;AAC3B,YAAM,4BAA6B,GAAuB;AACxD,eAAO,iBAAiB,YAAY,MAAM,WAAW,MAAM,KAAK,IAAI,GAAG,YAAY,OAAO;AAC1F,kCAA0B,KAAK,IAAI;AAAA;AAGrC,YAAM,+BAAgC,GAAuB;AAC3D,eAAO,oBAAoB,YAAY,MAAM,WAAW,MAAM,KAAK,IAAI,GAAG,YAAY,OAAO;AAC7F,qCAA6B,KAAK,IAAI;AAAA;AAGxC,aAAO;AAAA,IACT;AAEA,QAAI,cAAc,aAAa;AAC7B,YAAM,4BAA6B,GAAuB;AACxD,iBAAS,iBAAiB,YAAY,MAAM,WAAW,MAAM,KAAK,IAAI,GAAG,YAAY,OAAO;AAC5F,kCAA0B,KAAK,IAAI;AAAA;AAGrC,YAAM,+BAAgC,GAAuB;AAC3D,iBAAS,oBAAoB,YAAY,MAAM,WAAW,MAAM,KAAK,IAAI,GAAG,YAAY,OAAO;AAC/F,qCAA6B,KAAK,IAAI;AAAA;AAGxC,aAAO;AAAA,IACT;AAEA,UAAM,WAAW,cAAc,cAAc,YAAY,WAAW,cAAc,YAAY;AAE9F,UAAM,iBAAiB,WAAW;AAClC,UAAM,iBAAiB,GAAG,YAAY,QAAQ;AAE9C,UAAM,4BAA6B,GAAuB;AACxD,WAAK,eAAe;AAAA,QAClB,IAAI;AAAA,QACJ;AAAA,QACA,MAAM,YAAY;AAAA,QAClB,UAAU,eAAe,KAAK,IAAI;AAAA,QAClC,SAAS,aAAa,WAAW;AAAA,MACnC,CAAC;AAED,gCAA0B,KAAK,IAAI;AAAA;AAGrC,WAAO;AAAA;AAAA;",
|
|
8
|
+
"debugId": "49814E8BB4043A1364756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
|
@@ -3,4 +3,4 @@ import type { RadiantElement } from '../core/radiant-element';
|
|
|
3
3
|
* A decorator to subscribe to an updated callback when a reactive field or property changes.
|
|
4
4
|
* @param eventConfig The event configuration.
|
|
5
5
|
*/
|
|
6
|
-
export declare function onUpdated(
|
|
6
|
+
export declare function onUpdated(keyOrKeys: string | string[]): (proto: RadiantElement, methodName: string) => void;
|
|
@@ -1,4 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// src/decorators/on-updated.ts
|
|
2
|
+
function onUpdated(keyOrKeys) {
|
|
3
|
+
return (proto, methodName) => {
|
|
4
|
+
if (!("updatesRegistry" in proto)) {
|
|
5
|
+
Object.defineProperty(proto, "updatesRegistry", {
|
|
6
|
+
value: new Map,
|
|
7
|
+
configurable: true
|
|
8
|
+
});
|
|
9
|
+
}
|
|
10
|
+
const updatesRegistry = proto.updatesRegistry;
|
|
11
|
+
if (Array.isArray(keyOrKeys)) {
|
|
12
|
+
for (const key of keyOrKeys) {
|
|
13
|
+
if (!updatesRegistry.has(key)) {
|
|
14
|
+
updatesRegistry.set(key, new Set);
|
|
15
|
+
}
|
|
16
|
+
updatesRegistry.get(key)?.add(methodName);
|
|
17
|
+
}
|
|
18
|
+
} else if (typeof keyOrKeys === "string") {
|
|
19
|
+
if (!updatesRegistry.has(keyOrKeys)) {
|
|
20
|
+
updatesRegistry.set(keyOrKeys, new Set);
|
|
21
|
+
}
|
|
22
|
+
updatesRegistry.get(keyOrKeys)?.add(methodName);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
export {
|
|
27
|
+
onUpdated
|
|
28
|
+
};
|
|
3
29
|
|
|
4
|
-
//# debugId=
|
|
30
|
+
//# debugId=AF5929F6BACB358464756E2164756E21
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/decorators/on-updated.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"import type { RadiantElement } from '@/core/radiant-element';\n\n/**\n * A decorator to subscribe to an updated callback when a reactive field or property changes.\n * @param eventConfig The event configuration.\n */\nexport function onUpdated(
|
|
5
|
+
"import type { RadiantElement } from '@/core/radiant-element';\n\n/**\n * A decorator to subscribe to an updated callback when a reactive field or property changes.\n * @param eventConfig The event configuration.\n */\nexport function onUpdated(keyOrKeys: string | string[]) {\n return (proto: RadiantElement, methodName: string) => {\n if (!('updatesRegistry' in proto)) {\n Object.defineProperty(proto, 'updatesRegistry', {\n value: new Map<string, Set<string>>(),\n configurable: true,\n });\n }\n\n const updatesRegistry = proto.updatesRegistry;\n\n if (Array.isArray(keyOrKeys)) {\n for (const key of keyOrKeys) {\n if (!updatesRegistry.has(key)) {\n updatesRegistry.set(key, new Set());\n }\n updatesRegistry.get(key)?.add(methodName);\n }\n } else if (typeof keyOrKeys === 'string') {\n if (!updatesRegistry.has(keyOrKeys)) {\n updatesRegistry.set(keyOrKeys, new Set());\n }\n updatesRegistry.get(keyOrKeys)?.add(methodName);\n }\n };\n}\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": "AAMO,SAAS,
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": ";AAMO,SAAS,SAAS,CAAC,WAA8B;AACtD,SAAO,CAAC,OAAuB,eAAuB;AACpD,UAAM,qBAAqB,QAAQ;AACjC,aAAO,eAAe,OAAO,mBAAmB;AAAA,QAC9C,OAAO,IAAI;AAAA,QACX,cAAc;AAAA,MAChB,CAAC;AAAA,IACH;AAEA,UAAM,kBAAkB,MAAM;AAE9B,QAAI,MAAM,QAAQ,SAAS,GAAG;AAC5B,iBAAW,OAAO,WAAW;AAC3B,aAAK,gBAAgB,IAAI,GAAG,GAAG;AAC7B,0BAAgB,IAAI,KAAK,IAAI,GAAK;AAAA,QACpC;AACA,wBAAgB,IAAI,GAAG,GAAG,IAAI,UAAU;AAAA,MAC1C;AAAA,IACF,kBAAkB,cAAc,UAAU;AACxC,WAAK,gBAAgB,IAAI,SAAS,GAAG;AACnC,wBAAgB,IAAI,WAAW,IAAI,GAAK;AAAA,MAC1C;AACA,sBAAgB,IAAI,SAAS,GAAG,IAAI,UAAU;AAAA,IAChD;AAAA;AAAA;",
|
|
8
|
+
"debugId": "AF5929F6BACB358464756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
package/dist/decorators/query.js
CHANGED
|
@@ -1,4 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// src/decorators/query.ts
|
|
2
|
+
function query({
|
|
3
|
+
cache: shouldBeCached = true,
|
|
4
|
+
...options
|
|
5
|
+
}) {
|
|
6
|
+
const cache = new WeakMap;
|
|
7
|
+
return (proto, propertyKey) => {
|
|
8
|
+
const doQuery = function() {
|
|
9
|
+
if (shouldBeCached) {
|
|
10
|
+
const cachedResult = cache.get(this);
|
|
11
|
+
if (cachedResult !== undefined) {
|
|
12
|
+
return cachedResult;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const selector = "selector" in options ? options.selector : `[data-ref="${options.ref}"]`;
|
|
16
|
+
const queryResult = options.all ? this.querySelectorAll(selector) : this.querySelector(selector);
|
|
17
|
+
if (shouldBeCached) {
|
|
18
|
+
cache.set(this, queryResult);
|
|
19
|
+
}
|
|
20
|
+
return queryResult;
|
|
21
|
+
};
|
|
22
|
+
const originalConnectedCallback = proto.connectedCallback;
|
|
23
|
+
proto.connectedCallback = function() {
|
|
24
|
+
Object.defineProperty(this, propertyKey, {
|
|
25
|
+
get: doQuery,
|
|
26
|
+
enumerable: true,
|
|
27
|
+
configurable: true
|
|
28
|
+
});
|
|
29
|
+
originalConnectedCallback.call(this);
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
export {
|
|
34
|
+
query
|
|
35
|
+
};
|
|
3
36
|
|
|
4
|
-
//# debugId=
|
|
37
|
+
//# debugId=E7593899A4A7CB9A64756E2164756E21
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/decorators/query.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"import type { RadiantElement } from '@/core';\n\n/**\n * The base configuration object for the query.\n */\ntype BaseQueryConfig = {\n all?: boolean;\n cache?: boolean;\n};\n\n/**\n * The configuration object for the query.\n * It can be configured to query by CSS selector or data-ref attribute.\n */\nexport type QueryConfig = BaseQueryConfig &\n (\n | {\n selector: string;\n }\n | {\n ref: string;\n }\n );\n\n/**\n * A decorator to query by CSS selector or data-ref attribute.\n * By default it queries for the first element that matches the selector, but it can be configured to query for all elements.\n *\n * @param {QueryConfig} options - The configuration object for the query.\n * @param {boolean} [options.all] - A flag to query for all elements that match the selector. Defaults to `false`.\n * @param {boolean} [options.cache] - A flag to cache the query result. Defaults to `true`.\n * @param {string} [options.selector] - A CSS selector to match elements against. This property is mutually exclusive with `options.ref`.\n * @param {string} [options.ref] - A reference to an element. This property is mutually exclusive with `options.selector`.\n *\n * @returns {Function} A decorator function that, when applied to a class property, will replace it with a getter. The getter will return the result of the query when accessed.\n *\n * @example\n * class MyElement extends HTMLElement {\n * @query({ selector: '.my-class' })\n * myElement;\n * }\n *\n * // Now, `myElement` will return the first element in the light DOM of `MyElement` that matches the selector '.my-class'.\n */\nexport function query({
|
|
5
|
+
"import type { RadiantElement } from '@/core';\n\n/**\n * The base configuration object for the query.\n */\ntype BaseQueryConfig = {\n all?: boolean;\n cache?: boolean;\n};\n\n/**\n * The configuration object for the query.\n * It can be configured to query by CSS selector or data-ref attribute.\n */\nexport type QueryConfig = BaseQueryConfig &\n (\n | {\n selector: string;\n }\n | {\n ref: string;\n }\n );\n\n/**\n * A decorator to query by CSS selector or data-ref attribute.\n * By default it queries for the first element that matches the selector, but it can be configured to query for all elements.\n *\n * @param {QueryConfig} options - The configuration object for the query.\n * @param {boolean} [options.all] - A flag to query for all elements that match the selector. Defaults to `false`.\n * @param {boolean} [options.cache] - A flag to cache the query result. Defaults to `true`.\n * @param {string} [options.selector] - A CSS selector to match elements against. This property is mutually exclusive with `options.ref`.\n * @param {string} [options.ref] - A reference to an element. This property is mutually exclusive with `options.selector`.\n *\n * @returns {Function} A decorator function that, when applied to a class property, will replace it with a getter. The getter will return the result of the query when accessed.\n *\n * @example\n * class MyElement extends HTMLElement {\n * @query({ selector: '.my-class' })\n * myElement;\n * }\n *\n * // Now, `myElement` will return the first element in the light DOM of `MyElement` that matches the selector '.my-class'.\n */\nexport function query({\n cache: shouldBeCached = true,\n ...options\n}: QueryConfig): (proto: RadiantElement, propertyKey: string | symbol) => void {\n const cache = new WeakMap<Element, Element | NodeList | null>();\n\n return (proto: RadiantElement, propertyKey: string | symbol) => {\n const doQuery = function (this: Element) {\n if (shouldBeCached) {\n const cachedResult = cache.get(this);\n if (cachedResult !== undefined) {\n return cachedResult;\n }\n }\n\n const selector = 'selector' in options ? options.selector : `[data-ref=\"${options.ref}\"]`;\n const queryResult = options.all ? this.querySelectorAll(selector) : this.querySelector(selector);\n\n if (shouldBeCached) {\n cache.set(this, queryResult);\n }\n\n return queryResult;\n };\n\n const originalConnectedCallback = proto.connectedCallback;\n\n proto.connectedCallback = function (this: RadiantElement) {\n Object.defineProperty(this, propertyKey, {\n get: doQuery,\n enumerable: true,\n configurable: true,\n });\n\n originalConnectedCallback.call(this);\n };\n };\n}\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": "AA4CO,SAAS,
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": ";AA4CO,SAAS,KAAK;AAAA,EACnB,OAAO,iBAAiB;AAAA,KACrB;AAAA,GAC0E;AAC7E,QAAM,QAAQ,IAAI;AAElB,SAAO,CAAC,OAAuB,gBAAiC;AAC9D,UAAM,kBAAmB,GAAgB;AACvC,UAAI,gBAAgB;AAClB,cAAM,eAAe,MAAM,IAAI,IAAI;AACnC,YAAI,iBAAiB,WAAW;AAC9B,iBAAO;AAAA,QACT;AAAA,MACF;AAEA,YAAM,WAAW,cAAc,UAAU,QAAQ,WAAW,cAAc,QAAQ;AAClF,YAAM,cAAc,QAAQ,MAAM,KAAK,iBAAiB,QAAQ,IAAI,KAAK,cAAc,QAAQ;AAE/F,UAAI,gBAAgB;AAClB,cAAM,IAAI,MAAM,WAAW;AAAA,MAC7B;AAEA,aAAO;AAAA;AAGT,UAAM,4BAA4B,MAAM;AAExC,UAAM,4BAA6B,GAAuB;AACxD,aAAO,eAAe,MAAM,aAAa;AAAA,QACvC,KAAK;AAAA,QACL,YAAY;AAAA,QACZ,cAAc;AAAA,MAChB,CAAC;AAED,gCAA0B,KAAK,IAAI;AAAA;AAAA;AAAA;",
|
|
8
|
+
"debugId": "E7593899A4A7CB9A64756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
|
@@ -1,4 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// src/decorators/reactive-field.ts
|
|
2
|
+
function reactiveField(proto, propertyKey) {
|
|
3
|
+
const originalValues = new WeakMap;
|
|
4
|
+
const isDefined = new WeakSet;
|
|
5
|
+
Object.defineProperty(proto, propertyKey, {
|
|
6
|
+
get: function() {
|
|
7
|
+
return originalValues.get(this);
|
|
8
|
+
},
|
|
9
|
+
set: function(newValue) {
|
|
10
|
+
if (isDefined.has(this)) {
|
|
11
|
+
const oldValue = originalValues.get(this);
|
|
12
|
+
if (oldValue !== newValue) {
|
|
13
|
+
originalValues.set(this, newValue);
|
|
14
|
+
this.updated(propertyKey, oldValue, newValue);
|
|
15
|
+
}
|
|
16
|
+
} else {
|
|
17
|
+
originalValues.set(this, newValue);
|
|
18
|
+
isDefined.add(this);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
export {
|
|
24
|
+
reactiveField
|
|
25
|
+
};
|
|
3
26
|
|
|
4
|
-
//# debugId=
|
|
27
|
+
//# debugId=6BDE160AC013151B64756E2164756E21
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
"sourcesContent": [
|
|
5
5
|
"import type { RadiantElement } from '@/core/radiant-element';\n\n/**\n * A decorator to define a reactive field.\n * Every time the property changes, the `updated` method will be called.\n * Due the fact the value is always undefined before the first update,\n * we are adding a `isDefined` WeakSet to track if the property has been defined.\n * @param target The target element.\n * @param propertyKey The property key.\n */\nexport function reactiveField(proto: RadiantElement, propertyKey: string) {\n const originalValues = new WeakMap<WeakKey, unknown>();\n const isDefined = new WeakSet<WeakKey>();\n\n Object.defineProperty(proto, propertyKey, {\n get: function () {\n return originalValues.get(this);\n },\n set: function (newValue: unknown) {\n if (isDefined.has(this)) {\n const oldValue = originalValues.get(this);\n if (oldValue !== newValue) {\n originalValues.set(this, newValue);\n this.updated(propertyKey, oldValue, newValue);\n }\n } else {\n originalValues.set(this, newValue);\n isDefined.add(this);\n }\n },\n });\n}\n"
|
|
6
6
|
],
|
|
7
|
-
"mappings": "AAUO,SAAS,
|
|
8
|
-
"debugId": "
|
|
7
|
+
"mappings": ";AAUO,SAAS,aAAa,CAAC,OAAuB,aAAqB;AACxE,QAAM,iBAAiB,IAAI;AAC3B,QAAM,YAAY,IAAI;AAEtB,SAAO,eAAe,OAAO,aAAa;AAAA,IACxC,aAAc,GAAG;AACf,aAAO,eAAe,IAAI,IAAI;AAAA;AAAA,IAEhC,aAAc,CAAC,UAAmB;AAChC,UAAI,UAAU,IAAI,IAAI,GAAG;AACvB,cAAM,WAAW,eAAe,IAAI,IAAI;AACxC,YAAI,aAAa,UAAU;AACzB,yBAAe,IAAI,MAAM,QAAQ;AACjC,eAAK,QAAQ,aAAa,UAAU,QAAQ;AAAA,QAC9C;AAAA,MACF,OAAO;AACL,uBAAe,IAAI,MAAM,QAAQ;AACjC,kBAAU,IAAI,IAAI;AAAA;AAAA;AAAA,EAGxB,CAAC;AAAA;",
|
|
8
|
+
"debugId": "6BDE160AC013151B64756E2164756E21",
|
|
9
9
|
"names": []
|
|
10
10
|
}
|
|
@@ -15,5 +15,5 @@ type ReactivePropertyOptions<T> = {
|
|
|
15
15
|
* @param options.attribute The name of the attribute.
|
|
16
16
|
* @param options.defaultValue The default value of the property.
|
|
17
17
|
*/
|
|
18
|
-
export declare function reactiveProp<T = unknown>({ type, attribute, reflect, defaultValue }: ReactivePropertyOptions<T>): (
|
|
18
|
+
export declare function reactiveProp<T = unknown>({ type, attribute, reflect, defaultValue }: ReactivePropertyOptions<T>): (target: RadiantElement, propertyName: string) => void;
|
|
19
19
|
export {};
|