@schema-element-editor/host-sdk 2.0.3 → 2.1.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/dist/react.cjs CHANGED
@@ -25,11 +25,12 @@ __export(react_exports, {
25
25
  module.exports = __toCommonJS(react_exports);
26
26
  var import_react = require("react");
27
27
 
28
- // src/core.ts
28
+ // src/constants.ts
29
29
  var DEFAULT_SOURCE_CONFIG = {
30
30
  contentSource: "schema-element-editor-content",
31
31
  hostSource: "schema-element-editor-host"
32
32
  };
33
+ var SDK_COORDINATOR_SOURCE = "schema-element-editor-sdk-coordinator";
33
34
  var DEFAULT_MESSAGE_TYPES = {
34
35
  getSchema: "GET_SCHEMA",
35
36
  updateSchema: "UPDATE_SCHEMA",
@@ -41,8 +42,245 @@ var DEFAULT_MESSAGE_TYPES = {
41
42
  stopRecording: "STOP_RECORDING",
42
43
  schemaPush: "SCHEMA_PUSH"
43
44
  };
45
+ var SDK_COORDINATION_MESSAGE_TYPES = {
46
+ register: "SDK_REGISTER",
47
+ unregister: "SDK_UNREGISTER",
48
+ query: "SDK_QUERY"
49
+ };
50
+
51
+ // src/coordinator.ts
52
+ function generateSdkId() {
53
+ if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
54
+ return crypto.randomUUID();
55
+ }
56
+ return `sdk-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
57
+ }
58
+ var SdkCoordinator = class {
59
+ constructor(config) {
60
+ /**
61
+ * SDK 销毁标志
62
+ *
63
+ * 作用:防止在 cleanup() 执行期间,监听器移除前仍然响应请求
64
+ *
65
+ * 场景:cleanup() 中先调用 destroy()(设置 isDestroyed = true),
66
+ * 然后才移除 handleMessage 监听器。在这个微小的时间窗口内,
67
+ * 如果插件请求到达,没有 isDestroyed 检查会导致已销毁的 SDK 仍然响应。
68
+ */
69
+ this.isDestroyed = false;
70
+ /**
71
+ * 存储比当前 SDK 优先级更高的其他 SDK ID
72
+ * 按方法分类:如果某个方法的集合不为空,说明有更高优先级的 SDK 应该处理该方法
73
+ *
74
+ * 优化:只为当前 SDK 实现了的方法维护优先级集合
75
+ */
76
+ this.higherLevelSDKs = {};
77
+ /**
78
+ * 存储与当前 SDK 优先级相同的其他 SDK ID
79
+ * 按方法分类:用于判断是否需要在执行失败时跳过响应
80
+ *
81
+ * 当有同级竞争者时,SDK在执行失败/无数据时会跳过响应,让其他SDK有机会响应
82
+ */
83
+ this.sameLevelSDKs = {};
84
+ /**
85
+ * 处理 SDK 协调消息
86
+ */
87
+ this.handleCoordinationMessage = (event) => {
88
+ const isFromSelf = event.source === window;
89
+ const isFromParent = window !== window.top && event.source === window.parent;
90
+ if (!isFromSelf && !isFromParent) return;
91
+ const data = event.data;
92
+ if (!data || data.source !== SDK_COORDINATOR_SOURCE) return;
93
+ switch (data.type) {
94
+ case SDK_COORDINATION_MESSAGE_TYPES.query:
95
+ this.sendRegister();
96
+ break;
97
+ case SDK_COORDINATION_MESSAGE_TYPES.register:
98
+ this.handleSdkRegister(data.payload);
99
+ break;
100
+ case SDK_COORDINATION_MESSAGE_TYPES.unregister:
101
+ this.handleSdkUnregister(data.payload.sdkId);
102
+ break;
103
+ }
104
+ };
105
+ this.sdkId = config.sdkId || generateSdkId();
106
+ this.messageSource = config.messageSource;
107
+ this.level = config.level ?? 0;
108
+ this.methodLevels = config.methodLevels ?? {};
109
+ this.implementedMethods = config.implementedMethods;
110
+ this.implementedMethods.forEach((method) => {
111
+ this.higherLevelSDKs[method] = /* @__PURE__ */ new Set();
112
+ this.sameLevelSDKs[method] = /* @__PURE__ */ new Set();
113
+ });
114
+ }
115
+ /**
116
+ * 初始化协调器
117
+ */
118
+ init() {
119
+ window.addEventListener("message", this.handleCoordinationMessage);
120
+ this.sendQuery();
121
+ this.sendRegister();
122
+ }
123
+ /**
124
+ * 销毁协调器
125
+ */
126
+ destroy() {
127
+ this.isDestroyed = true;
128
+ this.sendUnregister();
129
+ window.removeEventListener("message", this.handleCoordinationMessage);
130
+ Object.values(this.higherLevelSDKs).forEach((set) => set.clear());
131
+ Object.values(this.sameLevelSDKs).forEach((set) => set.clear());
132
+ }
133
+ /**
134
+ * 判断是否应该响应某个方法的请求
135
+ * @param method - 方法名
136
+ * @returns 是否应该响应
137
+ */
138
+ shouldRespond(method) {
139
+ if (this.isDestroyed) return false;
140
+ if (!this.implementedMethods.includes(method)) {
141
+ return false;
142
+ }
143
+ const higherSDKs = this.higherLevelSDKs[method];
144
+ return !higherSDKs || higherSDKs.size === 0;
145
+ }
146
+ /**
147
+ * 判断某个方法是否有相同优先级的竞争者
148
+ * @param method - 方法名
149
+ * @returns 是否存在同级竞争者
150
+ */
151
+ hasSameLevelCompetitors(method) {
152
+ const sameSDKs = this.sameLevelSDKs[method];
153
+ return sameSDKs ? sameSDKs.size > 0 : false;
154
+ }
155
+ /**
156
+ * 获取方法的优先级
157
+ */
158
+ getMethodLevel(method) {
159
+ return this.methodLevels[method] ?? this.level;
160
+ }
161
+ /**
162
+ * 处理其他 SDK 的注册
163
+ */
164
+ handleSdkRegister(info) {
165
+ if (info.sdkId === this.sdkId) return;
166
+ if (info.messageSource !== this.messageSource) return;
167
+ this.implementedMethods.forEach((method) => {
168
+ if (!info.implementedMethods.includes(method)) {
169
+ return;
170
+ }
171
+ const myLevel = this.getMethodLevel(method);
172
+ const otherLevel = info.methodLevels[method] ?? info.level;
173
+ if (otherLevel > myLevel) {
174
+ this.higherLevelSDKs[method].add(info.sdkId);
175
+ this.sameLevelSDKs[method].delete(info.sdkId);
176
+ } else if (otherLevel === myLevel) {
177
+ this.sameLevelSDKs[method].add(info.sdkId);
178
+ this.higherLevelSDKs[method].delete(info.sdkId);
179
+ } else {
180
+ this.higherLevelSDKs[method].delete(info.sdkId);
181
+ this.sameLevelSDKs[method].delete(info.sdkId);
182
+ }
183
+ });
184
+ }
185
+ /**
186
+ * 处理其他 SDK 的注销
187
+ */
188
+ handleSdkUnregister(sdkId) {
189
+ Object.values(this.higherLevelSDKs).forEach((set) => {
190
+ set.delete(sdkId);
191
+ });
192
+ Object.values(this.sameLevelSDKs).forEach((set) => {
193
+ set.delete(sdkId);
194
+ });
195
+ }
196
+ /**
197
+ * 发送查询消息
198
+ */
199
+ sendQuery() {
200
+ const message = {
201
+ source: SDK_COORDINATOR_SOURCE,
202
+ type: SDK_COORDINATION_MESSAGE_TYPES.query,
203
+ payload: { sdkId: this.sdkId }
204
+ };
205
+ this.postCoordinationMessage(message);
206
+ }
207
+ /**
208
+ * 发送注册消息
209
+ */
210
+ sendRegister() {
211
+ const message = {
212
+ source: SDK_COORDINATOR_SOURCE,
213
+ type: SDK_COORDINATION_MESSAGE_TYPES.register,
214
+ payload: {
215
+ sdkId: this.sdkId,
216
+ messageSource: this.messageSource,
217
+ level: this.level,
218
+ methodLevels: this.methodLevels,
219
+ implementedMethods: this.implementedMethods
220
+ }
221
+ };
222
+ this.postCoordinationMessage(message);
223
+ }
224
+ /**
225
+ * 发送注销消息
226
+ */
227
+ sendUnregister() {
228
+ const message = {
229
+ source: SDK_COORDINATOR_SOURCE,
230
+ type: SDK_COORDINATION_MESSAGE_TYPES.unregister,
231
+ payload: { sdkId: this.sdkId }
232
+ };
233
+ this.postCoordinationMessage(message);
234
+ }
235
+ /**
236
+ * 发送协调消息
237
+ */
238
+ postCoordinationMessage(message) {
239
+ window.postMessage(message, "*");
240
+ if (window.top && window.top !== window) {
241
+ window.top.postMessage(message, "*");
242
+ }
243
+ }
244
+ };
245
+
246
+ // src/bridge.ts
247
+ var METHOD_NAMES = {
248
+ GET_SCHEMA: "getSchema",
249
+ UPDATE_SCHEMA: "updateSchema",
250
+ CHECK_PREVIEW: "checkPreview",
251
+ RENDER_PREVIEW: "renderPreview",
252
+ CLEANUP_PREVIEW: "cleanupPreview",
253
+ START_RECORDING: "startRecording",
254
+ STOP_RECORDING: "stopRecording"
255
+ };
256
+ function shouldSkipFailedResponse(method, result) {
257
+ switch (method) {
258
+ case METHOD_NAMES.GET_SCHEMA:
259
+ return result.success === true && result.data === void 0;
260
+ case METHOD_NAMES.UPDATE_SCHEMA:
261
+ return result.success === false;
262
+ case METHOD_NAMES.RENDER_PREVIEW:
263
+ case METHOD_NAMES.CLEANUP_PREVIEW:
264
+ return result.success === false;
265
+ case METHOD_NAMES.CHECK_PREVIEW:
266
+ case METHOD_NAMES.START_RECORDING:
267
+ case METHOD_NAMES.STOP_RECORDING:
268
+ return false;
269
+ default:
270
+ return false;
271
+ }
272
+ }
44
273
  function createSchemaElementEditorBridge(config) {
45
- const { getSchema, updateSchema, renderPreview, sourceConfig, messageTypes } = config;
274
+ const {
275
+ getSchema,
276
+ updateSchema,
277
+ renderPreview,
278
+ sourceConfig,
279
+ messageTypes,
280
+ sdkId,
281
+ level,
282
+ methodLevels
283
+ } = config;
46
284
  const mergedSourceConfig = {
47
285
  ...DEFAULT_SOURCE_CONFIG,
48
286
  ...sourceConfig
@@ -51,9 +289,40 @@ function createSchemaElementEditorBridge(config) {
51
289
  ...DEFAULT_MESSAGE_TYPES,
52
290
  ...messageTypes
53
291
  };
292
+ const implementedMethods = [];
293
+ if (typeof getSchema === "function") implementedMethods.push(METHOD_NAMES.GET_SCHEMA);
294
+ if (typeof updateSchema === "function") implementedMethods.push(METHOD_NAMES.UPDATE_SCHEMA);
295
+ if (typeof renderPreview === "function") {
296
+ implementedMethods.push(
297
+ METHOD_NAMES.CHECK_PREVIEW,
298
+ METHOD_NAMES.RENDER_PREVIEW,
299
+ METHOD_NAMES.CLEANUP_PREVIEW
300
+ );
301
+ }
302
+ implementedMethods.push(METHOD_NAMES.START_RECORDING, METHOD_NAMES.STOP_RECORDING);
303
+ const coordinator = new SdkCoordinator({
304
+ sdkId,
305
+ messageSource: mergedSourceConfig.contentSource,
306
+ level,
307
+ methodLevels,
308
+ implementedMethods
309
+ });
310
+ coordinator.init();
54
311
  let previewCleanupFn = null;
55
312
  const recordingParams = /* @__PURE__ */ new Set();
56
313
  const currentConfig = { getSchema, updateSchema, renderPreview };
314
+ const methodTypeToName = {
315
+ [mergedMessageTypes.getSchema]: METHOD_NAMES.GET_SCHEMA,
316
+ [mergedMessageTypes.updateSchema]: METHOD_NAMES.UPDATE_SCHEMA,
317
+ [mergedMessageTypes.checkPreview]: METHOD_NAMES.CHECK_PREVIEW,
318
+ [mergedMessageTypes.renderPreview]: METHOD_NAMES.RENDER_PREVIEW,
319
+ [mergedMessageTypes.cleanupPreview]: METHOD_NAMES.CLEANUP_PREVIEW,
320
+ [mergedMessageTypes.startRecording]: METHOD_NAMES.START_RECORDING,
321
+ [mergedMessageTypes.stopRecording]: METHOD_NAMES.STOP_RECORDING
322
+ };
323
+ const getMethodNameByType = (type) => {
324
+ return methodTypeToName[type] ?? null;
325
+ };
57
326
  const sendResponse = (requestId, result) => {
58
327
  const message = {
59
328
  source: mergedSourceConfig.hostSource,
@@ -86,10 +355,18 @@ function createSchemaElementEditorBridge(config) {
86
355
  if (!event.data || event.data.source !== mergedSourceConfig.contentSource) return;
87
356
  const { type, payload, requestId } = event.data;
88
357
  if (!requestId) return;
358
+ const methodName = getMethodNameByType(type);
359
+ if (!methodName) return;
360
+ if (!coordinator.shouldRespond(methodName)) {
361
+ return;
362
+ }
89
363
  const { getSchema: getSchema2, updateSchema: updateSchema2, renderPreview: renderPreview2 } = currentConfig;
90
364
  let result;
91
365
  switch (type) {
92
366
  case mergedMessageTypes.getSchema: {
367
+ if (typeof getSchema2 !== "function") {
368
+ return;
369
+ }
93
370
  const params = String(payload?.params ?? "");
94
371
  try {
95
372
  const data = getSchema2(params);
@@ -103,6 +380,9 @@ function createSchemaElementEditorBridge(config) {
103
380
  break;
104
381
  }
105
382
  case mergedMessageTypes.updateSchema: {
383
+ if (typeof updateSchema2 !== "function") {
384
+ return;
385
+ }
106
386
  const schema = payload?.schema;
107
387
  const params = String(payload?.params ?? "");
108
388
  try {
@@ -175,11 +455,16 @@ function createSchemaElementEditorBridge(config) {
175
455
  default:
176
456
  return;
177
457
  }
458
+ const hasSameLevelSdks = coordinator.hasSameLevelCompetitors(methodName);
459
+ if (hasSameLevelSdks && shouldSkipFailedResponse(methodName, result)) {
460
+ return;
461
+ }
178
462
  sendResponse(requestId, result);
179
463
  };
180
464
  window.addEventListener("message", handleMessage);
181
465
  return {
182
466
  cleanup: () => {
467
+ coordinator.destroy();
183
468
  window.removeEventListener("message", handleMessage);
184
469
  if (previewCleanupFn) {
185
470
  previewCleanupFn();
@@ -195,7 +480,17 @@ function createSchemaElementEditorBridge(config) {
195
480
 
196
481
  // src/react.ts
197
482
  function useSchemaElementEditor(config) {
198
- const { getSchema, updateSchema, renderPreview, sourceConfig, messageTypes, enabled } = config;
483
+ const {
484
+ getSchema,
485
+ updateSchema,
486
+ renderPreview,
487
+ sourceConfig,
488
+ messageTypes,
489
+ enabled,
490
+ sdkId,
491
+ level,
492
+ methodLevels
493
+ } = config;
199
494
  const configRef = (0, import_react.useRef)({ getSchema, updateSchema, renderPreview });
200
495
  const bridgeRef = (0, import_react.useRef)(null);
201
496
  (0, import_react.useEffect)(() => {
@@ -206,11 +501,16 @@ function useSchemaElementEditor(config) {
206
501
  return;
207
502
  }
208
503
  const proxyConfig = {
209
- getSchema: (params) => configRef.current.getSchema(params),
210
- updateSchema: (schema, params) => configRef.current.updateSchema(schema, params),
504
+ // 外层检查确保初始时函数存在才创建代理
505
+ // 内层使用可选链安全访问,配合类型断言确保类型正确
506
+ getSchema: configRef.current.getSchema ? (params) => configRef.current.getSchema?.(params) : void 0,
507
+ updateSchema: configRef.current.updateSchema ? (schema, params) => configRef.current.updateSchema?.(schema, params) : void 0,
211
508
  renderPreview: configRef.current.renderPreview ? (schema, containerId) => configRef.current.renderPreview?.(schema, containerId) : void 0,
212
509
  sourceConfig,
213
- messageTypes
510
+ messageTypes,
511
+ sdkId,
512
+ level,
513
+ methodLevels
214
514
  };
215
515
  const bridge = createSchemaElementEditorBridge(proxyConfig);
216
516
  bridgeRef.current = bridge;
@@ -218,7 +518,7 @@ function useSchemaElementEditor(config) {
218
518
  bridge.cleanup();
219
519
  bridgeRef.current = null;
220
520
  };
221
- }, [enabled, configRef, sourceConfig, messageTypes]);
521
+ }, [enabled, configRef, sourceConfig, messageTypes, sdkId, level, methodLevels]);
222
522
  const push = (0, import_react.useCallback)((params, data) => {
223
523
  bridgeRef.current?.recording.push(params, data);
224
524
  }, []);
package/dist/react.d.cts CHANGED
@@ -1,5 +1,4 @@
1
- import { SchemaElementEditorConfig, SchemaElementEditorRecording } from './core.cjs';
2
- export { PostMessageSourceConfig, PostMessageTypeConfig, SchemaElementEditorBridge, SchemaValue } from './core.cjs';
1
+ import { b as SchemaElementEditorConfig, d as SchemaElementEditorRecording } from './types-D2ZJx8T_.cjs';
3
2
 
4
3
  /**
5
4
  * Schema Element Editor Host SDK - React
@@ -51,4 +50,4 @@ interface UseSchemaElementEditorReturn {
51
50
  */
52
51
  declare function useSchemaElementEditor(config: ReactSchemaElementEditorConfig): UseSchemaElementEditorReturn;
53
52
 
54
- export { type ReactSchemaElementEditorConfig, SchemaElementEditorConfig, SchemaElementEditorRecording, type UseSchemaElementEditorReturn, useSchemaElementEditor };
53
+ export { type ReactSchemaElementEditorConfig, type UseSchemaElementEditorReturn, useSchemaElementEditor };
package/dist/react.d.ts CHANGED
@@ -1,5 +1,4 @@
1
- import { SchemaElementEditorConfig, SchemaElementEditorRecording } from './core.js';
2
- export { PostMessageSourceConfig, PostMessageTypeConfig, SchemaElementEditorBridge, SchemaValue } from './core.js';
1
+ import { b as SchemaElementEditorConfig, d as SchemaElementEditorRecording } from './types-D2ZJx8T_.js';
3
2
 
4
3
  /**
5
4
  * Schema Element Editor Host SDK - React
@@ -51,4 +50,4 @@ interface UseSchemaElementEditorReturn {
51
50
  */
52
51
  declare function useSchemaElementEditor(config: ReactSchemaElementEditorConfig): UseSchemaElementEditorReturn;
53
52
 
54
- export { type ReactSchemaElementEditorConfig, SchemaElementEditorConfig, SchemaElementEditorRecording, type UseSchemaElementEditorReturn, useSchemaElementEditor };
53
+ export { type ReactSchemaElementEditorConfig, type UseSchemaElementEditorReturn, useSchemaElementEditor };
package/dist/react.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  useSchemaElementEditor
3
- } from "./chunk-HBZGOJIJ.js";
4
- import "./chunk-52EFKQHQ.js";
3
+ } from "./chunk-MK3EDWC5.js";
4
+ import "./chunk-DLMX4NDA.js";
5
5
  export {
6
6
  useSchemaElementEditor
7
7
  };
@@ -0,0 +1,103 @@
1
+ /**
2
+ * Schema Element Editor Host SDK - Types
3
+ * 类型定义
4
+ */
5
+ /**
6
+ * Schema 数据类型
7
+ * 支持所有 JSON.parse 可返回的类型
8
+ */
9
+ type SchemaValue = Record<string, unknown> | unknown[] | string | number | boolean | null;
10
+ /** postMessage 消息标识配置 */
11
+ interface PostMessageSourceConfig {
12
+ /** 插件端发送消息的 source 标识 */
13
+ contentSource: string;
14
+ /** 宿主端响应消息的 source 标识 */
15
+ hostSource: string;
16
+ }
17
+ /** postMessage 消息类型配置 */
18
+ interface PostMessageTypeConfig {
19
+ getSchema: string;
20
+ updateSchema: string;
21
+ checkPreview: string;
22
+ renderPreview: string;
23
+ cleanupPreview: string;
24
+ startRecording: string;
25
+ stopRecording: string;
26
+ schemaPush: string;
27
+ }
28
+ /** SDK 方法级别配置 */
29
+ interface MethodLevelConfig {
30
+ getSchema?: number;
31
+ updateSchema?: number;
32
+ checkPreview?: number;
33
+ renderPreview?: number;
34
+ cleanupPreview?: number;
35
+ startRecording?: number;
36
+ stopRecording?: number;
37
+ }
38
+ /**
39
+ * Schema Element Editor 配置接口
40
+ */
41
+ interface SchemaElementEditorConfig {
42
+ /**
43
+ * 获取 Schema 数据(可选)
44
+ * @param params - 元素参数(通常是 data-id 的值)
45
+ * @returns Schema 数据(支持所有 JSON 类型)
46
+ */
47
+ getSchema?: (params: string) => SchemaValue;
48
+ /**
49
+ * 更新 Schema 数据(可选)
50
+ * @param schema - 新的 Schema 数据(支持所有 JSON 类型)
51
+ * @param params - 元素参数
52
+ * @returns 是否更新成功
53
+ */
54
+ updateSchema?: (schema: SchemaValue, params: string) => boolean;
55
+ /**
56
+ * 渲染预览(可选)
57
+ * @param schema - Schema 数据(支持所有 JSON 类型)
58
+ * @param containerId - 预览容器 ID
59
+ * @returns 清理函数(可选)
60
+ */
61
+ renderPreview?: (schema: SchemaValue, containerId: string) => (() => void) | void;
62
+ /** 消息标识配置(可选,有默认值) */
63
+ sourceConfig?: Partial<PostMessageSourceConfig>;
64
+ /** 消息类型配置(可选,有默认值) */
65
+ messageTypes?: Partial<PostMessageTypeConfig>;
66
+ /**
67
+ * SDK 实例唯一标识(可选,自动生成)
68
+ * 用于多 SDK 实例协调
69
+ */
70
+ sdkId?: string;
71
+ /**
72
+ * SDK 优先级(可选,默认 0)
73
+ * 数值越大优先级越高,当多个 SDK 实例共存时,优先级高的响应请求
74
+ */
75
+ level?: number;
76
+ /**
77
+ * 方法级别优先级配置(可选)
78
+ * 可以为每个方法单独配置优先级,未配置的方法使用 level 作为优先级
79
+ */
80
+ methodLevels?: MethodLevelConfig;
81
+ }
82
+ /**
83
+ * 录制相关方法
84
+ */
85
+ interface SchemaElementEditorRecording {
86
+ /**
87
+ * 推送 Schema 数据(SDK 内部判断是否在录制,未录制时静默忽略)
88
+ * @param params - 元素参数(data-id 的值)
89
+ * @param data - Schema 数据
90
+ */
91
+ push: (params: string, data: SchemaValue) => void;
92
+ }
93
+ /**
94
+ * Schema Element Editor 桥接器返回值
95
+ */
96
+ interface SchemaElementEditorBridge {
97
+ /** 清理桥接器,移除事件监听 */
98
+ cleanup: () => void;
99
+ /** 录制相关方法 */
100
+ recording: SchemaElementEditorRecording;
101
+ }
102
+
103
+ export type { MethodLevelConfig as M, PostMessageSourceConfig as P, SchemaValue as S, PostMessageTypeConfig as a, SchemaElementEditorConfig as b, SchemaElementEditorBridge as c, SchemaElementEditorRecording as d };
@@ -0,0 +1,103 @@
1
+ /**
2
+ * Schema Element Editor Host SDK - Types
3
+ * 类型定义
4
+ */
5
+ /**
6
+ * Schema 数据类型
7
+ * 支持所有 JSON.parse 可返回的类型
8
+ */
9
+ type SchemaValue = Record<string, unknown> | unknown[] | string | number | boolean | null;
10
+ /** postMessage 消息标识配置 */
11
+ interface PostMessageSourceConfig {
12
+ /** 插件端发送消息的 source 标识 */
13
+ contentSource: string;
14
+ /** 宿主端响应消息的 source 标识 */
15
+ hostSource: string;
16
+ }
17
+ /** postMessage 消息类型配置 */
18
+ interface PostMessageTypeConfig {
19
+ getSchema: string;
20
+ updateSchema: string;
21
+ checkPreview: string;
22
+ renderPreview: string;
23
+ cleanupPreview: string;
24
+ startRecording: string;
25
+ stopRecording: string;
26
+ schemaPush: string;
27
+ }
28
+ /** SDK 方法级别配置 */
29
+ interface MethodLevelConfig {
30
+ getSchema?: number;
31
+ updateSchema?: number;
32
+ checkPreview?: number;
33
+ renderPreview?: number;
34
+ cleanupPreview?: number;
35
+ startRecording?: number;
36
+ stopRecording?: number;
37
+ }
38
+ /**
39
+ * Schema Element Editor 配置接口
40
+ */
41
+ interface SchemaElementEditorConfig {
42
+ /**
43
+ * 获取 Schema 数据(可选)
44
+ * @param params - 元素参数(通常是 data-id 的值)
45
+ * @returns Schema 数据(支持所有 JSON 类型)
46
+ */
47
+ getSchema?: (params: string) => SchemaValue;
48
+ /**
49
+ * 更新 Schema 数据(可选)
50
+ * @param schema - 新的 Schema 数据(支持所有 JSON 类型)
51
+ * @param params - 元素参数
52
+ * @returns 是否更新成功
53
+ */
54
+ updateSchema?: (schema: SchemaValue, params: string) => boolean;
55
+ /**
56
+ * 渲染预览(可选)
57
+ * @param schema - Schema 数据(支持所有 JSON 类型)
58
+ * @param containerId - 预览容器 ID
59
+ * @returns 清理函数(可选)
60
+ */
61
+ renderPreview?: (schema: SchemaValue, containerId: string) => (() => void) | void;
62
+ /** 消息标识配置(可选,有默认值) */
63
+ sourceConfig?: Partial<PostMessageSourceConfig>;
64
+ /** 消息类型配置(可选,有默认值) */
65
+ messageTypes?: Partial<PostMessageTypeConfig>;
66
+ /**
67
+ * SDK 实例唯一标识(可选,自动生成)
68
+ * 用于多 SDK 实例协调
69
+ */
70
+ sdkId?: string;
71
+ /**
72
+ * SDK 优先级(可选,默认 0)
73
+ * 数值越大优先级越高,当多个 SDK 实例共存时,优先级高的响应请求
74
+ */
75
+ level?: number;
76
+ /**
77
+ * 方法级别优先级配置(可选)
78
+ * 可以为每个方法单独配置优先级,未配置的方法使用 level 作为优先级
79
+ */
80
+ methodLevels?: MethodLevelConfig;
81
+ }
82
+ /**
83
+ * 录制相关方法
84
+ */
85
+ interface SchemaElementEditorRecording {
86
+ /**
87
+ * 推送 Schema 数据(SDK 内部判断是否在录制,未录制时静默忽略)
88
+ * @param params - 元素参数(data-id 的值)
89
+ * @param data - Schema 数据
90
+ */
91
+ push: (params: string, data: SchemaValue) => void;
92
+ }
93
+ /**
94
+ * Schema Element Editor 桥接器返回值
95
+ */
96
+ interface SchemaElementEditorBridge {
97
+ /** 清理桥接器,移除事件监听 */
98
+ cleanup: () => void;
99
+ /** 录制相关方法 */
100
+ recording: SchemaElementEditorRecording;
101
+ }
102
+
103
+ export type { MethodLevelConfig as M, PostMessageSourceConfig as P, SchemaValue as S, PostMessageTypeConfig as a, SchemaElementEditorConfig as b, SchemaElementEditorBridge as c, SchemaElementEditorRecording as d };