質問編集履歴

1

ご指摘のあった箇所を修正してたソースコードを追加しました。

2021/01/10 23:37

投稿

Kentarou3
Kentarou3

スコア1

test CHANGED
File without changes
test CHANGED
@@ -222,6 +222,210 @@
222
222
 
223
223
 
224
224
 
225
+ 2021/1/11修正版
226
+
227
+ ```React
228
+
229
+
230
+
231
+ <!DOCTYPE html>
232
+
233
+ <html>
234
+
235
+
236
+
237
+ <head>
238
+
239
+ <meta charset="UTF-8" />
240
+
241
+ <title>React</title>
242
+
243
+ <script src="https://unpkg.com/react@17/umd/react.development.js"></script>
244
+
245
+ <script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
246
+
247
+ <script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
248
+
249
+ </head>
250
+
251
+
252
+
253
+ <body>
254
+
255
+ <div id="root"></div>
256
+
257
+ <script type="text/babel">
258
+
259
+
260
+
261
+ (() => {
262
+
263
+
264
+
265
+ class App extends React.Component {
266
+
267
+ constructor() {
268
+
269
+ super();
270
+
271
+ this.state = {
272
+
273
+ answer: "abc",
274
+
275
+ blank: "a__",
276
+
277
+ location: 1,
278
+
279
+ speaking: undefined,
280
+
281
+ }
282
+
283
+ this.keydown = this.keydown.bind(this)
284
+
285
+ this.speak = this.speak.bind(this)
286
+
287
+ }
288
+
289
+ speak(word) {
290
+
291
+ var speech = new SpeechSynthesisUtterance(word)
292
+
293
+ speechSynthesis.speak(speech)
294
+
295
+ console.log(speechSynthesis)
296
+
297
+ this.setState({
298
+
299
+ ...this.state,
300
+
301
+ speechSynthesis
302
+
303
+ })
304
+
305
+ speech.onend = function (event) {
306
+
307
+ console.log(event)
308
+
309
+ console.log(speechSynthesis)
310
+
311
+ }
312
+
313
+ }
314
+
315
+ keydown(e) {
316
+
317
+ console.log(e.key)
318
+
319
+
320
+
321
+ if (this.state.answer[this.state.location] == e.key) {
322
+
323
+ this.setState((state, props) => ({
324
+
325
+ location: state.location + 1
326
+
327
+ }));
328
+
329
+ let blank = this.state.answer.substring(0, this.state.location) + '_'.repeat(this.state.blank.length - this.state.location);
330
+
331
+
332
+
333
+ this.setState({
334
+
335
+ blank: blank
336
+
337
+ })
338
+
339
+ console.log(blank)
340
+
341
+
342
+
343
+ if (this.state.answer.length == this.state.location) {
344
+
345
+ this.speak(this.state.answer)
346
+
347
+ console.log(this.state.speechSynthesis)
348
+
349
+ // while (this.state.speechSynthesis.speaking)
350
+
351
+ this.setState({
352
+
353
+ ...this.state,
354
+
355
+ answer: "def",
356
+
357
+ blank: "d__",
358
+
359
+ location: 1,
360
+
361
+ })
362
+
363
+ }
364
+
365
+ }
366
+
367
+ }
368
+
369
+
370
+
371
+ componentDidMount() {
372
+
373
+ window.addEventListener('keydown', this.keydown)
374
+
375
+ }
376
+
377
+
378
+
379
+ render() {
380
+
381
+ return (
382
+
383
+ <div>
384
+
385
+ <div>
386
+
387
+ {this.state.answer}
388
+
389
+ </div>
390
+
391
+ <div>
392
+
393
+ {this.state.blank}
394
+
395
+ </div>
396
+
397
+ </div>
398
+
399
+ );
400
+
401
+ }
402
+
403
+ }
404
+
405
+
406
+
407
+ ReactDOM.render(
408
+
409
+ <App />,
410
+
411
+ document.getElementById('root')
412
+
413
+ );
414
+
415
+ })();
416
+
417
+
418
+
419
+ </script>
420
+
421
+ </body>
422
+
423
+
424
+
425
+ </html>
426
+
427
+ ```
428
+
225
429
  ### 試したこと
226
430
 
227
431