Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions assets/js/click-to-copy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
let codeListings = document.querySelectorAll('.highlight > pre');

for (let index = 0; index < codeListings.length; index++) {
const codeSample = codeListings[index].querySelector('code');

const copyButton = document.createElement('button');
Comment thread
akshatsinghai6682-sketch marked this conversation as resolved.
const buttonAttributes = {
type: 'button',
title: 'Copy to clipboard',
'data-bs-toggle': 'tooltip',
'data-bs-placement': 'top',
'data-bs-container': 'body',
};

Object.keys(buttonAttributes).forEach((key) => {
copyButton.setAttribute(key, buttonAttributes[key]);
});

copyButton.classList.add(
'fas',
'fa-copy',
'btn',
'btn-sm',
'td-click-to-copy'
);
const tooltip = new bootstrap.Tooltip(copyButton);

let revertTimeout;

copyButton.onclick = () => {
copyCode(codeSample)
.then(() => {
copyButton.setAttribute('data-bs-original-title', 'Copied!');
tooltip.show();
copyButton.classList.remove('fa-copy');
copyButton.classList.add('fa-check', 'td-click-to-copy--copied');
clearTimeout(revertTimeout);
revertTimeout = setTimeout(() => {
copyButton.classList.remove('fa-check', 'td-click-to-copy--copied');
copyButton.classList.add('fa-copy');
copyButton.setAttribute('data-bs-original-title', 'Copy to clipboard');
tooltip.hide();
}, 2500);
})
.catch((err) => {
console.warn('Failed to copy code to clipboard:', err);
copyButton.setAttribute('data-bs-original-title', 'Failed to copy');
tooltip.show();
clearTimeout(revertTimeout);
revertTimeout = setTimeout(() => {
copyButton.setAttribute('data-bs-original-title', 'Copy to clipboard');
tooltip.hide();
}, 2500);
});
};
const buttonDiv = document.createElement('div');
buttonDiv.classList.add('click-to-copy');
buttonDiv.append(copyButton);
codeListings[index].insertBefore(buttonDiv, codeSample);
}

const copyCode = (codeSample) => {
const isConsoleBlock = codeSample.matches(
"code[data-lang='console'], code.language-console"
);
let text;

if (isConsoleBlock) {
const clone = codeSample.cloneNode(true);
pruneUnselectableElements(codeSample, clone);
text = clone.textContent;
text = text.replace(/^ /gm, '');
} else {
text = codeSample.textContent;
}
text = text ? text.trim() : '';
return navigator.clipboard.writeText(text + '\n');
};

const pruneUnselectableElements = (sourceNode, cloneNode) => {
const sourceChildren = sourceNode.children;
const cloneChildren = cloneNode.children;

for (let i = sourceChildren.length - 1; i >= 0; i--) {
const sourceChild = sourceChildren[i];
const cloneChild = cloneChildren[i];
const style = window.getComputedStyle(sourceChild);
const unselectable =
style.userSelect === 'none' || style.webkitUserSelect === 'none';
if (unselectable) {
cloneChild.remove();
continue;
}

pruneUnselectableElements(sourceChild, cloneChild);
}
};
8 changes: 8 additions & 0 deletions assets/scss/_styles_project.scss
Original file line number Diff line number Diff line change
Expand Up @@ -705,6 +705,14 @@ a:not([href]):not([class]):hover {
&:hover {
color: $body-color !important;
}

&:active {
transform: none !important;
}

&.td-click-to-copy--copied {
color: $success !important;
}
Comment thread
akshatsinghai6682-sketch marked this conversation as resolved.
}

.tooltip .tooltip-inner {
Expand Down
Loading