@wsxjs/wsx-core 0.0.7 → 0.0.9
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/dist/chunk-CZII6RG2.mjs +229 -0
- package/dist/index.js +665 -487
- package/dist/index.mjs +657 -481
- package/dist/jsx-runtime.js +7 -0
- package/dist/jsx-runtime.mjs +1 -1
- package/dist/jsx.js +7 -0
- package/dist/jsx.mjs +1 -1
- package/package.json +1 -1
- package/src/base-component.ts +549 -0
- package/src/index.ts +2 -4
- package/src/jsx-factory.ts +15 -0
- package/src/light-component.ts +102 -186
- package/src/reactive-decorator.ts +132 -0
- package/src/utils/reactive.ts +209 -35
- package/src/web-component.ts +89 -129
- package/types/index.d.ts +2 -2
- package/src/reactive-component.ts +0 -306
|
@@ -1,306 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 响应式 WSX Web Component
|
|
3
|
-
*
|
|
4
|
-
* 扩展基础 WebComponent,提供响应式状态支持
|
|
5
|
-
* 遵循 WSX 理念:可选使用,不破坏现有组件
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
import { WebComponent, type WebComponentConfig } from "./web-component";
|
|
9
|
-
import { reactive, createState, reactiveWithDebug } from "./utils/reactive";
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* 响应式 WebComponent 配置
|
|
13
|
-
*/
|
|
14
|
-
export interface ReactiveWebComponentConfig extends WebComponentConfig {
|
|
15
|
-
/** 是否启用响应式调试模式 */
|
|
16
|
-
debug?: boolean;
|
|
17
|
-
/** 是否启用自动焦点保持 */
|
|
18
|
-
preserveFocus?: boolean;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* 响应式 WebComponent 基类
|
|
23
|
-
*
|
|
24
|
-
* 提供响应式状态管理能力,同时保持与标准 WebComponent 的完全兼容
|
|
25
|
-
*/
|
|
26
|
-
export abstract class ReactiveWebComponent extends WebComponent {
|
|
27
|
-
private _isDebugEnabled: boolean = false;
|
|
28
|
-
private _preserveFocus: boolean = true;
|
|
29
|
-
private _reactiveStates = new Map<string, any>();
|
|
30
|
-
|
|
31
|
-
constructor(config: ReactiveWebComponentConfig = {}) {
|
|
32
|
-
super(config);
|
|
33
|
-
|
|
34
|
-
this._isDebugEnabled = config.debug ?? false;
|
|
35
|
-
this._preserveFocus = config.preserveFocus ?? true;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* 创建响应式对象
|
|
40
|
-
*
|
|
41
|
-
* @param obj 要变为响应式的对象
|
|
42
|
-
* @param debugName 调试名称(可选)
|
|
43
|
-
* @returns 响应式代理对象
|
|
44
|
-
*/
|
|
45
|
-
protected reactive<T extends object>(obj: T, debugName?: string): T {
|
|
46
|
-
const reactiveFn = this._isDebugEnabled ? reactiveWithDebug : reactive;
|
|
47
|
-
const name = debugName || `${this.constructor.name}.reactive`;
|
|
48
|
-
|
|
49
|
-
return this._isDebugEnabled
|
|
50
|
-
? reactiveFn(obj, () => this.scheduleRerender(), name)
|
|
51
|
-
: reactiveFn(obj, () => this.scheduleRerender());
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* 创建响应式状态
|
|
56
|
-
*
|
|
57
|
-
* @param key 状态标识符
|
|
58
|
-
* @param initialValue 初始值
|
|
59
|
-
* @returns [getter, setter] 元组
|
|
60
|
-
*/
|
|
61
|
-
protected useState<T>(
|
|
62
|
-
key: string,
|
|
63
|
-
initialValue: T
|
|
64
|
-
): [() => T, (value: T | ((prev: T) => T)) => void] {
|
|
65
|
-
if (!this._reactiveStates.has(key)) {
|
|
66
|
-
const [getter, setter] = createState(initialValue, () => this.scheduleRerender());
|
|
67
|
-
this._reactiveStates.set(key, { getter, setter });
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
const state = this._reactiveStates.get(key);
|
|
71
|
-
return [state.getter, state.setter];
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
/**
|
|
75
|
-
* 调度重渲染
|
|
76
|
-
* 这个方法被响应式系统调用,开发者通常不需要直接调用
|
|
77
|
-
*/
|
|
78
|
-
protected scheduleRerender(): void {
|
|
79
|
-
// 确保组件已连接到 DOM
|
|
80
|
-
if (this.connected) {
|
|
81
|
-
this.rerender();
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
/**
|
|
86
|
-
* 重写 rerender 方法以支持焦点保持
|
|
87
|
-
*/
|
|
88
|
-
protected rerender(): void {
|
|
89
|
-
if (!this.connected) {
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
let focusData: any = null;
|
|
94
|
-
|
|
95
|
-
// 保存焦点状态(如果启用)
|
|
96
|
-
if (this._preserveFocus) {
|
|
97
|
-
const activeElement = this.shadowRoot.activeElement;
|
|
98
|
-
focusData = this.saveFocusState(activeElement);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
// 调用父类的重渲染逻辑
|
|
102
|
-
super.rerender();
|
|
103
|
-
|
|
104
|
-
// 恢复焦点状态(如果启用)- 立即同步执行避免闪烁
|
|
105
|
-
if (this._preserveFocus && focusData) {
|
|
106
|
-
this.restoreFocusState(focusData);
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* 保存焦点状态
|
|
112
|
-
*/
|
|
113
|
-
private saveFocusState(activeElement: Element | null): any {
|
|
114
|
-
if (!activeElement) {
|
|
115
|
-
return null;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
// 只保存可编辑元素的状态
|
|
119
|
-
const focusData: any = {
|
|
120
|
-
tagName: activeElement.tagName.toLowerCase(),
|
|
121
|
-
className: activeElement.className,
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
// 保存选择/光标位置
|
|
125
|
-
if (activeElement.hasAttribute("contenteditable")) {
|
|
126
|
-
const selection = window.getSelection();
|
|
127
|
-
if (selection && selection.rangeCount > 0) {
|
|
128
|
-
const range = selection.getRangeAt(0);
|
|
129
|
-
focusData.selectionStart = range.startOffset;
|
|
130
|
-
focusData.selectionEnd = range.endOffset;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
// 保存输入/选择元素的状态
|
|
135
|
-
if (
|
|
136
|
-
activeElement instanceof HTMLInputElement ||
|
|
137
|
-
activeElement instanceof HTMLSelectElement
|
|
138
|
-
) {
|
|
139
|
-
focusData.value = activeElement.value;
|
|
140
|
-
if ("selectionStart" in activeElement) {
|
|
141
|
-
focusData.selectionStart = activeElement.selectionStart;
|
|
142
|
-
focusData.selectionEnd = activeElement.selectionEnd;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return focusData;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/**
|
|
150
|
-
* 恢复焦点状态
|
|
151
|
-
*/
|
|
152
|
-
private restoreFocusState(focusData: any): void {
|
|
153
|
-
if (!focusData) return;
|
|
154
|
-
|
|
155
|
-
try {
|
|
156
|
-
// 查找要恢复焦点的元素
|
|
157
|
-
let targetElement: Element | null = null;
|
|
158
|
-
|
|
159
|
-
// 首先尝试通过类名查找(最具体)
|
|
160
|
-
if (focusData.className) {
|
|
161
|
-
targetElement = this.shadowRoot.querySelector(
|
|
162
|
-
`.${focusData.className.split(" ")[0]}`
|
|
163
|
-
);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
// 备用方案:通过标签名查找
|
|
167
|
-
if (!targetElement) {
|
|
168
|
-
targetElement = this.shadowRoot.querySelector(focusData.tagName);
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
if (targetElement) {
|
|
172
|
-
// 恢复焦点 - 防止页面滚动
|
|
173
|
-
(targetElement as HTMLElement).focus({ preventScroll: true });
|
|
174
|
-
|
|
175
|
-
// 恢复选择/光标位置
|
|
176
|
-
if (focusData.selectionStart !== undefined) {
|
|
177
|
-
if (targetElement instanceof HTMLInputElement) {
|
|
178
|
-
targetElement.setSelectionRange(
|
|
179
|
-
focusData.selectionStart,
|
|
180
|
-
focusData.selectionEnd
|
|
181
|
-
);
|
|
182
|
-
} else if (targetElement instanceof HTMLSelectElement) {
|
|
183
|
-
targetElement.value = focusData.value;
|
|
184
|
-
} else if (targetElement.hasAttribute("contenteditable")) {
|
|
185
|
-
this.setCursorPosition(targetElement, focusData.selectionStart);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
} catch {
|
|
190
|
-
// 静默处理焦点恢复失败,不应该影响正常渲染
|
|
191
|
-
}
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* 设置光标位置
|
|
196
|
-
*/
|
|
197
|
-
private setCursorPosition(element: Element, position: number): void {
|
|
198
|
-
try {
|
|
199
|
-
const selection = window.getSelection();
|
|
200
|
-
if (selection) {
|
|
201
|
-
const range = document.createRange();
|
|
202
|
-
const textNode = element.childNodes[0];
|
|
203
|
-
if (textNode) {
|
|
204
|
-
const maxPos = Math.min(position, textNode.textContent?.length || 0);
|
|
205
|
-
range.setStart(textNode, maxPos);
|
|
206
|
-
range.setEnd(textNode, maxPos);
|
|
207
|
-
selection.removeAllRanges();
|
|
208
|
-
selection.addRange(range);
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
} catch {
|
|
212
|
-
// 静默处理,焦点恢复失败不应该阻止渲染
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
/**
|
|
217
|
-
* 获取所有响应式状态的快照(用于调试)
|
|
218
|
-
*/
|
|
219
|
-
protected getStateSnapshot(): Record<string, any> {
|
|
220
|
-
const snapshot: Record<string, any> = {};
|
|
221
|
-
|
|
222
|
-
this._reactiveStates.forEach((state, key) => {
|
|
223
|
-
snapshot[key] = state.getter();
|
|
224
|
-
});
|
|
225
|
-
|
|
226
|
-
return snapshot;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
/**
|
|
230
|
-
* 清理响应式状态(组件销毁时)
|
|
231
|
-
*/
|
|
232
|
-
protected cleanupReactiveStates(): void {
|
|
233
|
-
this._reactiveStates.clear();
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/**
|
|
237
|
-
* 重写 disconnectedCallback 以清理状态
|
|
238
|
-
*/
|
|
239
|
-
disconnectedCallback(): void {
|
|
240
|
-
super.disconnectedCallback();
|
|
241
|
-
this.cleanupReactiveStates();
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* 启用调试模式
|
|
246
|
-
*/
|
|
247
|
-
protected enableDebug(): void {
|
|
248
|
-
this._isDebugEnabled = true;
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* 禁用调试模式
|
|
253
|
-
*/
|
|
254
|
-
protected disableDebug(): void {
|
|
255
|
-
this._isDebugEnabled = false;
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
/**
|
|
260
|
-
* 装饰器:自动使组件变为响应式
|
|
261
|
-
*
|
|
262
|
-
* @param debugMode 是否启用调试模式
|
|
263
|
-
*/
|
|
264
|
-
export function makeReactive(_debugMode: boolean = false) {
|
|
265
|
-
return function <T extends new (...args: any[]) => WebComponent>(constructor: T) {
|
|
266
|
-
return class ReactiveComponent extends constructor {
|
|
267
|
-
constructor(...args: any[]) {
|
|
268
|
-
super(...args);
|
|
269
|
-
|
|
270
|
-
// 如果不是 ReactiveWebComponent 的实例,则混入响应式能力
|
|
271
|
-
if (!(this instanceof ReactiveWebComponent)) {
|
|
272
|
-
// 添加响应式方法
|
|
273
|
-
(this as any).reactive = function <U extends object>(obj: U): U {
|
|
274
|
-
return reactive(obj, () => (this as any).rerender());
|
|
275
|
-
};
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
render(): HTMLElement {
|
|
280
|
-
// 抽象方法必须由子类实现
|
|
281
|
-
throw new Error("render() method must be implemented by subclass");
|
|
282
|
-
}
|
|
283
|
-
} as T;
|
|
284
|
-
};
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
/**
|
|
288
|
-
* 工具函数:创建响应式组件实例
|
|
289
|
-
*/
|
|
290
|
-
export function createReactiveComponent<T extends WebComponent>(
|
|
291
|
-
ComponentClass: new (...args: any[]) => T,
|
|
292
|
-
config?: ReactiveWebComponentConfig
|
|
293
|
-
): T {
|
|
294
|
-
// 如果已经是响应式组件,直接创建实例
|
|
295
|
-
if (ComponentClass.prototype instanceof ReactiveWebComponent) {
|
|
296
|
-
return new ComponentClass(config);
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
// 否则使用装饰器包装
|
|
300
|
-
const ReactiveComponent = makeReactive(config?.debug)(ComponentClass);
|
|
301
|
-
return new ReactiveComponent(config);
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
// 导出响应式相关的所有功能
|
|
305
|
-
export { reactive, createState, ReactiveDebug } from "./utils/reactive";
|
|
306
|
-
export type { ReactiveCallback } from "./utils/reactive";
|