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