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

Demo link outputtype #1196

Merged
merged 3 commits into from
Apr 4, 2018
Merged
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
43 changes: 38 additions & 5 deletions docs/demo/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ var $panes = document.querySelectorAll('.pane');
var inputDirty = true;
var $activeElem = null;
var changeTimeout = null;
var search = searchToObject();

var match = location.search.match(/[?&]text=([^&]*)$/);
if (match) {
$inputElem.value = decodeURIComponent(match[1]);
if ('text' in search) {
$inputElem.value = search.text;
} else {
fetch('./initial.md')
.then(function (res) { return res.text(); })
Expand All @@ -36,6 +36,10 @@ if (match) {
});
}

if (search.outputType) {
$outputTypeElem.value = search.outputType;
}

fetch('./quickref.md')
.then(function (res) { return res.text(); })
.then(function (text) {
Expand All @@ -48,6 +52,8 @@ function handleChange() {
}
$activeElem = document.querySelector('#' + $outputTypeElem.value);
$activeElem.style.display = 'block';

updateLink();
};

$outputTypeElem.addEventListener('change', handleChange, false);
Expand All @@ -67,6 +73,24 @@ $clearElem.addEventListener('click', function () {
handleInput();
}, false);

function searchToObject() {
// modified from https://stackoverflow.com/a/7090123/806777
var pairs = location.search.slice(1).split('&');
var obj = {};

for (var i = 0; i < pairs.length; i++) {
if (pairs[i] === '') {
continue;
}

var pair = pairs[i].split('=');

obj[decodeURIComponent(pair.shift())] = decodeURIComponent(pair.join('='));
}

return obj;
}

function jsonString(input) {
var output = (input + '')
.replace(/\n/g, '\\n')
Expand Down Expand Up @@ -96,13 +120,22 @@ function setScrollPercent(percent) {
$activeElem.scrollTop = percent * getScrollSize();
};

function updateLink() {
var outputType = '';
if ($outputTypeElem.value !== 'preview') {
outputType = 'outputType=' + $outputTypeElem.value + '&';
}

$permalinkElem.href = '?' + outputType + 'text=' + encodeURIComponent($inputElem.value);
history.replaceState('', document.title, $permalinkElem.href);
}

var delayTime = 1;
function checkForChanges() {
if (inputDirty) {
inputDirty = false;

$permalinkElem.href = '?text=' + encodeURIComponent($inputElem.value);
history.replaceState('', document.title, $permalinkElem.href);
updateLink();

var startTime = new Date();

Expand Down
10 changes: 5 additions & 5 deletions docs/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ <h1>Marked Demo</h1>
<div class="container">
<div class="label">
<select id="outputType">
<option value="preview">Preview</option>
<option value="html">HTML Source</option>
<option value="lexer">Lexer Data</option>
<option value="quickref">Quick Reference</option>
</select>
<option value="preview">Preview</option>
<option value="html">HTML Source</option>
<option value="lexer">Lexer Data</option>
<option value="quickref">Quick Reference</option>
</select>
</div>

<div id="preview" class="pane">
Expand Down