forked from eimaj/speak-and-spell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
218 lines (179 loc) · 4.01 KB
/
index.js
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
const languages = [
'en',
'fr',
];
const letters = [
'KeyA',
'KeyB',
'KeyC',
'KeyD',
'KeyE',
'KeyF',
'KeyG',
'KeyH',
'KeyI',
'KeyJ',
'KeyK',
'KeyL',
'KeyM',
'KeyN',
'KeyO',
'KeyP',
'KeyQ',
'KeyR',
'KeyS',
'KeyT',
'KeyU',
'KeyV',
'KeyW',
'KeyX',
'KeyY',
'KeyZ',
'Digit0',
'Digit1',
'Digit2',
'Digit3',
'Digit4',
'Digit5',
'Digit6',
'Digit7',
'Digit8',
'Digit9',
'Space',
];
const state = {
// DOM elements:
input: null,
output: null,
clear: null,
submit: null,
languages: [],
// User generated text:
text: '',
// Config for speechSynthesis:
lang: 'fr',
rate: '1',
// speechSynthesis instances:
synth: null,
utterance: null,
};
const setProperty = function (key, value) {
state[key] = value;
return state[key];
};
const renderText = function (text) {
// Set to state:
setProperty('text', text);
// Send to DOM:
state.output.innerText = text;
return state.output.innerText;
};
const speak = function (text) {
// Config:
state.utterance.lang = state.lang;
state.utterance.rate = state.rate;
state.utterance.text = text;
// Stop current speech:
state.synth.cancel();
// Speak:
return state.synth.speak(state.utterance);
};
const toggleLangButtons = function (newLang) {
const activeLang = state.languages.filter(language => language.dataset['lang'] === newLang);
state.languages.map((lang) => lang.className = 'languages__toggle');
activeLang[0].className = 'languages__toggle languages__toggle--is-active';
return activeLang[0];
};
const keypress = {
backspace() {
const text = state.text;
return renderText(text.substring(0, text.length - 1));
},
enter() {
return actions.speakText();
},
escape() {
return actions.clearText();
},
letter(key) {
renderText(state.text + key);
return speak(state.text[state.text.length - 1]);
},
};
const actions = {
handleKeyup(e) {
if (e.code === 'Backspace') { keypress.backspace(); };
if (e.code === 'Enter') { keypress.enter(); };
if (e.code === 'Escape') { keypress.escape(); };
if (letters.indexOf(e.code) === -1) { return false; };
return keypress.letter(e.key);
},
changeLanguage(e) {
const newLang = e.target.dataset['lang'];
toggleLangButtons(newLang);
return setProperty('lang', newLang);
},
speakText() {
setProperty('text', state.text);
return speak(state.text);
},
clearText() {
return renderText('');
},
};
const bind = {
input() {
state.input.focus();
return state.input.addEventListener('keyup', actions.handleKeyup);
},
languageButtons() {
return state.languages.map(function (language) {
return language.addEventListener('click', actions.changeLanguage)
});
},
submitButton() {
return state.submit.addEventListener('click', this.speakText);
},
clearButton() {
return state.clear.addEventListener('click', actions.clearText);
},
};
const app = {
initInput() {
setProperty('input', document.querySelector('body'));
return bind.input();
},
initOutput() {
return setProperty('output', document.querySelector('div[name="output"]'));
},
initSubmit() {
setProperty('submit', document.querySelector('a[name="speak-input"]'));
return bind.submitButton();
},
initClear() {
setProperty('clear', document.querySelector('a[name="clear-input"]'));
return bind.clearButton();
},
initSynth() {
const synth = window.speechSynthesis;
const utterance = new SpeechSynthesisUtterance('');
setProperty('utterance', utterance);
return setProperty('synth', synth);
},
initLanguages() {
const nodeList = document.querySelectorAll('a[name="change-language"]');
const languageButtons = Array.from(nodeList);
setProperty('languages', languageButtons);
return bind.languageButtons();
},
init() {
this.initInput();
this.initOutput();
this.initSubmit();
this.initClear();
this.initLanguages();
this.initSynth();
return this;
},
};
app.init();