96 lines
2.7 KiB
JavaScript
96 lines
2.7 KiB
JavaScript
// @license magnet:?xt=urn:btih:1f739d935676111cfff4b4693e3816e664797050&dn=gpl-3.0.txt GPL-3.0
|
|
|
|
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.getElementsByTagName("input")[0];
|
|
|
|
// Select all in input when clicked
|
|
input.onclick = function(e) {
|
|
var event = e || window.event;
|
|
var target = event.target || event.srcElement;
|
|
inputSelectAll(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(e) {
|
|
var event = e || window.event;
|
|
var target = event.target || event.srcElement;
|
|
|
|
copyInputText(
|
|
target.parentElement.parentElement.getElementsByTagName('input')[0],
|
|
target,
|
|
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.previousSibling.srcset = path + "check.png";
|
|
|
|
inputDeselect(input);
|
|
|
|
setTimeout(function() {
|
|
clipboardIcon.src = path + "copy.svg";
|
|
clipboardIcon.previousSibling.srcset = path + "copy.png";
|
|
}, 1000);
|
|
|
|
}
|
|
|
|
function inputSelectAll(input) {
|
|
input.focus();
|
|
if (input.select) input.select();
|
|
else input.setSelectionRange(0, this.value.length);
|
|
}
|
|
|
|
function inputDeselect(input) {
|
|
if (input.blur) input.blur();
|
|
else input.setSelectionRange(0, 0);
|
|
}
|
|
|
|
// @license-end
|