Skip to content

Commit

Permalink
More persistent test page (#1529)
Browse files Browse the repository at this point in the history
The test page will now remember the language and code last tested.
  • Loading branch information
RunDevelopment authored Mar 10, 2019
1 parent bc53e09 commit 3100fa3
Showing 1 changed file with 52 additions and 5 deletions.
57 changes: 52 additions & 5 deletions test.html
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,28 @@ <h2>Test drive</h2>
languages = components.languages,
highlightCode = function() { Prism.highlightElement(code); };


function updateHashLanguage(lang) {
location.hash = lang ? 'language=' + lang : '';
}
function getHashLanguage() {
var match = /[#&]language=([^&]+)/.exec(location.hash);
return match ? match[1] : null;
}
function getRadio(lang) {
return $('input[name=language][value="' + lang + '"]');
}

window.onhashchange = function () {
var input = getRadio(getHashLanguage());

if (input && !input.checked) {
input.checked = true;
input.onclick();
}
}


for (var id in languages) {
if (id == 'meta') {
continue;
Expand All @@ -160,6 +182,7 @@ <h2>Test drive</h2>
var lang = this.value;
code.className = 'language-' + lang;
code.textContent = code.textContent;
updateHashLanguage(lang);

// loadLanguage() returns a promise, so we use highlightCode()
// as resolve callback. The promise will be immediately
Expand Down Expand Up @@ -228,15 +251,39 @@ <h2>Test drive</h2>


var radios = $$('input[name=language]');
radios[0].checked = true;
radios[0].onclick();
var selectedRadio = radios[0];

var lastLanguageRadio = getRadio(getHashLanguage());
if (lastLanguageRadio) {
selectedRadio = lastLanguageRadio;
}

selectedRadio.checked = true;
selectedRadio.onclick();

var textarea = $('textarea', form);

(textarea.oninput = function() {
code.textContent = this.value || '';
try {
var lastCode = sessionStorage.getItem('test-code');
if (lastCode) {
textarea.value = lastCode;
}
} catch (e) {
// ignore sessionStorage errors
}

textarea.oninput = function() {
var codeText = this.value || '';
code.textContent = codeText;
highlightCode();
}).call(textarea);

try {
sessionStorage.setItem('test-code', codeText);
} catch (error) {
// ignore sessionStorage errors
}
}
textarea.oninput();

$('#option-show-tokens').onchange = function () {
var cls = 'show-tokens';
Expand Down

0 comments on commit 3100fa3

Please sign in to comment.