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