aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/shared-custom-markup/custom-markup-container.component.ts
blob: b2ee2d8f28de1c72ff3ab86faa79cce54b14ba13 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { Component, ElementRef, Input, OnChanges, ViewChild } from '@angular/core'
import { CustomMarkupService } from './custom-markup.service'

@Component({
  selector: 'my-custom-markup-container',
  templateUrl: './custom-markup-container.component.html'
})
export class CustomMarkupContainerComponent implements OnChanges {
  @ViewChild('contentWrapper', { static: true }) contentWrapper: ElementRef<HTMLInputElement>

  @Input() content: string | HTMLDivElement

  displayed = false

  constructor (
    private customMarkupService: CustomMarkupService
  ) { }

  async ngOnChanges () {
    await this.rebuild()
  }

  private async rebuild () {
    if (this.content instanceof HTMLDivElement) {
      return this.loadElement(this.content)
    }

    const { rootElement, componentsLoaded } = await this.customMarkupService.buildElement(this.content)
    await componentsLoaded

    return this.loadElement(rootElement)
  }

  private loadElement (el: HTMLDivElement) {
    this.contentWrapper.nativeElement.appendChild(el)

    this.displayed = true
  }
}