-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(html-loader): Update logic for loading HTML content
- Updated the template syntax to use single quotes instead of double quotes. - Replaced the deprecated `mergeMap` and `switchMap` operators with `computed` and `firstValueFrom`. - Removed unused imports and variables. - Added error handling for cases where the path is not provided.
- Loading branch information
1 parent
e70955b
commit fe39505
Showing
1 changed file
with
13 additions
and
12 deletions.
There are no files selected for viewing
25 changes: 13 additions & 12 deletions
25
wowskarma.app/src/app/shared/components/html-loader/html-loader.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,31 @@ | ||
import { HttpClient } from "@angular/common/http"; | ||
import { ChangeDetectionStrategy, Component, inject, Input } from "@angular/core"; | ||
import { ChangeDetectionStrategy, Component, computed, inject, Input, signal } from "@angular/core"; | ||
import { DomSanitizer } from "@angular/platform-browser"; | ||
import { ActivatedRoute } from "@angular/router"; | ||
import { mergeMap, Observable, merge, switchMap, shareReplay } from "rxjs"; | ||
import { map } from "rxjs/operators"; | ||
import { filterNotNull, InputObservable } from "../../rxjs-operators"; | ||
import { firstValueFrom } from "rxjs"; | ||
|
||
@Component({ | ||
selector: "html-loader", | ||
template: "<div [innerHtml]=\"content$ | async\"></div>", | ||
template: '<div [innerHtml]="content() | async"></div>', | ||
changeDetection: ChangeDetectionStrategy.OnPush, | ||
}) | ||
export class HtmlLoaderComponent { | ||
@Input() | ||
@InputObservable() | ||
path!: string; | ||
path$!: Observable<string> | ||
|
||
private http: HttpClient = inject(HttpClient); | ||
private route: ActivatedRoute = inject(ActivatedRoute); | ||
private sanitizer: DomSanitizer = inject(DomSanitizer); | ||
|
||
// Get the HTML content from the server, at the path specified by the path$ input. | ||
content$ = merge(this.path$.pipe(filterNotNull()), this.route.data.pipe(map(d => d["path"]), filterNotNull())).pipe( | ||
switchMap(path => this.http.get(path, { responseType: "text" })), | ||
map(html => this.sanitizer.bypassSecurityTrustHtml(html)), | ||
shareReplay(1), | ||
); | ||
content = computed(async () => { | ||
const path = this.path || this.route.snapshot.data["path"]; | ||
|
||
if (!path) { | ||
return null; | ||
} | ||
|
||
let html = await firstValueFrom(this.http.get(path, {responseType: "text"})); | ||
return this.sanitizer.bypassSecurityTrustHtml(html); | ||
}); | ||
} |