@schema-element-editor/host-sdk 2.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/chunk-2A2IOK5A.js +41 -0
- package/dist/chunk-5Q4T7TXN.js +173 -0
- package/dist/chunk-DEMUKVTU.js +175 -0
- package/dist/chunk-FG4Q5PL6.js +175 -0
- package/dist/chunk-KMNKOOWF.js +173 -0
- package/dist/chunk-MU6HL5E3.js +41 -0
- package/dist/chunk-OBBMC57I.js +41 -0
- package/dist/chunk-UF5J55D3.js +41 -0
- package/dist/core.cjs +197 -0
- package/dist/core.d.cts +107 -0
- package/dist/core.d.ts +107 -0
- package/dist/core.js +6 -0
- package/dist/index.cjs +237 -0
- package/dist/index.d.cts +2 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +10 -0
- package/dist/react.cjs +233 -0
- package/dist/react.d.cts +54 -0
- package/dist/react.d.ts +54 -0
- package/dist/react.js +7 -0
- package/dist/vue.cjs +257 -0
- package/dist/vue.d.cts +73 -0
- package/dist/vue.d.ts +73 -0
- package/dist/vue.js +64 -0
- package/package.json +69 -0
package/dist/core.d.ts
ADDED
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Schema Element Editor Host SDK - Core
|
|
3
|
+
* 纯 JS 核心逻辑,无框架依赖
|
|
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
|
+
/**
|
|
29
|
+
* Schema Element Editor 配置接口
|
|
30
|
+
*/
|
|
31
|
+
interface SchemaElementEditorConfig {
|
|
32
|
+
/**
|
|
33
|
+
* 获取 Schema 数据
|
|
34
|
+
* @param params - 元素参数(通常是 data-id 的值)
|
|
35
|
+
* @returns Schema 数据(支持所有 JSON 类型)
|
|
36
|
+
*/
|
|
37
|
+
getSchema: (params: string) => SchemaValue;
|
|
38
|
+
/**
|
|
39
|
+
* 更新 Schema 数据
|
|
40
|
+
* @param schema - 新的 Schema 数据(支持所有 JSON 类型)
|
|
41
|
+
* @param params - 元素参数
|
|
42
|
+
* @returns 是否更新成功
|
|
43
|
+
*/
|
|
44
|
+
updateSchema: (schema: SchemaValue, params: string) => boolean;
|
|
45
|
+
/**
|
|
46
|
+
* 渲染预览(可选)
|
|
47
|
+
* @param schema - Schema 数据(支持所有 JSON 类型)
|
|
48
|
+
* @param containerId - 预览容器 ID
|
|
49
|
+
* @returns 清理函数(可选)
|
|
50
|
+
*/
|
|
51
|
+
renderPreview?: (schema: SchemaValue, containerId: string) => (() => void) | void;
|
|
52
|
+
/** 消息标识配置(可选,有默认值) */
|
|
53
|
+
sourceConfig?: Partial<PostMessageSourceConfig>;
|
|
54
|
+
/** 消息类型配置(可选,有默认值) */
|
|
55
|
+
messageTypes?: Partial<PostMessageTypeConfig>;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* 录制相关方法
|
|
59
|
+
*/
|
|
60
|
+
interface SchemaElementEditorRecording {
|
|
61
|
+
/**
|
|
62
|
+
* 推送 Schema 数据(SDK 内部判断是否在录制,未录制时静默忽略)
|
|
63
|
+
* @param params - 元素参数(data-id 的值)
|
|
64
|
+
* @param data - Schema 数据
|
|
65
|
+
*/
|
|
66
|
+
push: (params: string, data: SchemaValue) => void;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Schema Element Editor 桥接器返回值
|
|
70
|
+
*/
|
|
71
|
+
interface SchemaElementEditorBridge {
|
|
72
|
+
/** 清理桥接器,移除事件监听 */
|
|
73
|
+
cleanup: () => void;
|
|
74
|
+
/** 录制相关方法 */
|
|
75
|
+
recording: SchemaElementEditorRecording;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* 创建 Schema Element Editor 桥接器
|
|
79
|
+
* 纯 JS 函数,返回桥接器对象
|
|
80
|
+
*
|
|
81
|
+
* @param config - Schema Element Editor 配置
|
|
82
|
+
* @returns 桥接器对象,包含 cleanup 和 pushSchema 方法
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```js
|
|
86
|
+
* // 最简用法:只需配置基本的 getSchema 和 updateSchema
|
|
87
|
+
* const bridge = createSchemaElementEditorBridge({
|
|
88
|
+
* getSchema: (params) => dataStore[params],
|
|
89
|
+
* updateSchema: (schema, params) => {
|
|
90
|
+
* dataStore[params] = schema
|
|
91
|
+
* return true
|
|
92
|
+
* },
|
|
93
|
+
* })
|
|
94
|
+
*
|
|
95
|
+
* // 数据变化时调用 pushSchema 推送数据(录制功能自动可用)
|
|
96
|
+
* sseHandler.onData = (params, data) => {
|
|
97
|
+
* bridge.pushSchema(params, data)
|
|
98
|
+
* }
|
|
99
|
+
*
|
|
100
|
+
* // 如需在录制开始/停止时执行额外逻辑,可配置回调(可选)
|
|
101
|
+
* // onStartRecording: (params) => console.log('开始录制:', params),
|
|
102
|
+
* // onStopRecording: (params) => console.log('停止录制:', params),
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
declare function createSchemaElementEditorBridge(config: SchemaElementEditorConfig): SchemaElementEditorBridge;
|
|
106
|
+
|
|
107
|
+
export { type PostMessageSourceConfig, type PostMessageTypeConfig, type SchemaElementEditorBridge, type SchemaElementEditorConfig, type SchemaElementEditorRecording, type SchemaValue, createSchemaElementEditorBridge };
|
package/dist/core.js
ADDED
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
createSchemaElementEditorBridge: () => createSchemaElementEditorBridge,
|
|
24
|
+
useSchemaElementEditor: () => useSchemaElementEditor
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(index_exports);
|
|
27
|
+
|
|
28
|
+
// src/react.ts
|
|
29
|
+
var import_react = require("react");
|
|
30
|
+
|
|
31
|
+
// src/core.ts
|
|
32
|
+
var DEFAULT_SOURCE_CONFIG = {
|
|
33
|
+
contentSource: "schema-element-editor-content",
|
|
34
|
+
hostSource: "schema-element-editor-host"
|
|
35
|
+
};
|
|
36
|
+
var DEFAULT_MESSAGE_TYPES = {
|
|
37
|
+
getSchema: "GET_SCHEMA",
|
|
38
|
+
updateSchema: "UPDATE_SCHEMA",
|
|
39
|
+
checkPreview: "CHECK_PREVIEW",
|
|
40
|
+
renderPreview: "RENDER_PREVIEW",
|
|
41
|
+
cleanupPreview: "CLEANUP_PREVIEW",
|
|
42
|
+
// 录制模式相关
|
|
43
|
+
startRecording: "START_RECORDING",
|
|
44
|
+
stopRecording: "STOP_RECORDING",
|
|
45
|
+
schemaPush: "SCHEMA_PUSH"
|
|
46
|
+
};
|
|
47
|
+
function createSchemaElementEditorBridge(config) {
|
|
48
|
+
const { getSchema, updateSchema, renderPreview, sourceConfig, messageTypes } = config;
|
|
49
|
+
const mergedSourceConfig = {
|
|
50
|
+
...DEFAULT_SOURCE_CONFIG,
|
|
51
|
+
...sourceConfig
|
|
52
|
+
};
|
|
53
|
+
const mergedMessageTypes = {
|
|
54
|
+
...DEFAULT_MESSAGE_TYPES,
|
|
55
|
+
...messageTypes
|
|
56
|
+
};
|
|
57
|
+
let previewCleanupFn = null;
|
|
58
|
+
const recordingParams = /* @__PURE__ */ new Set();
|
|
59
|
+
const currentConfig = { getSchema, updateSchema, renderPreview };
|
|
60
|
+
const sendResponse = (requestId, result) => {
|
|
61
|
+
const message = {
|
|
62
|
+
source: mergedSourceConfig.hostSource,
|
|
63
|
+
requestId,
|
|
64
|
+
...result
|
|
65
|
+
};
|
|
66
|
+
const isInIframe = window !== window.top;
|
|
67
|
+
const targetWindow = isInIframe ? window.parent : window;
|
|
68
|
+
targetWindow.postMessage(message, "*");
|
|
69
|
+
};
|
|
70
|
+
const pushSchema = (params, data) => {
|
|
71
|
+
if (!recordingParams.has(params)) {
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
const message = {
|
|
75
|
+
source: mergedSourceConfig.hostSource,
|
|
76
|
+
type: mergedMessageTypes.schemaPush,
|
|
77
|
+
payload: {
|
|
78
|
+
success: true,
|
|
79
|
+
data,
|
|
80
|
+
params
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
const isInIframe = window !== window.top;
|
|
84
|
+
const targetWindow = isInIframe ? window.parent : window;
|
|
85
|
+
targetWindow.postMessage(message, "*");
|
|
86
|
+
};
|
|
87
|
+
const handleMessage = (event) => {
|
|
88
|
+
const isFromSelf = event.source === window;
|
|
89
|
+
const isFromParent = window !== window.top && event.source === window.parent;
|
|
90
|
+
if (!isFromSelf && !isFromParent) return;
|
|
91
|
+
if (!event.data || event.data.source !== mergedSourceConfig.contentSource) return;
|
|
92
|
+
const { type, payload, requestId } = event.data;
|
|
93
|
+
if (!requestId) return;
|
|
94
|
+
const { getSchema: getSchema2, updateSchema: updateSchema2, renderPreview: renderPreview2 } = currentConfig;
|
|
95
|
+
let result;
|
|
96
|
+
switch (type) {
|
|
97
|
+
case mergedMessageTypes.getSchema: {
|
|
98
|
+
const params = String(payload?.params ?? "");
|
|
99
|
+
try {
|
|
100
|
+
const data = getSchema2(params);
|
|
101
|
+
result = { success: true, data };
|
|
102
|
+
} catch (error) {
|
|
103
|
+
result = {
|
|
104
|
+
success: false,
|
|
105
|
+
error: error instanceof Error ? error.message : "\u83B7\u53D6 Schema \u5931\u8D25"
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
break;
|
|
109
|
+
}
|
|
110
|
+
case mergedMessageTypes.updateSchema: {
|
|
111
|
+
const schema = payload?.schema;
|
|
112
|
+
const params = String(payload?.params ?? "");
|
|
113
|
+
try {
|
|
114
|
+
const success = updateSchema2(schema, params);
|
|
115
|
+
result = { success };
|
|
116
|
+
} catch (error) {
|
|
117
|
+
result = {
|
|
118
|
+
success: false,
|
|
119
|
+
error: error instanceof Error ? error.message : "\u66F4\u65B0 Schema \u5931\u8D25"
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
case mergedMessageTypes.checkPreview: {
|
|
125
|
+
result = { exists: typeof renderPreview2 === "function" };
|
|
126
|
+
break;
|
|
127
|
+
}
|
|
128
|
+
case mergedMessageTypes.renderPreview: {
|
|
129
|
+
if (typeof renderPreview2 !== "function") {
|
|
130
|
+
result = { success: false, error: "\u9884\u89C8\u529F\u80FD\u672A\u914D\u7F6E" };
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
const schema = payload?.schema;
|
|
134
|
+
const containerId = String(payload?.containerId ?? "");
|
|
135
|
+
try {
|
|
136
|
+
if (previewCleanupFn) {
|
|
137
|
+
previewCleanupFn();
|
|
138
|
+
previewCleanupFn = null;
|
|
139
|
+
}
|
|
140
|
+
const cleanup = renderPreview2(schema, containerId);
|
|
141
|
+
if (typeof cleanup === "function") {
|
|
142
|
+
previewCleanupFn = cleanup;
|
|
143
|
+
}
|
|
144
|
+
result = { success: true };
|
|
145
|
+
} catch (error) {
|
|
146
|
+
result = {
|
|
147
|
+
success: false,
|
|
148
|
+
error: error instanceof Error ? error.message : "\u6E32\u67D3\u9884\u89C8\u5931\u8D25"
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
break;
|
|
152
|
+
}
|
|
153
|
+
case mergedMessageTypes.cleanupPreview: {
|
|
154
|
+
try {
|
|
155
|
+
if (previewCleanupFn) {
|
|
156
|
+
previewCleanupFn();
|
|
157
|
+
previewCleanupFn = null;
|
|
158
|
+
}
|
|
159
|
+
result = { success: true };
|
|
160
|
+
} catch (error) {
|
|
161
|
+
result = {
|
|
162
|
+
success: false,
|
|
163
|
+
error: error instanceof Error ? error.message : "\u6E05\u7406\u9884\u89C8\u5931\u8D25"
|
|
164
|
+
};
|
|
165
|
+
}
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
case mergedMessageTypes.startRecording: {
|
|
169
|
+
const params = String(payload?.params ?? "");
|
|
170
|
+
recordingParams.add(params);
|
|
171
|
+
result = { success: true };
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
case mergedMessageTypes.stopRecording: {
|
|
175
|
+
const params = String(payload?.params ?? "");
|
|
176
|
+
recordingParams.delete(params);
|
|
177
|
+
result = { success: true };
|
|
178
|
+
break;
|
|
179
|
+
}
|
|
180
|
+
default:
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
sendResponse(requestId, result);
|
|
184
|
+
};
|
|
185
|
+
window.addEventListener("message", handleMessage);
|
|
186
|
+
return {
|
|
187
|
+
cleanup: () => {
|
|
188
|
+
window.removeEventListener("message", handleMessage);
|
|
189
|
+
if (previewCleanupFn) {
|
|
190
|
+
previewCleanupFn();
|
|
191
|
+
previewCleanupFn = null;
|
|
192
|
+
}
|
|
193
|
+
recordingParams.clear();
|
|
194
|
+
},
|
|
195
|
+
recording: {
|
|
196
|
+
push: pushSchema
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// src/react.ts
|
|
202
|
+
function useSchemaElementEditor(config) {
|
|
203
|
+
const { getSchema, updateSchema, renderPreview, sourceConfig, messageTypes, enabled } = config;
|
|
204
|
+
const configRef = (0, import_react.useRef)({ getSchema, updateSchema, renderPreview });
|
|
205
|
+
const bridgeRef = (0, import_react.useRef)(null);
|
|
206
|
+
(0, import_react.useEffect)(() => {
|
|
207
|
+
configRef.current = { getSchema, updateSchema, renderPreview };
|
|
208
|
+
}, [getSchema, updateSchema, renderPreview]);
|
|
209
|
+
(0, import_react.useEffect)(() => {
|
|
210
|
+
if (enabled === false) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const proxyConfig = {
|
|
214
|
+
getSchema: (params) => configRef.current.getSchema(params),
|
|
215
|
+
updateSchema: (schema, params) => configRef.current.updateSchema(schema, params),
|
|
216
|
+
renderPreview: configRef.current.renderPreview ? (schema, containerId) => configRef.current.renderPreview?.(schema, containerId) : void 0,
|
|
217
|
+
sourceConfig,
|
|
218
|
+
messageTypes
|
|
219
|
+
};
|
|
220
|
+
const bridge = createSchemaElementEditorBridge(proxyConfig);
|
|
221
|
+
bridgeRef.current = bridge;
|
|
222
|
+
return () => {
|
|
223
|
+
bridge.cleanup();
|
|
224
|
+
bridgeRef.current = null;
|
|
225
|
+
};
|
|
226
|
+
}, [enabled, configRef, sourceConfig, messageTypes]);
|
|
227
|
+
const push = (0, import_react.useCallback)((params, data) => {
|
|
228
|
+
bridgeRef.current?.recording.push(params, data);
|
|
229
|
+
}, []);
|
|
230
|
+
const recording = (0, import_react.useMemo)(() => ({ push }), [push]);
|
|
231
|
+
return { recording };
|
|
232
|
+
}
|
|
233
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
234
|
+
0 && (module.exports = {
|
|
235
|
+
createSchemaElementEditorBridge,
|
|
236
|
+
useSchemaElementEditor
|
|
237
|
+
});
|
package/dist/index.d.cts
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/react.cjs
ADDED
|
@@ -0,0 +1,233 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/react.ts
|
|
21
|
+
var react_exports = {};
|
|
22
|
+
__export(react_exports, {
|
|
23
|
+
useSchemaElementEditor: () => useSchemaElementEditor
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(react_exports);
|
|
26
|
+
var import_react = require("react");
|
|
27
|
+
|
|
28
|
+
// src/core.ts
|
|
29
|
+
var DEFAULT_SOURCE_CONFIG = {
|
|
30
|
+
contentSource: "schema-element-editor-content",
|
|
31
|
+
hostSource: "schema-element-editor-host"
|
|
32
|
+
};
|
|
33
|
+
var DEFAULT_MESSAGE_TYPES = {
|
|
34
|
+
getSchema: "GET_SCHEMA",
|
|
35
|
+
updateSchema: "UPDATE_SCHEMA",
|
|
36
|
+
checkPreview: "CHECK_PREVIEW",
|
|
37
|
+
renderPreview: "RENDER_PREVIEW",
|
|
38
|
+
cleanupPreview: "CLEANUP_PREVIEW",
|
|
39
|
+
// 录制模式相关
|
|
40
|
+
startRecording: "START_RECORDING",
|
|
41
|
+
stopRecording: "STOP_RECORDING",
|
|
42
|
+
schemaPush: "SCHEMA_PUSH"
|
|
43
|
+
};
|
|
44
|
+
function createSchemaElementEditorBridge(config) {
|
|
45
|
+
const { getSchema, updateSchema, renderPreview, sourceConfig, messageTypes } = config;
|
|
46
|
+
const mergedSourceConfig = {
|
|
47
|
+
...DEFAULT_SOURCE_CONFIG,
|
|
48
|
+
...sourceConfig
|
|
49
|
+
};
|
|
50
|
+
const mergedMessageTypes = {
|
|
51
|
+
...DEFAULT_MESSAGE_TYPES,
|
|
52
|
+
...messageTypes
|
|
53
|
+
};
|
|
54
|
+
let previewCleanupFn = null;
|
|
55
|
+
const recordingParams = /* @__PURE__ */ new Set();
|
|
56
|
+
const currentConfig = { getSchema, updateSchema, renderPreview };
|
|
57
|
+
const sendResponse = (requestId, result) => {
|
|
58
|
+
const message = {
|
|
59
|
+
source: mergedSourceConfig.hostSource,
|
|
60
|
+
requestId,
|
|
61
|
+
...result
|
|
62
|
+
};
|
|
63
|
+
const isInIframe = window !== window.top;
|
|
64
|
+
const targetWindow = isInIframe ? window.parent : window;
|
|
65
|
+
targetWindow.postMessage(message, "*");
|
|
66
|
+
};
|
|
67
|
+
const pushSchema = (params, data) => {
|
|
68
|
+
if (!recordingParams.has(params)) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const message = {
|
|
72
|
+
source: mergedSourceConfig.hostSource,
|
|
73
|
+
type: mergedMessageTypes.schemaPush,
|
|
74
|
+
payload: {
|
|
75
|
+
success: true,
|
|
76
|
+
data,
|
|
77
|
+
params
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
const isInIframe = window !== window.top;
|
|
81
|
+
const targetWindow = isInIframe ? window.parent : window;
|
|
82
|
+
targetWindow.postMessage(message, "*");
|
|
83
|
+
};
|
|
84
|
+
const handleMessage = (event) => {
|
|
85
|
+
const isFromSelf = event.source === window;
|
|
86
|
+
const isFromParent = window !== window.top && event.source === window.parent;
|
|
87
|
+
if (!isFromSelf && !isFromParent) return;
|
|
88
|
+
if (!event.data || event.data.source !== mergedSourceConfig.contentSource) return;
|
|
89
|
+
const { type, payload, requestId } = event.data;
|
|
90
|
+
if (!requestId) return;
|
|
91
|
+
const { getSchema: getSchema2, updateSchema: updateSchema2, renderPreview: renderPreview2 } = currentConfig;
|
|
92
|
+
let result;
|
|
93
|
+
switch (type) {
|
|
94
|
+
case mergedMessageTypes.getSchema: {
|
|
95
|
+
const params = String(payload?.params ?? "");
|
|
96
|
+
try {
|
|
97
|
+
const data = getSchema2(params);
|
|
98
|
+
result = { success: true, data };
|
|
99
|
+
} catch (error) {
|
|
100
|
+
result = {
|
|
101
|
+
success: false,
|
|
102
|
+
error: error instanceof Error ? error.message : "\u83B7\u53D6 Schema \u5931\u8D25"
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
case mergedMessageTypes.updateSchema: {
|
|
108
|
+
const schema = payload?.schema;
|
|
109
|
+
const params = String(payload?.params ?? "");
|
|
110
|
+
try {
|
|
111
|
+
const success = updateSchema2(schema, params);
|
|
112
|
+
result = { success };
|
|
113
|
+
} catch (error) {
|
|
114
|
+
result = {
|
|
115
|
+
success: false,
|
|
116
|
+
error: error instanceof Error ? error.message : "\u66F4\u65B0 Schema \u5931\u8D25"
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
case mergedMessageTypes.checkPreview: {
|
|
122
|
+
result = { exists: typeof renderPreview2 === "function" };
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
case mergedMessageTypes.renderPreview: {
|
|
126
|
+
if (typeof renderPreview2 !== "function") {
|
|
127
|
+
result = { success: false, error: "\u9884\u89C8\u529F\u80FD\u672A\u914D\u7F6E" };
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
const schema = payload?.schema;
|
|
131
|
+
const containerId = String(payload?.containerId ?? "");
|
|
132
|
+
try {
|
|
133
|
+
if (previewCleanupFn) {
|
|
134
|
+
previewCleanupFn();
|
|
135
|
+
previewCleanupFn = null;
|
|
136
|
+
}
|
|
137
|
+
const cleanup = renderPreview2(schema, containerId);
|
|
138
|
+
if (typeof cleanup === "function") {
|
|
139
|
+
previewCleanupFn = cleanup;
|
|
140
|
+
}
|
|
141
|
+
result = { success: true };
|
|
142
|
+
} catch (error) {
|
|
143
|
+
result = {
|
|
144
|
+
success: false,
|
|
145
|
+
error: error instanceof Error ? error.message : "\u6E32\u67D3\u9884\u89C8\u5931\u8D25"
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
case mergedMessageTypes.cleanupPreview: {
|
|
151
|
+
try {
|
|
152
|
+
if (previewCleanupFn) {
|
|
153
|
+
previewCleanupFn();
|
|
154
|
+
previewCleanupFn = null;
|
|
155
|
+
}
|
|
156
|
+
result = { success: true };
|
|
157
|
+
} catch (error) {
|
|
158
|
+
result = {
|
|
159
|
+
success: false,
|
|
160
|
+
error: error instanceof Error ? error.message : "\u6E05\u7406\u9884\u89C8\u5931\u8D25"
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
break;
|
|
164
|
+
}
|
|
165
|
+
case mergedMessageTypes.startRecording: {
|
|
166
|
+
const params = String(payload?.params ?? "");
|
|
167
|
+
recordingParams.add(params);
|
|
168
|
+
result = { success: true };
|
|
169
|
+
break;
|
|
170
|
+
}
|
|
171
|
+
case mergedMessageTypes.stopRecording: {
|
|
172
|
+
const params = String(payload?.params ?? "");
|
|
173
|
+
recordingParams.delete(params);
|
|
174
|
+
result = { success: true };
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
default:
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
sendResponse(requestId, result);
|
|
181
|
+
};
|
|
182
|
+
window.addEventListener("message", handleMessage);
|
|
183
|
+
return {
|
|
184
|
+
cleanup: () => {
|
|
185
|
+
window.removeEventListener("message", handleMessage);
|
|
186
|
+
if (previewCleanupFn) {
|
|
187
|
+
previewCleanupFn();
|
|
188
|
+
previewCleanupFn = null;
|
|
189
|
+
}
|
|
190
|
+
recordingParams.clear();
|
|
191
|
+
},
|
|
192
|
+
recording: {
|
|
193
|
+
push: pushSchema
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// src/react.ts
|
|
199
|
+
function useSchemaElementEditor(config) {
|
|
200
|
+
const { getSchema, updateSchema, renderPreview, sourceConfig, messageTypes, enabled } = config;
|
|
201
|
+
const configRef = (0, import_react.useRef)({ getSchema, updateSchema, renderPreview });
|
|
202
|
+
const bridgeRef = (0, import_react.useRef)(null);
|
|
203
|
+
(0, import_react.useEffect)(() => {
|
|
204
|
+
configRef.current = { getSchema, updateSchema, renderPreview };
|
|
205
|
+
}, [getSchema, updateSchema, renderPreview]);
|
|
206
|
+
(0, import_react.useEffect)(() => {
|
|
207
|
+
if (enabled === false) {
|
|
208
|
+
return;
|
|
209
|
+
}
|
|
210
|
+
const proxyConfig = {
|
|
211
|
+
getSchema: (params) => configRef.current.getSchema(params),
|
|
212
|
+
updateSchema: (schema, params) => configRef.current.updateSchema(schema, params),
|
|
213
|
+
renderPreview: configRef.current.renderPreview ? (schema, containerId) => configRef.current.renderPreview?.(schema, containerId) : void 0,
|
|
214
|
+
sourceConfig,
|
|
215
|
+
messageTypes
|
|
216
|
+
};
|
|
217
|
+
const bridge = createSchemaElementEditorBridge(proxyConfig);
|
|
218
|
+
bridgeRef.current = bridge;
|
|
219
|
+
return () => {
|
|
220
|
+
bridge.cleanup();
|
|
221
|
+
bridgeRef.current = null;
|
|
222
|
+
};
|
|
223
|
+
}, [enabled, configRef, sourceConfig, messageTypes]);
|
|
224
|
+
const push = (0, import_react.useCallback)((params, data) => {
|
|
225
|
+
bridgeRef.current?.recording.push(params, data);
|
|
226
|
+
}, []);
|
|
227
|
+
const recording = (0, import_react.useMemo)(() => ({ push }), [push]);
|
|
228
|
+
return { recording };
|
|
229
|
+
}
|
|
230
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
231
|
+
0 && (module.exports = {
|
|
232
|
+
useSchemaElementEditor
|
|
233
|
+
});
|
package/dist/react.d.cts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { SchemaElementEditorConfig, SchemaElementEditorRecording } from './core.cjs';
|
|
2
|
+
export { PostMessageSourceConfig, PostMessageTypeConfig, SchemaElementEditorBridge, SchemaValue } from './core.cjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Schema Element Editor Host SDK - React
|
|
6
|
+
* React hooks 包装
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/** React 版本的 Schema Element Editor 配置 */
|
|
10
|
+
interface ReactSchemaElementEditorConfig extends SchemaElementEditorConfig {
|
|
11
|
+
/**
|
|
12
|
+
* 是否启用桥接(默认 true)
|
|
13
|
+
* 设为 false 时不创建桥接器,不监听消息
|
|
14
|
+
*/
|
|
15
|
+
enabled?: boolean;
|
|
16
|
+
}
|
|
17
|
+
/** React hooks 返回值 */
|
|
18
|
+
interface UseSchemaElementEditorReturn {
|
|
19
|
+
/** 录制相关方法 */
|
|
20
|
+
recording: SchemaElementEditorRecording;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Schema Element Editor 插件接入 hooks(React)
|
|
24
|
+
* 用于在宿主页面接入 Schema Element Editor 插件,通过 postMessage 接收插件请求并返回响应
|
|
25
|
+
*
|
|
26
|
+
* @param config - Schema Element Editor 配置
|
|
27
|
+
* @returns 桥接器方法,包含 recording
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```tsx
|
|
31
|
+
* import { useSchemaElementEditor } from '@schema-element-editor/host-sdk'
|
|
32
|
+
*
|
|
33
|
+
* function App() {
|
|
34
|
+
* const { recording } = useSchemaElementEditor({
|
|
35
|
+
* getSchema: (params) => dataStore[params],
|
|
36
|
+
* updateSchema: (schema, params) => {
|
|
37
|
+
* dataStore[params] = schema
|
|
38
|
+
* return true
|
|
39
|
+
* },
|
|
40
|
+
* })
|
|
41
|
+
*
|
|
42
|
+
* // 数据变化时推送数据(SDK 内部管理录制状态,未录制时静默忽略)
|
|
43
|
+
* sseHandler.onData = (params, data) => recording.push(params, data)
|
|
44
|
+
*
|
|
45
|
+
* // 检查是否正在录制
|
|
46
|
+
* if (recording.isActive('chat-1')) { ... }
|
|
47
|
+
*
|
|
48
|
+
* return <div>...</div>
|
|
49
|
+
* }
|
|
50
|
+
* ```
|
|
51
|
+
*/
|
|
52
|
+
declare function useSchemaElementEditor(config: ReactSchemaElementEditorConfig): UseSchemaElementEditorReturn;
|
|
53
|
+
|
|
54
|
+
export { type ReactSchemaElementEditorConfig, SchemaElementEditorConfig, SchemaElementEditorRecording, type UseSchemaElementEditorReturn, useSchemaElementEditor };
|