Skip to content

Commit d78b68b

Browse files
author
Andrea Barbasso
committed
[UXP-126] add audio controls
1 parent e76ce85 commit d78b68b

3 files changed

Lines changed: 46 additions & 4 deletions

File tree

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
<div class="p-2 text-white">
2-
<i (click)="textToSpeech($event)" class="button fa fa-volume-high"></i>
2+
<div class="tts-controls">
3+
<i *ngIf="!this.utterance" class="button fa fa-volume-high" (click)="textToSpeech()"></i>
4+
<i *ngIf="this.utterance && !isPaused" class="button fa fa-pause" (click)="pauseTextToSpeech()"></i>
5+
<i *ngIf="this.utterance && isPaused" class="button fa fa-play" (click)="resumeTextToSpeech()"></i>
6+
</div>
37
</div>
48
<div class="pointer-down">&nbsp;</div>

src/app/directives/text-select/text-selection-tooltip/text-selection-tooltip.component.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@ div.pointer-down {
1818
width: 12px;
1919
height: 12px;
2020
}
21+
22+
div.tts-controls {
23+
width: 20px;
24+
display: flex;
25+
justify-content: center;
26+
}

src/app/directives/text-select/text-selection-tooltip/text-selection-tooltip.component.ts

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
1-
import { ChangeDetectionStrategy, Component, HostBinding, HostListener, Input, OnDestroy, OnInit } from '@angular/core';
1+
import {
2+
ChangeDetectionStrategy,
3+
ChangeDetectorRef,
4+
Component,
5+
HostBinding,
6+
HostListener,
7+
Input,
8+
OnDestroy,
9+
OnInit
10+
} from '@angular/core';
11+
import { NgIf } from '@angular/common';
212

313
@Component({
414
selector: 'ds-text-selection-tooltip',
515
templateUrl: './text-selection-tooltip.component.html',
616
styleUrls: ['./text-selection-tooltip.component.scss'],
717
changeDetection: ChangeDetectionStrategy.OnPush,
18+
imports: [
19+
NgIf
20+
],
821
standalone: true
922
})
1023
export class TextSelectionTooltipComponent implements OnInit, OnDestroy {
@@ -37,6 +50,11 @@ export class TextSelectionTooltipComponent implements OnInit, OnDestroy {
3750
bottom: number;
3851

3952
utterance: SpeechSynthesisUtterance;
53+
isPaused = false;
54+
55+
constructor(private changeDetectorRef: ChangeDetectorRef) {
56+
57+
}
4058

4159
ngOnInit(): void {
4260
this.top = this.rectangleTop - 6;
@@ -49,17 +67,31 @@ export class TextSelectionTooltipComponent implements OnInit, OnDestroy {
4967
event.preventDefault();
5068
}
5169

52-
public textToSpeech(event: MouseEvent): void {
70+
public textToSpeech(): void {
5371
if (this.utterance) {
5472
speechSynthesis.cancel();
5573
}
5674
this.utterance = new SpeechSynthesisUtterance(this.text);
75+
this.utterance.onend = () => {
76+
this.utterance = null;
77+
this.changeDetectorRef.detectChanges();
78+
};
5779
speechSynthesis.speak(this.utterance);
5880
}
5981

60-
public ngOnDestroy(): void {
82+
ngOnDestroy(): void {
6183
if (this.utterance) {
6284
speechSynthesis.cancel();
6385
}
6486
}
87+
88+
pauseTextToSpeech() {
89+
speechSynthesis.pause();
90+
this.isPaused = true;
91+
}
92+
93+
resumeTextToSpeech() {
94+
speechSynthesis.resume();
95+
this.isPaused = false;
96+
}
6597
}

0 commit comments

Comments
 (0)