diff options
author | 2021-05-25 09:54:41 +0300 | |
---|---|---|
committer | 2021-05-25 13:54:41 +0700 | |
commit | 2c6cbdf9c84329a06b62461c615f97ef935e5459 (patch) | |
tree | 570f9e3825272c2ae1568709b5fdc5b29fbcaa2c /source/features/one-click-pr-or-gist.tsx | |
parent | bb1e212200ee4917a964600248a522516bac45ef (diff) | |
download | refined-github-2c6cbdf9c84329a06b62461c615f97ef935e5459.tar.gz refined-github-2c6cbdf9c84329a06b62461c615f97ef935e5459.tar.zst refined-github-2c6cbdf9c84329a06b62461c615f97ef935e5459.zip |
Extend `separate-draft-pr-button` to `one-click-pr-or-gist` (#4382)
Diffstat (limited to 'source/features/one-click-pr-or-gist.tsx')
-rw-r--r-- | source/features/one-click-pr-or-gist.tsx | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/source/features/one-click-pr-or-gist.tsx b/source/features/one-click-pr-or-gist.tsx new file mode 100644 index 00000000..806cd53d --- /dev/null +++ b/source/features/one-click-pr-or-gist.tsx @@ -0,0 +1,52 @@ +import './one-click-pr-or-gist.css'; +import React from 'dom-chef'; +import select from 'select-dom'; +import * as pageDetect from 'github-url-detection'; + +import features from '.'; + +function init(): void | false { + const draftPROption = select('.new-pr-form [name="draft"], #new_gist [name="gist[public]"]'); + if (!draftPROption) { + // 1. Free accounts can't open Draft PRs in private repos, so this element is missing + // 2. PRs can't be created from some comparison pages: Either base is a tag, not a branch; or there already exists a PR. + return false; + } + + const initialGroupedButtons = draftPROption.closest('.BtnGroup')!; + + for (const dropdownItem of select.all('.select-menu-item', initialGroupedButtons)) { + let title = select('.select-menu-item-heading', dropdownItem)!.textContent!.trim(); + const description = select('.description', dropdownItem)!.textContent!.trim(); + const radioButton = select('input[type=radio]', dropdownItem)!; + const classList = ['btn', 'ml-2', 'tooltipped', 'tooltipped-s']; + + if (/\bdraft\b/i.test(title)) { + title = 'Create draft PR'; + } else { + classList.push('btn-primary'); + } + + initialGroupedButtons.after( + <button + className={classList.join(' ')} + aria-label={description} + type="submit" + name={radioButton.name} + value={radioButton.value} + > + {title} + </button> + ); + } + + initialGroupedButtons.remove(); +} + +void features.add(__filebasename, { + include: [ + pageDetect.isCompare, + pageDetect.isGist + ], + init +}); |