2021-10-13 20:35:10 +03:00
|
|
|
///
|
2023-01-31 10:43:56 +02:00
|
|
|
/// Copyright © 2016-2023 The Thingsboard Authors
|
2021-10-13 20:35:10 +03:00
|
|
|
///
|
|
|
|
|
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
/// you may not use this file except in compliance with the License.
|
|
|
|
|
/// You may obtain a copy of the License at
|
|
|
|
|
///
|
|
|
|
|
/// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
///
|
|
|
|
|
/// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
/// See the License for the specific language governing permissions and
|
|
|
|
|
/// limitations under the License.
|
|
|
|
|
///
|
|
|
|
|
|
|
|
|
|
import {
|
2021-10-14 16:02:25 +03:00
|
|
|
ChangeDetectorRef,
|
2021-10-13 20:35:10 +03:00
|
|
|
Component,
|
|
|
|
|
ComponentFactory,
|
2021-10-18 13:10:11 +03:00
|
|
|
ComponentRef, ElementRef,
|
2021-10-13 20:35:10 +03:00
|
|
|
EventEmitter,
|
|
|
|
|
Inject,
|
|
|
|
|
Injector,
|
|
|
|
|
Input, OnChanges,
|
|
|
|
|
Output,
|
2021-10-14 16:02:25 +03:00
|
|
|
SimpleChanges,
|
|
|
|
|
Type, ViewChild,
|
2021-10-13 20:35:10 +03:00
|
|
|
ViewContainerRef
|
|
|
|
|
} from '@angular/core';
|
|
|
|
|
import { HelpService } from '@core/services/help.service';
|
2021-10-14 16:02:25 +03:00
|
|
|
import { MarkdownService, PrismPlugin } from 'ngx-markdown';
|
2021-10-13 20:35:10 +03:00
|
|
|
import { DynamicComponentFactoryService } from '@core/services/dynamic-component-factory.service';
|
|
|
|
|
import { coerceBooleanProperty } from '@angular/cdk/coercion';
|
|
|
|
|
import { SHARED_MODULE_TOKEN } from '@shared/components/tokens';
|
2021-10-18 13:10:11 +03:00
|
|
|
import { isDefinedAndNotNull } from '@core/utils';
|
|
|
|
|
import { Observable, of, ReplaySubject } from 'rxjs';
|
2021-10-13 20:35:10 +03:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-markdown',
|
2021-10-18 13:10:11 +03:00
|
|
|
templateUrl: './markdown.component.html'
|
2021-10-13 20:35:10 +03:00
|
|
|
})
|
2021-10-14 16:02:25 +03:00
|
|
|
export class TbMarkdownComponent implements OnChanges {
|
2021-10-13 20:35:10 +03:00
|
|
|
|
|
|
|
|
@ViewChild('markdownContainer', {read: ViewContainerRef, static: true}) markdownContainer: ViewContainerRef;
|
2021-10-18 13:10:11 +03:00
|
|
|
@ViewChild('fallbackElement', {static: true}) fallbackElement: ElementRef<HTMLElement>;
|
2021-10-13 20:35:10 +03:00
|
|
|
|
|
|
|
|
@Input() data: string | undefined;
|
|
|
|
|
|
2021-12-22 17:15:10 +02:00
|
|
|
@Input() context: any;
|
|
|
|
|
|
|
|
|
|
@Input() additionalCompileModules: Type<any>[];
|
|
|
|
|
|
2021-10-13 20:35:10 +03:00
|
|
|
@Input() markdownClass: string | undefined;
|
|
|
|
|
|
|
|
|
|
@Input() style: { [klass: string]: any } = {};
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
get lineNumbers(): boolean { return this.lineNumbersValue; }
|
|
|
|
|
set lineNumbers(value: boolean) { this.lineNumbersValue = coerceBooleanProperty(value); }
|
|
|
|
|
|
2021-10-18 13:10:11 +03:00
|
|
|
@Input()
|
|
|
|
|
get fallbackToPlainMarkdown(): boolean { return this.fallbackToPlainMarkdownValue; }
|
|
|
|
|
set fallbackToPlainMarkdown(value: boolean) { this.fallbackToPlainMarkdownValue = coerceBooleanProperty(value); }
|
|
|
|
|
|
2021-10-13 20:35:10 +03:00
|
|
|
@Output() ready = new EventEmitter<void>();
|
|
|
|
|
|
|
|
|
|
private lineNumbersValue = false;
|
2021-10-18 13:10:11 +03:00
|
|
|
private fallbackToPlainMarkdownValue = false;
|
2021-10-13 20:35:10 +03:00
|
|
|
|
|
|
|
|
isMarkdownReady = false;
|
|
|
|
|
|
2021-10-14 16:02:25 +03:00
|
|
|
error = null;
|
|
|
|
|
|
2021-10-13 20:35:10 +03:00
|
|
|
private tbMarkdownInstanceComponentRef: ComponentRef<any>;
|
|
|
|
|
private tbMarkdownInstanceComponentFactory: ComponentFactory<any>;
|
|
|
|
|
|
|
|
|
|
constructor(private help: HelpService,
|
2021-10-14 16:02:25 +03:00
|
|
|
private cd: ChangeDetectorRef,
|
|
|
|
|
public markdownService: MarkdownService,
|
2021-10-13 20:35:10 +03:00
|
|
|
@Inject(SHARED_MODULE_TOKEN) private sharedModule: Type<any>,
|
|
|
|
|
private dynamicComponentFactoryService: DynamicComponentFactoryService) {}
|
|
|
|
|
|
|
|
|
|
ngOnChanges(changes: SimpleChanges): void {
|
2021-10-18 13:10:11 +03:00
|
|
|
if (isDefinedAndNotNull(this.data)) {
|
2021-10-14 16:02:25 +03:00
|
|
|
this.render(this.data);
|
2021-10-13 20:35:10 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-14 16:02:25 +03:00
|
|
|
private render(markdown: string) {
|
2023-02-06 13:09:43 +02:00
|
|
|
const compiled = this.markdownService.parse(markdown, { decodeHtml: false });
|
2021-10-18 13:10:11 +03:00
|
|
|
let template = this.sanitizeCurlyBraces(compiled);
|
2021-10-13 20:35:10 +03:00
|
|
|
let markdownClass = 'tb-markdown-view';
|
|
|
|
|
if (this.markdownClass) {
|
|
|
|
|
markdownClass += ` ${this.markdownClass}`;
|
|
|
|
|
}
|
|
|
|
|
template = `<div [ngStyle]="style" class="${markdownClass}">${template}</div>`;
|
|
|
|
|
this.markdownContainer.clear();
|
|
|
|
|
const parent = this;
|
2021-10-18 13:10:11 +03:00
|
|
|
let readyObservable: Observable<void>;
|
2021-12-22 17:15:10 +02:00
|
|
|
let compileModules = [this.sharedModule];
|
|
|
|
|
if (this.additionalCompileModules) {
|
|
|
|
|
compileModules = compileModules.concat(this.additionalCompileModules);
|
|
|
|
|
}
|
2021-10-13 20:35:10 +03:00
|
|
|
this.dynamicComponentFactoryService.createDynamicComponentFactory(
|
|
|
|
|
class TbMarkdownInstance {
|
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
|
parent.destroyMarkdownInstanceResources();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
template,
|
2021-12-22 17:15:10 +02:00
|
|
|
compileModules,
|
2021-10-13 20:35:10 +03:00
|
|
|
true
|
|
|
|
|
).subscribe((factory) => {
|
|
|
|
|
this.tbMarkdownInstanceComponentFactory = factory;
|
|
|
|
|
const injector: Injector = Injector.create({providers: [], parent: this.markdownContainer.injector});
|
|
|
|
|
try {
|
|
|
|
|
this.tbMarkdownInstanceComponentRef =
|
|
|
|
|
this.markdownContainer.createComponent(this.tbMarkdownInstanceComponentFactory, 0, injector);
|
2021-12-22 17:15:10 +02:00
|
|
|
if (this.context) {
|
|
|
|
|
for (const propName of Object.keys(this.context)) {
|
|
|
|
|
this.tbMarkdownInstanceComponentRef.instance[propName] = this.context[propName];
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-13 20:35:10 +03:00
|
|
|
this.tbMarkdownInstanceComponentRef.instance.style = this.style;
|
2021-10-14 16:02:25 +03:00
|
|
|
this.handlePlugins(this.tbMarkdownInstanceComponentRef.location.nativeElement);
|
|
|
|
|
this.markdownService.highlight(this.tbMarkdownInstanceComponentRef.location.nativeElement);
|
2021-10-18 13:10:11 +03:00
|
|
|
readyObservable = this.handleImages(this.tbMarkdownInstanceComponentRef.location.nativeElement);
|
2022-07-12 18:47:40 +03:00
|
|
|
this.cd.detectChanges();
|
2021-10-14 16:02:25 +03:00
|
|
|
this.error = null;
|
|
|
|
|
} catch (error) {
|
2021-10-18 13:10:11 +03:00
|
|
|
readyObservable = this.handleError(compiled, error);
|
2021-10-13 20:35:10 +03:00
|
|
|
}
|
2021-10-18 13:10:11 +03:00
|
|
|
readyObservable.subscribe(() => {
|
|
|
|
|
this.ready.emit();
|
|
|
|
|
});
|
2021-10-14 16:02:25 +03:00
|
|
|
},
|
|
|
|
|
(error) => {
|
2021-10-18 13:10:11 +03:00
|
|
|
readyObservable = this.handleError(compiled, error);
|
2021-10-14 16:02:25 +03:00
|
|
|
this.cd.detectChanges();
|
2021-10-18 13:10:11 +03:00
|
|
|
readyObservable.subscribe(() => {
|
|
|
|
|
this.ready.emit();
|
|
|
|
|
});
|
2021-10-13 20:35:10 +03:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-18 13:10:11 +03:00
|
|
|
private handleError(template: string, error): Observable<void> {
|
|
|
|
|
this.error = (error ? error + '' : 'Failed to render markdown!').replace(/\n/g, '<br>');
|
2022-07-12 18:47:40 +03:00
|
|
|
this.markdownContainer.clear();
|
2021-10-18 13:10:11 +03:00
|
|
|
if (this.fallbackToPlainMarkdownValue) {
|
|
|
|
|
const element = this.fallbackElement.nativeElement;
|
|
|
|
|
element.innerHTML = template;
|
|
|
|
|
this.handlePlugins(element);
|
|
|
|
|
this.markdownService.highlight(element);
|
|
|
|
|
return this.handleImages(element);
|
|
|
|
|
} else {
|
|
|
|
|
return of(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-14 16:02:25 +03:00
|
|
|
private handlePlugins(element: HTMLElement): void {
|
|
|
|
|
if (this.lineNumbers) {
|
|
|
|
|
this.setPluginClass(element, PrismPlugin.LineNumbers);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private setPluginClass(element: HTMLElement, plugin: string | string[]): void {
|
|
|
|
|
const preElements = element.querySelectorAll('pre');
|
|
|
|
|
for (let i = 0; i < preElements.length; i++) {
|
|
|
|
|
const classes = plugin instanceof Array ? plugin : [plugin];
|
|
|
|
|
preElements.item(i).classList.add(...classes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-18 13:10:11 +03:00
|
|
|
private handleImages(element: HTMLElement): Observable<void> {
|
|
|
|
|
const imgs = $('img', element);
|
|
|
|
|
if (imgs.length) {
|
|
|
|
|
let totalImages = imgs.length;
|
|
|
|
|
const imagesLoadedSubject = new ReplaySubject<void>();
|
|
|
|
|
imgs.each((index, img) => {
|
|
|
|
|
$(img).one('load error', () => {
|
|
|
|
|
totalImages--;
|
|
|
|
|
if (totalImages === 0) {
|
|
|
|
|
imagesLoadedSubject.next();
|
|
|
|
|
imagesLoadedSubject.complete();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
return imagesLoadedSubject.asObservable();
|
|
|
|
|
} else {
|
|
|
|
|
return of(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-13 20:35:10 +03:00
|
|
|
private sanitizeCurlyBraces(template: string): string {
|
|
|
|
|
return template.replace(/{/g, '{').replace(/}/g, '}');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private destroyMarkdownInstanceResources() {
|
|
|
|
|
if (this.tbMarkdownInstanceComponentFactory) {
|
|
|
|
|
this.dynamicComponentFactoryService.destroyDynamicComponentFactory(this.tbMarkdownInstanceComponentFactory);
|
|
|
|
|
this.tbMarkdownInstanceComponentFactory = null;
|
|
|
|
|
}
|
|
|
|
|
this.tbMarkdownInstanceComponentRef = null;
|
|
|
|
|
}
|
|
|
|
|
}
|