(null);
const html = useMemo(() => {
return panelHtml.replace("{{.HostPrefix}}", document.baseURI);
}, []);
function updateContent() {
if (!iFrameRef.current?.contentWindow) {
return;
}
iFrameRef.current.contentWindow.postMessage({
type: "html",
html: config.html,
script: config.script,
theme: document.getElementsByTagName("html")[0].dataset.theme,
});
}
useEffect(() => {
const iframe = iFrameRef.current;
if (!iframe) {
return;
}
iframe.addEventListener("load", updateContent);
updateContent();
return () => {
iframe.removeEventListener("load", updateContent);
};
}, [config.html, config.script]);
useEffect(() => {
const messageListener = (evt: any) => {
if (evt.source !== iFrameRef.current!.contentWindow) {
return;
}
const data = evt.data;
if (!data) {
return;
}
switch (data.type) {
case "syscall": {
const { id, name, args } = data;
editor.clientSystem
.localSyscall(name, args)
.then((result) => {
if (!iFrameRef.current?.contentWindow) {
// iFrame already went away
return;
}
iFrameRef.current!.contentWindow!.postMessage({
type: "syscall-response",
id,
result,
});
})
.catch((e: any) => {
if (!iFrameRef.current?.contentWindow) {
// iFrame already went away
return;
}
iFrameRef.current!.contentWindow!.postMessage({
type: "syscall-response",
id,
error: e.message,
});
});
break;
}
}
};
globalThis.addEventListener("message", messageListener);
return () => {
globalThis.removeEventListener("message", messageListener);
};
}, []);
return (
);
}
function ShadowPanel({ config }: { config: PanelConfig }) {
const panelRef = useRef(null);
const shadowRef = useRef(null);
function updateContent() {
const shadow = shadowRef.current;
if (!shadow) return;
const root = shadow.getElementById("panel-root");
if (!root) return;
root.innerHTML = "";
if (Array.isArray(config.html)) {
root.append(...config.html);
} else {
root.append(config.html as HTMLElement);
}
}
useEffect(() => {
if (!panelRef.current) return;
shadowRef.current = panelRef.current.attachShadow({ mode: "closed" });
const container = document.createElement("div");
container.id = "panel-root";
shadowRef.current.appendChild(container);
return () => {
shadowRef.current = null;
};
}, []);
useEffect(() => {
updateContent();
}, [config.html, config.script]);
return (
);
}