Add "Copy Link" link to demo, restore sample text from URL hash if present.

This commit is contained in:
David Anson 2019-04-02 22:07:32 -07:00
parent 7aadb1124e
commit 827e1acb56
2 changed files with 29 additions and 0 deletions

View file

@ -28,6 +28,8 @@
<div class="flex-columns inset">
<footer><a href="https://github.com/DavidAnson/markdownlint">markdownlint project on GitHub</a></footer>
<div class="flex-fill"></div>
<footer><a href="#" id="copyLink">Copy Link</a></footer>
<div class="flex-fill"></div>
<footer>Copyright &copy; 2015-2019 by <a href="https://dlaa.me/">David Anson</a></footer>
</div>
</div>

View file

@ -8,13 +8,18 @@
var violations = document.getElementById("violations");
var form = document.getElementsByTagName("form")[0];
var openFile = document.getElementById("openFile");
var copyLink = document.getElementById("copyLink");
// Variables
var markdownit = window.markdownit({ "html": true });
var newLineRe = /\r\n|\r|\n/;
var hashPrefix = "%m";
var rulesMd = "https://github.com/DavidAnson/markdownlint" +
"/blob/master/doc/Rules.md";
// Do-nothing function
function noop() {}
// Sanitize string for HTML display
function sanitize(str) {
return str
@ -132,12 +137,22 @@
}
}
// Updates the URL hash and copies the URL to the clipboard
function onCopyLinkClick(e) {
window.location.hash = encodeURIComponent(hashPrefix + markdown.value);
/* eslint-disable-next-line no-unused-expressions */
navigator.clipboard && navigator.clipboard.writeText &&
navigator.clipboard.writeText(window.location).then(noop, noop);
e.preventDefault();
}
// Add event listeners
document.body.addEventListener("dragover", onDragOver);
document.body.addEventListener("drop", onDrop);
openFile.addEventListener("change", onOpenFileChange);
markdown.addEventListener("input", onMarkdownInput);
violations.addEventListener("click", onLineNumberClick, true);
copyLink.addEventListener("click", onCopyLinkClick);
/* eslint-disable max-len */
markdown.value = [
@ -167,6 +182,18 @@
].join("\n");
/* eslint-enable max-len */
// Update Markdown from hash (if present)
if (window.location.hash) {
try {
const decodedHash = decodeURIComponent(window.location.hash.substring(1));
if (hashPrefix === decodedHash.substring(0, hashPrefix.length)) {
markdown.value = decodedHash.substring(hashPrefix.length);
}
} catch (ex) {
// Invalid
}
}
// Detect legacy browsers
try {
/* eslint-disable-next-line no-new */