2018-04-09 12:10:44 +08:00
|
|
|
"use strict";
|
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
// Fix back button cache problem
|
|
|
|
window.onunload = function () { };
|
2018-01-13 02:44:13 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
// Global variable, shared between modules
|
|
|
|
function playpen_text(playpen) {
|
|
|
|
let code_block = playpen.querySelector("code");
|
2015-08-06 00:28:59 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
if (window.ace && code_block.classList.contains("editable")) {
|
|
|
|
let editor = window.ace.edit(code_block);
|
|
|
|
return editor.getValue();
|
2017-06-29 12:35:20 +08:00
|
|
|
} else {
|
2018-01-21 23:01:57 +08:00
|
|
|
return code_block.textContent;
|
2017-06-29 12:35:20 +08:00
|
|
|
}
|
2018-01-21 23:01:57 +08:00
|
|
|
}
|
2017-08-08 06:36:38 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
(function codeSnippets() {
|
|
|
|
// Hide Rust code lines prepended with a specific character
|
|
|
|
var hiding_character = "#";
|
2018-06-15 02:09:20 +08:00
|
|
|
|
2018-08-02 01:38:36 +08:00
|
|
|
function fetch_with_timeout(url, options, timeout = 6000) {
|
|
|
|
return Promise.race([
|
|
|
|
fetch(url, options),
|
|
|
|
new Promise((_, reject) => setTimeout(() => reject(new Error('timeout')), timeout))
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-06-15 02:09:20 +08:00
|
|
|
var playpens = Array.from(document.querySelectorAll(".playpen"));
|
|
|
|
if (playpens.length > 0) {
|
2018-08-02 01:38:36 +08:00
|
|
|
fetch_with_timeout("https://play.rust-lang.org/meta/crates", {
|
2018-06-15 02:09:20 +08:00
|
|
|
headers: {
|
|
|
|
'Content-Type': "application/json",
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
mode: 'cors',
|
|
|
|
})
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(response => {
|
|
|
|
// get list of crates available in the rust playground
|
|
|
|
let playground_crates = response.crates.map(item => item["id"]);
|
|
|
|
playpens.forEach(block => handle_crate_list_update(block, playground_crates));
|
|
|
|
});
|
|
|
|
}
|
2015-08-06 00:28:59 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
function handle_crate_list_update(playpen_block, playground_crates) {
|
|
|
|
// update the play buttons after receiving the response
|
|
|
|
update_play_button(playpen_block, playground_crates);
|
2017-06-09 21:51:30 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
// and install on change listener to dynamically update ACE editors
|
|
|
|
if (window.ace) {
|
|
|
|
let code_block = playpen_block.querySelector("code");
|
|
|
|
if (code_block.classList.contains("editable")) {
|
|
|
|
let editor = window.ace.edit(code_block);
|
|
|
|
editor.addEventListener("change", function (e) {
|
|
|
|
update_play_button(playpen_block, playground_crates);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2015-12-27 11:45:50 +08:00
|
|
|
}
|
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
// updates the visibility of play button based on `no_run` class and
|
|
|
|
// used crates vs ones available on http://play.rust-lang.org
|
|
|
|
function update_play_button(pre_block, playground_crates) {
|
|
|
|
var play_button = pre_block.querySelector(".play-button");
|
2017-10-24 08:31:56 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
// skip if code is `no_run`
|
|
|
|
if (pre_block.querySelector('code').classList.contains("no_run")) {
|
|
|
|
play_button.classList.add("hidden");
|
2017-10-24 08:31:56 +08:00
|
|
|
return;
|
2018-01-21 23:01:57 +08:00
|
|
|
}
|
2017-10-24 08:31:56 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
// get list of `extern crate`'s from snippet
|
|
|
|
var txt = playpen_text(pre_block);
|
|
|
|
var re = /extern\s+crate\s+([a-zA-Z_0-9]+)\s*;/g;
|
|
|
|
var snippet_crates = [];
|
2018-04-09 12:10:44 +08:00
|
|
|
var item;
|
2018-01-21 23:01:57 +08:00
|
|
|
while (item = re.exec(txt)) {
|
|
|
|
snippet_crates.push(item[1]);
|
2017-10-24 08:31:56 +08:00
|
|
|
}
|
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
// check if all used crates are available on play.rust-lang.org
|
|
|
|
var all_available = snippet_crates.every(function (elem) {
|
|
|
|
return playground_crates.indexOf(elem) > -1;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (all_available) {
|
|
|
|
play_button.classList.remove("hidden");
|
2015-09-11 07:16:29 +08:00
|
|
|
} else {
|
2018-01-21 23:01:57 +08:00
|
|
|
play_button.classList.add("hidden");
|
2015-09-11 07:16:29 +08:00
|
|
|
}
|
2018-01-21 23:01:57 +08:00
|
|
|
}
|
2015-09-11 07:16:29 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
function run_rust_code(code_block) {
|
|
|
|
var result_block = code_block.querySelector(".result");
|
|
|
|
if (!result_block) {
|
|
|
|
result_block = document.createElement('code');
|
|
|
|
result_block.className = 'result hljs language-bash';
|
2018-01-15 21:26:53 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
code_block.append(result_block);
|
2017-06-23 19:18:06 +08:00
|
|
|
}
|
2015-09-11 07:16:29 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
let text = playpen_text(code_block);
|
2017-08-08 06:36:38 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
var params = {
|
2018-08-02 01:38:36 +08:00
|
|
|
version: "stable",
|
|
|
|
optimize: "0",
|
|
|
|
code: text
|
2018-07-31 07:37:45 +08:00
|
|
|
};
|
2017-08-08 06:36:38 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
if (text.indexOf("#![feature") !== -1) {
|
2018-08-02 01:38:36 +08:00
|
|
|
params.version = "nightly";
|
2018-01-21 23:01:57 +08:00
|
|
|
}
|
2017-08-08 06:36:38 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
result_block.innerText = "Running...";
|
2017-08-08 06:36:38 +08:00
|
|
|
|
2018-08-02 01:38:36 +08:00
|
|
|
fetch_with_timeout("https://play.rust-lang.org/evaluate.json", {
|
2018-01-21 23:01:57 +08:00
|
|
|
headers: {
|
|
|
|
'Content-Type': "application/json",
|
|
|
|
},
|
|
|
|
method: 'POST',
|
|
|
|
mode: 'cors',
|
|
|
|
body: JSON.stringify(params)
|
2018-08-02 01:38:36 +08:00
|
|
|
})
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(response => result_block.innerText = response.result)
|
|
|
|
.catch(error => result_block.innerText = "Playground Communication: " + error.message);
|
2018-01-21 23:01:57 +08:00
|
|
|
}
|
2018-01-16 20:29:20 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
// Syntax highlighting Configuration
|
|
|
|
hljs.configure({
|
|
|
|
tabReplace: ' ', // 4 spaces
|
|
|
|
languages: [], // Languages used for auto-detection
|
|
|
|
});
|
2015-09-14 14:39:53 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
if (window.ace) {
|
|
|
|
// language-rust class needs to be removed for editable
|
|
|
|
// blocks or highlightjs will capture events
|
|
|
|
Array
|
|
|
|
.from(document.querySelectorAll('code.editable'))
|
|
|
|
.forEach(function (block) { block.classList.remove('language-rust'); });
|
2015-09-14 14:39:53 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
Array
|
|
|
|
.from(document.querySelectorAll('code:not(.editable)'))
|
|
|
|
.forEach(function (block) { hljs.highlightBlock(block); });
|
|
|
|
} else {
|
|
|
|
Array
|
|
|
|
.from(document.querySelectorAll('code'))
|
|
|
|
.forEach(function (block) { hljs.highlightBlock(block); });
|
2015-09-14 14:39:53 +08:00
|
|
|
}
|
2015-12-29 06:52:05 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
// Adding the hljs class gives code blocks the color css
|
|
|
|
// even if highlighting doesn't apply
|
|
|
|
Array
|
|
|
|
.from(document.querySelectorAll('code'))
|
|
|
|
.forEach(function (block) { block.classList.add('hljs'); });
|
2015-12-29 06:52:05 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
Array.from(document.querySelectorAll("code.language-rust")).forEach(function (block) {
|
2015-12-29 20:08:25 +08:00
|
|
|
|
2018-01-13 02:44:13 +08:00
|
|
|
var code_block = block;
|
|
|
|
var pre_block = block.parentNode;
|
2015-12-29 20:08:25 +08:00
|
|
|
// hide lines
|
2018-01-13 02:44:13 +08:00
|
|
|
var lines = code_block.innerHTML.split("\n");
|
2015-12-29 19:26:32 +08:00
|
|
|
var first_non_hidden_line = false;
|
2015-12-29 20:08:25 +08:00
|
|
|
var lines_hidden = false;
|
2018-07-04 20:54:55 +08:00
|
|
|
var trimmed_line = "";
|
2015-12-29 06:52:05 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
for (var n = 0; n < lines.length; n++) {
|
2018-07-04 20:54:55 +08:00
|
|
|
trimmed_line = lines[n].trim();
|
|
|
|
if (trimmed_line[0] == hiding_character && trimmed_line[1] != hiding_character) {
|
2018-01-21 23:01:57 +08:00
|
|
|
if (first_non_hidden_line) {
|
2017-05-18 06:04:09 +08:00
|
|
|
lines[n] = "<span class=\"hidden\">" + "\n" + lines[n].replace(/(\s*)# ?/, "$1") + "</span>";
|
2015-12-29 20:08:25 +08:00
|
|
|
}
|
|
|
|
else {
|
2018-01-21 23:01:57 +08:00
|
|
|
lines[n] = "<span class=\"hidden\">" + lines[n].replace(/(\s*)# ?/, "$1") + "\n" + "</span>";
|
2015-12-29 20:08:25 +08:00
|
|
|
}
|
|
|
|
lines_hidden = true;
|
2015-12-29 19:26:32 +08:00
|
|
|
}
|
2018-01-21 23:01:57 +08:00
|
|
|
else if (first_non_hidden_line) {
|
2015-12-29 19:26:32 +08:00
|
|
|
lines[n] = "\n" + lines[n];
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
first_non_hidden_line = true;
|
2015-12-29 06:52:05 +08:00
|
|
|
}
|
2018-07-04 20:54:55 +08:00
|
|
|
if (trimmed_line[0] == hiding_character && trimmed_line[1] == hiding_character) {
|
|
|
|
lines[n] = lines[n].replace("##", "#")
|
|
|
|
}
|
2015-12-29 06:52:05 +08:00
|
|
|
}
|
2018-01-13 02:44:13 +08:00
|
|
|
code_block.innerHTML = lines.join("");
|
2015-12-29 20:08:25 +08:00
|
|
|
|
|
|
|
// If no lines were hidden, return
|
2018-01-21 23:01:57 +08:00
|
|
|
if (!lines_hidden) { return; }
|
2015-12-29 20:08:25 +08:00
|
|
|
|
2018-01-13 02:44:13 +08:00
|
|
|
var buttons = document.createElement('div');
|
|
|
|
buttons.className = 'buttons';
|
2018-01-27 18:52:47 +08:00
|
|
|
buttons.innerHTML = "<button class=\"fa fa-expand\" title=\"Show hidden lines\" aria-label=\"Show hidden lines\"></button>";
|
2015-12-29 20:08:25 +08:00
|
|
|
|
2018-01-13 02:44:13 +08:00
|
|
|
// add expand button
|
2018-04-09 12:10:44 +08:00
|
|
|
pre_block.insertBefore(buttons, pre_block.firstChild);
|
2018-01-13 02:44:13 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
pre_block.querySelector('.buttons').addEventListener('click', function (e) {
|
2018-01-13 02:44:13 +08:00
|
|
|
if (e.target.classList.contains('fa-expand')) {
|
|
|
|
var lines = pre_block.querySelectorAll('span.hidden');
|
|
|
|
|
|
|
|
e.target.classList.remove('fa-expand');
|
|
|
|
e.target.classList.add('fa-compress');
|
|
|
|
e.target.title = 'Hide lines';
|
2018-01-27 18:52:47 +08:00
|
|
|
e.target.setAttribute('aria-label', e.target.title);
|
2018-01-13 02:44:13 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
Array.from(lines).forEach(function (line) {
|
2018-01-13 02:44:13 +08:00
|
|
|
line.classList.remove('hidden');
|
|
|
|
line.classList.add('unhidden');
|
|
|
|
});
|
|
|
|
} else if (e.target.classList.contains('fa-compress')) {
|
|
|
|
var lines = pre_block.querySelectorAll('span.unhidden');
|
|
|
|
|
|
|
|
e.target.classList.remove('fa-compress');
|
|
|
|
e.target.classList.add('fa-expand');
|
|
|
|
e.target.title = 'Show hidden lines';
|
2018-01-27 18:52:47 +08:00
|
|
|
e.target.setAttribute('aria-label', e.target.title);
|
2018-01-13 02:44:13 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
Array.from(lines).forEach(function (line) {
|
2018-01-13 02:44:13 +08:00
|
|
|
line.classList.remove('unhidden');
|
|
|
|
line.classList.add('hidden');
|
|
|
|
});
|
2015-12-29 20:08:25 +08:00
|
|
|
}
|
|
|
|
});
|
2015-12-29 06:52:05 +08:00
|
|
|
});
|
2018-01-13 02:44:13 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
Array.from(document.querySelectorAll('pre code')).forEach(function (block) {
|
2018-01-13 02:44:13 +08:00
|
|
|
var pre_block = block.parentNode;
|
2018-01-21 23:01:57 +08:00
|
|
|
if (!pre_block.classList.contains('playpen')) {
|
2018-01-13 02:44:13 +08:00
|
|
|
var buttons = pre_block.querySelector(".buttons");
|
2018-01-21 23:01:57 +08:00
|
|
|
if (!buttons) {
|
2018-01-13 02:44:13 +08:00
|
|
|
buttons = document.createElement('div');
|
|
|
|
buttons.className = 'buttons';
|
2018-04-09 12:10:44 +08:00
|
|
|
pre_block.insertBefore(buttons, pre_block.firstChild);
|
2017-09-19 02:55:59 +08:00
|
|
|
}
|
2018-01-13 02:44:13 +08:00
|
|
|
|
|
|
|
var clipButton = document.createElement('button');
|
|
|
|
clipButton.className = 'fa fa-copy clip-button';
|
|
|
|
clipButton.title = 'Copy to clipboard';
|
2018-01-27 18:52:47 +08:00
|
|
|
clipButton.setAttribute('aria-label', clipButton.title);
|
2018-01-13 02:44:13 +08:00
|
|
|
clipButton.innerHTML = '<i class=\"tooltiptext\"></i>';
|
|
|
|
|
2018-04-09 12:10:44 +08:00
|
|
|
buttons.insertBefore(clipButton, buttons.firstChild);
|
2017-09-19 02:55:59 +08:00
|
|
|
}
|
|
|
|
});
|
2015-12-29 06:52:05 +08:00
|
|
|
|
2016-01-01 07:32:12 +08:00
|
|
|
// Process playpen code blocks
|
2018-01-21 23:01:57 +08:00
|
|
|
Array.from(document.querySelectorAll(".playpen")).forEach(function (pre_block) {
|
2016-01-01 07:32:12 +08:00
|
|
|
// Add play button
|
2018-01-13 02:44:13 +08:00
|
|
|
var buttons = pre_block.querySelector(".buttons");
|
2018-01-21 23:01:57 +08:00
|
|
|
if (!buttons) {
|
2018-01-13 02:44:13 +08:00
|
|
|
buttons = document.createElement('div');
|
|
|
|
buttons.className = 'buttons';
|
2018-04-09 12:10:44 +08:00
|
|
|
pre_block.insertBefore(buttons, pre_block.firstChild);
|
2016-01-01 07:32:12 +08:00
|
|
|
}
|
|
|
|
|
2018-01-13 02:44:13 +08:00
|
|
|
var runCodeButton = document.createElement('button');
|
|
|
|
runCodeButton.className = 'fa fa-play play-button';
|
|
|
|
runCodeButton.hidden = true;
|
|
|
|
runCodeButton.title = 'Run this code';
|
2018-01-27 18:52:47 +08:00
|
|
|
runCodeButton.setAttribute('aria-label', runCodeButton.title);
|
2018-01-13 02:44:13 +08:00
|
|
|
|
|
|
|
var copyCodeClipboardButton = document.createElement('button');
|
|
|
|
copyCodeClipboardButton.className = 'fa fa-copy clip-button';
|
|
|
|
copyCodeClipboardButton.innerHTML = '<i class="tooltiptext"></i>';
|
|
|
|
copyCodeClipboardButton.title = 'Copy to clipboard';
|
2018-01-27 18:52:47 +08:00
|
|
|
copyCodeClipboardButton.setAttribute('aria-label', copyCodeClipboardButton.title);
|
2018-01-13 02:44:13 +08:00
|
|
|
|
2018-04-09 12:10:44 +08:00
|
|
|
buttons.insertBefore(runCodeButton, buttons.firstChild);
|
|
|
|
buttons.insertBefore(copyCodeClipboardButton, buttons.firstChild);
|
2017-06-29 12:35:20 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
runCodeButton.addEventListener('click', function (e) {
|
2016-01-01 07:32:12 +08:00
|
|
|
run_rust_code(pre_block);
|
|
|
|
});
|
2018-01-13 02:44:13 +08:00
|
|
|
|
|
|
|
let code_block = pre_block.querySelector("code");
|
|
|
|
if (window.ace && code_block.classList.contains("editable")) {
|
|
|
|
var undoChangesButton = document.createElement('button');
|
|
|
|
undoChangesButton.className = 'fa fa-history reset-button';
|
|
|
|
undoChangesButton.title = 'Undo changes';
|
2018-01-27 18:52:47 +08:00
|
|
|
undoChangesButton.setAttribute('aria-label', undoChangesButton.title);
|
2018-01-13 02:44:13 +08:00
|
|
|
|
2018-04-09 12:10:44 +08:00
|
|
|
buttons.insertBefore(undoChangesButton, buttons.firstChild);
|
2018-01-13 02:44:13 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
undoChangesButton.addEventListener('click', function () {
|
2018-01-13 02:44:13 +08:00
|
|
|
let editor = window.ace.edit(code_block);
|
|
|
|
editor.setValue(editor.originalCode);
|
|
|
|
editor.clearSelection();
|
|
|
|
});
|
|
|
|
}
|
2016-01-01 07:32:12 +08:00
|
|
|
});
|
2018-01-21 23:01:57 +08:00
|
|
|
})();
|
2016-01-01 07:32:12 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
(function themes() {
|
2018-01-26 01:17:02 +08:00
|
|
|
var html = document.querySelector('html');
|
2018-01-21 23:01:57 +08:00
|
|
|
var themeToggleButton = document.getElementById('theme-toggle');
|
|
|
|
var themePopup = document.getElementById('theme-list');
|
|
|
|
var themeColorMetaTag = document.querySelector('meta[name="theme-color"]');
|
|
|
|
var stylesheets = {
|
2018-07-11 21:33:44 +08:00
|
|
|
ayuHighlight: document.querySelector("[href$='ayu-highlight.css']"),
|
|
|
|
tomorrowNight: document.querySelector("[href$='tomorrow-night.css']"),
|
|
|
|
highlight: document.querySelector("[href$='highlight.css']"),
|
2018-01-21 23:01:57 +08:00
|
|
|
};
|
2017-08-08 06:05:33 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
function showThemes() {
|
|
|
|
themePopup.style.display = 'block';
|
|
|
|
themeToggleButton.setAttribute('aria-expanded', true);
|
2018-02-15 07:37:19 +08:00
|
|
|
themePopup.querySelector("button#" + document.body.className).focus();
|
2017-08-08 06:05:33 +08:00
|
|
|
}
|
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
function hideThemes() {
|
|
|
|
themePopup.style.display = 'none';
|
|
|
|
themeToggleButton.setAttribute('aria-expanded', false);
|
2018-02-15 07:37:19 +08:00
|
|
|
themeToggleButton.focus();
|
2018-01-21 23:01:57 +08:00
|
|
|
}
|
2017-08-08 06:36:38 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
function set_theme(theme) {
|
|
|
|
let ace_theme;
|
|
|
|
|
|
|
|
if (theme == 'coal' || theme == 'navy') {
|
|
|
|
stylesheets.ayuHighlight.disabled = true;
|
|
|
|
stylesheets.tomorrowNight.disabled = false;
|
|
|
|
stylesheets.highlight.disabled = true;
|
|
|
|
|
|
|
|
ace_theme = "ace/theme/tomorrow_night";
|
|
|
|
} else if (theme == 'ayu') {
|
|
|
|
stylesheets.ayuHighlight.disabled = false;
|
|
|
|
stylesheets.tomorrowNight.disabled = true;
|
|
|
|
stylesheets.highlight.disabled = true;
|
|
|
|
|
|
|
|
ace_theme = "ace/theme/tomorrow_night";
|
|
|
|
} else {
|
|
|
|
stylesheets.ayuHighlight.disabled = true;
|
|
|
|
stylesheets.tomorrowNight.disabled = true;
|
|
|
|
stylesheets.highlight.disabled = false;
|
|
|
|
|
|
|
|
ace_theme = "ace/theme/dawn";
|
|
|
|
}
|
|
|
|
|
|
|
|
setTimeout(function () {
|
|
|
|
themeColorMetaTag.content = getComputedStyle(document.body).backgroundColor;
|
|
|
|
}, 1);
|
|
|
|
|
|
|
|
if (window.ace && window.editors) {
|
|
|
|
window.editors.forEach(function (editor) {
|
|
|
|
editor.setTheme(ace_theme);
|
2017-08-08 06:36:38 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-26 01:17:02 +08:00
|
|
|
var previousTheme;
|
|
|
|
try { previousTheme = localStorage.getItem('mdbook-theme'); } catch (e) { }
|
2018-10-13 02:57:59 +08:00
|
|
|
if (previousTheme === null || previousTheme === undefined) { previousTheme = default_theme; }
|
2018-01-26 01:17:02 +08:00
|
|
|
|
2018-01-22 07:04:16 +08:00
|
|
|
try { localStorage.setItem('mdbook-theme', theme); } catch (e) { }
|
2017-08-08 06:05:33 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
document.body.className = theme;
|
2018-01-26 01:17:02 +08:00
|
|
|
html.classList.remove(previousTheme);
|
|
|
|
html.classList.add(theme);
|
2017-08-08 06:36:38 +08:00
|
|
|
}
|
2017-08-08 06:05:33 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
// Set theme
|
2018-01-22 07:04:16 +08:00
|
|
|
var theme;
|
|
|
|
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
2018-10-13 02:57:59 +08:00
|
|
|
if (theme === null || theme === undefined) { theme = default_theme; }
|
2017-08-08 06:36:38 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
set_theme(theme);
|
|
|
|
|
|
|
|
themeToggleButton.addEventListener('click', function () {
|
|
|
|
if (themePopup.style.display === 'block') {
|
|
|
|
hideThemes();
|
|
|
|
} else {
|
|
|
|
showThemes();
|
|
|
|
}
|
2017-08-08 06:36:38 +08:00
|
|
|
});
|
2017-08-08 06:05:33 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
themePopup.addEventListener('click', function (e) {
|
|
|
|
var theme = e.target.id || e.target.parentElement.id;
|
|
|
|
set_theme(theme);
|
|
|
|
});
|
2017-08-08 06:05:33 +08:00
|
|
|
|
2018-02-15 07:37:19 +08:00
|
|
|
themePopup.addEventListener('focusout', function(e) {
|
2018-02-24 17:23:45 +08:00
|
|
|
// e.relatedTarget is null in Safari and Firefox on macOS (see workaround below)
|
2018-07-31 08:06:50 +08:00
|
|
|
if (!!e.relatedTarget && !themeToggleButton.contains(e.relatedTarget) && !themePopup.contains(e.relatedTarget)) {
|
2018-02-24 17:23:45 +08:00
|
|
|
hideThemes();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Should not be needed, but it works around an issue on macOS & iOS: https://github.com/rust-lang-nursery/mdBook/issues/628
|
|
|
|
document.addEventListener('click', function(e) {
|
|
|
|
if (themePopup.style.display === 'block' && !themeToggleButton.contains(e.target) && !themePopup.contains(e.target)) {
|
2018-01-21 23:01:57 +08:00
|
|
|
hideThemes();
|
|
|
|
}
|
|
|
|
});
|
2017-06-01 03:07:30 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
document.addEventListener('keydown', function (e) {
|
2018-02-15 07:37:19 +08:00
|
|
|
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; }
|
|
|
|
if (!themePopup.contains(e.target)) { return; }
|
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
switch (e.key) {
|
|
|
|
case 'Escape':
|
|
|
|
e.preventDefault();
|
|
|
|
hideThemes();
|
|
|
|
break;
|
2018-02-15 07:37:19 +08:00
|
|
|
case 'ArrowUp':
|
|
|
|
e.preventDefault();
|
|
|
|
var li = document.activeElement.parentElement;
|
|
|
|
if (li && li.previousElementSibling) {
|
|
|
|
li.previousElementSibling.querySelector('button').focus();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'ArrowDown':
|
|
|
|
e.preventDefault();
|
|
|
|
var li = document.activeElement.parentElement;
|
|
|
|
if (li && li.nextElementSibling) {
|
|
|
|
li.nextElementSibling.querySelector('button').focus();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'Home':
|
|
|
|
e.preventDefault();
|
|
|
|
themePopup.querySelector('li:first-child button').focus();
|
|
|
|
break;
|
|
|
|
case 'End':
|
|
|
|
e.preventDefault();
|
|
|
|
themePopup.querySelector('li:last-child button').focus();
|
|
|
|
break;
|
2018-01-21 23:01:57 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
})();
|
2016-01-01 07:32:12 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
(function sidebar() {
|
2018-01-13 02:44:13 +08:00
|
|
|
var html = document.querySelector("html");
|
2018-01-21 23:01:57 +08:00
|
|
|
var sidebar = document.getElementById("sidebar");
|
|
|
|
var sidebarLinks = document.querySelectorAll('#sidebar a');
|
|
|
|
var sidebarToggleButton = document.getElementById("sidebar-toggle");
|
|
|
|
var firstContact = null;
|
|
|
|
|
|
|
|
function showSidebar() {
|
|
|
|
html.classList.remove('sidebar-hidden')
|
|
|
|
html.classList.add('sidebar-visible');
|
|
|
|
Array.from(sidebarLinks).forEach(function (link) {
|
|
|
|
link.setAttribute('tabIndex', 0);
|
|
|
|
});
|
|
|
|
sidebarToggleButton.setAttribute('aria-expanded', true);
|
|
|
|
sidebar.setAttribute('aria-hidden', false);
|
2018-01-22 07:04:16 +08:00
|
|
|
try { localStorage.setItem('mdbook-sidebar', 'visible'); } catch (e) { }
|
2018-01-21 23:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function hideSidebar() {
|
|
|
|
html.classList.remove('sidebar-visible')
|
|
|
|
html.classList.add('sidebar-hidden');
|
|
|
|
Array.from(sidebarLinks).forEach(function (link) {
|
|
|
|
link.setAttribute('tabIndex', -1);
|
|
|
|
});
|
|
|
|
sidebarToggleButton.setAttribute('aria-expanded', false);
|
|
|
|
sidebar.setAttribute('aria-hidden', true);
|
2018-01-22 07:04:16 +08:00
|
|
|
try { localStorage.setItem('mdbook-sidebar', 'hidden'); } catch (e) { }
|
2018-01-21 23:01:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Toggle sidebar
|
|
|
|
sidebarToggleButton.addEventListener('click', function sidebarToggle() {
|
|
|
|
if (html.classList.contains("sidebar-hidden")) {
|
|
|
|
showSidebar();
|
|
|
|
} else if (html.classList.contains("sidebar-visible")) {
|
2017-10-24 08:31:56 +08:00
|
|
|
hideSidebar();
|
2017-06-09 21:51:30 +08:00
|
|
|
} else {
|
2018-01-21 23:01:57 +08:00
|
|
|
if (getComputedStyle(sidebar)['transform'] === 'none') {
|
|
|
|
hideSidebar();
|
|
|
|
} else {
|
|
|
|
showSidebar();
|
|
|
|
}
|
2017-06-09 21:51:30 +08:00
|
|
|
}
|
2018-01-21 23:01:57 +08:00
|
|
|
});
|
2017-06-09 21:51:30 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
document.addEventListener('touchstart', function (e) {
|
|
|
|
firstContact = {
|
|
|
|
x: e.touches[0].clientX,
|
|
|
|
time: Date.now()
|
|
|
|
};
|
2018-01-25 18:44:22 +08:00
|
|
|
}, { passive: true });
|
2018-01-19 23:41:50 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
document.addEventListener('touchmove', function (e) {
|
|
|
|
if (!firstContact)
|
|
|
|
return;
|
|
|
|
|
|
|
|
var curX = e.touches[0].clientX;
|
|
|
|
var xDiff = curX - firstContact.x,
|
|
|
|
tDiff = Date.now() - firstContact.time;
|
2018-01-19 23:41:50 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
if (tDiff < 250 && Math.abs(xDiff) >= 150) {
|
|
|
|
if (xDiff >= 0 && firstContact.x < Math.min(document.body.clientWidth * 0.25, 300))
|
|
|
|
showSidebar();
|
|
|
|
else if (xDiff < 0 && curX < 300)
|
|
|
|
hideSidebar();
|
|
|
|
|
|
|
|
firstContact = null;
|
|
|
|
}
|
2018-01-25 18:44:22 +08:00
|
|
|
}, { passive: true });
|
2017-10-24 08:31:56 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
// Scroll sidebar to current active section
|
|
|
|
var activeSection = sidebar.querySelector(".active");
|
|
|
|
if (activeSection) {
|
|
|
|
sidebar.scrollTop = activeSection.offsetTop;
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
|
|
|
|
(function chapterNavigation() {
|
|
|
|
document.addEventListener('keydown', function (e) {
|
|
|
|
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; }
|
2018-03-07 21:02:06 +08:00
|
|
|
if (window.search && window.search.hasFocus()) { return; }
|
2018-01-21 23:01:57 +08:00
|
|
|
|
|
|
|
switch (e.key) {
|
|
|
|
case 'ArrowRight':
|
|
|
|
e.preventDefault();
|
|
|
|
var nextButton = document.querySelector('.nav-chapters.next');
|
|
|
|
if (nextButton) {
|
|
|
|
window.location.href = nextButton.href;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'ArrowLeft':
|
|
|
|
e.preventDefault();
|
|
|
|
var previousButton = document.querySelector('.nav-chapters.previous');
|
|
|
|
if (previousButton) {
|
|
|
|
window.location.href = previousButton.href;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-01-13 02:44:13 +08:00
|
|
|
});
|
2018-01-21 23:01:57 +08:00
|
|
|
})();
|
2017-10-24 08:31:56 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
(function clipboard() {
|
|
|
|
var clipButtons = document.querySelectorAll('.clip-button');
|
2018-01-13 02:44:13 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
function hideTooltip(elem) {
|
|
|
|
elem.firstChild.innerText = "";
|
|
|
|
elem.className = 'fa fa-copy clip-button';
|
2016-01-01 07:32:12 +08:00
|
|
|
}
|
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
function showTooltip(elem, msg) {
|
|
|
|
elem.firstChild.innerText = msg;
|
|
|
|
elem.className = 'fa fa-copy tooltipped';
|
2017-09-19 02:55:59 +08:00
|
|
|
}
|
2017-04-01 05:06:03 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
var clipboardSnippets = new Clipboard('.clip-button', {
|
|
|
|
text: function (trigger) {
|
|
|
|
hideTooltip(trigger);
|
|
|
|
let playpen = trigger.closest("pre");
|
|
|
|
return playpen_text(playpen);
|
|
|
|
}
|
|
|
|
});
|
2017-04-01 05:06:03 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
Array.from(clipButtons).forEach(function (clipButton) {
|
|
|
|
clipButton.addEventListener('mouseout', function (e) {
|
|
|
|
hideTooltip(e.currentTarget);
|
|
|
|
});
|
|
|
|
});
|
2018-01-13 02:44:13 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
clipboardSnippets.on('success', function (e) {
|
|
|
|
e.clearSelection();
|
|
|
|
showTooltip(e.trigger, "Copied!");
|
2016-01-01 07:32:12 +08:00
|
|
|
});
|
2018-01-13 02:44:13 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
clipboardSnippets.on('error', function (e) {
|
|
|
|
showTooltip(e.trigger, "Clipboard error!");
|
|
|
|
});
|
|
|
|
})();
|
2018-01-19 23:41:50 +08:00
|
|
|
|
2018-02-09 18:34:18 +08:00
|
|
|
(function scrollToTop () {
|
|
|
|
var menuTitle = document.querySelector('.menu-title');
|
|
|
|
|
|
|
|
menuTitle.addEventListener('click', function () {
|
|
|
|
document.scrollingElement.scrollTo({ top: 0, behavior: 'smooth' });
|
|
|
|
});
|
|
|
|
})();
|
|
|
|
|
2018-01-19 23:41:50 +08:00
|
|
|
(function autoHideMenu() {
|
|
|
|
var menu = document.getElementById('menu-bar');
|
|
|
|
|
2018-01-23 21:13:11 +08:00
|
|
|
var previousScrollTop = document.scrollingElement.scrollTop;
|
2018-01-19 23:41:50 +08:00
|
|
|
|
2018-01-21 23:01:57 +08:00
|
|
|
document.addEventListener('scroll', function () {
|
2018-01-23 21:13:11 +08:00
|
|
|
if (menu.classList.contains('folded') && document.scrollingElement.scrollTop < previousScrollTop) {
|
2018-01-19 23:41:50 +08:00
|
|
|
menu.classList.remove('folded');
|
2018-01-23 21:13:11 +08:00
|
|
|
} else if (!menu.classList.contains('folded') && document.scrollingElement.scrollTop > previousScrollTop) {
|
2018-01-19 23:41:50 +08:00
|
|
|
menu.classList.add('folded');
|
|
|
|
}
|
|
|
|
|
2018-01-23 21:13:11 +08:00
|
|
|
if (!menu.classList.contains('bordered') && document.scrollingElement.scrollTop > 0) {
|
2018-01-19 23:41:50 +08:00
|
|
|
menu.classList.add('bordered');
|
|
|
|
}
|
|
|
|
|
2018-01-23 21:13:11 +08:00
|
|
|
if (menu.classList.contains('bordered') && document.scrollingElement.scrollTop === 0) {
|
2018-01-19 23:41:50 +08:00
|
|
|
menu.classList.remove('bordered');
|
|
|
|
}
|
|
|
|
|
2018-01-23 21:13:11 +08:00
|
|
|
previousScrollTop = document.scrollingElement.scrollTop;
|
2018-01-19 23:41:50 +08:00
|
|
|
}, { passive: true });
|
|
|
|
})();
|