]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-watch/comment/video-comment-add.component.ts
3fa5fd623af7a419549cdbfbe1f1c87da70966db
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-watch / comment / video-comment-add.component.ts
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'
11
12 @Component({
13 selector: 'my-video-comment-add',
14 templateUrl: './video-comment-add.component.html',
15 styleUrls: ['./video-comment-add.component.scss']
16 })
17 export class VideoCommentAddComponent extends FormReactive implements OnChanges, OnInit {
18 @Input() user: User
19 @Input() video: Video
20 @Input() parentComment?: VideoComment
21 @Input() parentComments?: VideoComment[]
22 @Input() focusOnInit = false
23 @Input() textValue?: string
24
25 @Output() commentCreated = new EventEmitter<VideoComment>()
26 @Output() cancel = new EventEmitter()
27
28 @ViewChild('visitorModal', { static: true }) visitorModal: NgbModal
29 @ViewChild('emojiModal', { static: true }) emojiModal: NgbModal
30 @ViewChild('textarea', { static: true }) textareaElement: ElementRef
31
32 addingComment = false
33 addingCommentButtonValue: string
34
35 constructor (
36 protected formValidatorService: FormValidatorService,
37 private videoCommentValidatorsService: VideoCommentValidatorsService,
38 private notifier: Notifier,
39 private videoCommentService: VideoCommentService,
40 private modalService: NgbModal,
41 private router: Router,
42 private i18n: I18n
43 ) {
44 super()
45 }
46
47 get emojiMarkupList () {
48 const emojiMarkup = require('markdown-it-emoji/lib/data/light.json')
49
50 return emojiMarkup
51 }
52
53 ngOnInit () {
54 this.buildForm({
55 text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT
56 })
57
58 if (this.user) {
59 if (!this.parentComment) {
60 this.addingCommentButtonValue = this.i18n('Comment')
61 } else {
62 this.addingCommentButtonValue = this.i18n('Reply')
63 }
64
65 this.initTextValue()
66 }
67 }
68
69 ngOnChanges (changes: SimpleChanges) {
70 if (changes.textValue && changes.textValue.currentValue && changes.textValue.currentValue !== changes.textValue.previousValue) {
71 this.patchTextValue(changes.textValue.currentValue, true)
72 }
73 }
74
75 onValidKey () {
76 this.check()
77 if (!this.form.valid) return
78
79 this.formValidated()
80 }
81
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()
87
88 this.modalService.open(this.visitorModal)
89 }
90 }
91
92 openEmojiModal (event: any) {
93 event.preventDefault()
94 this.modalService.open(this.emojiModal, { backdrop: true })
95 }
96
97 hideModals () {
98 this.modalService.dismissAll()
99 }
100
101 formValidated () {
102 // If we validate very quickly the comment form, we might comment twice
103 if (this.addingComment) return
104
105 this.addingComment = true
106
107 const commentCreate: VideoCommentCreate = this.form.value
108 let obs: Observable<VideoComment>
109
110 if (this.parentComment) {
111 obs = this.addCommentReply(commentCreate)
112 } else {
113 obs = this.addCommentThread(commentCreate)
114 }
115
116 obs.subscribe(
117 comment => {
118 this.addingComment = false
119 this.commentCreated.emit(comment)
120 this.form.reset()
121 },
122
123 err => {
124 this.addingComment = false
125
126 this.notifier.error(err.text)
127 }
128 )
129 }
130
131 isAddButtonDisplayed () {
132 return this.form.value['text']
133 }
134
135 getUri () {
136 return window.location.href
137 }
138
139 getAvatarUrl () {
140 if (this.user) return this.user.accountAvatarUrl
141 return window.location.origin + '/client/assets/images/default-avatar.png'
142 }
143
144 gotoLogin () {
145 this.hideModals()
146 this.router.navigate([ '/login' ])
147 }
148
149 cancelCommentReply () {
150 this.cancel.emit(null)
151 this.form.value['text'] = this.textareaElement.nativeElement.value = ''
152 }
153
154 private addCommentReply (commentCreate: VideoCommentCreate) {
155 return this.videoCommentService
156 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
157 }
158
159 private addCommentThread (commentCreate: VideoCommentCreate) {
160 return this.videoCommentService
161 .addCommentThread(this.video.id, commentCreate)
162 }
163
164 private initTextValue () {
165 if (this.textValue) {
166 this.patchTextValue(this.textValue, this.focusOnInit)
167 return
168 }
169
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)
174
175 const mentionsSet = new Set(mentions)
176 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
177
178 this.patchTextValue(mentionsText, this.focusOnInit)
179 }
180 }
181
182 private patchTextValue (text: string, focus: boolean) {
183 setTimeout(() => {
184 if (focus) {
185 this.textareaElement.nativeElement.focus()
186 }
187
188 this.textareaElement.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'nearest' })
189 })
190
191 this.form.patchValue({ text })
192 }
193 }