@schema-element-editor/host-sdk 2.0.3 → 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/dist/vue.cjs CHANGED
@@ -25,11 +25,12 @@ __export(vue_exports, {
25
25
  module.exports = __toCommonJS(vue_exports);
26
26
  var import_vue = require("vue");
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,239 @@ 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
+ var SdkCoordinator = class {
53
+ constructor(config) {
54
+ /**
55
+ * SDK 销毁标志
56
+ *
57
+ * 作用:防止在 cleanup() 执行期间,监听器移除前仍然响应请求
58
+ *
59
+ * 场景:cleanup() 中先调用 destroy()(设置 isDestroyed = true),
60
+ * 然后才移除 handleMessage 监听器。在这个微小的时间窗口内,
61
+ * 如果插件请求到达,没有 isDestroyed 检查会导致已销毁的 SDK 仍然响应。
62
+ */
63
+ this.isDestroyed = false;
64
+ /**
65
+ * 存储比当前 SDK 优先级更高的其他 SDK ID
66
+ * 按方法分类:如果某个方法的集合不为空,说明有更高优先级的 SDK 应该处理该方法
67
+ *
68
+ * 优化:只为当前 SDK 实现了的方法维护优先级集合
69
+ */
70
+ this.higherLevelSDKs = {};
71
+ /**
72
+ * 存储与当前 SDK 优先级相同的其他 SDK ID
73
+ * 按方法分类:用于判断是否需要在执行失败时跳过响应
74
+ *
75
+ * 当有同级竞争者时,SDK在执行失败/无数据时会跳过响应,让其他SDK有机会响应
76
+ */
77
+ this.sameLevelSDKs = {};
78
+ /**
79
+ * 处理 SDK 协调消息
80
+ */
81
+ this.handleCoordinationMessage = (event) => {
82
+ const isFromSelf = event.source === window;
83
+ const isFromParent = window !== window.top && event.source === window.parent;
84
+ if (!isFromSelf && !isFromParent) return;
85
+ const data = event.data;
86
+ if (!data || data.source !== SDK_COORDINATOR_SOURCE) return;
87
+ switch (data.type) {
88
+ case SDK_COORDINATION_MESSAGE_TYPES.query:
89
+ this.sendRegister();
90
+ break;
91
+ case SDK_COORDINATION_MESSAGE_TYPES.register:
92
+ this.handleSdkRegister(data.payload);
93
+ break;
94
+ case SDK_COORDINATION_MESSAGE_TYPES.unregister:
95
+ this.handleSdkUnregister(data.payload.sdkId);
96
+ break;
97
+ }
98
+ };
99
+ this.sdkId = config.sdkId || `sdk-${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
100
+ this.messageSource = config.messageSource;
101
+ this.level = config.level ?? 0;
102
+ this.methodLevels = config.methodLevels ?? {};
103
+ this.implementedMethods = config.implementedMethods;
104
+ this.implementedMethods.forEach((method) => {
105
+ this.higherLevelSDKs[method] = /* @__PURE__ */ new Set();
106
+ this.sameLevelSDKs[method] = /* @__PURE__ */ new Set();
107
+ });
108
+ }
109
+ /**
110
+ * 初始化协调器
111
+ */
112
+ init() {
113
+ window.addEventListener("message", this.handleCoordinationMessage);
114
+ this.sendQuery();
115
+ this.sendRegister();
116
+ }
117
+ /**
118
+ * 销毁协调器
119
+ */
120
+ destroy() {
121
+ this.isDestroyed = true;
122
+ this.sendUnregister();
123
+ window.removeEventListener("message", this.handleCoordinationMessage);
124
+ Object.values(this.higherLevelSDKs).forEach((set) => set.clear());
125
+ Object.values(this.sameLevelSDKs).forEach((set) => set.clear());
126
+ }
127
+ /**
128
+ * 判断是否应该响应某个方法的请求
129
+ * @param method - 方法名
130
+ * @returns 是否应该响应
131
+ */
132
+ shouldRespond(method) {
133
+ if (this.isDestroyed) return false;
134
+ if (!this.implementedMethods.includes(method)) {
135
+ return false;
136
+ }
137
+ const higherSDKs = this.higherLevelSDKs[method];
138
+ return !higherSDKs || higherSDKs.size === 0;
139
+ }
140
+ /**
141
+ * 判断某个方法是否有相同优先级的竞争者
142
+ * @param method - 方法名
143
+ * @returns 是否存在同级竞争者
144
+ */
145
+ hasSameLevelCompetitors(method) {
146
+ const sameSDKs = this.sameLevelSDKs[method];
147
+ return sameSDKs ? sameSDKs.size > 0 : false;
148
+ }
149
+ /**
150
+ * 获取方法的优先级
151
+ */
152
+ getMethodLevel(method) {
153
+ return this.methodLevels[method] ?? this.level;
154
+ }
155
+ /**
156
+ * 处理其他 SDK 的注册
157
+ */
158
+ handleSdkRegister(info) {
159
+ if (info.sdkId === this.sdkId) return;
160
+ if (info.messageSource !== this.messageSource) return;
161
+ this.implementedMethods.forEach((method) => {
162
+ if (!info.implementedMethods.includes(method)) {
163
+ return;
164
+ }
165
+ const myLevel = this.getMethodLevel(method);
166
+ const otherLevel = info.methodLevels[method] ?? info.level;
167
+ if (otherLevel > myLevel) {
168
+ this.higherLevelSDKs[method].add(info.sdkId);
169
+ this.sameLevelSDKs[method].delete(info.sdkId);
170
+ } else if (otherLevel === myLevel) {
171
+ this.sameLevelSDKs[method].add(info.sdkId);
172
+ this.higherLevelSDKs[method].delete(info.sdkId);
173
+ } else {
174
+ this.higherLevelSDKs[method].delete(info.sdkId);
175
+ this.sameLevelSDKs[method].delete(info.sdkId);
176
+ }
177
+ });
178
+ }
179
+ /**
180
+ * 处理其他 SDK 的注销
181
+ */
182
+ handleSdkUnregister(sdkId) {
183
+ Object.values(this.higherLevelSDKs).forEach((set) => {
184
+ set.delete(sdkId);
185
+ });
186
+ Object.values(this.sameLevelSDKs).forEach((set) => {
187
+ set.delete(sdkId);
188
+ });
189
+ }
190
+ /**
191
+ * 发送查询消息
192
+ */
193
+ sendQuery() {
194
+ const message = {
195
+ source: SDK_COORDINATOR_SOURCE,
196
+ type: SDK_COORDINATION_MESSAGE_TYPES.query,
197
+ payload: { sdkId: this.sdkId }
198
+ };
199
+ this.postCoordinationMessage(message);
200
+ }
201
+ /**
202
+ * 发送注册消息
203
+ */
204
+ sendRegister() {
205
+ const message = {
206
+ source: SDK_COORDINATOR_SOURCE,
207
+ type: SDK_COORDINATION_MESSAGE_TYPES.register,
208
+ payload: {
209
+ sdkId: this.sdkId,
210
+ messageSource: this.messageSource,
211
+ level: this.level,
212
+ methodLevels: this.methodLevels,
213
+ implementedMethods: this.implementedMethods
214
+ }
215
+ };
216
+ this.postCoordinationMessage(message);
217
+ }
218
+ /**
219
+ * 发送注销消息
220
+ */
221
+ sendUnregister() {
222
+ const message = {
223
+ source: SDK_COORDINATOR_SOURCE,
224
+ type: SDK_COORDINATION_MESSAGE_TYPES.unregister,
225
+ payload: { sdkId: this.sdkId }
226
+ };
227
+ this.postCoordinationMessage(message);
228
+ }
229
+ /**
230
+ * 发送协调消息
231
+ */
232
+ postCoordinationMessage(message) {
233
+ window.postMessage(message, "*");
234
+ if (window.top && window.top !== window) {
235
+ window.top.postMessage(message, "*");
236
+ }
237
+ }
238
+ };
239
+
240
+ // src/bridge.ts
241
+ var METHOD_NAMES = {
242
+ GET_SCHEMA: "getSchema",
243
+ UPDATE_SCHEMA: "updateSchema",
244
+ CHECK_PREVIEW: "checkPreview",
245
+ RENDER_PREVIEW: "renderPreview",
246
+ CLEANUP_PREVIEW: "cleanupPreview",
247
+ START_RECORDING: "startRecording",
248
+ STOP_RECORDING: "stopRecording"
249
+ };
250
+ function shouldSkipFailedResponse(method, result) {
251
+ switch (method) {
252
+ case METHOD_NAMES.GET_SCHEMA:
253
+ return result.success === true && result.data === void 0;
254
+ case METHOD_NAMES.UPDATE_SCHEMA:
255
+ return result.success === false;
256
+ case METHOD_NAMES.RENDER_PREVIEW:
257
+ case METHOD_NAMES.CLEANUP_PREVIEW:
258
+ return result.success === false;
259
+ case METHOD_NAMES.CHECK_PREVIEW:
260
+ case METHOD_NAMES.START_RECORDING:
261
+ case METHOD_NAMES.STOP_RECORDING:
262
+ return false;
263
+ default:
264
+ return false;
265
+ }
266
+ }
44
267
  function createSchemaElementEditorBridge(config) {
45
- const { getSchema, updateSchema, renderPreview, sourceConfig, messageTypes } = config;
268
+ const {
269
+ getSchema,
270
+ updateSchema,
271
+ renderPreview,
272
+ sourceConfig,
273
+ messageTypes,
274
+ sdkId,
275
+ level,
276
+ methodLevels
277
+ } = config;
46
278
  const mergedSourceConfig = {
47
279
  ...DEFAULT_SOURCE_CONFIG,
48
280
  ...sourceConfig
@@ -51,9 +283,40 @@ function createSchemaElementEditorBridge(config) {
51
283
  ...DEFAULT_MESSAGE_TYPES,
52
284
  ...messageTypes
53
285
  };
286
+ const implementedMethods = [];
287
+ if (typeof getSchema === "function") implementedMethods.push(METHOD_NAMES.GET_SCHEMA);
288
+ if (typeof updateSchema === "function") implementedMethods.push(METHOD_NAMES.UPDATE_SCHEMA);
289
+ if (typeof renderPreview === "function") {
290
+ implementedMethods.push(
291
+ METHOD_NAMES.CHECK_PREVIEW,
292
+ METHOD_NAMES.RENDER_PREVIEW,
293
+ METHOD_NAMES.CLEANUP_PREVIEW
294
+ );
295
+ }
296
+ implementedMethods.push(METHOD_NAMES.START_RECORDING, METHOD_NAMES.STOP_RECORDING);
297
+ const coordinator = new SdkCoordinator({
298
+ sdkId,
299
+ messageSource: mergedSourceConfig.contentSource,
300
+ level,
301
+ methodLevels,
302
+ implementedMethods
303
+ });
304
+ coordinator.init();
54
305
  let previewCleanupFn = null;
55
306
  const recordingParams = /* @__PURE__ */ new Set();
56
307
  const currentConfig = { getSchema, updateSchema, renderPreview };
308
+ const methodTypeToName = {
309
+ [mergedMessageTypes.getSchema]: METHOD_NAMES.GET_SCHEMA,
310
+ [mergedMessageTypes.updateSchema]: METHOD_NAMES.UPDATE_SCHEMA,
311
+ [mergedMessageTypes.checkPreview]: METHOD_NAMES.CHECK_PREVIEW,
312
+ [mergedMessageTypes.renderPreview]: METHOD_NAMES.RENDER_PREVIEW,
313
+ [mergedMessageTypes.cleanupPreview]: METHOD_NAMES.CLEANUP_PREVIEW,
314
+ [mergedMessageTypes.startRecording]: METHOD_NAMES.START_RECORDING,
315
+ [mergedMessageTypes.stopRecording]: METHOD_NAMES.STOP_RECORDING
316
+ };
317
+ const getMethodNameByType = (type) => {
318
+ return methodTypeToName[type] ?? null;
319
+ };
57
320
  const sendResponse = (requestId, result) => {
58
321
  const message = {
59
322
  source: mergedSourceConfig.hostSource,
@@ -86,10 +349,18 @@ function createSchemaElementEditorBridge(config) {
86
349
  if (!event.data || event.data.source !== mergedSourceConfig.contentSource) return;
87
350
  const { type, payload, requestId } = event.data;
88
351
  if (!requestId) return;
352
+ const methodName = getMethodNameByType(type);
353
+ if (!methodName) return;
354
+ if (!coordinator.shouldRespond(methodName)) {
355
+ return;
356
+ }
89
357
  const { getSchema: getSchema2, updateSchema: updateSchema2, renderPreview: renderPreview2 } = currentConfig;
90
358
  let result;
91
359
  switch (type) {
92
360
  case mergedMessageTypes.getSchema: {
361
+ if (typeof getSchema2 !== "function") {
362
+ return;
363
+ }
93
364
  const params = String(payload?.params ?? "");
94
365
  try {
95
366
  const data = getSchema2(params);
@@ -103,6 +374,9 @@ function createSchemaElementEditorBridge(config) {
103
374
  break;
104
375
  }
105
376
  case mergedMessageTypes.updateSchema: {
377
+ if (typeof updateSchema2 !== "function") {
378
+ return;
379
+ }
106
380
  const schema = payload?.schema;
107
381
  const params = String(payload?.params ?? "");
108
382
  try {
@@ -175,11 +449,16 @@ function createSchemaElementEditorBridge(config) {
175
449
  default:
176
450
  return;
177
451
  }
452
+ const hasSameLevelSdks = coordinator.hasSameLevelCompetitors(methodName);
453
+ if (hasSameLevelSdks && shouldSkipFailedResponse(methodName, result)) {
454
+ return;
455
+ }
178
456
  sendResponse(requestId, result);
179
457
  };
180
458
  window.addEventListener("message", handleMessage);
181
459
  return {
182
460
  cleanup: () => {
461
+ coordinator.destroy();
183
462
  window.removeEventListener("message", handleMessage);
184
463
  if (previewCleanupFn) {
185
464
  previewCleanupFn();
@@ -195,7 +474,17 @@ function createSchemaElementEditorBridge(config) {
195
474
 
196
475
  // src/vue.ts
197
476
  function useSchemaElementEditor(config) {
198
- const { getSchema, updateSchema, renderPreview, sourceConfig, messageTypes, enabled } = config;
477
+ const {
478
+ getSchema,
479
+ updateSchema,
480
+ renderPreview,
481
+ sourceConfig,
482
+ messageTypes,
483
+ enabled,
484
+ sdkId,
485
+ level,
486
+ methodLevels
487
+ } = config;
199
488
  let bridge = null;
200
489
  const destroyBridge = () => {
201
490
  if (bridge) {
@@ -213,7 +502,10 @@ function useSchemaElementEditor(config) {
213
502
  updateSchema: (schema, params) => (0, import_vue.toValue)(updateSchema)(schema, params),
214
503
  renderPreview: (0, import_vue.toValue)(renderPreview) ? (schema, containerId) => (0, import_vue.toValue)(renderPreview)?.(schema, containerId) : void 0,
215
504
  sourceConfig,
216
- messageTypes
505
+ messageTypes,
506
+ sdkId,
507
+ level,
508
+ methodLevels
217
509
  };
218
510
  bridge = createSchemaElementEditorBridge(proxyConfig);
219
511
  };
@@ -235,7 +527,10 @@ function useSchemaElementEditor(config) {
235
527
  messageTypes?.cleanupPreview,
236
528
  messageTypes?.startRecording,
237
529
  messageTypes?.stopRecording,
238
- messageTypes?.schemaPush
530
+ messageTypes?.schemaPush,
531
+ sdkId,
532
+ level,
533
+ methodLevels
239
534
  ],
240
535
  () => {
241
536
  createBridgeInstance();
package/dist/vue.d.cts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { MaybeRefOrGetter } from 'vue';
2
- import { SchemaValue, PostMessageSourceConfig, PostMessageTypeConfig, SchemaElementEditorRecording } from './core.cjs';
3
- export { SchemaElementEditorBridge, SchemaElementEditorConfig } from './core.cjs';
2
+ import { S as SchemaValue, P as PostMessageSourceConfig, a as PostMessageTypeConfig, M as MethodLevelConfig, d as SchemaElementEditorRecording } from './types-D2ZJx8T_.cjs';
4
3
 
5
4
  /**
6
5
  * Schema Element Editor Host SDK - Vue
@@ -34,6 +33,21 @@ interface VueSchemaElementEditorConfig {
34
33
  sourceConfig?: Partial<PostMessageSourceConfig>;
35
34
  /** 消息类型配置(可选,有默认值) */
36
35
  messageTypes?: Partial<PostMessageTypeConfig>;
36
+ /**
37
+ * SDK 实例唯一标识(可选,自动生成)
38
+ * 用于多 SDK 实例协调
39
+ */
40
+ sdkId?: string;
41
+ /**
42
+ * SDK 优先级(可选,默认 0)
43
+ * 数值越大优先级越高,当多个 SDK 实例共存时,优先级高的响应请求
44
+ */
45
+ level?: number;
46
+ /**
47
+ * 方法级别优先级配置(可选)
48
+ * 可以为每个方法单独配置优先级,未配置的方法使用 level 作为优先级
49
+ */
50
+ methodLevels?: MethodLevelConfig;
37
51
  }
38
52
  /** Vue composable 返回值 */
39
53
  interface UseSchemaElementEditorReturn {
@@ -70,4 +84,4 @@ interface UseSchemaElementEditorReturn {
70
84
  */
71
85
  declare function useSchemaElementEditor(config: VueSchemaElementEditorConfig): UseSchemaElementEditorReturn;
72
86
 
73
- export { PostMessageSourceConfig, PostMessageTypeConfig, SchemaElementEditorRecording, SchemaValue, type UseSchemaElementEditorReturn, type VueSchemaElementEditorConfig, useSchemaElementEditor };
87
+ export { type UseSchemaElementEditorReturn, type VueSchemaElementEditorConfig, useSchemaElementEditor };
package/dist/vue.d.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import { MaybeRefOrGetter } from 'vue';
2
- import { SchemaValue, PostMessageSourceConfig, PostMessageTypeConfig, SchemaElementEditorRecording } from './core.js';
3
- export { SchemaElementEditorBridge, SchemaElementEditorConfig } from './core.js';
2
+ import { S as SchemaValue, P as PostMessageSourceConfig, a as PostMessageTypeConfig, M as MethodLevelConfig, d as SchemaElementEditorRecording } from './types-D2ZJx8T_.js';
4
3
 
5
4
  /**
6
5
  * Schema Element Editor Host SDK - Vue
@@ -34,6 +33,21 @@ interface VueSchemaElementEditorConfig {
34
33
  sourceConfig?: Partial<PostMessageSourceConfig>;
35
34
  /** 消息类型配置(可选,有默认值) */
36
35
  messageTypes?: Partial<PostMessageTypeConfig>;
36
+ /**
37
+ * SDK 实例唯一标识(可选,自动生成)
38
+ * 用于多 SDK 实例协调
39
+ */
40
+ sdkId?: string;
41
+ /**
42
+ * SDK 优先级(可选,默认 0)
43
+ * 数值越大优先级越高,当多个 SDK 实例共存时,优先级高的响应请求
44
+ */
45
+ level?: number;
46
+ /**
47
+ * 方法级别优先级配置(可选)
48
+ * 可以为每个方法单独配置优先级,未配置的方法使用 level 作为优先级
49
+ */
50
+ methodLevels?: MethodLevelConfig;
37
51
  }
38
52
  /** Vue composable 返回值 */
39
53
  interface UseSchemaElementEditorReturn {
@@ -70,4 +84,4 @@ interface UseSchemaElementEditorReturn {
70
84
  */
71
85
  declare function useSchemaElementEditor(config: VueSchemaElementEditorConfig): UseSchemaElementEditorReturn;
72
86
 
73
- export { PostMessageSourceConfig, PostMessageTypeConfig, SchemaElementEditorRecording, SchemaValue, type UseSchemaElementEditorReturn, type VueSchemaElementEditorConfig, useSchemaElementEditor };
87
+ export { type UseSchemaElementEditorReturn, type VueSchemaElementEditorConfig, useSchemaElementEditor };
package/dist/vue.js CHANGED
@@ -1,11 +1,21 @@
1
1
  import {
2
2
  createSchemaElementEditorBridge
3
- } from "./chunk-52EFKQHQ.js";
3
+ } from "./chunk-LJJYJHOZ.js";
4
4
 
5
5
  // src/vue.ts
6
6
  import { onMounted, onUnmounted, watch, toValue } from "vue";
7
7
  function useSchemaElementEditor(config) {
8
- const { getSchema, updateSchema, renderPreview, sourceConfig, messageTypes, enabled } = config;
8
+ const {
9
+ getSchema,
10
+ updateSchema,
11
+ renderPreview,
12
+ sourceConfig,
13
+ messageTypes,
14
+ enabled,
15
+ sdkId,
16
+ level,
17
+ methodLevels
18
+ } = config;
9
19
  let bridge = null;
10
20
  const destroyBridge = () => {
11
21
  if (bridge) {
@@ -23,7 +33,10 @@ function useSchemaElementEditor(config) {
23
33
  updateSchema: (schema, params) => toValue(updateSchema)(schema, params),
24
34
  renderPreview: toValue(renderPreview) ? (schema, containerId) => toValue(renderPreview)?.(schema, containerId) : void 0,
25
35
  sourceConfig,
26
- messageTypes
36
+ messageTypes,
37
+ sdkId,
38
+ level,
39
+ methodLevels
27
40
  };
28
41
  bridge = createSchemaElementEditorBridge(proxyConfig);
29
42
  };
@@ -45,7 +58,10 @@ function useSchemaElementEditor(config) {
45
58
  messageTypes?.cleanupPreview,
46
59
  messageTypes?.startRecording,
47
60
  messageTypes?.stopRecording,
48
- messageTypes?.schemaPush
61
+ messageTypes?.schemaPush,
62
+ sdkId,
63
+ level,
64
+ methodLevels
49
65
  ],
50
66
  () => {
51
67
  createBridgeInstance();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@schema-element-editor/host-sdk",
3
- "version": "2.0.3",
3
+ "version": "2.1.0",
4
4
  "description": "Schema Element Editor (SEE) 插件宿主接入 SDK,支持 React/Vue/纯 JS",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -31,10 +31,6 @@
31
31
  "files": [
32
32
  "dist"
33
33
  ],
34
- "scripts": {
35
- "build": "tsup src/index.ts src/core.ts src/react.ts src/vue.ts --format esm,cjs --dts --out-dir dist --external react --external vue",
36
- "prepublishOnly": "npm run build"
37
- },
38
34
  "peerDependencies": {
39
35
  "react": ">=17.0.0",
40
36
  "vue": ">=3.0.0"
@@ -65,5 +61,8 @@
65
61
  "type": "git",
66
62
  "url": "https://github.com/hei-f/schema-element-editor.git",
67
63
  "directory": "packages/schema-element-editor-sdk"
64
+ },
65
+ "scripts": {
66
+ "build": "tsup src/index.ts src/core.ts src/react.ts src/vue.ts --format esm,cjs --dts --out-dir dist --external react --external vue"
68
67
  }
69
- }
68
+ }