}
diff --git a/src-ui/src/app/components/common/input/tags/tags.component.html b/src-ui/src/app/components/common/input/tags/tags.component.html
index 4254b0dca..1227bb35a 100644
--- a/src-ui/src/app/components/common/input/tags/tags.component.html
+++ b/src-ui/src/app/components/common/input/tags/tags.component.html
@@ -17,16 +17,21 @@
[searchFn]="searchFn"
addTagText="Add tag"
i18n-addTagText
+ [keyDownFn]="onKeyDown"
(add)="onAdd($event)"
(change)="onChange(value)">
diff --git a/src-ui/src/app/components/common/input/tags/tags.component.scss b/src-ui/src/app/components/common/input/tags/tags.component.scss
index 94da62699..80c8d8890 100644
--- a/src-ui/src/app/components/common/input/tags/tags.component.scss
+++ b/src-ui/src/app/components/common/input/tags/tags.component.scss
@@ -7,6 +7,21 @@
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;
+}
+
+// let a long (nowrap) split badge shrink to the field instead of widening it
+ng-select,
+::ng-deep .paperless-input-tags .ng-value-container {
+ min-width: 0 !important;
+}
+
+::ng-deep .paperless-input-tags .ng-value {
+ max-width: 100%;
+}
+
.paperless-input-select.disabled {
.input-group {
cursor: not-allowed;
diff --git a/src-ui/src/app/components/common/input/tags/tags.component.spec.ts b/src-ui/src/app/components/common/input/tags/tags.component.spec.ts
index 184a6284a..738a2413b 100644
--- a/src-ui/src/app/components/common/input/tags/tags.component.spec.ts
+++ b/src-ui/src/app/components/common/input/tags/tags.component.spec.ts
@@ -278,4 +278,59 @@ describe('TagsComponent', () => {
component.tags = [lone]
expect(component.getParentChain(5)).toEqual([])
})
+
+ describe('hierarchy', () => {
+ const root: Tag = { id: 1, name: 'root' }
+ const mid: Tag = { id: 2, name: 'mid', parent: 1 }
+ const leaf: Tag = { id: 3, name: 'leaf', parent: 2 }
+ const other: Tag = { id: 4, name: 'other' }
+
+ beforeEach(() => {
+ component.tags = [root, mid, leaf, other]
+ component.value = [3, 2, 1, 4]
+ })
+
+ it('should treat ancestors of another selected tag as implied', () => {
+ expect([1, 2, 3, 4].map((id) => component.isImplied(id))).toEqual([
+ true,
+ true,
+ false,
+ false,
+ ])
+ component.preserveHierarchy = false
+ expect(component.isImplied(1)).toBe(false)
+ })
+
+ it('should remove a whole badge, keeping ancestors other tags still need', () => {
+ component.removeBadge(3)
+ expect(component.value).toEqual([4])
+
+ const sibling: Tag = { id: 5, name: 'sibling', parent: 1 }
+ component.tags = [root, mid, leaf, other, sibling]
+ component.value = [3, 2, 1, 5, 4]
+ component.removeBadge(3)
+ expect(component.value).toEqual([1, 5, 4])
+ })
+
+ it('should remove only the tag itself from a badge when hierarchy is not shown', () => {
+ component.preserveHierarchy = false
+ component.removeBadge(3)
+ expect(component.value).toEqual([2, 1, 4])
+ })
+
+ it('should remove the last visible tag on backspace, not a hidden ancestor', () => {
+ const backspace = new KeyboardEvent('keydown', { key: 'Backspace' })
+ expect(component.onKeyDown(backspace)).toBe(false)
+ expect(component.value).toEqual([3, 2, 1])
+ expect(component.onKeyDown(backspace)).toBe(false)
+ expect(component.value).toEqual([2, 1])
+
+ // nothing hidden: leave it to ng-select
+ component.value = [4]
+ expect(component.onKeyDown(backspace)).toBe(true)
+ expect(
+ component.onKeyDown(new KeyboardEvent('keydown', { key: 'a' }))
+ ).toBe(true)
+ })
+ })
})
diff --git a/src-ui/src/app/components/common/input/tags/tags.component.ts b/src-ui/src/app/components/common/input/tags/tags.component.ts
index b0db1f3d3..710334fd7 100644
--- a/src-ui/src/app/components/common/input/tags/tags.component.ts
+++ b/src-ui/src/app/components/common/input/tags/tags.component.ts
@@ -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,33 @@ 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.showsHierarchy || !this.tags || !this.value) return false
+ return !collapseImpliedAncestors(this.value, this.tagsById).includes(id)
+ }
+
+ private get showsHierarchy(): boolean {
+ return (
+ this.preserveHierarchy &&
+ !!this.settingsService.get(SETTINGS_KEYS.TAGS_SHOW_HIERARCHY)
+ )
+ }
+
+ /**
+ * Remove the last visible tag (in case its actually a hidden ancestor)
+ */
+ readonly onKeyDown = (event: KeyboardEvent): boolean => {
+ if (event.key !== 'Backspace' || this.select?.searchTerm) return true
+ const visible = this.value?.filter((id) => !this.isImplied(id)) ?? []
+ if (visible.length === (this.value?.length ?? 0)) return true
+ this.removeTag(visible.at(-1))
+ return false
+ }
+
getTag(id: number) {
if (this.tags) {
return this.tags.find((tag) => tag.id == id)
@@ -139,6 +173,26 @@ export class TagsComponent implements OnInit, ControlValueAccessor {
}
}
+ /**
+ * Remove a whole badge i.e. the tag plus any ancestors that no other selected
+ * tag still needs.
+ */
+ removeBadge(tagID: number) {
+ if (this.disabled) return
+ if (!this.showsHierarchy) return this.removeTag(tagID)
+
+ const ancestors = new Set(this.getParentChain(tagID).map((t) => t.id))
+ const needed = new Set(
+ this.value
+ .filter((id) => id !== tagID && !ancestors.has(id))
+ .flatMap((id) => this.getParentChain(id).map((t) => t.id))
+ )
+ this.value = this.value.filter(
+ (id) => id !== tagID && (!ancestors.has(id) || needed.has(id))
+ )
+ this.onChange(this.value)
+ }
+
removeTag(tagID: number) {
if (this.disabled) return
@@ -235,18 +289,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()
- 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
+
+ private get tagsById(): Map {
+ 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
}
}
diff --git a/src-ui/src/app/components/common/input/text/text.component.html b/src-ui/src/app/components/common/input/text/text.component.html
index 5c7c5742e..71a4564a6 100644
--- a/src-ui/src/app/components/common/input/text/text.component.html
+++ b/src-ui/src/app/components/common/input/text/text.component.html
@@ -11,7 +11,7 @@
}
-
+
@if (hint) {
}
diff --git a/src-ui/src/app/components/common/input/textarea/textarea.component.html b/src-ui/src/app/components/common/input/textarea/textarea.component.html
index 36e8a01ff..f16e419a8 100644
--- a/src-ui/src/app/components/common/input/textarea/textarea.component.html
+++ b/src-ui/src/app/components/common/input/textarea/textarea.component.html
@@ -18,7 +18,8 @@
[class.font-monospace]="monospace"
[(ngModel)]="value"
[ngModelOptions]="{standalone: true}"
- (change)="onChange(value)"
+ (ngModelChange)="onChange($event)"
+ (blur)="onTouched()"
[disabled]="disabled"
[placeholder]="placeholder"
rows="4">
diff --git a/src-ui/src/app/components/common/input/url/url.component.html b/src-ui/src/app/components/common/input/url/url.component.html
index 85a907452..8e455bfe7 100644
--- a/src-ui/src/app/components/common/input/url/url.component.html
+++ b/src-ui/src/app/components/common/input/url/url.component.html
@@ -10,7 +10,7 @@