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() classes: string[] | { [klass: string]: any[] | any } = []
29 @Input() textareaMaxWidth = '100%'
30 @Input() textareaHeight = '150px'
32 @Input() truncate: number
34 @Input() markdownType: 'text' | 'enhanced' = 'text'
35 @Input() customMarkdownRenderer?: (text: string) => Promise<string | HTMLElement>
37 @Input() markdownVideo: Video
39 @Input() name = 'description'
41 @ViewChild('textarea') textareaElement: ElementRef
42 @ViewChild('previewElement') previewElement: ElementRef
44 truncatedPreviewHTML: SafeHtml | string = ''
45 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
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 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 private lockBodyScroll () {
112 this.scrollPosition = this.viewportScroller.getScrollPosition()
113 document.getElementById('content').classList.add('lock-scroll')
116 private unlockBodyScroll () {
117 document.getElementById('content').classList.remove('lock-scroll')
118 this.viewportScroller.scrollToPosition(this.scrollPosition)
121 private async updatePreviews () {
122 if (this.content === null || this.content === undefined) return
124 this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate }))
125 this.previewHTML = await this.markdownRender(this.content)
128 private async markdownRender (text: string) {
131 if (this.customMarkdownRenderer) {
132 const result = await this.customMarkdownRenderer(text)
134 if (result instanceof HTMLElement) {
137 const wrapperElement = this.previewElement.nativeElement as HTMLElement
138 wrapperElement.innerHTML = ''
139 wrapperElement.appendChild(result)
144 } else if (this.markdownType === 'text') {
145 html = await this.markdownService.textMarkdownToHTML(text)
147 html = await this.markdownService.enhancedMarkdownToHTML(text)
150 if (this.markdownVideo) {
151 html = this.markdownService.processVideoTimestamps(this.markdownVideo.shortUUID, html)