Fix copying on IE8

This commit is contained in:
ReimarPB 2022-11-03 21:05:54 +01:00
parent eb21cd3496
commit 8aee61c01c
2 changed files with 32 additions and 19 deletions

View File

@ -37,6 +37,10 @@ if (!document.head) {
document.head = document.getElementsByTagName("head")[0]; document.head = document.getElementsByTagName("head")[0];
} }
if (!window.HTMLPictureElement) {
document.createElement("picture");
}
function toArray(iter) { function toArray(iter) {
var arr = []; var arr = [];
for (var i = 0; i < iter.length; i++) arr.push(iter[i]); for (var i = 0; i < iter.length; i++) arr.push(iter[i]);

View File

@ -5,12 +5,13 @@ window.addEventListener("load", function() {
for (var i = 0; i < codes.length; i++) { for (var i = 0; i < codes.length; i++) {
var code = codes[i]; var code = codes[i];
var input = code.children[1].children[0]; var input = code.getElementsByTagName("input")[0];
// Select all in input when clicked // Select all in input when clicked
input.onclick = function(event) { input.onclick = function(e) {
console.log(event); var event = e || window.event;
inputSelectAll(event.target); var target = event.target || event.srcElement;
inputSelectAll(target);
} }
// Add clipboard icon // Add clipboard icon
@ -33,11 +34,14 @@ window.addEventListener("load", function() {
img.src = base + "copy.png"; img.src = base + "copy.png";
img.alt = "Clipboard Icon"; img.alt = "Clipboard Icon";
img.title = "Copy to clipboard"; img.title = "Copy to clipboard";
img.onclick = function(event) { img.onclick = function(e) {
var event = e || window.event;
var target = event.target || event.srcElement;
copyInputText( copyInputText(
event.target.parentElement.parentElement.children[1].children[0], target.parentElement.parentElement.getElementsByTagName('input')[0],
event.target, target,
event.target.getAttribute("data-theme") target.getAttribute("data-theme")
); );
}; };
@ -54,23 +58,23 @@ window.addEventListener("load", function() {
function copyInputText(input, clipboardIcon, theme) { function copyInputText(input, clipboardIcon, theme) {
inputSelectAll(input); inputSelectAll(input);
var path; var path;
if (document.execCommand("copy")) { if (document.execCommand("copy")) {
path = "/assets/icons/" + theme + "-mode/"; path = "/assets/icons/" + theme + "-mode/";
} else { } else {
console.log("no"); // TODO X icon console.log("no"); // TODO X icon
} }
clipboardIcon.src = path + "check.svg"; clipboardIcon.src = path + "check.svg";
clipboardIcon.previousElementSibling.srcset = path + "check.png"; clipboardIcon.previousSibling.srcset = path + "check.png";
input.setSelectionRange(0, 0); inputDeselect(input);
setTimeout(function() { setTimeout(function() {
clipboardIcon.src = path + "copy.svg"; clipboardIcon.src = path + "copy.svg";
clipboardIcon.previousElementSibling.srcset = path + "copy.png"; clipboardIcon.previousSibling.srcset = path + "copy.png";
}, 1000); }, 1000);
} }
@ -80,3 +84,8 @@ function inputSelectAll(input) {
if (input.select) input.select(); if (input.select) input.select();
else input.setSelectionRange(0, this.value.length); else input.setSelectionRange(0, this.value.length);
} }
function inputDeselect(input) {
if (input.blur) input.blur();
else input.setSelectionRange(0, 0);
}