@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/react.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { SchemaElementEditorConfig, SchemaElementEditorRecording } from './core.js';
|
|
2
|
+
export { PostMessageSourceConfig, PostMessageTypeConfig, SchemaElementEditorBridge, SchemaValue } from './core.js';
|
|
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 };
|
package/dist/react.js
ADDED
package/dist/vue.cjs
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
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/vue.ts
|
|
21
|
+
var vue_exports = {};
|
|
22
|
+
__export(vue_exports, {
|
|
23
|
+
useSchemaElementEditor: () => useSchemaElementEditor
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(vue_exports);
|
|
26
|
+
var import_vue = require("vue");
|
|
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/vue.ts
|
|
199
|
+
function useSchemaElementEditor(config) {
|
|
200
|
+
const { getSchema, updateSchema, renderPreview, sourceConfig, messageTypes, enabled } = config;
|
|
201
|
+
let bridge = null;
|
|
202
|
+
const destroyBridge = () => {
|
|
203
|
+
if (bridge) {
|
|
204
|
+
bridge.cleanup();
|
|
205
|
+
bridge = null;
|
|
206
|
+
}
|
|
207
|
+
};
|
|
208
|
+
const createBridgeInstance = () => {
|
|
209
|
+
destroyBridge();
|
|
210
|
+
if ((0, import_vue.toValue)(enabled) === false) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
const proxyConfig = {
|
|
214
|
+
getSchema: (params) => (0, import_vue.toValue)(getSchema)(params),
|
|
215
|
+
updateSchema: (schema, params) => (0, import_vue.toValue)(updateSchema)(schema, params),
|
|
216
|
+
renderPreview: (0, import_vue.toValue)(renderPreview) ? (schema, containerId) => (0, import_vue.toValue)(renderPreview)?.(schema, containerId) : void 0,
|
|
217
|
+
sourceConfig,
|
|
218
|
+
messageTypes
|
|
219
|
+
};
|
|
220
|
+
bridge = createSchemaElementEditorBridge(proxyConfig);
|
|
221
|
+
};
|
|
222
|
+
(0, import_vue.onMounted)(() => {
|
|
223
|
+
createBridgeInstance();
|
|
224
|
+
});
|
|
225
|
+
(0, import_vue.onUnmounted)(() => {
|
|
226
|
+
destroyBridge();
|
|
227
|
+
});
|
|
228
|
+
(0, import_vue.watch)(
|
|
229
|
+
() => [
|
|
230
|
+
(0, import_vue.toValue)(enabled),
|
|
231
|
+
sourceConfig?.contentSource,
|
|
232
|
+
sourceConfig?.hostSource,
|
|
233
|
+
messageTypes?.getSchema,
|
|
234
|
+
messageTypes?.updateSchema,
|
|
235
|
+
messageTypes?.checkPreview,
|
|
236
|
+
messageTypes?.renderPreview,
|
|
237
|
+
messageTypes?.cleanupPreview,
|
|
238
|
+
messageTypes?.startRecording,
|
|
239
|
+
messageTypes?.stopRecording,
|
|
240
|
+
messageTypes?.schemaPush
|
|
241
|
+
],
|
|
242
|
+
() => {
|
|
243
|
+
createBridgeInstance();
|
|
244
|
+
},
|
|
245
|
+
{ deep: true }
|
|
246
|
+
);
|
|
247
|
+
const recording = {
|
|
248
|
+
push: (params, data) => {
|
|
249
|
+
bridge?.recording.push(params, data);
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
return { recording };
|
|
253
|
+
}
|
|
254
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
255
|
+
0 && (module.exports = {
|
|
256
|
+
useSchemaElementEditor
|
|
257
|
+
});
|
package/dist/vue.d.cts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { MaybeRefOrGetter } from 'vue';
|
|
2
|
+
import { SchemaValue, PostMessageSourceConfig, PostMessageTypeConfig, SchemaElementEditorRecording } from './core.cjs';
|
|
3
|
+
export { SchemaElementEditorBridge, SchemaElementEditorConfig } from './core.cjs';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Schema Element Editor Host SDK - Vue
|
|
7
|
+
* Vue 3 Composition API 包装
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Vue 版本的 Schema Element Editor 配置
|
|
12
|
+
* 支持响应式值(ref/reactive/getter)
|
|
13
|
+
*/
|
|
14
|
+
interface VueSchemaElementEditorConfig {
|
|
15
|
+
/**
|
|
16
|
+
* 获取 Schema 数据
|
|
17
|
+
*/
|
|
18
|
+
getSchema: MaybeRefOrGetter<(params: string) => SchemaValue>;
|
|
19
|
+
/**
|
|
20
|
+
* 更新 Schema 数据
|
|
21
|
+
*/
|
|
22
|
+
updateSchema: MaybeRefOrGetter<(schema: SchemaValue, params: string) => boolean>;
|
|
23
|
+
/**
|
|
24
|
+
* 渲染预览(可选)
|
|
25
|
+
*/
|
|
26
|
+
renderPreview?: MaybeRefOrGetter<((schema: SchemaValue, containerId: string) => (() => void) | void) | undefined>;
|
|
27
|
+
/**
|
|
28
|
+
* 是否启用桥接(默认 true)
|
|
29
|
+
* 设为 false 时不创建桥接器,不监听消息
|
|
30
|
+
* 支持响应式值
|
|
31
|
+
*/
|
|
32
|
+
enabled?: MaybeRefOrGetter<boolean>;
|
|
33
|
+
/** 消息标识配置(可选,有默认值) */
|
|
34
|
+
sourceConfig?: Partial<PostMessageSourceConfig>;
|
|
35
|
+
/** 消息类型配置(可选,有默认值) */
|
|
36
|
+
messageTypes?: Partial<PostMessageTypeConfig>;
|
|
37
|
+
}
|
|
38
|
+
/** Vue composable 返回值 */
|
|
39
|
+
interface UseSchemaElementEditorReturn {
|
|
40
|
+
/** 录制相关方法 */
|
|
41
|
+
recording: SchemaElementEditorRecording;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Schema Element Editor 插件接入 composable(Vue 3)
|
|
45
|
+
* 用于在宿主页面接入 Schema Element Editor 插件,通过 postMessage 接收插件请求并返回响应
|
|
46
|
+
*
|
|
47
|
+
* @param config - Schema Element Editor 配置(支持响应式值)
|
|
48
|
+
* @returns 桥接器方法,包含 pushSchema
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```vue
|
|
52
|
+
* <script setup>
|
|
53
|
+
* import { useSchemaElementEditor } from '@schema-element-editor/host-sdk/vue'
|
|
54
|
+
* import { ref } from 'vue'
|
|
55
|
+
*
|
|
56
|
+
* const dataStore = ref({})
|
|
57
|
+
*
|
|
58
|
+
* const { pushSchema } = useSchemaElementEditor({
|
|
59
|
+
* getSchema: (params) => dataStore.value[params],
|
|
60
|
+
* updateSchema: (schema, params) => {
|
|
61
|
+
* dataStore.value[params] = schema
|
|
62
|
+
* return true
|
|
63
|
+
* },
|
|
64
|
+
* })
|
|
65
|
+
*
|
|
66
|
+
* // 数据变化时调用 pushSchema 推送数据(录制功能自动可用)
|
|
67
|
+
* sseHandler.onData = (params, data) => pushSchema(params, data)
|
|
68
|
+
* </script>
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
declare function useSchemaElementEditor(config: VueSchemaElementEditorConfig): UseSchemaElementEditorReturn;
|
|
72
|
+
|
|
73
|
+
export { PostMessageSourceConfig, PostMessageTypeConfig, SchemaElementEditorRecording, SchemaValue, type UseSchemaElementEditorReturn, type VueSchemaElementEditorConfig, useSchemaElementEditor };
|
package/dist/vue.d.ts
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { MaybeRefOrGetter } from 'vue';
|
|
2
|
+
import { SchemaValue, PostMessageSourceConfig, PostMessageTypeConfig, SchemaElementEditorRecording } from './core.js';
|
|
3
|
+
export { SchemaElementEditorBridge, SchemaElementEditorConfig } from './core.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Schema Element Editor Host SDK - Vue
|
|
7
|
+
* Vue 3 Composition API 包装
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Vue 版本的 Schema Element Editor 配置
|
|
12
|
+
* 支持响应式值(ref/reactive/getter)
|
|
13
|
+
*/
|
|
14
|
+
interface VueSchemaElementEditorConfig {
|
|
15
|
+
/**
|
|
16
|
+
* 获取 Schema 数据
|
|
17
|
+
*/
|
|
18
|
+
getSchema: MaybeRefOrGetter<(params: string) => SchemaValue>;
|
|
19
|
+
/**
|
|
20
|
+
* 更新 Schema 数据
|
|
21
|
+
*/
|
|
22
|
+
updateSchema: MaybeRefOrGetter<(schema: SchemaValue, params: string) => boolean>;
|
|
23
|
+
/**
|
|
24
|
+
* 渲染预览(可选)
|
|
25
|
+
*/
|
|
26
|
+
renderPreview?: MaybeRefOrGetter<((schema: SchemaValue, containerId: string) => (() => void) | void) | undefined>;
|
|
27
|
+
/**
|
|
28
|
+
* 是否启用桥接(默认 true)
|
|
29
|
+
* 设为 false 时不创建桥接器,不监听消息
|
|
30
|
+
* 支持响应式值
|
|
31
|
+
*/
|
|
32
|
+
enabled?: MaybeRefOrGetter<boolean>;
|
|
33
|
+
/** 消息标识配置(可选,有默认值) */
|
|
34
|
+
sourceConfig?: Partial<PostMessageSourceConfig>;
|
|
35
|
+
/** 消息类型配置(可选,有默认值) */
|
|
36
|
+
messageTypes?: Partial<PostMessageTypeConfig>;
|
|
37
|
+
}
|
|
38
|
+
/** Vue composable 返回值 */
|
|
39
|
+
interface UseSchemaElementEditorReturn {
|
|
40
|
+
/** 录制相关方法 */
|
|
41
|
+
recording: SchemaElementEditorRecording;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Schema Element Editor 插件接入 composable(Vue 3)
|
|
45
|
+
* 用于在宿主页面接入 Schema Element Editor 插件,通过 postMessage 接收插件请求并返回响应
|
|
46
|
+
*
|
|
47
|
+
* @param config - Schema Element Editor 配置(支持响应式值)
|
|
48
|
+
* @returns 桥接器方法,包含 pushSchema
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```vue
|
|
52
|
+
* <script setup>
|
|
53
|
+
* import { useSchemaElementEditor } from '@schema-element-editor/host-sdk/vue'
|
|
54
|
+
* import { ref } from 'vue'
|
|
55
|
+
*
|
|
56
|
+
* const dataStore = ref({})
|
|
57
|
+
*
|
|
58
|
+
* const { pushSchema } = useSchemaElementEditor({
|
|
59
|
+
* getSchema: (params) => dataStore.value[params],
|
|
60
|
+
* updateSchema: (schema, params) => {
|
|
61
|
+
* dataStore.value[params] = schema
|
|
62
|
+
* return true
|
|
63
|
+
* },
|
|
64
|
+
* })
|
|
65
|
+
*
|
|
66
|
+
* // 数据变化时调用 pushSchema 推送数据(录制功能自动可用)
|
|
67
|
+
* sseHandler.onData = (params, data) => pushSchema(params, data)
|
|
68
|
+
* </script>
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
declare function useSchemaElementEditor(config: VueSchemaElementEditorConfig): UseSchemaElementEditorReturn;
|
|
72
|
+
|
|
73
|
+
export { PostMessageSourceConfig, PostMessageTypeConfig, SchemaElementEditorRecording, SchemaValue, type UseSchemaElementEditorReturn, type VueSchemaElementEditorConfig, useSchemaElementEditor };
|
package/dist/vue.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createSchemaElementEditorBridge
|
|
3
|
+
} from "./chunk-KMNKOOWF.js";
|
|
4
|
+
|
|
5
|
+
// src/vue.ts
|
|
6
|
+
import { onMounted, onUnmounted, watch, toValue } from "vue";
|
|
7
|
+
function useSchemaElementEditor(config) {
|
|
8
|
+
const { getSchema, updateSchema, renderPreview, sourceConfig, messageTypes, enabled } = config;
|
|
9
|
+
let bridge = null;
|
|
10
|
+
const destroyBridge = () => {
|
|
11
|
+
if (bridge) {
|
|
12
|
+
bridge.cleanup();
|
|
13
|
+
bridge = null;
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
const createBridgeInstance = () => {
|
|
17
|
+
destroyBridge();
|
|
18
|
+
if (toValue(enabled) === false) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const proxyConfig = {
|
|
22
|
+
getSchema: (params) => toValue(getSchema)(params),
|
|
23
|
+
updateSchema: (schema, params) => toValue(updateSchema)(schema, params),
|
|
24
|
+
renderPreview: toValue(renderPreview) ? (schema, containerId) => toValue(renderPreview)?.(schema, containerId) : void 0,
|
|
25
|
+
sourceConfig,
|
|
26
|
+
messageTypes
|
|
27
|
+
};
|
|
28
|
+
bridge = createSchemaElementEditorBridge(proxyConfig);
|
|
29
|
+
};
|
|
30
|
+
onMounted(() => {
|
|
31
|
+
createBridgeInstance();
|
|
32
|
+
});
|
|
33
|
+
onUnmounted(() => {
|
|
34
|
+
destroyBridge();
|
|
35
|
+
});
|
|
36
|
+
watch(
|
|
37
|
+
() => [
|
|
38
|
+
toValue(enabled),
|
|
39
|
+
sourceConfig?.contentSource,
|
|
40
|
+
sourceConfig?.hostSource,
|
|
41
|
+
messageTypes?.getSchema,
|
|
42
|
+
messageTypes?.updateSchema,
|
|
43
|
+
messageTypes?.checkPreview,
|
|
44
|
+
messageTypes?.renderPreview,
|
|
45
|
+
messageTypes?.cleanupPreview,
|
|
46
|
+
messageTypes?.startRecording,
|
|
47
|
+
messageTypes?.stopRecording,
|
|
48
|
+
messageTypes?.schemaPush
|
|
49
|
+
],
|
|
50
|
+
() => {
|
|
51
|
+
createBridgeInstance();
|
|
52
|
+
},
|
|
53
|
+
{ deep: true }
|
|
54
|
+
);
|
|
55
|
+
const recording = {
|
|
56
|
+
push: (params, data) => {
|
|
57
|
+
bridge?.recording.push(params, data);
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
return { recording };
|
|
61
|
+
}
|
|
62
|
+
export {
|
|
63
|
+
useSchemaElementEditor
|
|
64
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@schema-element-editor/host-sdk",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Schema Element Editor (SEE) 插件宿主接入 SDK,支持 React/Vue/纯 JS",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
},
|
|
15
|
+
"./core": {
|
|
16
|
+
"types": "./dist/core.d.ts",
|
|
17
|
+
"import": "./dist/core.js",
|
|
18
|
+
"require": "./dist/core.cjs"
|
|
19
|
+
},
|
|
20
|
+
"./react": {
|
|
21
|
+
"types": "./dist/react.d.ts",
|
|
22
|
+
"import": "./dist/react.js",
|
|
23
|
+
"require": "./dist/react.cjs"
|
|
24
|
+
},
|
|
25
|
+
"./vue": {
|
|
26
|
+
"types": "./dist/vue.d.ts",
|
|
27
|
+
"import": "./dist/vue.js",
|
|
28
|
+
"require": "./dist/vue.cjs"
|
|
29
|
+
}
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist"
|
|
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
|
+
"peerDependencies": {
|
|
39
|
+
"react": ">=17.0.0",
|
|
40
|
+
"vue": ">=3.0.0"
|
|
41
|
+
},
|
|
42
|
+
"peerDependenciesMeta": {
|
|
43
|
+
"react": {
|
|
44
|
+
"optional": true
|
|
45
|
+
},
|
|
46
|
+
"vue": {
|
|
47
|
+
"optional": true
|
|
48
|
+
}
|
|
49
|
+
},
|
|
50
|
+
"devDependencies": {
|
|
51
|
+
"tsup": "^8.0.0",
|
|
52
|
+
"typescript": "^5.3.0",
|
|
53
|
+
"vue": "^3.4.0"
|
|
54
|
+
},
|
|
55
|
+
"keywords": [
|
|
56
|
+
"schema-element-editor",
|
|
57
|
+
"react",
|
|
58
|
+
"vue",
|
|
59
|
+
"hooks",
|
|
60
|
+
"composable",
|
|
61
|
+
"postMessage"
|
|
62
|
+
],
|
|
63
|
+
"license": "MIT",
|
|
64
|
+
"repository": {
|
|
65
|
+
"type": "git",
|
|
66
|
+
"url": "https://github.com/hei-f/schema-element-editor.git",
|
|
67
|
+
"directory": "packages/schema-element-editor-sdk"
|
|
68
|
+
}
|
|
69
|
+
}
|