@zzdadelu/schema-builder 1.0.0-alpha.54 → 1.0.0-alpha.56

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.
Files changed (3) hide show
  1. package/es/main.js +73 -116
  2. package/lib/main.js +72 -115
  3. package/package.json +1 -1
package/es/main.js CHANGED
@@ -13,7 +13,7 @@ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" !=
13
13
  function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
14
14
  function _objectWithoutProperties(e, t) { if (null == e) return {}; var o, r, i = _objectWithoutPropertiesLoose(e, t); if (Object.getOwnPropertySymbols) { var n = Object.getOwnPropertySymbols(e); for (r = 0; r < n.length; r++) o = n[r], -1 === t.indexOf(o) && {}.propertyIsEnumerable.call(e, o) && (i[o] = e[o]); } return i; }
15
15
  function _objectWithoutPropertiesLoose(r, e) { if (null == r) return {}; var t = {}; for (var n in r) if ({}.hasOwnProperty.call(r, n)) { if (-1 !== e.indexOf(n)) continue; t[n] = r[n]; } return t; }
16
- import React, { useEffect, useRef, useImperativeHandle, forwardRef, useState, useCallback } from 'react';
16
+ import React, { useEffect, useRef, useImperativeHandle, forwardRef, useState } from 'react';
17
17
  import createIframe from './createIframe';
18
18
  import * as defaultSetting from './settings';
