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