1 import { Observable } from 'rxjs'
2 import { getLocaleDirection } from '@angular/common'
15 } from '@angular/core'
16 import { Router } from '@angular/router'
17 import { Notifier, User } from '@app/core'
18 import { VIDEO_COMMENT_TEXT_VALIDATOR } from '@app/shared/form-validators/video-comment-validators'
19 import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
20 import { Video } from '@app/shared/shared-main'
21 import { VideoComment, VideoCommentService } from '@app/shared/shared-video-comment'
22 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
23 import { VideoCommentCreate } from '@shared/models'
26 selector: 'my-video-comment-add',
27 templateUrl: './video-comment-add.component.html',
28 styleUrls: [ './video-comment-add.component.scss' ]
30 export class VideoCommentAddComponent extends FormReactive implements OnChanges, OnInit {
33 @Input() parentComment?: VideoComment
34 @Input() parentComments?: VideoComment[]
35 @Input() focusOnInit = false
36 @Input() textValue?: string
38 @Output() commentCreated = new EventEmitter<VideoComment>()
39 @Output() cancel = new EventEmitter()
41 @ViewChild('visitorModal', { static: true }) visitorModal: NgbModal
42 @ViewChild('emojiModal', { static: true }) emojiModal: NgbModal
43 @ViewChild('textarea', { static: true }) textareaElement: ElementRef
46 addingCommentButtonValue: string
49 protected formValidatorService: FormValidatorService,
50 private notifier: Notifier,
51 private videoCommentService: VideoCommentService,
52 private modalService: NgbModal,
53 private router: Router,
54 @Inject(LOCALE_ID) private localeId: string
59 get emojiMarkupList () {
60 const emojiMarkupObjectList = require('markdown-it-emoji/lib/data/light.json')
62 // Populate emoji-markup-list from object to array to avoid keys alphabetical order
63 const emojiMarkupArrayList = []
64 for (const emojiMarkupName in emojiMarkupObjectList) {
65 if (emojiMarkupName) {
66 const emoji = emojiMarkupObjectList[emojiMarkupName]
67 emojiMarkupArrayList.push([ emoji, emojiMarkupName ])
71 return emojiMarkupArrayList
76 text: VIDEO_COMMENT_TEXT_VALIDATOR
80 if (!this.parentComment) {
81 this.addingCommentButtonValue = $localize`Comment`
83 this.addingCommentButtonValue = $localize`Reply`
90 ngOnChanges (changes: SimpleChanges) {
91 // Not initialized yet
92 if (!this.form) return
94 if (changes.textValue?.currentValue && changes.textValue.currentValue !== changes.textValue.previousValue) {
95 this.patchTextValue(changes.textValue.currentValue, true)
101 if (!this.form.valid) return
106 openVisitorModal (event: any) {
107 if (this.user === null) { // we only open it for visitors
108 // fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error
109 event.srcElement.blur()
110 event.preventDefault()
112 this.modalService.open(this.visitorModal)
116 openEmojiModal (event: any) {
117 event.preventDefault()
118 this.modalService.open(this.emojiModal, { backdrop: true, size: 'lg' })
122 this.modalService.dismissAll()
126 // If we validate very quickly the comment form, we might comment twice
127 if (this.addingComment) return
129 this.addingComment = true
131 const commentCreate: VideoCommentCreate = this.form.value
132 let obs: Observable<VideoComment>
134 if (this.parentComment) {
135 obs = this.addCommentReply(commentCreate)
137 obs = this.addCommentThread(commentCreate)
142 this.addingComment = false
143 this.commentCreated.emit(comment)
148 this.addingComment = false
150 this.notifier.error(err.text)
155 isAddButtonDisplayed () {
156 return this.form.value['text']
160 return window.location.href
165 this.router.navigate([ '/login' ])
168 cancelCommentReply () {
169 this.cancel.emit(null)
170 this.form.value['text'] = this.textareaElement.nativeElement.value = ''
174 return getLocaleDirection(this.localeId) === 'rtl'
177 private addCommentReply (commentCreate: VideoCommentCreate) {
178 return this.videoCommentService
179 .addCommentReply(this.video.uuid, this.parentComment.id, commentCreate)
182 private addCommentThread (commentCreate: VideoCommentCreate) {
183 return this.videoCommentService
184 .addCommentThread(this.video.uuid, commentCreate)
187 private initTextValue () {
188 if (this.textValue) {
189 this.patchTextValue(this.textValue, this.focusOnInit)
193 if (this.parentComment) {
194 const mentions = this.parentComments
195 .filter(c => c.account && c.account.id !== this.user.account.id) // Don't add mention of ourselves
196 .map(c => '@' + c.by)
198 const mentionsSet = new Set(mentions)
199 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
201 this.patchTextValue(mentionsText, this.focusOnInit)
205 private patchTextValue (text: string, focus: boolean) {
208 this.textareaElement.nativeElement.focus()
211 // Scroll to textarea
212 this.textareaElement.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'nearest' })
214 // Use the native textarea autosize according to the text's break lines
215 this.textareaElement.nativeElement.dispatchEvent(new Event('input'))
218 this.form.patchValue({ text })