compelem 0.2.0-beta → 0.2.2-beta
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/CompElem.d.ts +2 -2
- package/README.md +66 -53
- package/decorator/Decorator.d.ts +8 -0
- package/decorator/index.d.ts +3 -2
- package/decorators/prop.d.ts +1 -8
- package/decorators/query.d.ts +2 -2
- package/decorators/watch.d.ts +1 -1
- package/directives/Model.d.ts +1 -1
- package/index.js +2 -3143
- package/package.json +3 -2
- package/utils.d.ts +3 -1
package/CompElem.d.ts
CHANGED
|
@@ -26,7 +26,7 @@ declare const CompElem_base: {
|
|
|
26
26
|
*
|
|
27
27
|
* @author holyhigh2
|
|
28
28
|
*/
|
|
29
|
-
export declare
|
|
29
|
+
export declare class CompElem extends CompElem_base implements IComponent {
|
|
30
30
|
#private;
|
|
31
31
|
static __l_globalRule: HTMLStyleElement;
|
|
32
32
|
__events: Record<string, Array<Node | ((e: Event) => void)>[]>;
|
|
@@ -66,7 +66,7 @@ export declare abstract class CompElem extends CompElem_base implements ICompone
|
|
|
66
66
|
/**
|
|
67
67
|
* 每次更新时调用
|
|
68
68
|
*/
|
|
69
|
-
|
|
69
|
+
render(): Template;
|
|
70
70
|
/**
|
|
71
71
|
* dom渲染完毕后调用,该回调内可以query注解初始化完成
|
|
72
72
|
*/
|
package/README.md
CHANGED
|
@@ -1,111 +1,101 @@
|
|
|
1
1
|
# CompElem
|
|
2
|
+
一个现代化、响应式、快速、轻量的WebComponent开发库。为开发者提供丰富、灵活、可扩展的声明式接口
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
## 概览
|
|
6
|
-
|
|
4
|
+
## 概览
|
|
7
5
|
CompElem 基于 Class 进行构建,该模型允许开发者使用装饰器进行声明式编码,核心特性包括:
|
|
8
|
-
|
|
9
|
-
- 类 JSX 的原生模板系统
|
|
6
|
+
- 类JSX的原生模板系统
|
|
10
7
|
- 丰富的装饰器及指令
|
|
11
8
|
- 可选的生命周期
|
|
12
9
|
- 原生插槽系统
|
|
13
10
|
- 响应式域样式
|
|
14
11
|
- ...
|
|
15
12
|
|
|
16
|
-
创建一个
|
|
17
|
-
|
|
13
|
+
创建一个WebComponent总会从声明一个组件元素(CompElem子类)开始
|
|
18
14
|
```ts
|
|
19
|
-
const Slogan = [
|
|
15
|
+
const Slogan = ['complete', 'componentize', 'compact', 'companion']
|
|
20
16
|
@tag("page-test")
|
|
21
17
|
export class PageTest extends CompElem {
|
|
22
18
|
//////////////////////////////////// props
|
|
23
|
-
@prop arg:
|
|
19
|
+
@prop arg:any
|
|
24
20
|
|
|
25
|
-
@state colorR =
|
|
26
|
-
@state colorG =
|
|
27
|
-
@state colorB =
|
|
28
|
-
@state rotation = 0
|
|
21
|
+
@state colorR = Math.random() * 255 % 255 >> 0;
|
|
22
|
+
@state colorG = Math.random() * 255 % 255 >> 0;
|
|
23
|
+
@state colorB = Math.random() * 255 % 255 >> 0;
|
|
24
|
+
@state rotation = 0
|
|
29
25
|
|
|
30
26
|
//////////////////////////////////// computed
|
|
31
27
|
@computed
|
|
32
28
|
get color() {
|
|
33
|
-
return `linear-gradient(90deg,rgb(${this.colorR},${this.colorG},${
|
|
34
|
-
this.colorB
|
|
35
|
-
}), rgb(${255 - this.colorR},${255 - this.colorG},${255 - this.colorB}));`;
|
|
29
|
+
return `linear-gradient(90deg,rgb(${this.colorR},${this.colorG},${this.colorB}), rgb(${255 - this.colorR},${255 - this.colorG},${255 - this.colorB}));`
|
|
36
30
|
}
|
|
37
31
|
|
|
38
32
|
//////////////////////////////////// watch
|
|
39
|
-
@watch(
|
|
40
|
-
function(nv:
|
|
41
|
-
console.log(nv)
|
|
33
|
+
@watch('rotation')
|
|
34
|
+
function(nv:number) {
|
|
35
|
+
console.log(nv)
|
|
42
36
|
}
|
|
43
37
|
|
|
44
38
|
//////////////////////////////////// styles
|
|
45
39
|
//静态样式
|
|
46
40
|
static get styles(): Array<string | CSSStyleSheet> {
|
|
47
|
-
return [
|
|
48
|
-
`:host{
|
|
41
|
+
return [`:host{
|
|
49
42
|
font-size:16px;
|
|
50
|
-
}
|
|
51
|
-
];
|
|
43
|
+
}...`];
|
|
52
44
|
}
|
|
53
45
|
//动态样式
|
|
54
46
|
get css() {
|
|
55
47
|
return `h2,p,i,h3{
|
|
56
48
|
background-image:${this.color}
|
|
57
49
|
filter:hue-rotate(${this.rotation}deg)
|
|
58
|
-
}
|
|
50
|
+
}`
|
|
59
51
|
}
|
|
60
52
|
|
|
61
53
|
@query('i[name="text"]')
|
|
62
|
-
text: HTMLElement
|
|
63
|
-
sloganIndex = 0
|
|
54
|
+
text: HTMLElement
|
|
55
|
+
sloganIndex = 0
|
|
64
56
|
|
|
65
57
|
//////////////////////////////////// lifecycles
|
|
66
58
|
mounted(): void {
|
|
67
59
|
setInterval(() => {
|
|
68
|
-
this.rotation += 1
|
|
60
|
+
this.rotation += 1
|
|
69
61
|
}, 24);
|
|
70
62
|
|
|
71
63
|
setInterval(() => {
|
|
72
|
-
this.text.classList.add(
|
|
64
|
+
this.text.classList.add('hide')
|
|
73
65
|
setTimeout(() => {
|
|
74
|
-
this.text.innerHTML = Slogan[this.sloganIndex % 4]
|
|
75
|
-
this.sloganIndex
|
|
76
|
-
this.text.classList.remove(
|
|
66
|
+
this.text.innerHTML = Slogan[this.sloganIndex % 4]
|
|
67
|
+
this.sloganIndex++
|
|
68
|
+
this.text.classList.remove('hide')
|
|
77
69
|
}, 500);
|
|
78
70
|
}, 5000);
|
|
79
71
|
}
|
|
80
72
|
render(): Template {
|
|
81
73
|
return html`<div>
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
74
|
+
<i>Welcome to</i>
|
|
75
|
+
<br>
|
|
76
|
+
<h2>CompElem</h2>
|
|
77
|
+
<br>
|
|
78
|
+
<i>A modern, reactive, fast and lightweight library</i>
|
|
79
|
+
<br>
|
|
80
|
+
<i>for building</i>
|
|
81
|
+
<h3>Web Components</h3>
|
|
82
|
+
<p>
|
|
83
|
+
<c-element> <i name="text">...</i> </c-element>
|
|
84
|
+
</p>
|
|
85
|
+
${this.arg}
|
|
86
|
+
</div>`
|
|
93
87
|
}
|
|
94
88
|
}
|
|
95
89
|
```
|
|
96
|
-
|
|
97
|
-
而后即可在 HTML 中直接使用,与使用一个原生元素如 DIV 没有任何区别
|
|
98
|
-
|
|
90
|
+
而后即可在HTML中直接使用,与使用一个原生元素如DIV没有任何区别
|
|
99
91
|
```html
|
|
100
92
|
<body>
|
|
101
|
-
|
|
93
|
+
<page-test arg="args..."></page-test>
|
|
102
94
|
</body>
|
|
103
95
|
```
|
|
96
|
+
当然,也可以直接嵌入其他UI库中只要引入编译后的js即可
|
|
104
97
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
## APIs
|
|
108
|
-
|
|
98
|
+
## APIs
|
|
109
99
|
- ### 视图模板
|
|
110
100
|
使用`render()`函数定义组件视图模板
|
|
111
101
|
```ts
|
|
@@ -180,6 +170,22 @@ export class PageTest extends CompElem {
|
|
|
180
170
|
属性可以在组件内修改但默认不会同步父组件,除非显式指定`sync`或自行 emit update 事件
|
|
181
171
|
全部注解参数见 `PropOption`
|
|
182
172
|
|
|
173
|
+
> 在某些无法使用装饰器的场景中(如MixinClass),可以使用函数`makeProp(...)`定义prop
|
|
174
|
+
```ts
|
|
175
|
+
export function Loadable<T extends Constructor<any>>(spuerClass: T) {
|
|
176
|
+
return class Loadable extends spuerClass {
|
|
177
|
+
//declare a prop
|
|
178
|
+
name: string
|
|
179
|
+
|
|
180
|
+
constructor(...args: any[]) {
|
|
181
|
+
super()
|
|
182
|
+
//make the prop reactive
|
|
183
|
+
Decorator.call(this, prop, 'name', { type: String })
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
```
|
|
188
|
+
|
|
183
189
|
- ### 状态
|
|
184
190
|
状态是仅由组件内部初始化的响应变量,可通过`@state`注解定义
|
|
185
191
|
```ts
|
|
@@ -374,7 +380,7 @@ static get autoSlot() {
|
|
|
374
380
|
| ------- | --------- | ----------------------------------------------------------- | ------------------------------------------------------ |
|
|
375
381
|
| bind | TAG | 绑定属性/特性到标签上,根据标签类型及组件 prop 定义自动判断 | `<div a="b" ${bind(obj)}>` |
|
|
376
382
|
| show | TAG | 隐藏/显示标签(基于 display) | `<div a="b" ${show(visible)}>` |
|
|
377
|
-
| model | TAG | 双向绑定 | `<div a="b" ${model(xx)}>` |
|
|
383
|
+
| model | TAG | 双向绑定 | `<div a="b" ${model(xx,modelPath?)}>` |
|
|
378
384
|
| classes | CLASS | 绑定样式类属性,支持对象/数组/字符串。可以和静态字符混用 | `<div class="otherClass ${classes(obj)}>"` |
|
|
379
385
|
| styles | STYLE | 绑定样式规则属性,支持对象/字符串。可以和静态字符混用 | `<div style="a:b;${styles(obj)}>"` |
|
|
380
386
|
| forEach | TEXT/SLOT | 输出循环结构 | `...>${forEach(ary,(item)=>html`...`)}<...` |
|
|
@@ -393,10 +399,17 @@ static get autoSlot() {
|
|
|
393
399
|
- @event 定义全局事件
|
|
394
400
|
- @watch 监控 state/prop 变更
|
|
395
401
|
|
|
402
|
+
> 装饰器还可通过函数方式进行调用,如
|
|
403
|
+
> ```ts
|
|
404
|
+
> //Decorator.call(class, decorator, fieldName, ...args)
|
|
405
|
+
> Decorator.call(this, prop, 'name', { type: String })
|
|
406
|
+
> ```
|
|
407
|
+
> **注意**,调用入口必须放在类的构造函数中
|
|
408
|
+
|
|
396
409
|
## 事件
|
|
397
410
|
|
|
398
411
|
事件分为三类
|
|
399
412
|
|
|
400
413
|
1. 原生事件 —— `<div @click="..."`,监听器回调参数返回原生事件对象
|
|
401
414
|
2. 组件自定义事件 —— `<l-select @change="..."`,监听器回调参数返回`CustomEvent`事件对象
|
|
402
|
-
3. 扩展原生事件 —— `<div @resize="..."`,监听器回调参数返回`CustomEvent`事件对象
|
|
415
|
+
3. 扩展原生事件 —— `<div @resize="..."`,监听器回调参数返回`CustomEvent`事件对象
|
package/decorator/Decorator.d.ts
CHANGED
|
@@ -9,6 +9,14 @@ export declare enum DecoratorType {
|
|
|
9
9
|
* @author holyhigh2
|
|
10
10
|
*/
|
|
11
11
|
export declare abstract class Decorator {
|
|
12
|
+
/**
|
|
13
|
+
* 通过函数方式进行装饰器调用
|
|
14
|
+
* @param target 所在类
|
|
15
|
+
* @param deco 装饰器函数
|
|
16
|
+
* @param fieldName 装饰器应用的字段名/函数名/函数
|
|
17
|
+
* @param args 不定参数
|
|
18
|
+
*/
|
|
19
|
+
static call(target: any, deco: Function, fieldName: string | Function, ...args: any[]): void;
|
|
12
20
|
/**
|
|
13
21
|
* 装饰器使用范围,超出范围会报错
|
|
14
22
|
*/
|
package/decorator/index.d.ts
CHANGED
|
@@ -6,7 +6,8 @@ import { Constructor } from './../types';
|
|
|
6
6
|
import { CompElem } from "../CompElem";
|
|
7
7
|
import { Decorator } from "./Decorator";
|
|
8
8
|
export declare const GetKeyFnName = "getKey";
|
|
9
|
-
export declare const
|
|
9
|
+
export declare const _DecoratorsKey = "__decorators";
|
|
10
|
+
export declare const _DecoratorMap: WeakMap<WeakKey, any>;
|
|
10
11
|
/**
|
|
11
12
|
* 装饰器包装类
|
|
12
13
|
* 用于框架内部,表示class上的一个装饰器属性定义
|
|
@@ -29,4 +30,4 @@ export declare class DecoratorWrapper {
|
|
|
29
30
|
* @param decoClass 装饰器构造
|
|
30
31
|
* @returns 装饰器函数
|
|
31
32
|
*/
|
|
32
|
-
export declare function decorator<T extends Array<any>>(decoClass: Constructor<Decorator>): (...args: T) => (...metadata: any[]) =>
|
|
33
|
+
export declare function decorator<T extends Array<any>>(decoClass: Constructor<Decorator>): (...args: T) => (...metadata: any[]) => any;
|
package/decorators/prop.d.ts
CHANGED
|
@@ -54,12 +54,5 @@ export type PropOption = {
|
|
|
54
54
|
* @param options 可选参数 PropOption,如果type未定义则根据默认值自动推断类型
|
|
55
55
|
*/
|
|
56
56
|
export declare function prop(options: PropOption): (target: any, propertyKey: any) => void;
|
|
57
|
-
export declare function prop(target: any, propertyKey: any): void;
|
|
58
|
-
/**
|
|
59
|
-
* 同@prop装饰器,但可用于构造器中调用
|
|
60
|
-
* @param ctor 类构造函数
|
|
61
|
-
* @param propertyKey prop名称
|
|
62
|
-
* @param options
|
|
63
|
-
*/
|
|
64
|
-
export declare function makeProp(ctor: Function, propertyKey: string, options?: PropOption): void;
|
|
57
|
+
export declare function prop(target: any, propertyKey: any, options?: PropOption): void;
|
|
65
58
|
export declare function _getObservedAttrs(ctor: Function): Set<string>;
|
package/decorators/query.d.ts
CHANGED
|
@@ -22,5 +22,5 @@ export type QueryOption = {
|
|
|
22
22
|
*/
|
|
23
23
|
cache?: string;
|
|
24
24
|
};
|
|
25
|
-
export declare const query: (selector: string, cache?: QueryCache | undefined) => (...metadata: any[]) =>
|
|
26
|
-
export declare const queryAll: (selector: string, cache?: QueryCache | undefined) => (...metadata: any[]) =>
|
|
25
|
+
export declare const query: (selector: string, cache?: QueryCache | undefined) => (...metadata: any[]) => any;
|
|
26
|
+
export declare const queryAll: (selector: string, cache?: QueryCache | undefined) => (...metadata: any[]) => any;
|
package/decorators/watch.d.ts
CHANGED
|
@@ -23,4 +23,4 @@ export type DecoratorWatch = {
|
|
|
23
23
|
handler: WatchHandler;
|
|
24
24
|
options: WatchOptions;
|
|
25
25
|
};
|
|
26
|
-
export declare const watch: (source: string | string[], options?: WatchOptions | undefined) => (...metadata: any[]) =>
|
|
26
|
+
export declare const watch: (source: string | string[], options?: WatchOptions | undefined, handler?: WatchHandler | undefined) => (...metadata: any[]) => any;
|
package/directives/Model.d.ts
CHANGED
|
@@ -2,4 +2,4 @@ export declare const enum ModelTriggerType {
|
|
|
2
2
|
CHANGE = "change",
|
|
3
3
|
INPUT = "input"
|
|
4
4
|
}
|
|
5
|
-
export declare const model: (modelValue: any) => import("../directive/index").DirectiveWrapper;
|
|
5
|
+
export declare const model: (modelValue: any, modelPath?: string | undefined) => import("../directive/index").DirectiveWrapper;
|