]>
Commit | Line | Data |
---|---|---|
363726fe | 1 | import { ViewportScroller } from '@angular/common' |
67ed6552 C |
2 | import truncate from 'lodash-es/truncate' |
3 | import { Subject } from 'rxjs' | |
db400f44 | 4 | import { debounceTime, distinctUntilChanged } from 'rxjs/operators' |
67ed6552 | 5 | import { Component, ElementRef, forwardRef, Input, OnInit, ViewChild } from '@angular/core' |
2de96f4d | 6 | import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms' |
aa9cf3b9 | 7 | import { MarkdownService, ScreenService } from '@app/core' |
2de96f4d C |
8 | |
9 | @Component({ | |
66b16caf C |
10 | selector: 'my-markdown-textarea', |
11 | templateUrl: './markdown-textarea.component.html', | |
12 | styleUrls: [ './markdown-textarea.component.scss' ], | |
2de96f4d C |
13 | providers: [ |
14 | { | |
15 | provide: NG_VALUE_ACCESSOR, | |
66b16caf | 16 | useExisting: forwardRef(() => MarkdownTextareaComponent), |
2de96f4d C |
17 | multi: true |
18 | } | |
19 | ] | |
20 | }) | |
21 | ||
66b16caf | 22 | export class MarkdownTextareaComponent implements ControlValueAccessor, OnInit { |
07fa4c97 | 23 | @Input() content = '' |
be27ef3b | 24 | @Input() classes: string[] | { [klass: string]: any[] | any } = [] |
b15fe00f | 25 | @Input() textareaMaxWidth = '100%' |
66b16caf | 26 | @Input() textareaHeight = '150px' |
66b16caf | 27 | @Input() truncate: number |
07fa4c97 | 28 | @Input() markdownType: 'text' | 'enhanced' = 'text' |
d68ebf0b | 29 | @Input() markdownVideo = false |
4ee6a8b1 | 30 | @Input() name = 'description' |
66b16caf | 31 | |
b15fe00f K |
32 | @ViewChild('textarea') textareaElement: ElementRef |
33 | ||
07fa4c97 C |
34 | truncatedPreviewHTML = '' |
35 | previewHTML = '' | |
b15fe00f | 36 | isMaximized = false |
2de96f4d | 37 | |
07fa4c97 | 38 | private contentChanged = new Subject<string>() |
363726fe | 39 | private scrollPosition: [number, number] |
2de96f4d | 40 | |
363726fe | 41 | constructor ( |
42 | private viewportScroller: ViewportScroller, | |
aa9cf3b9 | 43 | private screenService: ScreenService, |
363726fe | 44 | private markdownService: MarkdownService |
45 | ) { } | |
2de96f4d C |
46 | |
47 | ngOnInit () { | |
07fa4c97 | 48 | this.contentChanged |
db400f44 C |
49 | .pipe( |
50 | debounceTime(150), | |
51 | distinctUntilChanged() | |
52 | ) | |
53 | .subscribe(() => this.updatePreviews()) | |
2de96f4d | 54 | |
07fa4c97 | 55 | this.contentChanged.next(this.content) |
2de96f4d C |
56 | } |
57 | ||
58 | propagateChange = (_: any) => { /* empty */ } | |
59 | ||
60 | writeValue (description: string) { | |
07fa4c97 | 61 | this.content = description |
2de96f4d | 62 | |
07fa4c97 | 63 | this.contentChanged.next(this.content) |
2de96f4d C |
64 | } |
65 | ||
66 | registerOnChange (fn: (_: any) => void) { | |
67 | this.propagateChange = fn | |
68 | } | |
69 | ||
70 | registerOnTouched () { | |
71 | // Unused | |
72 | } | |
73 | ||
74 | onModelChange () { | |
07fa4c97 | 75 | this.propagateChange(this.content) |
2de96f4d | 76 | |
07fa4c97 | 77 | this.contentChanged.next(this.content) |
2de96f4d C |
78 | } |
79 | ||
b15fe00f K |
80 | onMaximizeClick () { |
81 | this.isMaximized = !this.isMaximized | |
82 | ||
83 | // Make sure textarea have the focus | |
aa9cf3b9 | 84 | // Except on touchscreens devices, the virtual keyboard may move up and hide the textarea in maximized mode |
85 | if (!this.screenService.isInTouchScreen()) { | |
86 | this.textareaElement.nativeElement.focus() | |
87 | } | |
b15fe00f K |
88 | |
89 | // Make sure the window has no scrollbars | |
90 | if (!this.isMaximized) { | |
91 | this.unlockBodyScroll() | |
92 | } else { | |
93 | this.lockBodyScroll() | |
94 | } | |
95 | } | |
96 | ||
97 | private lockBodyScroll () { | |
363726fe | 98 | this.scrollPosition = this.viewportScroller.getScrollPosition() |
b15fe00f K |
99 | document.getElementById('content').classList.add('lock-scroll') |
100 | } | |
101 | ||
102 | private unlockBodyScroll () { | |
103 | document.getElementById('content').classList.remove('lock-scroll') | |
363726fe | 104 | this.viewportScroller.scrollToPosition(this.scrollPosition) |
6693df9d C |
105 | } |
106 | ||
41d71344 | 107 | private async updatePreviews () { |
07fa4c97 | 108 | if (this.content === null || this.content === undefined) return |
f595d394 | 109 | |
41d71344 C |
110 | this.truncatedPreviewHTML = await this.markdownRender(truncate(this.content, { length: this.truncate })) |
111 | this.previewHTML = await this.markdownRender(this.content) | |
07fa4c97 C |
112 | } |
113 | ||
d68ebf0b L |
114 | private async markdownRender (text: string) { |
115 | const html = this.markdownType === 'text' ? | |
116 | await this.markdownService.textMarkdownToHTML(text) : | |
117 | await this.markdownService.enhancedMarkdownToHTML(text) | |
07fa4c97 | 118 | |
d68ebf0b | 119 | return this.markdownVideo ? this.markdownService.processVideoTimestamps(html) : html |
2de96f4d C |
120 | } |
121 | } |