import { Observable } from 'rxjs'
-import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
+import { Component, ElementRef, EventEmitter, Input, OnChanges, OnInit, Output, SimpleChanges, ViewChild } from '@angular/core'
import { Router } from '@angular/router'
import { Notifier, User } from '@app/core'
import { FormReactive, FormValidatorService, VideoCommentValidatorsService } from '@app/shared/shared-forms'
templateUrl: './video-comment-add.component.html',
styleUrls: ['./video-comment-add.component.scss']
})
-export class VideoCommentAddComponent extends FormReactive implements OnInit {
+export class VideoCommentAddComponent extends FormReactive implements OnChanges, OnInit {
@Input() user: User
@Input() video: Video
@Input() parentComment: VideoComment
@Input() parentComments: VideoComment[]
@Input() focusOnInit = false
+ @Input() textValue?: string
@Output() commentCreated = new EventEmitter<VideoComment>()
@Output() cancel = new EventEmitter()
})
if (this.user) {
- if (this.focusOnInit === true) {
- this.textareaElement.nativeElement.focus()
+ if (this.textValue) {
+ this.patchTextValue(this.textValue, this.focusOnInit)
+ return
}
if (this.parentComment) {
const mentionsSet = new Set(mentions)
const mentionsText = Array.from(mentionsSet).join(' ') + ' '
- this.form.patchValue({ text: mentionsText })
+ this.patchTextValue(mentionsText, this.focusOnInit)
}
}
}
+ ngOnChanges (changes: SimpleChanges) {
+ if (changes.textValue && changes.textValue.currentValue && changes.textValue.currentValue !== changes.textValue.previousValue) {
+ this.patchTextValue(changes.textValue.currentValue, true)
+ }
+ }
+
onValidKey () {
this.check()
if (!this.form.valid) return
return this.videoCommentService
.addCommentThread(this.video.id, commentCreate)
}
+
+ private patchTextValue (text: string, focus: boolean) {
+ setTimeout(() => {
+ if (focus) {
+ this.textareaElement.nativeElement.focus()
+ }
+
+ this.textareaElement.nativeElement.scrollIntoView({ behavior: 'smooth', block: 'center', inline: 'nearest' })
+ })
+
+ this.form.patchValue({ text })
+ }
}