@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
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
// src/core.ts
|
|
2
|
+
var DEFAULT_SOURCE_CONFIG = {
|
|
3
|
+
contentSource: "schema-element-editor-content",
|
|
4
|
+
hostSource: "schema-element-editor-host"
|
|
5
|
+
};
|
|
6
|
+
var DEFAULT_MESSAGE_TYPES = {
|
|
7
|
+
getSchema: "GET_SCHEMA",
|
|
8
|
+
updateSchema: "UPDATE_SCHEMA",
|
|
9
|
+
checkPreview: "CHECK_PREVIEW",
|
|
10
|
+
renderPreview: "RENDER_PREVIEW",
|
|
11
|
+
cleanupPreview: "CLEANUP_PREVIEW",
|
|
12
|
+
// 录制模式相关
|
|
13
|
+
startRecording: "START_RECORDING",
|
|
14
|
+
stopRecording: "STOP_RECORDING",
|
|
15
|
+
schemaPush: "SCHEMA_PUSH"
|
|
16
|
+
};
|
|
17
|
+
function createSchemaElementEditorBridge(config) {
|
|
18
|
+
const { getSchema, updateSchema, renderPreview, sourceConfig, messageTypes } = config;
|
|
19
|
+
const mergedSourceConfig = {
|
|
20
|
+
...DEFAULT_SOURCE_CONFIG,
|
|
21
|
+
...sourceConfig
|
|
22
|
+
};
|
|
23
|
+
const mergedMessageTypes = {
|
|
24
|
+
...DEFAULT_MESSAGE_TYPES,
|
|
25
|
+
...messageTypes
|
|
26
|
+
};
|
|
27
|
+
let previewCleanupFn = null;
|
|
28
|
+
const recordingParams = /* @__PURE__ */ new Set();
|
|
29
|
+
const currentConfig = { getSchema, updateSchema, renderPreview };
|
|
30
|
+
const sendResponse = (requestId, result) => {
|
|
31
|
+
const message = {
|
|
32
|
+
source: mergedSourceConfig.hostSource,
|
|
33
|
+
requestId,
|
|
34
|
+
...result
|
|
35
|
+
};
|
|
36
|
+
const isInIframe = window !== window.top;
|
|
37
|
+
const targetWindow = isInIframe ? window.parent : window;
|
|
38
|
+
targetWindow.postMessage(message, "*");
|
|
39
|
+
};
|
|
40
|
+
const pushSchema = (params, data) => {
|
|
41
|
+
if (!recordingParams.has(params)) {
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
const message = {
|
|
45
|
+
source: mergedSourceConfig.hostSource,
|
|
46
|
+
type: mergedMessageTypes.schemaPush,
|
|
47
|
+
payload: {
|
|
48
|
+
success: true,
|
|
49
|
+
data,
|
|
50
|
+
params
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
const isInIframe = window !== window.top;
|
|
54
|
+
const targetWindow = isInIframe ? window.parent : window;
|
|
55
|
+
targetWindow.postMessage(message, "*");
|
|
56
|
+
};
|
|
57
|
+
const handleMessage = (event) => {
|
|
58
|
+
const isFromSelf = event.source === window;
|
|
59
|
+
const isFromParent = window !== window.top && event.source === window.parent;
|
|
60
|
+
if (!isFromSelf && !isFromParent) return;
|
|
61
|
+
if (!event.data || event.data.source !== mergedSourceConfig.contentSource) return;
|
|
62
|
+
const { type, payload, requestId } = event.data;
|
|
63
|
+
if (!requestId) return;
|
|
64
|
+
const { getSchema: getSchema2, updateSchema: updateSchema2, renderPreview: renderPreview2 } = currentConfig;
|
|
65
|
+
let result;
|
|
66
|
+
switch (type) {
|
|
67
|
+
case mergedMessageTypes.getSchema: {
|
|
68
|
+
const params = String(payload?.params ?? "");
|
|
69
|
+
try {
|
|
70
|
+
const data = getSchema2(params);
|
|
71
|
+
result = { success: true, data };
|
|
72
|
+
} catch (error) {
|
|
73
|
+
result = {
|
|
74
|
+
success: false,
|
|
75
|
+
error: error instanceof Error ? error.message : "\u83B7\u53D6 Schema \u5931\u8D25"
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
case mergedMessageTypes.updateSchema: {
|
|
81
|
+
const schema = payload?.schema;
|
|
82
|
+
const params = String(payload?.params ?? "");
|
|
83
|
+
try {
|
|
84
|
+
const success = updateSchema2(schema, params);
|
|
85
|
+
result = { success };
|
|
86
|
+
} catch (error) {
|
|
87
|
+
result = {
|
|
88
|
+
success: false,
|
|
89
|
+
error: error instanceof Error ? error.message : "\u66F4\u65B0 Schema \u5931\u8D25"
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
case mergedMessageTypes.checkPreview: {
|
|
95
|
+
result = { exists: typeof renderPreview2 === "function" };
|
|
96
|
+
break;
|
|
97
|
+
}
|
|
98
|
+
case mergedMessageTypes.renderPreview: {
|
|
99
|
+
if (typeof renderPreview2 !== "function") {
|
|
100
|
+
result = { success: false, error: "\u9884\u89C8\u529F\u80FD\u672A\u914D\u7F6E" };
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
const schema = payload?.schema;
|
|
104
|
+
const containerId = String(payload?.containerId ?? "");
|
|
105
|
+
try {
|
|
106
|
+
if (previewCleanupFn) {
|
|
107
|
+
previewCleanupFn();
|
|
108
|
+
previewCleanupFn = null;
|
|
109
|
+
}
|
|
110
|
+
const cleanup = renderPreview2(schema, containerId);
|
|
111
|
+
if (typeof cleanup === "function") {
|
|
112
|
+
previewCleanupFn = cleanup;
|
|
113
|
+
}
|
|
114
|
+
result = { success: true };
|
|
115
|
+
} catch (error) {
|
|
116
|
+
result = {
|
|
117
|
+
success: false,
|
|
118
|
+
error: error instanceof Error ? error.message : "\u6E32\u67D3\u9884\u89C8\u5931\u8D25"
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
case mergedMessageTypes.cleanupPreview: {
|
|
124
|
+
try {
|
|
125
|
+
if (previewCleanupFn) {
|
|
126
|
+
previewCleanupFn();
|
|
127
|
+
previewCleanupFn = null;
|
|
128
|
+
}
|
|
129
|
+
result = { success: true };
|
|
130
|
+
} catch (error) {
|
|
131
|
+
result = {
|
|
132
|
+
success: false,
|
|
133
|
+
error: error instanceof Error ? error.message : "\u6E05\u7406\u9884\u89C8\u5931\u8D25"
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
case mergedMessageTypes.startRecording: {
|
|
139
|
+
const params = String(payload?.params ?? "");
|
|
140
|
+
recordingParams.add(params);
|
|
141
|
+
result = { success: true };
|
|
142
|
+
break;
|
|
143
|
+
}
|
|
144
|
+
case mergedMessageTypes.stopRecording: {
|
|
145
|
+
const params = String(payload?.params ?? "");
|
|
146
|
+
recordingParams.delete(params);
|
|
147
|
+
result = { success: true };
|
|
148
|
+
break;
|
|
149
|
+
}
|
|
150
|
+
default:
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
sendResponse(requestId, result);
|
|
154
|
+
};
|
|
155
|
+
window.addEventListener("message", handleMessage);
|
|
156
|
+
return {
|
|
157
|
+
cleanup: () => {
|
|
158
|
+
window.removeEventListener("message", handleMessage);
|
|
159
|
+
if (previewCleanupFn) {
|
|
160
|
+
previewCleanupFn();
|
|
161
|
+
previewCleanupFn = null;
|
|
162
|
+
}
|
|
163
|
+
recordingParams.clear();
|
|
164
|
+
},
|
|
165
|
+
recording: {
|
|
166
|
+
push: pushSchema
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export {
|
|
172
|
+
createSchemaElementEditorBridge
|
|
173
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createSchemaElementEditorBridge
|
|
3
|
+
} from "./chunk-KMNKOOWF.js";
|
|
4
|
+
|
|
5
|
+
// src/react.ts
|
|
6
|
+
import { useEffect, useRef, useMemo, useCallback } from "react";
|
|
7
|
+
function useSchemaElementEditor(config) {
|
|
8
|
+
const { getSchema, updateSchema, renderPreview, sourceConfig, messageTypes, enabled } = config;
|
|
9
|
+
const configRef = useRef({ getSchema, updateSchema, renderPreview });
|
|
10
|
+
const bridgeRef = useRef(null);
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
configRef.current = { getSchema, updateSchema, renderPreview };
|
|
13
|
+
}, [getSchema, updateSchema, renderPreview]);
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
if (enabled === false) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const proxyConfig = {
|
|
19
|
+
getSchema: (params) => configRef.current.getSchema(params),
|
|
20
|
+
updateSchema: (schema, params) => configRef.current.updateSchema(schema, params),
|
|
21
|
+
renderPreview: configRef.current.renderPreview ? (schema, containerId) => configRef.current.renderPreview?.(schema, containerId) : void 0,
|
|
22
|
+
sourceConfig,
|
|
23
|
+
messageTypes
|
|
24
|
+
};
|
|
25
|
+
const bridge = createSchemaElementEditorBridge(proxyConfig);
|
|
26
|
+
bridgeRef.current = bridge;
|
|
27
|
+
return () => {
|
|
28
|
+
bridge.cleanup();
|
|
29
|
+
bridgeRef.current = null;
|
|
30
|
+
};
|
|
31
|
+
}, [enabled, configRef, sourceConfig, messageTypes]);
|
|
32
|
+
const push = useCallback((params, data) => {
|
|
33
|
+
bridgeRef.current?.recording.push(params, data);
|
|
34
|
+
}, []);
|
|
35
|
+
const recording = useMemo(() => ({ push }), [push]);
|
|
36
|
+
return { recording };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export {
|
|
40
|
+
useSchemaElementEditor
|
|
41
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createSchemaElementEditorBridge
|
|
3
|
+
} from "./chunk-5Q4T7TXN.js";
|
|
4
|
+
|
|
5
|
+
// src/react.ts
|
|
6
|
+
import { useEffect, useRef, useMemo, useCallback } from "react";
|
|
7
|
+
function useSchemaElementEditor(config) {
|
|
8
|
+
const { getSchema, updateSchema, renderPreview, sourceConfig, messageTypes, enabled } = config;
|
|
9
|
+
const configRef = useRef({ getSchema, updateSchema, renderPreview });
|
|
10
|
+
const bridgeRef = useRef(null);
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
configRef.current = { getSchema, updateSchema, renderPreview };
|
|
13
|
+
}, [getSchema, updateSchema, renderPreview]);
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
if (enabled === false) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const proxyConfig = {
|
|
19
|
+
getSchema: (params) => configRef.current.getSchema(params),
|
|
20
|
+
updateSchema: (schema, params) => configRef.current.updateSchema(schema, params),
|
|
21
|
+
renderPreview: configRef.current.renderPreview ? (schema, containerId) => configRef.current.renderPreview?.(schema, containerId) : void 0,
|
|
22
|
+
sourceConfig,
|
|
23
|
+
messageTypes
|
|
24
|
+
};
|
|
25
|
+
const bridge = createSchemaElementEditorBridge(proxyConfig);
|
|
26
|
+
bridgeRef.current = bridge;
|
|
27
|
+
return () => {
|
|
28
|
+
bridge.cleanup();
|
|
29
|
+
bridgeRef.current = null;
|
|
30
|
+
};
|
|
31
|
+
}, [enabled, configRef, sourceConfig, messageTypes]);
|
|
32
|
+
const push = useCallback((params, data) => {
|
|
33
|
+
bridgeRef.current?.recording.push(params, data);
|
|
34
|
+
}, []);
|
|
35
|
+
const recording = useMemo(() => ({ push }), [push]);
|
|
36
|
+
return { recording };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export {
|
|
40
|
+
useSchemaElementEditor
|
|
41
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createSchemaElementEditorBridge
|
|
3
|
+
} from "./chunk-DEMUKVTU.js";
|
|
4
|
+
|
|
5
|
+
// src/react.ts
|
|
6
|
+
import { useEffect, useRef, useMemo, useCallback } from "react";
|
|
7
|
+
function useSchemaElementEditor(config) {
|
|
8
|
+
const { getSchema, updateSchema, renderPreview, sourceConfig, messageTypes, enabled } = config;
|
|
9
|
+
const configRef = useRef({ getSchema, updateSchema, renderPreview });
|
|
10
|
+
const bridgeRef = useRef(null);
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
configRef.current = { getSchema, updateSchema, renderPreview };
|
|
13
|
+
}, [getSchema, updateSchema, renderPreview]);
|
|
14
|
+
useEffect(() => {
|
|
15
|
+
if (enabled === false) {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
const proxyConfig = {
|
|
19
|
+
getSchema: (params) => configRef.current.getSchema(params),
|
|
20
|
+
updateSchema: (schema, params) => configRef.current.updateSchema(schema, params),
|
|
21
|
+
renderPreview: configRef.current.renderPreview ? (schema, containerId) => configRef.current.renderPreview?.(schema, containerId) : void 0,
|
|
22
|
+
sourceConfig,
|
|
23
|
+
messageTypes
|
|
24
|
+
};
|
|
25
|
+
const bridge = createSchemaElementEditorBridge(proxyConfig);
|
|
26
|
+
bridgeRef.current = bridge;
|
|
27
|
+
return () => {
|
|
28
|
+
bridge.cleanup();
|
|
29
|
+
bridgeRef.current = null;
|
|
30
|
+
};
|
|
31
|
+
}, [enabled, configRef, sourceConfig, messageTypes]);
|
|
32
|
+
const push = useCallback((params, data) => {
|
|
33
|
+
bridgeRef.current?.recording.push(params, data);
|
|
34
|
+
}, []);
|
|
35
|
+
const recording = useMemo(() => ({ push }), [push]);
|
|
36
|
+
return { recording };
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export {
|
|
40
|
+
useSchemaElementEditor
|
|
41
|
+
};
|
package/dist/core.cjs
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
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/core.ts
|
|
21
|
+
var core_exports = {};
|
|
22
|
+
__export(core_exports, {
|
|
23
|
+
createSchemaElementEditorBridge: () => createSchemaElementEditorBridge
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(core_exports);
|
|
26
|
+
var DEFAULT_SOURCE_CONFIG = {
|
|
27
|
+
contentSource: "schema-element-editor-content",
|
|
28
|
+
hostSource: "schema-element-editor-host"
|
|
29
|
+
};
|
|
30
|
+
var DEFAULT_MESSAGE_TYPES = {
|
|
31
|
+
getSchema: "GET_SCHEMA",
|
|
32
|
+
updateSchema: "UPDATE_SCHEMA",
|
|
33
|
+
checkPreview: "CHECK_PREVIEW",
|
|
34
|
+
renderPreview: "RENDER_PREVIEW",
|
|
35
|
+
cleanupPreview: "CLEANUP_PREVIEW",
|
|
36
|
+
// 录制模式相关
|
|
37
|
+
startRecording: "START_RECORDING",
|
|
38
|
+
stopRecording: "STOP_RECORDING",
|
|
39
|
+
schemaPush: "SCHEMA_PUSH"
|
|
40
|
+
};
|
|
41
|
+
function createSchemaElementEditorBridge(config) {
|
|
42
|
+
const { getSchema, updateSchema, renderPreview, sourceConfig, messageTypes } = config;
|
|
43
|
+
const mergedSourceConfig = {
|
|
44
|
+
...DEFAULT_SOURCE_CONFIG,
|
|
45
|
+
...sourceConfig
|
|
46
|
+
};
|
|
47
|
+
const mergedMessageTypes = {
|
|
48
|
+
...DEFAULT_MESSAGE_TYPES,
|
|
49
|
+
...messageTypes
|
|
50
|
+
};
|
|
51
|
+
let previewCleanupFn = null;
|
|
52
|
+
const recordingParams = /* @__PURE__ */ new Set();
|
|
53
|
+
const currentConfig = { getSchema, updateSchema, renderPreview };
|
|
54
|
+
const sendResponse = (requestId, result) => {
|
|
55
|
+
const message = {
|
|
56
|
+
source: mergedSourceConfig.hostSource,
|
|
57
|
+
requestId,
|
|
58
|
+
...result
|
|
59
|
+
};
|
|
60
|
+
const isInIframe = window !== window.top;
|
|
61
|
+
const targetWindow = isInIframe ? window.parent : window;
|
|
62
|
+
targetWindow.postMessage(message, "*");
|
|
63
|
+
};
|
|
64
|
+
const pushSchema = (params, data) => {
|
|
65
|
+
if (!recordingParams.has(params)) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const message = {
|
|
69
|
+
source: mergedSourceConfig.hostSource,
|
|
70
|
+
type: mergedMessageTypes.schemaPush,
|
|
71
|
+
payload: {
|
|
72
|
+
success: true,
|
|
73
|
+
data,
|
|
74
|
+
params
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
const isInIframe = window !== window.top;
|
|
78
|
+
const targetWindow = isInIframe ? window.parent : window;
|
|
79
|
+
targetWindow.postMessage(message, "*");
|
|
80
|
+
};
|
|
81
|
+
const handleMessage = (event) => {
|
|
82
|
+
const isFromSelf = event.source === window;
|
|
83
|
+
const isFromParent = window !== window.top && event.source === window.parent;
|
|
84
|
+
if (!isFromSelf && !isFromParent) return;
|
|
85
|
+
if (!event.data || event.data.source !== mergedSourceConfig.contentSource) return;
|
|
86
|
+
const { type, payload, requestId } = event.data;
|
|
87
|
+
if (!requestId) return;
|
|
88
|
+
const { getSchema: getSchema2, updateSchema: updateSchema2, renderPreview: renderPreview2 } = currentConfig;
|
|
89
|
+
let result;
|
|
90
|
+
switch (type) {
|
|
91
|
+
case mergedMessageTypes.getSchema: {
|
|
92
|
+
const params = String(payload?.params ?? "");
|
|
93
|
+
try {
|
|
94
|
+
const data = getSchema2(params);
|
|
95
|
+
result = { success: true, data };
|
|
96
|
+
} catch (error) {
|
|
97
|
+
result = {
|
|
98
|
+
success: false,
|
|
99
|
+
error: error instanceof Error ? error.message : "\u83B7\u53D6 Schema \u5931\u8D25"
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
break;
|
|
103
|
+
}
|
|
104
|
+
case mergedMessageTypes.updateSchema: {
|
|
105
|
+
const schema = payload?.schema;
|
|
106
|
+
const params = String(payload?.params ?? "");
|
|
107
|
+
try {
|
|
108
|
+
const success = updateSchema2(schema, params);
|
|
109
|
+
result = { success };
|
|
110
|
+
} catch (error) {
|
|
111
|
+
result = {
|
|
112
|
+
success: false,
|
|
113
|
+
error: error instanceof Error ? error.message : "\u66F4\u65B0 Schema \u5931\u8D25"
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
case mergedMessageTypes.checkPreview: {
|
|
119
|
+
result = { exists: typeof renderPreview2 === "function" };
|
|
120
|
+
break;
|
|
121
|
+
}
|
|
122
|
+
case mergedMessageTypes.renderPreview: {
|
|
123
|
+
if (typeof renderPreview2 !== "function") {
|
|
124
|
+
result = { success: false, error: "\u9884\u89C8\u529F\u80FD\u672A\u914D\u7F6E" };
|
|
125
|
+
break;
|
|
126
|
+
}
|
|
127
|
+
const schema = payload?.schema;
|
|
128
|
+
const containerId = String(payload?.containerId ?? "");
|
|
129
|
+
try {
|
|
130
|
+
if (previewCleanupFn) {
|
|
131
|
+
previewCleanupFn();
|
|
132
|
+
previewCleanupFn = null;
|
|
133
|
+
}
|
|
134
|
+
const cleanup = renderPreview2(schema, containerId);
|
|
135
|
+
if (typeof cleanup === "function") {
|
|
136
|
+
previewCleanupFn = cleanup;
|
|
137
|
+
}
|
|
138
|
+
result = { success: true };
|
|
139
|
+
} catch (error) {
|
|
140
|
+
result = {
|
|
141
|
+
success: false,
|
|
142
|
+
error: error instanceof Error ? error.message : "\u6E32\u67D3\u9884\u89C8\u5931\u8D25"
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
case mergedMessageTypes.cleanupPreview: {
|
|
148
|
+
try {
|
|
149
|
+
if (previewCleanupFn) {
|
|
150
|
+
previewCleanupFn();
|
|
151
|
+
previewCleanupFn = null;
|
|
152
|
+
}
|
|
153
|
+
result = { success: true };
|
|
154
|
+
} catch (error) {
|
|
155
|
+
result = {
|
|
156
|
+
success: false,
|
|
157
|
+
error: error instanceof Error ? error.message : "\u6E05\u7406\u9884\u89C8\u5931\u8D25"
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
break;
|
|
161
|
+
}
|
|
162
|
+
case mergedMessageTypes.startRecording: {
|
|
163
|
+
const params = String(payload?.params ?? "");
|
|
164
|
+
recordingParams.add(params);
|
|
165
|
+
result = { success: true };
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
case mergedMessageTypes.stopRecording: {
|
|
169
|
+
const params = String(payload?.params ?? "");
|
|
170
|
+
recordingParams.delete(params);
|
|
171
|
+
result = { success: true };
|
|
172
|
+
break;
|
|
173
|
+
}
|
|
174
|
+
default:
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
sendResponse(requestId, result);
|
|
178
|
+
};
|
|
179
|
+
window.addEventListener("message", handleMessage);
|
|
180
|
+
return {
|
|
181
|
+
cleanup: () => {
|
|
182
|
+
window.removeEventListener("message", handleMessage);
|
|
183
|
+
if (previewCleanupFn) {
|
|
184
|
+
previewCleanupFn();
|
|
185
|
+
previewCleanupFn = null;
|
|
186
|
+
}
|
|
187
|
+
recordingParams.clear();
|
|
188
|
+
},
|
|
189
|
+
recording: {
|
|
190
|
+
push: pushSchema
|
|
191
|
+
}
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
195
|
+
0 && (module.exports = {
|
|
196
|
+
createSchemaElementEditorBridge
|
|
197
|
+
});
|
package/dist/core.d.cts
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 };
|