]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - client/src/app/+videos/+video-watch/comment/video-comment-add.component.ts
Add emoji list to markdown infos
[github/Chocobozzz/PeerTube.git] / client / src / app / +videos / +video-watch / comment / video-comment-add.component.ts
1 import { Observable } from 'rxjs'
2 import { Component, ElementRef, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core'
3 import { Router } from '@angular/router'
4 import { Notifier, User } from '@app/core'
5 import { FormReactive, FormValidatorService, VideoCommentValidatorsService } from '@app/shared/shared-forms'
6 import { Video } from '@app/shared/shared-main'
7 import { VideoComment, VideoCommentService } from '@app/shared/shared-video-comment'
8 import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
9 import { VideoCommentCreate } from '@shared/models'
10 import { I18n } from '@ngx-translate/i18n-polyfill'
11
12 @Component({
13 selector: 'my-video-comment-add',
14 templateUrl: './video-comment-add.component.html',
15 styleUrls: ['./video-comment-add.component.scss']
16 })
17 export class VideoCommentAddComponent extends FormReactive implements OnChanges, OnInit {
18 @Input() user: User
19 @Input() video: Video
20 @Input() parentComment: VideoComment
21 @Input() parentComments: VideoComment[]
22 @Input() focusOnInit = false
23 @Input() textValue?: string
24 @Input() commentThread?: boolean
25
26 @Output() commentCreated = new EventEmitter<VideoComment>()
27 @Output() cancel = new EventEmitter()
28
29 @ViewChild('visitorModal', { static: true }) visitorModal: NgbModal
30 @ViewChild('emojiModal', { static: true }) emojiModal: NgbModal
31 @ViewChild('textarea', { static: true }) textareaElement: ElementRef
32
33 addingComment = false
34 addingCommentButtonValue: string
35
36 constructor (
37 protected formValidatorService: FormValidatorService,
38 private videoCommentValidatorsService: VideoCommentValidatorsService,
39 private notifier: Notifier,
40 private videoCommentService: VideoCommentService,
41 private modalService: NgbModal,
42 private router: Router,
43 private i18n: I18n
44 ) {
45 super()
46 }
47
48 get emojiMarkupList () {
49 const emojiMarkup = require('markdown-it-emoji/lib/data/light.json')
50
51 return emojiMarkup
52 }
53
54 ngOnInit () {
55 this.buildForm({
56 text: this.videoCommentValidatorsService.VIDEO_COMMENT_TEXT
57 })
58
59 if (this.user) {
60 if (this.commentThread) {
61 this.addingCommentButtonValue = this.i18n('Comment')
62 } else {
63 this.addingCommentButtonValue = this.i18n('Reply')
64 }
65
66 if (this.textValue) {
67 this.patchTextValue(this.textValue, this.focusOnInit)
68 return
69 }
70
71 if (this.parentComment) {
72 const mentions = this.parentComments
73 .filter(c => c.account && c.account.id !== this.user.account.id) // Don't add mention of ourselves
74 .map(c => '@' + c.by)
75
76 const mentionsSet = new Set(mentions)
77 const mentionsText = Array.from(mentionsSet).join(' ') + ' '
78
79 this.patchTextValue(mentionsText, this.focusOnInit)
80 }
81 }
82 }
83
84 ngOnChanges (changes: SimpleChanges) {
85 if (changes.textValue && changes.textValue.currentValue && changes.textValue.currentValue !== changes.textValue.previousValue) {
86 this.patchTextValue(changes.textValue.currentValue, true)
87 }
88 }
89
90 onValidKey () {
91 this.check()
92 if (!this.form.valid) return
93
94 this.formValidated()
95 }
96
97 openVisitorModal (event: any) {
98 if (this.user === null) { // we only open it for visitors
99 // fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error
100 event.srcElement.blur()
101 event.preventDefault()
102
103 this.modalService.open(this.visitorModal)
104 }
105 }
106
107 openEmojiModal (event: any) {
108 event.preventDefault()
109 this.modalService.open(this.emojiModal, { backdrop: true })
110 }
111
112 hideModals () {
113 this.modalService.dismissAll()
114 }
115
116 formValidated () {
117 // If we validate very quickly the comment form, we might comment twice
118 if (this.addingComment) return
119
120 this.addingComment = true
121
122 const commentCreate: VideoCommentCreate = this.form.value
123 let obs: Observable<VideoComment>
124
125 if (this.parentComment) {
126 obs = this.addCommentReply(commentCreate)
127 } else {
128 obs = this.addCommentThread(commentCreate)
129 }
130
131 obs.subscribe(
132 comment => {
133 this.addingComment = false
134 this.commentCreated.emit(comment)
135 this.form.reset()
136 },
137
138 err => {
139 this.addingComment = false
140
141 this.notifier.error(err.text)
142 }
143 )
144 }
145
146 isAddButtonDisplayed () {
147 return this.form.value['text']
148 }
149
150 getUri () {
151 return window.location.href
152 }
153
154 getAvatarUrl () {
155 if (this.user) return this.user.accountAvatarUrl
156 return window.location.origin + '/client/assets/images/default-avatar.png'
157 }
158
159 gotoLogin () {
160 this.hideModals()
161 this.router.navigate([ '/login' ])
162 }
163
164 cancelCommentReply () {
165 this.cancel.emit(null)
166 this.form.value['text'] = this.textareaElement.nativeElement.value = ''
167 }
168
169 private addCommentReply (commentCreate: VideoCommentCreate) {
170 return this.videoCommentService
171 .addCommentReply(this.video.id, this.parentComment.id, commentCreate)
172 }
173
174 private addCommentThread (commentCreate: VideoCommentCreate) {
175 return this.videoCommentService
176 .addCommentThread(this.video.id, commentCreate)
177 }
178
179 private patchTextValue (text: string, focus: boolean) {
180 setTimeout(() => {
181 if (focus) {
182 this.textareaElement.nativeElement.focus()
183 }
184
185 this.textareaElement.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'nearest' })
186 })
187
188 this.form.patchValue({ text })
189 }
190 }