mirror of
https://github.com/ethanrusz/ethanrusz
synced 2024-11-24 08:57:47 -05:00
Improve copy button script formatting
This commit is contained in:
parent
d5d4df98a8
commit
d7010dafd1
1 changed files with 43 additions and 35 deletions
|
@ -1,47 +1,55 @@
|
||||||
/*
|
/*
|
||||||
* A nice little clipboard script provided by crykn. Thanks!
|
* A nice little clipboard script provided by Crykn. Thanks!
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function sleep(ms) {
|
function sleep(ms) {
|
||||||
return new Promise(resolve => setTimeout(resolve, ms));
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onClickEffect(btn, style) {
|
async function onClickEffect(btn, style) {
|
||||||
btn.removeClass("btn-light");
|
btn.removeClass("btn-light");
|
||||||
btn.addClass(style);
|
btn.addClass(style);
|
||||||
await sleep(250);
|
await sleep(250);
|
||||||
btn.removeClass(style);
|
btn.removeClass(style);
|
||||||
btn.addClass("btn-light");
|
btn.addClass("btn-light");
|
||||||
}
|
}
|
||||||
|
|
||||||
$(document).ready(function() {
|
$(document).ready(function () {
|
||||||
// Create butons
|
// Create button
|
||||||
$(".page__content pre > code").each(function() {
|
$(".page__content pre > code").each(function () {
|
||||||
$(this).parent().prepend(
|
$(this)
|
||||||
$(document.createElement('button')).prop({
|
.parent()
|
||||||
type: 'button',
|
.prepend(
|
||||||
innerHTML: '<i class="far fa-copy"></i>',
|
$(document.createElement("button"))
|
||||||
})
|
.prop({
|
||||||
.attr('title', 'Copy to clipboard')
|
type: "button",
|
||||||
.addClass('btn')
|
innerHTML: '<i class="far fa-copy"></i>',
|
||||||
.addClass('btn--primary')
|
})
|
||||||
.css('position', 'absolute')
|
.attr("title", "Copy to clipboard")
|
||||||
.css('right', '1em')
|
.addClass("btn")
|
||||||
// Click listener
|
.addClass("btn--primary")
|
||||||
.on('click', function() {
|
.css("position", "absolute")
|
||||||
let codeElement = $(this).parent().children('code').first();
|
.css("right", "1em")
|
||||||
|
// Click listener
|
||||||
|
.on("click", function () {
|
||||||
|
let codeElement = $(this).parent().children("code").first();
|
||||||
|
|
||||||
if (!codeElement) {
|
if (!codeElement) {
|
||||||
throw new Error("Unexpected error! No corresponding code block was found for this button.");
|
throw new Error(
|
||||||
}
|
"Unexpected error! No corresponding code block was found for this button."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
// Blink effect
|
// Blink effect
|
||||||
onClickEffect($(this), "btn--success")
|
onClickEffect($(this), "btn--success");
|
||||||
|
|
||||||
// Copy to clipoard function
|
// Copy to clipoard function
|
||||||
navigator.clipboard.writeText($(codeElement).text()).then(() => true, () => true);
|
navigator.clipboard.writeText($(codeElement).text()).then(
|
||||||
return true;
|
() => true,
|
||||||
})
|
() => true
|
||||||
);
|
);
|
||||||
});
|
return true;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue