Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run examples with php wasm #1097

Merged
merged 6 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/footer.inc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ if (!empty($_SERVER['BASE_PAGE'])
echo '<script src="/cached.php?t=' . @filemtime($path) . '&amp;f=/js/' . $filename . '"></script>' . "\n";
}
?>
<?php
$jsfiles = ["interactive-examples.js"];
foreach ($jsfiles as $filename) {
$path = dirname(__DIR__) . '/js/' . $filename;
echo '<script type="module" src="/cached.php?t=' . @filemtime($path) . '&amp;f=/js/' . $filename . '"></script>' . "\n";
}
?>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe that this deserves only one line?


<a id="toTop" href="javascript:;"><span id="toTopHover"></span><img width="40" height="40" alt="To Top" src="/images/[email protected]"></a>

Expand Down
62 changes: 62 additions & 0 deletions js/interactive-examples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import phpBinary from "/js/php-web.mjs";

function createOutput(output) {
const container = document.createElement('div');
container.classList.add('screen', 'example-contents');
const title = document.createElement('p');
title.innerText = 'Output: ';
container.appendChild(title)
const div = document.createElement('div');
div.classList.add('examplescode');
container.appendChild(div);
const pre = document.createElement('pre');
pre.classList.add('examplescode');
pre.innerText = output;
div.appendChild(pre)
return container;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const container = document.createElement('div');
container.classList.add('screen', 'example-contents');
const title = document.createElement('p');
title.innerText = 'Output: ';
container.appendChild(title)
const div = document.createElement('div');
div.classList.add('examplescode');
container.appendChild(div);
const pre = document.createElement('pre');
pre.classList.add('examplescode');
pre.innerText = output;
div.appendChild(pre)
return container;
const container = document.createElement('div');
container.classList.add('screen', 'example-contents');
const title = document.createElement('p');
title.innerText = 'Output: ';
container.appendChild(title);
const div = document.createElement('div');
div.classList.add('examplescode');
container.appendChild(div);
const pre = document.createElement('pre');
pre.classList.add('examplescode');
pre.innerText = output;
div.appendChild(pre);
return container;

CS

}

async function main() {
const buffer = [];
const { ccall } = await phpBinary({
print(data) {
if (!data) {
return;
}

if (buffer.length) {
buffer.push('\n');
}
buffer.push(data);
}
})

console.log("PHP wasm %s loaded.", ccall("phpw_exec", "string", ["string"], ["phpversion();"]));
let lastOutput = null

document.querySelectorAll('.example').forEach((example) => {
const button = document.createElement('button');
button.setAttribute('type', 'button')
const phpcode = example.querySelector('.phpcode');

const code = phpcode.querySelector('pre code')
soyuka marked this conversation as resolved.
Show resolved Hide resolved
code.setAttribute('contentEditable', true)

button.innerText = 'Run code';
button.onclick = function() {
if (lastOutput && lastOutput.parentNode) {
lastOutput.remove()
}

ccall("phpw_run", null, ["string"], ['?>' + phpcode.innerText]);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this use .innerText or .textContent here ? innerText is impacted by the styles of the page

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no idea 🤷

lastOutput = createOutput(buffer.join(''))
phpcode.parentNode.appendChild(lastOutput);
buffer.length = 0;
};

phpcode.before(button);
});

}

main()
16 changes: 16 additions & 0 deletions js/php-web.mjs

Large diffs are not rendered by default.

Binary file added js/php-web.wasm
Binary file not shown.
Loading