Skip to content

Commit

Permalink
2024-04-29 19:56:17
Browse files Browse the repository at this point in the history
  • Loading branch information
KaiKimera committed Apr 29, 2024
1 parent 07fa86a commit bcd3dd3
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions assets/js/system/modules/clipboard.code.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,30 @@
export const $init = () => {
$clipboard('.shortcode-codeblock').catch(console.error);
$clipboard('.shortcode-file').catch(console.error);
$clipboard('.shortcode-codeblock', '[data-fn*="clipboard"]').catch(console.error);
$clipboard('.shortcode-file', '[data-fn*="clipboard"]').catch(console.error);
};

const $clipboard = async ($selector) => {
const $el = document.querySelectorAll($selector);
const $clipboard = async ($shortcode, $button) => {
const $el = document.querySelectorAll($shortcode);
const $len = $el.length;
for (let $i = 0; $i < $len; ++$i) {
const $lines = $el[$i].querySelectorAll('[data-lang] > .line');
const $button = $el[$i].querySelector('[data-fn*="clipboard"]');
const $code = Array.from($lines).map($line => {
const $eLines = $el[$i].querySelectorAll('[data-lang] > .line');
const $eButton = $el[$i].querySelector($button);
const $cIcon = $eButton.firstElementChild.classList;
const $fa = $cIcon[1];

const $code = Array.from($eLines).map($line => {
return Array.from($line.childNodes).map($node => $node.textContent).join('');
});

const $text = $code.join('').replace(/\n{3,}/g, '\n\n');
const $icon = $button.firstElementChild.classList.value;

$button.addEventListener('click', ($e) => {
$eButton.addEventListener('click', ($e) => {
$e.preventDefault();
navigator.clipboard.writeText($text);
$button.firstElementChild.className = 'fas fa-check fa-fw';
$cIcon.replace($fa, 'fa-check');
setTimeout(() => {
$button.firstElementChild.className = $icon;
}, 1000);
$cIcon.replace('fa-check', $fa);
}, 500);
}
);
}
Expand Down

0 comments on commit bcd3dd3

Please sign in to comment.