2018-03-24 18:57:05 +11:00
|
|
|
/*jshint strict:false */
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Add data-platform to the body tag to show platform related shortcuts
|
|
|
|
// -----------------------------------------------------------------------------
|
2018-04-29 20:00:00 +10:00
|
|
|
const isWindows = navigator.appVersion.indexOf('Win') !== -1;
|
2018-03-24 18:57:05 +11:00
|
|
|
document.body.dataset.platform = isWindows ? 'win' : 'mac';
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Autofocus the content field on the homepage
|
|
|
|
// -----------------------------------------------------------------------------
|
2018-04-29 20:00:00 +10:00
|
|
|
const af = document.querySelector('.autofocus textarea');
|
2018-03-24 18:57:05 +11:00
|
|
|
if (af !== null) {
|
|
|
|
af.focus();
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Cmd+Enter or Ctrl+Enter submits the form
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
document.body.onkeydown = function(e) {
|
|
|
|
const metaKey = isWindows ? e.ctrlKey : e.metaKey;
|
2018-04-29 20:00:00 +10:00
|
|
|
const form = document.querySelector('.snippet-form');
|
2018-03-24 18:57:05 +11:00
|
|
|
|
|
|
|
if (form && e.keyCode === 13 && metaKey) {
|
|
|
|
form.submit();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Toggle Wordwrap
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
const wordwrapCheckbox = document.getElementById('wordwrap');
|
2018-03-24 19:26:27 +11:00
|
|
|
const snippetDiv = document.querySelectorAll('.snippet-code');
|
2018-03-24 18:57:05 +11:00
|
|
|
|
|
|
|
function toggleWordwrap() {
|
|
|
|
if (wordwrapCheckbox.checked) {
|
2018-03-24 19:26:27 +11:00
|
|
|
snippetDiv.forEach(i => i.classList.add('wordwrap'));
|
2018-03-24 18:57:05 +11:00
|
|
|
} else {
|
2018-03-24 19:26:27 +11:00
|
|
|
snippetDiv.forEach(i => i.classList.remove('wordwrap'));
|
2018-03-24 18:57:05 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wordwrapCheckbox && snippetDiv) {
|
|
|
|
toggleWordwrap();
|
|
|
|
wordwrapCheckbox.onchange = toggleWordwrap;
|
|
|
|
}
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Line Highlighting
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
const curLine = document.location.hash;
|
|
|
|
if (curLine.startsWith('#L')) {
|
|
|
|
const hashlist = curLine.substring(2).split(',');
|
|
|
|
if (hashlist.length > 0 && hashlist[0] !== '') {
|
|
|
|
hashlist.forEach(function(el) {
|
|
|
|
const line = document.getElementById(`l${el}`);
|
|
|
|
if (line) {
|
|
|
|
line.classList.add('marked');
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-07 17:14:32 +10:00
|
|
|
const lines = document.querySelectorAll('.snippet-code li');
|
2018-03-24 18:57:05 +11:00
|
|
|
lines.forEach(function(el) {
|
|
|
|
el.onclick = function() {
|
|
|
|
el.classList.toggle('marked');
|
|
|
|
let hash = 'L';
|
2018-04-07 17:14:32 +10:00
|
|
|
const marked = document.querySelectorAll('.snippet-code li.marked');
|
2018-03-24 18:57:05 +11:00
|
|
|
marked.forEach(function(line) {
|
|
|
|
if (hash !== 'L') {
|
|
|
|
hash += ',';
|
|
|
|
}
|
|
|
|
hash += line.getAttribute('id').substring(1);
|
|
|
|
});
|
|
|
|
window.location.hash = hash;
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
// Copy URL to Clipboard
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
const clipboardLink = document.getElementById('copyToClipboard');
|
|
|
|
const copyToClipboardField = document.getElementById('copyToClipboardField');
|
|
|
|
|
|
|
|
if (clipboardLink && copyToClipboardField) {
|
|
|
|
clipboardLink.onclick = function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
copyToClipboardField.select();
|
2018-04-29 20:00:00 +10:00
|
|
|
document.execCommand('Copy');
|
2018-03-24 18:57:05 +11:00
|
|
|
};
|
|
|
|
}
|