This Angular component, ResultsComponent
, displays search results, fetching them from a service and using Prism.js to highlight code snippets within each result.
npm run import -- "Display code results"
import {SearchService} from '../../../imports/search.service';
import {ChangeDetectorRef, Component, OnDestroy, OnInit} from '@angular/core';
import {Subscription} from 'rxjs/Subscription';
import * as Prism from 'prismjs';
@Component({
selector: 'bc-results',
template: `
<pre [class]="'language-'+(r.lang||'javascript')" *ngFor="let r of results" [innerHTML]="highlight(r.code)">
</pre>
`,
styleUrls: ['./results.component.scss']
})
export class ResultsComponent implements OnInit, OnDestroy {
query = '';
results: Array<any> = [];
private resultsSub: Subscription;
constructor(public service: SearchService, public ref: ChangeDetectorRef) {}
ngOnInit(): void {
this.resultsSub = this.service.results(this.query).subscribe(r => {
this.results = [(r as Array < any >)[0]];
this.ref.detectChanges();
});
}
ngOnDestroy(): void {
this.resultsSub.unsubscribe();
}
highlight(code: string): string {
return Prism.highlight(code, Prism.languages.javascript);
}
}
import { SearchService } from '../../../imports/search.service';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Subscription } from 'rxjs';
import * as Prism from 'prismjs';
@Component({
selector: 'bc-results',
template: `
`,
styleUrls: ['./results.component.scss']
})
export class ResultsComponent implements OnInit, OnDestroy {
query = '';
results: any[] = [];
constructor(private service: SearchService, private ref: ChangeDetectorRef) { }
ngOnInit(): void {
// Subscribe to search results observable
const resultsSub = this.service.results(this.query).subscribe((data: any[]) => {
// Handle successful data response
if (data && data.length > 0) {
this.results = data;
this.ref.detectChanges();
} else {
// Handle empty or no data response
console.error('No results found for the given query.');
}
}, (error: any) => {
// Handle observable error
console.error('Error fetching search results:', error);
});
// Store subscription for later use
this.resultsSub = resultsSub;
}
ngOnDestroy(): void {
// Unsubscribe from observable to prevent memory leaks
this.resultsSub.unsubscribe();
}
/**
* Highlights the given code snippet using PrismJS
* @param code Code snippet to highlight
*/
highlight(code: string): string {
return Prism.highlight(code, Prism.languages.javascript);
}
}
This code defines an Angular component called ResultsComponent
that displays search results, highlighting code snippets.
Here's a breakdown:
Imports:
SearchService
, Angular core modules (Component
, OnInit
, OnDestroy
, ChangeDetectorRef
), Subscription
from RxJS, and Prism
for code highlighting.ResultsComponent
:
results
and displays each result as a preformatted code block with syntax highlighting.highlight()
method uses Prism
to highlight the code.Constructor:
SearchService
and ChangeDetectorRef
.ngOnInit()
:
results
observable from SearchService
and updates the results
array when new data arrives.ChangeDetectorRef.detectChanges()
to trigger change detection.ngOnDestroy()
:
Let me know if you have any other code snippets you'd like me to explain!