wiki-plugin-rcngraph 0.1.15 → 0.1.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/client/rcngraph.js +56 -2
- package/package.json +1 -1
package/client/rcngraph.js
CHANGED
|
@@ -221,6 +221,55 @@
|
|
|
221
221
|
})
|
|
222
222
|
}
|
|
223
223
|
|
|
224
|
+
// People's own recordings of a step, made in the Graph Tool, Map or Timeline walk
|
|
225
|
+
// editor: step.voices = {name: {audio, say}}. A step plays its recording when it has
|
|
226
|
+
// one, else the computer voice. The listener picks whose, kept in this browser with
|
|
227
|
+
// the other walk settings (rcnWalkPrefs.speaker: '' any, '-' computer only, or a name).
|
|
228
|
+
let recAudio = null
|
|
229
|
+
const recOk = src => typeof src === 'string' && /^(data:audio\/|https?:\/\/|\/)/.test(src)
|
|
230
|
+
const recNames = st => st && st.voices ? Object.keys(st.voices).filter(n => st.voices[n] && recOk(st.voices[n].audio)) : []
|
|
231
|
+
const recSpeakers = walk => [...new Set(((walk && walk.steps) || []).flatMap(recNames))].sort()
|
|
232
|
+
function recSpeaker() {
|
|
233
|
+
try { return JSON.parse(localStorage.getItem('rcnWalkPrefs') || '{}').speaker || '' } catch (e) { return '' }
|
|
234
|
+
}
|
|
235
|
+
function recSetSpeaker(v) {
|
|
236
|
+
let p = {}
|
|
237
|
+
try { p = JSON.parse(localStorage.getItem('rcnWalkPrefs') || '{}') } catch (e) {}
|
|
238
|
+
p.speaker = v
|
|
239
|
+
try { localStorage.setItem('rcnWalkPrefs', JSON.stringify(p)) } catch (e) {}
|
|
240
|
+
}
|
|
241
|
+
function recSpeakerOptions(walk) {
|
|
242
|
+
const ns = recSpeakers(walk), cur = recSpeaker()
|
|
243
|
+
if (!ns.length) return ''
|
|
244
|
+
const opt = (v, label) => `<option value="${esc(v)}"${cur === v ? ' selected' : ''}>${esc(label)}</option>`
|
|
245
|
+
return opt('', 'Recorded voices') + ns.map(n => opt(n, 'Recorded: ' + n)).join('') + opt('-', 'Computer voice only')
|
|
246
|
+
}
|
|
247
|
+
function recHush() {
|
|
248
|
+
const a = recAudio
|
|
249
|
+
recAudio = null
|
|
250
|
+
if (a) { a.onended = a.onerror = null; try { a.pause() } catch (e) {} }
|
|
251
|
+
}
|
|
252
|
+
// Say a step: a person's recording when it has one, else the computer voice.
|
|
253
|
+
function walkSayStep(st, isCurrent, done) {
|
|
254
|
+
const ns = recNames(st), want = recSpeaker()
|
|
255
|
+
if (!walkPrefs().on || want === '-' || !ns.length) { walkSay(st.say, isCurrent, done); return }
|
|
256
|
+
try { if (window.speechSynthesis) speechSynthesis.cancel() } catch (e) {}
|
|
257
|
+
recHush()
|
|
258
|
+
const a = new Audio(st.voices[ns.includes(want) ? want : ns[0]].audio)
|
|
259
|
+
recAudio = a
|
|
260
|
+
let out = false
|
|
261
|
+
const fin = ok => {
|
|
262
|
+
if (out || recAudio !== a) return
|
|
263
|
+
out = true; recAudio = null
|
|
264
|
+
if (!isCurrent()) return
|
|
265
|
+
if (ok) done(); else walkSay(st.say, isCurrent, done)
|
|
266
|
+
}
|
|
267
|
+
a.onended = () => fin(true)
|
|
268
|
+
a.onerror = () => fin(false)
|
|
269
|
+
const p = a.play()
|
|
270
|
+
if (p && p.catch) p.catch(() => fin(false))
|
|
271
|
+
}
|
|
272
|
+
|
|
224
273
|
function walkBar(item) {
|
|
225
274
|
const g = graphModel(item)
|
|
226
275
|
const ws = walkList(g)
|
|
@@ -239,6 +288,7 @@
|
|
|
239
288
|
<button class="walk-next" style="${b}display:none" title="Next step (→)">▶</button>
|
|
240
289
|
<span class="walk-count" style="display:none;font-size:12px;color:#64748b;min-width:70px;text-align:center"></span>
|
|
241
290
|
<button class="walk-voice" style="${b}display:none" title="Read the words aloud, or show them only"></button>
|
|
291
|
+
<select class="walk-speaker" style="font-size:12px;max-width:170px;display:none" title="Whose voice reads the walk (kept in this browser)"></select>
|
|
242
292
|
<button class="walk-close" style="${b}display:none" title="Stop the walk (Esc)">■ Stop</button>
|
|
243
293
|
</div>
|
|
244
294
|
<div class="walk-say" aria-live="polite" style="display:none;margin:8px 4px 0;padding:10px 12px;background:#fff;border:1px solid #c7d2fe;border-radius:8px;font-size:17px;line-height:1.45;color:#1e1b4b"></div>
|
|
@@ -364,7 +414,7 @@
|
|
|
364
414
|
// so a step that was skipped or stopped can never move the walk on.
|
|
365
415
|
const wk = { w: null, i: 0, playing: false, token: 0, heard: null }
|
|
366
416
|
const curWalk = () => { const g = graphModel(item); return g && wk.w !== null ? walkList(g)[wk.w] : null }
|
|
367
|
-
const stopVoice = () => { wk.token++; try { if (window.speechSynthesis) speechSynthesis.cancel() } catch (e) {} }
|
|
417
|
+
const stopVoice = () => { wk.token++; recHush(); try { if (window.speechSynthesis) speechSynthesis.cancel() } catch (e) {} }
|
|
368
418
|
const walkUI = note => {
|
|
369
419
|
const walk = curWalk(), open = !!walk
|
|
370
420
|
if (note) wk.heard = wk.i; else if (wk.playing) wk.heard = null
|
|
@@ -374,6 +424,9 @@
|
|
|
374
424
|
$bar.find('.walk-say').toggle(open)
|
|
375
425
|
$bar.find('.walk-play').text(wk.playing ? '❚❚' : '▶')
|
|
376
426
|
$bar.find('.walk-voice').text(walkPrefs().on ? '🔈 Voice on' : '🔇 Voice off')
|
|
427
|
+
const so = open ? recSpeakerOptions(walk) : '', $sp = $bar.find('.walk-speaker')
|
|
428
|
+
if ($sp.data('opts') !== so) $sp.html(so).data('opts', so)
|
|
429
|
+
$sp.toggle(!!so)
|
|
377
430
|
$bar.find('.walk-count').text(open ? 'Step ' + (wk.i + 1) + ' of ' + walk.steps.length : '')
|
|
378
431
|
$bar.find('.walk-note').text(note || (open && !wk.playing ? 'Space plays · ← → step · Esc stops' : ''))
|
|
379
432
|
}
|
|
@@ -391,7 +444,7 @@
|
|
|
391
444
|
const walkSpeak = () => {
|
|
392
445
|
const tok = ++wk.token, walk = curWalk(), st = walk && walk.steps[wk.i]
|
|
393
446
|
if (!st) return
|
|
394
|
-
|
|
447
|
+
walkSayStep(st, () => tok === wk.token && activeWalk === wk, () => {
|
|
395
448
|
const last = wk.i >= walk.steps.length - 1
|
|
396
449
|
if (st.ask || last) { wk.playing = false; walkUI(last && !st.ask ? 'End of the walk. ← to go back, Esc to stop.' : 'Over to you. Press Space or ▶ to go on.'); return }
|
|
397
450
|
setTimeout(() => { if (tok === wk.token && wk.playing) walkShow(wk.i + 1) }, 800)
|
|
@@ -433,6 +486,7 @@
|
|
|
433
486
|
$item.on('click.rcnwalk', '.walk-prev', () => walkShow(wk.i - 1))
|
|
434
487
|
$item.on('click.rcnwalk', '.walk-next', () => walkShow(wk.i + 1))
|
|
435
488
|
$item.on('click.rcnwalk', '.walk-close', () => walkEnd())
|
|
489
|
+
$item.on('change.rcnwalk', '.walk-speaker', e => { recSetSpeaker(e.target.value); if (wk.playing) walkSpeak() })
|
|
436
490
|
$item.on('click.rcnwalk', '.walk-voice', () => {
|
|
437
491
|
const on = !walkPrefs().on
|
|
438
492
|
try { localStorage.setItem('rcnWalkVoice', on ? 'on' : 'off') } catch (e) {}
|