diff options
author | 2023-11-09 12:12:44 -0500 | |
---|---|---|
committer | 2023-11-10 01:12:44 +0800 | |
commit | efac2cd0b633f07d299c21be1665e93f0124f03b (patch) | |
tree | bf7a6388831e0646fcb84b4e889546a6319b4b7d /source | |
parent | bccc37b757e87ef9c3def6fbb5786ed174600ed8 (diff) | |
download | refined-github-efac2cd0b633f07d299c21be1665e93f0124f03b.tar.gz refined-github-efac2cd0b633f07d299c21be1665e93f0124f03b.tar.zst refined-github-efac2cd0b633f07d299c21be1665e93f0124f03b.zip |
`easy-toggle-files` - Apply to code search (#7036)
Co-authored-by: Federico Brigante <me@fregante.com>
Diffstat (limited to 'source')
-rw-r--r-- | source/features/easy-toggle-files.tsx | 34 | ||||
-rw-r--r-- | source/github-helpers/selectors.ts | 5 |
2 files changed, 39 insertions, 0 deletions
diff --git a/source/features/easy-toggle-files.tsx b/source/features/easy-toggle-files.tsx index db6d0381..74ed87e2 100644 --- a/source/features/easy-toggle-files.tsx +++ b/source/features/easy-toggle-files.tsx @@ -2,7 +2,9 @@ import {$} from 'select-dom'; import delegate, {DelegateEvent} from 'delegate-it'; import * as pageDetect from 'github-url-detection'; +import {codeSearchHeader} from '../github-helpers/selectors.js'; import features from '../feature-manager.js'; +import {isHasSelectorSupported} from '../helpers/select-has.js'; function toggleFile(event: DelegateEvent<MouseEvent>): void { const elementClicked = event.target as HTMLElement; @@ -15,14 +17,46 @@ function toggleFile(event: DelegateEvent<MouseEvent>): void { } } +function toggleCodeSearchFile(event: DelegateEvent<MouseEvent>): void { + const elementClicked = event.target as HTMLElement; + const headerBar = event.delegateTarget; + const toggle = $(':scope > button', headerBar)!; + + // The clicked element is either the bar itself or one of its children excluding the button + if (elementClicked === headerBar || (elementClicked.parentElement === headerBar && elementClicked !== toggle)) { + toggle.dispatchEvent(new MouseEvent('click', {bubbles: true, altKey: event.altKey})); + } +} + function init(signal: AbortSignal): void { delegate('.file-header', 'click', toggleFile, {signal}); } +function initSearchPage(signal: AbortSignal): void { + delegate(codeSearchHeader, 'click', toggleCodeSearchFile, {signal}); +} + void features.add(import.meta.url, { include: [ pageDetect.hasFiles, pageDetect.isGistRevision, ], init, +}, { + asLongAs: [ + isHasSelectorSupported, + ], + include: [ + pageDetect.isGlobalSearchResults, + ], + init: initSearchPage, }); + +/* + +## Test URLs + +- Pull Request: https://github.com/refined-github/refined-github/pull/7036/files +- Code Search: https://github.com/search?q=repo%3Arefined-github%2Frefined-github%20easy&type=code + +*/ diff --git a/source/github-helpers/selectors.ts b/source/github-helpers/selectors.ts index 453b4bc5..6b121ec8 100644 --- a/source/github-helpers/selectors.ts +++ b/source/github-helpers/selectors.ts @@ -48,3 +48,8 @@ export const actionsTab = '#actions-tab'; export const actionsTab_ = [ 'https://github.com/refined-github/sandbox', ]; + +export const codeSearchHeader = 'div:has(>:is([aria-label^="Collapse "], [aria-label^="Expand "]))'; +export const codeSearchHeader_ = [ + 'https://github.com/search?q=repo%3Arefined-github%2Frefined-github&type=code', +]; |