@djvlc/sandbox 1.0.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/dist/index.cjs +631 -0
- package/dist/index.d.cts +406 -0
- package/dist/index.d.ts +406 -0
- package/dist/index.js +598 -0
- package/package.json +50 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,406 @@
|
|
|
1
|
+
import { PageSchema, ComponentInstance } from '@djvlc/contracts-types';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 沙箱通信协议
|
|
5
|
+
* 定义 Editor 与 iframe 预览之间的消息格式
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 消息类型枚举
|
|
10
|
+
*/
|
|
11
|
+
declare enum MessageType {
|
|
12
|
+
READY = "ready",
|
|
13
|
+
INIT = "init",
|
|
14
|
+
DESTROY = "destroy",
|
|
15
|
+
LOAD_PAGE = "load_page",
|
|
16
|
+
UPDATE_SCHEMA = "update_schema",
|
|
17
|
+
REFRESH = "refresh",
|
|
18
|
+
SELECT_COMPONENT = "select_component",
|
|
19
|
+
HOVER_COMPONENT = "hover_component",
|
|
20
|
+
UPDATE_COMPONENT = "update_component",
|
|
21
|
+
DELETE_COMPONENT = "delete_component",
|
|
22
|
+
ADD_COMPONENT = "add_component",
|
|
23
|
+
MOVE_COMPONENT = "move_component",
|
|
24
|
+
SYNC_STATE = "sync_state",
|
|
25
|
+
SYNC_VARIABLES = "sync_variables",
|
|
26
|
+
COMPONENT_CLICK = "component_click",
|
|
27
|
+
COMPONENT_HOVER = "component_hover",
|
|
28
|
+
COMPONENT_CONTEXT_MENU = "component_context_menu",
|
|
29
|
+
COMPONENT_DRAG_START = "component_drag_start",
|
|
30
|
+
COMPONENT_DRAG_END = "component_drag_end",
|
|
31
|
+
ERROR = "error",
|
|
32
|
+
LOG = "log"
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* 基础消息结构
|
|
36
|
+
*/
|
|
37
|
+
interface BaseMessage<T = unknown> {
|
|
38
|
+
/** 消息类型 */
|
|
39
|
+
type: MessageType;
|
|
40
|
+
/** 消息 ID(用于请求/响应匹配) */
|
|
41
|
+
id: string;
|
|
42
|
+
/** 消息数据 */
|
|
43
|
+
data: T;
|
|
44
|
+
/** 时间戳 */
|
|
45
|
+
timestamp: number;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* 响应消息
|
|
49
|
+
*/
|
|
50
|
+
interface ResponseMessage<T = unknown> extends BaseMessage<T> {
|
|
51
|
+
/** 是否成功 */
|
|
52
|
+
success: boolean;
|
|
53
|
+
/** 错误信息 */
|
|
54
|
+
error?: string;
|
|
55
|
+
/** 请求 ID */
|
|
56
|
+
requestId: string;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Ready 消息数据
|
|
60
|
+
*/
|
|
61
|
+
interface ReadyMessageData {
|
|
62
|
+
/** 运行时版本 */
|
|
63
|
+
runtimeVersion: string;
|
|
64
|
+
/** 支持的功能 */
|
|
65
|
+
capabilities: string[];
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Init 消息数据
|
|
69
|
+
*/
|
|
70
|
+
interface InitMessageData {
|
|
71
|
+
/** 页面 UID */
|
|
72
|
+
pageUid?: string;
|
|
73
|
+
/** API 基础 URL */
|
|
74
|
+
apiBaseUrl: string;
|
|
75
|
+
/** CDN 基础 URL */
|
|
76
|
+
cdnBaseUrl: string;
|
|
77
|
+
/** 预览 Token */
|
|
78
|
+
previewToken?: string;
|
|
79
|
+
/** 调试模式 */
|
|
80
|
+
debug?: boolean;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Load Page 消息数据
|
|
84
|
+
*/
|
|
85
|
+
interface LoadPageMessageData {
|
|
86
|
+
/** 页面 UID */
|
|
87
|
+
pageUid: string;
|
|
88
|
+
/** 预览 Token */
|
|
89
|
+
previewToken?: string;
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Update Schema 消息数据
|
|
93
|
+
*/
|
|
94
|
+
interface UpdateSchemaMessageData {
|
|
95
|
+
/** 完整 Schema */
|
|
96
|
+
schema: PageSchema;
|
|
97
|
+
/** 是否全量更新 */
|
|
98
|
+
fullUpdate?: boolean;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Select Component 消息数据
|
|
102
|
+
*/
|
|
103
|
+
interface SelectComponentMessageData {
|
|
104
|
+
/** 组件 ID */
|
|
105
|
+
componentId: string | null;
|
|
106
|
+
/** 是否多选 */
|
|
107
|
+
multi?: boolean;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Hover Component 消息数据
|
|
111
|
+
*/
|
|
112
|
+
interface HoverComponentMessageData {
|
|
113
|
+
/** 组件 ID */
|
|
114
|
+
componentId: string | null;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Update Component 消息数据
|
|
118
|
+
*/
|
|
119
|
+
interface UpdateComponentMessageData {
|
|
120
|
+
/** 组件 ID */
|
|
121
|
+
componentId: string;
|
|
122
|
+
/** 更新的属性 */
|
|
123
|
+
props?: Record<string, unknown>;
|
|
124
|
+
/** 更新的样式 */
|
|
125
|
+
style?: Record<string, unknown>;
|
|
126
|
+
/** 完整组件实例 */
|
|
127
|
+
component?: ComponentInstance;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Add Component 消息数据
|
|
131
|
+
*/
|
|
132
|
+
interface AddComponentMessageData {
|
|
133
|
+
/** 组件实例 */
|
|
134
|
+
component: ComponentInstance;
|
|
135
|
+
/** 父组件 ID */
|
|
136
|
+
parentId?: string;
|
|
137
|
+
/** 插入位置索引 */
|
|
138
|
+
index?: number;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Move Component 消息数据
|
|
142
|
+
*/
|
|
143
|
+
interface MoveComponentMessageData {
|
|
144
|
+
/** 组件 ID */
|
|
145
|
+
componentId: string;
|
|
146
|
+
/** 目标父组件 ID */
|
|
147
|
+
targetParentId?: string;
|
|
148
|
+
/** 目标位置索引 */
|
|
149
|
+
targetIndex: number;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Delete Component 消息数据
|
|
153
|
+
*/
|
|
154
|
+
interface DeleteComponentMessageData {
|
|
155
|
+
/** 组件 ID */
|
|
156
|
+
componentId: string;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Component Event 消息数据
|
|
160
|
+
*/
|
|
161
|
+
interface ComponentEventMessageData {
|
|
162
|
+
/** 组件 ID */
|
|
163
|
+
componentId: string;
|
|
164
|
+
/** 组件类型 */
|
|
165
|
+
componentType: string;
|
|
166
|
+
/** 组件边界信息 */
|
|
167
|
+
bounds?: DOMRect;
|
|
168
|
+
/** 鼠标位置 */
|
|
169
|
+
position?: {
|
|
170
|
+
x: number;
|
|
171
|
+
y: number;
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Sync State 消息数据
|
|
176
|
+
*/
|
|
177
|
+
interface SyncStateMessageData {
|
|
178
|
+
/** 当前阶段 */
|
|
179
|
+
phase: string;
|
|
180
|
+
/** 页面信息 */
|
|
181
|
+
page?: {
|
|
182
|
+
pageUid: string;
|
|
183
|
+
pageVersionId: string;
|
|
184
|
+
};
|
|
185
|
+
/** 已选中组件 */
|
|
186
|
+
selectedComponents: string[];
|
|
187
|
+
/** 悬停组件 */
|
|
188
|
+
hoveredComponent: string | null;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
191
|
+
* Error 消息数据
|
|
192
|
+
*/
|
|
193
|
+
interface ErrorMessageData {
|
|
194
|
+
/** 错误代码 */
|
|
195
|
+
code: string;
|
|
196
|
+
/** 错误信息 */
|
|
197
|
+
message: string;
|
|
198
|
+
/** 错误详情 */
|
|
199
|
+
details?: Record<string, unknown>;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* 生成消息 ID
|
|
203
|
+
*/
|
|
204
|
+
declare function generateMessageId(): string;
|
|
205
|
+
/**
|
|
206
|
+
* 创建消息
|
|
207
|
+
*/
|
|
208
|
+
declare function createMessage<T>(type: MessageType, data: T): BaseMessage<T>;
|
|
209
|
+
/**
|
|
210
|
+
* 创建响应消息
|
|
211
|
+
*/
|
|
212
|
+
declare function createResponse<T>(requestId: string, type: MessageType, success: boolean, data?: T, error?: string): ResponseMessage<T>;
|
|
213
|
+
/**
|
|
214
|
+
* 检查是否为 DJVLC 消息
|
|
215
|
+
*/
|
|
216
|
+
declare function isDjvlcMessage(data: unknown): data is BaseMessage;
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* 沙箱宿主端
|
|
220
|
+
* 在 Editor 中使用,控制 iframe 预览
|
|
221
|
+
*/
|
|
222
|
+
|
|
223
|
+
/**
|
|
224
|
+
* 沙箱宿主配置
|
|
225
|
+
*/
|
|
226
|
+
interface SandboxHostOptions {
|
|
227
|
+
/** iframe 元素 */
|
|
228
|
+
iframe: HTMLIFrameElement;
|
|
229
|
+
/** 目标 origin */
|
|
230
|
+
targetOrigin: string;
|
|
231
|
+
/** 就绪回调 */
|
|
232
|
+
onReady?: () => void;
|
|
233
|
+
/** 组件点击回调 */
|
|
234
|
+
onComponentClick?: (componentId: string, data: ComponentEventMessageData) => void;
|
|
235
|
+
/** 组件悬停回调 */
|
|
236
|
+
onComponentHover?: (componentId: string | null, data: ComponentEventMessageData) => void;
|
|
237
|
+
/** 组件右键菜单回调 */
|
|
238
|
+
onComponentContextMenu?: (componentId: string, data: ComponentEventMessageData) => void;
|
|
239
|
+
/** 状态同步回调 */
|
|
240
|
+
onSyncState?: (state: SyncStateMessageData) => void;
|
|
241
|
+
/** 错误回调 */
|
|
242
|
+
onError?: (error: {
|
|
243
|
+
code: string;
|
|
244
|
+
message: string;
|
|
245
|
+
}) => void;
|
|
246
|
+
/** 日志回调 */
|
|
247
|
+
onLog?: (level: string, message: string, ...args: unknown[]) => void;
|
|
248
|
+
/** 超时时间 */
|
|
249
|
+
timeout?: number;
|
|
250
|
+
/** 调试模式 */
|
|
251
|
+
debug?: boolean;
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* 沙箱宿主
|
|
255
|
+
*/
|
|
256
|
+
declare class SandboxHost {
|
|
257
|
+
private options;
|
|
258
|
+
private pendingRequests;
|
|
259
|
+
private isReady;
|
|
260
|
+
private readyPromise;
|
|
261
|
+
private readyResolve;
|
|
262
|
+
constructor(options: SandboxHostOptions);
|
|
263
|
+
/**
|
|
264
|
+
* 连接到 iframe
|
|
265
|
+
*/
|
|
266
|
+
connect(): void;
|
|
267
|
+
/**
|
|
268
|
+
* 断开连接
|
|
269
|
+
*/
|
|
270
|
+
disconnect(): void;
|
|
271
|
+
/**
|
|
272
|
+
* 等待就绪
|
|
273
|
+
*/
|
|
274
|
+
waitReady(): Promise<void>;
|
|
275
|
+
/**
|
|
276
|
+
* 检查是否就绪
|
|
277
|
+
*/
|
|
278
|
+
get ready(): boolean;
|
|
279
|
+
/**
|
|
280
|
+
* 初始化
|
|
281
|
+
*/
|
|
282
|
+
init(data: InitMessageData): Promise<void>;
|
|
283
|
+
/**
|
|
284
|
+
* 加载页面
|
|
285
|
+
*/
|
|
286
|
+
loadPage(data: LoadPageMessageData): Promise<void>;
|
|
287
|
+
/**
|
|
288
|
+
* 更新 Schema
|
|
289
|
+
*/
|
|
290
|
+
updateSchema(data: UpdateSchemaMessageData): Promise<void>;
|
|
291
|
+
/**
|
|
292
|
+
* 刷新
|
|
293
|
+
*/
|
|
294
|
+
refresh(): Promise<void>;
|
|
295
|
+
/**
|
|
296
|
+
* 选择组件
|
|
297
|
+
*/
|
|
298
|
+
selectComponent(componentId: string | null, multi?: boolean): Promise<void>;
|
|
299
|
+
/**
|
|
300
|
+
* 悬停组件
|
|
301
|
+
*/
|
|
302
|
+
hoverComponent(componentId: string | null): Promise<void>;
|
|
303
|
+
/**
|
|
304
|
+
* 更新组件
|
|
305
|
+
*/
|
|
306
|
+
updateComponent(data: UpdateComponentMessageData): Promise<void>;
|
|
307
|
+
/**
|
|
308
|
+
* 添加组件
|
|
309
|
+
*/
|
|
310
|
+
addComponent(data: AddComponentMessageData): Promise<void>;
|
|
311
|
+
/**
|
|
312
|
+
* 移动组件
|
|
313
|
+
*/
|
|
314
|
+
moveComponent(data: MoveComponentMessageData): Promise<void>;
|
|
315
|
+
/**
|
|
316
|
+
* 删除组件
|
|
317
|
+
*/
|
|
318
|
+
deleteComponent(componentId: string): Promise<void>;
|
|
319
|
+
/**
|
|
320
|
+
* 同步变量
|
|
321
|
+
*/
|
|
322
|
+
syncVariables(variables: Record<string, unknown>): Promise<void>;
|
|
323
|
+
private handleMessage;
|
|
324
|
+
private send;
|
|
325
|
+
private log;
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* 沙箱客户端
|
|
330
|
+
* 在 iframe 预览中使用,响应 Editor 的控制
|
|
331
|
+
*/
|
|
332
|
+
/**
|
|
333
|
+
* 运行时接口(避免直接依赖 @djvlc/runtime-core)
|
|
334
|
+
*/
|
|
335
|
+
interface RuntimeLike {
|
|
336
|
+
getState(): {
|
|
337
|
+
phase: string;
|
|
338
|
+
page: {
|
|
339
|
+
pageUid: string;
|
|
340
|
+
pageVersionId: string;
|
|
341
|
+
} | null;
|
|
342
|
+
};
|
|
343
|
+
updateComponent(componentId: string, props: Record<string, unknown>): void;
|
|
344
|
+
setVariable(key: string, value: unknown): void;
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* 沙箱客户端配置
|
|
348
|
+
*/
|
|
349
|
+
interface SandboxClientOptions {
|
|
350
|
+
/** 目标 origin(Editor 的 origin) */
|
|
351
|
+
targetOrigin?: string;
|
|
352
|
+
/** 调试模式 */
|
|
353
|
+
debug?: boolean;
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* 沙箱客户端
|
|
357
|
+
*/
|
|
358
|
+
declare class SandboxClient {
|
|
359
|
+
private options;
|
|
360
|
+
private runtime;
|
|
361
|
+
private selectedComponents;
|
|
362
|
+
private hoveredComponent;
|
|
363
|
+
constructor(options?: SandboxClientOptions);
|
|
364
|
+
/**
|
|
365
|
+
* 设置运行时实例
|
|
366
|
+
*/
|
|
367
|
+
setRuntime(runtime: RuntimeLike): void;
|
|
368
|
+
/**
|
|
369
|
+
* 连接到父窗口
|
|
370
|
+
*/
|
|
371
|
+
connect(): void;
|
|
372
|
+
/**
|
|
373
|
+
* 断开连接
|
|
374
|
+
*/
|
|
375
|
+
disconnect(): void;
|
|
376
|
+
/**
|
|
377
|
+
* 初始化组件事件监听
|
|
378
|
+
*/
|
|
379
|
+
initComponentEventListeners(): void;
|
|
380
|
+
/**
|
|
381
|
+
* 注入编辑器样式
|
|
382
|
+
*/
|
|
383
|
+
injectEditorStyles(): void;
|
|
384
|
+
private handleMessage;
|
|
385
|
+
private handleInit;
|
|
386
|
+
private handleLoadPage;
|
|
387
|
+
private handleUpdateSchema;
|
|
388
|
+
private handleRefresh;
|
|
389
|
+
private handleSelectComponent;
|
|
390
|
+
private handleHoverComponent;
|
|
391
|
+
private handleUpdateComponent;
|
|
392
|
+
private handleAddComponent;
|
|
393
|
+
private handleMoveComponent;
|
|
394
|
+
private handleDeleteComponent;
|
|
395
|
+
private handleSyncVariables;
|
|
396
|
+
private updateSelectionStyles;
|
|
397
|
+
private syncState;
|
|
398
|
+
private sendReady;
|
|
399
|
+
private sendComponentEvent;
|
|
400
|
+
private send;
|
|
401
|
+
private sendResponse;
|
|
402
|
+
private findComponentElement;
|
|
403
|
+
private log;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
export { type AddComponentMessageData, type BaseMessage, type ComponentEventMessageData, type DeleteComponentMessageData, type ErrorMessageData, type HoverComponentMessageData, type InitMessageData, type LoadPageMessageData, MessageType, type MoveComponentMessageData, type ReadyMessageData, type ResponseMessage, SandboxClient, type SandboxClientOptions, SandboxHost, type SandboxHostOptions, type SelectComponentMessageData, type SyncStateMessageData, type UpdateComponentMessageData, type UpdateSchemaMessageData, createMessage, createResponse, generateMessageId, isDjvlcMessage };
|