-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
251 lines (205 loc) · 8.82 KB
/
index.html
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<!doctype html>
<meta charset="utf-8">
<title>Nums123 - Learn to count in multiple languages</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Inter:wght@400;600;700&display=swap" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@lmc-eu/spirit-web@latest/css/foundation.min.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@lmc-eu/spirit-web@latest/css/components.min.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@lmc-eu/spirit-web@latest/css/utilities.min.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@lmc-eu/spirit-web@latest/css/components.min.css" />
<script src="https://cdn.jsdelivr.net/npm/@lmc-eu/spirit-web@latest/js/bundle/spirit-web.bundle.js" defer></script>
<style>
body {
background-color: #FDFDF1;
}
footer a {
font-weight: bold;
}
</style>
<body>
<header class="Header Header--simple">
<a href="./">
<img src="logo.png" alt="Nums123 logo" width="80" />
</a>
<h1 class="text-center">Nums123 (beta)</h1>
</header>
<div id="uiConf" class="text-center">
<div class="Select">
<label for="selectSimple" class="Select__label">Choose language:</label>
<div class="Select__inputContainer">
<select id="slc-voices" class="Select__input"></select>
<div class="Select__icon">
<svg width="24" height="24" aria-hidden="true">
<use xlink:href="sprite.svg#chevron-down" />
</svg>
</div>
</div>
<div class="Select__helperText">Cannot see language you are learning? Install its voice in your system.</div>
</div>
<div class="mt-200">
<label for="range-speed">Change voice speed if necessary:</label>
<input id="range-speed" type="range" min="0.1" max="1" value="1" step="0.1">
</div>
<div class="mt-600">
<button id="btn-play-new-10" type="button" class="Button Button--primary Button--medium mr-200 mb-200" data-nums123-value="10">Say 1-10</button>
<button id="btn-play-new-20" type="button" class="Button Button--primary Button--medium mr-200 mb-200" data-nums123-value="20">Say 1-20</button>
<button id="btn-play-new-100" type="button" class="Button Button--primary Button--medium mr-200 mb-200" data-nums123-value="100">Say 1-100</button>
<button id="btn-play-new-more" type="button" class="Button Button--primary Button--medium mr-200 mb-200" data-nums123-value="1000000">Say 1-1.000.000</button>
</div>
<div class="mt-600">
<button id="btn-repeat" type="button" class="Button Button--secondary Button--medium mr-200 mb-200">Repeat</button>
<button id="btn-stop" type="button" class="Button Button--warning Button--medium mr-200 mb-200">Stop speach</button>
</div>
</div>
<div id="uiOutput" class="text-center mt-800 mb-800">
<button id="btn-reveal" type="button" class="Button Button--informative Button--medium" role="button" data-spirit-toggle="collapse" data-spirit-target="CollapseOutput">Reveal me the number</button>
<div id="CollapseOutput" class="Collapse">
<div class="Collapse__content" id="results">not yet ... press <em>Say 1-10</em> first</div>
</div>
</div>
<div id="uiLearning" class="text-center mt-800 mb-800">
<button id="btn-teach-1-10" type="button" class="Button Button--tertiary Button--medium mr-200 mb-200" role="button" data-nums123-value="1, 2, 3, 4, 5, 6, 7, 8, 9, 10">Teach me 1-10</button>
<button id="btn-teach-11-20" type="button" class="Button Button--tertiary Button--medium mr-200 mb-200" role="button" data-nums123-value="11, 12, 13, 14, 15, 16, 17, 18, 19, 20">Teach me 11-20</button>
<button id="btn-teach-10-100" type="button" class="Button Button--tertiary Button--medium mr-200 mb-200" role="button" data-nums123-value="10, 20, 30, 40, 50, 60, 70, 80, 90, 100">Teach me 10-100</button>
<button id="btn-teach-more" type="button" class="Button Button--tertiary Button--medium mr-200 mb-200" role="button" data-nums123-value="0, 10, 100, 1000, 10000, 100000, 1000000, 1000000000, 1000000000000">Teach me more</button>
</div>
<footer class="text-center mt-800 mb-800"><p>Made by <a href="https://www.linkedin.com/in/hassman/">Martin Hassman</a>. <a href="https://github.com/met/nums123">Source code</a> available (MIT License).</p></footer>
<script type="text/javascript">
"use strict";
let appState = { voices: null, lastNumber: null };
function detectSupport() {
if (window.speechSynthesis)
return true;
else
return false;
}
function getVoicesList() {
let list = window.speechSynthesis.getVoices();
if (list != null && list.length > 0)
return list;
else {
console.warn("Cannot obtain list of voices.");
return null;
}
}
function populateVoiceList() {
appState.voices = getVoicesList();
if (appState.voices != null) {
let select = document.getElementById("slc-voices");
for (let voice in appState.voices) {
//console.log(voice);
//console.log(appState.voices[voice]);
let optEl = document.createElement("option");
optEl.textContent = `${appState.voices[voice].lang} :: ${appState.voices[voice].name}`;
optEl.value = voice;
select.appendChild(optEl);
}
// Set select element to value from last session when available
if (localStorage.getItem("nums123")) {
let savedConf = JSON.parse(localStorage.getItem("nums123"));
let lastIndex = Number(savedConf.selection);
select.value = lastIndex;
}
}
// Sometimes speechSynthesis.getVoices() does not work immitiatelly, we can schedule here to call it a little it later when ready
else if (window.speechSynthesis.onvoiceschanged !== undefined) {
speechSynthesis.onvoiceschanged = populateVoiceList;
}
}
// Random integer from closed interval 1..maximum
function randInt(maximum = 1000) {
return (Math.floor(Math.random()*maximum) + 1);
}
function getSelectedVoiceIndex() {
let voiceIndex = Number(document.getElementById("slc-voices").value);
return voiceIndex;
}
function getSelectedVoiceObj() {
let voice = appState.voices[getSelectedVoiceIndex()];
return voice;
}
function playNew(maximum = 1000) {
let newNumber = randInt(maximum);
console.info(newNumber);
collapseResultIfOpened();
setTimeout(() => {writeResult(newNumber) }, 200); // small timeout is needed, otherwise user could see new number before collapsing
appState.lastNumber = newNumber;
sayItWithSelectedVoice(newNumber);
}
function sayItWithSelectedVoice(text) {
sayIt(text, getSelectedVoiceObj());
}
function sayIt(text, voice) {
let sayThis = new SpeechSynthesisUtterance(text);
sayThis.voice = voice;
sayThis.lang = voice.lang; // This is IMPORTANT for Chrome on Android, without this Chrome ignores voice setting and use always default voice
sayThis.rate = Number(document.getElementById("range-speed").value);
//console.log(sayThis.rate);
//sayThis.addEventListener("start", (event) => { console.log(event) });
//sayThis.addEventListener("end", (event) => { console.log(event) });
window.speechSynthesis.speak(sayThis);
}
function collapseResultIfOpened() {
let clps = window["spirit-web"].Collapse.getInstance("#btn-reveal");
if (clps.state.open) {
clps.hide();
}
}
function writeResult(number) {
document.getElementById("results").innerText = String(number);
}
function initUI() {
populateVoiceList();
document.getElementById("btn-repeat").addEventListener("click", (event) => {
if (appState.lastNumber && appState.voices) {
sayItWithSelectedVoice(appState.lastNumber);
}
return false;
});
// one universal handler to handle all play new number buttons
["btn-play-new-10", "btn-play-new-20", "btn-play-new-100", "btn-play-new-more"].forEach((element) => {
document.getElementById(element).addEventListener("click", (event) => {
if (!appState.voices) {
populateVoiceList();
}
playNew(event.target.dataset.nums123Value);
return false;
});
});
// one universal handler to handle all teaching buttons
["btn-teach-1-10", "btn-teach-11-20", "btn-teach-10-100", "btn-teach-more"].forEach((element) => {
document.getElementById(element).addEventListener("click", (event) => {
if (appState.voices) {
sayItWithSelectedVoice(event.target.dataset.nums123Value);
}
return false;
});
});
document.getElementById("slc-voices").addEventListener("focus", (event) => {
if (!appState.voices) {
populateVoiceList();
}
});
document.getElementById("slc-voices").addEventListener("change", (event) => {
if (appState.lastNumber && appState.voices) {
sayItWithSelectedVoice(appState.lastNumber);
}
// preserve selected voice between sessions
// (to make is easier we assume here that the list of voices did not change between sessions)
localStorage.setItem("nums123", JSON.stringify( { selection: getSelectedVoiceIndex() } ));
});
document.getElementById("btn-stop").addEventListener("click", (event) => {
window.speechSynthesis.cancel();
return false;
});
}
if (!detectSupport()) {
console.error("This browser is not compatible");
}
else {
document.addEventListener("DOMContentLoaded", (argument) => {
initUI();
});
}
</script>
</body>