fix: prevent duplicated copy file buttons

This commit is contained in:
EnixCoda 2021-04-13 21:54:23 +08:00
parent 4b96e01845
commit 48d2053851
No known key found for this signature in database
GPG key ID: 0C1A07377913A1DD
2 changed files with 11 additions and 7 deletions

View file

@ -43,7 +43,7 @@ export function CopyFileButton(props: React.PropsWithChildren<Props>) {
}, [])
return (
<a ref={elementRef} className={cx('btn btn-sm BtnGroup-item copy-file-btn', className)}>
<a ref={elementRef} className={cx('btn btn-sm BtnGroup-item', className)}>
{content}
</a>
)

View file

@ -139,6 +139,13 @@ export function getCodeElement() {
* click these buttons will copy file content to clipboard
*/
export function attachCopyFileBtn() {
const removeButtons = () => {
const buttons = document.querySelectorAll(`.${copyFileButtonClassName}`)
buttons.forEach(button => {
button.parentElement?.removeChild(button)
})
}
if (getCurrentPageType() === PAGE_TYPES.RAW_TEXT) {
// the button group in file content header
const buttonGroupSelector = '.repository-content .Box-header .BtnGroup'
@ -148,6 +155,8 @@ export function attachCopyFileBtn() {
raiseError(new Error(`No button groups found`))
}
removeButtons() // prevent duplicated buttons
buttonGroups.forEach(async buttonGroup => {
if (!buttonGroup.lastElementChild) return
const button = await renderReact(React.createElement(CopyFileButton))
@ -158,12 +167,7 @@ export function attachCopyFileBtn() {
}
// return callback so that disabling after redirecting from file page to non-page works properly
return () => {
const buttons = document.querySelectorAll(`.${copyFileButtonClassName}`)
buttons.forEach(button => {
button.parentElement?.removeChild(button)
})
}
return removeButtons
}
export function attachCopySnippet() {