Skip to content

Commit b4638bf

Browse files
authored
Reuse existing output (#1180)
1 parent 8231de0 commit b4638bf

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

js/interactive-examples.js

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import phpBinary from "/js/php-web.mjs";
22

3+
function generateExampleOutputTitle(phpVersion) {
4+
return "Output of the above example in PHP "+ phpVersion +":";
5+
}
6+
37
function createOutput(output) {
48
const container = document.createElement("div");
59
container.classList.add("screen", "example-contents");
610
const title = document.createElement("p");
7-
title.innerText = "Output (PHP "+ PHP.version +"):";
11+
title.innerText = generateExampleOutputTitle(PHP.version);
812
container.appendChild(title);
913
const div = document.createElement("div");
1014
div.classList.add("examplescode");
@@ -56,6 +60,15 @@ async function main() {
5660
return;
5761
}
5862

63+
const exampleTitleContainer = example.nextElementSibling;
64+
let exampleTitleParagraphElement = null;
65+
let exampleScreenPreElement = null;
66+
if (exampleTitleContainer !== null) {
67+
exampleTitleParagraphElement = exampleTitleContainer.querySelector("p")
68+
const exampleScreenContainer = exampleTitleContainer.nextElementSibling;
69+
exampleScreenPreElement = exampleScreenContainer.querySelector("pre");
70+
}
71+
5972
const code = phpcode.querySelector("code");
6073
code.spellcheck = false;
6174
code.setAttribute("contentEditable", true);
@@ -68,8 +81,13 @@ async function main() {
6881

6982
const runPhp = await PHP.loadPhp();
7083
runPhp(phpcode.innerText);
71-
lastOutput = createOutput(PHP.buffer.join(""));
72-
phpcode.parentNode.appendChild(lastOutput);
84+
if (exampleScreenPreElement !== null) {
85+
exampleTitleParagraphElement.innerText = generateExampleOutputTitle(PHP.version);
86+
exampleScreenPreElement.innerText = PHP.buffer.join("");
87+
} else {
88+
lastOutput = createOutput(PHP.buffer.join(""));
89+
phpcode.parentNode.appendChild(lastOutput);
90+
}
7391
PHP.buffer.length = 0;
7492
};
7593

0 commit comments

Comments
 (0)