@htmlplus/element 2.5.1 → 2.7.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/client/decorators/context.d.ts +3 -0
- package/client/decorators/context.js +114 -0
- package/client/decorators/element.js +11 -7
- package/client/decorators/index.d.ts +1 -2
- package/client/decorators/index.js +1 -2
- package/package.json +1 -1
- package/transformer/plugins/customElement.js +10 -3
- package/client/decorators/consumer.d.ts +0 -6
- package/client/decorators/consumer.js +0 -21
- package/client/decorators/provider.d.ts +0 -6
- package/client/decorators/provider.js +0 -31
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { HTMLPlusElement } from '../../types/index.js';
|
|
2
|
+
export declare function Provider(namespace: string): (target: HTMLPlusElement, key: PropertyKey, descriptor: PropertyDescriptor) => void;
|
|
3
|
+
export declare function Consumer(namespace: string): (target: HTMLPlusElement, key: PropertyKey) => void;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import * as CONSTANTS from '../../constants/index.js';
|
|
2
|
+
import { appendToMethod, dispatch, off, on } from '../utils/index.js';
|
|
3
|
+
export function Provider(namespace) {
|
|
4
|
+
return function (target, key, descriptor) {
|
|
5
|
+
const symbol = Symbol();
|
|
6
|
+
const [MAIN, SUB] = namespace.split('.');
|
|
7
|
+
const prefix = `htmlplus:${MAIN}`;
|
|
8
|
+
const cleanups = (instance) => {
|
|
9
|
+
return (instance[symbol] || (instance[symbol] = new Map()));
|
|
10
|
+
};
|
|
11
|
+
const update = (instance) => {
|
|
12
|
+
const options = {};
|
|
13
|
+
options.detail = descriptor.get.call(instance);
|
|
14
|
+
dispatch(instance, `${prefix}:update`, options);
|
|
15
|
+
if (!SUB)
|
|
16
|
+
return;
|
|
17
|
+
options.bubbles = true;
|
|
18
|
+
dispatch(instance, `${prefix}:${instance[SUB]}:update`, options);
|
|
19
|
+
};
|
|
20
|
+
// TODO
|
|
21
|
+
appendToMethod(target, CONSTANTS.LIFECYCLE_CONNECTED, function () {
|
|
22
|
+
const cleanup = () => {
|
|
23
|
+
off(this, `${prefix}:presence`, onPresence);
|
|
24
|
+
cleanups(this).delete(prefix);
|
|
25
|
+
};
|
|
26
|
+
const onPresence = (event) => {
|
|
27
|
+
event.stopPropagation();
|
|
28
|
+
event.detail(this, descriptor.get.call(this));
|
|
29
|
+
};
|
|
30
|
+
on(this, `${prefix}:presence`, onPresence);
|
|
31
|
+
cleanups(this).set(prefix, cleanup);
|
|
32
|
+
});
|
|
33
|
+
appendToMethod(target, CONSTANTS.LIFECYCLE_UPDATE, function (states) {
|
|
34
|
+
var _a;
|
|
35
|
+
update(this);
|
|
36
|
+
if (cleanups(this).size && !states.has(SUB))
|
|
37
|
+
return;
|
|
38
|
+
(_a = cleanups(this).get(`${prefix}:${states.get(SUB)}`)) === null || _a === void 0 ? void 0 : _a();
|
|
39
|
+
const type = `${prefix}:${this[SUB]}`;
|
|
40
|
+
const cleanup = () => {
|
|
41
|
+
off(window, `${type}:presence`, onPresence);
|
|
42
|
+
cleanups(this).delete(type);
|
|
43
|
+
dispatch(window, `${type}:disconnect`);
|
|
44
|
+
};
|
|
45
|
+
const onPresence = () => {
|
|
46
|
+
update(this);
|
|
47
|
+
};
|
|
48
|
+
on(window, `${type}:presence`, onPresence);
|
|
49
|
+
cleanups(this).set(type, cleanup);
|
|
50
|
+
});
|
|
51
|
+
appendToMethod(target, CONSTANTS.LIFECYCLE_DISCONNECTED, function () {
|
|
52
|
+
cleanups(this).forEach((cleanup) => cleanup());
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
export function Consumer(namespace) {
|
|
57
|
+
return function (target, key) {
|
|
58
|
+
const symbol = Symbol();
|
|
59
|
+
const [MAIN, SUB] = namespace.split('.');
|
|
60
|
+
const prefix = `htmlplus:${MAIN}`;
|
|
61
|
+
const cleanups = (instance) => {
|
|
62
|
+
return (instance[symbol] || (instance[symbol] = new Map()));
|
|
63
|
+
};
|
|
64
|
+
const update = (instance, state) => {
|
|
65
|
+
instance[key] = state;
|
|
66
|
+
};
|
|
67
|
+
// TODO
|
|
68
|
+
appendToMethod(target, CONSTANTS.LIFECYCLE_CONNECTED, function () {
|
|
69
|
+
const options = {
|
|
70
|
+
bubbles: true
|
|
71
|
+
};
|
|
72
|
+
options.detail = (parent, state) => {
|
|
73
|
+
update(this, state);
|
|
74
|
+
const cleanup = () => {
|
|
75
|
+
off(parent, `${prefix}:update`, onUpdate);
|
|
76
|
+
cleanups(this).delete(prefix);
|
|
77
|
+
update(this, undefined);
|
|
78
|
+
};
|
|
79
|
+
const onUpdate = (event) => {
|
|
80
|
+
update(this, event.detail);
|
|
81
|
+
};
|
|
82
|
+
on(parent, `${prefix}:update`, onUpdate);
|
|
83
|
+
cleanups(this).set(prefix, cleanup);
|
|
84
|
+
};
|
|
85
|
+
dispatch(this, `${prefix}:presence`, options);
|
|
86
|
+
});
|
|
87
|
+
appendToMethod(target, CONSTANTS.LIFECYCLE_UPDATE, function (states) {
|
|
88
|
+
var _a;
|
|
89
|
+
if (cleanups(this).size && !states.has(SUB))
|
|
90
|
+
return;
|
|
91
|
+
(_a = cleanups(this).get(`${prefix}:${states.get(SUB)}`)) === null || _a === void 0 ? void 0 : _a();
|
|
92
|
+
const type = `${prefix}:${this[SUB]}`;
|
|
93
|
+
const cleanup = () => {
|
|
94
|
+
off(window, `${type}:disconnect`, onDisconnect);
|
|
95
|
+
off(window, `${type}:update`, onUpdate);
|
|
96
|
+
cleanups(this).delete(type);
|
|
97
|
+
update(this, undefined);
|
|
98
|
+
};
|
|
99
|
+
const onDisconnect = () => {
|
|
100
|
+
update(this, undefined);
|
|
101
|
+
};
|
|
102
|
+
const onUpdate = (event) => {
|
|
103
|
+
update(this, event.detail);
|
|
104
|
+
};
|
|
105
|
+
on(window, `${type}:disconnect`, onDisconnect);
|
|
106
|
+
on(window, `${type}:update`, onUpdate);
|
|
107
|
+
cleanups(this).set(type, cleanup);
|
|
108
|
+
dispatch(window, `${type}:presence`);
|
|
109
|
+
});
|
|
110
|
+
appendToMethod(target, CONSTANTS.LIFECYCLE_DISCONNECTED, function () {
|
|
111
|
+
cleanups(this).forEach((cleanup) => cleanup());
|
|
112
|
+
});
|
|
113
|
+
};
|
|
114
|
+
}
|
|
@@ -13,7 +13,12 @@ export function Element() {
|
|
|
13
13
|
const tag = getTag(constructor);
|
|
14
14
|
if (customElements.get(tag))
|
|
15
15
|
return;
|
|
16
|
-
|
|
16
|
+
customElements.define(tag, proxy(constructor));
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
const proxy = (constructor) => {
|
|
20
|
+
var _a;
|
|
21
|
+
return _a = class Plus extends HTMLElement {
|
|
17
22
|
constructor() {
|
|
18
23
|
super();
|
|
19
24
|
this.attachShadow({
|
|
@@ -54,11 +59,10 @@ export function Element() {
|
|
|
54
59
|
disconnectedCallback() {
|
|
55
60
|
call(this[CONSTANTS.API_INSTANCE], CONSTANTS.LIFECYCLE_DISCONNECTED);
|
|
56
61
|
}
|
|
57
|
-
}
|
|
62
|
+
},
|
|
58
63
|
// TODO
|
|
59
|
-
|
|
64
|
+
_a.formAssociated = constructor['formAssociated'],
|
|
60
65
|
// TODO
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}
|
|
66
|
+
_a.observedAttributes = constructor['observedAttributes'],
|
|
67
|
+
_a;
|
|
68
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export * from './bind.js';
|
|
2
|
-
export * from './
|
|
2
|
+
export * from './context.js';
|
|
3
3
|
export * from './direction.js';
|
|
4
4
|
export * from './element.js';
|
|
5
5
|
export * from './event.js';
|
|
@@ -8,7 +8,6 @@ export * from './isRTL.js';
|
|
|
8
8
|
export * from './listen.js';
|
|
9
9
|
export * from './method.js';
|
|
10
10
|
export * from './property.js';
|
|
11
|
-
export * from './provider.js';
|
|
12
11
|
export * from './query.js';
|
|
13
12
|
export * from './queryAll.js';
|
|
14
13
|
export * from './slots.js';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export * from './bind.js';
|
|
2
|
-
export * from './
|
|
2
|
+
export * from './context.js';
|
|
3
3
|
export * from './direction.js';
|
|
4
4
|
export * from './element.js';
|
|
5
5
|
export * from './event.js';
|
|
@@ -8,7 +8,6 @@ export * from './isRTL.js';
|
|
|
8
8
|
export * from './listen.js';
|
|
9
9
|
export * from './method.js';
|
|
10
10
|
export * from './property.js';
|
|
11
|
-
export * from './provider.js';
|
|
12
11
|
export * from './query.js';
|
|
13
12
|
export * from './queryAll.js';
|
|
14
13
|
export * from './slots.js';
|
package/package.json
CHANGED
|
@@ -280,7 +280,9 @@ export const customElement = (options) => {
|
|
|
280
280
|
if (options.typings) {
|
|
281
281
|
visitor(ast, {
|
|
282
282
|
Program(path) {
|
|
283
|
-
const attributes = context
|
|
283
|
+
const attributes = context
|
|
284
|
+
.classProperties.filter((property) => !t.isClassMethod(property))
|
|
285
|
+
.map((property) => {
|
|
284
286
|
const key = property.key;
|
|
285
287
|
const typeAnnotation = property.typeAnnotation;
|
|
286
288
|
return Object.assign(t.tSPropertySignature(t.stringLiteral(kebabCase(key.name)), typeAnnotation), {
|
|
@@ -310,8 +312,13 @@ export const customElement = (options) => {
|
|
|
310
312
|
});
|
|
311
313
|
const properties = context.classProperties.map((property) => {
|
|
312
314
|
const key = property.key;
|
|
313
|
-
|
|
314
|
-
|
|
315
|
+
// TODO
|
|
316
|
+
const readonly = property.readonly || !!property['returnType'];
|
|
317
|
+
// TODO
|
|
318
|
+
const typeAnnotation = (property.typeAnnotation ||
|
|
319
|
+
property['returnType']);
|
|
320
|
+
return Object.assign(t.tsPropertySignature(t.identifier(key.name), typeAnnotation), {
|
|
321
|
+
readonly,
|
|
315
322
|
optional: property.optional,
|
|
316
323
|
leadingComments: t.cloneNode(property, true).leadingComments
|
|
317
324
|
});
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import * as CONSTANTS from '../../constants/index.js';
|
|
2
|
-
import { appendToMethod, dispatch, host } from '../utils/index.js';
|
|
3
|
-
/**
|
|
4
|
-
* TODO
|
|
5
|
-
* @param namespace
|
|
6
|
-
*/
|
|
7
|
-
export function Consumer(namespace) {
|
|
8
|
-
return function (target, key) {
|
|
9
|
-
appendToMethod(target, CONSTANTS.LIFECYCLE_CONSTRUCTED, function () {
|
|
10
|
-
const options = {
|
|
11
|
-
bubbles: true
|
|
12
|
-
};
|
|
13
|
-
options.detail = (state) => {
|
|
14
|
-
this[key] = state;
|
|
15
|
-
const successful = !!host(this).parentElement;
|
|
16
|
-
return successful;
|
|
17
|
-
};
|
|
18
|
-
dispatch(this, `internal:context:${namespace}`, options);
|
|
19
|
-
});
|
|
20
|
-
};
|
|
21
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import * as CONSTANTS from '../../constants/index.js';
|
|
2
|
-
import { appendToMethod, on } from '../utils/index.js';
|
|
3
|
-
/**
|
|
4
|
-
* TODO
|
|
5
|
-
* @param namespace
|
|
6
|
-
*/
|
|
7
|
-
export function Provider(namespace) {
|
|
8
|
-
return function (target, key, descriptor) {
|
|
9
|
-
const symbol = Symbol();
|
|
10
|
-
const update = (instance) => (updater) => {
|
|
11
|
-
const state = descriptor.get.call(instance);
|
|
12
|
-
const successful = updater(state);
|
|
13
|
-
if (successful)
|
|
14
|
-
return;
|
|
15
|
-
instance[symbol].delete(updater);
|
|
16
|
-
};
|
|
17
|
-
appendToMethod(target, CONSTANTS.LIFECYCLE_CONSTRUCTED, function () {
|
|
18
|
-
this[symbol] || (this[symbol] = new Set());
|
|
19
|
-
const handler = (event) => {
|
|
20
|
-
event.stopPropagation();
|
|
21
|
-
const updater = event.detail;
|
|
22
|
-
this[symbol].add(updater);
|
|
23
|
-
update(this)(updater);
|
|
24
|
-
};
|
|
25
|
-
on(this, `internal:context:${namespace}`, handler);
|
|
26
|
-
});
|
|
27
|
-
appendToMethod(target, CONSTANTS.LIFECYCLE_UPDATED, function () {
|
|
28
|
-
this[symbol].forEach(update(this));
|
|
29
|
-
});
|
|
30
|
-
};
|
|
31
|
-
}
|