Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cdk): Media add playbackRate input #83

Merged
merged 2 commits into from
Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions projects/cdk/directives/media/media.directive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import {
Input,
Output,
} from '@angular/core';
import {tuiDefaultProp} from '@taiga-ui/cdk/decorators';
import {tuiDefaultProp, tuiRequiredSetter} from '@taiga-ui/cdk/decorators';

export function currentTimeAssertion(currentTime: number): boolean {
return isFinite(currentTime) && currentTime >= 0;
export function nonNegativeFiniteAssertion(value: number): boolean {
return isFinite(value) && value >= 0;
}

export function volumeAssertion(volume: number): boolean {
Expand All @@ -28,8 +28,14 @@ export class TuiMediaDirective {
@tuiDefaultProp(volumeAssertion)
volume = 1;

@Input('playbackRate')
@tuiRequiredSetter(nonNegativeFiniteAssertion)
set playbackRateSetter(playbackRate: number) {
this.updatePlaybackRate(playbackRate);
}

@Input()
@tuiDefaultProp(currentTimeAssertion)
@tuiRequiredSetter(nonNegativeFiniteAssertion)
set currentTime(currentTime: number) {
if (currentTime !== this.currentTime) {
this.elementRef.nativeElement.currentTime = currentTime;
Expand All @@ -42,6 +48,7 @@ export class TuiMediaDirective {
this.elementRef.nativeElement.pause();
} else {
this.elementRef.nativeElement.play();
this.updatePlaybackRate(this.playbackRate);
}
}

Expand All @@ -54,6 +61,8 @@ export class TuiMediaDirective {
@Output()
readonly volumeChange = new EventEmitter<number>();

private playbackRate = 1;

constructor(
@Inject(ElementRef)
private readonly elementRef: ElementRef<HTMLMediaElement>,
Expand All @@ -73,6 +82,7 @@ export class TuiMediaDirective {
@HostListener('play', ['false'])
onPausedChange(paused: boolean) {
this.pausedChange.emit(paused);
this.updatePlaybackRate(this.playbackRate);
}

@HostListener('volumechange')
Expand All @@ -92,4 +102,9 @@ export class TuiMediaDirective {
changeDetectionTrigger() {
// @bad TODO: consider if other events need to trigger CD
}

private updatePlaybackRate(playbackRate: number) {
this.playbackRate = playbackRate;
this.elementRef.nativeElement.playbackRate = this.playbackRate;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class ExampleTuiMediaComponent {

readonly volumeVariants: readonly number[] = [1, 0.5, 0.25, 0];

playbackRate = 1;
currentTime = 0;
volume = this.volumeVariants[0];
paused = true;
Expand Down
12 changes: 11 additions & 1 deletion projects/demo/src/modules/directives/media/media.template.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@
tuiMedia
controls
src="assets/media/strays.mp3"
[playbackRate]="playbackRate"
[(paused)]="paused"
[(currentTime)]="currentTime"
[paused]="paused"
[(volume)]="volume"
></audio>
<tui-doc-documentation>
Expand All @@ -58,6 +59,15 @@
>
Paused state
</ng-template>
<ng-template
i18n
documentationPropertyName="playbackRate"
documentationPropertyType="number"
documentationPropertyMode="input"
[(documentationPropertyValue)]="playbackRate"
>
Playback speed
</ng-template>
<ng-template
i18n
documentationPropertyName="volume"
Expand Down