Skip to content

Commit

Permalink
Reuse existing output (#1180)
Browse files Browse the repository at this point in the history
  • Loading branch information
Girgias authored Jan 31, 2025
1 parent 8231de0 commit b4638bf
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions js/interactive-examples.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import phpBinary from "/js/php-web.mjs";

function generateExampleOutputTitle(phpVersion) {
return "Output of the above example in PHP "+ phpVersion +":";
}

function createOutput(output) {
const container = document.createElement("div");
container.classList.add("screen", "example-contents");
const title = document.createElement("p");
title.innerText = "Output (PHP "+ PHP.version +"):";
title.innerText = generateExampleOutputTitle(PHP.version);
container.appendChild(title);
const div = document.createElement("div");
div.classList.add("examplescode");
Expand Down Expand Up @@ -56,6 +60,15 @@ async function main() {
return;
}

const exampleTitleContainer = example.nextElementSibling;
let exampleTitleParagraphElement = null;
let exampleScreenPreElement = null;
if (exampleTitleContainer !== null) {
exampleTitleParagraphElement = exampleTitleContainer.querySelector("p")
const exampleScreenContainer = exampleTitleContainer.nextElementSibling;
exampleScreenPreElement = exampleScreenContainer.querySelector("pre");
}

const code = phpcode.querySelector("code");
code.spellcheck = false;
code.setAttribute("contentEditable", true);
Expand All @@ -68,8 +81,13 @@ async function main() {

const runPhp = await PHP.loadPhp();
runPhp(phpcode.innerText);
lastOutput = createOutput(PHP.buffer.join(""));
phpcode.parentNode.appendChild(lastOutput);
if (exampleScreenPreElement !== null) {
exampleTitleParagraphElement.innerText = generateExampleOutputTitle(PHP.version);
exampleScreenPreElement.innerText = PHP.buffer.join("");
} else {
lastOutput = createOutput(PHP.buffer.join(""));
phpcode.parentNode.appendChild(lastOutput);
}
PHP.buffer.length = 0;
};

Expand Down

0 comments on commit b4638bf

Please sign in to comment.