Skip to content

Commit

Permalink
feat(html-loader): Update logic for loading HTML content
Browse files Browse the repository at this point in the history
- 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
SakuraIsayeki committed Feb 2, 2024
1 parent e70955b commit fe39505
Showing 1 changed file with 13 additions and 12 deletions.
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);
});
}

0 comments on commit fe39505

Please sign in to comment.