aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/DictationManager.ts
blob: b0866a82644997169a5693ba134c771c08ee0ca7 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { SelectionManager } from "./SelectionManager";
import { DocumentView } from "../views/nodes/DocumentView";
import { undoBatch } from "./UndoManager";
import * as converter from "words-to-numbers";
import { Doc } from "../../new_fields/Doc";
import { List } from "../../new_fields/List";
import { Docs, DocumentType } from "../documents/Documents";
import { CollectionViewType } from "../views/collections/CollectionBaseView";
import { Cast, CastCtor } from "../../new_fields/Types";
import { listSpec } from "../../new_fields/Schema";
import { AudioField, ImageField } from "../../new_fields/URLField";
import { HistogramField } from "../northstar/dash-fields/HistogramField";

namespace CORE {
    export interface IWindow extends Window {
        webkitSpeechRecognition: any;
    }
}

const ConstructorMap = new Map<DocumentType, CastCtor>([
    [DocumentType.COL, listSpec(Doc)],
    [DocumentType.AUDIO, AudioField],
    [DocumentType.IMG, ImageField],
    [DocumentType.HIST, HistogramField],
    [DocumentType.IMPORT, listSpec(Doc)]
]);

const tryCast = (view: DocumentView, type: DocumentType) => {
    let ctor = ConstructorMap.get(type);
    if (!ctor) {
        return false;
    }
    return Cast(Doc.GetProto(view.props.Document).data, ctor) !== undefined;
};

const validate = (target: DocumentView, types: DocumentType[]) => {
    for (let type of types) {
        if (tryCast(target, type)) {
            return true;
        }
    }
    return false;
};

const { webkitSpeechRecognition }: CORE.IWindow = window as CORE.IWindow;
export type IndependentAction = (target: DocumentView) => any | Promise<any>;
export type DependentAction = (target: DocumentView, matches: RegExpExecArray) => any | Promise<any>;
export type RegistrationEntry = { action: IndependentAction, restrictTo?: DocumentType[] };
export type ActionPredicate = (target: DocumentView) => boolean;
export type RegexEntry = { expression: RegExp, action: DependentAction, restrictTo?: DocumentType[] };

export default class DictationManager {
    public static Instance = new DictationManager();
    private recognizer: any;
    private isListening = false;

    constructor() {
        this.recognizer = new webkitSpeechRecognition();
        this.recognizer.interimResults = false;
        this.recognizer.continuous = true;
    }

    finish = (handler: any, data: any) => {
        handler(data);
        this.stop();
    }

    stop = () => {
        this.recognizer.stop();
        this.isListening = false;
    }

    listen = () => {
        if (this.isListening) {
            return undefined;
        }
        this.isListening = true;
        this.recognizer.start();
        return new Promise<string>((resolve, reject) => {
            this.recognizer.onresult = (e: any) => this.finish(resolve, e.results[0][0].transcript);
            this.recognizer.onerror = (e: any) => this.finish(reject, e);
        });
    }

    public interpretNumber = (number: string) => {
        let initial = parseInt(number);
        if (!isNaN(initial)) {
            return initial;
        }
        let converted = converter.wordsToNumbers(number, { fuzzy: true });
        if (converted === null) {
            return NaN;
        }
        return typeof converted === "string" ? parseInt(converted) : converted;
    }

    @undoBatch
    public execute = async (phrase: string) => {
        let targets = SelectionManager.SelectedDocuments();
        if (!targets || !targets.length) {
            return;
        }

        let entry = RegisteredCommands.Independent.get(phrase);
        if (entry) {
            let success = false;
            for (let target of targets) {
                if (!entry.restrictTo || validate(target, entry.restrictTo)) {
                    await entry.action(target);
                    success = true;
                }
            }
            return success;
        }

        for (let entry of RegisteredCommands.Dependent) {
            let regex = entry.expression;
            let matches = regex.exec(phrase);
            regex.lastIndex = 0;
            if (matches !== null) {
                let success = false;
                for (let target of targets) {
                    if (!entry.restrictTo || validate(target, entry.restrictTo)) {
                        await entry.action(target, matches);
                        success = true;
                    }
                }
                return success;
            }
        }

        return false;
    }

}

export namespace RegisteredCommands {

    export const Independent = new Map<string, RegistrationEntry>([

        ["clear", {
            action: (target: DocumentView) => {
                Doc.GetProto(target.props.Document).data = new List();
            },
            restrictTo: [DocumentType.COL]
        }],

        ["open fields", {
            action: (target: DocumentView) => {
                let kvp = Docs.Create.KVPDocument(target.props.Document, { width: 300, height: 300 });
                target.props.addDocTab(kvp, target.dataDoc, "onRight");
            }
        }]

    ]);

    export const Dependent = new Array<RegexEntry>(

        {
            expression: /create (\w+) documents of type (image|nested collection)/g,
            action: (target: DocumentView, matches: RegExpExecArray) => {
                let count = DictationManager.Instance.interpretNumber(matches[1]);
                let what = matches[2];
                let dataDoc = Doc.GetProto(target.props.Document);
                let fieldKey = "data";
                for (let i = 0; i < count; i++) {
                    let created: Doc | undefined;
                    switch (what) {
                        case "image":
                            created = Docs.Create.ImageDocument("https://upload.wikimedia.org/wikipedia/commons/thumb/3/3a/Cat03.jpg/1200px-Cat03.jpg");
                            break;
                        case "nested collection":
                            created = Docs.Create.FreeformDocument([], {});
                            break;
                    }
                    created && Doc.AddDocToList(dataDoc, fieldKey, created);
                }
            },
            restrictTo: [DocumentType.COL]
        },

        {
            expression: /view as (freeform|stacking|masonry|schema|tree)/g,
            action: (target: DocumentView, matches: RegExpExecArray) => {
                let mode = CollectionViewType.ValueOf(matches[1]);
                mode && (target.props.Document.viewType = mode);
            },
            restrictTo: [DocumentType.COL]
        }

    );

}