]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-watch/shared/comment/video-comment-add.component.ts
Refactor actor avatar component
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-watch / shared / comment / video-comment-add.component.ts
CommitLineData
67ed6552 1import { Observable } from 'rxjs'
27bc9586
C
2import { getLocaleDirection } from '@angular/common'
3import {
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 16import { Router } from '@angular/router'
67ed6552 17import { Notifier, User } from '@app/core'
7ed1edbb
C
18import { VIDEO_COMMENT_TEXT_VALIDATOR } from '@app/shared/form-validators/video-comment-validators'
19import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
fe88ca69 20import { Video } from '@app/shared/shared-main'
cfde28ba 21import { VideoComment, VideoCommentService } from '@app/shared/shared-video-comment'
67ed6552
C
22import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
23import { 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 30export 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 () {
87fdea2f 60 console.log('hi')
f34cc2a4
K
61 const emojiMarkupObjectList = require('markdown-it-emoji/lib/data/light.json')
62
63 // Populate emoji-markup-list from object to array to avoid keys alphabetical order
64 const emojiMarkupArrayList = []
65 for (const emojiMarkupName in emojiMarkupObjectList) {
66 if (emojiMarkupName) {
67 const emoji = emojiMarkupObjectList[emojiMarkupName]
9df52d66 68 emojiMarkupArrayList.push([ emoji, emojiMarkupName ])
f34cc2a4
K
69 }
70 }
ee3bd9db 71
f34cc2a4 72 return emojiMarkupArrayList
ee3bd9db 73 }
74
4635f59d 75 ngOnInit () {
d18d6478 76 this.buildForm({
7ed1edbb 77 text: VIDEO_COMMENT_TEXT_VALIDATOR
d18d6478 78 })
eacf925e 79
660d11e9 80 if (this.user) {
fdd12965 81 if (!this.parentComment) {
da188b9f 82 this.addingCommentButtonValue = $localize`Comment`
cb54210c 83 } else {
da188b9f 84 this.addingCommentButtonValue = $localize`Reply`
cb54210c 85 }
86
fdd12965 87 this.initTextValue()
d7e70384 88 }
4635f59d
C
89 }
90
f63c03fb 91 ngOnChanges (changes: SimpleChanges) {
5982ffc4
C
92 // Not initialized yet
93 if (!this.form) return
94
9df52d66 95 if (changes.textValue?.currentValue && changes.textValue.currentValue !== changes.textValue.previousValue) {
f63c03fb 96 this.patchTextValue(changes.textValue.currentValue, true)
97 }
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
f8b2c1b4 151 this.notifier.error(err.text)
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}