refactor(news-keyword-search): adapt projects' highlighting approach and drop tooltips
This commit is contained in:
+5
-17
@@ -72,24 +72,12 @@
|
||||
|
||||
<ng-template #snippetTmp let-article>
|
||||
<div class="snippet-detail">
|
||||
@if (snippetSegments().get(article.id); as segments) {
|
||||
@if (snippetHighlights().get(article.id); as snippet) {
|
||||
<span class="snippet-label">Fundstelle im Artikel</span>
|
||||
<p class="snippet">
|
||||
@for (segment of segments; 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>
|
||||
<app-highlighted-text
|
||||
class="snippet"
|
||||
[content]="snippet.content"
|
||||
[highlights]="snippet.terms" />
|
||||
} @else {
|
||||
<span class="empty-text">
|
||||
Für diesen Artikel liegt keine Textstelle zu den gesuchten Keywords
|
||||
|
||||
-15
@@ -153,21 +153,6 @@
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.snippet-mark {
|
||||
background: transparent;
|
||||
color: var(--sg-blue-highlight);
|
||||
font-weight: 700;
|
||||
cursor: help;
|
||||
|
||||
&.has-topic {
|
||||
color: var(--t);
|
||||
text-decoration: underline;
|
||||
text-decoration-color: var(--t);
|
||||
text-decoration-thickness: 2px;
|
||||
text-underline-offset: 2px;
|
||||
}
|
||||
}
|
||||
|
||||
.date-text {
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
+64
-52
@@ -42,18 +42,15 @@ import {
|
||||
SORT_ICON,
|
||||
} from '../../../../shared/ui/icons';
|
||||
import {
|
||||
keywordTooltip,
|
||||
splitByKeywords,
|
||||
topicKeywordColors,
|
||||
} from '../../../../resolvers/utils/keyword-highlight';
|
||||
HighlightedTextComponent,
|
||||
HighlightTerm,
|
||||
} from '../../../../shared/ui/highlighted-text/highlighted-text.component';
|
||||
|
||||
type NewsResult = NewsPage['results'][number];
|
||||
|
||||
interface SnippetSegment {
|
||||
text: string;
|
||||
color: string | null;
|
||||
tooltip: string | null;
|
||||
isSearchMatch: boolean;
|
||||
interface SnippetHighlight {
|
||||
content: string;
|
||||
terms: HighlightTerm[];
|
||||
}
|
||||
|
||||
@Component({
|
||||
@@ -64,6 +61,7 @@ interface SnippetSegment {
|
||||
MatIconModule,
|
||||
MatTooltipModule,
|
||||
DataTableComponent,
|
||||
HighlightedTextComponent,
|
||||
AppTopicChipComponent,
|
||||
ChipOverflowToggleComponent,
|
||||
FilterCardComponent,
|
||||
@@ -264,70 +262,84 @@ export class KeywordsToNewsResultsComponent {
|
||||
return this.dataTable()?.expandedElement() === article;
|
||||
}
|
||||
|
||||
readonly snippetSegments = computed(() => {
|
||||
const byArticleId = new Map<string, SnippetSegment[]>();
|
||||
readonly snippetHighlights = computed(() => {
|
||||
const byArticleId = new Map<string, SnippetHighlight>();
|
||||
|
||||
for (const article of this.elements()) {
|
||||
const segments = this.buildSnippetSegments(article);
|
||||
if (segments.length) {
|
||||
byArticleId.set(article.id, segments);
|
||||
const snippet = this.buildSnippetHighlight(article);
|
||||
if (snippet) {
|
||||
byArticleId.set(article.id, snippet);
|
||||
}
|
||||
}
|
||||
|
||||
return byArticleId;
|
||||
});
|
||||
|
||||
private buildSnippetSegments(article: NewsResult): SnippetSegment[] {
|
||||
private buildSnippetHighlight(article: NewsResult): SnippetHighlight | null {
|
||||
const snippet = article.snippet;
|
||||
if (!snippet) {
|
||||
return [];
|
||||
return null;
|
||||
}
|
||||
|
||||
const keywordColors = topicKeywordColors(article.topics);
|
||||
const searchMatch = /<em>([\s\S]*?)<\/em>/g;
|
||||
const topicByKeyword = new Map<string, MatchedTopic>();
|
||||
for (const topic of article.topics ?? []) {
|
||||
const keyword = topic.source_keyword?.toLowerCase();
|
||||
if (keyword && !topicByKeyword.has(keyword)) {
|
||||
topicByKeyword.set(keyword, topic);
|
||||
}
|
||||
}
|
||||
|
||||
const segments: SnippetSegment[] = [];
|
||||
let lastIndex = 0;
|
||||
const terms: HighlightTerm[] = [];
|
||||
const seen = new Set<string>();
|
||||
const searchMatch = /<em>([\s\S]*?)<\/em>/g;
|
||||
let match: RegExpExecArray | null;
|
||||
|
||||
const pushPlain = (raw: string) => {
|
||||
const text = this.decodeHtml(raw);
|
||||
if (text) {
|
||||
segments.push({
|
||||
text,
|
||||
color: null,
|
||||
tooltip: null,
|
||||
isSearchMatch: false,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
while ((match = searchMatch.exec(snippet)) !== null) {
|
||||
pushPlain(snippet.slice(lastIndex, match.index));
|
||||
|
||||
const term = this.decodeHtml(match[1]);
|
||||
const parts = splitByKeywords(term, keywordColors);
|
||||
const hasTopic = parts.some((part) => part.match);
|
||||
|
||||
for (const part of parts) {
|
||||
segments.push({
|
||||
text: part.text,
|
||||
color: part.match?.color ?? null,
|
||||
tooltip: part.match
|
||||
? keywordTooltip(part.match.keyword, part.match.topicNames)
|
||||
: hasTopic
|
||||
? null
|
||||
: keywordTooltip(term, []),
|
||||
isSearchMatch: true,
|
||||
});
|
||||
const matched = this.decodeHtml(match[1]).trim();
|
||||
const topic = this.topicForTerm(matched, topicByKeyword);
|
||||
const term = topic?.source_keyword ?? matched;
|
||||
const key = term.toLowerCase();
|
||||
if (!term || seen.has(key)) {
|
||||
continue;
|
||||
}
|
||||
seen.add(key);
|
||||
|
||||
lastIndex = searchMatch.lastIndex;
|
||||
terms.push({
|
||||
term,
|
||||
topic: topic
|
||||
? {
|
||||
id: topic.id,
|
||||
name: topic.display_name,
|
||||
field_name: topic.field_name,
|
||||
domain_name: topic.domain_name,
|
||||
}
|
||||
: null,
|
||||
});
|
||||
}
|
||||
|
||||
pushPlain(snippet.slice(lastIndex));
|
||||
return {
|
||||
content: this.decodeHtml(snippet.replace(/<\/?em>/g, '')),
|
||||
terms,
|
||||
};
|
||||
}
|
||||
|
||||
return segments;
|
||||
private topicForTerm(
|
||||
term: string,
|
||||
topicByKeyword: ReadonlyMap<string, MatchedTopic>,
|
||||
): MatchedTopic | null {
|
||||
const lower = term.toLowerCase();
|
||||
const exact = topicByKeyword.get(lower);
|
||||
if (exact) {
|
||||
return exact;
|
||||
}
|
||||
|
||||
for (const [keyword, topic] of topicByKeyword) {
|
||||
if (lower.startsWith(keyword)) {
|
||||
return topic;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private decodeHtml(value: string): string {
|
||||
|
||||
@@ -18,10 +18,6 @@ import {
|
||||
} from '../../shared/ui/highlighted-text/highlighted-text.component';
|
||||
import { WorksService } from '../../api';
|
||||
import { topicHierarchyColor } from '../../resolvers/utils/topic-colors';
|
||||
import {
|
||||
highlightKeywords,
|
||||
topicKeywordColors,
|
||||
} from '../../resolvers/utils/keyword-highlight';
|
||||
import { LoadingComponent } from '../../shared/ui/loading/loading.component';
|
||||
import {
|
||||
ARROW_BACK_ICON,
|
||||
|
||||
Reference in New Issue
Block a user