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
48 private emojiMarkupList: { emoji: string, name: string }[]
51 protected formValidatorService: FormValidatorService,
52 private notifier: Notifier,
53 private videoCommentService: VideoCommentService,
54 private modalService: NgbModal,
55 private router: Router,
56 @Inject(LOCALE_ID) private localeId: string
63 text: VIDEO_COMMENT_TEXT_VALIDATOR
67 if (!this.parentComment) {
68 this.addingCommentButtonValue = $localize`Comment`
70 this.addingCommentButtonValue = $localize`Reply`
77 ngOnChanges (changes: SimpleChanges) {
78 // Not initialized yet
79 if (!this.form) return
81 if (changes.textValue?.currentValue && changes.textValue.currentValue !== changes.textValue.previousValue) {
82 this.patchTextValue(changes.textValue.currentValue, true)
86 getEmojiMarkupList () {
87 if (this.emojiMarkupList) return this.emojiMarkupList
89 const emojiMarkupObjectList = require('markdown-it-emoji/lib/data/light.json')
91 this.emojiMarkupList = []
92 for (const name of Object.keys(emojiMarkupObjectList)) {
93 const emoji = emojiMarkupObjectList[name]
94 this.emojiMarkupList.push({ emoji, name })
97 return this.emojiMarkupList
102 if (!this.form.valid) return
107 openVisitorModal (event: any) {
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()
113 this.modalService.open(this.visitorModal)
117 openEmojiModal (event: any) {
118 event.preventDefault()
119 this.modalService.open(this.emojiModal, { backdrop: true, size: 'lg' })
123 this.modalService.dismissAll()
127 // If we validate very quickly the comment form, we might comment twice
128 if (this.addingComment) return
130 this.addingComment = true
132 const commentCreate: VideoCommentCreate = this.form.value
133 let obs: Observable<VideoComment>
135 if (this.parentComment) {
136 obs = this.addCommentReply(commentCreate)
138 obs = this.addCommentThread(commentCreate)
143 this.addingComment = false
144 this.commentCreated.emit(comment)
149 this.addingComment = false
151 this.notifier.error(err.text)
156 isAddButtonDisplayed () {
157 return this.form.value['text']
161 return window.location.href
166 this.router.navigate([ '/login' ])
169 cancelCommentReply () {
170 this.cancel.emit(null)
171 this.form.value['text'] = this.textareaElement.nativeElement.value = ''
175 return getLocaleDirection(this.localeId) === 'rtl'
178 getAvatarActorType () {
179 if (this.user) return 'account'
184 private addCommentReply (commentCreate: VideoCommentCreate) {
185 return this.videoCommentService
186 .addCommentReply(this.video.uuid, this.parentComment.id, commentCreate)
189 private addCommentThread (commentCreate: VideoCommentCreate) {
190 return this.videoCommentService
191 .addCommentThread(this.video.uuid, commentCreate)
194 private initTextValue () {
195 if (this.textValue) {
196 this.patchTextValue(this.textValue, this.focusOnInit)
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)
205 const mentionsSet = new Set(mentions)
206 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
208 this.patchTextValue(mentionsText, this.focusOnInit)
212 private patchTextValue (text: string, focus: boolean) {
215 this.textareaElement.nativeElement.focus()
218 // Scroll to textarea
219 this.textareaElement.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'nearest' })
221 // Use the native textarea autosize according to the text's break lines
222 this.textareaElement.nativeElement.dispatchEvent(new Event('input'))
225 this.form.patchValue({ text })