forked from FusionAuth/fusionauth-site
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
342c51a
commit 276e92a
Showing
2 changed files
with
43 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
export const initCodeCopyIcons = async (): Promise<void> => { | ||
document.addEventListener('DOMContentLoaded', () => { | ||
document.querySelectorAll('pre.astro-code').forEach((codeBlock) => { | ||
const wrapper = document.createElement('div'); | ||
wrapper.style.position = 'relative'; | ||
|
||
const copyButton = document.createElement('button'); | ||
copyButton.className = 'absolute top-2.5 right-2.5 text-xs leading-4'; | ||
|
||
const icon = document.createElement('i'); | ||
icon.className = 'fad fa-copy w-10 py-2 px-2.5 bg-slate-800 text-center text-indigo-200 leading-4 rounded-sm hover:text-indigo-600 dark:hover:text-indigo-400 [.active]:text-indigo-600 dark:[.active]:text-indigo-400'; | ||
icon.title = 'Copy contents'; | ||
copyButton.appendChild(icon); | ||
|
||
wrapper.appendChild(copyButton); | ||
|
||
// wrap codebock with relative parent element | ||
codeBlock.parentNode.insertBefore(wrapper, codeBlock); | ||
wrapper.appendChild(codeBlock); | ||
|
||
copyButton.addEventListener('click', () => { | ||
copyCode(codeBlock, icon); | ||
}); | ||
}); | ||
}); | ||
} | ||
|
||
const copyCode = async (block: Element, icon: HTMLElement): Promise<void> => { | ||
const code = block.querySelector('code'); | ||
const text = code.innerText; | ||
|
||
await navigator.clipboard.writeText(text); | ||
|
||
icon.classList.replace('fa-copy', 'fa-check'); | ||
|
||
setTimeout(() => { | ||
icon.classList.replace('fa-check', 'fa-copy'); | ||
}, 1000); | ||
} |