wiki-plugin-rcngraph 0.1.15 → 0.1.17

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.
Files changed (2) hide show
  1. package/client/rcngraph.js +120 -8
  2. package/package.json +1 -1
@@ -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
- walkSay(st.say, () => tok === wk.token && activeWalk === wk, () => {
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) {}
@@ -463,6 +517,57 @@
463
517
  })
464
518
  }
465
519
 
520
+ // Recordings live as files in this site's assets (assets/voice-files), one file per
521
+ // recording, named by its content. On save, any recording still held inside a walk
522
+ // (a data: URL from a walk editor) is uploaded there and replaced by the file's full
523
+ // address, so a page and its history keep only addresses, a recording is never
524
+ // stored twice, and a forked page still plays from the site that has the file.
525
+ // If the upload fails (not signed in, say) the recording stays inside the walk, so
526
+ // nothing is lost, and the next save that can upload moves it out.
527
+ const VOICE_FOLDER = 'voice-files'
528
+ function voiceSteps(walks) {
529
+ const out = []
530
+ for (const w of walks || []) for (const st of (w && w.steps) || []) {
531
+ for (const n of Object.keys((st && st.voices) || {})) {
532
+ const v = st.voices[n]
533
+ if (v && typeof v.audio === 'string' && /^data:audio\//.test(v.audio)) out.push(v)
534
+ }
535
+ }
536
+ return out
537
+ }
538
+ async function voiceFile(dataUrl) {
539
+ const blob = await (await fetch(dataUrl)).blob()
540
+ const sum = new Uint8Array(await crypto.subtle.digest('SHA-256', await blob.arrayBuffer()))
541
+ const hex = [...sum.slice(0, 12)].map(b => b.toString(16).padStart(2, '0')).join('')
542
+ const ext = /mp4|m4a|aac/.test(blob.type) ? 'm4a' : /wav/.test(blob.type) ? 'wav' : /ogg/.test(blob.type) ? 'ogg' : 'webm'
543
+ const name = hex + '.' + ext
544
+ const url = location.origin + '/assets/' + VOICE_FOLDER + '/' + name
545
+ const have = await fetch(url, { method: 'HEAD', cache: 'no-store' }).then(r => r.ok, () => false)
546
+ if (!have) {
547
+ const form = new FormData()
548
+ form.append('assets', VOICE_FOLDER) // before the file: the server reads the folder first
549
+ form.append('uploads[]', blob, name)
550
+ const r = await fetch('/plugin/assets/upload', { method: 'POST', body: form, credentials: 'same-origin' })
551
+ if (!r.ok) throw new Error(r.status === 401 ? 'sign in to this wiki to save recordings as files' : 'upload failed (' + r.status + ')')
552
+ }
553
+ return url
554
+ }
555
+ // Move every embedded recording in these walks to a file; resolves to a note
556
+ // for the reader when some could not be moved, else ''.
557
+ async function voicesToFiles(walks) {
558
+ const vs = voiceSteps(walks)
559
+ let failed = '', n = 0
560
+ for (const v of vs) {
561
+ try { v.audio = await voiceFile(v.audio); n++ } catch (e) { failed = e.message || String(e) }
562
+ }
563
+ if (n) console.log('voice files: ' + n + ' recording' + (n === 1 ? '' : 's') + ' now point to assets/' + VOICE_FOLDER + ' (each file is uploaded once)')
564
+ return failed ? 'Recordings were kept inside the page: ' + failed + '.' : ''
565
+ }
566
+ function voiceNotice($item, msg) {
567
+ $item.find('.voice-note').remove()
568
+ if (msg) $item.append($('<div class="voice-note" style="font-size:12px;color:#92400e;padding:4px 0"></div>').text(msg))
569
+ }
570
+
466
571
  function graphListener(event) {
467
572
  if (!event.source || !event.source.opener) return
468
573
  if (!event.data || event.data.toolType !== 'rcn-graph') return
@@ -479,12 +584,19 @@
479
584
  }
480
585
  case 'saveGraph': {
481
586
  if (!pendingItem || !pending$item) break
482
- pendingItem.graphJSON = data.graphJSON
483
- pendingItem.svgString = data.svgString
484
- const $page = pending$item.parents('.page:first')
485
- wiki.pageHandler.put($page, { type: 'edit', id: pendingItem.id, item: pendingItem })
486
- renderContent(pending$item, pendingItem)
487
- bind(pending$item, pendingItem)
587
+ const item = pendingItem, $item = pending$item
588
+ let g = data.graphJSON
589
+ const asText = typeof g === 'string'
590
+ try { if (asText) g = JSON.parse(g) } catch (e) { g = null }
591
+ const moved = g && voiceSteps(g.walks).length ? voicesToFiles(g.walks) : Promise.resolve('')
592
+ moved.then(note => {
593
+ item.graphJSON = g ? (asText ? JSON.stringify(g) : g) : data.graphJSON
594
+ item.svgString = data.svgString
595
+ wiki.pageHandler.put($item.parents('.page:first'), { type: 'edit', id: item.id, item })
596
+ renderContent($item, item)
597
+ bind($item, item)
598
+ voiceNotice($item, note)
599
+ })
488
600
  break
489
601
  }
490
602
  case 'doInternalLink': {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wiki-plugin-rcngraph",
3
- "version": "0.1.15",
3
+ "version": "0.1.17",
4
4
  "description": "Federated Wiki - RCN Graph Plug-in",
5
5
  "keywords": [
6
6
  "wiki",