compelem 0.1.1-beta → 0.2.0-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/decorators/prop.d.ts +8 -0
- package/decorators/state.d.ts +7 -0
- package/index.js +70 -41
- package/package.json +1 -1
package/decorators/prop.d.ts
CHANGED
|
@@ -55,3 +55,11 @@ export type PropOption = {
|
|
|
55
55
|
*/
|
|
56
56
|
export declare function prop(options: PropOption): (target: any, propertyKey: any) => void;
|
|
57
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;
|
|
65
|
+
export declare function _getObservedAttrs(ctor: Function): Set<string>;
|
package/decorators/state.d.ts
CHANGED
|
@@ -17,3 +17,10 @@ export type StateOption = {
|
|
|
17
17
|
*/
|
|
18
18
|
export declare function state(target: any, stateKey: any): void;
|
|
19
19
|
export declare function state(options: StateOption): (target: any, stateKey: any) => void;
|
|
20
|
+
/**
|
|
21
|
+
* 同@state装饰器,但可用于构造器中调用
|
|
22
|
+
* @param ctor 类构造函数
|
|
23
|
+
* @param stateKey state 名称
|
|
24
|
+
* @param options
|
|
25
|
+
*/
|
|
26
|
+
export declare function makeState(ctor: Function, stateKey: string, options?: StateOption): void;
|
package/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* compelem v0.
|
|
2
|
+
* compelem v0.2.0-beta.1731509434
|
|
3
3
|
* A modern, reactive, fast, lightweight and flexible lib for building web components
|
|
4
4
|
* @holyhigh2
|
|
5
5
|
* https://github.com/holyhigh2/compelem
|
|
6
6
|
*/
|
|
7
|
-
import { toPath, isObject, isDefined, get, isUndefined, isEmpty, has, concat, set, last, isEqual, startsWith, includes, clone, isFunction, isElement, isArray, initial, find, debounce, throttle, once, remove, map, size, isBlank, join, split, replace, compact, toArray, assign, first, each, isString,
|
|
7
|
+
import { toPath, isObject, isDefined, get, isUndefined, isEmpty, has, concat, set, cloneDeep, kebabCase, last, isEqual, startsWith, includes, clone, isFunction, isElement, isArray, initial, find, debounce, throttle, once, remove, map, size, isBlank, join, split, replace, compact, toArray, assign, first, each, isString, closest, bind as bind$1, trim, omitBy, camelCase, isNil, some, isNull, fval, test, merge, omit, flatMap, groupBy, reject, filter, slice, head, trimStart, trimEnd, replaceAll, snakeCase, toString, findIndex, call } from 'myfx';
|
|
8
8
|
|
|
9
9
|
function showError(msg) {
|
|
10
10
|
console.error(`[CompElem]`, msg);
|
|
@@ -127,6 +127,62 @@ function decorator(decoClass) {
|
|
|
127
127
|
};
|
|
128
128
|
}
|
|
129
129
|
|
|
130
|
+
//每个类需要监控的属性
|
|
131
|
+
const ObservedAttrsMap = new WeakMap;
|
|
132
|
+
function prop(options) {
|
|
133
|
+
if (arguments.length === 1) {
|
|
134
|
+
return (target, propertyKey, descriptor) => {
|
|
135
|
+
options.required = options.required || false;
|
|
136
|
+
options.attribute = options.attribute === false ? false : true;
|
|
137
|
+
defineProp(target, propertyKey, options, descriptor);
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
let target = arguments[0], propertyKey = arguments[1], descriptor = arguments[2];
|
|
141
|
+
defineProp(target, propertyKey, { type: undefined, required: false, attribute: true }, descriptor);
|
|
142
|
+
}
|
|
143
|
+
function defineProp(target, propertyKey, options, descriptor) {
|
|
144
|
+
if (!/[a-z]/.test(propertyKey[0])) {
|
|
145
|
+
showError(`Prop '${propertyKey}' must be in CamelCase`);
|
|
146
|
+
}
|
|
147
|
+
if (!has(target.constructor, '__deco_props')) {
|
|
148
|
+
target.constructor.__deco_props = isEmpty(target.constructor.__deco_props) ? {} : cloneDeep(target.constructor.__deco_props);
|
|
149
|
+
}
|
|
150
|
+
if (descriptor) {
|
|
151
|
+
if (descriptor.get)
|
|
152
|
+
options.getter = descriptor.get;
|
|
153
|
+
if (descriptor.set)
|
|
154
|
+
options.setter = descriptor.set;
|
|
155
|
+
}
|
|
156
|
+
if (options.attribute) {
|
|
157
|
+
let attrSet = ObservedAttrsMap.get(target.constructor);
|
|
158
|
+
if (!attrSet) {
|
|
159
|
+
attrSet = new Set();
|
|
160
|
+
ObservedAttrsMap.set(target.constructor, attrSet);
|
|
161
|
+
}
|
|
162
|
+
let kbb = kebabCase(propertyKey);
|
|
163
|
+
attrSet?.add(kbb);
|
|
164
|
+
}
|
|
165
|
+
target.constructor.__deco_props[propertyKey] = options;
|
|
166
|
+
}
|
|
167
|
+
/**
|
|
168
|
+
* 同@prop装饰器,但可用于构造器中调用
|
|
169
|
+
* @param ctor 类构造函数
|
|
170
|
+
* @param propertyKey prop名称
|
|
171
|
+
* @param options
|
|
172
|
+
*/
|
|
173
|
+
function makeProp(ctor, propertyKey, options) {
|
|
174
|
+
if (options) {
|
|
175
|
+
options.required = options.required || false;
|
|
176
|
+
options.attribute = options.attribute === false ? false : true;
|
|
177
|
+
}
|
|
178
|
+
defineProp(ctor.prototype, propertyKey, options || { type: undefined, required: false, attribute: true });
|
|
179
|
+
}
|
|
180
|
+
//内部接口
|
|
181
|
+
const emptySet = new Set;
|
|
182
|
+
function _getObservedAttrs(ctor) {
|
|
183
|
+
return ObservedAttrsMap.get(ctor) ?? emptySet;
|
|
184
|
+
}
|
|
185
|
+
|
|
130
186
|
/**
|
|
131
187
|
* 用于提供全局state状态管理
|
|
132
188
|
* @author holyhigh2
|
|
@@ -820,7 +876,6 @@ class CompElem extends RenderContext(HTMLElement) {
|
|
|
820
876
|
#slotHooks = {};
|
|
821
877
|
#slotNodes = {};
|
|
822
878
|
#mounted = false;
|
|
823
|
-
#observedAttrs = new Set();
|
|
824
879
|
#instanceCss = new CSSStyleSheet();
|
|
825
880
|
/**
|
|
826
881
|
* 是否自动插入插槽,如果需要控制插槽类型时,可以设置为false
|
|
@@ -888,15 +943,6 @@ class CompElem extends RenderContext(HTMLElement) {
|
|
|
888
943
|
});
|
|
889
944
|
set(this.constructor, '_component_style_attached', styleSheets);
|
|
890
945
|
}
|
|
891
|
-
//observAttrs
|
|
892
|
-
let propDefs = get(this.constructor, "__deco_props");
|
|
893
|
-
each(propDefs, (def, key) => {
|
|
894
|
-
let propDef = propDefs[key];
|
|
895
|
-
if (propDef.attribute) {
|
|
896
|
-
let kbb = kebabCase(key);
|
|
897
|
-
this.#observedAttrs.add(kbb);
|
|
898
|
-
}
|
|
899
|
-
});
|
|
900
946
|
/////////////////////////////////////////////////// shadow
|
|
901
947
|
this.#shadow = this.attachShadow({
|
|
902
948
|
mode: "open",
|
|
@@ -1618,7 +1664,8 @@ class CompElem extends RenderContext(HTMLElement) {
|
|
|
1618
1664
|
#attrChanged(name, oldValue, newValue) {
|
|
1619
1665
|
if (!this.isMounted)
|
|
1620
1666
|
return;
|
|
1621
|
-
|
|
1667
|
+
let observedAttrs = _getObservedAttrs(this.constructor);
|
|
1668
|
+
if (observedAttrs.has(name)) {
|
|
1622
1669
|
if (isNull(newValue)) {
|
|
1623
1670
|
let propDefs = get(this.constructor, "__deco_props");
|
|
1624
1671
|
//使用默认值
|
|
@@ -2389,33 +2436,6 @@ function event(eventName, options) {
|
|
|
2389
2436
|
};
|
|
2390
2437
|
}
|
|
2391
2438
|
|
|
2392
|
-
function prop(options) {
|
|
2393
|
-
if (arguments.length === 1) {
|
|
2394
|
-
return (target, propertyKey, descriptor) => {
|
|
2395
|
-
options.required = options.required || false;
|
|
2396
|
-
options.attribute = options.attribute === false ? false : true;
|
|
2397
|
-
defineProp(target, propertyKey, options, descriptor);
|
|
2398
|
-
};
|
|
2399
|
-
}
|
|
2400
|
-
let target = arguments[0], propertyKey = arguments[1], descriptor = arguments[2];
|
|
2401
|
-
defineProp(target, propertyKey, { type: undefined, required: false, attribute: true }, descriptor);
|
|
2402
|
-
}
|
|
2403
|
-
function defineProp(target, propertyKey, options, descriptor) {
|
|
2404
|
-
if (!/[a-z]/.test(propertyKey[0])) {
|
|
2405
|
-
showError(`Prop '${propertyKey}' must be in CamelCase`);
|
|
2406
|
-
}
|
|
2407
|
-
if (!has(target.constructor, '__deco_props')) {
|
|
2408
|
-
target.constructor.__deco_props = isEmpty(target.constructor.__deco_props) ? {} : cloneDeep(target.constructor.__deco_props);
|
|
2409
|
-
}
|
|
2410
|
-
if (descriptor) {
|
|
2411
|
-
if (descriptor.get)
|
|
2412
|
-
options.getter = descriptor.get;
|
|
2413
|
-
if (descriptor.set)
|
|
2414
|
-
options.setter = descriptor.set;
|
|
2415
|
-
}
|
|
2416
|
-
target.constructor.__deco_props[propertyKey] = options;
|
|
2417
|
-
}
|
|
2418
|
-
|
|
2419
2439
|
/**
|
|
2420
2440
|
* 缓存策略
|
|
2421
2441
|
*/
|
|
@@ -2492,6 +2512,15 @@ function defineState(target, stateKey, options) {
|
|
|
2492
2512
|
}
|
|
2493
2513
|
target.constructor.__deco_states[stateKey] = options;
|
|
2494
2514
|
}
|
|
2515
|
+
/**
|
|
2516
|
+
* 同@state装饰器,但可用于构造器中调用
|
|
2517
|
+
* @param ctor 类构造函数
|
|
2518
|
+
* @param stateKey state 名称
|
|
2519
|
+
* @param options
|
|
2520
|
+
*/
|
|
2521
|
+
function makeState(ctor, stateKey, options) {
|
|
2522
|
+
defineState(ctor.prototype, stateKey, options || { prop: "" });
|
|
2523
|
+
}
|
|
2495
2524
|
|
|
2496
2525
|
/**
|
|
2497
2526
|
* class用注解,用于自动注册自定义组件
|
|
@@ -3116,4 +3145,4 @@ class When extends Directive {
|
|
|
3116
3145
|
}
|
|
3117
3146
|
const when = directive(When);
|
|
3118
3147
|
|
|
3119
|
-
export { CompElem, Decorator, DecoratorType, DecoratorWrapper, DecoratorsKey, Directive, DirectiveUpdateTag, DirectiveWrapper, EnterPoint, EnterPointType, ExpPos, GetKeyFnName, ModelTriggerType, QueryCache, Template, bind, buildHTML, classes, computed, createRef, decorator, directive, event, forEach, html, ifElse, ifTrue, model, prop, query, queryAll, show, slot, state, styles, sync, tag, watch, when };
|
|
3148
|
+
export { CompElem, Decorator, DecoratorType, DecoratorWrapper, DecoratorsKey, Directive, DirectiveUpdateTag, DirectiveWrapper, EnterPoint, EnterPointType, ExpPos, GetKeyFnName, ModelTriggerType, QueryCache, Template, _getObservedAttrs, bind, buildHTML, classes, computed, createRef, decorator, directive, event, forEach, html, ifElse, ifTrue, makeProp, makeState, model, prop, query, queryAll, show, slot, state, styles, sync, tag, watch, when };
|