]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-watch/comment/video-comment-add.component.ts
Small refactor comments
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-watch / comment / video-comment-add.component.ts
CommitLineData
67ed6552 1import { Observable } from 'rxjs'
f63c03fb 2import { Component, ElementRef, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core'
660d11e9 3import { Router } from '@angular/router'
67ed6552
C
4import { Notifier, User } from '@app/core'
5import { FormReactive, FormValidatorService, VideoCommentValidatorsService } from '@app/shared/shared-forms'
6import { Video } from '@app/shared/shared-main'
cfde28ba 7import { VideoComment, VideoCommentService } from '@app/shared/shared-video-comment'
67ed6552
C
8import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
9import { VideoCommentCreate } from '@shared/models'
cb54210c 10import { I18n } from '@ngx-translate/i18n-polyfill'
4635f59d
C
11
12@Component({
13 selector: 'my-video-comment-add',
14 templateUrl: './video-comment-add.component.html',
15 styleUrls: ['./video-comment-add.component.scss']
16})
f63c03fb 17export class VideoCommentAddComponent extends FormReactive implements OnChanges, OnInit {
cf117aaa 18 @Input() user: User
4635f59d 19 @Input() video: Video
fdd12965 20 @Input() parentComment?: VideoComment
21 @Input() parentComments?: VideoComment[]
eacf925e 22 @Input() focusOnInit = false
f63c03fb 23 @Input() textValue?: string
4635f59d 24
be27ef3b 25 @Output() commentCreated = new EventEmitter<VideoComment>()
88adad2d 26 @Output() cancel = new EventEmitter()
4635f59d 27
f36da21e 28 @ViewChild('visitorModal', { static: true }) visitorModal: NgbModal
ee3bd9db 29 @ViewChild('emojiModal', { static: true }) emojiModal: NgbModal
f36da21e 30 @ViewChild('textarea', { static: true }) textareaElement: ElementRef
eacf925e 31
2fbe7f19 32 addingComment = false
cb54210c 33 addingCommentButtonValue: string
ff336427 34
4635f59d 35 constructor (
d18d6478 36 protected formValidatorService: FormValidatorService,
e309822b 37 private videoCommentValidatorsService: VideoCommentValidatorsService,
f8b2c1b4 38 private notifier: Notifier,
b1d40cff 39 private videoCommentService: VideoCommentService,
660d11e9 40 private modalService: NgbModal,
cb54210c 41 private router: Router,
42 private i18n: I18n
4635f59d
C
43 ) {
44 super()
45 }
46
ee3bd9db 47 get emojiMarkupList () {
48 const emojiMarkup = require('markdown-it-emoji/lib/data/light.json')
49
50 return emojiMarkup
51 }
52
4635f59d 53 ngOnInit () {
d18d6478 54 this.buildForm({
e309822b 55 text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT
d18d6478 56 })
eacf925e 57
660d11e9 58 if (this.user) {
fdd12965 59 if (!this.parentComment) {
cb54210c 60 this.addingCommentButtonValue = this.i18n('Comment')
61 } else {
62 this.addingCommentButtonValue = this.i18n('Reply')
63 }
64
fdd12965 65 this.initTextValue()
d7e70384 66 }
4635f59d
C
67 }
68
f63c03fb 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
1263fc4e 75 onValidKey () {
3866f1a0 76 this.check()
1263fc4e
C
77 if (!this.form.valid) return
78
79 this.formValidated()
80 }
81
244b4ae3 82 openVisitorModal (event: any) {
660d11e9
RK
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
ee3bd9db 92 openEmojiModal (event: any) {
93 event.preventDefault()
94 this.modalService.open(this.emojiModal, { backdrop: true })
95 }
96
97 hideModals () {
660d11e9
RK
98 this.modalService.dismissAll()
99 }
100
4635f59d 101 formValidated () {
ff336427
C
102 // If we validate very quickly the comment form, we might comment twice
103 if (this.addingComment) return
104
105 this.addingComment = true
106
4635f59d 107 const commentCreate: VideoCommentCreate = this.form.value
be27ef3b 108 let obs: Observable<VideoComment>
4635f59d
C
109
110 if (this.parentComment) {
111 obs = this.addCommentReply(commentCreate)
112 } else {
113 obs = this.addCommentThread(commentCreate)
114 }
115
116 obs.subscribe(
117 comment => {
ff336427 118 this.addingComment = false
4635f59d
C
119 this.commentCreated.emit(comment)
120 this.form.reset()
121 },
122
ff336427
C
123 err => {
124 this.addingComment = false
125
f8b2c1b4 126 this.notifier.error(err.text)
ff336427 127 }
4635f59d 128 )
d50acfab 129 }
4635f59d
C
130
131 isAddButtonDisplayed () {
132 return this.form.value['text']
133 }
134
3ddb1ec5 135 getUri () {
6d5973fa
RK
136 return window.location.href
137 }
138
660d11e9
RK
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 () {
ee3bd9db 145 this.hideModals()
660d11e9
RK
146 this.router.navigate([ '/login' ])
147 }
148
3f9c4955
C
149 cancelCommentReply () {
150 this.cancel.emit(null)
79671021 151 this.form.value['text'] = this.textareaElement.nativeElement.value = ''
3f9c4955
C
152 }
153
4635f59d
C
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 }
f63c03fb 163
fdd12965 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
f63c03fb 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 }
4635f59d 193}