2020-08-14 04:03:07 +00:00
|
|
|
window.addEventListener('load', () => {
|
2020-08-15 00:09:59 +00:00
|
|
|
const tutorialWrapper = document.getElementById('tutorial-wrapper');
|
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
const ajax = new XMLHttpRequest();
|
|
|
|
ajax.onreadystatechange = () => {
|
|
|
|
if (ajax.readyState !== 4) { return; }
|
|
|
|
if (ajax.status === 404) {
|
|
|
|
reject("Sorry, the tutorial is not available in that language yet.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (ajax.status !== 200) {
|
|
|
|
reject("Something went wrong while loading the tutorial.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve(ajax.responseText);
|
|
|
|
};
|
2021-01-03 22:46:47 +00:00
|
|
|
ajax.open('GET', `${window.location.origin}/static/assets/tutorial/` +
|
|
|
|
`${tutorialWrapper.getAttribute('data-game')}/${tutorialWrapper.getAttribute('data-file')}_` +
|
|
|
|
`${tutorialWrapper.getAttribute('data-lang')}.md`, true);
|
2020-08-15 00:09:59 +00:00
|
|
|
ajax.send();
|
|
|
|
}).then((results) => {
|
|
|
|
// Populate page with HTML generated from markdown
|
2021-09-07 23:22:04 +00:00
|
|
|
showdown.setOption('tables', true);
|
|
|
|
showdown.setOption('strikethrough', true);
|
|
|
|
showdown.setOption('literalMidWordUnderscores', true);
|
2021-11-30 03:26:08 +00:00
|
|
|
showdown.setOption('disableForced4SpacesIndentedSublists', true);
|
2020-09-19 19:20:13 +00:00
|
|
|
tutorialWrapper.innerHTML += (new showdown.Converter()).makeHtml(results);
|
2020-12-03 06:55:06 +00:00
|
|
|
adjustHeaderWidth();
|
2020-08-14 04:03:07 +00:00
|
|
|
|
2020-08-15 00:09:59 +00:00
|
|
|
// Reset the id of all header divs to something nicer
|
|
|
|
const headers = Array.from(document.querySelectorAll('h1, h2, h3, h4, h5, h6'));
|
|
|
|
const scrollTargetIndex = window.location.href.search(/#[A-z0-9-_]*$/);
|
|
|
|
for (let i=0; i < headers.length; i++){
|
|
|
|
const headerId = headers[i].innerText.replace(/[ ]/g,'-').toLowerCase()
|
|
|
|
headers[i].setAttribute('id', headerId);
|
|
|
|
headers[i].addEventListener('click', () =>
|
|
|
|
window.location.href = window.location.href.substring(0, scrollTargetIndex) + `#${headerId}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Manually scroll the user to the appropriate header if anchor navigation is used
|
|
|
|
if (scrollTargetIndex > -1) {
|
|
|
|
try{
|
|
|
|
const scrollTarget = window.location.href.substring(scrollTargetIndex + 1);
|
|
|
|
document.getElementById(scrollTarget).scrollIntoView({ behavior: "smooth" });
|
|
|
|
} catch(error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}).catch((error) => {
|
2021-01-03 22:46:47 +00:00
|
|
|
console.error(error);
|
2020-08-15 00:09:59 +00:00
|
|
|
tutorialWrapper.innerHTML =
|
2021-01-03 22:46:47 +00:00
|
|
|
`<h2>This page is out of logic!</h2>
|
2020-08-15 00:09:59 +00:00
|
|
|
<h3>Click <a href="${window.location.origin}/tutorial">here</a> to return to safety.</h3>`;
|
|
|
|
});
|
2020-08-14 04:03:07 +00:00
|
|
|
});
|