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
24 @Input() commentThread?: boolean
26 @Output() commentCreated = new EventEmitter<VideoComment>()
27 @Output() cancel = new EventEmitter()
29 @ViewChild('visitorModal', { static: true }) visitorModal: NgbModal
30 @ViewChild('emojiModal', { static: true }) emojiModal: NgbModal
31 @ViewChild('textarea', { static: true }) textareaElement: ElementRef
34 addingCommentButtonValue: string
37 protected formValidatorService: FormValidatorService,
38 private videoCommentValidatorsService: VideoCommentValidatorsService,
39 private notifier: Notifier,
40 private videoCommentService: VideoCommentService,
41 private modalService: NgbModal,
42 private router: Router,
48 get emojiMarkupList () {
49 const emojiMarkup = require('markdown-it-emoji/lib/data/light.json')
56 text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT
60 if (this.commentThread) {
61 this.addingCommentButtonValue = this.i18n('Comment')
63 this.addingCommentButtonValue = this.i18n('Reply')
67 this.patchTextValue(this.textValue, this.focusOnInit)
71 if (this.parentComment) {
72 const mentions = this.parentComments
73 .filter(c => c.account && c.account.id !== this.user.account.id) // Don't add mention of ourselves
76 const mentionsSet = new Set(mentions)
77 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
79 this.patchTextValue(mentionsText, this.focusOnInit)
84 ngOnChanges (changes: SimpleChanges) {
85 if (changes.textValue && changes.textValue.currentValue && changes.textValue.currentValue !== changes.textValue.previousValue) {
86 this.patchTextValue(changes.textValue.currentValue, true)
92 if (!this.form.valid) return
97 openVisitorModal (event: any) {
98 if (this.user === null) { // we only open it for visitors
99 // fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error
100 event.srcElement.blur()
101 event.preventDefault()
103 this.modalService.open(this.visitorModal)
107 openEmojiModal (event: any) {
108 event.preventDefault()
109 this.modalService.open(this.emojiModal, { backdrop: true })
113 this.modalService.dismissAll()
117 // If we validate very quickly the comment form, we might comment twice
118 if (this.addingComment) return
120 this.addingComment = true
122 const commentCreate: VideoCommentCreate = this.form.value
123 let obs: Observable<VideoComment>
125 if (this.parentComment) {
126 obs = this.addCommentReply(commentCreate)
128 obs = this.addCommentThread(commentCreate)
133 this.addingComment = false
134 this.commentCreated.emit(comment)
139 this.addingComment = false
141 this.notifier.error(err.text)
146 isAddButtonDisplayed () {
147 return this.form.value['text']
151 return window.location.href
155 if (this.user) return this.user.accountAvatarUrl
156 return window.location.origin + '/client/assets/images/default-avatar.png'
161 this.router.navigate([ '/login' ])
164 cancelCommentReply () {
165 this.cancel.emit(null)
166 this.form.value['text'] = this.textareaElement.nativeElement.value = ''
169 private addCommentReply (commentCreate: VideoCommentCreate) {
170 return this.videoCommentService
171 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
174 private addCommentThread (commentCreate: VideoCommentCreate) {
175 return this.videoCommentService
176 .addCommentThread(this.video.id, commentCreate)
179 private patchTextValue (text: string, focus: boolean) {
182 this.textareaElement.nativeElement.focus()
185 this.textareaElement.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'nearest' })
188 this.form.patchValue({ text })