]>
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', | |
9df52d66 | 28 | styleUrls: [ './video-comment-add.component.scss' ] |
4635f59d | 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 | |
88edc66e C |
48 | private emojiMarkupList: { emoji: string, name: string }[] |
49 | ||
4635f59d | 50 | constructor ( |
d18d6478 | 51 | protected formValidatorService: FormValidatorService, |
f8b2c1b4 | 52 | private notifier: Notifier, |
b1d40cff | 53 | private videoCommentService: VideoCommentService, |
660d11e9 | 54 | private modalService: NgbModal, |
27bc9586 C |
55 | private router: Router, |
56 | @Inject(LOCALE_ID) private localeId: string | |
4635f59d C |
57 | ) { |
58 | super() | |
59 | } | |
60 | ||
4635f59d | 61 | ngOnInit () { |
d18d6478 | 62 | this.buildForm({ |
7ed1edbb | 63 | text: VIDEO_COMMENT_TEXT_VALIDATOR |
d18d6478 | 64 | }) |
eacf925e | 65 | |
660d11e9 | 66 | if (this.user) { |
fdd12965 | 67 | if (!this.parentComment) { |
da188b9f | 68 | this.addingCommentButtonValue = $localize`Comment` |
cb54210c | 69 | } else { |
da188b9f | 70 | this.addingCommentButtonValue = $localize`Reply` |
cb54210c | 71 | } |
72 | ||
fdd12965 | 73 | this.initTextValue() |
d7e70384 | 74 | } |
4635f59d C |
75 | } |
76 | ||
f63c03fb | 77 | ngOnChanges (changes: SimpleChanges) { |
5982ffc4 C |
78 | // Not initialized yet |
79 | if (!this.form) return | |
80 | ||
9df52d66 | 81 | if (changes.textValue?.currentValue && changes.textValue.currentValue !== changes.textValue.previousValue) { |
f63c03fb | 82 | this.patchTextValue(changes.textValue.currentValue, true) |
83 | } | |
84 | } | |
85 | ||
88edc66e C |
86 | getEmojiMarkupList () { |
87 | if (this.emojiMarkupList) return this.emojiMarkupList | |
88 | ||
89 | const emojiMarkupObjectList = require('markdown-it-emoji/lib/data/light.json') | |
90 | ||
91 | this.emojiMarkupList = [] | |
92 | for (const name of Object.keys(emojiMarkupObjectList)) { | |
93 | const emoji = emojiMarkupObjectList[name] | |
94 | this.emojiMarkupList.push({ emoji, name }) | |
95 | } | |
96 | ||
97 | return this.emojiMarkupList | |
98 | } | |
99 | ||
1263fc4e | 100 | onValidKey () { |
cc4bf76c | 101 | this.forceCheck() |
1263fc4e C |
102 | if (!this.form.valid) return |
103 | ||
104 | this.formValidated() | |
105 | } | |
106 | ||
244b4ae3 | 107 | openVisitorModal (event: any) { |
660d11e9 RK |
108 | if (this.user === null) { // we only open it for visitors |
109 | // fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error | |
110 | event.srcElement.blur() | |
111 | event.preventDefault() | |
112 | ||
113 | this.modalService.open(this.visitorModal) | |
114 | } | |
115 | } | |
116 | ||
ee3bd9db | 117 | openEmojiModal (event: any) { |
118 | event.preventDefault() | |
f34cc2a4 | 119 | this.modalService.open(this.emojiModal, { backdrop: true, size: 'lg' }) |
ee3bd9db | 120 | } |
121 | ||
122 | hideModals () { | |
660d11e9 RK |
123 | this.modalService.dismissAll() |
124 | } | |
125 | ||
4635f59d | 126 | formValidated () { |
ff336427 C |
127 | // If we validate very quickly the comment form, we might comment twice |
128 | if (this.addingComment) return | |
129 | ||
130 | this.addingComment = true | |
131 | ||
4635f59d | 132 | const commentCreate: VideoCommentCreate = this.form.value |
be27ef3b | 133 | let obs: Observable<VideoComment> |
4635f59d C |
134 | |
135 | if (this.parentComment) { | |
136 | obs = this.addCommentReply(commentCreate) | |
137 | } else { | |
138 | obs = this.addCommentThread(commentCreate) | |
139 | } | |
140 | ||
1378c0d3 C |
141 | obs.subscribe({ |
142 | next: comment => { | |
ff336427 | 143 | this.addingComment = false |
4635f59d C |
144 | this.commentCreated.emit(comment) |
145 | this.form.reset() | |
146 | }, | |
147 | ||
1378c0d3 | 148 | error: err => { |
ff336427 C |
149 | this.addingComment = false |
150 | ||
255c0030 | 151 | this.notifier.error(err.message) |
ff336427 | 152 | } |
1378c0d3 | 153 | }) |
d50acfab | 154 | } |
4635f59d C |
155 | |
156 | isAddButtonDisplayed () { | |
157 | return this.form.value['text'] | |
158 | } | |
159 | ||
3ddb1ec5 | 160 | getUri () { |
6d5973fa RK |
161 | return window.location.href |
162 | } | |
163 | ||
660d11e9 | 164 | gotoLogin () { |
ee3bd9db | 165 | this.hideModals() |
660d11e9 RK |
166 | this.router.navigate([ '/login' ]) |
167 | } | |
168 | ||
3f9c4955 C |
169 | cancelCommentReply () { |
170 | this.cancel.emit(null) | |
79671021 | 171 | this.form.value['text'] = this.textareaElement.nativeElement.value = '' |
3f9c4955 C |
172 | } |
173 | ||
27bc9586 C |
174 | isRTL () { |
175 | return getLocaleDirection(this.localeId) === 'rtl' | |
176 | } | |
177 | ||
87fdea2f C |
178 | getAvatarActorType () { |
179 | if (this.user) return 'account' | |
180 | ||
181 | return 'unlogged' | |
182 | } | |
183 | ||
4635f59d C |
184 | private addCommentReply (commentCreate: VideoCommentCreate) { |
185 | return this.videoCommentService | |
7c07259a | 186 | .addCommentReply(this.video.uuid, this.parentComment.id, commentCreate) |
4635f59d C |
187 | } |
188 | ||
189 | private addCommentThread (commentCreate: VideoCommentCreate) { | |
190 | return this.videoCommentService | |
7c07259a | 191 | .addCommentThread(this.video.uuid, commentCreate) |
4635f59d | 192 | } |
f63c03fb | 193 | |
fdd12965 | 194 | private initTextValue () { |
195 | if (this.textValue) { | |
196 | this.patchTextValue(this.textValue, this.focusOnInit) | |
197 | return | |
198 | } | |
199 | ||
200 | if (this.parentComment) { | |
201 | const mentions = this.parentComments | |
202 | .filter(c => c.account && c.account.id !== this.user.account.id) // Don't add mention of ourselves | |
203 | .map(c => '@' + c.by) | |
204 | ||
205 | const mentionsSet = new Set(mentions) | |
206 | const mentionsText = Array.from(mentionsSet).join(' ') + ' ' | |
207 | ||
208 | this.patchTextValue(mentionsText, this.focusOnInit) | |
209 | } | |
210 | } | |
211 | ||
f63c03fb | 212 | private patchTextValue (text: string, focus: boolean) { |
213 | setTimeout(() => { | |
214 | if (focus) { | |
215 | this.textareaElement.nativeElement.focus() | |
216 | } | |
217 | ||
6a9498e3 | 218 | // Scroll to textarea |
f63c03fb | 219 | this.textareaElement.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'nearest' }) |
6a9498e3 K |
220 | |
221 | // Use the native textarea autosize according to the text's break lines | |
222 | this.textareaElement.nativeElement.dispatchEvent(new Event('input')) | |
f63c03fb | 223 | }) |
224 | ||
225 | this.form.patchValue({ text }) | |
226 | } | |
4635f59d | 227 | } |