]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - client/src/app/+videos/+video-watch/comment/video-comment-add.component.ts
ACCOUNT instead of PEERTUBE ACCOUNT
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-watch / comment / video-comment-add.component.ts
CommitLineData
67ed6552 1import { Observable } from 'rxjs'
27bc9586
C
2import { getLocaleDirection } from '@angular/common'
3import {
4 Component,
5 ElementRef,
6 EventEmitter,
7 Inject,
8 Input,
9 LOCALE_ID,
10 OnChanges,
11 OnInit,
12 Output,
13 SimpleChanges,
14 ViewChild
15} from '@angular/core'
660d11e9 16import { Router } from '@angular/router'
67ed6552 17import { Notifier, User } from '@app/core'
7ed1edbb
C
18import { VIDEO_COMMENT_TEXT_VALIDATOR } from '@app/shared/form-validators/video-comment-validators'
19import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
fe88ca69 20import { Video } from '@app/shared/shared-main'
cfde28ba 21import { VideoComment, VideoCommentService } from '@app/shared/shared-video-comment'
67ed6552
C
22import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
23import { VideoCommentCreate } from '@shared/models'
4635f59d
C
24
25@Component({
26 selector: 'my-video-comment-add',
27 templateUrl: './video-comment-add.component.html',
28 styleUrls: ['./video-comment-add.component.scss']
29})
f63c03fb 30export class VideoCommentAddComponent extends FormReactive implements OnChanges, OnInit {
cf117aaa 31 @Input() user: User
4635f59d 32 @Input() video: Video
fdd12965 33 @Input() parentComment?: VideoComment
34 @Input() parentComments?: VideoComment[]
eacf925e 35 @Input() focusOnInit = false
f63c03fb 36 @Input() textValue?: string
4635f59d 37
be27ef3b 38 @Output() commentCreated = new EventEmitter<VideoComment>()
88adad2d 39 @Output() cancel = new EventEmitter()
4635f59d 40
f36da21e 41 @ViewChild('visitorModal', { static: true }) visitorModal: NgbModal
ee3bd9db 42 @ViewChild('emojiModal', { static: true }) emojiModal: NgbModal
f36da21e 43 @ViewChild('textarea', { static: true }) textareaElement: ElementRef
eacf925e 44
2fbe7f19 45 addingComment = false
cb54210c 46 addingCommentButtonValue: string
ff336427 47
4635f59d 48 constructor (
d18d6478 49 protected formValidatorService: FormValidatorService,
f8b2c1b4 50 private notifier: Notifier,
b1d40cff 51 private videoCommentService: VideoCommentService,
660d11e9 52 private modalService: NgbModal,
27bc9586
C
53 private router: Router,
54 @Inject(LOCALE_ID) private localeId: string
4635f59d
C
55 ) {
56 super()
57 }
58
ee3bd9db 59 get emojiMarkupList () {
f34cc2a4
K
60 const emojiMarkupObjectList = require('markdown-it-emoji/lib/data/light.json')
61
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])
68 }
69 }
ee3bd9db 70
f34cc2a4 71 return emojiMarkupArrayList
ee3bd9db 72 }
73
4635f59d 74 ngOnInit () {
d18d6478 75 this.buildForm({
7ed1edbb 76 text: VIDEO_COMMENT_TEXT_VALIDATOR
d18d6478 77 })
eacf925e 78
660d11e9 79 if (this.user) {
fdd12965 80 if (!this.parentComment) {
da188b9f 81 this.addingCommentButtonValue = $localize`Comment`
cb54210c 82 } else {
da188b9f 83 this.addingCommentButtonValue = $localize`Reply`
cb54210c 84 }
85
fdd12965 86 this.initTextValue()
d7e70384 87 }
4635f59d
C
88 }
89
f63c03fb 90 ngOnChanges (changes: SimpleChanges) {
91 if (changes.textValue && changes.textValue.currentValue && changes.textValue.currentValue !== changes.textValue.previousValue) {
92 this.patchTextValue(changes.textValue.currentValue, true)
93 }
94 }
95
1263fc4e 96 onValidKey () {
3866f1a0 97 this.check()
1263fc4e
C
98 if (!this.form.valid) return
99
100 this.formValidated()
101 }
102
244b4ae3 103 openVisitorModal (event: any) {
660d11e9
RK
104 if (this.user === null) { // we only open it for visitors
105 // fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error
106 event.srcElement.blur()
107 event.preventDefault()
108
109 this.modalService.open(this.visitorModal)
110 }
111 }
112
ee3bd9db 113 openEmojiModal (event: any) {
114 event.preventDefault()
f34cc2a4 115 this.modalService.open(this.emojiModal, { backdrop: true, size: 'lg' })
ee3bd9db 116 }
117
118 hideModals () {
660d11e9
RK
119 this.modalService.dismissAll()
120 }
121
4635f59d 122 formValidated () {
ff336427
C
123 // If we validate very quickly the comment form, we might comment twice
124 if (this.addingComment) return
125
126 this.addingComment = true
127
4635f59d 128 const commentCreate: VideoCommentCreate = this.form.value
be27ef3b 129 let obs: Observable<VideoComment>
4635f59d
C
130
131 if (this.parentComment) {
132 obs = this.addCommentReply(commentCreate)
133 } else {
134 obs = this.addCommentThread(commentCreate)
135 }
136
137 obs.subscribe(
138 comment => {
ff336427 139 this.addingComment = false
4635f59d
C
140 this.commentCreated.emit(comment)
141 this.form.reset()
142 },
143
ff336427
C
144 err => {
145 this.addingComment = false
146
f8b2c1b4 147 this.notifier.error(err.text)
ff336427 148 }
4635f59d 149 )
d50acfab 150 }
4635f59d
C
151
152 isAddButtonDisplayed () {
153 return this.form.value['text']
154 }
155
3ddb1ec5 156 getUri () {
6d5973fa
RK
157 return window.location.href
158 }
159
660d11e9 160 gotoLogin () {
ee3bd9db 161 this.hideModals()
660d11e9
RK
162 this.router.navigate([ '/login' ])
163 }
164
3f9c4955
C
165 cancelCommentReply () {
166 this.cancel.emit(null)
79671021 167 this.form.value['text'] = this.textareaElement.nativeElement.value = ''
3f9c4955
C
168 }
169
27bc9586
C
170 isRTL () {
171 return getLocaleDirection(this.localeId) === 'rtl'
172 }
173
4635f59d
C
174 private addCommentReply (commentCreate: VideoCommentCreate) {
175 return this.videoCommentService
176 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
177 }
178
179 private addCommentThread (commentCreate: VideoCommentCreate) {
180 return this.videoCommentService
181 .addCommentThread(this.video.id, commentCreate)
182 }
f63c03fb 183
fdd12965 184 private initTextValue () {
185 if (this.textValue) {
186 this.patchTextValue(this.textValue, this.focusOnInit)
187 return
188 }
189
190 if (this.parentComment) {
191 const mentions = this.parentComments
192 .filter(c => c.account && c.account.id !== this.user.account.id) // Don't add mention of ourselves
193 .map(c => '@' + c.by)
194
195 const mentionsSet = new Set(mentions)
196 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
197
198 this.patchTextValue(mentionsText, this.focusOnInit)
199 }
200 }
201
f63c03fb 202 private patchTextValue (text: string, focus: boolean) {
203 setTimeout(() => {
204 if (focus) {
205 this.textareaElement.nativeElement.focus()
206 }
207
6a9498e3 208 // Scroll to textarea
f63c03fb 209 this.textareaElement.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'nearest' })
6a9498e3
K
210
211 // Use the native textarea autosize according to the text's break lines
212 this.textareaElement.nativeElement.dispatchEvent(new Event('input'))
f63c03fb 213 })
214
215 this.form.patchValue({ text })
216 }
4635f59d 217}