wiki-plugin-rcngraph 0.1.16 → 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.
- package/client/rcngraph.js +64 -6
- package/package.json +1 -1
package/client/rcngraph.js
CHANGED
|
@@ -517,6 +517,57 @@
|
|
|
517
517
|
})
|
|
518
518
|
}
|
|
519
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
|
+
|
|
520
571
|
function graphListener(event) {
|
|
521
572
|
if (!event.source || !event.source.opener) return
|
|
522
573
|
if (!event.data || event.data.toolType !== 'rcn-graph') return
|
|
@@ -533,12 +584,19 @@
|
|
|
533
584
|
}
|
|
534
585
|
case 'saveGraph': {
|
|
535
586
|
if (!pendingItem || !pending$item) break
|
|
536
|
-
pendingItem
|
|
537
|
-
|
|
538
|
-
const
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
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
|
+
})
|
|
542
600
|
break
|
|
543
601
|
}
|
|
544
602
|
case 'doInternalLink': {
|