]>
Commit | Line | Data |
---|---|---|
67ed6552 | 1 | import { Observable } from 'rxjs' |
f63c03fb | 2 | import { Component, ElementRef, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core' |
660d11e9 | 3 | import { Router } from '@angular/router' |
67ed6552 | 4 | import { Notifier, User } from '@app/core' |
7ed1edbb C |
5 | import { VIDEO_COMMENT_TEXT_VALIDATOR } from '@app/shared/form-validators/video-comment-validators' |
6 | import { FormReactive, FormValidatorService } from '@app/shared/shared-forms' | |
fe88ca69 | 7 | import { Video } from '@app/shared/shared-main' |
cfde28ba | 8 | import { VideoComment, VideoCommentService } from '@app/shared/shared-video-comment' |
67ed6552 C |
9 | import { NgbModal } from '@ng-bootstrap/ng-bootstrap' |
10 | import { 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 | 17 | export 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 | 146 | gotoLogin () { |
ee3bd9db | 147 | this.hideModals() |
660d11e9 RK |
148 | this.router.navigate([ '/login' ]) |
149 | } | |
150 | ||
3f9c4955 C |
151 | cancelCommentReply () { |
152 | this.cancel.emit(null) | |
79671021 | 153 | this.form.value['text'] = this.textareaElement.nativeElement.value = '' |
3f9c4955 C |
154 | } |
155 | ||
4635f59d C |
156 | private addCommentReply (commentCreate: VideoCommentCreate) { |
157 | return this.videoCommentService | |
158 | .addCommentReply(this.video.id, this.parentComment.id, commentCreate) | |
159 | } | |
160 | ||
161 | private addCommentThread (commentCreate: VideoCommentCreate) { | |
162 | return this.videoCommentService | |
163 | .addCommentThread(this.video.id, commentCreate) | |
164 | } | |
f63c03fb | 165 | |
fdd12965 | 166 | private initTextValue () { |
167 | if (this.textValue) { | |
168 | this.patchTextValue(this.textValue, this.focusOnInit) | |
169 | return | |
170 | } | |
171 | ||
172 | if (this.parentComment) { | |
173 | const mentions = this.parentComments | |
174 | .filter(c => c.account && c.account.id !== this.user.account.id) // Don't add mention of ourselves | |
175 | .map(c => '@' + c.by) | |
176 | ||
177 | const mentionsSet = new Set(mentions) | |
178 | const mentionsText = Array.from(mentionsSet).join(' ') + ' ' | |
179 | ||
180 | this.patchTextValue(mentionsText, this.focusOnInit) | |
181 | } | |
182 | } | |
183 | ||
f63c03fb | 184 | private patchTextValue (text: string, focus: boolean) { |
185 | setTimeout(() => { | |
186 | if (focus) { | |
187 | this.textareaElement.nativeElement.focus() | |
188 | } | |
189 | ||
6a9498e3 | 190 | // Scroll to textarea |
f63c03fb | 191 | this.textareaElement.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'nearest' }) |
6a9498e3 K |
192 | |
193 | // Use the native textarea autosize according to the text's break lines | |
194 | this.textareaElement.nativeElement.dispatchEvent(new Event('input')) | |
f63c03fb | 195 | }) |
196 | ||
197 | this.form.patchValue({ text }) | |
198 | } | |
4635f59d | 199 | } |