wiki-plugin-rcngraph 0.1.13 → 0.1.15

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.
@@ -91,18 +91,174 @@
91
91
  bar.find('.build-prev, .build-next').toggle(idx !== null)
92
92
  }
93
93
 
94
+ // ── Walk playback ──────────────────────────────────────────────────────
95
+ // A walk, written in the Graph Tool, is a list of steps: words to say, and
96
+ // what each step lights. The tool saves each step's lit node and edge ids, so
97
+ // playing it here is setting the opacity of the saved SVG's groups and
98
+ // speaking the words with the browser's own voice, shown as a caption too.
99
+ const WALK_GHOST = 0.13, WALK_SEEN = 0.42
100
+ let activeWalk = null // one walk speaks at a time on the whole lineup
101
+
102
+ function walkList(g) {
103
+ return g && Array.isArray(g.walks) ? g.walks.filter(w => w && Array.isArray(w.steps) && w.steps.length) : []
104
+ }
105
+
106
+ // null = the whole drawing. Files saved before lit existed fall back to the
107
+ // step's own nodes and the edges among them.
108
+ function stepLit(g, st) {
109
+ if (!st || st.all) return null
110
+ if (st.lit) return { nodes: new Set(st.lit.nodes.map(String)), edges: new Set(st.lit.edges.map(String)) }
111
+ const nodes = new Set((st.nodes || []).map(String))
112
+ if (st.loop) String(st.loop).split(',').forEach(id => nodes.add(id))
113
+ const edges = new Set(st.edges ? st.edges.map(String)
114
+ : g.edges.filter(e => nodes.has(String(e.src)) && nodes.has(String(e.tgt))).map(e => String(e.id)))
115
+ return { nodes, edges }
116
+ }
117
+
118
+ // Zoom the saved SVG towards what a step lights, so a narrow wiki column can
119
+ // still read it. Never closer than 2.5×, and the faded rest stays around it.
120
+ // els === null: back to the whole drawing.
121
+ function walkZoom(svg, els) {
122
+ if (!svg.__fullViewBox) svg.__fullViewBox = svg.getAttribute('viewBox') || ('0 0 ' + (svg.getAttribute('width') || 1000) + ' ' + (svg.getAttribute('height') || 600))
123
+ const full = svg.__fullViewBox.split(/[\s,]+/).map(Number)
124
+ let to = full
125
+ if (els && els.length) {
126
+ const inv = svg.getScreenCTM() && svg.getScreenCTM().inverse()
127
+ let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity
128
+ els.forEach(el => {
129
+ const r = el.getBoundingClientRect()
130
+ if (!inv || !r.width) return
131
+ ;[[r.left, r.top], [r.right, r.bottom]].forEach(([x, y]) => {
132
+ const pt = svg.createSVGPoint(); pt.x = x; pt.y = y
133
+ const q = pt.matrixTransform(inv)
134
+ x0 = Math.min(x0, q.x); y0 = Math.min(y0, q.y); x1 = Math.max(x1, q.x); y1 = Math.max(y1, q.y)
135
+ })
136
+ })
137
+ if (isFinite(x0)) {
138
+ const pad = 40, aspect = full[3] / full[2]
139
+ let w = Math.max(x1 - x0 + 2 * pad, full[2] / 2.5), h = Math.max(y1 - y0 + 2 * pad, full[3] / 2.5)
140
+ if (h / w > aspect) w = h / aspect; else h = w * aspect
141
+ w = Math.min(w, full[2]); h = Math.min(h, full[3])
142
+ let x = (x0 + x1) / 2 - w / 2, y = (y0 + y1) / 2 - h / 2
143
+ x = Math.max(full[0], Math.min(full[0] + full[2] - w, x)); y = Math.max(full[1], Math.min(full[1] + full[3] - h, y))
144
+ to = [x, y, w, h]
145
+ }
146
+ }
147
+ const from = (svg.getAttribute('viewBox') || svg.__fullViewBox).split(/[\s,]+/).map(Number)
148
+ const t0 = performance.now(), dur = 600, id = (svg.__zoomId = (svg.__zoomId || 0) + 1)
149
+ const tick = now => {
150
+ if (svg.__zoomId !== id) return
151
+ const k = Math.min(1, (now - t0) / dur), e = k < 0.5 ? 2 * k * k : 1 - Math.pow(-2 * k + 2, 2) / 2
152
+ svg.setAttribute('viewBox', from.map((v, j) => v + (to[j] - v) * e).join(' '))
153
+ if (k < 1) requestAnimationFrame(tick)
154
+ }
155
+ requestAnimationFrame(tick)
156
+ }
157
+
158
+ // i === null: no walk, everything back to normal.
159
+ function showWalkStep($item, g, walk, i) {
160
+ const svg = $item.find('svg').get(0)
161
+ if (!svg) return
162
+ svg.querySelectorAll('.rcn-walk-ring').forEach(el => el.remove())
163
+ $item.find('.graph-box').css('max-height', i === null ? '280px' : 'none')
164
+ if (i === null) {
165
+ g.nodes.forEach(n => { const el = groupFor(svg, 'node', n.id); if (el) el.style.opacity = '' })
166
+ g.edges.forEach(e => { const el = groupFor(svg, 'edge', e.id); if (el) el.style.opacity = '' })
167
+ walkZoom(svg, null)
168
+ return
169
+ }
170
+ const seen = { nodes: new Set(), edges: new Set() }
171
+ walk.steps.slice(0, i).forEach(st => {
172
+ const L = stepLit(g, st)
173
+ if (L) { L.nodes.forEach(id => seen.nodes.add(id)); L.edges.forEach(id => seen.edges.add(id)) }
174
+ })
175
+ const lit = stepLit(g, walk.steps[i])
176
+ const op = (kind, id) => !lit || lit[kind].has(id) ? '1' : seen[kind].has(id) ? String(WALK_SEEN) : String(WALK_GHOST)
177
+ g.nodes.forEach(n => {
178
+ const el = groupFor(svg, 'node', n.id)
179
+ if (!el) return
180
+ el.style.opacity = op('nodes', String(n.id))
181
+ })
182
+ g.edges.forEach(e => {
183
+ const el = groupFor(svg, 'edge', e.id)
184
+ if (el) el.style.opacity = op('edges', String(e.id))
185
+ })
186
+ walkZoom(svg, lit ? [...lit.nodes].map(id => groupFor(svg, 'node', id)).filter(Boolean) : null)
187
+ }
188
+
189
+ // Voice and speed are the listener's: kept in this browser, the same keys the Graph Tool uses.
190
+ function walkPrefs() {
191
+ let p = {}
192
+ try { p = JSON.parse(localStorage.getItem('rcnWalkPrefs') || '{}') } catch (e) {}
193
+ let on = true
194
+ try { on = localStorage.getItem('rcnWalkVoice') !== 'off' } catch (e) {}
195
+ return { voice: p.voice || '', rate: (+p.rate >= 0.6 && +p.rate <= 1.4) ? +p.rate : 0.95, on }
196
+ }
197
+ function walkVoice(name) {
198
+ const vs = window.speechSynthesis ? speechSynthesis.getVoices().filter(v => /^en/i.test(v.lang)) : []
199
+ return vs.find(v => v.name === name) || vs.find(v => /Enhanced|Premium|Natural|Neural/i.test(v.name)) ||
200
+ ['Samantha', 'Daniel', 'Karen', 'Moira', 'Google UK English Female', 'Google US English', 'Microsoft Aria']
201
+ .map(p => vs.find(v => v.name.indexOf(p) === 0)).find(Boolean) || vs[0] || null
202
+ }
203
+ // Say the words; done() runs once when they have been said. One utterance per
204
+ // sentence, because Chrome stops a long one after about fifteen seconds.
205
+ function walkSay(text, isCurrent, done) {
206
+ const prefs = walkPrefs(), words = (text || '').split(/\s+/).length
207
+ let fired = false
208
+ const after = () => { if (fired || !isCurrent()) return; fired = true; done() }
209
+ setTimeout(after, words * (prefs.on ? 650 : 420) + (prefs.on ? 6000 : 2500)) // if the voice never says it finished
210
+ if (!prefs.on || !window.speechSynthesis || !text) { setTimeout(after, text ? words * 400 + 2000 : 1200); return }
211
+ const v = walkVoice(prefs.voice)
212
+ const parts = text.match(/[^.!?]+[.!?]+["'”’)]*\s*|[^.!?]+$/g) || [text]
213
+ parts.forEach((s, k) => {
214
+ const u = new SpeechSynthesisUtterance(s.trim())
215
+ if (v) { u.voice = v; u.lang = v.lang }
216
+ u.rate = prefs.rate
217
+ if (k === parts.length - 1) u.onend = after
218
+ speechSynthesis.speak(u)
219
+ })
220
+ }
221
+
222
+ function walkBar(item) {
223
+ const g = graphModel(item)
224
+ const ws = walkList(g)
225
+ if (!ws.length) return ''
226
+ const b = 'cursor:pointer;color:' + BUILD_COLOR + ';'
227
+ const pick = ws.length > 1
228
+ ? `<select class="walk-pick" style="max-width:220px;font-size:12px" title="Which walk">${ws.map((w, i) => `<option value="${i}">${esc(w.name || 'Walk ' + (i + 1))} (${w.steps.length} steps)</option>`).join('')}</select>`
229
+ : `<span style="font-size:12px;color:#475569">${esc(ws[0].name || 'Walk')}</span>`
230
+ return `
231
+ <div class="walk-bar" tabindex="0" title="Hear and see this drawing explained one part at a time. Space plays or pauses, ← → step, Esc stops." style="padding:8px 0 0;outline:none;">
232
+ <div style="display:flex;gap:6px;justify-content:center;align-items:center;flex-wrap:wrap">
233
+ <button class="walk-start" style="${b}font-weight:600">▶ Walk</button>
234
+ ${pick}
235
+ <button class="walk-prev" style="${b}display:none" title="Back a step (←)">◀</button>
236
+ <button class="walk-play" style="${b}display:none" title="Play or pause (Space)">❚❚</button>
237
+ <button class="walk-next" style="${b}display:none" title="Next step (→)">▶</button>
238
+ <span class="walk-count" style="display:none;font-size:12px;color:#64748b;min-width:70px;text-align:center"></span>
239
+ <button class="walk-voice" style="${b}display:none" title="Read the words aloud, or show them only"></button>
240
+ <button class="walk-close" style="${b}display:none" title="Stop the walk (Esc)">■ Stop</button>
241
+ </div>
242
+ <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>
243
+ <div class="walk-note" style="font-size:11px;color:#64748b;text-align:center;margin-top:3px"></div>
244
+ </div>`
245
+ }
246
+
247
+ function esc(s) { return String(s).replace(/[&<>"]/g, c => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c])) }
248
+
94
249
  function renderContent($item, item) {
95
250
  $item.empty()
96
251
  if (item.svgString) {
97
252
  $item.append(`
98
253
  <div style="background:#f5f5f5;padding:8px;">
99
- <div style="overflow:auto;max-height:280px;border:1px solid #ddd;background:#fff;">
254
+ <div class="graph-box" style="overflow:auto;max-height:280px;border:1px solid #ddd;background:#fff;">
100
255
  ${item.svgString}
101
256
  </div>
102
257
  <div style="padding:6px 0 0;display:flex;gap:6px;justify-content:center;">
103
258
  <button class="edit-graph" style="cursor:pointer;">Edit in Graph Tool ↗</button>
104
259
  </div>
105
260
  ${buildBar(item)}
261
+ ${walkBar(item)}
106
262
  </div>
107
263
  `)
108
264
  } else {
@@ -172,8 +328,8 @@
172
328
  if (popup) popup.focus()
173
329
  })
174
330
  $item.on('dblclick', e => {
175
- // Stepping a build is quick clicking — never let it open the editor.
176
- if ($(e.target).closest('.build-bar').length) return
331
+ // Stepping a build or a walk is quick clicking — never let it open the editor.
332
+ if ($(e.target).closest('.build-bar, .walk-bar').length) return
177
333
  wiki.textEditor($item, item)
178
334
  })
179
335
  // Build playback. Namespaced and reset, because a save re-runs bind.
@@ -189,6 +345,7 @@
189
345
  $item.on('click.rcnbuild', '.build-play', () => {
190
346
  const g = graphModel(item)
191
347
  if (!g) return
348
+ if (wk.w !== null) walkEnd()
192
349
  buildIdx = buildIdx === null ? 1 : null
193
350
  showBuildStep($item, g, buildIdx)
194
351
  $item.find('.build-bar').trigger('focus')
@@ -201,6 +358,95 @@
201
358
  else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') { e.preventDefault(); e.stopPropagation(); step(-1) }
202
359
  else if (e.key === 'Escape') { e.preventDefault(); buildIdx = null; showBuildStep($item, graphModel(item), null) }
203
360
  })
361
+ // Walk playback. w === null: no walk open. token guards every voice callback,
362
+ // so a step that was skipped or stopped can never move the walk on.
363
+ const wk = { w: null, i: 0, playing: false, token: 0, heard: null }
364
+ const curWalk = () => { const g = graphModel(item); return g && wk.w !== null ? walkList(g)[wk.w] : null }
365
+ const stopVoice = () => { wk.token++; try { if (window.speechSynthesis) speechSynthesis.cancel() } catch (e) {} }
366
+ const walkUI = note => {
367
+ const walk = curWalk(), open = !!walk
368
+ if (note) wk.heard = wk.i; else if (wk.playing) wk.heard = null
369
+ const $bar = $item.find('.walk-bar')
370
+ $bar.find('.walk-prev, .walk-next, .walk-play, .walk-count, .walk-voice, .walk-close').toggle(open)
371
+ $bar.find('.walk-start, .walk-pick').toggle(!open)
372
+ $bar.find('.walk-say').toggle(open)
373
+ $bar.find('.walk-play').text(wk.playing ? '❚❚' : '▶')
374
+ $bar.find('.walk-voice').text(walkPrefs().on ? '🔈 Voice on' : '🔇 Voice off')
375
+ $bar.find('.walk-count').text(open ? 'Step ' + (wk.i + 1) + ' of ' + walk.steps.length : '')
376
+ $bar.find('.walk-note').text(note || (open && !wk.playing ? 'Space plays · ← → step · Esc stops' : ''))
377
+ }
378
+ const walkShow = i => {
379
+ const g = graphModel(item), walk = curWalk()
380
+ if (!walk) return
381
+ wk.i = Math.max(0, Math.min(walk.steps.length - 1, i))
382
+ stopVoice()
383
+ showWalkStep($item, g, walk, wk.i)
384
+ const st = walk.steps[wk.i]
385
+ $item.find('.walk-say').text(st.say || '').css({ color: st.ask ? '#7c2d12' : '#1e1b4b', fontStyle: st.ask ? 'italic' : 'normal' })
386
+ walkUI()
387
+ if (wk.playing) walkSpeak()
388
+ }
389
+ const walkSpeak = () => {
390
+ const tok = ++wk.token, walk = curWalk(), st = walk && walk.steps[wk.i]
391
+ if (!st) return
392
+ walkSay(st.say, () => tok === wk.token && activeWalk === wk, () => {
393
+ const last = wk.i >= walk.steps.length - 1
394
+ 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 }
395
+ setTimeout(() => { if (tok === wk.token && wk.playing) walkShow(wk.i + 1) }, 800)
396
+ })
397
+ }
398
+ const walkPause = () => { wk.playing = false; stopVoice(); walkUI() }
399
+ const walkToggle = () => {
400
+ if (wk.w === null) return
401
+ if (wk.playing) { walkPause(); return }
402
+ if (activeWalk && activeWalk !== wk && activeWalk.end) activeWalk.end()
403
+ activeWalk = wk
404
+ wk.playing = true
405
+ // Play after a step already heard (an ask step, say) goes on to the next.
406
+ if (wk.heard === wk.i && wk.i < curWalk().steps.length - 1) { walkShow(wk.i + 1); return }
407
+ walkUI(); walkSpeak()
408
+ }
409
+ const walkEnd = () => {
410
+ if (wk.w === null) return
411
+ wk.playing = false; stopVoice()
412
+ const g = graphModel(item)
413
+ if (g) showWalkStep($item, g, null, null)
414
+ wk.w = null
415
+ if (activeWalk === wk) activeWalk = null
416
+ walkUI()
417
+ }
418
+ wk.end = walkEnd
419
+ $item.off('.rcnwalk')
420
+ $item.on('click.rcnwalk', '.walk-start', () => {
421
+ const g = graphModel(item)
422
+ if (!g) return
423
+ if (buildIdx !== null) { buildIdx = null; showBuildStep($item, g, null) }
424
+ wk.w = +($item.find('.walk-pick').val() || 0)
425
+ wk.heard = null
426
+ walkShow(0)
427
+ walkToggle() // a click is a user gesture, so the browser will speak
428
+ $item.find('.walk-bar').trigger('focus')
429
+ })
430
+ $item.on('click.rcnwalk', '.walk-play', () => walkToggle())
431
+ $item.on('click.rcnwalk', '.walk-prev', () => walkShow(wk.i - 1))
432
+ $item.on('click.rcnwalk', '.walk-next', () => walkShow(wk.i + 1))
433
+ $item.on('click.rcnwalk', '.walk-close', () => walkEnd())
434
+ $item.on('click.rcnwalk', '.walk-voice', () => {
435
+ const on = !walkPrefs().on
436
+ try { localStorage.setItem('rcnWalkVoice', on ? 'on' : 'off') } catch (e) {}
437
+ if (!on) stopVoice()
438
+ if (wk.playing) walkSpeak()
439
+ walkUI()
440
+ })
441
+ // Clicking the drawing to look closer pauses the voice.
442
+ $item.on('pointerdown.rcnwalk', '.graph-box', () => { if (wk.playing) walkPause() })
443
+ $item.on('keydown.rcnwalk', '.walk-bar', e => {
444
+ if (wk.w === null || $(e.target).is('select')) return
445
+ if (e.key === ' ') { e.preventDefault(); e.stopPropagation(); walkToggle() }
446
+ else if (e.key === 'ArrowRight') { e.preventDefault(); e.stopPropagation(); walkShow(wk.i + 1) }
447
+ else if (e.key === 'ArrowLeft') { e.preventDefault(); e.stopPropagation(); walkShow(wk.i - 1) }
448
+ else if (e.key === 'Escape') { e.preventDefault(); walkEnd() }
449
+ })
204
450
  // Labels in the saved SVG are wrapped as <a class="internal" data-title=…>, but
205
451
  // two things stopped a click reaching the page: the <text> keeps the canvas's
206
452
  // pointer-events="none", so the click fell through to the box behind it; and
@@ -93,18 +93,174 @@
93
93
  bar.find('.build-prev, .build-next').toggle(idx !== null)
94
94
  }
95
95
 
96
+ // ── Walk playback ──────────────────────────────────────────────────────
97
+ // A walk, written in the Graph Tool, is a list of steps: words to say, and
98
+ // what each step lights. The tool saves each step's lit node and edge ids, so
99
+ // playing it here is setting the opacity of the saved SVG's groups and
100
+ // speaking the words with the browser's own voice, shown as a caption too.
101
+ const WALK_GHOST = 0.13, WALK_SEEN = 0.42
102
+ let activeWalk = null // one walk speaks at a time on the whole lineup
103
+
104
+ function walkList(g) {
105
+ return g && Array.isArray(g.walks) ? g.walks.filter(w => w && Array.isArray(w.steps) && w.steps.length) : []
106
+ }
107
+
108
+ // null = the whole drawing. Files saved before lit existed fall back to the
109
+ // step's own nodes and the edges among them.
110
+ function stepLit(g, st) {
111
+ if (!st || st.all) return null
112
+ if (st.lit) return { nodes: new Set(st.lit.nodes.map(String)), edges: new Set(st.lit.edges.map(String)) }
113
+ const nodes = new Set((st.nodes || []).map(String))
114
+ if (st.loop) String(st.loop).split(',').forEach(id => nodes.add(id))
115
+ const edges = new Set(st.edges ? st.edges.map(String)
116
+ : g.edges.filter(e => nodes.has(String(e.src)) && nodes.has(String(e.tgt))).map(e => String(e.id)))
117
+ return { nodes, edges }
118
+ }
119
+
120
+ // Zoom the saved SVG towards what a step lights, so a narrow wiki column can
121
+ // still read it. Never closer than 2.5×, and the faded rest stays around it.
122
+ // els === null: back to the whole drawing.
123
+ function walkZoom(svg, els) {
124
+ if (!svg.__fullViewBox) svg.__fullViewBox = svg.getAttribute('viewBox') || ('0 0 ' + (svg.getAttribute('width') || 1000) + ' ' + (svg.getAttribute('height') || 600))
125
+ const full = svg.__fullViewBox.split(/[\s,]+/).map(Number)
126
+ let to = full
127
+ if (els && els.length) {
128
+ const inv = svg.getScreenCTM() && svg.getScreenCTM().inverse()
129
+ let x0 = Infinity, y0 = Infinity, x1 = -Infinity, y1 = -Infinity
130
+ els.forEach(el => {
131
+ const r = el.getBoundingClientRect()
132
+ if (!inv || !r.width) return
133
+ ;[[r.left, r.top], [r.right, r.bottom]].forEach(([x, y]) => {
134
+ const pt = svg.createSVGPoint(); pt.x = x; pt.y = y
135
+ const q = pt.matrixTransform(inv)
136
+ x0 = Math.min(x0, q.x); y0 = Math.min(y0, q.y); x1 = Math.max(x1, q.x); y1 = Math.max(y1, q.y)
137
+ })
138
+ })
139
+ if (isFinite(x0)) {
140
+ const pad = 40, aspect = full[3] / full[2]
141
+ let w = Math.max(x1 - x0 + 2 * pad, full[2] / 2.5), h = Math.max(y1 - y0 + 2 * pad, full[3] / 2.5)
142
+ if (h / w > aspect) w = h / aspect; else h = w * aspect
143
+ w = Math.min(w, full[2]); h = Math.min(h, full[3])
144
+ let x = (x0 + x1) / 2 - w / 2, y = (y0 + y1) / 2 - h / 2
145
+ x = Math.max(full[0], Math.min(full[0] + full[2] - w, x)); y = Math.max(full[1], Math.min(full[1] + full[3] - h, y))
146
+ to = [x, y, w, h]
147
+ }
148
+ }
149
+ const from = (svg.getAttribute('viewBox') || svg.__fullViewBox).split(/[\s,]+/).map(Number)
150
+ const t0 = performance.now(), dur = 600, id = (svg.__zoomId = (svg.__zoomId || 0) + 1)
151
+ const tick = now => {
152
+ if (svg.__zoomId !== id) return
153
+ const k = Math.min(1, (now - t0) / dur), e = k < 0.5 ? 2 * k * k : 1 - Math.pow(-2 * k + 2, 2) / 2
154
+ svg.setAttribute('viewBox', from.map((v, j) => v + (to[j] - v) * e).join(' '))
155
+ if (k < 1) requestAnimationFrame(tick)
156
+ }
157
+ requestAnimationFrame(tick)
158
+ }
159
+
160
+ // i === null: no walk, everything back to normal.
161
+ function showWalkStep($item, g, walk, i) {
162
+ const svg = $item.find('svg').get(0)
163
+ if (!svg) return
164
+ svg.querySelectorAll('.rcn-walk-ring').forEach(el => el.remove())
165
+ $item.find('.graph-box').css('max-height', i === null ? '280px' : 'none')
166
+ if (i === null) {
167
+ g.nodes.forEach(n => { const el = groupFor(svg, 'node', n.id); if (el) el.style.opacity = '' })
168
+ g.edges.forEach(e => { const el = groupFor(svg, 'edge', e.id); if (el) el.style.opacity = '' })
169
+ walkZoom(svg, null)
170
+ return
171
+ }
172
+ const seen = { nodes: new Set(), edges: new Set() }
173
+ walk.steps.slice(0, i).forEach(st => {
174
+ const L = stepLit(g, st)
175
+ if (L) { L.nodes.forEach(id => seen.nodes.add(id)); L.edges.forEach(id => seen.edges.add(id)) }
176
+ })
177
+ const lit = stepLit(g, walk.steps[i])
178
+ const op = (kind, id) => !lit || lit[kind].has(id) ? '1' : seen[kind].has(id) ? String(WALK_SEEN) : String(WALK_GHOST)
179
+ g.nodes.forEach(n => {
180
+ const el = groupFor(svg, 'node', n.id)
181
+ if (!el) return
182
+ el.style.opacity = op('nodes', String(n.id))
183
+ })
184
+ g.edges.forEach(e => {
185
+ const el = groupFor(svg, 'edge', e.id)
186
+ if (el) el.style.opacity = op('edges', String(e.id))
187
+ })
188
+ walkZoom(svg, lit ? [...lit.nodes].map(id => groupFor(svg, 'node', id)).filter(Boolean) : null)
189
+ }
190
+
191
+ // Voice and speed are the listener's: kept in this browser, the same keys the Graph Tool uses.
192
+ function walkPrefs() {
193
+ let p = {}
194
+ try { p = JSON.parse(localStorage.getItem('rcnWalkPrefs') || '{}') } catch (e) {}
195
+ let on = true
196
+ try { on = localStorage.getItem('rcnWalkVoice') !== 'off' } catch (e) {}
197
+ return { voice: p.voice || '', rate: (+p.rate >= 0.6 && +p.rate <= 1.4) ? +p.rate : 0.95, on }
198
+ }
199
+ function walkVoice(name) {
200
+ const vs = window.speechSynthesis ? speechSynthesis.getVoices().filter(v => /^en/i.test(v.lang)) : []
201
+ return vs.find(v => v.name === name) || vs.find(v => /Enhanced|Premium|Natural|Neural/i.test(v.name)) ||
202
+ ['Samantha', 'Daniel', 'Karen', 'Moira', 'Google UK English Female', 'Google US English', 'Microsoft Aria']
203
+ .map(p => vs.find(v => v.name.indexOf(p) === 0)).find(Boolean) || vs[0] || null
204
+ }
205
+ // Say the words; done() runs once when they have been said. One utterance per
206
+ // sentence, because Chrome stops a long one after about fifteen seconds.
207
+ function walkSay(text, isCurrent, done) {
208
+ const prefs = walkPrefs(), words = (text || '').split(/\s+/).length
209
+ let fired = false
210
+ const after = () => { if (fired || !isCurrent()) return; fired = true; done() }
211
+ setTimeout(after, words * (prefs.on ? 650 : 420) + (prefs.on ? 6000 : 2500)) // if the voice never says it finished
212
+ if (!prefs.on || !window.speechSynthesis || !text) { setTimeout(after, text ? words * 400 + 2000 : 1200); return }
213
+ const v = walkVoice(prefs.voice)
214
+ const parts = text.match(/[^.!?]+[.!?]+["'”’)]*\s*|[^.!?]+$/g) || [text]
215
+ parts.forEach((s, k) => {
216
+ const u = new SpeechSynthesisUtterance(s.trim())
217
+ if (v) { u.voice = v; u.lang = v.lang }
218
+ u.rate = prefs.rate
219
+ if (k === parts.length - 1) u.onend = after
220
+ speechSynthesis.speak(u)
221
+ })
222
+ }
223
+
224
+ function walkBar(item) {
225
+ const g = graphModel(item)
226
+ const ws = walkList(g)
227
+ if (!ws.length) return ''
228
+ const b = 'cursor:pointer;color:' + BUILD_COLOR + ';'
229
+ const pick = ws.length > 1
230
+ ? `<select class="walk-pick" style="max-width:220px;font-size:12px" title="Which walk">${ws.map((w, i) => `<option value="${i}">${esc(w.name || 'Walk ' + (i + 1))} (${w.steps.length} steps)</option>`).join('')}</select>`
231
+ : `<span style="font-size:12px;color:#475569">${esc(ws[0].name || 'Walk')}</span>`
232
+ return `
233
+ <div class="walk-bar" tabindex="0" title="Hear and see this drawing explained one part at a time. Space plays or pauses, ← → step, Esc stops." style="padding:8px 0 0;outline:none;">
234
+ <div style="display:flex;gap:6px;justify-content:center;align-items:center;flex-wrap:wrap">
235
+ <button class="walk-start" style="${b}font-weight:600">▶ Walk</button>
236
+ ${pick}
237
+ <button class="walk-prev" style="${b}display:none" title="Back a step (←)">◀</button>
238
+ <button class="walk-play" style="${b}display:none" title="Play or pause (Space)">❚❚</button>
239
+ <button class="walk-next" style="${b}display:none" title="Next step (→)">▶</button>
240
+ <span class="walk-count" style="display:none;font-size:12px;color:#64748b;min-width:70px;text-align:center"></span>
241
+ <button class="walk-voice" style="${b}display:none" title="Read the words aloud, or show them only"></button>
242
+ <button class="walk-close" style="${b}display:none" title="Stop the walk (Esc)">■ Stop</button>
243
+ </div>
244
+ <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>
245
+ <div class="walk-note" style="font-size:11px;color:#64748b;text-align:center;margin-top:3px"></div>
246
+ </div>`
247
+ }
248
+
249
+ function esc(s) { return String(s).replace(/[&<>"]/g, c => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c])) }
250
+
96
251
  function renderContent($item, item) {
97
252
  $item.empty()
98
253
  if (item.svgString) {
99
254
  $item.append(`
100
255
  <div style="background:#f5f5f5;padding:8px;">
101
- <div style="overflow:auto;max-height:280px;border:1px solid #ddd;background:#fff;">
256
+ <div class="graph-box" style="overflow:auto;max-height:280px;border:1px solid #ddd;background:#fff;">
102
257
  ${item.svgString}
103
258
  </div>
104
259
  <div style="padding:6px 0 0;display:flex;gap:6px;justify-content:center;">
105
260
  <button class="edit-graph" style="cursor:pointer;">Edit in Graph Tool ↗</button>
106
261
  </div>
107
262
  ${buildBar(item)}
263
+ ${walkBar(item)}
108
264
  </div>
109
265
  `)
110
266
  } else {
@@ -174,8 +330,8 @@
174
330
  if (popup) popup.focus()
175
331
  })
176
332
  $item.on('dblclick', e => {
177
- // Stepping a build is quick clicking — never let it open the editor.
178
- if ($(e.target).closest('.build-bar').length) return
333
+ // Stepping a build or a walk is quick clicking — never let it open the editor.
334
+ if ($(e.target).closest('.build-bar, .walk-bar').length) return
179
335
  wiki.textEditor($item, item)
180
336
  })
181
337
  // Build playback. Namespaced and reset, because a save re-runs bind.
@@ -191,6 +347,7 @@
191
347
  $item.on('click.rcnbuild', '.build-play', () => {
192
348
  const g = graphModel(item)
193
349
  if (!g) return
350
+ if (wk.w !== null) walkEnd()
194
351
  buildIdx = buildIdx === null ? 1 : null
195
352
  showBuildStep($item, g, buildIdx)
196
353
  $item.find('.build-bar').trigger('focus')
@@ -203,6 +360,95 @@
203
360
  else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') { e.preventDefault(); e.stopPropagation(); step(-1) }
204
361
  else if (e.key === 'Escape') { e.preventDefault(); buildIdx = null; showBuildStep($item, graphModel(item), null) }
205
362
  })
363
+ // Walk playback. w === null: no walk open. token guards every voice callback,
364
+ // so a step that was skipped or stopped can never move the walk on.
365
+ const wk = { w: null, i: 0, playing: false, token: 0, heard: null }
366
+ 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) {} }
368
+ const walkUI = note => {
369
+ const walk = curWalk(), open = !!walk
370
+ if (note) wk.heard = wk.i; else if (wk.playing) wk.heard = null
371
+ const $bar = $item.find('.walk-bar')
372
+ $bar.find('.walk-prev, .walk-next, .walk-play, .walk-count, .walk-voice, .walk-close').toggle(open)
373
+ $bar.find('.walk-start, .walk-pick').toggle(!open)
374
+ $bar.find('.walk-say').toggle(open)
375
+ $bar.find('.walk-play').text(wk.playing ? '❚❚' : '▶')
376
+ $bar.find('.walk-voice').text(walkPrefs().on ? '🔈 Voice on' : '🔇 Voice off')
377
+ $bar.find('.walk-count').text(open ? 'Step ' + (wk.i + 1) + ' of ' + walk.steps.length : '')
378
+ $bar.find('.walk-note').text(note || (open && !wk.playing ? 'Space plays · ← → step · Esc stops' : ''))
379
+ }
380
+ const walkShow = i => {
381
+ const g = graphModel(item), walk = curWalk()
382
+ if (!walk) return
383
+ wk.i = Math.max(0, Math.min(walk.steps.length - 1, i))
384
+ stopVoice()
385
+ showWalkStep($item, g, walk, wk.i)
386
+ const st = walk.steps[wk.i]
387
+ $item.find('.walk-say').text(st.say || '').css({ color: st.ask ? '#7c2d12' : '#1e1b4b', fontStyle: st.ask ? 'italic' : 'normal' })
388
+ walkUI()
389
+ if (wk.playing) walkSpeak()
390
+ }
391
+ const walkSpeak = () => {
392
+ const tok = ++wk.token, walk = curWalk(), st = walk && walk.steps[wk.i]
393
+ if (!st) return
394
+ walkSay(st.say, () => tok === wk.token && activeWalk === wk, () => {
395
+ const last = wk.i >= walk.steps.length - 1
396
+ 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
+ setTimeout(() => { if (tok === wk.token && wk.playing) walkShow(wk.i + 1) }, 800)
398
+ })
399
+ }
400
+ const walkPause = () => { wk.playing = false; stopVoice(); walkUI() }
401
+ const walkToggle = () => {
402
+ if (wk.w === null) return
403
+ if (wk.playing) { walkPause(); return }
404
+ if (activeWalk && activeWalk !== wk && activeWalk.end) activeWalk.end()
405
+ activeWalk = wk
406
+ wk.playing = true
407
+ // Play after a step already heard (an ask step, say) goes on to the next.
408
+ if (wk.heard === wk.i && wk.i < curWalk().steps.length - 1) { walkShow(wk.i + 1); return }
409
+ walkUI(); walkSpeak()
410
+ }
411
+ const walkEnd = () => {
412
+ if (wk.w === null) return
413
+ wk.playing = false; stopVoice()
414
+ const g = graphModel(item)
415
+ if (g) showWalkStep($item, g, null, null)
416
+ wk.w = null
417
+ if (activeWalk === wk) activeWalk = null
418
+ walkUI()
419
+ }
420
+ wk.end = walkEnd
421
+ $item.off('.rcnwalk')
422
+ $item.on('click.rcnwalk', '.walk-start', () => {
423
+ const g = graphModel(item)
424
+ if (!g) return
425
+ if (buildIdx !== null) { buildIdx = null; showBuildStep($item, g, null) }
426
+ wk.w = +($item.find('.walk-pick').val() || 0)
427
+ wk.heard = null
428
+ walkShow(0)
429
+ walkToggle() // a click is a user gesture, so the browser will speak
430
+ $item.find('.walk-bar').trigger('focus')
431
+ })
432
+ $item.on('click.rcnwalk', '.walk-play', () => walkToggle())
433
+ $item.on('click.rcnwalk', '.walk-prev', () => walkShow(wk.i - 1))
434
+ $item.on('click.rcnwalk', '.walk-next', () => walkShow(wk.i + 1))
435
+ $item.on('click.rcnwalk', '.walk-close', () => walkEnd())
436
+ $item.on('click.rcnwalk', '.walk-voice', () => {
437
+ const on = !walkPrefs().on
438
+ try { localStorage.setItem('rcnWalkVoice', on ? 'on' : 'off') } catch (e) {}
439
+ if (!on) stopVoice()
440
+ if (wk.playing) walkSpeak()
441
+ walkUI()
442
+ })
443
+ // Clicking the drawing to look closer pauses the voice.
444
+ $item.on('pointerdown.rcnwalk', '.graph-box', () => { if (wk.playing) walkPause() })
445
+ $item.on('keydown.rcnwalk', '.walk-bar', e => {
446
+ if (wk.w === null || $(e.target).is('select')) return
447
+ if (e.key === ' ') { e.preventDefault(); e.stopPropagation(); walkToggle() }
448
+ else if (e.key === 'ArrowRight') { e.preventDefault(); e.stopPropagation(); walkShow(wk.i + 1) }
449
+ else if (e.key === 'ArrowLeft') { e.preventDefault(); e.stopPropagation(); walkShow(wk.i - 1) }
450
+ else if (e.key === 'Escape') { e.preventDefault(); walkEnd() }
451
+ })
206
452
  // Labels in the saved SVG are wrapped as <a class="internal" data-title=…>, but
207
453
  // two things stopped a click reaching the page: the <text> keeps the canvas's
208
454
  // pointer-events="none", so the click fell through to the box behind it; and
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wiki-plugin-rcngraph",
3
- "version": "0.1.13",
3
+ "version": "0.1.15",
4
4
  "description": "Federated Wiki - RCN Graph Plug-in",
5
5
  "keywords": [
6
6
  "wiki",
@@ -26,6 +26,11 @@
26
26
  "id": "b7d1e9c3a5f20814",
27
27
  "text": "Builds: a diagram made in the Graph Tool’s Build mode carries an order for its nodes. The item then shows a Play build button that reveals the diagram right here on the page, step by step: ◀ and ▶ or the arrow keys move through it, each edge appears once both its nodes are on screen, and what a step just brought in is ringed in indigo. Show all puts the whole diagram back. The Graph Tool’s live build, where the room decides the order, runs in the tool itself."
28
28
  },
29
+ {
30
+ "type": "paragraph",
31
+ "id": "8e7e435485d56e9e",
32
+ "text": "Walks: a diagram can also carry walks, written in the Graph Tool (press Walk there to play, edit or record one). A walk is a list of steps, each with a sentence or two and the part of the diagram it is about. The item shows a ▶ Walk button: each step lights its part, fades the rest, zooms in so a narrow column can read it, and says the words aloud with the browser’s own voice while showing them as a caption. Space pauses, ◀ ▶ or the arrow keys step, Esc stops, and clicking the drawing pauses so you can look. A walk stops at its questions so a group can talk. Voice off shows the words only. The voice and its speed are chosen in the Graph Tool and remembered by your browser."
33
+ },
29
34
  {
30
35
  "type": "rcngraph",
31
36
  "id": "8d4e2a7f1c9b3065",
@@ -34,7 +39,7 @@
34
39
  {
35
40
  "type": "paragraph",
36
41
  "id": "4b6f8e3d9c2a1705",
37
- "text": "Developed by Marc Pierson with Claude Sonnet 4.6 (Anthropic) for the ReLocalize Creativity Network. Build playback added October 2026 with Claude Opus 5.5. Source: [https://github.com/marpie1/wiki-plugin-rcngraph GitHub]. Published on npm as wiki-plugin-rcngraph."
42
+ "text": "Developed by Marc Pierson with Claude Sonnet 4.6 (Anthropic) for the ReLocalize Creativity Network. Build playback and walks added October 2026 with Claude Opus 5.5. Source: [https://github.com/marpie1/wiki-plugin-rcngraph GitHub]. Published on npm as wiki-plugin-rcngraph."
38
43
  }
39
44
  ],
40
45
  "journal": [
@@ -160,6 +165,27 @@
160
165
  "text": "Developed by Marc Pierson with Claude Sonnet 4.6 (Anthropic) for the ReLocalize Creativity Network. Build playback added October 2026 with Claude Opus 5.5. Source: [https://github.com/marpie1/wiki-plugin-rcngraph GitHub]. Published on npm as wiki-plugin-rcngraph."
161
166
  },
162
167
  "date": 1791313722634
168
+ },
169
+ {
170
+ "type": "add",
171
+ "id": "8e7e435485d56e9e",
172
+ "item": {
173
+ "type": "paragraph",
174
+ "id": "8e7e435485d56e9e",
175
+ "text": "Walks: a diagram can also carry walks, written in the Graph Tool (press Walk there to play, edit or record one). A walk is a list of steps, each with a sentence or two and the part of the diagram it is about. The item shows a ▶ Walk button: each step lights its part, fades the rest, zooms in so a narrow column can read it, and says the words aloud with the browser’s own voice while showing them as a caption. Space pauses, ◀ ▶ or the arrow keys step, Esc stops, and clicking the drawing pauses so you can look. A walk stops at its questions so a group can talk. Voice off shows the words only. The voice and its speed are chosen in the Graph Tool and remembered by your browser."
176
+ },
177
+ "after": "b7d1e9c3a5f20814",
178
+ "date": 1792602075757
179
+ },
180
+ {
181
+ "type": "edit",
182
+ "id": "4b6f8e3d9c2a1705",
183
+ "item": {
184
+ "type": "paragraph",
185
+ "id": "4b6f8e3d9c2a1705",
186
+ "text": "Developed by Marc Pierson with Claude Sonnet 4.6 (Anthropic) for the ReLocalize Creativity Network. Build playback and walks added October 2026 with Claude Opus 5.5. Source: [https://github.com/marpie1/wiki-plugin-rcngraph GitHub]. Published on npm as wiki-plugin-rcngraph."
187
+ },
188
+ "date": 1792602075758
163
189
  }
164
190
  ]
165
191
  }