-
-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added logic to attach listeners to 'copy code block' buttons
- Loading branch information
Showing
2 changed files
with
54 additions
and
37 deletions.
There are no files selected for viewing
65 changes: 38 additions & 27 deletions
65
packages/shared/src/markdown/copyToClipboard/attachCopyClickListener.ts
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 |
---|---|---|
@@ -1,35 +1,46 @@ | ||
export const attachCopyClickListener = (markdownContainer: HTMLElement) => { | ||
markdownContainer.querySelectorAll('.nlux-comp-copyButton').forEach((copyButton) => { | ||
if (!(copyButton instanceof HTMLButtonElement)) { | ||
return; | ||
} | ||
const addListenersToCopyButton = (copyButton: Element) => { | ||
if (!(copyButton instanceof HTMLButtonElement)) { | ||
return; | ||
} | ||
|
||
// If button has click event listener, do not add another one | ||
if (copyButton.dataset.clickListenerSet === 'true') { | ||
// If button has click event listener, do not add another one | ||
if (copyButton.dataset.clickListenerSet === 'true') { | ||
return; | ||
} | ||
|
||
let clicked = false; | ||
const codeBlock = copyButton.nextElementSibling as HTMLElement; | ||
copyButton.addEventListener('click', () => { | ||
if (clicked || !codeBlock) { | ||
return; | ||
} | ||
|
||
let clicked = false; | ||
const codeBlock = copyButton.nextElementSibling as HTMLElement; | ||
copyButton.addEventListener('click', () => { | ||
if (clicked || !codeBlock) { | ||
return; | ||
} | ||
// Copy code to clipboard | ||
const code = codeBlock.innerText; | ||
navigator.clipboard.writeText(code ?? ''); | ||
|
||
// Copy code to clipboard | ||
const code = codeBlock.innerText; | ||
navigator.clipboard.writeText(code ?? ''); | ||
// Mark button as clicked for 1 second | ||
clicked = true; | ||
copyButton.classList.add('clicked'); | ||
setTimeout(() => { | ||
clicked = false; | ||
copyButton.classList.remove('clicked'); | ||
}, 1000); | ||
}); | ||
|
||
// Mark button as clicked for 1 second | ||
clicked = true; | ||
copyButton.classList.add('clicked'); | ||
setTimeout(() => { | ||
clicked = false; | ||
copyButton.classList.remove('clicked'); | ||
}, 1000); | ||
}); | ||
// Set data attribute to indicate that click event listener has been set | ||
copyButton.dataset.clickListenerSet = 'true'; | ||
}; | ||
|
||
// Set data attribute to indicate that click event listener has been set | ||
copyButton.dataset.clickListenerSet = 'true'; | ||
}); | ||
export const attachCopyClickListener = (markdownContainer: HTMLElement) => { | ||
const copyButtonCssClass = 'nlux-comp-copyButton'; | ||
if ( | ||
markdownContainer instanceof HTMLButtonElement && | ||
markdownContainer.classList.contains(copyButtonCssClass) | ||
) { | ||
addListenersToCopyButton(markdownContainer); | ||
return; | ||
} | ||
|
||
markdownContainer.querySelectorAll(`.${copyButtonCssClass}`).forEach(addListenersToCopyButton); | ||
}; |
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