Skip to content

ActivityPub-federated video streaming platform using P2P directly in your web browser

Add transcription re-sync button

Chocobozzz5e0788c968d3
diff --git a/client/src/app/+video-watch/shared/player-widgets/video-transcription.component.html b/client/src/app/+video-watch/shared/player-widgets/video-transcription.component.html
index 3f039d92e8..a74d6ee94e 100644
--- a/client/src/app/+video-watch/shared/player-widgets/video-transcription.component.html
+++ b/client/src/app/+video-watch/shared/player-widgets/video-transcription.component.html
@@ -38,13 +38,14 @@
</div>
<input
+ #searchInput
type="text" class="mb-3" name="search-transcript" i18n-placeholder placeholder="Search transcript"
(input)="onSearchChange($event)"
>
</div>
</div>
- <div class="widget-content">
+ <div #segmentsContainer class="widget-content" [class.has-resync-button]="!autoScrollEnabled">
@if (search && segments.length === 0) {
<div class="widget-content-padded" i18n>No results for your search</div>
}
@@ -62,4 +63,7 @@
}
</div>
+ @if (!autoScrollEnabled) {
+ <my-button class="resync-button" theme="primary" label="Synchronize with video" i18n-label (click)="resyncWithVideo()"></my-button>
+ }
</div>
diff --git a/client/src/app/+video-watch/shared/player-widgets/video-transcription.component.scss b/client/src/app/+video-watch/shared/player-widgets/video-transcription.component.scss
index 41cc963680..a40fcf43df 100644
--- a/client/src/app/+video-watch/shared/player-widgets/video-transcription.component.scss
+++ b/client/src/app/+video-watch/shared/player-widgets/video-transcription.component.scss
@@ -12,10 +12,15 @@
flex: 0 0 auto;
}
-.widget-content {
+.widget-root .widget-content {
flex: 1 1 auto;
min-height: 0;
overflow-y: auto;
+
+ // Don't hide the last segments behind the re-sync button
+ &.has-resync-button {
+ padding-bottom: 3.5rem;
+ }
}
.segment {
@@ -40,3 +45,10 @@ input[type="text"] {
left: 0;
right: 0;
}
+
+.resync-button {
+ position: absolute;
+ bottom: 1rem;
+ left: 50%;
+ transform: translateX(-50%);
+}
diff --git a/client/src/app/+video-watch/shared/player-widgets/video-transcription.component.ts b/client/src/app/+video-watch/shared/player-widgets/video-transcription.component.ts
index a0dedadd2d..8c7b9b0441 100644
--- a/client/src/app/+video-watch/shared/player-widgets/video-transcription.component.ts
+++ b/client/src/app/+video-watch/shared/player-widgets/video-transcription.component.ts
@@ -1,14 +1,18 @@
import { NgClass } from '@angular/common'
import {
+ AfterViewInit,
ChangeDetectionStrategy,
Component,
ElementRef,
HostListener,
inject,
input,
+ NgZone,
OnChanges,
+ OnDestroy,
OnInit,
output,
+ Renderer2,
SimpleChanges,
viewChild
} from '@angular/core'
@@ -17,6 +21,7 @@ import { Notifier } from '@app/core'
import { durationToString, isInViewport } from '@app/helpers'
import { SelectOptionsComponent } from '@app/shared/shared-forms/select/select-options.component'
import { GlobalIconComponent } from '@app/shared/shared-icons/global-icon.component'
+import { ButtonComponent } from '@app/shared/shared-main/buttons/button.component'
import { Nl2BrPipe } from '@app/shared/shared-main/common/nl2br.pipe'
import { VideoCaptionService } from '@app/shared/shared-main/video-caption/video-caption.service'
import { NgbCollapse } from '@ng-bootstrap/ng-bootstrap'
@@ -48,14 +53,19 @@ type Segment = {
NgbCollapse,
FormsModule,
SelectOptionsComponent,
- Nl2BrPipe
+ Nl2BrPipe,
+ ButtonComponent
]
})
-export class VideoTranscriptionComponent implements OnInit, OnChanges {
+export class VideoTranscriptionComponent implements OnInit, OnChanges, AfterViewInit, OnDestroy {
private notifier = inject(Notifier)
private captionService = inject(VideoCaptionService)
+ private zone = inject(NgZone)
+ private renderer = inject(Renderer2)
readonly settingsPanel = viewChild<ElementRef>('settingsPanel')
+ readonly searchInput = viewChild<ElementRef<HTMLInputElement>>('searchInput')
+ readonly segmentsContainer = viewChild<ElementRef<HTMLElement>>('segmentsContainer')
readonly video = input<Video>(undefined)
readonly captions = input<VideoCaption[]>(undefined)
@@ -79,9 +89,17 @@ export class VideoTranscriptionComponent implements OnInit, OnChanges {
// true when collapsed has been shown (after the transition)
settingsPanelShown: boolean
+ // false when the user manually scrolled the segments, disabling the auto scroll to the played segment
+ autoScrollEnabled = true
+
private segmentsStore: Segment[] = []
private searchSubject = new Subject<string>()
+ // Scroll position we set automatically when syncing with the video
+ // undefined when the segments have just been replaced and the container position is not settled yet
+ private expectedScrollTop = 0
+ private unlistenScroll: () => void
+
@HostListener('document:click', [ '$event' ])
clickout (event: Event) {
if (!this.settingsPanelShown) return
@@ -100,6 +118,19 @@ export class VideoTranscriptionComponent implements OnInit, OnChanges {
.subscribe(search => this.filterSegments(search))
}
+ ngAfterViewInit () {
+ const container = this.segmentsContainer().nativeElement
+
+ // Scroll events are emitted very often: don't run change detection for each of them
+ this.zone.runOutsideAngular(() => {
+ this.unlistenScroll = this.renderer.listen(container, 'scroll', () => this.onSegmentsScroll(container))
+ })
+ }
+
+ ngOnDestroy () {
+ if (this.unlistenScroll) this.unlistenScroll()
+ }
+
ngOnChanges (changes: SimpleChanges) {
if (changes['video'] || changes['captions']) {
this.load()
@@ -125,11 +156,13 @@ export class VideoTranscriptionComponent implements OnInit, OnChanges {
this.search = ''
this.segmentsStore = []
- this.segments = []
+ this.setSegments([])
this.activeSegment = undefined
this.currentCaption = undefined
+ this.autoScrollEnabled = true
+
this.isSettingsPanelCollapsed = true
this.settingsPanelShown = false
@@ -172,7 +205,7 @@ export class VideoTranscriptionComponent implements OnInit, OnChanges {
}
})
- this.segments = this.segmentsStore
+ this.setSegments(this.segmentsStore)
} catch (err: unknown) {
this.notifier.error($localize`Cannot load transcript: ${(err as Error).message}`)
}
@@ -196,15 +229,63 @@ export class VideoTranscriptionComponent implements OnInit, OnChanges {
this.segmentClicked.emit(segment.start)
}
+ resyncWithVideo () {
+ this.autoScrollEnabled = true
+
+ // The active segment may be filtered out by the search: display the whole transcript so we can scroll to it
+ this.clearSearch()
+
+ this.scrollToActiveSegment({ force: true })
+ }
+
// ---------------------------------------------------------------------------
+ // Run outside of the Angular zone
+ private onSegmentsScroll (container: HTMLElement) {
+ if (!this.autoScrollEnabled) return
+
+ // The segments have just been replaced: the browser may still adjust the scroll position
+ if (this.expectedScrollTop === undefined) return
+
+ // We scrolled the container ourselves
+ if (Math.abs(container.scrollTop - this.expectedScrollTop) < 1) return
+
+ this.zone.run(() => {
+ this.autoScrollEnabled = false
+ })
+ }
+
+ private clearSearch () {
+ const input = this.searchInput()?.nativeElement
+ if (input) input.value = ''
+
+ // Update the segments right away but also feed the subject, so the same search can be typed again
+ this.searchSubject.next('')
+ this.filterSegments('')
+ }
+
private filterSegments (search: string) {
this.search = search
const searchLowercase = search.toLocaleLowerCase()
- this.segments = this.segmentsStore.filter(s => {
+ this.setSegments(this.segmentsStore.filter(s => {
return s.text.toLocaleLowerCase().includes(searchLowercase)
+ }))
+ }
+
+ private setSegments (segments: Segment[]) {
+ this.segments = segments
+
+ // Restore expected scroll when the container will be rendered
+ this.expectedScrollTop = undefined
+
+ setTimeout(() => {
+ const container = this.segmentsContainer()?.nativeElement
+
+ this.expectedScrollTop = container
+ ? container.scrollTop
+ : 0
})
}
@@ -224,19 +305,39 @@ export class VideoTranscriptionComponent implements OnInit, OnChanges {
}
}
- if (lastActiveSegment !== this.activeSegment) {
- setTimeout(() => {
- const element = document.querySelector<HTMLElement>('.segment-' + this.activeSegment.start)
- if (!element) return // Can happen with a search
+ if (lastActiveSegment !== this.activeSegment && this.autoScrollEnabled) {
+ this.scrollToActiveSegment()
+ }
+ }
- const container = document.querySelector<HTMLElement>('.widget-root')
+ private scrollToActiveSegment (options: { force?: boolean } = {}) {
+ const { force = false } = options
- if (isInViewport(element, container)) return
+ setTimeout(() => {
+ const container = this.segmentsContainer()?.nativeElement
+ if (!container) return
- container.scrollTop = element.offsetTop
+ if (!this.activeSegment) {
+ if (force) this.setScrollTop(container, 0)
- debugLogger(`Set transcription segment ${this.activeSegment.start} in viewport`)
- })
- }
+ return
+ }
+
+ const element = container.querySelector<HTMLElement>('.segment-' + this.activeSegment.start)
+ if (!element) return // Can happen with a search
+
+ if (!force && isInViewport(element, container)) return
+
+ this.setScrollTop(container, container.scrollTop + element.getBoundingClientRect().top - container.getBoundingClientRect().top)
+
+ debugLogger(`Set transcription segment ${this.activeSegment.start} in viewport`)
+ })
+ }
+
+ private setScrollTop (container: HTMLElement, scrollTop: number) {
+ container.scrollTop = scrollTop
+
+ // The browser may have clamped the value, so use the effective position as reference
+ this.expectedScrollTop = container.scrollTop
}
}