1 import { debounceTime, distinctUntilChanged } from 'rxjs/operators'
2 import { Component, forwardRef, Input, OnInit } from '@angular/core'
3 import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'
4 import { MarkdownService } from '@app/videos/shared'
5 import { Subject } from 'rxjs'
6 import truncate from 'lodash-es/truncate'
7 import { ScreenService } from '@app/shared/misc/screen.service'
10 selector: 'my-markdown-textarea',
11 templateUrl: './markdown-textarea.component.html',
12 styleUrls: [ './markdown-textarea.component.scss' ],
15 provide: NG_VALUE_ACCESSOR,
16 useExisting: forwardRef(() => MarkdownTextareaComponent),
22 export class MarkdownTextareaComponent implements ControlValueAccessor, OnInit {
24 @Input() classes: string[] = []
25 @Input() textareaWidth = '100%'
26 @Input() textareaHeight = '150px'
27 @Input() previewColumn = false
28 @Input() truncate: number
29 @Input() markdownType: 'text' | 'enhanced' = 'text'
31 textareaMarginRight = '0'
32 flexDirection = 'column'
33 truncatedPreviewHTML = ''
36 private contentChanged = new Subject<string>()
39 private screenService: ScreenService,
40 private markdownService: MarkdownService
47 distinctUntilChanged()
49 .subscribe(() => this.updatePreviews())
51 this.contentChanged.next(this.content)
53 if (this.previewColumn) {
54 this.flexDirection = 'row'
55 this.textareaMarginRight = '15px'
59 propagateChange = (_: any) => { /* empty */ }
61 writeValue (description: string) {
62 this.content = description
64 this.contentChanged.next(this.content)
67 registerOnChange (fn: (_: any) => void) {
68 this.propagateChange = fn
71 registerOnTouched () {
76 this.propagateChange(this.content)
78 this.contentChanged.next(this.content)
81 arePreviewsDisplayed () {
82 return this.screenService.isInSmallView() === false
85 private updatePreviews () {
86 if (this.content === null || this.content === undefined) return
88 this.truncatedPreviewHTML = this.markdownRender(truncate(this.content, { length: this.truncate }))
89 this.previewHTML = this.markdownRender(this.content)
92 private markdownRender (text: string) {
93 if (this.markdownType === 'text') return this.markdownService.textMarkdownToHTML(text)
95 return this.markdownService.enhancedMarkdownToHTML(text)