aboutsummaryrefslogtreecommitdiff
path: root/src/client/views/DictationButton.tsx
blob: fc3165f67478b56b9e5503e7f02067e4ad695e29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import { makeObservable, observable, action } from 'mobx';
import { observer } from 'mobx-react';
import * as React from 'react';
import './DictationButton.scss';
import { DictationManager } from '../util/DictationManager';
import { SnappingManager } from '../util/SnappingManager';

export interface DictationButtonProps {
    setInput: (val: string) => void;
    inputRef?: HTMLInputElement | null | undefined;
}

@observer
export class DictationButton extends React.Component<DictationButtonProps> {
    @observable private _isRecording = false;

    constructor(props: DictationButtonProps) {
        super(props);
        makeObservable(this);
    }

    stopDictation = action(() => {
        this._isRecording = false;
        DictationManager.Controls.stop();
    });

    render() {
        return (
            <button
                className={`dictation-button ${this._isRecording ? 'recording' : ''}`}
                title="Record"
                onClick={action(() => {
                    if (!this._isRecording) {
                        this._isRecording = true;
                        DictationManager.Controls.listen({
                            interimHandler: (value: string) => {
                                this.props.setInput(value);
                                if (this.props.inputRef) {
                                    this.props.inputRef.focus();
                                    this.props.inputRef.scrollLeft = 1000000;
                                }
                            },
                            continuous: { indefinite: false },
                        }).then(results => {
                            if (results && [DictationManager.Controls.Infringed].includes(results)) {
                                DictationManager.Controls.stop();
                            }
                        });
                    } else {
                        this.stopDictation();
                    }
                })}>
                <svg xmlns="http://www.w3.org/2000/svg" width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round">
                    <path d="M12 1a3 3 0 0 0-3 3v8a3 3 0 0 0 6 0V4a3 3 0 0 0-3-3z"></path>
                    <path d="M19 10v2a7 7 0 0 1-14 0v-2"></path>
                    <line x1="12" y1="19" x2="12" y2="23"></line>
                    <line x1="8" y1="23" x2="16" y2="23"></line>
                </svg>
            </button>
        );
    }
}