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

[3/?] Add ability to run code examples in the playground: Fetch tutorial snippet from URL by file name #205

Merged
merged 8 commits into from
May 28, 2024
21 changes: 21 additions & 0 deletions static/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,23 @@
);
}

function fetchSnippet(session, result, snippet_file_name, do_evaluate, evaluateButton) {
session.setValue("// Loading snippet: https://github.com/ponylang/pony-tutorial/blob/cmain/code-samples/actors-behaviors.pony" + snippet_file_name + " ...");
shaedrich marked this conversation as resolved.
Show resolved Hide resolved
httpRequest("GET", "https://raw.githubusercontent.com/ponylang/pony-tutorial/main/code-samples/" + snippet_file_name, null, 200,
function (response) {
session.setValue(response);

if (do_evaluate) {
doEvaluate();
}
},
function (status, response) {
set_result(result, "<p class=error>Failed to fetch snippet" +
"<p class=error-explanation>Are you connected to the Internet?");
}
);
}

function getQueryParameters() {
var a = window.location.search.substr(1).split('&');
if (a === "") return {};
Expand Down Expand Up @@ -519,6 +536,10 @@
// fetchGist() must defer evaluation until after the content has been loaded
fetchGist(session, result, query.gist, query.run === "1", evaluateButton);
query.run = 0;
} else if ("snippet" in query) {
// fetchSnippet() must defer evaluation until after the content has been loaded
fetchSnippet(session, result, query.snippet, query.run === "1", evaluateButton);
query.run = 0;
} else {
var code = optionalLocalStorageGetItem("code");
if (code !== null) {
Expand Down
Loading