PeerTube/client/src/app/+videos/+video-watch/shared/comment/video-comment-add.component.ts

227 lines
6.0 KiB
TypeScript
Raw Normal View History

2020-06-23 14:10:17 +02:00
import { Observable } from 'rxjs'
2021-06-07 17:38:31 +02:00
import { getLocaleDirection } from '@angular/common'
import {
Component,
ElementRef,
EventEmitter,
Inject,
Input,
LOCALE_ID,
OnChanges,
OnInit,
Output,
SimpleChanges,
ViewChild
} from '@angular/core'
2020-06-23 14:10:17 +02:00
import { Notifier, User } from '@app/core'
import { VIDEO_COMMENT_TEXT_VALIDATOR } from '@app/shared/form-validators/video-comment-validators'
2022-10-07 15:26:53 +02:00
import { FormReactive, FormReactiveService } from '@app/shared/shared-forms'
2021-04-15 11:38:24 +02:00
import { Video } from '@app/shared/shared-main'
2020-07-09 15:54:24 +02:00
import { VideoComment, VideoCommentService } from '@app/shared/shared-video-comment'
2020-06-23 14:10:17 +02:00
import { NgbModal } from '@ng-bootstrap/ng-bootstrap'
import { VideoCommentCreate } from '@peertube/peertube-models'
2017-12-27 16:11:53 +01:00
@Component({
selector: 'my-video-comment-add',
templateUrl: './video-comment-add.component.html',
2021-08-17 14:42:53 +02:00
styleUrls: [ './video-comment-add.component.scss' ]
2017-12-27 16:11:53 +01:00
})
export class VideoCommentAddComponent extends FormReactive implements OnChanges, OnInit {
2018-01-03 17:25:47 +01:00
@Input() user: User
2017-12-27 16:11:53 +01:00
@Input() video: Video
@Input() videoPassword: string
2020-08-12 12:43:26 +02:00
@Input() parentComment?: VideoComment
@Input() parentComments?: VideoComment[]
2017-12-29 09:18:00 +01:00
@Input() focusOnInit = false
@Input() textValue?: string
2017-12-27 16:11:53 +01:00
2020-02-10 14:25:38 +01:00
@Output() commentCreated = new EventEmitter<VideoComment>()
@Output() cancel = new EventEmitter()
2017-12-27 16:11:53 +01:00
2019-07-24 16:05:59 +02:00
@ViewChild('visitorModal', { static: true }) visitorModal: NgbModal
2020-08-09 16:20:31 +02:00
@ViewChild('emojiModal', { static: true }) emojiModal: NgbModal
2019-07-24 16:05:59 +02:00
@ViewChild('textarea', { static: true }) textareaElement: ElementRef
2017-12-29 09:18:00 +01:00
2018-11-15 09:24:56 +01:00
addingComment = false
addingCommentButtonValue: string
2018-06-18 10:29:17 +02:00
2022-06-27 11:28:22 +02:00
private emojiMarkupList: { emoji: string, name: string }[]
2017-12-27 16:11:53 +01:00
constructor (
2022-10-07 15:26:53 +02:00
protected formReactiveService: FormReactiveService,
private notifier: Notifier,
2018-06-04 16:21:17 +02:00
private videoCommentService: VideoCommentService,
private modalService: NgbModal,
2021-06-07 17:38:31 +02:00
@Inject(LOCALE_ID) private localeId: string
2017-12-27 16:11:53 +01:00
) {
super()
}
ngOnInit () {
2018-06-05 10:58:45 +02:00
this.buildForm({
text: VIDEO_COMMENT_TEXT_VALIDATOR
2018-06-05 10:58:45 +02:00
})
2017-12-29 09:18:00 +01:00
if (this.user) {
2020-08-12 12:43:26 +02:00
if (!this.parentComment) {
2020-08-14 14:34:47 +02:00
this.addingCommentButtonValue = $localize`Comment`
} else {
2020-08-14 14:34:47 +02:00
this.addingCommentButtonValue = $localize`Reply`
}
2020-08-12 12:43:26 +02:00
this.initTextValue()
2018-01-05 11:19:25 +01:00
}
2017-12-27 16:11:53 +01:00
}
ngOnChanges (changes: SimpleChanges) {
// Not initialized yet
if (!this.form) return
2021-08-17 14:42:53 +02:00
if (changes.textValue?.currentValue && changes.textValue.currentValue !== changes.textValue.previousValue) {
this.patchTextValue(changes.textValue.currentValue, true)
}
}
2022-06-27 11:28:22 +02:00
getEmojiMarkupList () {
if (this.emojiMarkupList) return this.emojiMarkupList
2024-02-22 10:12:04 +01:00
const emojiMarkupObjectList = require('markdown-it-emoji/lib/data/light.mjs').default
2022-06-27 11:28:22 +02:00
this.emojiMarkupList = []
for (const name of Object.keys(emojiMarkupObjectList)) {
const emoji = emojiMarkupObjectList[name]
this.emojiMarkupList.push({ emoji, name })
}
return this.emojiMarkupList
}
2018-02-19 10:38:24 +01:00
onValidKey () {
2021-12-29 15:33:24 +01:00
this.forceCheck()
2018-02-19 10:38:24 +01:00
if (!this.form.valid) return
this.formValidated()
}
openVisitorModal (event: any) {
if (this.user === null) { // we only open it for visitors
// fixing ng-bootstrap ModalService and the "Expression Changed After It Has Been Checked" Error
event.srcElement.blur()
event.preventDefault()
this.modalService.open(this.visitorModal)
}
}
2020-08-09 16:20:31 +02:00
openEmojiModal (event: any) {
event.preventDefault()
this.modalService.open(this.emojiModal, { backdrop: true, size: 'lg' })
2020-08-09 16:20:31 +02:00
}
hideModals () {
this.modalService.dismissAll()
}
2017-12-27 16:11:53 +01:00
formValidated () {
2018-06-18 10:29:17 +02:00
// If we validate very quickly the comment form, we might comment twice
if (this.addingComment) return
this.addingComment = true
2017-12-27 16:11:53 +01:00
const commentCreate: VideoCommentCreate = this.form.value
2020-02-10 14:25:38 +01:00
let obs: Observable<VideoComment>
2017-12-27 16:11:53 +01:00
if (this.parentComment) {
obs = this.addCommentReply(commentCreate)
} else {
obs = this.addCommentThread(commentCreate)
}
2021-08-17 11:27:47 +02:00
obs.subscribe({
next: comment => {
2018-06-18 10:29:17 +02:00
this.addingComment = false
2017-12-27 16:11:53 +01:00
this.commentCreated.emit(comment)
this.form.reset()
},
2021-08-17 11:27:47 +02:00
error: err => {
2018-06-18 10:29:17 +02:00
this.addingComment = false
2022-09-23 14:41:14 +02:00
this.notifier.error(err.message)
2018-06-18 10:29:17 +02:00
}
2021-08-17 11:27:47 +02:00
})
2017-12-27 20:03:37 +01:00
}
2017-12-27 16:11:53 +01:00
isAddButtonDisplayed () {
return this.form.value['text']
}
getUri () {
return window.location.href
}
2019-12-17 11:20:24 +01:00
cancelCommentReply () {
this.cancel.emit(null)
2020-03-15 16:05:37 +01:00
this.form.value['text'] = this.textareaElement.nativeElement.value = ''
2019-12-17 11:20:24 +01:00
}
2021-06-07 17:38:31 +02:00
isRTL () {
return getLocaleDirection(this.localeId) === 'rtl'
}
2022-06-27 11:22:21 +02:00
getAvatarActorType () {
if (this.user) return 'account'
return 'unlogged'
}
2017-12-27 16:11:53 +01:00
private addCommentReply (commentCreate: VideoCommentCreate) {
return this.videoCommentService
.addCommentReply({
videoId: this.video.uuid,
inReplyToCommentId: this.parentComment.id,
comment: commentCreate,
videoPassword: this.videoPassword
})
2017-12-27 16:11:53 +01:00
}
private addCommentThread (commentCreate: VideoCommentCreate) {
return this.videoCommentService
.addCommentThread(this.video.uuid, commentCreate, this.videoPassword)
2017-12-27 16:11:53 +01:00
}
2020-08-12 12:43:26 +02:00
private initTextValue () {
if (this.textValue) {
this.patchTextValue(this.textValue, this.focusOnInit)
return
}
if (this.parentComment) {
const mentions = this.parentComments
.filter(c => c.account && c.account.id !== this.user.account.id) // Don't add mention of ourselves
.map(c => '@' + c.by)
const mentionsSet = new Set(mentions)
const mentionsText = Array.from(mentionsSet).join(' ') + ' '
this.patchTextValue(mentionsText, this.focusOnInit)
}
}
private patchTextValue (text: string, focus: boolean) {
setTimeout(() => {
if (focus) {
this.textareaElement.nativeElement.focus()
}
// Scroll to textarea
this.textareaElement.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'nearest' })
// Use the native textarea autosize according to the text's break lines
this.textareaElement.nativeElement.dispatchEvent(new Event('input'))
})
this.form.patchValue({ text })
}
2017-12-27 16:11:53 +01:00
}