Skip to content

Commit 2c46224

Browse files
shaedrichmfelschejemc
authored
Add ability to run code examples in the playground: Fetch tutorial snippet from URL by file name (#205)
See ponylang/pony-tutorial#340 > [!WARNING] Will only work, after #222 has been merged. Example: https://playground.ponylang.io/?snippet=hello-world-main.pony Co-authored-by: Joe Eli McIlvain <[email protected]> --------- Co-authored-by: Matthias Wahl <[email protected]> Co-authored-by: Joe Eli McIlvain <[email protected]>
1 parent a08532f commit 2c46224

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

Diff for: static/web.js

+32-1
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@
320320
}
321321

322322
/**
323-
*
323+
* Fetches a gist from the [gist.github.com](https://gist.github.com/) and loads it into the code editor
324324
* @param {Ace.EditSession} session (see [ace.Ace.Editor::getSession](https://ajaxorg.github.io/ace-api-docs/interfaces/ace.Ace.Editor.html#getSession))
325325
* @param {HTMLDivElement} result
326326
* @param {String} gist_id
@@ -358,6 +358,33 @@
358358
);
359359
}
360360

361+
/**
362+
* Fetches a code snippet from the [`ponylang/pony-tutorial`](https://github.com/ponylang/pony-tutorial) repository on GitHub
363+
* and loads it into the code editor
364+
* @param {Ace.EditSession} session (see [ace.Ace.Editor::getSession](https://ajaxorg.github.io/ace-api-docs/interfaces/ace.Ace.Editor.html#getSession))
365+
* @param {HTMLDivElement} result
366+
* @param {String} gist_id
367+
* @param {bool} do_evaluate
368+
* @param {HTMLButtonElement} evaluateButton
369+
* @return {void}
370+
*/
371+
function fetchSnippet(session, result, snippet_file_name, do_evaluate, evaluateButton) {
372+
session.setValue("// Loading snippet: https://github.com/ponylang/pony-tutorial/blob/main/code-samples/" + snippet_file_name + " ...");
373+
httpRequest("GET", "https://raw.githubusercontent.com/ponylang/pony-tutorial/main/code-samples/" + snippet_file_name, null, 200,
374+
function (response) {
375+
session.setValue(response);
376+
377+
if (do_evaluate) {
378+
doEvaluate();
379+
}
380+
},
381+
function (status, response) {
382+
set_result(result, "<p class=error>Failed to fetch snippet" +
383+
"<p class=error-explanation>Are you connected to the Internet?");
384+
}
385+
);
386+
}
387+
361388
/**
362389
* Get URL query parameters as Object
363390
* @returns {URLSearchParams}
@@ -620,6 +647,10 @@
620647
// fetchGist() must defer evaluation until after the content has been loaded
621648
fetchGist(session, result, query.get("gist"), query.get("run") === "1", evaluateButton);
622649
query.set("run", 0);
650+
} else if (query.has("snippet")) {
651+
// fetchSnippet() must defer evaluation until after the content has been loaded
652+
fetchSnippet(session, result, query.get("snippet"), query.get("run") === "1", evaluateButton);
653+
query.set("run", 0);
623654
} else {
624655
var code = optionalLocalStorageGetItem("code");
625656
if (code !== null) {

0 commit comments

Comments
 (0)