diff --git a/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.html b/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.html index 7c21c0a7..be417d50 100644 --- a/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.html +++ b/webapp/src/app/modules/keywords-search/search-results/news-results/news-results.component.html @@ -39,26 +39,6 @@ } - - @if (article.snippet) { - @let expanded = isSnippetExpanded(article); - - } - -
+ [detailTemplate]="snippetTmp" + [canExpand]="hasSnippet" />
0 || this.selectedTopicIds().length > 0; }); - expandTmp = viewChild.required>('expandTmp'); titleTmp = viewChild.required>('titleTmp'); dateTmp = viewChild.required>('dateTmp'); relevanceTmp = viewChild.required>('relevanceTmp'); topicsTmp = viewChild.required>('topicsTmp'); actionsTmp = viewChild.required>('actionsTmp'); - private readonly dataTable = viewChild(DataTableComponent); - readonly columns = computed[]>(() => [ - { - id: 'expand', - header: '', - type: 'display', - cellTemplate: this.expandTmp(), - }, { id: 'maintitle', header: 'Titel', @@ -261,10 +250,6 @@ export class KeywordsToNewsResultsComponent { return !!topic.id && this.selectedTopicIds().includes(topic.id); } - isSnippetExpanded(article: NewsResult): boolean { - return this.dataTable()?.expandedElement() === article; - } - readonly snippetHighlights = computed(() => { const byArticleId = new Map(); @@ -332,6 +317,8 @@ export class KeywordsToNewsResultsComponent { return result; } + readonly hasSnippet = (article: NewsResult): boolean => !!article.snippet; + isTopicsExpanded(article: NewsResult): boolean { return this.expandedTopicsIds().has(article.id); } diff --git a/webapp/src/app/modules/keywords-search/search-results/works-results/works-results.component.html b/webapp/src/app/modules/keywords-search/search-results/works-results/works-results.component.html index aa5496a9..58e23f77 100644 --- a/webapp/src/app/modules/keywords-search/search-results/works-results/works-results.component.html +++ b/webapp/src/app/modules/keywords-search/search-results/works-results/works-results.component.html @@ -11,15 +11,7 @@ Fundstelle im {{ snippetSourceLabel(work) }} -

- @for (segment of segments; track $index) { - @if (segment.isSearchMatch) { - {{ segment.text }} - } @else { - {{ segment.text }} - } - } -

+ } @else if (work.matched_via?.length) { Kein Treffer im Text

diff --git a/webapp/src/app/modules/keywords-search/search-results/works-results/works-results.component.scss b/webapp/src/app/modules/keywords-search/search-results/works-results/works-results.component.scss index 7cab69b1..6449903c 100644 --- a/webapp/src/app/modules/keywords-search/search-results/works-results/works-results.component.scss +++ b/webapp/src/app/modules/keywords-search/search-results/works-results/works-results.component.scss @@ -121,12 +121,6 @@ overflow-wrap: anywhere; } -.snippet-mark { - background: transparent; - color: var(--sg-blue-highlight); - font-weight: 700; -} - .matched-topic { color: var(--sg-text); font-weight: 600; diff --git a/webapp/src/app/modules/keywords-search/search-results/works-results/works-results.component.ts b/webapp/src/app/modules/keywords-search/search-results/works-results/works-results.component.ts index ec24a966..13077372 100644 --- a/webapp/src/app/modules/keywords-search/search-results/works-results/works-results.component.ts +++ b/webapp/src/app/modules/keywords-search/search-results/works-results/works-results.component.ts @@ -18,20 +18,11 @@ import { Work, } from '../../../../api'; import { SearchResultsStateService } from '../search-results-state.service'; - -interface SnippetSegment { - text: string; - isSearchMatch: boolean; -} - -function decodeHtml(text: string): string { - return text - .replace(/</g, '<') - .replace(/>/g, '>') - .replace(/"/g, '"') - .replace(/&#(?:39|x27);/g, "'") - .replace(/&/g, '&'); -} +import { + SnippetSegment, + splitSnippet, +} from '../../../../resolvers/utils/keyword-highlight'; +import { SnippetTextComponent } from '../../../../shared/ui/snippet-text/snippet-text.component'; @Component({ selector: 'app-works-results', @@ -42,6 +33,7 @@ function decodeHtml(text: string): string { PublicationTableComponent, PublicationFilterComponent, QueryParamPaginatorComponent, + SnippetTextComponent, ], templateUrl: './works-results.component.html', styleUrl: './works-results.component.scss', @@ -90,37 +82,12 @@ export class KeywordsToWorksResultsComponent implements OnInit { if (!work.id || !work.snippet) { continue; } - byWorkId.set(work.id, this.buildSegments(work.snippet)); + byWorkId.set(work.id, splitSnippet(work.snippet)); } return byWorkId; }); - private buildSegments(snippet: string): SnippetSegment[] { - const searchMatch = /([\s\S]*?)<\/em>/g; - const segments: SnippetSegment[] = []; - - let lastIndex = 0; - let match: RegExpExecArray | null; - - const pushPlain = (raw: string) => { - const text = decodeHtml(raw); - if (text) { - segments.push({ text, isSearchMatch: false }); - } - }; - - while ((match = searchMatch.exec(snippet)) !== null) { - pushPlain(snippet.slice(lastIndex, match.index)); - segments.push({ text: decodeHtml(match[1]), isSearchMatch: true }); - lastIndex = searchMatch.lastIndex; - } - - pushPlain(snippet.slice(lastIndex)); - - return segments; - } - snippetSourceLabel(work: Work): string { return work.snippet_field === 'title' ? 'Titel' : 'Abstract'; } diff --git a/webapp/src/app/resolvers/utils/keyword-highlight.ts b/webapp/src/app/resolvers/utils/keyword-highlight.ts index b22ec493..c48459d1 100644 --- a/webapp/src/app/resolvers/utils/keyword-highlight.ts +++ b/webapp/src/app/resolvers/utils/keyword-highlight.ts @@ -166,6 +166,67 @@ export function highlightKeywords( .join(''); } +export interface SnippetSegment { + text: string; + isSearchMatch: boolean; + match: KeywordColor | null; + groupHasMatch: boolean; +} + +export function splitSnippet( + snippet: string, + keywordColors: readonly KeywordColor[] = [], +): SnippetSegment[] { + const searchMatch = /([\s\S]*?)<\/em>/g; + const segments: SnippetSegment[] = []; + + let lastIndex = 0; + let match: RegExpExecArray | null; + + const pushPlain = (raw: string) => { + const text = decodeHtml(raw); + if (text) { + segments.push({ + text, + isSearchMatch: false, + match: null, + groupHasMatch: false, + }); + } + }; + + while ((match = searchMatch.exec(snippet)) !== null) { + pushPlain(snippet.slice(lastIndex, match.index)); + + const parts = splitByKeywords(decodeHtml(match[1]), keywordColors); + const groupHasMatch = parts.some((part) => part.match); + + for (const part of parts) { + segments.push({ + text: part.text, + isSearchMatch: true, + match: part.match, + groupHasMatch, + }); + } + + lastIndex = searchMatch.lastIndex; + } + + pushPlain(snippet.slice(lastIndex)); + + return segments; +} + +export function decodeHtml(text: string): string { + return text + .replace(/</g, '<') + .replace(/>/g, '>') + .replace(/"/g, '"') + .replace(/&#(?:39|x27);/g, "'") + .replace(/&/g, '&'); +} + export function escapeHtml(text: string): string { return text .replace(/&/g, '&') diff --git a/webapp/src/app/shared/ui/data-table/data-table.component.html b/webapp/src/app/shared/ui/data-table/data-table.component.html index 61871fe2..5834271f 100644 --- a/webapp/src/app/shared/ui/data-table/data-table.component.html +++ b/webapp/src/app/shared/ui/data-table/data-table.component.html @@ -8,6 +8,31 @@ matSort (matSortChange)="_handleSortChange($event)" multiTemplateDataRows> + @if (detailTemplate()) { + + + + @if (isExpandable(row)) { + @let expanded = row === expandedElement(); + + } + + + } + @for (col of columns(); track col.id) { @if (col.headerTemplate) { diff --git a/webapp/src/app/shared/ui/data-table/data-table.component.scss b/webapp/src/app/shared/ui/data-table/data-table.component.scss index 05b619f9..0f27deef 100644 --- a/webapp/src/app/shared/ui/data-table/data-table.component.scss +++ b/webapp/src/app/shared/ui/data-table/data-table.component.scss @@ -27,3 +27,18 @@ tr.detail-row { background: var(--sg-surface-soft); cursor: pointer; } + +.expand-cell { + width: 1%; + white-space: nowrap; + padding-right: 0; +} + +.expand-icon { + color: var(--sg-text-muted); + transition: transform 150ms ease-in-out; + + &.is-expanded { + transform: rotate(180deg); + } +} diff --git a/webapp/src/app/shared/ui/data-table/data-table.component.ts b/webapp/src/app/shared/ui/data-table/data-table.component.ts index 092488b2..e6a188b6 100644 --- a/webapp/src/app/shared/ui/data-table/data-table.component.ts +++ b/webapp/src/app/shared/ui/data-table/data-table.component.ts @@ -19,6 +19,9 @@ import { SortDirection, } from '@angular/material/sort'; import { MatTableDataSource, MatTableModule } from '@angular/material/table'; +import { MatButtonModule } from '@angular/material/button'; +import { MatIconModule } from '@angular/material/icon'; +import { MatTooltipModule } from '@angular/material/tooltip'; import { ActivatedRoute, Router } from '@angular/router'; import { SkTextDirective } from '../../../custom-directives/sk-text.directive'; export interface BaseColumn { @@ -43,7 +46,15 @@ export type GenericColumn = AccessorColumn | DisplayColumn; @Component({ selector: 'app-data-table', standalone: true, - imports: [MatTableModule, NgTemplateOutlet, MatSortModule, SkTextDirective], + imports: [ + MatTableModule, + NgTemplateOutlet, + MatSortModule, + MatButtonModule, + MatIconModule, + MatTooltipModule, + SkTextDirective, + ], templateUrl: './data-table.component.html', styleUrl: './data-table.component.scss', }) @@ -54,6 +65,7 @@ export class DataTableComponent implements AfterViewInit { detailTemplate = input | null>(null); expandedElement = signal(null); + canExpand = input<((row: T) => boolean) | null>(null); maxHeight = input(null); containerOverflow = computed(() => (this.maxHeight() ? 'auto' : 'visible')); @@ -78,7 +90,12 @@ export class DataTableComponent implements AfterViewInit { handleSortChange = output(); - displayedColumns = computed(() => this.columns().map((col) => col.id)); + readonly expandColumnId = 'expand'; + + displayedColumns = computed(() => { + const ids = this.columns().map((col) => col.id); + return this.detailTemplate() ? [this.expandColumnId, ...ids] : ids; + }); dataSource = new MatTableDataSource([]); sort = viewChild.required(MatSort); @@ -160,8 +177,13 @@ export class DataTableComponent implements AfterViewInit { } } + isExpandable(row: T): boolean { + if (!this.detailTemplate()) return false; + return this.canExpand()?.(row) ?? true; + } + toggleRow(row: T): void { - if (!this.detailTemplate()) return; + if (!this.isExpandable(row)) return; this.expandedElement.update((current) => (current === row ? null : row)); } } diff --git a/webapp/src/app/shared/ui/publication-table/publication-table.component.html b/webapp/src/app/shared/ui/publication-table/publication-table.component.html index 393ba1d9..c9014f46 100644 --- a/webapp/src/app/shared/ui/publication-table/publication-table.component.html +++ b/webapp/src/app/shared/ui/publication-table/publication-table.component.html @@ -5,22 +5,6 @@ queryParamSortKey="sort_by" queryParamSortDirectionKey="sort_direction" /> - - @let expanded = isRowExpanded(work); - - - {{ work.title }} diff --git a/webapp/src/app/shared/ui/publication-table/publication-table.component.scss b/webapp/src/app/shared/ui/publication-table/publication-table.component.scss index 7a1d7b66..6df1e4e9 100644 --- a/webapp/src/app/shared/ui/publication-table/publication-table.component.scss +++ b/webapp/src/app/shared/ui/publication-table/publication-table.component.scss @@ -562,12 +562,3 @@ mat-panel-description { align-items: flex-start; max-width: 350px; } - -.expand-icon { - color: var(--sg-text-muted); - transition: transform 150ms ease-in-out; - - &.is-expanded { - transform: rotate(180deg); - } -} diff --git a/webapp/src/app/shared/ui/publication-table/publication-table.component.ts b/webapp/src/app/shared/ui/publication-table/publication-table.component.ts index e5d6532a..98300190 100644 --- a/webapp/src/app/shared/ui/publication-table/publication-table.component.ts +++ b/webapp/src/app/shared/ui/publication-table/publication-table.component.ts @@ -87,7 +87,6 @@ export class PublicationTableComponent { elements = computed(() => this.page().elements ?? []); - expandTmp = viewChild.required>('expandTmp'); titleTmp = viewChild.required>('titleTmp'); typeTmp = viewChild.required>('typeTmp'); yearTmp = viewChild.required>('yearTmp'); @@ -205,24 +204,9 @@ export class PublicationTableComponent { }, ); - if (this.detailTemplate()) { - baseColumns.unshift({ - id: 'expand', - header: '', - type: 'display', - cellTemplate: this.expandTmp(), - }); - } - return baseColumns; }); - private readonly dataTable = viewChild(DataTableComponent); - - isRowExpanded(work: Work): boolean { - return this.dataTable()?.expandedElement() === work; - } - workIds = computed( () => this.elements() diff --git a/webapp/src/app/shared/ui/snippet-text/snippet-text.component.html b/webapp/src/app/shared/ui/snippet-text/snippet-text.component.html new file mode 100644 index 00000000..6f39bd6d --- /dev/null +++ b/webapp/src/app/shared/ui/snippet-text/snippet-text.component.html @@ -0,0 +1,16 @@ +

+ @for (segment of rendered(); track $index) { + @if (segment.isSearchMatch) { + {{ segment.text }} + } @else { + {{ segment.text }} + } + } +

diff --git a/webapp/src/app/shared/ui/snippet-text/snippet-text.component.scss b/webapp/src/app/shared/ui/snippet-text/snippet-text.component.scss new file mode 100644 index 00000000..5eabaa09 --- /dev/null +++ b/webapp/src/app/shared/ui/snippet-text/snippet-text.component.scss @@ -0,0 +1,25 @@ +.snippet { + margin: 0; + font-size: 12.5px; + line-height: 1.6; + color: var(--sg-text-secondary); + overflow-wrap: anywhere; +} + +.snippet-mark { + background: transparent; + color: var(--sg-blue-highlight); + font-weight: 700; + + &.has-topic { + color: var(--t); + text-decoration: underline; + text-decoration-color: var(--t); + text-decoration-thickness: 2px; + text-underline-offset: 2px; + } +} + +:host(.has-tooltips) .snippet-mark { + cursor: help; +} diff --git a/webapp/src/app/shared/ui/snippet-text/snippet-text.component.ts b/webapp/src/app/shared/ui/snippet-text/snippet-text.component.ts new file mode 100644 index 00000000..a9e1e3a3 --- /dev/null +++ b/webapp/src/app/shared/ui/snippet-text/snippet-text.component.ts @@ -0,0 +1,43 @@ +import { Component, computed, input } from '@angular/core'; +import { MatTooltipModule } from '@angular/material/tooltip'; + +import { + keywordTooltip, + SnippetSegment, +} from '../../../resolvers/utils/keyword-highlight'; + +@Component({ + host: { + '[class.has-tooltips]': 'showTooltips()', + }, + selector: 'app-snippet-text', + imports: [MatTooltipModule], + templateUrl: './snippet-text.component.html', + styleUrl: './snippet-text.component.scss', +}) +export class SnippetTextComponent { + segments = input.required(); + + showTooltips = input(false); + + protected readonly rendered = computed(() => + this.segments().map((segment) => ({ + text: segment.text, + isSearchMatch: segment.isSearchMatch, + color: segment.match?.color ?? null, + tooltip: this.tooltipFor(segment), + })), + ); + + private tooltipFor(segment: SnippetSegment): string { + if (!this.showTooltips() || !segment.isSearchMatch) { + return ''; + } + + if (segment.match) { + return keywordTooltip(segment.match.keyword, segment.match.topicNames); + } + + return segment.groupHasMatch ? '' : keywordTooltip(segment.text, []); + } +}