wiki-plugin-rcnmap 0.4.2 → 0.4.3

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/README.md CHANGED
@@ -48,7 +48,7 @@ Like FedWiki's own Map plugin, this one combines maps across the lineup.
48
48
 
49
49
  A map can carry walks, written in the RCN Map (🚶 Walk, then ✎ Edit) and copied into the item: `item.map.walks = [{name, steps:[{say, points:[ids], ties:['from>to'], view?:{center:[lat,lng], zoom}, all?, ask?}]}]`. A lit tie lights its two ends.
50
50
 
51
- **Recorded voices** (0.4.2). A step may carry `voices`, people's own recordings of its words, made with ● Record in the RCN Map's walk editor: `{"<name>": {audio, say}}`. The walk plays a step's recording when it has one and the computer voice otherwise; a chooser in the walk bar picks whose, or the computer voice only (kept in `rcnWalkPrefs.speaker`). Recordings travel inside the walks, so Open in RCN Map and Save to wiki carry them.
51
+ **Recorded voices** (0.4.2). A step may carry `voices`, people's own recordings of its words, made with ● Record in the RCN Map's walk editor: `{"<name>": {audio, say}}`. The walk plays a step's recording when it has one and the computer voice otherwise; a chooser in the walk bar picks whose, or the computer voice only (kept in `rcnWalkPrefs.speaker`). Recordings travel inside the walks, so Open in RCN Map and Save to wiki carry them. On Save to wiki the plugin moves each recording into the site's assets, `assets/voice-files/<content hash>.webm`, uploaded once however often the page is saved, and the step keeps only the file's full address, so pages and their history never hold audio and a forked page still plays from the original site. If the upload fails (not signed in), the recording stays inside the walk until the next save that can upload it.
52
52
 
53
53
  - **▶ Walk** under the map plays it: each step flies the map to what it lights (or to its own `view`), draws the lit ties heavier, names the lit points when there are eight or fewer, fades the rest (points and ties already visited stay half lit) and reads the words aloud, shown as a caption too. An `ask` step stops for the group.
54
54
  - Space plays or pauses, ← → step, Esc or ■ Stop ends it and puts the map back.
package/client/rcnmap.js CHANGED
@@ -450,6 +450,57 @@
450
450
  })
451
451
  }
452
452
 
453
+ // Recordings live as files in this site's assets (assets/voice-files), one file per
454
+ // recording, named by its content. On save, any recording still held inside a walk
455
+ // (a data: URL from a walk editor) is uploaded there and replaced by the file's full
456
+ // address, so a page and its history keep only addresses, a recording is never
457
+ // stored twice, and a forked page still plays from the site that has the file.
458
+ // If the upload fails (not signed in, say) the recording stays inside the walk, so
459
+ // nothing is lost, and the next save that can upload moves it out.
460
+ const VOICE_FOLDER = 'voice-files'
461
+ function voiceSteps (walks) {
462
+ const out = []
463
+ for (const w of walks || []) for (const st of (w && w.steps) || []) {
464
+ for (const n of Object.keys((st && st.voices) || {})) {
465
+ const v = st.voices[n]
466
+ if (v && typeof v.audio === 'string' && /^data:audio\//.test(v.audio)) out.push(v)
467
+ }
468
+ }
469
+ return out
470
+ }
471
+ async function voiceFile (dataUrl) {
472
+ const blob = await (await fetch(dataUrl)).blob()
473
+ const sum = new Uint8Array(await crypto.subtle.digest('SHA-256', await blob.arrayBuffer()))
474
+ const hex = [...sum.slice(0, 12)].map(b => b.toString(16).padStart(2, '0')).join('')
475
+ const ext = /mp4|m4a|aac/.test(blob.type) ? 'm4a' : /wav/.test(blob.type) ? 'wav' : /ogg/.test(blob.type) ? 'ogg' : 'webm'
476
+ const name = hex + '.' + ext
477
+ const url = location.origin + '/assets/' + VOICE_FOLDER + '/' + name
478
+ const have = await fetch(url, { method: 'HEAD', cache: 'no-store' }).then(r => r.ok, () => false)
479
+ if (!have) {
480
+ const form = new FormData()
481
+ form.append('assets', VOICE_FOLDER) // before the file: the server reads the folder first
482
+ form.append('uploads[]', blob, name)
483
+ const r = await fetch('/plugin/assets/upload', { method: 'POST', body: form, credentials: 'same-origin' })
484
+ if (!r.ok) throw new Error(r.status === 401 ? 'sign in to this wiki to save recordings as files' : 'upload failed (' + r.status + ')')
485
+ }
486
+ return url
487
+ }
488
+ // Move every embedded recording in these walks to a file; resolves to a note
489
+ // for the reader when some could not be moved, else ''.
490
+ async function voicesToFiles (walks) {
491
+ const vs = voiceSteps(walks)
492
+ let failed = '', n = 0
493
+ for (const v of vs) {
494
+ try { v.audio = await voiceFile(v.audio); n++ } catch (e) { failed = e.message || String(e) }
495
+ }
496
+ if (n) console.log('voice files: ' + n + ' recording' + (n === 1 ? '' : 's') + ' now point to assets/' + VOICE_FOLDER + ' (each file is uploaded once)')
497
+ return failed ? 'Recordings were kept inside the page: ' + failed + '.' : ''
498
+ }
499
+ function voiceNotice ($item, msg) {
500
+ $item.find('.voice-note').remove()
501
+ if (msg) $item.append($('<div class="voice-note" style="font-size:12px;color:#92400e;padding:4px 0"></div>').text(msg))
502
+ }
503
+
453
504
  // Save-back from the full map. Only the window this page opened is heard,
454
505
  // and it saves into the item opened last. An item drawn from its text alone
455
506
  // gets a map of its own, so the walks have somewhere to live.
@@ -457,12 +508,15 @@
457
508
  if (!pending || !event.data || event.data.toolType !== 'rcnmap') return
458
509
  if (pending.win && event.source !== pending.win) return
459
510
  if (event.data.action !== 'saveMap' || !Array.isArray(event.data.walks)) return
460
- const { item, $item } = pending
461
- if (!item.map) item.map = model(item)
462
- if (event.data.walks.length) item.map.walks = event.data.walks
463
- else delete item.map.walks
464
- save($item, item)
465
- try { event.source.postMessage({ toolType: 'rcnmap', action: 'saved', page: ($item.parents('.page:first').data('data') || {}).title }, '*') } catch (e) {}
511
+ const { item, $item } = pending, src = event.source, walks = event.data.walks
512
+ voicesToFiles(walks).then(note => {
513
+ if (!item.map) item.map = model(item)
514
+ if (walks.length) item.map.walks = walks
515
+ else delete item.map.walks
516
+ save($item, item)
517
+ voiceNotice($item, note)
518
+ try { src.postMessage({ toolType: 'rcnmap', action: 'saved', page: ($item.parents('.page:first').data('data') || {}).title }, '*') } catch (e) {}
519
+ })
466
520
  }
467
521
 
468
522
  function loadLeaflet (done) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wiki-plugin-rcnmap",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "description": "Federated Wiki plugin: a small RCN map on a page that draws points and the ties between them; each point opens its page",
5
5
  "keywords": ["wiki", "federated wiki", "plugin", "rcn", "map"],
6
6
  "author": "RCN Network <rcn@relocalizecreativity.net>",