-
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.
Merge pull request #5 from pt1602/feature/add-mr-copy-brach-name-button
feat(mergeRequestPage): added button to copy branch name
- Loading branch information
Showing
2 changed files
with
92 additions
and
17 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 |
---|---|---|
@@ -1,22 +1,63 @@ | ||
let cloneButtons: NodeList | null = document.querySelectorAll('#ssh_project_clone, #http_project_clone'); | ||
let cloneOptionsDropdown: NodeList | null = document.querySelectorAll('.clone-options-dropdown > li > a'); | ||
let mobileCloneButtons: Element | null = document.querySelector('.mobile-git-clone .clone-dropdown-btn'); | ||
let mobileCloneButtons: HTMLElement | null = document.querySelector('.mobile-git-clone .clone-dropdown-btn'); | ||
const currentPath: string = window.location.pathname; | ||
const beforeVal: string = 'git clone '; | ||
const copyNameBtnVal: string = 'Copy Branch Name'; | ||
const copyNameBtnClasses: string = 'gl-display-none gl-md-display-block btn gl-button btn-default btn-grouped'; | ||
let branchNameEl: HTMLElement | null; | ||
|
||
if (cloneButtons) { | ||
cloneButtons.forEach(function (item) { | ||
(item as HTMLInputElement).value = beforeVal + (item as HTMLInputElement).value; | ||
}); | ||
function modifyMobileCloneButtons() { | ||
(mobileCloneButtons! as HTMLInputElement).dataset.clipboardText = beforeVal + (mobileCloneButtons! as HTMLInputElement).dataset.clipboardText; | ||
} | ||
|
||
if (mobileCloneButtons) { | ||
function modifyMobileCloneButtons() { | ||
(mobileCloneButtons! as HTMLInputElement).dataset.clipboardText = beforeVal + (mobileCloneButtons! as HTMLInputElement).dataset.clipboardText; | ||
function createCopyBranchNameButton(branchNameEl: HTMLElement) { | ||
const headerActions: HTMLElement | null = document.querySelector('.detail-page-header-actions'); | ||
const branchName: string = branchNameEl.innerHTML; | ||
let cloneBranchNameBtn: HTMLElement | null = document.createElement("button"); | ||
cloneBranchNameBtn.className = copyNameBtnClasses; | ||
cloneBranchNameBtn.innerText = copyNameBtnVal; | ||
|
||
if (branchName) { | ||
cloneBranchNameBtn.addEventListener('click', () => { | ||
navigator.clipboard.writeText(branchName).then( | ||
() => { | ||
/* success - do nothing */ | ||
}, | ||
() => { | ||
/* failure - do nothing */ | ||
} | ||
); | ||
}); | ||
} | ||
|
||
modifyMobileCloneButtons(); | ||
if (headerActions) { | ||
headerActions.prepend(cloneBranchNameBtn); | ||
} | ||
} | ||
|
||
if (currentPath.includes('merge_requests/')) { | ||
let checkInterval = window.setInterval(() => { | ||
branchNameEl = document.querySelector('.label-branch a'); | ||
|
||
if (branchNameEl) { | ||
createCopyBranchNameButton(branchNameEl); | ||
clearInterval(checkInterval); | ||
return; | ||
} | ||
}, 300); | ||
} | ||
|
||
try { | ||
cloneButtons.forEach(function (item) { | ||
(item as HTMLInputElement).value = beforeVal + (item as HTMLInputElement).value; | ||
}); | ||
|
||
cloneOptionsDropdown.forEach(item => { | ||
item.addEventListener('click', modifyMobileCloneButtons); | ||
}); | ||
} | ||
|
||
modifyMobileCloneButtons(); | ||
} catch (err) { | ||
// do nothing | ||
} |