Files
oxygen-ui/src/components/Playground/Output/Preview/iframe.html

57 lines
1.7 KiB
HTML

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Preview</title>
<style id="global-css-variable"></style>
</head>
<body>
<script>
window.addEventListener("message", ({ data }) => {
if (data?.type === "UPDATE") {
// Record old styles that need to be removed
const appStyleElement = document.querySelectorAll("style:not(:is(style[id$=\"_oxygen_base_style.css\"], style[id=\"global-css-variable\"]))") || [];
// Remove old app
const appSrcElement = document.querySelector("#appSrc");
const oldSrc = appSrcElement.getAttribute("src");
appSrcElement.remove();
// Add new app
const script = document.createElement("script");
script.src = URL.createObjectURL(
new Blob([data.data.compiledCode], {
type: "application/javascript"
})
);
script.id = "appSrc";
script.type = "module";
script.onload = () => {
// Remove old style
appStyleElement.forEach(element => {
element.remove();
});
};
document.body.appendChild(script);
URL.revokeObjectURL(oldSrc);
}
if (data?.type === "GLOBAL_VARIABLES") {
document.querySelector("#global-css-variable").textContent = data.data.globalCssVariables;
for (const key in data.data.globalJsVariables) {
globalThis[key] = data.data.globalJsVariables[key]
}
}
});
</script>
<script type="module" id="appSrc"></script>
<div id="root">
<div
style="position:absolute;top: 0;left:0;width:100%;height:100%;display: flex;justify-content: center;align-items: center;">
Loading...
</div>
</div>
</body>
</html>