@antv/dumi-theme-antv 0.7.6-beta.0 → 0.7.6-beta.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.
- package/dist/slots/LiveExample/index.js +57 -54
- package/package.json +1 -1
|
@@ -45,22 +45,25 @@ export default function LiveExample(props) {
|
|
|
45
45
|
}),
|
|
46
46
|
_useState8 = _slicedToArray(_useState7, 1),
|
|
47
47
|
containerId = _useState8[0];
|
|
48
|
-
var
|
|
48
|
+
var _useState9 = useState(false),
|
|
49
|
+
_useState10 = _slicedToArray(_useState9, 2),
|
|
50
|
+
hasSuccessfulRender = _useState10[0],
|
|
51
|
+
setHasSuccessfulRender = _useState10[1];
|
|
52
|
+
var clearContainer = useCallback(function () {
|
|
49
53
|
if (previewRef.current) {
|
|
50
54
|
previewRef.current.innerHTML = '';
|
|
51
55
|
}
|
|
52
56
|
}, []);
|
|
53
|
-
var
|
|
54
|
-
if (
|
|
57
|
+
var handleError = useCallback(function (message) {
|
|
58
|
+
if (hasSuccessfulRender) {
|
|
59
|
+
console.error('LiveExample Error:', message);
|
|
60
|
+
} else if (previewRef.current) {
|
|
55
61
|
previewRef.current.innerHTML = "\n <div style=\"color: #fb1716; padding: 8px; border-left: 3px solid #ff0000; padding-left: 12px;\">\n ".concat(message, "\n </div>\n ");
|
|
56
62
|
}
|
|
57
|
-
}, []);
|
|
63
|
+
}, [hasSuccessfulRender]);
|
|
58
64
|
var renderResult = useCallback(function (value) {
|
|
59
65
|
var container = previewRef.current;
|
|
60
|
-
if (!container)
|
|
61
|
-
console.warn('渲染结果时容器不存在');
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
66
|
+
if (!container) return;
|
|
64
67
|
if (value instanceof HTMLElement || value instanceof SVGElement || value && _typeof(value) === 'object' && value.nodeType) {
|
|
65
68
|
container.appendChild(value);
|
|
66
69
|
} else if (value !== null && value !== undefined) {
|
|
@@ -72,56 +75,69 @@ export default function LiveExample(props) {
|
|
|
72
75
|
|
|
73
76
|
// Inject模式:编译并注入代码
|
|
74
77
|
var executeInjectMode = useCallback(function (code) {
|
|
75
|
-
// 编译代码
|
|
76
78
|
var compiledCode;
|
|
77
79
|
try {
|
|
78
80
|
compiledCode = compile(code, '', true);
|
|
79
81
|
} catch (compileError) {
|
|
80
82
|
var errorMessage = compileError instanceof Error ? compileError.message : String(compileError);
|
|
81
|
-
|
|
83
|
+
handleError("Compile Error: ".concat(errorMessage));
|
|
82
84
|
return;
|
|
83
85
|
}
|
|
84
86
|
|
|
85
|
-
//
|
|
86
|
-
|
|
87
|
+
// 清理容器并准备新的渲染
|
|
88
|
+
clearContainer();
|
|
87
89
|
var container = previewRef.current;
|
|
88
90
|
container.innerHTML = "<div id=\"".concat(containerId, "\" style=\"width: 100%; height: 100%;\"></div>");
|
|
89
91
|
|
|
90
|
-
//
|
|
92
|
+
// 生成唯一的执行ID并创建脚本
|
|
93
|
+
var executionId = "exec-".concat(Date.now(), "-").concat(Math.random().toString(36).substr(2, 9));
|
|
91
94
|
var script = document.createElement('script');
|
|
92
95
|
var modifiedCode = compiledCode.replace(/'container'|"container"/g, "'".concat(containerId, "'"));
|
|
93
|
-
script.textContent = "\n try {\n ".concat(modifiedCode, "\n } catch (error) {\n console.error('\u811A\u672C\u6267\u884C\u65F6\u53D1\u751F\u9519\u8BEF:', error);\n const errorContainer = document.getElementById('").concat(containerId, "');\n
|
|
96
|
+
script.textContent = "\n try {\n ".concat(modifiedCode, "\n const successElement = document.createElement('div');\n successElement.id = 'execution-success-").concat(executionId, "';\n successElement.style.display = 'none';\n document.body.appendChild(successElement);\n } catch (error) {\n console.error('\u811A\u672C\u6267\u884C\u65F6\u53D1\u751F\u9519\u8BEF:', error);\n const errorElement = document.createElement('div');\n errorElement.id = 'execution-error-").concat(executionId, "';\n errorElement.textContent = error.message;\n errorElement.style.display = 'none';\n document.body.appendChild(errorElement);\n\n if (!").concat(hasSuccessfulRender, ") {\n const errorContainer = document.getElementById('").concat(containerId, "');\n if (errorContainer) {\n errorContainer.innerHTML = '<div style=\"color: #fb1716; padding: 8px; border-left: 3px solid #ff0000; padding-left: 12px;\">Runtime Error: ' + error.message + '</div>';\n }\n }\n }\n ");
|
|
94
97
|
document.head.appendChild(script);
|
|
95
98
|
|
|
96
|
-
//
|
|
99
|
+
// 检查执行结果并清理
|
|
97
100
|
setTimeout(function () {
|
|
101
|
+
var successElement = document.getElementById("execution-success-".concat(executionId));
|
|
102
|
+
var errorElement = document.getElementById("execution-error-".concat(executionId));
|
|
103
|
+
if (successElement) {
|
|
104
|
+
setHasSuccessfulRender(true);
|
|
105
|
+
document.body.removeChild(successElement);
|
|
106
|
+
} else if (errorElement) {
|
|
107
|
+
var errorMsg = errorElement.textContent || 'Unknown error';
|
|
108
|
+
if (hasSuccessfulRender) {
|
|
109
|
+
console.error('LiveExample Runtime Error:', errorMsg);
|
|
110
|
+
}
|
|
111
|
+
document.body.removeChild(errorElement);
|
|
112
|
+
}
|
|
98
113
|
if (script.parentNode) {
|
|
99
114
|
script.parentNode.removeChild(script);
|
|
100
115
|
}
|
|
101
116
|
}, 100);
|
|
102
|
-
}, [containerId,
|
|
117
|
+
}, [containerId, clearContainer, handleError, hasSuccessfulRender]);
|
|
103
118
|
|
|
104
119
|
// Direct模式:直接执行代码
|
|
105
120
|
var executeDirectMode = useCallback(function (code) {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
// 处理Promise结果
|
|
110
|
-
if (result && typeof result.then === 'function') {
|
|
111
|
-
result.then(function (resolvedValue) {
|
|
112
|
-
renderResult(resolvedValue);
|
|
113
|
-
}).catch(function (error) {
|
|
114
|
-
showError("Promise Error: ".concat(error.message || String(error)));
|
|
115
|
-
});
|
|
116
|
-
} else {
|
|
121
|
+
var executeAndRender = function executeAndRender(result) {
|
|
122
|
+
clearContainer();
|
|
117
123
|
renderResult(result);
|
|
124
|
+
setHasSuccessfulRender(true);
|
|
125
|
+
};
|
|
126
|
+
try {
|
|
127
|
+
var result = safeEval(code);
|
|
128
|
+
if (result && typeof result.then === 'function') {
|
|
129
|
+
result.then(executeAndRender).catch(function (error) {
|
|
130
|
+
handleError("Promise Error: ".concat(error.message || String(error)));
|
|
131
|
+
});
|
|
132
|
+
} else {
|
|
133
|
+
executeAndRender(result);
|
|
134
|
+
}
|
|
135
|
+
} catch (error) {
|
|
136
|
+
handleError("Execution Error: ".concat(error instanceof Error ? error.message : String(error)));
|
|
118
137
|
}
|
|
119
|
-
}, [
|
|
120
|
-
|
|
121
|
-
// 主执行函数
|
|
138
|
+
}, [clearContainer, renderResult, handleError]);
|
|
122
139
|
var execute = useCallback( /*#__PURE__*/function () {
|
|
123
140
|
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(code) {
|
|
124
|
-
var errorMessage;
|
|
125
141
|
return _regeneratorRuntime().wrap(function _callee$(_context) {
|
|
126
142
|
while (1) switch (_context.prev = _context.next) {
|
|
127
143
|
case 0:
|
|
@@ -129,25 +145,16 @@ export default function LiveExample(props) {
|
|
|
129
145
|
isLoading: true,
|
|
130
146
|
error: null
|
|
131
147
|
});
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
executeDirectMode(code);
|
|
137
|
-
}
|
|
138
|
-
setExecutionState({
|
|
139
|
-
isLoading: false,
|
|
140
|
-
error: null
|
|
141
|
-
});
|
|
142
|
-
} catch (error) {
|
|
143
|
-
errorMessage = error instanceof Error ? error.message : String(error);
|
|
144
|
-
setExecutionState({
|
|
145
|
-
isLoading: false,
|
|
146
|
-
error: errorMessage
|
|
147
|
-
});
|
|
148
|
-
showError("Execution Error: ".concat(errorMessage));
|
|
148
|
+
if (inject) {
|
|
149
|
+
executeInjectMode(code);
|
|
150
|
+
} else {
|
|
151
|
+
executeDirectMode(code);
|
|
149
152
|
}
|
|
150
|
-
|
|
153
|
+
setExecutionState({
|
|
154
|
+
isLoading: false,
|
|
155
|
+
error: null
|
|
156
|
+
});
|
|
157
|
+
case 3:
|
|
151
158
|
case "end":
|
|
152
159
|
return _context.stop();
|
|
153
160
|
}
|
|
@@ -156,17 +163,13 @@ export default function LiveExample(props) {
|
|
|
156
163
|
return function (_x) {
|
|
157
164
|
return _ref.apply(this, arguments);
|
|
158
165
|
};
|
|
159
|
-
}(), [inject, executeInjectMode, executeDirectMode
|
|
160
|
-
|
|
161
|
-
// 工具栏高度更新
|
|
166
|
+
}(), [inject, executeInjectMode, executeDirectMode]);
|
|
162
167
|
var updateToolbarHeight = useCallback(function () {
|
|
163
168
|
if (!toolbarRef.current || !previewRef.current || !codeRef.current) return;
|
|
164
169
|
var codeHeight = codeRef.current.clientHeight;
|
|
165
170
|
var previewHeight = previewRef.current.clientHeight;
|
|
166
171
|
toolbarRef.current.style.height = "".concat(codeHeight + previewHeight, "px");
|
|
167
172
|
}, []);
|
|
168
|
-
|
|
169
|
-
// 事件处理函数
|
|
170
173
|
var toggleCodeVisibility = useCallback(function () {
|
|
171
174
|
var visible = !isCodeVisible;
|
|
172
175
|
setIsCodeVisible(visible);
|