]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-watch/shared/comment/video-comment-add.component.ts
Refactor form reactive
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-watch / shared / comment / video-comment-add.component.ts
1 import { Observable } from 'rxjs'
2 import { getLocaleDirection } from '@angular/common'
3 import {
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'
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, FormReactiveService } 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'
24
25 @Component({
26 selector: 'my-video-comment-add',
27 templateUrl: './video-comment-add.component.html',
28 styleUrls: [ './video-comment-add.component.scss' ]
29 })
30 export class VideoCommentAddComponent extends FormReactive implements OnChanges, OnInit {
31 @Input() user: User
32 @Input() video: Video
33 @Input() parentComment?: VideoComment
34 @Input() parentComments?: VideoComment[]
35 @Input() focusOnInit = false
36 @Input() textValue?: string
37
38 @Output() commentCreated = new EventEmitter<VideoComment>()
39 @Output() cancel = new EventEmitter()
40
41 @ViewChild('visitorModal', { static: true }) visitorModal: NgbModal
42 @ViewChild('emojiModal', { static: true }) emojiModal: NgbModal
43 @ViewChild('textarea', { static: true }) textareaElement: ElementRef
44
45 addingComment = false
46 addingCommentButtonValue: string
47
48 private emojiMarkupList: { emoji: string, name: string }[]
49
50 constructor (
51 protected formReactiveService: FormReactiveService,
52 private notifier: Notifier,
53 private videoCommentService: VideoCommentService,
54 private modalService: NgbModal,
55 private router: Router,
56 @Inject(LOCALE_ID) private localeId: string
57 ) {
58 super()
59 }
60
61 ngOnInit () {
62 this.buildForm({
63 text: VIDEO_COMMENT_TEXT_VALIDATOR
64 })
65
66 if (this.user) {
67 if (!this.parentComment) {
68 this.addingCommentButtonValue = $localize`Comment`
69 } else {
70 this.addingCommentButtonValue = $localize`Reply`
71 }
72
73 this.initTextValue()
74 }
75 }
76
77 ngOnChanges (changes: SimpleChanges) {
78 // Not initialized yet
79 if (!this.form) return
80
81 if (changes.textValue?.currentValue && changes.textValue.currentValue !== changes.textValue.previousValue) {
82 this.patchTextValue(changes.textValue.currentValue, true)
83 }
84 }
85
86 getEmojiMarkupList () {
87 if (this.emojiMarkupList) return this.emojiMarkupList
88
89 const emojiMarkupObjectList = require('markdown-it-emoji/lib/data/light.json')
90
91 this.emojiMarkupList = []
92 for (const name of Object.keys(emojiMarkupObjectList)) {
93 const emoji = emojiMarkupObjectList[name]
94 this.emojiMarkupList.push({ emoji, name })
95 }
96
97 return this.emojiMarkupList
98 }
99
100 onValidKey () {
101 this.forceCheck()
102 if (!this.form.valid) return
103
104 this.formValidated()
105 }
106
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()
112
113 this.modalService.open(this.visitorModal)
114 }
115 }
116
117 openEmojiModal (event: any) {
118 event.preventDefault()
119 this.modalService.open(this.emojiModal, { backdrop: true, size: 'lg' })
120 }
121
122 hideModals () {
123 this.modalService.dismissAll()
124 }
125
126 formValidated () {
127 // If we validate very quickly the comment form, we might comment twice
128 if (this.addingComment) return
129
130 this.addingComment = true
131
132 const commentCreate: VideoCommentCreate = this.form.value
133 let obs: Observable<VideoComment>
134
135 if (this.parentComment) {
136 obs = this.addCommentReply(commentCreate)
137 } else {
138 obs = this.addCommentThread(commentCreate)
139 }
140
141 obs.subscribe({
142 next: comment => {
143 this.addingComment = false
144 this.commentCreated.emit(comment)
145 this.form.reset()
146 },
147
148 error: err => {
149 this.addingComment = false
150
151 this.notifier.error(err.message)
152 }
153 })
154 }
155
156 isAddButtonDisplayed () {
157 return this.form.value['text']
158 }
159
160 getUri () {
161 return window.location.href
162 }
163
164 gotoLogin () {
165 this.hideModals()
166 this.router.navigate([ '/login' ])
167 }
168
169 cancelCommentReply () {
170 this.cancel.emit(null)
171 this.form.value['text'] = this.textareaElement.nativeElement.value = ''
172 }
173
174 isRTL () {
175 return getLocaleDirection(this.localeId) === 'rtl'
176 }
177
178 getAvatarActorType () {
179 if (this.user) return 'account'
180
181 return 'unlogged'
182 }
183
184 private addCommentReply (commentCreate: VideoCommentCreate) {
185 return this.videoCommentService
186 .addCommentReply(this.video.uuid, this.parentComment.id, commentCreate)
187 }
188
189 private addCommentThread (commentCreate: VideoCommentCreate) {
190 return this.videoCommentService
191 .addCommentThread(this.video.uuid, commentCreate)
192 }
193
194 private initTextValue () {
195 if (this.textValue) {
196 this.patchTextValue(this.textValue, this.focusOnInit)
197 return
198 }
199
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)
204
205 const mentionsSet = new Set(mentions)
206 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
207
208 this.patchTextValue(mentionsText, this.focusOnInit)
209 }
210 }
211
212 private patchTextValue (text: string, focus: boolean) {
213 setTimeout(() => {
214 if (focus) {
215 this.textareaElement.nativeElement.focus()
216 }
217
218 // Scroll to textarea
219 this.textareaElement.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'nearest' })
220
221 // Use the native textarea autosize according to the text's break lines
222 this.textareaElement.nativeElement.dispatchEvent(new Event('input'))
223 })
224
225 this.form.patchValue({ text })
226 }
227 }