refactor(keyword-highlighting): remove duplication after rebase
This commit is contained in:
+2
-21
@@ -39,26 +39,6 @@
|
||||
}
|
||||
</div>
|
||||
|
||||
<ng-template #expandTmp let-article>
|
||||
@if (article.snippet) {
|
||||
@let expanded = isSnippetExpanded(article);
|
||||
<button
|
||||
mat-icon-button
|
||||
class="expand-toggle"
|
||||
[attr.aria-expanded]="expanded"
|
||||
[matTooltip]="
|
||||
expanded ? 'Textstelle ausblenden' : 'Textstelle anzeigen'
|
||||
"
|
||||
[attr.aria-label]="
|
||||
expanded ? 'Textstelle ausblenden' : 'Textstelle anzeigen'
|
||||
">
|
||||
<mat-icon class="expand-icon" [class.is-expanded]="expanded">
|
||||
{{ EXPAND_MORE_ICON }}
|
||||
</mat-icon>
|
||||
</button>
|
||||
}
|
||||
</ng-template>
|
||||
|
||||
<ng-template #titleTmp let-article>
|
||||
<div class="title-cell">
|
||||
<a
|
||||
@@ -205,7 +185,8 @@
|
||||
<app-data-table
|
||||
[columns]="columns()"
|
||||
[data]="elements()"
|
||||
[detailTemplate]="snippetTmp" />
|
||||
[detailTemplate]="snippetTmp"
|
||||
[canExpand]="hasSnippet" />
|
||||
</div>
|
||||
|
||||
<app-query-param-paginator
|
||||
|
||||
-14
@@ -80,11 +80,6 @@
|
||||
overflow: visible;
|
||||
}
|
||||
|
||||
.mat-column-expand {
|
||||
width: 48px;
|
||||
padding-right: 0;
|
||||
}
|
||||
|
||||
.detail-row .mat-mdc-cell {
|
||||
padding: 0;
|
||||
border-bottom: none;
|
||||
@@ -121,15 +116,6 @@
|
||||
}
|
||||
}
|
||||
|
||||
.expand-icon {
|
||||
color: var(--sg-text-muted);
|
||||
transition: transform 150ms ease-in-out;
|
||||
|
||||
&.is-expanded {
|
||||
transform: rotate(180deg);
|
||||
}
|
||||
}
|
||||
|
||||
.date-text {
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
+2
-15
@@ -35,7 +35,6 @@ import { YearRangeSliderComponent } from '../../../../shared/ui/year-range-slide
|
||||
import {
|
||||
CANCEL_ICON,
|
||||
CHECK_CIRCLE_ICON,
|
||||
EXPAND_MORE_ICON,
|
||||
FILTER_OFF_ICON,
|
||||
HELP_ICON,
|
||||
OPEN_IN_NEW_ICON,
|
||||
@@ -79,7 +78,6 @@ export class KeywordsToNewsResultsComponent {
|
||||
readonly CHECK_CIRCLE_ICON = CHECK_CIRCLE_ICON;
|
||||
readonly SORT_ICON = SORT_ICON;
|
||||
readonly FILTER_OFF_ICON = FILTER_OFF_ICON;
|
||||
readonly EXPAND_MORE_ICON = EXPAND_MORE_ICON;
|
||||
readonly CANCEL_ICON = CANCEL_ICON;
|
||||
readonly HELP_ICON = HELP_ICON;
|
||||
readonly OPEN_IN_NEW_ICON = OPEN_IN_NEW_ICON;
|
||||
@@ -166,22 +164,13 @@ export class KeywordsToNewsResultsComponent {
|
||||
return baseTopicIds.length > 0 || this.selectedTopicIds().length > 0;
|
||||
});
|
||||
|
||||
expandTmp = viewChild.required<TemplateRef<any>>('expandTmp');
|
||||
titleTmp = viewChild.required<TemplateRef<any>>('titleTmp');
|
||||
dateTmp = viewChild.required<TemplateRef<any>>('dateTmp');
|
||||
relevanceTmp = viewChild.required<TemplateRef<any>>('relevanceTmp');
|
||||
topicsTmp = viewChild.required<TemplateRef<any>>('topicsTmp');
|
||||
actionsTmp = viewChild.required<TemplateRef<any>>('actionsTmp');
|
||||
|
||||
private readonly dataTable = viewChild(DataTableComponent);
|
||||
|
||||
readonly columns = computed<GenericColumn<NewsResult>[]>(() => [
|
||||
{
|
||||
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<string, SnippetHighlight>();
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
+1
-9
@@ -11,15 +11,7 @@
|
||||
<span class="snippet-label">
|
||||
Fundstelle im {{ snippetSourceLabel(work) }}
|
||||
</span>
|
||||
<p class="snippet">
|
||||
@for (segment of segments; track $index) {
|
||||
@if (segment.isSearchMatch) {
|
||||
<mark class="snippet-mark">{{ segment.text }}</mark>
|
||||
} @else {
|
||||
<span>{{ segment.text }}</span>
|
||||
}
|
||||
}
|
||||
</p>
|
||||
<app-snippet-text [segments]="segments" />
|
||||
} @else if (work.matched_via?.length) {
|
||||
<span class="snippet-label">Kein Treffer im Text</span>
|
||||
<p class="snippet">
|
||||
|
||||
-6
@@ -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;
|
||||
|
||||
+7
-40
@@ -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 = /<em>([\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';
|
||||
}
|
||||
|
||||
@@ -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 = /<em>([\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, '&')
|
||||
|
||||
@@ -8,6 +8,31 @@
|
||||
matSort
|
||||
(matSortChange)="_handleSortChange($event)"
|
||||
multiTemplateDataRows>
|
||||
@if (detailTemplate()) {
|
||||
<ng-container [matColumnDef]="expandColumnId">
|
||||
<th mat-header-cell *matHeaderCellDef class="expand-cell"></th>
|
||||
<td mat-cell *matCellDef="let row" class="expand-cell">
|
||||
@if (isExpandable(row)) {
|
||||
@let expanded = row === expandedElement();
|
||||
<button
|
||||
mat-icon-button
|
||||
type="button"
|
||||
[attr.aria-expanded]="expanded"
|
||||
[matTooltip]="
|
||||
expanded ? 'Details ausblenden' : 'Details anzeigen'
|
||||
"
|
||||
[attr.aria-label]="
|
||||
expanded ? 'Details ausblenden' : 'Details anzeigen'
|
||||
">
|
||||
<mat-icon class="expand-icon" [class.is-expanded]="expanded">
|
||||
expand_more
|
||||
</mat-icon>
|
||||
</button>
|
||||
}
|
||||
</td>
|
||||
</ng-container>
|
||||
}
|
||||
|
||||
@for (col of columns(); track col.id) {
|
||||
<ng-container [matColumnDef]="col.id">
|
||||
@if (col.headerTemplate) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<T> {
|
||||
@@ -43,7 +46,15 @@ export type GenericColumn<T> = AccessorColumn<T> | DisplayColumn<T>;
|
||||
@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<T> implements AfterViewInit {
|
||||
|
||||
detailTemplate = input<TemplateRef<any> | null>(null);
|
||||
expandedElement = signal<T | null>(null);
|
||||
canExpand = input<((row: T) => boolean) | null>(null);
|
||||
|
||||
maxHeight = input<string | null>(null);
|
||||
containerOverflow = computed(() => (this.maxHeight() ? 'auto' : 'visible'));
|
||||
@@ -78,7 +90,12 @@ export class DataTableComponent<T> implements AfterViewInit {
|
||||
|
||||
handleSortChange = output<Sort>();
|
||||
|
||||
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<T>([]);
|
||||
|
||||
sort = viewChild.required(MatSort);
|
||||
@@ -160,8 +177,13 @@ export class DataTableComponent<T> 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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,22 +5,6 @@
|
||||
queryParamSortKey="sort_by"
|
||||
queryParamSortDirectionKey="sort_direction" />
|
||||
|
||||
<ng-template #expandTmp let-work>
|
||||
@let expanded = isRowExpanded(work);
|
||||
<button
|
||||
mat-icon-button
|
||||
class="expand-toggle"
|
||||
[attr.aria-expanded]="expanded"
|
||||
[matTooltip]="expanded ? 'Fundstelle ausblenden' : 'Fundstelle anzeigen'"
|
||||
[attr.aria-label]="
|
||||
expanded ? 'Fundstelle ausblenden' : 'Fundstelle anzeigen'
|
||||
">
|
||||
<mat-icon class="expand-icon" [class.is-expanded]="expanded">
|
||||
expand_more
|
||||
</mat-icon>
|
||||
</button>
|
||||
</ng-template>
|
||||
|
||||
<ng-template #titleTmp let-work>
|
||||
<span>{{ work.title }}</span>
|
||||
</ng-template>
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,7 +87,6 @@ export class PublicationTableComponent {
|
||||
|
||||
elements = computed(() => this.page().elements ?? []);
|
||||
|
||||
expandTmp = viewChild.required<TemplateRef<any>>('expandTmp');
|
||||
titleTmp = viewChild.required<TemplateRef<any>>('titleTmp');
|
||||
typeTmp = viewChild.required<TemplateRef<any>>('typeTmp');
|
||||
yearTmp = viewChild.required<TemplateRef<any>>('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()
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
<p class="snippet">
|
||||
@for (segment of rendered(); track $index) {
|
||||
@if (segment.isSearchMatch) {
|
||||
<mark
|
||||
class="snippet-mark"
|
||||
[class.has-topic]="!!segment.color"
|
||||
[style.--t]="segment.color"
|
||||
[matTooltip]="segment.tooltip"
|
||||
matTooltipPosition="above"
|
||||
>{{ segment.text }}</mark
|
||||
>
|
||||
} @else {
|
||||
<span>{{ segment.text }}</span>
|
||||
}
|
||||
}
|
||||
</p>
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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<SnippetSegment[]>();
|
||||
|
||||
showTooltips = input<boolean>(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, []);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user