Skip to content

Commit

Permalink
updating the stopAfterSubmit API
Browse files Browse the repository at this point in the history
  • Loading branch information
OvidijusParsiunas committed Mar 6, 2024
1 parent 252f5e7 commit 7ba3d17
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
4 changes: 2 additions & 2 deletions component/src/types/microphone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export interface AudioRecordingFiles {
maxDurationSeconds?: number;
}

export type SubmitAfterSilence = true | {ms?: number; stop?: boolean};
export type SubmitAfterSilence = true | number;

export type SpeechToTextConfig = {
webSpeech?: true | WebSpeechOptions;
Expand All @@ -26,7 +26,7 @@ export type SpeechToTextConfig = {
textColor?: TextColor;
translations?: Translations;
commands?: Commands & {submit?: string};
stopAfterSubmitClick?: false;
stopAfterSubmit?: false;
submitAfterSilence?: SubmitAfterSilence;
button?: {commandMode?: ButtonStyles} & MicrophoneStyles; // TO-DO - potentially include a pause style
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,12 @@ import {SpeechToText} from './speechToText';

export class SilenceSubmit {
private _silenceTimeout?: number;
private readonly ms: number = 2000;
private readonly silenceMS: number = 2000;
private readonly stop: boolean = true;

constructor(submitAfterSilence: SubmitAfterSilence) {
if (typeof submitAfterSilence === 'object') {
const {ms, stop} = submitAfterSilence;
if (ms && ms > 0) this.ms = ms;
if (stop !== undefined && stop !== null) this.stop = stop;
}
constructor(submitAfterSilence: SubmitAfterSilence, stopAfterSubmit?: boolean) {
if (typeof stopAfterSubmit === 'boolean' && stopAfterSubmit === false) this.stop = false;
if (typeof submitAfterSilence === 'number') this.silenceMS = submitAfterSilence;
}

private setSilenceTimeout(textInput: TextInputEl, buttonClick: Function) {
Expand All @@ -23,7 +20,7 @@ export class SilenceSubmit {
if (!this.stop) {
setTimeout(buttonClick, SpeechToText.MICROPHONE_RESET_TIMEOUT_MS);
}
}, this.ms);
}, this.silenceMS);
}

public clearSilenceTimeout() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ export class SpeechToText extends MicrophoneButton {
return null;
};
}
if (newConfig.submitAfterSilence) this._silenceSubmit = new SilenceSubmit(newConfig.submitAfterSilence);
if (newConfig.submitAfterSilence) {
this._silenceSubmit = new SilenceSubmit(newConfig.submitAfterSilence, newConfig.stopAfterSubmit);
}
const serviceName = SpeechToText.getServiceName(newConfig);
return {serviceName, processedConfig};
}
Expand Down Expand Up @@ -107,8 +109,8 @@ export class SpeechToText extends MicrophoneButton {
this._addErrorMessage('speechToText', 'speech input error');
}

public static toggleSpeechAfterSubmit(microphoneButton: HTMLElement, stopAfterSubmitClick = true) {
public static toggleSpeechAfterSubmit(microphoneButton: HTMLElement, stopAfterSubmit = true) {
microphoneButton.click();
if (!stopAfterSubmitClick) setTimeout(() => microphoneButton.click(), SpeechToText.MICROPHONE_RESET_TIMEOUT_MS);
if (!stopAfterSubmit) setTimeout(() => microphoneButton.click(), SpeechToText.MICROPHONE_RESET_TIMEOUT_MS);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ export class SubmitButton extends InputButton<Styles> {

private setUpSpeechToText(microphoneButton: InputButton, speechToText: DeepChat['speechToText']) {
this._microphoneButton = microphoneButton.elementRef;
this._stopSTTAfterSubmit = typeof speechToText === 'object' ? speechToText.stopAfterSubmitClick : false;
this._stopSTTAfterSubmit = typeof speechToText === 'object' ? speechToText.stopAfterSubmit : false;
}

private resetSubmit(validationHandler: ValidationHandler) {
Expand Down

0 comments on commit 7ba3d17

Please sign in to comment.