@schema-element-editor/host-sdk 2.0.2 → 2.1.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/LICENSE +21 -0
- package/dist/chunk-3OZ7E3LK.js +391 -0
- package/dist/chunk-432BCQVI.js +0 -0
- package/dist/chunk-5URPGJPF.js +391 -0
- package/dist/chunk-APBK7FQZ.js +437 -0
- package/dist/chunk-BVGJ5UDR.js +390 -0
- package/dist/chunk-D67MT673.js +44 -0
- package/dist/chunk-DGO4Z6NW.js +399 -0
- package/dist/chunk-EIQP2PZ6.js +56 -0
- package/dist/chunk-IYQL6QJY.js +60 -0
- package/dist/chunk-LD4OEJ4X.js +56 -0
- package/dist/chunk-LJJYJHOZ.js +450 -0
- package/dist/chunk-MQTHVJYR.js +54 -0
- package/dist/chunk-NF4H2OT3.js +355 -0
- package/dist/chunk-NWZVU75I.js +54 -0
- package/dist/chunk-Q6EJUFM6.js +56 -0
- package/dist/chunk-R4LAS3QA.js +54 -0
- package/dist/chunk-XIS7CAP4.js +54 -0
- package/dist/chunk-Y2OF4YA4.js +54 -0
- package/dist/core.cjs +282 -1
- package/dist/core.d.cts +16 -81
- package/dist/core.d.ts +16 -81
- package/dist/core.js +2 -1
- package/dist/index.cjs +301 -11
- package/dist/index.d.cts +2 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +3 -2
- package/dist/react.cjs +301 -11
- package/dist/react.d.cts +2 -3
- package/dist/react.d.ts +2 -3
- package/dist/react.js +2 -2
- package/dist/types-D2ZJx8T_.d.cts +103 -0
- package/dist/types-D2ZJx8T_.d.ts +103 -0
- package/dist/vue.cjs +300 -5
- package/dist/vue.d.cts +17 -3
- package/dist/vue.d.ts +17 -3
- package/dist/vue.js +20 -4
- package/package.json +5 -6
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 hei-f
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,391 @@
|
|
|
1
|
+
// src/constants.ts
|
|
2
|
+
var DEFAULT_SOURCE_CONFIG = {
|
|
3
|
+
contentSource: "schema-element-editor-content",
|
|
4
|
+
hostSource: "schema-element-editor-host"
|
|
5
|
+
};
|
|
6
|
+
var SDK_COORDINATOR_SOURCE = "schema-element-editor-sdk-coordinator";
|
|
7
|
+
var DEFAULT_MESSAGE_TYPES = {
|
|
8
|
+
getSchema: "GET_SCHEMA",
|
|
9
|
+
updateSchema: "UPDATE_SCHEMA",
|
|
10
|
+
checkPreview: "CHECK_PREVIEW",
|
|
11
|
+
renderPreview: "RENDER_PREVIEW",
|
|
12
|
+
cleanupPreview: "CLEANUP_PREVIEW",
|
|
13
|
+
// 录制模式相关
|
|
14
|
+
startRecording: "START_RECORDING",
|
|
15
|
+
stopRecording: "STOP_RECORDING",
|
|
16
|
+
schemaPush: "SCHEMA_PUSH"
|
|
17
|
+
};
|
|
18
|
+
var SDK_COORDINATION_MESSAGE_TYPES = {
|
|
19
|
+
register: "SDK_REGISTER",
|
|
20
|
+
unregister: "SDK_UNREGISTER",
|
|
21
|
+
query: "SDK_QUERY"
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// src/coordinator.ts
|
|
25
|
+
var SdkCoordinator = class {
|
|
26
|
+
constructor(config) {
|
|
27
|
+
/**
|
|
28
|
+
* SDK 销毁标志
|
|
29
|
+
*
|
|
30
|
+
* 作用:防止在 cleanup() 执行期间,监听器移除前仍然响应请求
|
|
31
|
+
*
|
|
32
|
+
* 场景:cleanup() 中先调用 destroy()(设置 isDestroyed = true),
|
|
33
|
+
* 然后才移除 handleMessage 监听器。在这个微小的时间窗口内,
|
|
34
|
+
* 如果插件请求到达,没有 isDestroyed 检查会导致已销毁的 SDK 仍然响应。
|
|
35
|
+
*/
|
|
36
|
+
this.isDestroyed = false;
|
|
37
|
+
/**
|
|
38
|
+
* 存储比当前 SDK 优先级更高的其他 SDK ID
|
|
39
|
+
* 按方法分类:如果某个方法的集合不为空,说明有更高优先级的 SDK 应该处理该方法
|
|
40
|
+
*
|
|
41
|
+
* 优化:只为当前 SDK 实现了的方法维护优先级集合
|
|
42
|
+
*/
|
|
43
|
+
this.higherLevelSdks = {};
|
|
44
|
+
/**
|
|
45
|
+
* 处理 SDK 协调消息
|
|
46
|
+
*/
|
|
47
|
+
this.handleCoordinationMessage = (event) => {
|
|
48
|
+
const isFromSelf = event.source === window;
|
|
49
|
+
const isFromParent = window !== window.top && event.source === window.parent;
|
|
50
|
+
if (!isFromSelf && !isFromParent) return;
|
|
51
|
+
const data = event.data;
|
|
52
|
+
if (!data || data.source !== SDK_COORDINATOR_SOURCE) return;
|
|
53
|
+
switch (data.type) {
|
|
54
|
+
case SDK_COORDINATION_MESSAGE_TYPES.query:
|
|
55
|
+
this.sendRegister();
|
|
56
|
+
break;
|
|
57
|
+
case SDK_COORDINATION_MESSAGE_TYPES.register:
|
|
58
|
+
this.handleSdkRegister(data.payload);
|
|
59
|
+
break;
|
|
60
|
+
case SDK_COORDINATION_MESSAGE_TYPES.unregister:
|
|
61
|
+
this.handleSdkUnregister(data.payload.sdkId);
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
this.sdkId = config.sdkId || `sdk-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
|
|
66
|
+
this.messageSource = config.messageSource;
|
|
67
|
+
this.level = config.level ?? 0;
|
|
68
|
+
this.methodLevels = config.methodLevels ?? {};
|
|
69
|
+
this.implementedMethods = config.implementedMethods;
|
|
70
|
+
this.implementedMethods.forEach((method) => {
|
|
71
|
+
this.higherLevelSdks[method] = /* @__PURE__ */ new Set();
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* 初始化协调器
|
|
76
|
+
*/
|
|
77
|
+
init() {
|
|
78
|
+
window.addEventListener("message", this.handleCoordinationMessage);
|
|
79
|
+
this.sendQuery();
|
|
80
|
+
this.sendRegister();
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* 销毁协调器
|
|
84
|
+
*/
|
|
85
|
+
destroy() {
|
|
86
|
+
this.isDestroyed = true;
|
|
87
|
+
this.sendUnregister();
|
|
88
|
+
window.removeEventListener("message", this.handleCoordinationMessage);
|
|
89
|
+
Object.values(this.higherLevelSdks).forEach((set) => set.clear());
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* 判断是否应该响应某个方法的请求
|
|
93
|
+
* @param method - 方法名
|
|
94
|
+
* @returns 是否应该响应
|
|
95
|
+
*/
|
|
96
|
+
shouldRespond(method) {
|
|
97
|
+
if (this.isDestroyed) return false;
|
|
98
|
+
if (!this.implementedMethods.includes(method)) {
|
|
99
|
+
return false;
|
|
100
|
+
}
|
|
101
|
+
const higherSdks = this.higherLevelSdks[method];
|
|
102
|
+
return !higherSdks || higherSdks.size === 0;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* 获取方法的优先级
|
|
106
|
+
*/
|
|
107
|
+
getMethodLevel(method) {
|
|
108
|
+
return this.methodLevels[method] ?? this.level;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* 处理其他 SDK 的注册
|
|
112
|
+
*/
|
|
113
|
+
handleSdkRegister(info) {
|
|
114
|
+
if (info.sdkId === this.sdkId) return;
|
|
115
|
+
if (info.messageSource !== this.messageSource) return;
|
|
116
|
+
this.implementedMethods.forEach((method) => {
|
|
117
|
+
if (!info.implementedMethods.includes(method)) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
const myLevel = this.getMethodLevel(method);
|
|
121
|
+
const otherLevel = info.methodLevels[method] ?? info.level;
|
|
122
|
+
if (otherLevel > myLevel) {
|
|
123
|
+
this.higherLevelSdks[method].add(info.sdkId);
|
|
124
|
+
} else {
|
|
125
|
+
this.higherLevelSdks[method].delete(info.sdkId);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* 处理其他 SDK 的注销
|
|
131
|
+
*/
|
|
132
|
+
handleSdkUnregister(sdkId) {
|
|
133
|
+
Object.values(this.higherLevelSdks).forEach((set) => {
|
|
134
|
+
set.delete(sdkId);
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* 发送查询消息
|
|
139
|
+
*/
|
|
140
|
+
sendQuery() {
|
|
141
|
+
const message = {
|
|
142
|
+
source: SDK_COORDINATOR_SOURCE,
|
|
143
|
+
type: SDK_COORDINATION_MESSAGE_TYPES.query,
|
|
144
|
+
payload: { sdkId: this.sdkId }
|
|
145
|
+
};
|
|
146
|
+
this.postCoordinationMessage(message);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* 发送注册消息
|
|
150
|
+
*/
|
|
151
|
+
sendRegister() {
|
|
152
|
+
const message = {
|
|
153
|
+
source: SDK_COORDINATOR_SOURCE,
|
|
154
|
+
type: SDK_COORDINATION_MESSAGE_TYPES.register,
|
|
155
|
+
payload: {
|
|
156
|
+
sdkId: this.sdkId,
|
|
157
|
+
messageSource: this.messageSource,
|
|
158
|
+
level: this.level,
|
|
159
|
+
methodLevels: this.methodLevels,
|
|
160
|
+
implementedMethods: this.implementedMethods
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
this.postCoordinationMessage(message);
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* 发送注销消息
|
|
167
|
+
*/
|
|
168
|
+
sendUnregister() {
|
|
169
|
+
const message = {
|
|
170
|
+
source: SDK_COORDINATOR_SOURCE,
|
|
171
|
+
type: SDK_COORDINATION_MESSAGE_TYPES.unregister,
|
|
172
|
+
payload: { sdkId: this.sdkId }
|
|
173
|
+
};
|
|
174
|
+
this.postCoordinationMessage(message);
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* 发送协调消息
|
|
178
|
+
*/
|
|
179
|
+
postCoordinationMessage(message) {
|
|
180
|
+
window.postMessage(message, "*");
|
|
181
|
+
if (window.top && window.top !== window) {
|
|
182
|
+
window.top.postMessage(message, "*");
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
// src/core.ts
|
|
188
|
+
function createSchemaElementEditorBridge(config) {
|
|
189
|
+
const {
|
|
190
|
+
getSchema,
|
|
191
|
+
updateSchema,
|
|
192
|
+
renderPreview,
|
|
193
|
+
sourceConfig,
|
|
194
|
+
messageTypes,
|
|
195
|
+
sdkId,
|
|
196
|
+
level,
|
|
197
|
+
methodLevels
|
|
198
|
+
} = config;
|
|
199
|
+
const mergedSourceConfig = {
|
|
200
|
+
...DEFAULT_SOURCE_CONFIG,
|
|
201
|
+
...sourceConfig
|
|
202
|
+
};
|
|
203
|
+
const mergedMessageTypes = {
|
|
204
|
+
...DEFAULT_MESSAGE_TYPES,
|
|
205
|
+
...messageTypes
|
|
206
|
+
};
|
|
207
|
+
const implementedMethods = [];
|
|
208
|
+
if (typeof getSchema === "function") implementedMethods.push("getSchema");
|
|
209
|
+
if (typeof updateSchema === "function") implementedMethods.push("updateSchema");
|
|
210
|
+
if (typeof renderPreview === "function") {
|
|
211
|
+
implementedMethods.push("checkPreview", "renderPreview", "cleanupPreview");
|
|
212
|
+
}
|
|
213
|
+
implementedMethods.push("startRecording", "stopRecording");
|
|
214
|
+
const coordinator = new SdkCoordinator({
|
|
215
|
+
sdkId,
|
|
216
|
+
messageSource: mergedSourceConfig.contentSource,
|
|
217
|
+
level,
|
|
218
|
+
methodLevels,
|
|
219
|
+
implementedMethods
|
|
220
|
+
});
|
|
221
|
+
coordinator.init();
|
|
222
|
+
let previewCleanupFn = null;
|
|
223
|
+
const recordingParams = /* @__PURE__ */ new Set();
|
|
224
|
+
const currentConfig = { getSchema, updateSchema, renderPreview };
|
|
225
|
+
const createMethodTypeMap = () => ({
|
|
226
|
+
[mergedMessageTypes.getSchema]: "getSchema",
|
|
227
|
+
[mergedMessageTypes.updateSchema]: "updateSchema",
|
|
228
|
+
[mergedMessageTypes.checkPreview]: "checkPreview",
|
|
229
|
+
[mergedMessageTypes.renderPreview]: "renderPreview",
|
|
230
|
+
[mergedMessageTypes.cleanupPreview]: "cleanupPreview",
|
|
231
|
+
[mergedMessageTypes.startRecording]: "startRecording",
|
|
232
|
+
[mergedMessageTypes.stopRecording]: "stopRecording"
|
|
233
|
+
});
|
|
234
|
+
const methodTypeToName = createMethodTypeMap();
|
|
235
|
+
const getMethodNameByType = (type) => {
|
|
236
|
+
return methodTypeToName[type] ?? null;
|
|
237
|
+
};
|
|
238
|
+
const sendResponse = (requestId, result) => {
|
|
239
|
+
const message = {
|
|
240
|
+
source: mergedSourceConfig.hostSource,
|
|
241
|
+
requestId,
|
|
242
|
+
...result
|
|
243
|
+
};
|
|
244
|
+
const targetWindow = window.top ?? window;
|
|
245
|
+
targetWindow.postMessage(message, "*");
|
|
246
|
+
};
|
|
247
|
+
const pushSchema = (params, data) => {
|
|
248
|
+
if (!recordingParams.has(params)) {
|
|
249
|
+
return;
|
|
250
|
+
}
|
|
251
|
+
const message = {
|
|
252
|
+
source: mergedSourceConfig.hostSource,
|
|
253
|
+
type: mergedMessageTypes.schemaPush,
|
|
254
|
+
payload: {
|
|
255
|
+
success: true,
|
|
256
|
+
data,
|
|
257
|
+
params
|
|
258
|
+
}
|
|
259
|
+
};
|
|
260
|
+
const targetWindow = window.top ?? window;
|
|
261
|
+
targetWindow.postMessage(message, "*");
|
|
262
|
+
};
|
|
263
|
+
const handleMessage = (event) => {
|
|
264
|
+
const isFromSelf = event.source === window;
|
|
265
|
+
const isFromParent = window !== window.top && event.source === window.parent;
|
|
266
|
+
if (!isFromSelf && !isFromParent) return;
|
|
267
|
+
if (!event.data || event.data.source !== mergedSourceConfig.contentSource) return;
|
|
268
|
+
const { type, payload, requestId } = event.data;
|
|
269
|
+
if (!requestId) return;
|
|
270
|
+
const methodName = getMethodNameByType(type);
|
|
271
|
+
if (!methodName) return;
|
|
272
|
+
if (!coordinator.shouldRespond(methodName)) {
|
|
273
|
+
return;
|
|
274
|
+
}
|
|
275
|
+
const { getSchema: getSchema2, updateSchema: updateSchema2, renderPreview: renderPreview2 } = currentConfig;
|
|
276
|
+
let result;
|
|
277
|
+
switch (type) {
|
|
278
|
+
case mergedMessageTypes.getSchema: {
|
|
279
|
+
if (typeof getSchema2 !== "function") {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
const params = String(payload?.params ?? "");
|
|
283
|
+
try {
|
|
284
|
+
const data = getSchema2(params);
|
|
285
|
+
result = { success: true, data };
|
|
286
|
+
} catch (error) {
|
|
287
|
+
result = {
|
|
288
|
+
success: false,
|
|
289
|
+
error: error instanceof Error ? error.message : "\u83B7\u53D6 Schema \u5931\u8D25"
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
break;
|
|
293
|
+
}
|
|
294
|
+
case mergedMessageTypes.updateSchema: {
|
|
295
|
+
if (typeof updateSchema2 !== "function") {
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
const schema = payload?.schema;
|
|
299
|
+
const params = String(payload?.params ?? "");
|
|
300
|
+
try {
|
|
301
|
+
const success = updateSchema2(schema, params);
|
|
302
|
+
result = { success };
|
|
303
|
+
} catch (error) {
|
|
304
|
+
result = {
|
|
305
|
+
success: false,
|
|
306
|
+
error: error instanceof Error ? error.message : "\u66F4\u65B0 Schema \u5931\u8D25"
|
|
307
|
+
};
|
|
308
|
+
}
|
|
309
|
+
break;
|
|
310
|
+
}
|
|
311
|
+
case mergedMessageTypes.checkPreview: {
|
|
312
|
+
result = { exists: typeof renderPreview2 === "function" };
|
|
313
|
+
break;
|
|
314
|
+
}
|
|
315
|
+
case mergedMessageTypes.renderPreview: {
|
|
316
|
+
if (typeof renderPreview2 !== "function") {
|
|
317
|
+
result = { success: false, error: "\u9884\u89C8\u529F\u80FD\u672A\u914D\u7F6E" };
|
|
318
|
+
break;
|
|
319
|
+
}
|
|
320
|
+
const schema = payload?.schema;
|
|
321
|
+
const containerId = String(payload?.containerId ?? "");
|
|
322
|
+
try {
|
|
323
|
+
if (previewCleanupFn) {
|
|
324
|
+
previewCleanupFn();
|
|
325
|
+
previewCleanupFn = null;
|
|
326
|
+
}
|
|
327
|
+
const cleanup = renderPreview2(schema, containerId);
|
|
328
|
+
if (typeof cleanup === "function") {
|
|
329
|
+
previewCleanupFn = cleanup;
|
|
330
|
+
}
|
|
331
|
+
result = { success: true };
|
|
332
|
+
} catch (error) {
|
|
333
|
+
result = {
|
|
334
|
+
success: false,
|
|
335
|
+
error: error instanceof Error ? error.message : "\u6E32\u67D3\u9884\u89C8\u5931\u8D25"
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
break;
|
|
339
|
+
}
|
|
340
|
+
case mergedMessageTypes.cleanupPreview: {
|
|
341
|
+
try {
|
|
342
|
+
if (previewCleanupFn) {
|
|
343
|
+
previewCleanupFn();
|
|
344
|
+
previewCleanupFn = null;
|
|
345
|
+
}
|
|
346
|
+
result = { success: true };
|
|
347
|
+
} catch (error) {
|
|
348
|
+
result = {
|
|
349
|
+
success: false,
|
|
350
|
+
error: error instanceof Error ? error.message : "\u6E05\u7406\u9884\u89C8\u5931\u8D25"
|
|
351
|
+
};
|
|
352
|
+
}
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
case mergedMessageTypes.startRecording: {
|
|
356
|
+
const params = String(payload?.params ?? "");
|
|
357
|
+
recordingParams.add(params);
|
|
358
|
+
result = { success: true };
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
case mergedMessageTypes.stopRecording: {
|
|
362
|
+
const params = String(payload?.params ?? "");
|
|
363
|
+
recordingParams.delete(params);
|
|
364
|
+
result = { success: true };
|
|
365
|
+
break;
|
|
366
|
+
}
|
|
367
|
+
default:
|
|
368
|
+
return;
|
|
369
|
+
}
|
|
370
|
+
sendResponse(requestId, result);
|
|
371
|
+
};
|
|
372
|
+
window.addEventListener("message", handleMessage);
|
|
373
|
+
return {
|
|
374
|
+
cleanup: () => {
|
|
375
|
+
coordinator.destroy();
|
|
376
|
+
window.removeEventListener("message", handleMessage);
|
|
377
|
+
if (previewCleanupFn) {
|
|
378
|
+
previewCleanupFn();
|
|
379
|
+
previewCleanupFn = null;
|
|
380
|
+
}
|
|
381
|
+
recordingParams.clear();
|
|
382
|
+
},
|
|
383
|
+
recording: {
|
|
384
|
+
push: pushSchema
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
export {
|
|
390
|
+
createSchemaElementEditorBridge
|
|
391
|
+
};
|
|
File without changes
|