-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Filter by file extension #37068
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
McMichalK
wants to merge
14
commits into
go-gitea:main
Choose a base branch
from
McMichalK:filter-by-file-extension
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Filter by file extension #37068
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2ad19a7
Implemented filtering pull request diff files by file extension
McMichalK cf2c438
Code cleanup and self review
McMichalK f66a78a
Fix event listener handling and improve button click behavior in Diff…
McMichalK d939757
Small visual fix for buttons in dropdown
McMichalK 402c244
Enhance keyboard navigation and focus management in DiffFileExtension…
McMichalK 55cb58e
code cleaning
McMichalK 4e79902
Code cleaning, small issues fix
McMichalK 9aa1e69
Refactor DiffFileExtensionFilter to use Composition API, added handli…
McMichalK 5560fe6
Fix type assertion for element selection in DiffFileExtensionFilter
McMichalK 8f34cb3
Update outline color for active filter button in DiffFileExtensionFilter
McMichalK 86fc0e7
Implement search functionality for file extensions in DiffFileExtensi…
McMichalK f377fca
ESLint compliance fix, code cleaning
McMichalK 2547f59
lint-frontend fix
McMichalK 24d9041
code review changes
McMichalK File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,363 @@ | ||
| <script lang="ts" setup> | ||
| import {ref, computed, onMounted, onUnmounted} from 'vue'; | ||
| import {SvgIcon} from '../svg.ts'; | ||
| import {generateElemId} from '../utils/dom.ts'; | ||
|
|
||
| type Extension = { | ||
| ext: string, | ||
| checked: boolean, | ||
| count: number, | ||
| } | ||
|
|
||
| const el = document.querySelector<HTMLElement>('#diff-extension-filter')!; | ||
| const menuVisible = ref(false); | ||
| const extensions = ref<Array<Extension>>([]); | ||
| const isFiltering = ref(false); | ||
| const appliedExtensions = ref<Array<string> | null>(null); | ||
| const searchQuery = ref(''); | ||
| const mutationObserver = ref<MutationObserver | null>(null); | ||
| const uniqueIdMenu = generateElemId('diff-extension-filter-menu-'); | ||
| const locale = { | ||
| filter_by_file_extension: el.getAttribute('data-filter-by-file-extension'), | ||
| select_all: el.getAttribute('data-select-all'), | ||
| deselect_all: el.getAttribute('data-deselect-all'), | ||
| apply: el.getAttribute('data-apply'), | ||
| search: el.getAttribute('data-search'), | ||
| no_file_extension: el.getAttribute('data-no-file-extension'), | ||
| no_file_extensions_found: el.getAttribute('data-no-file-extensions-found'), | ||
| } as Record<string, string>; | ||
|
|
||
| // Subset of extensions shown in the dropdown while the user types in the search box. | ||
| // Does not affect which extensions are checked or which files are hidden — only narrows | ||
| // the visible list to help the user find a specific extension quickly. | ||
| // e.g. searchQuery=".ts" → [.ts, .tsx]; searchQuery="" → all extensions | ||
| const filteredExtensions = computed(() => { | ||
| if (!searchQuery.value.trim()) { | ||
| return extensions.value; | ||
| } | ||
| const query = searchQuery.value.toLowerCase(); | ||
| return extensions.value.filter((ext) => ext.ext.toLowerCase().includes(query)); | ||
| }); | ||
|
|
||
| function getExtension(filename: string): string { | ||
| const lastDot = filename.lastIndexOf('.'); | ||
| if (lastDot === -1) { | ||
| return locale.no_file_extension; | ||
| } | ||
| return filename.substring(lastDot); | ||
| } | ||
|
|
||
| function scanExtensions() { | ||
| const extensionMap = new Map<string, {total: number, visible: number}>(); | ||
| const fileBoxes = document.querySelectorAll<HTMLElement>('#diff-file-boxes .diff-file-box[data-new-filename]'); | ||
|
|
||
| let hiddenCount = 0; | ||
| fileBoxes.forEach((box) => { | ||
| const filename = box.getAttribute('data-new-filename') || ''; | ||
| const ext = getExtension(filename); | ||
| const isHidden = box.classList.contains('tw-hidden'); | ||
| if (!extensionMap.has(ext)) { | ||
| extensionMap.set(ext, {total: 0, visible: 0}); | ||
| } | ||
| const stats = extensionMap.get(ext)!; | ||
| stats.total += 1; | ||
| if (!isHidden) { | ||
| stats.visible += 1; | ||
| } else { | ||
| hiddenCount += 1; | ||
| } | ||
| }); | ||
|
|
||
| extensions.value = Array.from(extensionMap.entries()) | ||
| .map(([ext, stats]) => ({ | ||
| ext, | ||
| checked: appliedExtensions.value ? appliedExtensions.value.includes(ext) : stats.visible > 0, | ||
| count: stats.total, | ||
| })) | ||
| .sort((a, b) => b.count - a.count); | ||
|
|
||
| isFiltering.value = hiddenCount > 0; | ||
| } | ||
|
|
||
| function applyFilterToFileBoxes(checkedExtensions: Set<string>) { | ||
| const fileBoxes = document.querySelectorAll<HTMLElement>('#diff-file-boxes .diff-file-box[data-new-filename]'); | ||
| let hiddenCount = 0; | ||
|
|
||
| fileBoxes.forEach((box) => { | ||
| const filename = box.getAttribute('data-new-filename') || ''; | ||
| const ext = getExtension(filename); | ||
| const isChecked = checkedExtensions.has(ext); | ||
|
|
||
| if (isChecked) { | ||
| box.classList.remove('tw-hidden'); | ||
| } else { | ||
| box.classList.add('tw-hidden'); | ||
| hiddenCount += 1; | ||
| } | ||
| }); | ||
|
|
||
| isFiltering.value = hiddenCount > 0; | ||
| appliedExtensions.value = hiddenCount > 0 ? Array.from(checkedExtensions) : null; | ||
| } | ||
|
|
||
| function focusElem(elem: HTMLElement | null, prevElem: HTMLElement | null) { | ||
| if (elem) { | ||
| elem.tabIndex = 0; | ||
| if (prevElem) prevElem.tabIndex = -1; | ||
| // Focus the input/button inside the menuitem if it exists, otherwise focus the item itself | ||
| const focusTarget = elem.querySelector('input, button') as HTMLElement || elem; | ||
| focusTarget.focus(); | ||
| } | ||
| } | ||
|
|
||
| function toggleMenu() { | ||
| menuVisible.value = !menuVisible.value; | ||
| if (menuVisible.value) { | ||
| searchQuery.value = ''; | ||
| scanExtensions(); | ||
| setTimeout(() => { | ||
| const searchInput = el.querySelector('.diff-ext-search-input') as HTMLInputElement; | ||
| if (searchInput) searchInput.focus(); | ||
| }, 0); | ||
| } | ||
| } | ||
|
|
||
| function selectAll() { | ||
| for (const ext of extensions.value) { | ||
| ext.checked = true; | ||
| } | ||
| } | ||
|
|
||
| function deselectAll() { | ||
| for (const ext of extensions.value) { | ||
| ext.checked = false; | ||
| } | ||
| } | ||
|
|
||
| function applyFilter() { | ||
| const checkedExtensions = new Set(extensions.value.filter((e) => e.checked).map((e) => e.ext)); | ||
| applyFilterToFileBoxes(checkedExtensions); | ||
| toggleMenu(); | ||
| } | ||
|
|
||
| /** | ||
| * Handle keyboard navigation within the dropdown menu | ||
| * Arrow Up/Down: navigate through checkboxes and buttons | ||
| * Space/Enter: toggle checkboxes or activate buttons | ||
| * Escape: close the dropdown | ||
| * @param event Keyboard event | ||
| */ | ||
| function onKeyDown(event: KeyboardEvent) { | ||
| if (!menuVisible.value) return; | ||
| const currentFocused = document.activeElement as HTMLElement; | ||
| if (!el.contains(currentFocused)) return; | ||
|
|
||
| const menu = el.querySelector('.menu') as HTMLElement; | ||
| const focusableItems = Array.from(menu.querySelectorAll('[role="menuitem"]')) as HTMLElement[]; | ||
|
|
||
| if (!focusableItems.length) return; | ||
|
|
||
| const currentIndex = focusableItems.indexOf(currentFocused.closest('[role="menuitem"]') as HTMLElement); | ||
|
|
||
| switch (event.key) { | ||
| case 'ArrowDown': { | ||
| event.preventDefault(); | ||
| const nextIndex = currentIndex === -1 ? 0 : Math.min(currentIndex + 1, focusableItems.length - 1); | ||
| focusElem(focusableItems[nextIndex], currentIndex >= 0 ? focusableItems[currentIndex] : null); | ||
| break; | ||
| } | ||
| case 'ArrowUp': { | ||
| event.preventDefault(); | ||
| const prevIndex = currentIndex === -1 ? focusableItems.length - 1 : Math.max(currentIndex - 1, 0); | ||
| focusElem(focusableItems[prevIndex], currentIndex >= 0 ? focusableItems[currentIndex] : null); | ||
| break; | ||
| } | ||
| case ' ': | ||
| case 'Enter': { | ||
| event.preventDefault(); | ||
| const currentElement = document.activeElement as HTMLElement; | ||
|
|
||
| // Try to find and toggle a checkbox (currentElement may be the input itself or a parent) | ||
| const checkbox = (currentElement?.matches('input[type="checkbox"]') | ||
| ? currentElement | ||
| : currentElement?.querySelector('input[type="checkbox"]')) as HTMLInputElement | null; | ||
| if (checkbox) { | ||
| checkbox.checked = !checkbox.checked; | ||
| checkbox.dispatchEvent(new Event('change', {bubbles: true})); | ||
| break; | ||
| } | ||
|
|
||
| // If focused element is a button, click it | ||
| if (currentElement?.tagName === 'BUTTON') { | ||
| currentElement.click(); | ||
| } | ||
| break; | ||
| } | ||
| case 'Escape': | ||
| event.preventDefault(); | ||
| if (currentIndex >= 0) { | ||
| focusableItems[currentIndex].tabIndex = -1; | ||
| } | ||
| toggleMenu(); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| onMounted(() => { | ||
| el.addEventListener('keydown', onKeyDown); | ||
|
|
||
| // Watch for new files being added (e.g., when "load more" is clicked) | ||
| const fileBoxesContainer = document.querySelector('#diff-file-boxes'); | ||
| if (fileBoxesContainer) { | ||
| mutationObserver.value = new MutationObserver(() => { | ||
| if (appliedExtensions.value) { | ||
| applyFilterToFileBoxes(new Set(appliedExtensions.value)); | ||
| } | ||
|
|
||
| if (menuVisible.value) { | ||
| scanExtensions(); | ||
| } | ||
| }); | ||
| mutationObserver.value.observe(fileBoxesContainer, {childList: true, subtree: false}); | ||
| } | ||
| }); | ||
|
|
||
| onUnmounted(() => { | ||
| el.removeEventListener('keydown', onKeyDown); | ||
|
|
||
| if (mutationObserver.value) { | ||
| mutationObserver.value.disconnect(); | ||
| } | ||
| }); | ||
| </script> | ||
| <template> | ||
| <div class="ui scrolling dropdown custom diff-file-extension-filter"> | ||
| <div v-if="menuVisible" class="diff-ext-backdrop" @click="toggleMenu()"/> | ||
| <button | ||
| class="ui tiny basic button" | ||
| :class="{'diff-ext-filter-btn-active': isFiltering}" | ||
| @click="toggleMenu()" | ||
| :data-tooltip-content="locale.filter_by_file_extension" | ||
| aria-haspopup="true" | ||
| :aria-label="locale.filter_by_file_extension" | ||
| :aria-controls="uniqueIdMenu" | ||
| > | ||
| <SvgIcon name="octicon-filter"/> | ||
| </button> | ||
| <!-- this dropdown is not managed by Fomantic UI, so it needs some classes like "transition" explicitly --> | ||
| <div class="left menu transition" :id="uniqueIdMenu" :class="{visible: menuVisible}" v-show="menuVisible" v-cloak :aria-expanded="menuVisible ? 'true': 'false'"> | ||
| <div class="header">{{ locale.filter_by_file_extension }}</div> | ||
| <div class="ui divider tw-mt-2 tw-mb-0"/> | ||
| <!-- Search input --> | ||
| <div class="ui form tw-mb-2"> | ||
| <div class="ui input fluid field tw-mb-0"> | ||
| <input | ||
| type="text" | ||
| v-model="searchQuery" | ||
| class="diff-ext-search-input" | ||
| :placeholder="locale.search" | ||
| @keydown.escape="toggleMenu()" | ||
| > | ||
| </div> | ||
| </div> | ||
| <div class="ui divider tw-mt-2 tw-mb-0"/> | ||
| <div class="ui form"> | ||
| <!-- Extension checkboxes --> | ||
| <div class="grouped fields"> | ||
| <div v-if="filteredExtensions.length > 0"> | ||
| <template v-for="ext in filteredExtensions" :key="ext.ext"> | ||
| <div class="field" role="menuitem" tabindex="-1"> | ||
| <div class="ui checkbox"> | ||
| <input | ||
| type="checkbox" | ||
| :id="`ext-filter-${ext.ext}`" | ||
| v-model="ext.checked" | ||
| > | ||
| <label :for="`ext-filter-${ext.ext}`" class="tw-cursor-pointer"> | ||
| <span>{{ ext.ext }}</span> | ||
| <span class="tw-text-text-light-2"> ({{ ext.count }})</span> | ||
| </label> | ||
| </div> | ||
| </div> | ||
| </template> | ||
| </div> | ||
| <div v-if="filteredExtensions.length === 0" class="tw-py-4 tw-text-center tw-text-text-light-2"> | ||
| {{ locale.no_file_extensions_found }} | ||
| </div> | ||
| </div> | ||
| </div> | ||
|
|
||
| <!-- Select all / Deselect all buttons --> | ||
| <div class="ui divider tw-my-2"/> | ||
| <div class="tw-flex tw-items-center tw-justify-center tw-gap-4 tw-px-2 tw-py-1"> | ||
| <button type="button" class="diff-ext-text-btn" tabindex="-1" role="menuitem" @click="selectAll()">{{ locale.select_all }}</button> | ||
| <button type="button" class="diff-ext-text-btn" tabindex="-1" role="menuitem" @click="deselectAll()">{{ locale.deselect_all }}</button> | ||
| </div> | ||
|
|
||
| <!-- Apply button --> | ||
| <div class="ui divider tw-my-2"/> | ||
| <button type="button" class="ui button fluid" tabindex="-1" role="menuitem" @click="applyFilter()"> | ||
| {{ locale.apply }} | ||
| </button> | ||
| </div> | ||
| </div> | ||
| </template> | ||
| <style scoped> | ||
| .ui.dropdown.diff-file-extension-filter .menu { | ||
| margin-top: 0.25em; | ||
| overflow-x: hidden; | ||
| max-height: 450px; | ||
| padding: 0.75rem; | ||
| padding-top: 0.5rem; | ||
| } | ||
|
|
||
| .ui.dropdown.diff-file-extension-filter .menu > .header { | ||
| margin-top: 0; | ||
| padding-top: 0; | ||
| } | ||
|
|
||
| .ui.dropdown.diff-file-extension-filter .menu .ui.form { | ||
| margin: 0; | ||
| } | ||
|
|
||
| .ui.dropdown.diff-file-extension-filter .grouped.fields > div > .field { | ||
| margin-bottom: 0.5rem; | ||
| } | ||
|
|
||
| .ui.dropdown.diff-file-extension-filter .grouped.fields > div > .field:last-child { | ||
| margin-bottom: 0; | ||
| } | ||
|
|
||
| .ui.dropdown.diff-file-extension-filter .diff-ext-filter-btn-active { | ||
| outline: 1px solid var(--color-primary); | ||
| outline-offset: -2px; | ||
| } | ||
|
|
||
| .ui.dropdown.diff-file-extension-filter .diff-ext-text-btn { | ||
| background: none; | ||
| border: none; | ||
| padding: 0; | ||
| color: var(--color-primary); | ||
| cursor: pointer; | ||
| font-size: inherit; | ||
| text-align: center; | ||
| } | ||
|
|
||
| .ui.dropdown.diff-file-extension-filter .diff-ext-text-btn:hover { | ||
| text-decoration: underline; | ||
| } | ||
|
|
||
| .ui.dropdown.diff-file-extension-filter .diff-ext-search-input { | ||
| width: 100%; | ||
| } | ||
|
|
||
| .diff-ext-backdrop { | ||
| position: fixed; | ||
| inset: 0; | ||
| z-index: 10; | ||
| } | ||
|
|
||
| .ui.dropdown.diff-file-extension-filter .ui.input { | ||
| margin-bottom: 0.5rem; | ||
| } | ||
| </style> | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.