]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { Observable } from 'rxjs' |
27bc9586 C |
2 | import { getLocaleDirection } from '@angular/common' |
3 | import { | |
4 | Component, | |
5 | ElementRef, | |
6 | EventEmitter, | |
7 | Inject, | |
8 | Input, | |
9 | LOCALE_ID, | |
10 | OnChanges, | |
11 | OnInit, | |
12 | Output, | |
13 | SimpleChanges, | |
14 | ViewChild | |
15 | } from '@angular/core' | |
660d11e9 | 16 | import { Router } from '@angular/router' |
67ed6552 | 17 | import { Notifier, User } from '@app/core' |
7ed1edbb C |
18 | import { VIDEO_COMMENT_TEXT_VALIDATOR } from '@app/shared/form-validators/video-comment-validators' |
19 | import { FormReactive, FormValidatorService } from '@app/shared/shared-forms' | |
fe88ca69 | 20 | import { Video } from '@app/shared/shared-main' |
cfde28ba | 21 | import { VideoComment, VideoCommentService } from '@app/shared/shared-video-comment' |
67ed6552 C |
22 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' |
23 | import { VideoCommentCreate } from '@shared/models' | |
4635f59d C |
24 | |
25 | @Component({ | |
26 | selector: 'my-video-comment-add', | |
27 | templateUrl: './video-comment-add.component.html', | |
28 | styleUrls: ['./video-comment-add.component.scss'] | |
29 | }) | |
f63c03fb | 30 | export class VideoCommentAddComponent extends FormReactive implements OnChanges, OnInit { |
cf117aaa | 31 | @Input() user: User |
4635f59d | 32 | @Input() video: Video |
fdd12965 | 33 | @Input() parentComment?: VideoComment |
34 | @Input() parentComments?: VideoComment[] | |
eacf925e | 35 | @Input() focusOnInit = false |
f63c03fb | 36 | @Input() textValue?: string |
4635f59d | 37 | |
be27ef3b | 38 | @Output() commentCreated = new EventEmitter<VideoComment>() |
88adad2d | 39 | @Output() cancel = new EventEmitter() |
4635f59d | 40 | |
f36da21e | 41 | @ViewChild('visitorModal', { static: true }) visitorModal: NgbModal |
ee3bd9db | 42 | @ViewChild('emojiModal', { static: true }) emojiModal: NgbModal |
f36da21e | 43 | @ViewChild('textarea', { static: true }) textareaElement: ElementRef |
eacf925e | 44 | |
2fbe7f19 | 45 | addingComment = false |
cb54210c | 46 | addingCommentButtonValue: string |
ff336427 | 47 | |
4635f59d | 48 | constructor ( |
d18d6478 | 49 | protected formValidatorService: FormValidatorService, |
f8b2c1b4 | 50 | private notifier: Notifier, |
b1d40cff | 51 | private videoCommentService: VideoCommentService, |
660d11e9 | 52 | private modalService: NgbModal, |
27bc9586 C |
53 | private router: Router, |
54 | @Inject(LOCALE_ID) private localeId: string | |
4635f59d C |
55 | ) { |
56 | super() | |
57 | } | |
58 | ||
ee3bd9db | 59 | get emojiMarkupList () { |
f34cc2a4 K |
60 | const emojiMarkupObjectList = require('markdown-it-emoji/lib/data/light.json') |
61 | ||
62 | // Populate emoji-markup-list from object to array to avoid keys alphabetical order | |
63 | const emojiMarkupArrayList = [] | |
64 | for (const emojiMarkupName in emojiMarkupObjectList) { | |
65 | if (emojiMarkupName) { | |
66 | const emoji = emojiMarkupObjectList[emojiMarkupName] | |
67 | emojiMarkupArrayList.push([emoji, emojiMarkupName]) | |
68 | } | |
69 | } | |
ee3bd9db | 70 | |
f34cc2a4 | 71 | return emojiMarkupArrayList |
ee3bd9db | 72 | } |
73 | ||
4635f59d | 74 | ngOnInit () { |
d18d6478 | 75 | this.buildForm({ |
7ed1edbb | 76 | text: VIDEO_COMMENT_TEXT_VALIDATOR |
d18d6478 | 77 | }) |
eacf925e | 78 | |
660d11e9 | 79 | if (this.user) { |
fdd12965 | 80 | if (!this.parentComment) { |
da188b9f | 81 | this.addingCommentButtonValue = $localize`Comment` |
cb54210c | 82 | } else { |
da188b9f | 83 | this.addingCommentButtonValue = $localize`Reply` |
cb54210c | 84 | } |
85 | ||
fdd12965 | 86 | this.initTextValue() |
d7e70384 | 87 | } |
4635f59d C |
88 | } |
89 | ||
f63c03fb | 90 | ngOnChanges (changes: SimpleChanges) { |
5982ffc4 C |
91 | // Not initialized yet |
92 | if (!this.form) return | |
93 | ||
f63c03fb | 94 | if (changes.textValue && changes.textValue.currentValue && changes.textValue.currentValue !== changes.textValue.previousValue) { |
95 | this.patchTextValue(changes.textValue.currentValue, true) | |
96 | } | |
97 | } | |
98 | ||
1263fc4e | 99 | onValidKey () { |
3866f1a0 | 100 | this.check() |
1263fc4e C |
101 | if (!this.form.valid) return |
102 | ||
103 | this.formValidated() | |
104 | } | |
105 | ||
244b4ae3 | 106 | openVisitorModal (event: any) { |
660d11e9 RK |
107 | if (this.user === null) { // we only open it for visitors |
108 | // fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error | |
109 | event.srcElement.blur() | |
110 | event.preventDefault() | |
111 | ||
112 | this.modalService.open(this.visitorModal) | |
113 | } | |
114 | } | |
115 | ||
ee3bd9db | 116 | openEmojiModal (event: any) { |
117 | event.preventDefault() | |
f34cc2a4 | 118 | this.modalService.open(this.emojiModal, { backdrop: true, size: 'lg' }) |
ee3bd9db | 119 | } |
120 | ||
121 | hideModals () { | |
660d11e9 RK |
122 | this.modalService.dismissAll() |
123 | } | |
124 | ||
4635f59d | 125 | formValidated () { |
ff336427 C |
126 | // If we validate very quickly the comment form, we might comment twice |
127 | if (this.addingComment) return | |
128 | ||
129 | this.addingComment = true | |
130 | ||
4635f59d | 131 | const commentCreate: VideoCommentCreate = this.form.value |
be27ef3b | 132 | let obs: Observable<VideoComment> |
4635f59d C |
133 | |
134 | if (this.parentComment) { | |
135 | obs = this.addCommentReply(commentCreate) | |
136 | } else { | |
137 | obs = this.addCommentThread(commentCreate) | |
138 | } | |
139 | ||
140 | obs.subscribe( | |
141 | comment => { | |
ff336427 | 142 | this.addingComment = false |
4635f59d C |
143 | this.commentCreated.emit(comment) |
144 | this.form.reset() | |
145 | }, | |
146 | ||
ff336427 C |
147 | err => { |
148 | this.addingComment = false | |
149 | ||
f8b2c1b4 | 150 | this.notifier.error(err.text) |
ff336427 | 151 | } |
4635f59d | 152 | ) |
d50acfab | 153 | } |
4635f59d C |
154 | |
155 | isAddButtonDisplayed () { | |
156 | return this.form.value['text'] | |
157 | } | |
158 | ||
3ddb1ec5 | 159 | getUri () { |
6d5973fa RK |
160 | return window.location.href |
161 | } | |
162 | ||
660d11e9 | 163 | gotoLogin () { |
ee3bd9db | 164 | this.hideModals() |
660d11e9 RK |
165 | this.router.navigate([ '/login' ]) |
166 | } | |
167 | ||
3f9c4955 C |
168 | cancelCommentReply () { |
169 | this.cancel.emit(null) | |
79671021 | 170 | this.form.value['text'] = this.textareaElement.nativeElement.value = '' |
3f9c4955 C |
171 | } |
172 | ||
27bc9586 C |
173 | isRTL () { |
174 | return getLocaleDirection(this.localeId) === 'rtl' | |
175 | } | |
176 | ||
4635f59d C |
177 | private addCommentReply (commentCreate: VideoCommentCreate) { |
178 | return this.videoCommentService | |
179 | .addCommentReply(this.video.id, this.parentComment.id, commentCreate) | |
180 | } | |
181 | ||
182 | private addCommentThread (commentCreate: VideoCommentCreate) { | |
183 | return this.videoCommentService | |
184 | .addCommentThread(this.video.id, commentCreate) | |
185 | } | |
f63c03fb | 186 | |
fdd12965 | 187 | private initTextValue () { |
188 | if (this.textValue) { | |
189 | this.patchTextValue(this.textValue, this.focusOnInit) | |
190 | return | |
191 | } | |
192 | ||
193 | if (this.parentComment) { | |
194 | const mentions = this.parentComments | |
195 | .filter(c => c.account && c.account.id !== this.user.account.id) // Don't add mention of ourselves | |
196 | .map(c => '@' + c.by) | |
197 | ||
198 | const mentionsSet = new Set(mentions) | |
199 | const mentionsText = Array.from(mentionsSet).join(' ') + ' ' | |
200 | ||
201 | this.patchTextValue(mentionsText, this.focusOnInit) | |
202 | } | |
203 | } | |
204 | ||
f63c03fb | 205 | private patchTextValue (text: string, focus: boolean) { |
206 | setTimeout(() => { | |
207 | if (focus) { | |
208 | this.textareaElement.nativeElement.focus() | |
209 | } | |
210 | ||
6a9498e3 | 211 | // Scroll to textarea |
f63c03fb | 212 | this.textareaElement.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'nearest' }) |
6a9498e3 K |
213 | |
214 | // Use the native textarea autosize according to the text's break lines | |
215 | this.textareaElement.nativeElement.dispatchEvent(new Event('input')) | |
f63c03fb | 216 | }) |
217 | ||
218 | this.form.patchValue({ text }) | |
219 | } | |
4635f59d | 220 | } |