// if localhost, use local playground
function getLabelStudioPlaygroundUrl() {
if (window.location.hostname === "localhost") {
return "http://localhost:4200/";
}
if (window.location.hostname === "labelstud.io") {
return "https://labelstud.io/playground-app/";
}
// This is for preview deployments
return "https://label-studio-playground.netlify.app/";
}
function normalizeNewlines(text) {
return text.replace(/(\r\n|\r)/gm, "\n");
}
function encodeConfig(text) {
return encodeURIComponent(normalizeNewlines(text));
}
function editorIframe(config, modal, id, inline = false) {
// generate new iframe
const iframeTemplate = ``;
modal.insertAdjacentHTML("beforeend", iframeTemplate);
const iframe = document.querySelector(`#render-editor-${id}`);
const spinner = modal.querySelector(".render-editor-loader");
iframe.addEventListener("load", function () {
if (spinner) spinner.style.display = "none";
iframe.style.display = "block";
});
const templateValue = encodeConfig(config);
iframe.src = `${getLabelStudioPlaygroundUrl()}?config=${templateValue}&mode=${inline ? "preview-inline" : "preview"}`;
}
function showRenderEditor(config) {
const id = `id${Math.random().toString(16).slice(2)}`;
const body = document.querySelector("body");
const modalTemplate = `
`;
body.insertAdjacentHTML("beforeend", modalTemplate);
const modal = document.querySelector(`#preview-wrapper-${id}`);
editorIframe(config, modal, id);
}
(function () {
const codeBlocks = document.querySelectorAll('code[class*="hljs"]');
const handleCopy = (event) => {
event.preventDefault();
const CheckIcon = event.currentTarget.querySelector(".code-block-copy-check-icon");
const CopyIcon = event.currentTarget.querySelector(".code-block-copy-copy-icon");
const text = event.currentTarget.nextElementSibling.textContent;
navigator.clipboard.writeText(text).then(() => {
CopyIcon.style.display = "none";
CheckIcon.style.display = "block";
// Hide after 3 seconds
setTimeout(() => {
CopyIcon.style.display = "block";
CheckIcon.style.display = "none";
}, 3000);
});
};
const addCopyCodeButton = (codeBlock, language) => {
const pre = codeBlock.parentElement;
const htmlTemplate = `
`;
pre.insertAdjacentHTML("afterBegin", htmlTemplate);
const button = pre.querySelector(".code-block-copy-button");
button.addEventListener("click", handleCopy);
};
const addPlaygroundButtons = (codeBlock) => {
const pre = codeBlock.parentElement;
const code = codeBlock.textContent;
const htmlTemplate = `
`;
pre.insertAdjacentHTML("beforeend", htmlTemplate);
const openPreviewButton = pre.querySelector(".code-block-open-preview");
openPreviewButton.addEventListener("click", () => showRenderEditor(code));
const inlinePlayground = document.querySelector("#main-preview");
if (inlinePlayground) editorIframe(code, inlinePlayground, "inline", true);
};
const enhanceCodeBlocks = (codeBlock) => {
const language = codeBlock.classList[1];
if (language === "html") addPlaygroundButtons(codeBlock);
addCopyCodeButton(codeBlock, language);
};
codeBlocks.forEach((block) => enhanceCodeBlocks(block));
})();