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 { FormReactive, FormValidatorService, VideoCommentValidatorsService } from '@app/shared/shared-forms'
6 import { Video } from '@app/shared/shared-main'
7 import { VideoComment, VideoCommentService } from '@app/shared/shared-video-comment'
8 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
9 import { VideoCommentCreate } from '@shared/models'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
13 selector: 'my-video-comment-add',
14 templateUrl: './video-comment-add.component.html',
15 styleUrls: ['./video-comment-add.component.scss']
17 export class VideoCommentAddComponent extends FormReactive implements OnChanges, OnInit {
20 @Input() parentComment?: VideoComment
21 @Input() parentComments?: VideoComment[]
22 @Input() focusOnInit = false
23 @Input() textValue?: string
25 @Output() commentCreated = new EventEmitter<VideoComment>()
26 @Output() cancel = new EventEmitter()
28 @ViewChild('visitorModal', { static: true }) visitorModal: NgbModal
29 @ViewChild('emojiModal', { static: true }) emojiModal: NgbModal
30 @ViewChild('textarea', { static: true }) textareaElement: ElementRef
33 addingCommentButtonValue: string
36 protected formValidatorService: FormValidatorService,
37 private videoCommentValidatorsService: VideoCommentValidatorsService,
38 private notifier: Notifier,
39 private videoCommentService: VideoCommentService,
40 private modalService: NgbModal,
41 private router: Router,
47 get emojiMarkupList () {
48 const emojiMarkup = require('markdown-it-emoji/lib/data/light.json')
55 text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT
59 if (!this.parentComment) {
60 this.addingCommentButtonValue = this.i18n('Comment')
62 this.addingCommentButtonValue = this.i18n('Reply')
69 ngOnChanges (changes: SimpleChanges) {
70 if (changes.textValue && changes.textValue.currentValue && changes.textValue.currentValue !== changes.textValue.previousValue) {
71 this.patchTextValue(changes.textValue.currentValue, true)
77 if (!this.form.valid) return
82 openVisitorModal (event: any) {
83 if (this.user === null) { // we only open it for visitors
84 // fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error
85 event.srcElement.blur()
86 event.preventDefault()
88 this.modalService.open(this.visitorModal)
92 openEmojiModal (event: any) {
93 event.preventDefault()
94 this.modalService.open(this.emojiModal, { backdrop: true })
98 this.modalService.dismissAll()
102 // If we validate very quickly the comment form, we might comment twice
103 if (this.addingComment) return
105 this.addingComment = true
107 const commentCreate: VideoCommentCreate = this.form.value
108 let obs: Observable<VideoComment>
110 if (this.parentComment) {
111 obs = this.addCommentReply(commentCreate)
113 obs = this.addCommentThread(commentCreate)
118 this.addingComment = false
119 this.commentCreated.emit(comment)
124 this.addingComment = false
126 this.notifier.error(err.text)
131 isAddButtonDisplayed () {
132 return this.form.value['text']
136 return window.location.href
140 if (this.user) return this.user.accountAvatarUrl
141 return window.location.origin + '/client/assets/images/default-avatar.png'
146 this.router.navigate([ '/login' ])
149 cancelCommentReply () {
150 this.cancel.emit(null)
151 this.form.value['text'] = this.textareaElement.nativeElement.value = ''
154 private addCommentReply (commentCreate: VideoCommentCreate) {
155 return this.videoCommentService
156 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
159 private addCommentThread (commentCreate: VideoCommentCreate) {
160 return this.videoCommentService
161 .addCommentThread(this.video.id, commentCreate)
164 private initTextValue () {
165 if (this.textValue) {
166 this.patchTextValue(this.textValue, this.focusOnInit)
170 if (this.parentComment) {
171 const mentions = this.parentComments
172 .filter(c => c.account && c.account.id !== this.user.account.id) // Don't add mention of ourselves
173 .map(c => '@' + c.by)
175 const mentionsSet = new Set(mentions)
176 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
178 this.patchTextValue(mentionsText, this.focusOnInit)
182 private patchTextValue (text: string, focus: boolean) {
185 this.textareaElement.nativeElement.focus()
188 this.textareaElement.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'nearest' })
191 this.form.patchValue({ text })