<script>
document.querySelectorAll(".repl").forEach((replElement) => {
const iframeElement = replElement.querySelector("iframe");
const id = replElement.getAttribute("data-id");
const lang = replElement.getAttribute("data-lang");
const code = getCode(replElement.nextElementSibling);
const readonly = replElement.getAttribute("data-readonly") === "true";
const editorTheme = replElement.getAttribute("data-editor-theme") ?? "";
const editorDarkTheme = replElement.getAttribute("data-editor-dark-theme") ?? "";
let theme = mapTheme(localStorage.getItem("mdbook-theme") || document.querySelector(".theme.theme-selected")?.id);
const postmessage = (msg) => iframeElement.contentWindow.postMessage({ repl: msg }, "*");
function mapTheme(bookTheme) {
return bookTheme === "light" || bookTheme === "rust" ? "light" : "dark";
}
function getCode(element) {
const code = element.cloneNode(true);
code.querySelectorAll(".boring").forEach((boring) => boring.remove());
return code.innerText.trim();
}
window.addEventListener("message", (event) => {
const repl = event.data.repl;
if (event.source === window || !repl) return;
if (repl.id === "") {
postmessage({ id, editor: { theme, editorTheme, editorDarkTheme, lang, code, readonly, defaultCode: code } });
return;
}
if (repl.id !== id) return;
replElement.style.height = repl.dimensions.height + "px";
iframeElement.classList.remove("hide");
replElement.nextElementSibling.style.display = "none";
});
document.querySelectorAll("button[role='menuitem'].theme").forEach((btn) => {
btn.addEventListener("click", (event) => {
theme = mapTheme(event.target.id);
postmessage({ id, editor: { theme } });
});
});
});
</script>