83 lines
2.3 KiB
JavaScript
83 lines
2.3 KiB
JavaScript
window.addEventListener("load", function() {
|
|
|
|
// Get list of inline code elements
|
|
var codes = document.getElementsByClassName("inline-code");
|
|
for (var i = 0; i < codes.length; i++) {
|
|
|
|
var code = codes[i];
|
|
var input = code.children[1].children[0];
|
|
|
|
// Select all in input when clicked
|
|
input.onclick = function(event) {
|
|
console.log(event);
|
|
inputSelectAll(event.target);
|
|
}
|
|
|
|
// Add clipboard icon
|
|
// TODO doesn't work on IE8 and below
|
|
|
|
var themes = ["dark", "light"];
|
|
for (var j = 0; j < themes.length; j++) {
|
|
|
|
var base = "/assets/icons/" + themes[j] + "-mode/";
|
|
|
|
var svg = document.createElement("source");
|
|
svg.type = "image/svg+xml";
|
|
svg.srcset = base + "copy.svg";
|
|
|
|
//var img = document.createElement("img");
|
|
var img = new Image();
|
|
img.style.zIndex = "3";
|
|
img.className = "inline-code-icon inline-code-icon-end " + themes[j];
|
|
img.setAttribute("data-theme", themes[j]);
|
|
img.src = base + "copy.png";
|
|
img.alt = "Clipboard Icon";
|
|
img.title = "Copy to clipboard";
|
|
img.onclick = function(event) {
|
|
copyInputText(
|
|
event.target.parentElement.parentElement.children[1].children[0],
|
|
event.target,
|
|
event.target.getAttribute("data-theme")
|
|
);
|
|
};
|
|
|
|
var picture = document.createElement("picture");
|
|
picture.appendChild(svg);
|
|
picture.appendChild(img);
|
|
|
|
code.appendChild(picture);
|
|
|
|
}
|
|
}
|
|
|
|
}, false);
|
|
|
|
function copyInputText(input, clipboardIcon, theme) {
|
|
|
|
inputSelectAll(input);
|
|
|
|
var path;
|
|
if (document.execCommand("copy")) {
|
|
path = "/assets/icons/" + theme + "-mode/";
|
|
} else {
|
|
console.log("no"); // TODO X icon
|
|
}
|
|
|
|
clipboardIcon.src = path + "check.svg";
|
|
clipboardIcon.previousElementSibling.srcset = path + "check.png";
|
|
|
|
input.setSelectionRange(0, 0);
|
|
|
|
setTimeout(function() {
|
|
clipboardIcon.src = path + "copy.svg";
|
|
clipboardIcon.previousElementSibling.srcset = path + "copy.png";
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
function inputSelectAll(input) {
|
|
input.focus();
|
|
if (input.select) input.select();
|
|
else input.setSelectionRange(0, this.value.length);
|
|
}
|