]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-watch/comment/video-comment-add.component.ts
add link to open video on origin instance
[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 4import { Notifier, User } from '@app/core'
7ed1edbb
C
5import { VIDEO_COMMENT_TEXT_VALIDATOR } from '@app/shared/form-validators/video-comment-validators'
6import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
c418d483 7import { Video, Account } from '@app/shared/shared-main'
cfde28ba 8import { VideoComment, VideoCommentService } from '@app/shared/shared-video-comment'
67ed6552
C
9import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
10import { VideoCommentCreate } from '@shared/models'
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,
f8b2c1b4 37 private notifier: Notifier,
b1d40cff 38 private videoCommentService: VideoCommentService,
660d11e9 39 private modalService: NgbModal,
93e903ac 40 private router: Router
4635f59d
C
41 ) {
42 super()
43 }
44
ee3bd9db 45 get emojiMarkupList () {
f34cc2a4
K
46 const emojiMarkupObjectList = require('markdown-it-emoji/lib/data/light.json')
47
48 // Populate emoji-markup-list from object to array to avoid keys alphabetical order
49 const emojiMarkupArrayList = []
50 for (const emojiMarkupName in emojiMarkupObjectList) {
51 if (emojiMarkupName) {
52 const emoji = emojiMarkupObjectList[emojiMarkupName]
53 emojiMarkupArrayList.push([emoji, emojiMarkupName])
54 }
55 }
ee3bd9db 56
f34cc2a4 57 return emojiMarkupArrayList
ee3bd9db 58 }
59
4635f59d 60 ngOnInit () {
d18d6478 61 this.buildForm({
7ed1edbb 62 text: VIDEO_COMMENT_TEXT_VALIDATOR
d18d6478 63 })
eacf925e 64
660d11e9 65 if (this.user) {
fdd12965 66 if (!this.parentComment) {
da188b9f 67 this.addingCommentButtonValue = $localize`Comment`
cb54210c 68 } else {
da188b9f 69 this.addingCommentButtonValue = $localize`Reply`
cb54210c 70 }
71
fdd12965 72 this.initTextValue()
d7e70384 73 }
4635f59d
C
74 }
75
f63c03fb 76 ngOnChanges (changes: SimpleChanges) {
77 if (changes.textValue && changes.textValue.currentValue && changes.textValue.currentValue !== changes.textValue.previousValue) {
78 this.patchTextValue(changes.textValue.currentValue, true)
79 }
80 }
81
1263fc4e 82 onValidKey () {
3866f1a0 83 this.check()
1263fc4e
C
84 if (!this.form.valid) return
85
86 this.formValidated()
87 }
88
244b4ae3 89 openVisitorModal (event: any) {
660d11e9
RK
90 if (this.user === null) { // we only open it for visitors
91 // fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error
92 event.srcElement.blur()
93 event.preventDefault()
94
95 this.modalService.open(this.visitorModal)
96 }
97 }
98
ee3bd9db 99 openEmojiModal (event: any) {
100 event.preventDefault()
f34cc2a4 101 this.modalService.open(this.emojiModal, { backdrop: true, size: 'lg' })
ee3bd9db 102 }
103
104 hideModals () {
660d11e9
RK
105 this.modalService.dismissAll()
106 }
107
4635f59d 108 formValidated () {
ff336427
C
109 // If we validate very quickly the comment form, we might comment twice
110 if (this.addingComment) return
111
112 this.addingComment = true
113
4635f59d 114 const commentCreate: VideoCommentCreate = this.form.value
be27ef3b 115 let obs: Observable<VideoComment>
4635f59d
C
116
117 if (this.parentComment) {
118 obs = this.addCommentReply(commentCreate)
119 } else {
120 obs = this.addCommentThread(commentCreate)
121 }
122
123 obs.subscribe(
124 comment => {
ff336427 125 this.addingComment = false
4635f59d
C
126 this.commentCreated.emit(comment)
127 this.form.reset()
128 },
129
ff336427
C
130 err => {
131 this.addingComment = false
132
f8b2c1b4 133 this.notifier.error(err.text)
ff336427 134 }
4635f59d 135 )
d50acfab 136 }
4635f59d
C
137
138 isAddButtonDisplayed () {
139 return this.form.value['text']
140 }
141
3ddb1ec5 142 getUri () {
6d5973fa
RK
143 return window.location.href
144 }
145
660d11e9
RK
146 getAvatarUrl () {
147 if (this.user) return this.user.accountAvatarUrl
c418d483 148 return Account.GET_DEFAULT_AVATAR_URL()
660d11e9
RK
149 }
150
151 gotoLogin () {
ee3bd9db 152 this.hideModals()
660d11e9
RK
153 this.router.navigate([ '/login' ])
154 }
155
3f9c4955
C
156 cancelCommentReply () {
157 this.cancel.emit(null)
79671021 158 this.form.value['text'] = this.textareaElement.nativeElement.value = ''
3f9c4955
C
159 }
160
4635f59d
C
161 private addCommentReply (commentCreate: VideoCommentCreate) {
162 return this.videoCommentService
163 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
164 }
165
166 private addCommentThread (commentCreate: VideoCommentCreate) {
167 return this.videoCommentService
168 .addCommentThread(this.video.id, commentCreate)
169 }
f63c03fb 170
fdd12965 171 private initTextValue () {
172 if (this.textValue) {
173 this.patchTextValue(this.textValue, this.focusOnInit)
174 return
175 }
176
177 if (this.parentComment) {
178 const mentions = this.parentComments
179 .filter(c => c.account && c.account.id !== this.user.account.id) // Don't add mention of ourselves
180 .map(c => '@' + c.by)
181
182 const mentionsSet = new Set(mentions)
183 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
184
185 this.patchTextValue(mentionsText, this.focusOnInit)
186 }
187 }
188
f63c03fb 189 private patchTextValue (text: string, focus: boolean) {
190 setTimeout(() => {
191 if (focus) {
192 this.textareaElement.nativeElement.focus()
193 }
194
6a9498e3 195 // Scroll to textarea
f63c03fb 196 this.textareaElement.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'nearest' })
6a9498e3
K
197
198 // Use the native textarea autosize according to the text's break lines
199 this.textareaElement.nativeElement.dispatchEvent(new Event('input'))
f63c03fb 200 })
201
202 this.form.patchValue({ text })
203 }
4635f59d 204}