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' = 'text'
32 @Input() customMarkdownRenderer?: (text: string) => Promise<string | HTMLElement>
34 @Input() markdownVideo: Video
36 @Input() name = 'description'
38 @ViewChild('textarea') textareaElement: ElementRef
39 @ViewChild('previewElement') previewElement: ElementRef
41 truncatedPreviewHTML: SafeHtml | string = ''
42 previewHTML: SafeHtml | string = ''
47 maximizeInText = $localize`Maximize editor`
48 maximizeOutText = $localize`Exit maximized editor`
50 private contentChanged = new Subject<string>()
51 private scrollPosition: [number, number]
54 private viewportScroller: ViewportScroller,
55 private screenService: ScreenService,
56 private markdownService: MarkdownService
63 distinctUntilChanged()
65 .subscribe(() => this.updatePreviews())
67 this.contentChanged.next(this.content)
70 propagateChange = (_: any) => { /* empty */ }
72 writeValue (description: string) {
73 this.content = description
75 this.contentChanged.next(this.content)
78 registerOnChange (fn: (_: any) => void) {
79 this.propagateChange = fn
82 registerOnTouched () {
87 this.propagateChange(this.content)
89 this.contentChanged.next(this.content)
93 if (this.disabled) return
95 this.isMaximized = !this.isMaximized
97 // Make sure textarea have the focus
98 // Except on touchscreens devices, the virtual keyboard may move up and hide the textarea in maximized mode
99 if (!this.screenService.isInTouchScreen()) {
100 this.textareaElement.nativeElement.focus()
103 // Make sure the window has no scrollbars
104 if (!this.isMaximized) {
105 this.unlockBodyScroll()
107 this.lockBodyScroll()
111 setDisabledState (isDisabled: boolean) {
112 this.disabled = isDisabled
115 private lockBodyScroll () {
116 this.scrollPosition = this.viewportScroller.getScrollPosition()
117 document.getElementById('content').classList.add('lock-scroll')
120 private unlockBodyScroll () {
121 document.getElementById('content').classList.remove('lock-scroll')
122 this.viewportScroller.scrollToPosition(this.scrollPosition)
125 private async updatePreviews () {
126 if (this.content === null || this.content === undefined) return
128 this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate }))
129 this.previewHTML = await this.markdownRender(this.content)
132 private async markdownRender (text: string) {
135 if (this.customMarkdownRenderer) {
136 const result = await this.customMarkdownRenderer(text)
138 if (result instanceof HTMLElement) {
139 const wrapperElement = this.previewElement.nativeElement as HTMLElement
140 wrapperElement.innerHTML = ''
141 wrapperElement.appendChild(result)
146 } else if (this.markdownType === 'text') {
147 html = await this.markdownService.textMarkdownToHTML({ markdown: text })
149 html = await this.markdownService.enhancedMarkdownToHTML({ markdown: text })
152 if (this.markdownVideo) {
153 html = this.markdownService.processVideoTimestamps(this.markdownVideo.shortUUID, html)