solely 0.2.9 → 0.3.1
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 +109 -40
- package/dist/runtime/component/base-element.cjs +1 -1
- package/dist/runtime/component/base-element.d.ts +1 -1
- package/dist/runtime/component/base-element.js +1 -1
- package/dist/runtime/component/decorators.cjs +1 -1
- package/dist/runtime/component/decorators.d.ts +9 -0
- package/dist/runtime/component/decorators.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -522,8 +522,96 @@ Solely 框架内置了多层安全防护机制,有效防止常见的 XSS(跨
|
|
|
522
522
|
styles: '...', // 可选:组件样式
|
|
523
523
|
shadowDOM: { use: true } // 可选:使用 Shadow DOM
|
|
524
524
|
})
|
|
525
|
+
```
|
|
526
|
+
|
|
527
|
+
#### Props 属性名转换规则
|
|
528
|
+
|
|
529
|
+
框架会自动处理属性名的转换,遵循 **HTML attribute 使用 kebab-case,组件内部使用 camelCase** 的约定:
|
|
530
|
+
|
|
531
|
+
| 声明方式 | HTML 使用 | 组件内部访问 | `$data` 键名 |
|
|
532
|
+
| ------------------------------- | ------------------------- | --------------- | ------------ |
|
|
533
|
+
| `props: ['myProp']` | `<my-comp my-prop="x">` | `this.myProp` | `myProp` |
|
|
534
|
+
| `props: [{ name: 'userName' }]` | `<my-comp user-name="x">` | `this.userName` | `userName` |
|
|
535
|
+
|
|
536
|
+
**转换规则**:
|
|
537
|
+
|
|
538
|
+
- HTML attribute 名:camelCase → kebab-case(如 `myProp` → `my-prop`)
|
|
539
|
+
- 组件代码中:保持原始 camelCase(如 `this.myProp`)
|
|
540
|
+
- `$data` 对象:使用 camelCase 作为键名(如 `$data.myProp`)
|
|
541
|
+
|
|
542
|
+
> **注意**:如果属性名已经是 kebab-case(如 `a-b`),框架**不会**进行额外转换。此时 HTML 和代码中都需使用原始名称:
|
|
543
|
+
>
|
|
544
|
+
> ```typescript
|
|
545
|
+
> props: ['a-b']; // HTML: <my-comp a-b="x">, 代码: this['a-b'], $data['a-b']
|
|
546
|
+
> ```
|
|
547
|
+
>
|
|
548
|
+
> 建议统一使用 camelCase 命名,让框架自动处理转换。
|
|
549
|
+
|
|
550
|
+
```typescript
|
|
551
|
+
@CustomElement({
|
|
552
|
+
tagName: 'user-card',
|
|
553
|
+
template: `<div>{{ $data.userName }} - {{ $data.age }}</div>`,
|
|
554
|
+
props: [
|
|
555
|
+
{ name: 'userName', type: 'string' }, // HTML: user-name, 内部: userName
|
|
556
|
+
{ name: 'age', type: 'number' }, // HTML: age, 内部: age
|
|
557
|
+
],
|
|
558
|
+
})
|
|
559
|
+
class UserCard extends BaseElement<{ userName: string; age: number }> {
|
|
560
|
+
// 使用方式:
|
|
561
|
+
// HTML: <user-card user-name="张三" age="25"></user-card>
|
|
562
|
+
// 代码: this.userName, this.$data.userName
|
|
563
|
+
}
|
|
564
|
+
```
|
|
565
|
+
|
|
566
|
+
```typescript
|
|
525
567
|
class MyComponent extends BaseElement<MyData> {
|
|
526
|
-
|
|
568
|
+
// 组件逻辑
|
|
569
|
+
}
|
|
570
|
+
```
|
|
571
|
+
|
|
572
|
+
#### Props 配置选项
|
|
573
|
+
|
|
574
|
+
完整的 `PropDescriptor` 配置:
|
|
575
|
+
|
|
576
|
+
```typescript
|
|
577
|
+
interface PropDescriptor {
|
|
578
|
+
name: string; // 属性名(camelCase)
|
|
579
|
+
type?: 'string' | 'number' | 'boolean' | 'object' | 'array'; // 类型转换
|
|
580
|
+
default?: unknown; // 默认值
|
|
581
|
+
reflect?: boolean; // 是否同步回 HTML attribute
|
|
582
|
+
reactive?: boolean; // 是否响应式同步到 $data(默认 true)
|
|
583
|
+
}
|
|
584
|
+
```
|
|
585
|
+
|
|
586
|
+
**`reactive` 参数说明**:
|
|
587
|
+
|
|
588
|
+
| 值 | 行为 | 适用场景 |
|
|
589
|
+
| -------------- | ----------------------------------------------- | ---------------------------- |
|
|
590
|
+
| `true`(默认) | 属性变化自动同步到 `$data`,触发模板更新 | 需要响应式绑定的数据 |
|
|
591
|
+
| `false` | 仅作为普通属性,不触发 `$data` 更新和模板重渲染 | 纯配置项,如 `theme`、`size` |
|
|
592
|
+
|
|
593
|
+
**示例**:
|
|
594
|
+
|
|
595
|
+
```typescript
|
|
596
|
+
@CustomElement({
|
|
597
|
+
tagName: 'my-button',
|
|
598
|
+
template: `
|
|
599
|
+
<button class="btn {{ $data.size }}">
|
|
600
|
+
{{ $data.label }}
|
|
601
|
+
</button>
|
|
602
|
+
`,
|
|
603
|
+
props: [
|
|
604
|
+
{ name: 'label', type: 'string', default: 'Button' }, // reactive: true(默认)
|
|
605
|
+
{ name: 'size', type: 'string', default: 'medium', reactive: false }, // 非响应式
|
|
606
|
+
{ name: 'disabled', type: 'boolean', reactive: true },
|
|
607
|
+
],
|
|
608
|
+
})
|
|
609
|
+
class MyButton extends BaseElement<{ label: string; size: string; disabled: boolean }> {
|
|
610
|
+
// 使用方式:
|
|
611
|
+
// <my-button label="提交" size="large" :disabled="$data.isLoading"></my-button>
|
|
612
|
+
// - label 变化 → 自动更新模板
|
|
613
|
+
// - size 变化 → 不触发模板更新(需在样式中处理)
|
|
614
|
+
// - disabled 变化 → 自动更新模板
|
|
527
615
|
}
|
|
528
616
|
```
|
|
529
617
|
|
|
@@ -544,64 +632,44 @@ Solely 支持两种属性设置方式:
|
|
|
544
632
|
2. **动态属性绑定** (`:attr="表达式"`):
|
|
545
633
|
- 语法:`:attr="表达式"`
|
|
546
634
|
- 作用:绑定动态表达式的值
|
|
547
|
-
-
|
|
548
|
-
- 注意:如果需要在元素定义前设置该属性,需要在 `upgradeProps` 中添加,以防止属性失效
|
|
635
|
+
- 行为:声明在 `props` 中的属性**会自动触发响应式更新**;对于自定义逻辑,可实现 `get`/`set` 方法来处理
|
|
549
636
|
- 示例:`<my-component :title="$data.pageTitle"></my-component>`
|
|
550
637
|
|
|
551
|
-
#### Upgrade Property
|
|
638
|
+
#### 属性自动升级(Upgrade Property)
|
|
552
639
|
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
**示例**:
|
|
640
|
+
框架会自动处理元素定义前设置的属性,无需手动配置:
|
|
556
641
|
|
|
557
642
|
```typescript
|
|
558
643
|
// 1. 创建元素实例(此时自定义元素尚未注册)
|
|
559
644
|
const element = document.createElement('my-component');
|
|
560
645
|
|
|
561
|
-
// 2.
|
|
646
|
+
// 2. 设置属性(此时属性会直接成为实例属性)
|
|
562
647
|
element.value = 'initial value';
|
|
563
|
-
element.
|
|
564
|
-
|
|
565
|
-
// 3. 定义自定义元素
|
|
566
|
-
class MyComponent extends BaseElement<{ value: string; disabled: boolean }> {
|
|
567
|
-
static upgradeProps = ['value', 'disabled']; // 指定需要升级的属性
|
|
648
|
+
element.count = 5;
|
|
568
649
|
|
|
650
|
+
// 3. 定义并注册自定义元素
|
|
651
|
+
class MyComponent extends BaseElement<{ value: string; count: number }> {
|
|
569
652
|
constructor() {
|
|
570
|
-
super({ value: '',
|
|
571
|
-
}
|
|
572
|
-
|
|
573
|
-
// 假设我们有自定义的 getter/setter
|
|
574
|
-
get value() {
|
|
575
|
-
return this.$data.value;
|
|
576
|
-
}
|
|
577
|
-
|
|
578
|
-
set value(newValue) {
|
|
579
|
-
this.$data.value = newValue;
|
|
580
|
-
console.log('Value changed:', newValue);
|
|
581
|
-
}
|
|
582
|
-
|
|
583
|
-
get disabled() {
|
|
584
|
-
return this.$data.disabled;
|
|
585
|
-
}
|
|
586
|
-
|
|
587
|
-
set disabled(newValue) {
|
|
588
|
-
this.$data.disabled = newValue;
|
|
589
|
-
console.log('Disabled changed:', newValue);
|
|
653
|
+
super({ value: '', count: 0 });
|
|
590
654
|
}
|
|
591
655
|
}
|
|
592
656
|
|
|
593
|
-
// 4. 注册自定义元素
|
|
594
657
|
customElements.define('my-component', MyComponent);
|
|
595
658
|
|
|
596
|
-
//
|
|
659
|
+
// 4. 添加到 DOM
|
|
597
660
|
document.body.appendChild(element);
|
|
598
|
-
//
|
|
599
|
-
//
|
|
600
|
-
//
|
|
601
|
-
// Disabled changed: true
|
|
661
|
+
// 框架自动升级属性:
|
|
662
|
+
// - 声明在 props 中的属性 → 同步到 $data 并触发响应式
|
|
663
|
+
// - 有自定义 getter/setter 的属性 → 触发 setter 让用户逻辑处理
|
|
602
664
|
```
|
|
603
665
|
|
|
604
|
-
|
|
666
|
+
**自动升级规则**:
|
|
667
|
+
|
|
668
|
+
| 属性类型 | 升级行为 | 说明 |
|
|
669
|
+
| ----------------------------- | -------------- | ------------------------------------ |
|
|
670
|
+
| 声明在 `props` 中的属性 | 同步到 `$data` | 通过框架生成的 setter 自动同步 |
|
|
671
|
+
| 有自定义 getter/setter 的属性 | 触发 setter | 用户自定义逻辑执行,同时同步 `$data` |
|
|
672
|
+
| 普通数据属性 | 保持原样 | 不干预,作为普通实例属性 |
|
|
605
673
|
|
|
606
674
|
#### 生命周期钩子
|
|
607
675
|
|
|
@@ -757,6 +825,7 @@ class MyComponent extends BaseElement<MyData> {
|
|
|
757
825
|
| `$data` | 获取/设置组件数据 | 新数据对象(设置时) | 当前数据对象 |
|
|
758
826
|
| `refresh()` | 手动触发组件刷新 | 无 | 无 |
|
|
759
827
|
| `emit(eventName, detail, options)` | 派发自定义事件 | eventName: 事件名<br>detail: 事件详情<br>options: 事件选项 | 无 |
|
|
828
|
+
| `emitNative(eventName, options)` | 派发原生事件 | eventName: 事件名<br>options: 事件选项 | 无 |
|
|
760
829
|
|
|
761
830
|
### observe
|
|
762
831
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var t,e,s,i,n,a,h,
|
|
1
|
+
"use strict";var t,e,s,i,n,o,a,h,r,c,u,l,d,f,b,p,v,m,y,w,k,M,g,S,j=Object.defineProperty,W=t=>{throw TypeError(t)},$=(t,e,s)=>e.has(t)||W("Cannot "+s),C=(t,e,s)=>($(t,e,"read from private field"),s?s.call(t):e.get(t)),O=(t,e,s)=>e.has(t)?W("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(t):e.set(t,s),A=(t,e,s,i)=>($(t,e,"write to private field"),i?i.call(t,s):e.set(t,s),s),N=(t,e,s)=>($(t,e,"access private method"),s);Object.defineProperties(exports,{t:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const E=require("../../shared/is-object.cjs"),q=require("../../shared/env.cjs"),U=require("../reactivity/observe.cjs"),x=require("../renderer/renderIR.cjs"),I=Symbol.for("solely.manifest"),J=class v extends HTMLElement{constructor(d={}){var b,v;super(),O(this,f),((t,e,s)=>{e in t?j(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s})(this,"symbol"!=typeof(v="$refs")?v+"":v,{}),O(this,t),O(this,e),O(this,s),O(this,i),O(this,n),O(this,o),O(this,a,!1),O(this,h,!1),O(this,r,!1),O(this,c,!1),O(this,u,!1),O(this,l,!1);const y=this.constructor;A(this,t,y[I]||{tagName:"base-element"});const w=C(this,t);A(this,n,(null==(b=w.shadowDOM)?void 0:b.use)?this.attachShadow({mode:w.shadowDOM.mode||"open"}):this),N(this,f,p).call(this,d),N(this,f,m).call(this,w),queueMicrotask(()=>N(this,f,S).call(this))}static get observedAttributes(){const t=this[I];return(null==t?void 0:t.attributeMap)?Array.from(t.attributeMap.keys()):[]}get $data(){return C(this,i)}set $data(t){var e;E.isObject(t)||(t={}),t!==C(this,i)&&(null==(e=C(this,o))||e.call(this),N(this,f,p).call(this,t),N(this,f,k).call(this))}refresh(){N(this,f,k).call(this)}connectedCallback(){var i;A(this,u,!0);const o=C(this,t);if(N(this,f,S).call(this),(o.className||o.tagName||"").split(" ").forEach(t=>t&&this.classList.add(t)),N(this,f,g).call(this),o.attributeMap&&E.isObject(this.$data)){for(const[t,e]of o.attributeMap){const s=e.name;if(this.hasAttribute(t)){const i=this.getAttribute(t),n=N(this,f,w).call(this,i,e.default,e.type);this.$data[s]=n}else"default"in e&&(this.$data[s]=e.default,"boolean"===e.type&&!0===e.default&&this.setAttribute(t,""))}N(this,f,y).call(this)}N(this,f,b).call(this),C(this,s)&&!C(this,e)&&A(this,e,x.createRender(C(this,s),C(this,n),this)),C(this,l)&&(A(this,l,!1),N(this,f,k).call(this)),this.mounted(),null==(i=this.onInit)||i.call(this)}disconnectedCallback(){var e,s;A(this,u,!1);const i=C(this,t);if(!(null==(e=i.shadowDOM)?void 0:e.use)&&i.styles){const t=`solely-style-${i.tagName}`,e=C(v,d).get(t)||0;e<=1?(null==(s=document.head.querySelector(`#${t}`))||s.remove(),C(v,d).delete(t)):C(v,d).set(t,e-1)}this.unmounted()}attributeChangedCallback(e,s,i){var n;if(s===i||!E.isObject(this.$data))return;const o=null==(n=C(this,t).attributeMap)?void 0:n.get(e);if(!o)return;const a=o.name,h=this.$data[a],c=N(this,f,w).call(this,i,o.default??h,o.type);this.$data[a]=c,A(this,r,!0),this.attributeChanged(e,h,c)}emit(t,e,s){this.dispatchEvent(new CustomEvent(t,{bubbles:!0,composed:!0,detail:e,...s}))}emitNative(t,e){this.dispatchEvent(new Event(t,{bubbles:!0,composed:!0,...e}))}onInit(){}created(){}mounted(){}beforeUpdate(){}updated(){}unmounted(){}attributeChanged(t,e,s){}beforeAttributesUpdate(){}afterAttributesUpdate(){}};t=new WeakMap,e=new WeakMap,s=new WeakMap,i=new WeakMap,n=new WeakMap,o=new WeakMap,a=new WeakMap,h=new WeakMap,r=new WeakMap,c=new WeakMap,u=new WeakMap,l=new WeakMap,d=new WeakMap,f=new WeakSet,b=function(){const t=Object.getOwnPropertyNames(this),e=["#","_"],s=new Set(["$data","$refs","constructor","onInit","created","mounted","beforeUpdate","updated","unmounted","attributeChanged","beforeAttributesUpdate","afterAttributesUpdate","emit","refresh"]);for(const i of t){if(s.has(i))continue;if(e.some(t=>i.startsWith(t)))continue;const t=Object.getOwnPropertyDescriptor(this,i);if(!t||!("value"in t))continue;const n=t.value;let o,a=this;for(;a&&(o=Object.getOwnPropertyDescriptor(a,i),!(null==o?void 0:o.set));)a=Object.getPrototypeOf(a);(null==o?void 0:o.set)&&(delete this[i],this[i]=n)}},p=function(e){const{proxy:s,dispose:n}=U.observe(e,e=>{var s;if("set"===e.type){const{key:i,newValue:n}=e,o=C(this,t),a=String(i),h=null==(s=o.propMap)?void 0:s.get(a);(null==h?void 0:h.reflect)&&N(this,f,v).call(this,h,n)}this.emit("dataChange",{...e}),N(this,f,k).call(this)});A(this,i,s),A(this,o,n)},v=function(t,e){const s=t.name.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase();if("boolean"===t.type)e?this.setAttribute(s,""):this.removeAttribute(s);else{const t="object"==typeof e?JSON.stringify(e):String(e);this.setAttribute(s,t)}},m=function(t){t.ir&&A(this,s,t.ir)},y=function(){const e=C(this,t);if(e.attributeMap)for(const[t,s]of e.attributeMap){if(!1===s.reactive)continue;const t=Object.getOwnPropertyDescriptor(this.constructor.prototype,s.name);(null==t?void 0:t.get)||(null==t?void 0:t.set)||Object.defineProperty(this,s.name,{get:()=>this.$data[s.name],set:t=>{this.$data[s.name]=t},configurable:!0,enumerable:!0})}},w=function(t,e,s){if(null===t)return"boolean"!==s&&e;switch(s){case"boolean":return!0;case"number":{const s=Number(t);return isNaN(s)?e:s}case"object":try{return JSON.parse(t)}catch(i){return console.error(`[Solely] Failed to parse object value: "${t}", using default value.`,i),e}case"array":try{const s=t.replace(/'/g,'"'),i=JSON.parse(s);return Array.isArray(i)?i:e}catch(i){return console.error(`[Solely] Failed to parse array value: "${t}", using default value.`,i),e}default:return t}},k=function(){C(this,u)?!C(this,h)&&C(this,e)&&(A(this,h,!0),queueMicrotask(()=>{A(this,h,!1),N(this,f,M).call(this)})):A(this,l,!0)},M=function(){if(!C(this,a)&&C(this,u)){A(this,a,!0);try{C(this,r)&&this.beforeAttributesUpdate(),this.beforeUpdate(),C(this,e).update(),this.updated(),C(this,r)&&(this.afterAttributesUpdate(),A(this,r,!1))}finally{A(this,a,!1)}}},g=function(){var e;const s=C(this,t);if(!s.styles)return;const i=C(this,n);if(s.sheet&&i instanceof ShadowRoot&&"adoptedStyleSheets"in i)return void(i.adoptedStyleSheets.includes(s.sheet)||(i.adoptedStyleSheets=[...i.adoptedStyleSheets,s.sheet]));const o=i instanceof ShadowRoot?i:document.head,a=`solely-style-${s.tagName}`,h=!(null==(e=s.shadowDOM)?void 0:e.use);if(!o.querySelector(`#${a}`)){const t=document.createElement("style");t.id=a,t.textContent=s.styles,o.appendChild(t)}if(h){const t=C(J,d).get(a)||0;C(J,d).set(a,t+1)}},S=function(){C(this,c)||(A(this,c,!0),this.created())},O(J,d,new Map),(()=>{if(q.IS_DEV&&"undefined"!=typeof customElements){const t=customElements.define,e=new WeakSet;customElements.define=function(s,i,n){if(J.isPrototypeOf(i)&&!e.has(i)){e.add(i);const t=i.prototype,n=["connectedCallback","disconnectedCallback","attributeChangedCallback"];for(const e of n)t[e]!==J.prototype[e]&&console.error(`[${s}] 禁止重写 ${e}(),请使用框架钩子`)}return t.call(this,s,i,n)}}})();let R=J;exports.default=R;
|
|
@@ -9,7 +9,6 @@ declare interface BaseElement<TData extends object = Record<string, unknown>, TR
|
|
|
9
9
|
declare class BaseElement<TData extends object = Record<string, unknown>, TRefs extends Record<string, Element> = Record<string, Element>> extends HTMLElement implements BaseElement<TData, TRefs> {
|
|
10
10
|
#private;
|
|
11
11
|
$refs: TRefs;
|
|
12
|
-
static upgradeProps?: readonly string[];
|
|
13
12
|
static get observedAttributes(): string[];
|
|
14
13
|
constructor(initialData?: TData);
|
|
15
14
|
get $data(): TData;
|
|
@@ -19,6 +18,7 @@ declare class BaseElement<TData extends object = Record<string, unknown>, TRefs
|
|
|
19
18
|
disconnectedCallback(): void;
|
|
20
19
|
attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null): void;
|
|
21
20
|
emit(eventName: string, detail?: unknown, options?: Partial<CustomEventInit>): void;
|
|
21
|
+
emitNative(eventName: string, options?: EventInit): void;
|
|
22
22
|
onInit(): void | Promise<void>;
|
|
23
23
|
created(): void;
|
|
24
24
|
mounted(): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var t,s,
|
|
1
|
+
var t,e,s,i,n,o,a,h,r,c,l,u,d,f,b,m,p,v,w,y,k,g,M,S,W=Object.defineProperty,j=t=>{throw TypeError(t)},$=(t,e,s)=>e.has(t)||j("Cannot "+s),C=(t,e,s)=>($(t,e,"read from private field"),s?s.call(t):e.get(t)),O=(t,e,s)=>e.has(t)?j("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(t):e.set(t,s),A=(t,e,s,i)=>($(t,e,"write to private field"),i?i.call(t,s):e.set(t,s),s),N=(t,e,s)=>($(t,e,"access private method"),s);import{isObject as E}from"../../shared/is-object.js";import{IS_DEV as U}from"../../shared/env.js";import{observe as x}from"../reactivity/observe.js";import{createRender as I}from"../renderer/renderIR.js";const J=Symbol.for("solely.manifest"),R=class p extends HTMLElement{constructor(d={}){var b,p;super(),O(this,f),((t,e,s)=>{e in t?W(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s})(this,"symbol"!=typeof(p="$refs")?p+"":p,{}),O(this,t),O(this,e),O(this,s),O(this,i),O(this,n),O(this,o),O(this,a,!1),O(this,h,!1),O(this,r,!1),O(this,c,!1),O(this,l,!1),O(this,u,!1);const w=this.constructor;A(this,t,w[J]||{tagName:"base-element"});const y=C(this,t);A(this,n,(null==(b=y.shadowDOM)?void 0:b.use)?this.attachShadow({mode:y.shadowDOM.mode||"open"}):this),N(this,f,m).call(this,d),N(this,f,v).call(this,y),queueMicrotask(()=>N(this,f,S).call(this))}static get observedAttributes(){const t=this[J];return(null==t?void 0:t.attributeMap)?Array.from(t.attributeMap.keys()):[]}get $data(){return C(this,i)}set $data(t){var e;E(t)||(t={}),t!==C(this,i)&&(null==(e=C(this,o))||e.call(this),N(this,f,m).call(this,t),N(this,f,k).call(this))}refresh(){N(this,f,k).call(this)}connectedCallback(){var i;A(this,l,!0);const o=C(this,t);if(N(this,f,S).call(this),(o.className||o.tagName||"").split(" ").forEach(t=>t&&this.classList.add(t)),N(this,f,M).call(this),o.attributeMap&&E(this.$data)){for(const[t,e]of o.attributeMap){const s=e.name;if(this.hasAttribute(t)){const i=this.getAttribute(t),n=N(this,f,y).call(this,i,e.default,e.type);this.$data[s]=n}else"default"in e&&(this.$data[s]=e.default,"boolean"===e.type&&!0===e.default&&this.setAttribute(t,""))}N(this,f,w).call(this)}N(this,f,b).call(this),C(this,s)&&!C(this,e)&&A(this,e,I(C(this,s),C(this,n),this)),C(this,u)&&(A(this,u,!1),N(this,f,k).call(this)),this.mounted(),null==(i=this.onInit)||i.call(this)}disconnectedCallback(){var e,s;A(this,l,!1);const i=C(this,t);if(!(null==(e=i.shadowDOM)?void 0:e.use)&&i.styles){const t=`solely-style-${i.tagName}`,e=C(p,d).get(t)||0;e<=1?(null==(s=document.head.querySelector(`#${t}`))||s.remove(),C(p,d).delete(t)):C(p,d).set(t,e-1)}this.unmounted()}attributeChangedCallback(e,s,i){var n;if(s===i||!E(this.$data))return;const o=null==(n=C(this,t).attributeMap)?void 0:n.get(e);if(!o)return;const a=o.name,h=this.$data[a],c=N(this,f,y).call(this,i,o.default??h,o.type);this.$data[a]=c,A(this,r,!0),this.attributeChanged(e,h,c)}emit(t,e,s){this.dispatchEvent(new CustomEvent(t,{bubbles:!0,composed:!0,detail:e,...s}))}emitNative(t,e){this.dispatchEvent(new Event(t,{bubbles:!0,composed:!0,...e}))}onInit(){}created(){}mounted(){}beforeUpdate(){}updated(){}unmounted(){}attributeChanged(t,e,s){}beforeAttributesUpdate(){}afterAttributesUpdate(){}};t=new WeakMap,e=new WeakMap,s=new WeakMap,i=new WeakMap,n=new WeakMap,o=new WeakMap,a=new WeakMap,h=new WeakMap,r=new WeakMap,c=new WeakMap,l=new WeakMap,u=new WeakMap,d=new WeakMap,f=new WeakSet,b=function(){const t=Object.getOwnPropertyNames(this),e=["#","_"],s=new Set(["$data","$refs","constructor","onInit","created","mounted","beforeUpdate","updated","unmounted","attributeChanged","beforeAttributesUpdate","afterAttributesUpdate","emit","refresh"]);for(const i of t){if(s.has(i))continue;if(e.some(t=>i.startsWith(t)))continue;const t=Object.getOwnPropertyDescriptor(this,i);if(!t||!("value"in t))continue;const n=t.value;let o,a=this;for(;a&&(o=Object.getOwnPropertyDescriptor(a,i),!(null==o?void 0:o.set));)a=Object.getPrototypeOf(a);(null==o?void 0:o.set)&&(delete this[i],this[i]=n)}},m=function(e){const{proxy:s,dispose:n}=x(e,e=>{var s;if("set"===e.type){const{key:i,newValue:n}=e,o=C(this,t),a=String(i),h=null==(s=o.propMap)?void 0:s.get(a);(null==h?void 0:h.reflect)&&N(this,f,p).call(this,h,n)}this.emit("dataChange",{...e}),N(this,f,k).call(this)});A(this,i,s),A(this,o,n)},p=function(t,e){const s=t.name.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase();if("boolean"===t.type)e?this.setAttribute(s,""):this.removeAttribute(s);else{const t="object"==typeof e?JSON.stringify(e):String(e);this.setAttribute(s,t)}},v=function(t){t.ir&&A(this,s,t.ir)},w=function(){const e=C(this,t);if(e.attributeMap)for(const[t,s]of e.attributeMap){if(!1===s.reactive)continue;const t=Object.getOwnPropertyDescriptor(this.constructor.prototype,s.name);(null==t?void 0:t.get)||(null==t?void 0:t.set)||Object.defineProperty(this,s.name,{get:()=>this.$data[s.name],set:t=>{this.$data[s.name]=t},configurable:!0,enumerable:!0})}},y=function(t,e,s){if(null===t)return"boolean"!==s&&e;switch(s){case"boolean":return!0;case"number":{const s=Number(t);return isNaN(s)?e:s}case"object":try{return JSON.parse(t)}catch(i){return console.error(`[Solely] Failed to parse object value: "${t}", using default value.`,i),e}case"array":try{const s=t.replace(/'/g,'"'),i=JSON.parse(s);return Array.isArray(i)?i:e}catch(i){return console.error(`[Solely] Failed to parse array value: "${t}", using default value.`,i),e}default:return t}},k=function(){C(this,l)?!C(this,h)&&C(this,e)&&(A(this,h,!0),queueMicrotask(()=>{A(this,h,!1),N(this,f,g).call(this)})):A(this,u,!0)},g=function(){if(!C(this,a)&&C(this,l)){A(this,a,!0);try{C(this,r)&&this.beforeAttributesUpdate(),this.beforeUpdate(),C(this,e).update(),this.updated(),C(this,r)&&(this.afterAttributesUpdate(),A(this,r,!1))}finally{A(this,a,!1)}}},M=function(){var e;const s=C(this,t);if(!s.styles)return;const i=C(this,n);if(s.sheet&&i instanceof ShadowRoot&&"adoptedStyleSheets"in i)return void(i.adoptedStyleSheets.includes(s.sheet)||(i.adoptedStyleSheets=[...i.adoptedStyleSheets,s.sheet]));const o=i instanceof ShadowRoot?i:document.head,a=`solely-style-${s.tagName}`,h=!(null==(e=s.shadowDOM)?void 0:e.use);if(!o.querySelector(`#${a}`)){const t=document.createElement("style");t.id=a,t.textContent=s.styles,o.appendChild(t)}if(h){const t=C(R,d).get(a)||0;C(R,d).set(a,t+1)}},S=function(){C(this,c)||(A(this,c,!0),this.created())},O(R,d,new Map),(()=>{if(U&&"undefined"!=typeof customElements){const t=customElements.define,e=new WeakSet;customElements.define=function(s,i,n){if(R.isPrototypeOf(i)&&!e.has(i)){e.add(i);const t=i.prototype,n=["connectedCallback","disconnectedCallback","attributeChangedCallback"];for(const e of n)t[e]!==R.prototype[e]&&console.error(`[${s}] 禁止重写 ${e}(),请使用框架钩子`)}return t.call(this,s,i,n)}}})();let q=R;export{q as default};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("../../shared/env.cjs"),o=require("../../compiler/parser/parser.cjs"),t=require("../../compiler/ir/buildIR.cjs"),n=Symbol.for("solely.manifest");exports.CustomElement=r=>{const s=r;return r=>{const{tagName:l}=s;if(customElements.get(l))return e.IS_DEV&&console.warn(`[Solely] 标签名称 "${l}" 已经被注册,跳过重复定义。`),r;if("string"!=typeof s.template||s.ir){if(s.template&&!s.ir)if("object"==typeof s.template&&null!==s.template){const o=s.template;"root"===o.t&&Array.isArray(o.fns)&&Array.isArray(o.n)?(s.ir=o,e.IS_DEV&&console.info(`[Solely] Using precompiled IR for <${l}> | Nodes: ${o.n.length} | Fns: ${o.fns.length}`)):console.error(`[Manifest Error] <${l}>: 提供的 template 不是有效的 IRRoot 对象。\n期望: { t: "root", fns: [], n: [] }\n实际: ${JSON.stringify(Object.keys(o))}`)}else console.error(`[Manifest Error] <${l}>: template 类型无效。\n期望: string 或 IRRoot 对象\n实际: ${typeof s.template}`)}else if(s.template.trim())try{const e=o.parseHtml(s.template);s.ir=t.buildIR(e,{source:s.template,filename:l})}catch(i){const o=i instanceof Error?i:new Error(String(i));console.error(`[Compiler Error] <${l}>: 模板编译失败\n错误: ${o.message}\n模板长度: ${s.template.length} 字符`,e.IS_DEV?o.stack:"")}else e.IS_DEV&&console.warn(`[Solely] <${l}>: 模板为空字符串`);if(s.styles&&!s.sheet&&"CSSStyleSheet"in window)try{const e=new CSSStyleSheet;"function"==typeof e.replaceSync&&(e.replaceSync(s.styles),s.sheet=e)}catch(i){e.IS_DEV&&console.warn(`[Style Warning] <${l}>: CSSStyleSheet 不可用,将使用 style 标签回退方案`)}if(s.props&&!s.
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("../../shared/env.cjs"),o=require("../../compiler/parser/parser.cjs"),t=require("../../compiler/ir/buildIR.cjs"),n=Symbol.for("solely.manifest");exports.CustomElement=r=>{const s=r;return r=>{const{tagName:l}=s;if(customElements.get(l))return e.IS_DEV&&console.warn(`[Solely] 标签名称 "${l}" 已经被注册,跳过重复定义。`),r;if("string"!=typeof s.template||s.ir){if(s.template&&!s.ir)if("object"==typeof s.template&&null!==s.template){const o=s.template;"root"===o.t&&Array.isArray(o.fns)&&Array.isArray(o.n)?(s.ir=o,e.IS_DEV&&console.info(`[Solely] Using precompiled IR for <${l}> | Nodes: ${o.n.length} | Fns: ${o.fns.length}`)):console.error(`[Manifest Error] <${l}>: 提供的 template 不是有效的 IRRoot 对象。\n期望: { t: "root", fns: [], n: [] }\n实际: ${JSON.stringify(Object.keys(o))}`)}else console.error(`[Manifest Error] <${l}>: template 类型无效。\n期望: string 或 IRRoot 对象\n实际: ${typeof s.template}`)}else if(s.template.trim())try{const e=o.parseHtml(s.template);s.ir=t.buildIR(e,{source:s.template,filename:l})}catch(i){const o=i instanceof Error?i:new Error(String(i));console.error(`[Compiler Error] <${l}>: 模板编译失败\n错误: ${o.message}\n模板长度: ${s.template.length} 字符`,e.IS_DEV?o.stack:"")}else e.IS_DEV&&console.warn(`[Solely] <${l}>: 模板为空字符串`);if(s.styles&&!s.sheet&&"CSSStyleSheet"in window)try{const e=new CSSStyleSheet;"function"==typeof e.replaceSync&&(e.replaceSync(s.styles),s.sheet=e)}catch(i){e.IS_DEV&&console.warn(`[Style Warning] <${l}>: CSSStyleSheet 不可用,将使用 style 标签回退方案`)}if(s.props&&!s.attributeMap){const e=new Map,o=new Map;s.props.forEach(t=>{const n="string"==typeof t?{name:t}:t,r=n.name.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),s=n.name,l={...n,name:s};e.set(r,l),o.set(s,l)}),s.attributeMap=e,s.propMap=o}class c extends r{}return c[n]=s,customElements.define(l,c),c}};
|
|
@@ -11,6 +11,13 @@ export interface PropDescriptor {
|
|
|
11
11
|
default?: unknown;
|
|
12
12
|
/** 是否同步回 HTML Attribute */
|
|
13
13
|
reflect?: boolean;
|
|
14
|
+
/**
|
|
15
|
+
* 是否响应式同步到 $data
|
|
16
|
+
* - true: 支持 :a="xx" 绑定,变化自动更新模板
|
|
17
|
+
* - false: 仅作为普通 prop,不触发模板更新
|
|
18
|
+
* @default true
|
|
19
|
+
*/
|
|
20
|
+
reactive?: boolean;
|
|
14
21
|
}
|
|
15
22
|
/**
|
|
16
23
|
* Manifest:组件声明配置
|
|
@@ -41,6 +48,8 @@ export interface InternalManifest extends Manifest {
|
|
|
41
48
|
/** 预编译的样式表 */
|
|
42
49
|
sheet?: CSSStyleSheet;
|
|
43
50
|
/** 预处理后的属性映射表:attr-name -> PropDescriptor */
|
|
51
|
+
attributeMap?: Map<string, PropDescriptor>;
|
|
52
|
+
/** prop 原始名索引:prop name -> PropDescriptor */
|
|
44
53
|
propMap?: Map<string, PropDescriptor>;
|
|
45
54
|
}
|
|
46
55
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{IS_DEV as e}from"../../shared/env.js";import{parseHtml as o}from"../../compiler/parser/parser.js";import{buildIR as t}from"../../compiler/ir/buildIR.js";const n=Symbol.for("solely.manifest"),r=r=>{const s=r;return r=>{const{tagName:l}=s;if(customElements.get(l))return e&&console.warn(`[Solely] 标签名称 "${l}" 已经被注册,跳过重复定义。`),r;if("string"!=typeof s.template||s.ir){if(s.template&&!s.ir)if("object"==typeof s.template&&null!==s.template){const o=s.template;"root"===o.t&&Array.isArray(o.fns)&&Array.isArray(o.n)?(s.ir=o,e&&console.info(`[Solely] Using precompiled IR for <${l}> | Nodes: ${o.n.length} | Fns: ${o.fns.length}`)):console.error(`[Manifest Error] <${l}>: 提供的 template 不是有效的 IRRoot 对象。\n期望: { t: "root", fns: [], n: [] }\n实际: ${JSON.stringify(Object.keys(o))}`)}else console.error(`[Manifest Error] <${l}>: template 类型无效。\n期望: string 或 IRRoot 对象\n实际: ${typeof s.template}`)}else if(s.template.trim())try{const e=o(s.template);s.ir=t(e,{source:s.template,filename:l})}catch(i){const o=i instanceof Error?i:new Error(String(i));console.error(`[Compiler Error] <${l}>: 模板编译失败\n错误: ${o.message}\n模板长度: ${s.template.length} 字符`,e?o.stack:"")}else e&&console.warn(`[Solely] <${l}>: 模板为空字符串`);if(s.styles&&!s.sheet&&"CSSStyleSheet"in window)try{const e=new CSSStyleSheet;"function"==typeof e.replaceSync&&(e.replaceSync(s.styles),s.sheet=e)}catch(i){e&&console.warn(`[Style Warning] <${l}>: CSSStyleSheet 不可用,将使用 style 标签回退方案`)}if(s.props&&!s.
|
|
1
|
+
import{IS_DEV as e}from"../../shared/env.js";import{parseHtml as o}from"../../compiler/parser/parser.js";import{buildIR as t}from"../../compiler/ir/buildIR.js";const n=Symbol.for("solely.manifest"),r=r=>{const s=r;return r=>{const{tagName:l}=s;if(customElements.get(l))return e&&console.warn(`[Solely] 标签名称 "${l}" 已经被注册,跳过重复定义。`),r;if("string"!=typeof s.template||s.ir){if(s.template&&!s.ir)if("object"==typeof s.template&&null!==s.template){const o=s.template;"root"===o.t&&Array.isArray(o.fns)&&Array.isArray(o.n)?(s.ir=o,e&&console.info(`[Solely] Using precompiled IR for <${l}> | Nodes: ${o.n.length} | Fns: ${o.fns.length}`)):console.error(`[Manifest Error] <${l}>: 提供的 template 不是有效的 IRRoot 对象。\n期望: { t: "root", fns: [], n: [] }\n实际: ${JSON.stringify(Object.keys(o))}`)}else console.error(`[Manifest Error] <${l}>: template 类型无效。\n期望: string 或 IRRoot 对象\n实际: ${typeof s.template}`)}else if(s.template.trim())try{const e=o(s.template);s.ir=t(e,{source:s.template,filename:l})}catch(i){const o=i instanceof Error?i:new Error(String(i));console.error(`[Compiler Error] <${l}>: 模板编译失败\n错误: ${o.message}\n模板长度: ${s.template.length} 字符`,e?o.stack:"")}else e&&console.warn(`[Solely] <${l}>: 模板为空字符串`);if(s.styles&&!s.sheet&&"CSSStyleSheet"in window)try{const e=new CSSStyleSheet;"function"==typeof e.replaceSync&&(e.replaceSync(s.styles),s.sheet=e)}catch(i){e&&console.warn(`[Style Warning] <${l}>: CSSStyleSheet 不可用,将使用 style 标签回退方案`)}if(s.props&&!s.attributeMap){const e=new Map,o=new Map;s.props.forEach(t=>{const n="string"==typeof t?{name:t}:t,r=n.name.replace(/([a-z])([A-Z])/g,"$1-$2").toLowerCase(),s=n.name,l={...n,name:s};e.set(r,l),o.set(s,l)}),s.attributeMap=e,s.propMap=o}class c extends r{}return c[n]=s,customElements.define(l,c),c}};export{r as CustomElement};
|
package/package.json
CHANGED