@schema-element-editor/host-sdk 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,171 @@
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 targetWindow = window.top ?? window;
37
+ targetWindow.postMessage(message, "*");
38
+ };
39
+ const pushSchema = (params, data) => {
40
+ if (!recordingParams.has(params)) {
41
+ return;
42
+ }
43
+ const message = {
44
+ source: mergedSourceConfig.hostSource,
45
+ type: mergedMessageTypes.schemaPush,
46
+ payload: {
47
+ success: true,
48
+ data,
49
+ params
50
+ }
51
+ };
52
+ const targetWindow = window.top ?? window;
53
+ targetWindow.postMessage(message, "*");
54
+ };
55
+ const handleMessage = (event) => {
56
+ const isFromSelf = event.source === window;
57
+ const isFromParent = window !== window.top && event.source === window.parent;
58
+ if (!isFromSelf && !isFromParent) return;
59
+ if (!event.data || event.data.source !== mergedSourceConfig.contentSource) return;
60
+ const { type, payload, requestId } = event.data;
61
+ if (!requestId) return;
62
+ const { getSchema: getSchema2, updateSchema: updateSchema2, renderPreview: renderPreview2 } = currentConfig;
63
+ let result;
64
+ switch (type) {
65
+ case mergedMessageTypes.getSchema: {
66
+ const params = String(payload?.params ?? "");
67
+ try {
68
+ const data = getSchema2(params);
69
+ result = { success: true, data };
70
+ } catch (error) {
71
+ result = {
72
+ success: false,
73
+ error: error instanceof Error ? error.message : "\u83B7\u53D6 Schema \u5931\u8D25"
74
+ };
75
+ }
76
+ break;
77
+ }
78
+ case mergedMessageTypes.updateSchema: {
79
+ const schema = payload?.schema;
80
+ const params = String(payload?.params ?? "");
81
+ try {
82
+ const success = updateSchema2(schema, params);
83
+ result = { success };
84
+ } catch (error) {
85
+ result = {
86
+ success: false,
87
+ error: error instanceof Error ? error.message : "\u66F4\u65B0 Schema \u5931\u8D25"
88
+ };
89
+ }
90
+ break;
91
+ }
92
+ case mergedMessageTypes.checkPreview: {
93
+ result = { exists: typeof renderPreview2 === "function" };
94
+ break;
95
+ }
96
+ case mergedMessageTypes.renderPreview: {
97
+ if (typeof renderPreview2 !== "function") {
98
+ result = { success: false, error: "\u9884\u89C8\u529F\u80FD\u672A\u914D\u7F6E" };
99
+ break;
100
+ }
101
+ const schema = payload?.schema;
102
+ const containerId = String(payload?.containerId ?? "");
103
+ try {
104
+ if (previewCleanupFn) {
105
+ previewCleanupFn();
106
+ previewCleanupFn = null;
107
+ }
108
+ const cleanup = renderPreview2(schema, containerId);
109
+ if (typeof cleanup === "function") {
110
+ previewCleanupFn = cleanup;
111
+ }
112
+ result = { success: true };
113
+ } catch (error) {
114
+ result = {
115
+ success: false,
116
+ error: error instanceof Error ? error.message : "\u6E32\u67D3\u9884\u89C8\u5931\u8D25"
117
+ };
118
+ }
119
+ break;
120
+ }
121
+ case mergedMessageTypes.cleanupPreview: {
122
+ try {
123
+ if (previewCleanupFn) {
124
+ previewCleanupFn();
125
+ previewCleanupFn = null;
126
+ }
127
+ result = { success: true };
128
+ } catch (error) {
129
+ result = {
130
+ success: false,
131
+ error: error instanceof Error ? error.message : "\u6E05\u7406\u9884\u89C8\u5931\u8D25"
132
+ };
133
+ }
134
+ break;
135
+ }
136
+ case mergedMessageTypes.startRecording: {
137
+ const params = String(payload?.params ?? "");
138
+ recordingParams.add(params);
139
+ result = { success: true };
140
+ break;
141
+ }
142
+ case mergedMessageTypes.stopRecording: {
143
+ const params = String(payload?.params ?? "");
144
+ recordingParams.delete(params);
145
+ result = { success: true };
146
+ break;
147
+ }
148
+ default:
149
+ return;
150
+ }
151
+ sendResponse(requestId, result);
152
+ };
153
+ window.addEventListener("message", handleMessage);
154
+ return {
155
+ cleanup: () => {
156
+ window.removeEventListener("message", handleMessage);
157
+ if (previewCleanupFn) {
158
+ previewCleanupFn();
159
+ previewCleanupFn = null;
160
+ }
161
+ recordingParams.clear();
162
+ },
163
+ recording: {
164
+ push: pushSchema
165
+ }
166
+ };
167
+ }
168
+
169
+ export {
170
+ createSchemaElementEditorBridge
171
+ };
@@ -0,0 +1,41 @@
1
+ import {
2
+ createSchemaElementEditorBridge
3
+ } from "./chunk-52EFKQHQ.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 CHANGED
@@ -57,8 +57,7 @@ function createSchemaElementEditorBridge(config) {
57
57
  requestId,
58
58
  ...result
59
59
  };
