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

Add docs button to playground #230

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
9 changes: 5 additions & 4 deletions static/web.css
Original file line number Diff line number Diff line change
Expand Up @@ -119,18 +119,18 @@
margin-right: 0.5em;
}

button:first-child {
button:first-child, a.button:first-child {
border-top-left-radius: 4px;
border-bottom-left-radius: 4px;
border-left-width: 1px;
}

button:last-child {
button:last-child, a.button:last-child {
border-top-right-radius: 4px;
border-bottom-right-radius: 4px;
}

button {
button, a.button {
color: #333;
background-color: #fff;
border: 1px solid rgba(153, 153, 153, 0.5);
Expand All @@ -149,7 +149,8 @@ label {
cursor: pointer;
}

button:hover, button:focus, button:active {
button:hover, button:focus, button:active,
a.button:hover, a.button:focus, a.button:active {
background: #ebebeb;
}

Expand Down
1 change: 1 addition & 0 deletions static/web.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
type="button" id="asm" title="Compile to ASM">ASM</button><button type="button" id="llvm-ir"
title="Compile to LLVM IR">LLVM IR</button></div><wbr>
<div><button type="button" id="gist" title="Share a link to your code via Gist">Share</button></div><wbr>
<div><a class="button" id="docs" title="Return to the tutorial chapter" target="_blank" hidden>&larr;&ensp;Docs "<output></output>"</a></div><wbr>
<div class="right-c-e"><button type="button" id="configure-editor"><span>Configure editor</span></button>
<div class="dropdown">
<p><label for="keyboard">Keyboard bindings:</label>
Expand Down
19 changes: 17 additions & 2 deletions static/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -368,12 +368,25 @@
* @param {HTMLButtonElement} evaluateButton
* @return {void}
*/
function fetchSnippet(session, result, snippet_file_name, do_evaluate, evaluateButton) {
function fetchSnippet(session, result, snippet_file_name, do_evaluate, evaluateButton, docsButton) {
session.setValue("// Loading snippet: https://github.com/ponylang/pony-tutorial/blob/main/code-samples/" + snippet_file_name + " ...");
httpRequest("GET", "https://raw.githubusercontent.com/ponylang/pony-tutorial/main/code-samples/" + snippet_file_name, null, 200,
function (response) {
session.setValue(response);

if (query.has('docs') && /^([a-z\/-]+)$/.test(query.get('docs'))) {
const docsUrl = `https://tutorial.ponylang.io/${query.get('docs')}`
fetch(docsUrl)
.then(res => res.text())
.then(htmlString => (new DOMParser()).parseFromString(htmlString, "text/html"))
.then(htmlDocument => {
docsButton.querySelector('output').textContent = htmlDocument.querySelector('head > title').textContent
})
docsButton.removeAttribute('hidden')
docsButton.querySelector('output').textContent = query.get('docs') // placeholder, until title is loaded
docsButton.href = docsUrl
}

if (do_evaluate) {
doEvaluate();
}
Expand Down Expand Up @@ -481,6 +494,7 @@
let asmButton;
let irButton;
let gistButton;
let docsButton;
let configureEditorButton;
let result;
let clearResultButton;
Expand Down Expand Up @@ -597,6 +611,7 @@
asmButton = document.getElementById("asm");
irButton = document.getElementById("llvm-ir");
gistButton = document.getElementById("gist");
docsButton = document.getElementById("docs");
configureEditorButton = document.getElementById("configure-editor");
result = document.getElementById("result").firstElementChild;
clearResultButton = document.getElementById("clear-result");
Expand Down Expand Up @@ -649,7 +664,7 @@
query.set("run", 0);
} else if (query.has("snippet")) {
// fetchSnippet() must defer evaluation until after the content has been loaded
fetchSnippet(session, result, query.get("snippet"), query.get("run") === "1", evaluateButton);
fetchSnippet(session, result, query.get("snippet"), query.get("run") === "1", evaluateButton, docsButton);
query.set("run", 0);
} else {
var code = optionalLocalStorageGetItem("code");
Expand Down