-
Notifications
You must be signed in to change notification settings - Fork 398
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add button to copy code to clipboard
- Loading branch information
1 parent
88dbacc
commit 01d55ed
Showing
6 changed files
with
190 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
37 changes: 37 additions & 0 deletions
37
pages/interface/components/Markdown/plugins/copy-code-to-clipboard.js
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,37 @@ | ||
/** | ||
* @returns {import('@bytemd/react').ViewerProps['plugins'][0]} | ||
*/ | ||
|
||
export function copyCodeToClipboardPlugin(createCopyButton) { | ||
return { | ||
viewerEffect({ markdownBody }) { | ||
if (!navigator.clipboard) return; | ||
|
||
const preElements = markdownBody.querySelectorAll('pre'); | ||
|
||
preElements.forEach((pre) => { | ||
const codeToCopy = pre.querySelector('code')?.innerText; | ||
if (!codeToCopy) return; | ||
|
||
const externalDivElement = document.createElement('div'); | ||
externalDivElement.classList.add('copy-button-external-container'); | ||
pre.appendChild(externalDivElement); | ||
|
||
const internalDivElement = document.createElement('div'); | ||
internalDivElement.classList.add('copy-button-internal-container'); | ||
externalDivElement.appendChild(internalDivElement); | ||
|
||
createCopyButton(internalDivElement, codeToCopy, { | ||
beforeCopy: { | ||
title: 'Copiar código', | ||
ariaLabel: 'Copiar código', | ||
}, | ||
afterCopy: { | ||
title: 'Código copiado', | ||
ariaLabel: 'Código copiado', | ||
}, | ||
}); | ||
}); | ||
}, | ||
}; | ||
} |
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,87 @@ | ||
import { check, copy } from '@primer/octicons'; | ||
|
||
const defaultOptions = { | ||
beforeCopy: { | ||
title: 'Copiar', | ||
ariaLabel: 'Copiar', | ||
icon: copy.toSVG(), | ||
}, | ||
afterCopy: { | ||
title: 'Copiado', | ||
ariaLabel: 'Copiado', | ||
icon: check.toSVG(), | ||
}, | ||
}; | ||
|
||
export function createCopyButton(parentElement, contentToCopy, options = defaultOptions) { | ||
const validatedOptions = validateOptions(options); | ||
const buttonElement = createButtonElement(validatedOptions); | ||
|
||
buttonElement.onclick = async () => { | ||
await navigator.clipboard.writeText(contentToCopy); | ||
setStateAfterCopy(buttonElement, validatedOptions); | ||
setTimeout(() => setStateBeforeCopy(buttonElement, validatedOptions), 2000); | ||
}; | ||
|
||
return parentElement.appendChild(buttonElement); | ||
|
||
function validateOptions(options) { | ||
const { beforeCopy, afterCopy } = options; | ||
|
||
const allowedOptions = Object.keys(defaultOptions.beforeCopy); | ||
Object.entries(beforeCopy).forEach(([key, value]) => { | ||
if (!allowedOptions.includes(key)) { | ||
throw new Error(` | ||
"beforeCopy.${key}" is invalid, please use one of the following: [${allowedOptions.join(', ')}] | ||
`); | ||
} | ||
if (typeof value !== 'string') { | ||
throw new TypeError(`"beforeCopy.${key}" must receive a string as a value`); | ||
} | ||
}); | ||
Object.entries(afterCopy).forEach(([key, value]) => { | ||
if (!allowedOptions.includes(key)) { | ||
throw new Error(` | ||
"afterCopy.${key}" is invalid, please use one of the following: [${allowedOptions.join(', ')}] | ||
`); | ||
} | ||
if (typeof value !== 'string') { | ||
throw new TypeError(`"afterCopy.${key}" must receive a string as a value`); | ||
} | ||
}); | ||
|
||
return Object.freeze({ | ||
beforeCopy: Object.assign({}, defaultOptions.beforeCopy, beforeCopy), | ||
afterCopy: Object.assign({}, defaultOptions.afterCopy, afterCopy), | ||
}); | ||
} | ||
|
||
function createButtonElement(options) { | ||
const buttonElement = document.createElement('button'); | ||
buttonElement.setAttribute('type', 'button'); | ||
setStateBeforeCopy(buttonElement, options); | ||
return buttonElement; | ||
} | ||
|
||
function setStateBeforeCopy(buttonElement, options) { | ||
setAttributes(buttonElement, { | ||
title: options.beforeCopy.title, | ||
'aria-label': options.beforeCopy.ariaLabel, | ||
}); | ||
buttonElement.innerHTML = options.beforeCopy.icon; | ||
} | ||
|
||
function setStateAfterCopy(buttonElement, options) { | ||
setAttributes(buttonElement, { | ||
title: options.afterCopy.title, | ||
'aria-label': options.afterCopy.ariaLabel, | ||
}); | ||
buttonElement.innerHTML = options.afterCopy.icon; | ||
} | ||
|
||
function setAttributes(buttonElement, attributes) { | ||
Object.entries(attributes).forEach(([key, value]) => { | ||
buttonElement.setAttribute(key, value); | ||
}); | ||
} | ||
} |