Skip to content
Merged
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
1 change: 1 addition & 0 deletions assets/images/icon-copy.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 changes: 28 additions & 15 deletions assets/javascripts/new-javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,41 @@ document.addEventListener('DOMContentLoaded', function () {
if (navigator && navigator.clipboard) {
const codeBlocks = document.querySelectorAll(
':is([class^="language-"] pre.highlight, .code-box pre code)',
)
);

const copyIcon = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="currentColor"><path d="M16 1H4c-1.1 0-2 .9-2 2v14h2V3h12V1zm3 4H8c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h11c1.1 0 2-.9 2-2V7c0-1.1-.9-2-2-2zm0 16H8V7h11v14z"/></svg>`;
const copiedIcon = `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" fill="currentColor"><path d="M9 16.2L4.8 12l-1.4 1.4L9 19 21 7l-1.4-1.4L9 16.2z"/></svg>`;

codeBlocks.forEach((codeBlock) => {
const button = document.createElement('button')
const codeElement = codeBlock.querySelector('code') || codeBlock
const container = codeBlock.parentElement
const button = document.createElement('button');
const codeElement = codeBlock.querySelector('code') || codeBlock;
const container = codeBlock.parentElement;

container.style.position = 'relative'
container.appendChild(button)
button.innerText = 'copy'
button.classList.add('copy-button');
container.appendChild(button);
button.innerHTML = copyIcon

button.addEventListener('mousedown', async () => {
const originalText = button.innerText
const originalIcon = copyIcon; // Store the original icon

await navigator.clipboard.writeText(codeElement.innerText)
button.innerText = 'copied!'
try {
await navigator.clipboard.writeText(codeElement.innerText);

setTimeout(() => {
button.innerText = originalText
}, 1000)
})
})
button.classList.add('copied');
button.innerHTML = copiedIcon;

setTimeout(() => {
button.classList.remove('copied');
button.innerHTML = originalIcon;
}, 1000);
} catch (err) {
console.error('Failed to copy text: ', err);
setTimeout(() => {
button.innerHTML = originalIcon; // Revert to original icon
}, 1000);
}
});
});
}

// Check for reduced motion setting
Expand Down
40 changes: 28 additions & 12 deletions assets/stylesheets/new-stylesheets/_syntax.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,43 @@
position: relative;

button {
background-color: #c5c5c5;
color: white;
border: 0;
padding: 5px 8px;
font-family: sans-serif;
transition: background-color 0.2s;
cursor: pointer;
position: absolute;
top: 0;
right: 0;
top: 1em;
right: 1em;
background: none;
border: medium;
cursor: pointer;
padding: 7px 6px;
border-radius: 6px;
transition: all 0.2s ease-in-out;
display: none;
border-radius: 0 10px 0 0;
background-color: var(--color-syntax-clipboard-bg);

svg {
width: 20px;
height: 20px;
opacity: 0.8;
}

&.copied {
svg {
color: var(--color-syntax-clipboard-check-color);
}
}


&:hover {
background-color: #868686;
background-color: var(--color-syntax-clipboard-hover-bg);

svg {
opacity: 1;
}
}
}

&:hover {
button {
display: block;
display: flex;
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions assets/stylesheets/new-stylesheets/_themes.scss
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@
--nav-scroller-gradient-end: #fff;

// syntax
--color-syntax-clipboard-bg: rgb(223, 223, 247);
--color-syntax-clipboard-hover-bg: rgb(216, 216, 242);
--color-syntax-clipboard-check-color: rgb(12, 156, 12);

--color-syntax-attributes: rgb(148, 113, 0);
--color-syntax-characters: rgb(39, 42, 216);
--color-syntax-comments: rgb(112, 127, 140);
Expand Down Expand Up @@ -165,6 +169,10 @@
--nav-scroller-gradient-end: #000;

// syntax
--color-syntax-clipboard-bg: rgb(71, 79, 110);
--color-syntax-clipboard-hover-bg: rgb(80, 89, 124);
--color-syntax-clipboard-check-color: rgb(11, 242, 11);

--color-syntax-attributes: rgb(204, 151, 104);
--color-syntax-characters: rgb(217, 201, 124);
--color-syntax-comments: rgb(127, 140, 152);
Expand Down