19
19
  var Design = function Design(props, ref) {
@@ -21,15 +21,12 @@ var Design = function Design(props, ref) {
21
21
  settings = props.settings,
22
22
  restProps = _objectWithoutProperties(props, _excluded);
23
23
  var containerRef = useRef(null);
24
- var iframeRef;
24
+ var iframeRef = useRef(null);
25
25
  var _useState = useState(true),
26
26
  _useState2 = _slicedToArray(_useState, 2),
27
27
  loading = _useState2[0],
28
28
  setLoading = _useState2[1];
29
- var _useState3 = useState(0),
30
- _useState4 = _slicedToArray(_useState3, 2),
31
- retryCount = _useState4[0],
32
- setRetryCount = _useState4[1];
29
+ var retryCountRef = useRef(0);
33
30
  var MAX_RETRIES = 3;
34
31
  useImperativeHandle(ref, function () {
35
32
  return {
@@ -40,151 +37,111 @@ var Design = function Design(props, ref) {
40
37
  setValue: function setValue(schema) {
41
38
  var _iframeRef$current2, _iframeRef$current2$c, _iframeRef$current2$c2, _iframeRef$current2$c3;
42
39
  return (_iframeRef$current2 = iframeRef.current) === null || _iframeRef$current2 === void 0 ? void 0 : (_iframeRef$current2$c = _iframeRef$current2.contentWindow) === null || _iframeRef$current2$c === void 0 ? void 0 : (_iframeRef$current2$c2 = _iframeRef$current2$c.__FR_ENGINE__) === null || _iframeRef$current2$c2 === void 0 ? void 0 : (_iframeRef$current2$c3 = _iframeRef$current2$c2.importSchema) === null || _iframeRef$current2$c3 === void 0 ? void 0 : _iframeRef$current2$c3.call(_iframeRef$current2$c2, schema);
43
- },
44
- reload: function reload() {
45
- setRetryCount(0);
46
- cleanupIframe();
47
- initIframe();
48
40
  }
49
41
  };
50
42
  });
51
- var cleanupIframe = useCallback(function () {
52
- if (iframeRef.current && containerRef.current) {
53
- try {
54
- containerRef.current.removeChild(iframeRef.current);
55
- } catch (e) {
56
- // 忽略错误
57
- }
58
- iframeRef.current = null;
59
- }
60
- }, []);
61
- var initIframe = useCallback(function () {
62
- console.log('[SchemaBuilder] Initializing iframe, retry:', retryCount);
63
-
64
- // 清理旧的 iframe
65
- cleanupIframe();
43
+ var _initIframe = function initIframe() {
44
+ console.log('[SchemaBuilder] Initializing iframe');
66
45
 
67
46
  // 确保容器存在
68
47
  if (!containerRef.current) {
69
- console.error('[SchemaBuilder] Container ref is null, delaying iframe creation');
70
- setTimeout(initIframe, 100);
48
+ console.error('[SchemaBuilder] Container not ready');
49
+ setTimeout(_initIframe, 100);
71
50
  return;
72
51
  }
73
52
 
53
+ // 清理旧的 iframe
54
+ if (iframeRef.current && containerRef.current.contains(iframeRef.current)) {
55
+ containerRef.current.removeChild(iframeRef.current);
56
+ }
57
+
74
58
  // 创建新的 iframe
75
59
  var iframe = createIframe();
76
60
  iframeRef.current = iframe;
77
-
78
- // 添加错误处理
79
- iframe.onerror = function (e) {
80
- console.error('[SchemaBuilder] Iframe error:', e);
81
- };
82
61
  iframe.onload = function () {
83
62
  console.log('[SchemaBuilder] Iframe loaded');
84
63
  setLoading(false);
85
64
  };
65
+ iframe.onerror = function () {
66
+ console.error('[SchemaBuilder] Iframe load error');
67
+ retryIframe();
68
+ };
86
69
 
87
- // 添加到容器
88
- try {
89
- containerRef.current.appendChild(iframe);
90
- } catch (error) {
91
- console.error('[SchemaBuilder] Failed to append iframe:', error);
92
- if (retryCount < MAX_RETRIES) {
93
- setRetryCount(function (prev) {
94
- return prev + 1;
95
- });
96
- setTimeout(initIframe, 1000);
97
- }
98
- return;
70
+ // 添加到 DOM
71
+ containerRef.current.appendChild(iframe);
72
+ };
73
+ var retryIframe = function retryIframe() {
74
+ if (retryCountRef.current < MAX_RETRIES) {
75
+ retryCountRef.current++;
76
+ console.log("[SchemaBuilder] Retrying iframe (".concat(retryCountRef.current, "/").concat(MAX_RETRIES, ")"));
77
+ setTimeout(_initIframe, 1000);
78
+ } else {
79
+ console.error('[SchemaBuilder] Max retries reached');
80
+ setLoading(false);
99
81
  }
100
-
101
- // 设置超时检查
102
- var timeoutId = setTimeout(function () {
103
- if (iframe && (!iframe.contentWindow || !iframe.contentDocument)) {
104
- console.warn('[SchemaBuilder] Iframe load timeout');
105
- if (retryCount < MAX_RETRIES) {
106
- setRetryCount(function (prev) {
107
- return prev + 1;
108
- });
109
- cleanupIframe();
110
- initIframe();
111
- }
112
- }
113
- }, 10000);
114
- return function () {
115
- return clearTimeout(timeoutId);
116
- };
117
- }, [cleanupIframe, retryCount]);
118
- var handleMessage = useCallback(function (event) {
82
+ };
83
+ var engineOnLoad = function engineOnLoad(event) {
119
84
  if (event.data.type !== 'engine-load') {
120
85
  return;
121
86
  }
122
- console.log('[SchemaBuilder] Engine loaded');
87
+ console.log('[SchemaBuilder] Engine load event received');
88
+
89
+ // 延迟执行以确保 iframe 完全加载
123
90
  setTimeout(function () {
124
- try {
125
- var _iframeRef$current3, _iframeRef$current3$c;
126
- if ((_iframeRef$current3 = iframeRef.current) === null || _iframeRef$current3 === void 0 ? void 0 : (_iframeRef$current3$c = _iframeRef$current3.contentWindow) === null || _iframeRef$current3$c === void 0 ? void 0 : _iframeRef$current3$c.__FR_ENGINE__) {
127
- iframeRef.current.contentWindow.__FR_ENGINE__.init(_objectSpread({
128
- settings: _objectSpread(_objectSpread({}, defaultSetting), settings),
129
- widgets: widgets,
130
- logo: {
131
- title: 'XRender'
132
- }
133
- }, restProps));
134
- console.log('[SchemaBuilder] Engine initialized successfully');
135
- } else {
136
- console.error('[SchemaBuilder] __FR_ENGINE__ not found in iframe');
137
- if (retryCount < MAX_RETRIES) {
138
- setRetryCount(function (prev) {
139
- return prev + 1;
140
- });
141
- cleanupIframe();
142
- initIframe();
91
+ var _iframeRef$current3, _iframeRef$current3$c;
92
+ if ((_iframeRef$current3 = iframeRef.current) === null || _iframeRef$current3 === void 0 ? void 0 : (_iframeRef$current3$c = _iframeRef$current3.contentWindow) === null || _iframeRef$current3$c === void 0 ? void 0 : _iframeRef$current3$c.__FR_ENGINE__) {
93
+ iframeRef.current.contentWindow.__FR_ENGINE__.init(_objectSpread({
94
+ settings: _objectSpread(_objectSpread({}, defaultSetting), settings),
95
+ widgets: widgets,
96
+ logo: {
97
+ title: 'XRender'
143
98
  }
144
- }
145
- } catch (error) {
146
- console.error('[SchemaBuilder] Error initializing engine:', error);
147
- if (retryCount < MAX_RETRIES) {
148
- setRetryCount(function (prev) {
149
- return prev + 1;
150
- });
151
- cleanupIframe();
152
- initIframe();
153
- }
99
+ }, restProps));
100
+ console.log('[SchemaBuilder] Engine initialized');
101
+ } else {
102
+ console.error('[SchemaBuilder] Engine not found in iframe');
103
+ retryIframe();
154
104
  }
155
- }, 500);
156
- }, [cleanupIframe, initIframe, retryCount, settings, widgets, restProps]);
105
+ }, 300);
106
+ };
157
107
  useEffect(function () {
158
- initIframe();
159
- window.addEventListener('message', handleMessage);
108
+ // 等待容器渲染完成
109
+ var timer = setTimeout(function () {
110
+ _initIframe();
111
+ }, 100);
112
+ window.addEventListener('message', engineOnLoad);
160
113
  return function () {
161
- window.removeEventListener('message', handleMessage);
162
- cleanupIframe();
114
+ clearTimeout(timer);
115
+ window.removeEventListener('message', engineOnLoad);
116
+
117
+ // 清理 iframe
118
+ if (iframeRef.current && containerRef.current) {
119
+ try {
120
+ containerRef.current.removeChild(iframeRef.current);
121
+ } catch (e) {
122
+ // 忽略错误
123
+ }
124
+ }
163
125
  };
164
- }, [initIframe, handleMessage, cleanupIframe]);
126
+ }, []);
165
127
  return /*#__PURE__*/React.createElement(React.Fragment, null, loading && /*#__PURE__*/React.createElement("div", {
166
128
  style: {
167
129
  position: 'absolute',
168
- top: 0,
169
- left: 0,
170
- right: 0,
171
- bottom: 0,
172
- display: 'flex',
173
- alignItems: 'center',
174
- justifyContent: 'center',
175
- backgroundColor: 'rgba(255, 255, 255, 0.8)',
130
+ top: '50%',
131
+ left: '50%',
132
+ transform: 'translate(-50%, -50%)',
133
+ padding: '20px',
134
+ backgroundColor: 'rgba(255, 255, 255, 0.9)',
135
+ borderRadius: '4px',
136
+ boxShadow: '0 2px 8px rgba(0,0,0,0.1)',
176
137
  zIndex: 10
177
138
  }
178
- }, /*#__PURE__*/React.createElement("div", null, "\u52A0\u8F7D\u8868\u5355\u8BBE\u8BA1\u5668...", retryCount > 0 && /*#__PURE__*/React.createElement("div", {
179
- style: {
180
- fontSize: '12px',
181
- color: '#666'
182
- }
183
- }, "\u91CD\u8BD5 ", retryCount, "/", MAX_RETRIES))), /*#__PURE__*/React.createElement("div", {
139
+ }, "\u6B63\u5728\u52A0\u8F7D\u8868\u5355\u8BBE\u8BA1\u5668..."), /*#__PURE__*/React.createElement("div", {
184
140
  ref: containerRef,
185
141
  style: {
186
142
  width: '100%',
187
- height: '100%'
143
+ height: '100%',
144
+ minHeight: '500px'
188
145
  }
189
146
  }));
190
147
  };
package/lib/main.js CHANGED
@@ -29,15 +29,12 @@ var Design = function Design(props, ref) {
29
29
  settings = props.settings,
30
30
  restProps = _objectWithoutProperties(props, _excluded);
31
31
  var containerRef = (0, _react.useRef)(null);
32
- var iframeRef;
32
+ var iframeRef = (0, _react.useRef)(null);
33
33
  var _useState = (0, _react.useState)(true),
34
34
  _useState2 = _slicedToArray(_useState, 2),
35
35
  loading = _useState2[0],
36
36
  setLoading = _useState2[1];
37
- var _useState3 = (0, _react.useState)(0),
38
- _useState4 = _slicedToArray(_useState3, 2),
39
- retryCount = _useState4[0],
40
- setRetryCount = _useState4[1];
37
+ var retryCountRef = (0, _react.useRef)(0);
41
38
  var MAX_RETRIES = 3;
42
39
  (0, _react.useImperativeHandle)(ref, function () {
43
40
  return {
@@ -48,151 +45,111 @@ var Design = function Design(props, ref) {
48
45
  setValue: function setValue(schema) {
49
46
  var _iframeRef$current2, _iframeRef$current2$c, _iframeRef$current2$c2, _iframeRef$current2$c3;
50
47
  return (_iframeRef$current2 = iframeRef.current) === null || _iframeRef$current2 === void 0 ? void 0 : (_iframeRef$current2$c = _iframeRef$current2.contentWindow) === null || _iframeRef$current2$c === void 0 ? void 0 : (_iframeRef$current2$c2 = _iframeRef$current2$c.__FR_ENGINE__) === null || _iframeRef$current2$c2 === void 0 ? void 0 : (_iframeRef$current2$c3 = _iframeRef$current2$c2.importSchema) === null || _iframeRef$current2$c3 === void 0 ? void 0 : _iframeRef$current2$c3.call(_iframeRef$current2$c2, schema);
51
- },
52
- reload: function reload() {
53
- setRetryCount(0);
54
- cleanupIframe();
55
- initIframe();
56
48
  }
57
49
  };
58
50
  });
59
- var cleanupIframe = (0, _react.useCallback)(function () {
60
- if (iframeRef.current && containerRef.current) {
61
- try {
62
- containerRef.current.removeChild(iframeRef.current);
63
- } catch (e) {
64
- // 忽略错误
65
- }
66
- iframeRef.current = null;
67
- }
68
- }, []);
69
- var initIframe = (0, _react.useCallback)(function () {
70
- console.log('[SchemaBuilder] Initializing iframe, retry:', retryCount);
71
-
72
- // 清理旧的 iframe
73
- cleanupIframe();
51
+ var _initIframe = function initIframe() {
52
+ console.log('[SchemaBuilder] Initializing iframe');
74
53
 
75
54
  // 确保容器存在
76
55
  if (!containerRef.current) {
77
- console.error('[SchemaBuilder] Container ref is null, delaying iframe creation');
78
- setTimeout(initIframe, 100);
56
+ console.error('[SchemaBuilder] Container not ready');
57
+ setTimeout(_initIframe, 100);
79
58
  return;
80
59
  }
81
60
 
61
+ // 清理旧的 iframe
62
+ if (iframeRef.current && containerRef.current.contains(iframeRef.current)) {
63
+ containerRef.current.removeChild(iframeRef.current);
64
+ }
65
+
82
66
  // 创建新的 iframe
83
67
  var iframe = (0, _createIframe.default)();
84
68
  iframeRef.current = iframe;
85
-
86
- // 添加错误处理
87
- iframe.onerror = function (e) {
88
- console.error('[SchemaBuilder] Iframe error:', e);
89
- };
90
69
  iframe.onload = function () {
91
70
  console.log('[SchemaBuilder] Iframe loaded');
92
71
  setLoading(false);
93
72
  };
73
+ iframe.onerror = function () {
74
+ console.error('[SchemaBuilder] Iframe load error');
75
+ retryIframe();
76
+ };
94
77
 
95
- // 添加到容器
96
- try {
97
- containerRef.current.appendChild(iframe);
98
- } catch (error) {
99
- console.error('[SchemaBuilder] Failed to append iframe:', error);
100
- if (retryCount < MAX_RETRIES) {
101
- setRetryCount(function (prev) {
102
- return prev + 1;
103
- });
104
- setTimeout(initIframe, 1000);
105
- }
106
- return;
78
+ // 添加到 DOM
79
+ containerRef.current.appendChild(iframe);
80
+ };
81
+ var retryIframe = function retryIframe() {
82
+ if (retryCountRef.current < MAX_RETRIES) {
83
+ retryCountRef.current++;
84
+ console.log("[SchemaBuilder] Retrying iframe (".concat(retryCountRef.current, "/").concat(MAX_RETRIES, ")"));
85
+ setTimeout(_initIframe, 1000);
86
+ } else {
87
+ console.error('[SchemaBuilder] Max retries reached');
88
+ setLoading(false);
107
89
  }
108
-
109
- // 设置超时检查
110
- var timeoutId = setTimeout(function () {
111
- if (iframe && (!iframe.contentWindow || !iframe.contentDocument)) {
112
- console.warn('[SchemaBuilder] Iframe load timeout');
113
- if (retryCount < MAX_RETRIES) {
114
- setRetryCount(function (prev) {
115
- return prev + 1;
116
- });
117
- cleanupIframe();
118
- initIframe();
119
- }
120
- }
121
- }, 10000);
122
- return function () {
123
- return clearTimeout(timeoutId);
124
- };
125
- }, [cleanupIframe, retryCount]);
126
- var handleMessage = (0, _react.useCallback)(function (event) {
90
+ };
91
+ var engineOnLoad = function engineOnLoad(event) {
127
92
  if (event.data.type !== 'engine-load') {
128
93
  return;
129
94
  }
130
- console.log('[SchemaBuilder] Engine loaded');
95
+ console.log('[SchemaBuilder] Engine load event received');
96
+
97
+ // 延迟执行以确保 iframe 完全加载
131
98
  setTimeout(function () {
132
- try {
133
- var _iframeRef$current3, _iframeRef$current3$c;
134
- if ((_iframeRef$current3 = iframeRef.current) === null || _iframeRef$current3 === void 0 ? void 0 : (_iframeRef$current3$c = _iframeRef$current3.contentWindow) === null || _iframeRef$current3$c === void 0 ? void 0 : _iframeRef$current3$c.__FR_ENGINE__) {
135
- iframeRef.current.contentWindow.__FR_ENGINE__.init(_objectSpread({
136
- settings: _objectSpread(_objectSpread({}, defaultSetting), settings),
137
- widgets: widgets,
138
- logo: {
139
- title: 'XRender'
140
- }
141
- }, restProps));
142
- console.log('[SchemaBuilder] Engine initialized successfully');
143
- } else {
144
- console.error('[SchemaBuilder] __FR_ENGINE__ not found in iframe');
145
- if (retryCount < MAX_RETRIES) {
146
- setRetryCount(function (prev) {
147
- return prev + 1;
148
- });
149
- cleanupIframe();
150
- initIframe();
99
+ var _iframeRef$current3, _iframeRef$current3$c;
100
+ if ((_iframeRef$current3 = iframeRef.current) === null || _iframeRef$current3 === void 0 ? void 0 : (_iframeRef$current3$c = _iframeRef$current3.contentWindow) === null || _iframeRef$current3$c === void 0 ? void 0 : _iframeRef$current3$c.__FR_ENGINE__) {
101
+ iframeRef.current.contentWindow.__FR_ENGINE__.init(_objectSpread({
102
+ settings: _objectSpread(_objectSpread({}, defaultSetting), settings),
103
+ widgets: widgets,
104
+ logo: {
105
+ title: 'XRender'
151
106
  }
152
- }
153
- } catch (error) {
154
- console.error('[SchemaBuilder] Error initializing engine:', error);
155
- if (retryCount < MAX_RETRIES) {
156
- setRetryCount(function (prev) {
157
- return prev + 1;
158
- });
159
- cleanupIframe();
160
- initIframe();
161
- }
107
+ }, restProps));
108
+ console.log('[SchemaBuilder] Engine initialized');
109
+ } else {
110
+ console.error('[SchemaBuilder] Engine not found in iframe');
111
+ retryIframe();
162
112
  }
163
- }, 500);
164
- }, [cleanupIframe, initIframe, retryCount, settings, widgets, restProps]);
113
+ }, 300);
114
+ };
165
115
  (0, _react.useEffect)(function () {
166
- initIframe();
167
- window.addEventListener('message', handleMessage);
116
+ // 等待容器渲染完成
117
+ var timer = setTimeout(function () {
118
+ _initIframe();
119
+ }, 100);
120
+ window.addEventListener('message', engineOnLoad);
168
121
  return function () {
169
- window.removeEventListener('message', handleMessage);
170
- cleanupIframe();
122
+ clearTimeout(timer);
123
+ window.removeEventListener('message', engineOnLoad);
124
+
125
+ // 清理 iframe
126
+ if (iframeRef.current && containerRef.current) {
127
+ try {
128
+ containerRef.current.removeChild(iframeRef.current);
129
+ } catch (e) {
130
+ // 忽略错误
131
+ }
132
+ }
171
133
  };
172
- }, [initIframe, handleMessage, cleanupIframe]);
134
+ }, []);
173
135
  return /*#__PURE__*/_react.default.createElement(_react.default.Fragment, null, loading && /*#__PURE__*/_react.default.createElement("div", {
174
136
  style: {
175
137
  position: 'absolute',
176
- top: 0,
177
- left: 0,
178
- right: 0,
179
- bottom: 0,
180
- display: 'flex',
181
- alignItems: 'center',
182
- justifyContent: 'center',
183
- backgroundColor: 'rgba(255, 255, 255, 0.8)',
138
+ top: '50%',
139
+ left: '50%',
140
+ transform: 'translate(-50%, -50%)',
141
+ padding: '20px',
142
+ backgroundColor: 'rgba(255, 255, 255, 0.9)',
143
+ borderRadius: '4px',
144
+ boxShadow: '0 2px 8px rgba(0,0,0,0.1)',
184
145
  zIndex: 10
185
146
  }
186
- }, /*#__PURE__*/_react.default.createElement("div", null, "\u52A0\u8F7D\u8868\u5355\u8BBE\u8BA1\u5668...", retryCount > 0 && /*#__PURE__*/_react.default.createElement("div", {
187
- style: {
188
- fontSize: '12px',
189
- color: '#666'
190
- }
191
- }, "\u91CD\u8BD5 ", retryCount, "/", MAX_RETRIES))), /*#__PURE__*/_react.default.createElement("div", {
147
+ }, "\u6B63\u5728\u52A0\u8F7D\u8868\u5355\u8BBE\u8BA1\u5668..."), /*#__PURE__*/_react.default.createElement("div", {
192
148
  ref: containerRef,
193
149
  style: {
194
150
  width: '100%',
195
- height: '100%'
151
+ height: '100%',
152
+ minHeight: '500px'
196
153
  }
197
154
  }));
198
155
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zzdadelu/schema-builder",
3
- "version": "1.0.0-alpha.54",
3
+ "version": "1.0.0-alpha.56",
4
4
  "description": "通过 JSON Schema 生成标准 Form,常用于自定义搭建配置界面生成",
5
5
  "keywords": [
6
6
  "Form",