60
- const isInIframe = window !== window.top;
61
- const targetWindow = isInIframe ? window.parent : window;
60
+ const targetWindow = window.top ?? window;
62
61
  targetWindow.postMessage(message, "*");
63
62
  };
64
63
  const pushSchema = (params, data) => {
@@ -74,8 +73,7 @@ function createSchemaElementEditorBridge(config) {
74
73
  params
75
74
  }
76
75
  };
77
- const isInIframe = window !== window.top;
78
- const targetWindow = isInIframe ? window.parent : window;
76
+ const targetWindow = window.top ?? window;
79
77
  targetWindow.postMessage(message, "*");
80
78
  };
81
79
  const handleMessage = (event) => {
package/dist/core.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  createSchemaElementEditorBridge
3
- } from "./chunk-KMNKOOWF.js";
3
+ } from "./chunk-52EFKQHQ.js";
4
4
  export {
5
5
  createSchemaElementEditorBridge
6
6
  };
package/dist/index.cjs CHANGED
@@ -63,8 +63,7 @@ function createSchemaElementEditorBridge(config) {
63
63
  requestId,
64
64
  ...result
65
65
  };
66
- const isInIframe = window !== window.top;
67
- const targetWindow = isInIframe ? window.parent : window;
66
+ const targetWindow = window.top ?? window;
68
67
  targetWindow.postMessage(message, "*");
69
68
  };
70
69
  const pushSchema = (params, data) => {
@@ -80,8 +79,7 @@ function createSchemaElementEditorBridge(config) {
80
79
  params
81
80
  }
82
81
  };
83
- const isInIframe = window !== window.top;
84
- const targetWindow = isInIframe ? window.parent : window;
82
+ const targetWindow = window.top ?? window;
85
83
  targetWindow.postMessage(message, "*");
86
84
  };
87
85
  const handleMessage = (event) => {
package/dist/index.js CHANGED
@@ -1,9 +1,9 @@
1
1
  import {
2
2
  useSchemaElementEditor
3
- } from "./chunk-MU6HL5E3.js";
3
+ } from "./chunk-HBZGOJIJ.js";
4
4
  import {
5
5
  createSchemaElementEditorBridge
6
- } from "./chunk-KMNKOOWF.js";
6
+ } from "./chunk-52EFKQHQ.js";
7
7
  export {
8
8
  createSchemaElementEditorBridge,
9
9
  useSchemaElementEditor
package/dist/react.cjs CHANGED
@@ -60,8 +60,7 @@ function createSchemaElementEditorBridge(config) {
60
60
  requestId,
61
61
  ...result
62
62
  };
63
- const isInIframe = window !== window.top;
64
- const targetWindow = isInIframe ? window.parent : window;
63
+ const targetWindow = window.top ?? window;
65
64
  targetWindow.postMessage(message, "*");
66
65
  };
67
66
  const pushSchema = (params, data) => {
@@ -77,8 +76,7 @@ function createSchemaElementEditorBridge(config) {
77
76
  params
78
77
  }
79
78
  };
80
- const isInIframe = window !== window.top;
81
- const targetWindow = isInIframe ? window.parent : window;
79
+ const targetWindow = window.top ?? window;
82
80
  targetWindow.postMessage(message, "*");
83
81
  };
84
82
  const handleMessage = (event) => {
package/dist/react.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  useSchemaElementEditor
3
- } from "./chunk-MU6HL5E3.js";
4
- import "./chunk-KMNKOOWF.js";
3
+ } from "./chunk-HBZGOJIJ.js";
4
+ import "./chunk-52EFKQHQ.js";
5
5
  export {
6
6
  useSchemaElementEditor
7
7
  };
package/dist/vue.cjs CHANGED
@@ -60,8 +60,7 @@ function createSchemaElementEditorBridge(config) {
60
60
  requestId,
61
61
  ...result
62
62
  };
63
- const isInIframe = window !== window.top;
64
- const targetWindow = isInIframe ? window.parent : window;
63
+ const targetWindow = window.top ?? window;
65
64
  targetWindow.postMessage(message, "*");
66
65
  };
67
66
  const pushSchema = (params, data) => {
@@ -77,8 +76,7 @@ function createSchemaElementEditorBridge(config) {
77
76
  params
78
77
  }
79
78
  };
80
- const isInIframe = window !== window.top;
81
- const targetWindow = isInIframe ? window.parent : window;
79
+ const targetWindow = window.top ?? window;
82
80
  targetWindow.postMessage(message, "*");
83
81
  };
84
82
  const handleMessage = (event) => {
package/dist/vue.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import {
2
2
  createSchemaElementEditorBridge
3
- } from "./chunk-KMNKOOWF.js";
3
+ } from "./chunk-52EFKQHQ.js";
4
4
 
5
5
  // src/vue.ts
6
6
  import { onMounted, onUnmounted, watch, toValue } from "vue";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@schema-element-editor/host-sdk",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "description": "Schema Element Editor (SEE) 插件宿主接入 SDK,支持 React/Vue/纯 JS",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",