aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/shared-forms/markdown-textarea.component.ts
blob: 0899918848b59a20a735c8a2d042ecd9fd30448b (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import truncate from 'lodash-es/truncate'
import { Subject } from 'rxjs'
import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
import { ViewportScroller } from '@angular/common'
import { Component, ElementRef, forwardRef, Input, OnInit, ViewChild } from '@angular/core'
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
import { SafeHtml } from '@angular/platform-browser'
import { MarkdownService, ScreenService } from '@app/core'
import { Video } from '@shared/models'

@Component({
  selector: 'my-markdown-textarea',
  templateUrl: './markdown-textarea.component.html',
  styleUrls: [ './markdown-textarea.component.scss' ],
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => MarkdownTextareaComponent),
      multi: true
    }
  ]
})

export class MarkdownTextareaComponent implements ControlValueAccessor, OnInit {
  @Input() content = ''

  @Input() formError: string

  @Input() truncate: number

  @Input() markdownType: 'text' | 'enhanced' = 'text'
  @Input() customMarkdownRenderer?: (text: string) => Promise<string | HTMLElement>

  @Input() markdownVideo: Video

  @Input() name = 'description'

  @ViewChild('textarea') textareaElement: ElementRef
  @ViewChild('previewElement') previewElement: ElementRef

  truncatedPreviewHTML: SafeHtml | string = ''
  previewHTML: SafeHtml | string = ''

  isMaximized = false
  disabled = false

  maximizeInText = $localize`Maximize editor`
  maximizeOutText = $localize`Exit maximized editor`

  private contentChanged = new Subject<string>()
  private scrollPosition: [number, number]

  constructor (
    private viewportScroller: ViewportScroller,
    private screenService: ScreenService,
    private markdownService: MarkdownService
  ) { }

  ngOnInit () {
    this.contentChanged
        .pipe(
          debounceTime(150),
          distinctUntilChanged()
        )
        .subscribe(() => this.updatePreviews())

    this.contentChanged.next(this.content)
  }

  propagateChange = (_: any) => { /* empty */ }

  writeValue (description: string) {
    this.content = description

    this.contentChanged.next(this.content)
  }

  registerOnChange (fn: (_: any) => void) {
    this.propagateChange = fn
  }

  registerOnTouched () {
    // Unused
  }

  onModelChange () {
    this.propagateChange(this.content)

    this.contentChanged.next(this.content)
  }

  onMaximizeClick () {
    if (this.disabled) return

    this.isMaximized = !this.isMaximized

    // Make sure textarea have the focus
    // Except on touchscreens devices, the virtual keyboard may move up and hide the textarea in maximized mode
    if (!this.screenService.isInTouchScreen()) {
      this.textareaElement.nativeElement.focus()
    }

    // Make sure the window has no scrollbars
    if (!this.isMaximized) {
      this.unlockBodyScroll()
    } else {
      this.lockBodyScroll()
    }
  }

  setDisabledState (isDisabled: boolean) {
    this.disabled = isDisabled
  }

  private lockBodyScroll () {
    this.scrollPosition = this.viewportScroller.getScrollPosition()
    document.getElementById('content').classList.add('lock-scroll')
  }

  private unlockBodyScroll () {
    document.getElementById('content').classList.remove('lock-scroll')
    this.viewportScroller.scrollToPosition(this.scrollPosition)
  }

  private async updatePreviews () {
    if (this.content === null || this.content === undefined) return

    this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate }))
    this.previewHTML = await this.markdownRender(this.content)
  }

  private async markdownRender (text: string) {
    let html: string

    if (this.customMarkdownRenderer) {
      const result = await this.customMarkdownRenderer(text)

      if (result instanceof HTMLElement) {
        const wrapperElement = this.previewElement.nativeElement as HTMLElement
        wrapperElement.innerHTML = ''
        wrapperElement.appendChild(result)
        return
      }

      html = result
    } else if (this.markdownType === 'text') {
      html = await this.markdownService.textMarkdownToHTML(text)
    } else {
      html = await this.markdownService.enhancedMarkdownToHTML(text)
    }

    if (this.markdownVideo) {
      html = this.markdownService.processVideoTimestamps(this.markdownVideo.shortUUID, html)
    }

    return html
  }
}