aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/+videos/+video-edit/shared/video-caption-edit-modal-content/video-caption-edit-modal-content.component.ts
diff options
context:
space:
mode:
authorlutangar <johan.dufour@gmail.com>2022-08-30 17:13:26 +0200
committerChocobozzz <chocobozzz@cpy.re>2022-09-08 08:41:36 +0200
commit2873a53efd8913b6b5fbf305320f88731cd07771 (patch)
tree5063f5b38f222ce27f6923dc4bb8765f713ac4c7 /client/src/app/+videos/+video-edit/shared/video-caption-edit-modal-content/video-caption-edit-modal-content.component.ts
parent5f016383a4fabf2f296cda6d5e383719ee9d5e27 (diff)
downloadPeerTube-2873a53efd8913b6b5fbf305320f88731cd07771.tar.gz
PeerTube-2873a53efd8913b6b5fbf305320f88731cd07771.tar.zst
PeerTube-2873a53efd8913b6b5fbf305320f88731cd07771.zip
Set scroll position at top of the textarea when opening the subtitle editor.
## Description This set the position of the scrollbar at the top of the textarea when opening the __subtitle editor__. Previously the textarea scroll position was at the bottom of the textarea which doesn't make much sense when you want to edit a subtitle : you most likely want to edit the beginning of the subtitle first. This also set the caret position on the first character. ## Design decision I had to use a *component approach* instead of an `<ng-template>` for the edition modal because the `@viewChild` directive doesn't work for elements __inside__ an `<ng-template>`. I needed the `viewChild` directive to get an `ElementRef` of the `textarea`. > See the following issue and its workaround : > - https://github.com/valor-software/ngx-bootstrap/issues/3825 > - https://stackblitz.com/edit/angular-t5dfp7 > - https://medium.com/@izzatnadiri/how-to-pass-data-to-and-receive-from-ng-bootstrap-modals-916f2ad5d66e ## Related issues Closes [peertube-plugin-transcription/#39](https://gitlab.com/apps_education/peertube/plugin-transcription/-/issues/39)
Diffstat (limited to 'client/src/app/+videos/+video-edit/shared/video-caption-edit-modal-content/video-caption-edit-modal-content.component.ts')
-rw-r--r--client/src/app/+videos/+video-edit/shared/video-caption-edit-modal-content/video-caption-edit-modal-content.component.ts91
1 files changed, 91 insertions, 0 deletions
diff --git a/client/src/app/+videos/+video-edit/shared/video-caption-edit-modal-content/video-caption-edit-modal-content.component.ts b/client/src/app/+videos/+video-edit/shared/video-caption-edit-modal-content/video-caption-edit-modal-content.component.ts
new file mode 100644
index 000000000..f33353d36
--- /dev/null
+++ b/client/src/app/+videos/+video-edit/shared/video-caption-edit-modal-content/video-caption-edit-modal-content.component.ts
@@ -0,0 +1,91 @@
1import { Component, ElementRef, EventEmitter, Input, OnInit, Output, ViewChild } from '@angular/core'
2import { VIDEO_CAPTION_FILE_CONTENT_VALIDATOR } from '@app/shared/form-validators/video-captions-validators'
3import { FormReactive, FormValidatorService } from '@app/shared/shared-forms'
4import { VideoCaptionEdit, VideoCaptionService, VideoCaptionWithPathEdit } from '@app/shared/shared-main'
5import { NgbModal, NgbActiveModal } from '@ng-bootstrap/ng-bootstrap'
6import { HTMLServerConfig, VideoConstant } from '@shared/models'
7import { ServerService } from '../../../../core'
8
9/**
10 * https://github.com/valor-software/ngx-bootstrap/issues/3825
11 * https://stackblitz.com/edit/angular-t5dfp7
12 * https://medium.com/@izzatnadiri/how-to-pass-data-to-and-receive-from-ng-bootstrap-modals-916f2ad5d66e
13 */
14@Component({
15 selector: 'my-video-caption-edit-modal-content',
16 styleUrls: [ './video-caption-edit-modal-content.component.scss' ],
17 templateUrl: './video-caption-edit-modal-content.component.html'
18})
19
20export class VideoCaptionEditModalContentComponent extends FormReactive implements OnInit {
21 @Input() videoCaption: VideoCaptionWithPathEdit
22 @Input() serverConfig: HTMLServerConfig
23
24 @Output() captionEdited = new EventEmitter<VideoCaptionEdit>()
25
26 @ViewChild('textarea', { static: true }) textarea!: ElementRef
27
28 videoCaptionLanguages: VideoConstant<string>[] = []
29
30 constructor (
31 protected openedModal: NgbActiveModal,
32 protected formValidatorService: FormValidatorService,
33 private modalService: NgbModal,
34 private videoCaptionService: VideoCaptionService,
35 private serverService: ServerService
36 ) {
37 super()
38 }
39
40 ngOnInit () {
41 this.serverService.getVideoLanguages().subscribe(languages => this.videoCaptionLanguages = languages)
42
43 this.buildForm({ captionFileContent: VIDEO_CAPTION_FILE_CONTENT_VALIDATOR })
44
45 this.loadCaptionContent()
46 }
47
48 loadCaptionContent () {
49 const { captionPath } = this.videoCaption
50 if (!captionPath) return
51
52 this.videoCaptionService.getCaptionContent({ captionPath })
53 .subscribe(res => {
54 this.form.patchValue({
55 captionFileContent: res
56 })
57 this.resetTextarea()
58 })
59 }
60
61 resetTextarea () {
62 this.textarea.nativeElement.scrollTop = 0
63 this.textarea.nativeElement.selectionStart = 0
64 this.textarea.nativeElement.selectionEnd = 0
65 }
66
67 hide () {
68 this.openedModal.close()
69 }
70
71 cancel () {
72 this.hide()
73 }
74
75 updateCaption () {
76 const format = 'vtt'
77 const languageId = this.videoCaption.language.id
78 const languageObject = this.videoCaptionLanguages.find(l => l.id === languageId)
79
80 this.captionEdited.emit({
81 language: languageObject,
82 captionfile: new File([ this.form.value['captionFileContent'] ], `${languageId}.${format}`, {
83 type: 'text/vtt',
84 lastModified: Date.now()
85 }),
86 action: 'UPDATE'
87 })
88
89 this.hide()
90 }
91}