1 import truncate from 'lodash-es/truncate'
2 import { Subject } from 'rxjs'
3 import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
4 import { ViewportScroller } from '@angular/common'
5 import { Component, ElementRef, forwardRef, Input, OnInit, ViewChild } from '@angular/core'
6 import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
7 import { SafeHtml } from '@angular/platform-browser'
8 import { MarkdownService, ScreenService } from '@app/core'
9 import { Video } from '@shared/models'
12 selector: 'my-markdown-textarea',
13 templateUrl: './markdown-textarea.component.html',
14 styleUrls: [ './markdown-textarea.component.scss' ],
17 provide: NG_VALUE_ACCESSOR,
18 useExisting: forwardRef(() => MarkdownTextareaComponent),
24 export class MarkdownTextareaComponent implements ControlValueAccessor, OnInit {
27 @Input() formError: string
29 @Input() truncate: number
31 @Input() markdownType: 'text' | 'enhanced' | 'to-unsafe-html' = 'text'
32 @Input() customMarkdownRenderer?: (text: string) => Promise<string | HTMLElement>
34 @Input() debounceTime = 150
36 @Input() markdownVideo: Video
38 @Input() name = 'description'
40 @ViewChild('textarea') textareaElement: ElementRef
41 @ViewChild('previewElement') previewElement: ElementRef
43 truncatedPreviewHTML: SafeHtml | string = ''
44 previewHTML: SafeHtml | string = ''
49 maximizeInText = $localize`Maximize editor`
50 maximizeOutText = $localize`Exit maximized editor`
52 private contentChanged = new Subject<string>()
53 private scrollPosition: [number, number]
56 private viewportScroller: ViewportScroller,
57 private screenService: ScreenService,
58 private markdownService: MarkdownService
64 debounceTime(this.debounceTime),
65 distinctUntilChanged()
67 .subscribe(() => this.updatePreviews())
69 this.contentChanged.next(this.content)
72 propagateChange = (_: any) => { /* empty */ }
74 writeValue (description: string) {
75 this.content = description
77 this.contentChanged.next(this.content)
80 registerOnChange (fn: (_: any) => void) {
81 this.propagateChange = fn
84 registerOnTouched () {
89 this.propagateChange(this.content)
91 this.contentChanged.next(this.content)
95 if (this.disabled) return
97 this.isMaximized = !this.isMaximized
99 // Make sure textarea have the focus
100 // Except on touchscreens devices, the virtual keyboard may move up and hide the textarea in maximized mode
101 if (!this.screenService.isInTouchScreen()) {
102 this.textareaElement.nativeElement.focus()
105 // Make sure the window has no scrollbars
106 if (!this.isMaximized) {
107 this.unlockBodyScroll()
109 this.lockBodyScroll()
113 setDisabledState (isDisabled: boolean) {
114 this.disabled = isDisabled
117 private lockBodyScroll () {
118 this.scrollPosition = this.viewportScroller.getScrollPosition()
119 document.getElementById('content').classList.add('lock-scroll')
122 private unlockBodyScroll () {
123 document.getElementById('content').classList.remove('lock-scroll')
124 this.viewportScroller.scrollToPosition(this.scrollPosition)
127 private async updatePreviews () {
128 if (this.content === null || this.content === undefined) return
130 this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate }))
131 this.previewHTML = await this.markdownRender(this.content)
134 private async markdownRender (text: string) {
137 if (this.customMarkdownRenderer) {
138 const result = await this.customMarkdownRenderer(text)
140 if (result instanceof HTMLElement) {
141 const wrapperElement = this.previewElement.nativeElement as HTMLElement
142 wrapperElement.innerHTML = ''
143 wrapperElement.appendChild(result)
148 } else if (this.markdownType === 'text') {
149 html = await this.markdownService.textMarkdownToHTML({ markdown: text })
150 } else if (this.markdownType === 'enhanced') {
151 html = await this.markdownService.enhancedMarkdownToHTML({ markdown: text })
152 } else if (this.markdownType === 'to-unsafe-html') {
153 html = await this.markdownService.markdownToUnsafeHTML({ markdown: text })
156 if (this.markdownVideo) {
157 html = this.markdownService.processVideoTimestamps(this.markdownVideo.shortUUID, html)