Use the ancestor helper and switch based on the setting in tags component

This commit is contained in:
shamoon committed 2026-11-21 10:29:36 -08:00
1 parent 4ef982e9dc
commit f95abddaf4
3 files changed
+48 -18

No files matched your search

@@ -21,12 +21,16 @@
(change)="onChange(value)">
<ng-template ng-label-tmp let-item="item">
<button class="tag-wrap btn p-0 d-flex align-items-center" (click)="removeTag(item.id)" (mousedown)="$event.stopImmediatePropagation()" type="button" title="Remove tag" i18n-title>
<pngx-bs-icon name="x" style="margin-inline-end: 1px;"></pngx-bs-icon>
@if (item.id && tags) {
<pngx-tag style="background-color: none;" [tag]="getTag(item.id)"></pngx-tag>
}
</button>
@if (isImplied(item.id)) {
<span class="implied-tag"></span>
} @else {
<button class="tag-wrap btn p-0 d-flex align-items-center mw-100" (click)="removeTag(item.id)" (mousedown)="$event.stopImmediatePropagation()" type="button" title="Remove tag" i18n-title>
<pngx-bs-icon name="x" style="margin-inline-end: 1px;"></pngx-bs-icon>
@if (item.id && tags) {
<pngx-tag class="mw-100 overflow-hidden" style="background-color: none;" [tag]="getTag(item.id)" [split]="preserveHierarchy" (parentClick)="removeTag($event)"></pngx-tag>
}
</button>
}
</ng-template>
<ng-template ng-option-tmp let-item="item" let-index="index" let-search="searchTerm">
<div class="tag-option-row d-flex align-items-center" [class.w-auto]="!getTag(item.id)?.parent">
@@ -7,6 +7,11 @@
font-size: 1rem;
}
// ancestors of another selected tag render inside that tag's badge
::ng-deep .paperless-input-tags .ng-value:has(> .implied-tag) {
display: none !important;
}
.paperless-input-select.disabled {
.input-group {
cursor: not-allowed;
@@ -21,7 +21,13 @@ import { NgSelectComponent, NgSelectModule } from '@ng-select/ng-select'
import { first, firstValueFrom, tap } from 'rxjs'
import { BootstrapIconComponent } from 'src/app/components/common/bootstrap-icon/bootstrap-icon.component'
import { Tag } from 'src/app/data/tag'
import { SETTINGS_KEYS } from 'src/app/data/ui-settings'
import { TagService } from 'src/app/services/rest/tag.service'
import { SettingsService } from 'src/app/services/settings.service'
import {
collapseImpliedAncestors,
getTagAncestors,
} from 'src/app/utils/collapse-tags'
import { matchesSearchText } from 'src/app/utils/text-search'
import { EditDialogMode } from '../../edit-dialog/edit-dialog.component'
import { TagEditDialogComponent } from '../../edit-dialog/tag-edit-dialog/tag-edit-dialog.component'
@@ -50,6 +56,7 @@ import { TagComponent } from '../../tag/tag.component'
export class TagsComponent implements OnInit, ControlValueAccessor {
private tagService = inject(TagService)
private modalService = inject(NgbModal)
private readonly settingsService = inject(SettingsService)
private readonly changeDetector = inject(ChangeDetectorRef)
constructor() {
@@ -131,6 +138,21 @@ export class TagsComponent implements OnInit, ControlValueAccessor {
term
)
/**
* Selected tags that are ancestors of another selected tag which are shown
* as segments of their descendant's badge.
*/
isImplied(id: number): boolean {
if (
!this.preserveHierarchy ||
!this.tags ||
!this.value ||
!this.settingsService.get(SETTINGS_KEYS.TAGS_SHOW_HIERARCHY)
)
return false
return !collapseImpliedAncestors(this.value, this.tagsById).includes(id)
}
getTag(id: number) {
if (this.tags) {
return this.tags.find((tag) => tag.id == id)
@@ -235,18 +257,17 @@ export class TagsComponent implements OnInit, ControlValueAccessor {
}
getParentChain(id: number): Tag[] {
// Returns ancestors from root → immediate parent for a tag id
const chain: Tag[] = []
let current = this.getTag(id)
const guard = new Set<number>()
while (current?.parent) {
if (guard.has(current.parent)) break
guard.add(current.parent)
const parent = this.getTag(current.parent)
if (!parent) break
chain.unshift(parent)
current = parent
return getTagAncestors(id, this.tagsById)
}
private tagsByIdSource: Tag[]
private tagsByIdCache: Map<number, Tag>
private get tagsById(): Map<number, Tag> {
if (this.tagsByIdSource !== this.tags) {
this.tagsByIdSource = this.tags
this.tagsByIdCache = new Map(this.tags?.map((t) => [t.id, t]))
}
return chain
return this.tagsByIdCache
}
}