Skip to content

Commit

Permalink
fix(content): do not run change detection when loading mermaid (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
arturovt authored Jul 28, 2023
1 parent ce348f6 commit 0436b6f
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions packages/content/src/lib/markdown.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import {
ViewEncapsulation,
inject,
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { DomSanitizer, SafeHtml } from '@angular/platform-browser';
import { ActivatedRoute, Data } from '@angular/router';
import { Observable, of } from 'rxjs';
import { Observable, from, of } from 'rxjs';
import { catchError, map, mergeMap } from 'rxjs/operators';

import { AnchorNavigationDirective } from './anchor-navigation.directive';
Expand Down Expand Up @@ -54,15 +55,7 @@ export default class AnalogMarkdownComponent
}
}

async loadMermaid(mermaidImport: Promise<typeof import('mermaid')>) {
this.mermaid = await mermaidImport;
this.mermaid.default.initialize({ startOnLoad: false });
// Explicitly running mermaid as ngAfterViewChecked
// has probably already been called
this.zone.runOutsideAngular(() => this.mermaid?.default.run());
}

ngOnInit() {
ngOnInit(): void {
this.updateContent();
}

Expand All @@ -87,4 +80,20 @@ export default class AnalogMarkdownComponent
this.contentRenderer.enhance();
this.zone.runOutsideAngular(() => this.mermaid?.default.run());
}

private loadMermaid(mermaidImport: Promise<typeof import('mermaid')>) {
this.zone.runOutsideAngular(() =>
// Wrap into an observable to avoid redundant initialization once
// the markdown component is destroyed before the promise is resolved.
from(mermaidImport)
.pipe(takeUntilDestroyed())
.subscribe((mermaid) => {
this.mermaid = mermaid;
this.mermaid.default.initialize({ startOnLoad: false });
// Explicitly running mermaid as ngAfterViewChecked
// has probably already been called
this.mermaid?.default.run();
})
);
}
}

0 comments on commit 0436b6f

Please sign in to comment.