wiki-plugin-rcngraph 0.1.13 → 0.1.14

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,185 @@
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
+ // A ring on what this step brings in for the first time.
182
+ if (lit && lit.nodes.has(String(n.id)) && !seen.nodes.has(String(n.id)) && n.w && n.h) {
183
+ const r = document.createElementNS('http://www.w3.org/2000/svg', 'ellipse')
184
+ r.setAttribute('class', 'rcn-walk-ring')
185
+ r.setAttribute('cx', '0'); r.setAttribute('cy', '0')
186
+ r.setAttribute('rx', String(n.w / 2 + 9)); r.setAttribute('ry', String(n.h / 2 + 9))
187
+ r.setAttribute('fill', 'none'); r.setAttribute('stroke', BUILD_COLOR)
188
+ r.setAttribute('stroke-width', '3'); r.setAttribute('opacity', '.8')
189
+ r.setAttribute('pointer-events', 'none')
190
+ el.appendChild(r)
191
+ }
192
+ })
193
+ g.edges.forEach(e => {
194
+ const el = groupFor(svg, 'edge', e.id)
195
+ if (el) el.style.opacity = op('edges', String(e.id))
196
+ })
197
+ walkZoom(svg, lit ? [...lit.nodes].map(id => groupFor(svg, 'node', id)).filter(Boolean) : null)
198
+ }
199
+
200
+ // Voice and speed are the listener's: kept in this browser, the same keys the Graph Tool uses.
201
+ function walkPrefs() {
202
+ let p = {}
203
+ try { p = JSON.parse(localStorage.getItem('rcnWalkPrefs') || '{}') } catch (e) {}
204
+ let on = true
205
+ try { on = localStorage.getItem('rcnWalkVoice') !== 'off' } catch (e) {}
206
+ return { voice: p.voice || '', rate: (+p.rate >= 0.6 && +p.rate <= 1.4) ? +p.rate : 0.95, on }
207
+ }
208
+ function walkVoice(name) {
209
+ const vs = window.speechSynthesis ? speechSynthesis.getVoices().filter(v => /^en/i.test(v.lang)) : []
210
+ return vs.find(v => v.name === name) || vs.find(v => /Enhanced|Premium|Natural|Neural/i.test(v.name)) ||
211
+ ['Samantha', 'Daniel', 'Karen', 'Moira', 'Google UK English Female', 'Google US English', 'Microsoft Aria']
212
+ .map(p => vs.find(v => v.name.indexOf(p) === 0)).find(Boolean) || vs[0] || null
213
+ }
214
+ // Say the words; done() runs once when they have been said. One utterance per
215
+ // sentence, because Chrome stops a long one after about fifteen seconds.
216
+ function walkSay(text, isCurrent, done) {
217
+ const prefs = walkPrefs(), words = (text || '').split(/\s+/).length
218
+ let fired = false
219
+ const after = () => { if (fired || !isCurrent()) return; fired = true; done() }
220
+ setTimeout(after, words * (prefs.on ? 650 : 420) + (prefs.on ? 6000 : 2500)) // if the voice never says it finished
221
+ if (!prefs.on || !window.speechSynthesis || !text) { setTimeout(after, text ? words * 400 + 2000 : 1200); return }
222
+ const v = walkVoice(prefs.voice)
223
+ const parts = text.match(/[^.!?]+[.!?]+["'”’)]*\s*|[^.!?]+$/g) || [text]
224
+ parts.forEach((s, k) => {
225
+ const u = new SpeechSynthesisUtterance(s.trim())
226
+ if (v) { u.voice = v; u.lang = v.lang }
227
+ u.rate = prefs.rate
228
+ if (k === parts.length - 1) u.onend = after
229
+ speechSynthesis.speak(u)
230
+ })
231
+ }
232
+
233
+ function walkBar(item) {
234
+ const g = graphModel(item)
235
+ const ws = walkList(g)
236
+ if (!ws.length) return ''
237
+ const b = 'cursor:pointer;color:' + BUILD_COLOR + ';'
238
+ const pick = ws.length > 1
239
+ ? `<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>`
240
+ : `<span style="font-size:12px;color:#475569">${esc(ws[0].name || 'Walk')}</span>`
241
+ return `
242
+ <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;">
243
+ <div style="display:flex;gap:6px;justify-content:center;align-items:center;flex-wrap:wrap">
244
+ <button class="walk-start" style="${b}font-weight:600">▶ Walk</button>
245
+ ${pick}
246
+ <button class="walk-prev" style="${b}display:none" title="Back a step (←)">◀</button>
247
+ <button class="walk-play" style="${b}display:none" title="Play or pause (Space)">❚❚</button>
248
+ <button class="walk-next" style="${b}display:none" title="Next step (→)">▶</button>
249
+ <span class="walk-count" style="display:none;font-size:12px;color:#64748b;min-width:70px;text-align:center"></span>
250
+ <button class="walk-voice" style="${b}display:none" title="Read the words aloud, or show them only"></button>
251
+ <button class="walk-close" style="${b}display:none" title="Stop the walk (Esc)">■ Stop</button>
252
+ </div>
253
+ <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>
254
+ <div class="walk-note" style="font-size:11px;color:#64748b;text-align:center;margin-top:3px"></div>
255
+ </div>`
256
+ }
257
+
258
+ function esc(s) { return String(s).replace(/[&<>"]/g, c => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c])) }
259
+
94
260
  function renderContent($item, item) {
95
261
  $item.empty()
96
262
  if (item.svgString) {
97
263
  $item.append(`
98
264
  <div style="background:#f5f5f5;padding:8px;">
99
- <div style="overflow:auto;max-height:280px;border:1px solid #ddd;background:#fff;">
265
+ <div class="graph-box" style="overflow:auto;max-height:280px;border:1px solid #ddd;background:#fff;">
100
266
  ${item.svgString}
101
267
  </div>
102
268
  <div style="padding:6px 0 0;display:flex;gap:6px;justify-content:center;">
103
269
  <button class="edit-graph" style="cursor:pointer;">Edit in Graph Tool ↗</button>
104
270
  </div>
105
271
  ${buildBar(item)}
272
+ ${walkBar(item)}
106
273
  </div>
107
274
  `)
108
275
  } else {
@@ -172,8 +339,8 @@
172
339
  if (popup) popup.focus()
173
340
  })
174
341
  $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
342
+ // Stepping a build or a walk is quick clicking — never let it open the editor.
343
+ if ($(e.target).closest('.build-bar, .walk-bar').length) return
177
344
  wiki.textEditor($item, item)
178
345
  })
179
346
  // Build playback. Namespaced and reset, because a save re-runs bind.
@@ -189,6 +356,7 @@
189
356
  $item.on('click.rcnbuild', '.build-play', () => {
190
357
  const g = graphModel(item)
191
358
  if (!g) return
359
+ if (wk.w !== null) walkEnd()
192
360
  buildIdx = buildIdx === null ? 1 : null
193
361
  showBuildStep($item, g, buildIdx)
194
362
  $item.find('.build-bar').trigger('focus')
@@ -201,6 +369,95 @@
201
369
  else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') { e.preventDefault(); e.stopPropagation(); step(-1) }
202
370
  else if (e.key === 'Escape') { e.preventDefault(); buildIdx = null; showBuildStep($item, graphModel(item), null) }
203
371
  })
372
+ // Walk playback. w === null: no walk open. token guards every voice callback,
373
+ // so a step that was skipped or stopped can never move the walk on.
374
+ const wk = { w: null, i: 0, playing: false, token: 0, heard: null }
375
+ const curWalk = () => { const g = graphModel(item); return g && wk.w !== null ? walkList(g)[wk.w] : null }
376
+ const stopVoice = () => { wk.token++; try { if (window.speechSynthesis) speechSynthesis.cancel() } catch (e) {} }
377
+ const walkUI = note => {
378
+ const walk = curWalk(), open = !!walk
379
+ if (note) wk.heard = wk.i; else if (wk.playing) wk.heard = null
380
+ const $bar = $item.find('.walk-bar')
381
+ $bar.find('.walk-prev, .walk-next, .walk-play, .walk-count, .walk-voice, .walk-close').toggle(open)
382
+ $bar.find('.walk-start, .walk-pick').toggle(!open)
383
+ $bar.find('.walk-say').toggle(open)
384
+ $bar.find('.walk-play').text(wk.playing ? '❚❚' : '▶')
385
+ $bar.find('.walk-voice').text(walkPrefs().on ? '🔈 Voice on' : '🔇 Voice off')
386
+ $bar.find('.walk-count').text(open ? 'Step ' + (wk.i + 1) + ' of ' + walk.steps.length : '')
387
+ $bar.find('.walk-note').text(note || (open && !wk.playing ? 'Space plays · ← → step · Esc stops' : ''))
388
+ }
389
+ const walkShow = i => {
390
+ const g = graphModel(item), walk = curWalk()
391
+ if (!walk) return
392
+ wk.i = Math.max(0, Math.min(walk.steps.length - 1, i))
393
+ stopVoice()
394
+ showWalkStep($item, g, walk, wk.i)
395
+ const st = walk.steps[wk.i]
396
+ $item.find('.walk-say').text(st.say || '').css({ color: st.ask ? '#7c2d12' : '#1e1b4b', fontStyle: st.ask ? 'italic' : 'normal' })
397
+ walkUI()
398
+ if (wk.playing) walkSpeak()
399
+ }
400
+ const walkSpeak = () => {
401
+ const tok = ++wk.token, walk = curWalk(), st = walk && walk.steps[wk.i]
402
+ if (!st) return
403
+ walkSay(st.say, () => tok === wk.token && activeWalk === wk, () => {
404
+ const last = wk.i >= walk.steps.length - 1
405
+ 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 }
406
+ setTimeout(() => { if (tok === wk.token && wk.playing) walkShow(wk.i + 1) }, 800)
407
+ })
408
+ }
409
+ const walkPause = () => { wk.playing = false; stopVoice(); walkUI() }
410
+ const walkToggle = () => {
411
+ if (wk.w === null) return
412
+ if (wk.playing) { walkPause(); return }
413
+ if (activeWalk && activeWalk !== wk && activeWalk.end) activeWalk.end()
414
+ activeWalk = wk
415
+ wk.playing = true
416
+ // Play after a step already heard (an ask step, say) goes on to the next.
417
+ if (wk.heard === wk.i && wk.i < curWalk().steps.length - 1) { walkShow(wk.i + 1); return }
418
+ walkUI(); walkSpeak()
419
+ }
420
+ const walkEnd = () => {
421
+ if (wk.w === null) return
422
+ wk.playing = false; stopVoice()
423
+ const g = graphModel(item)
424
+ if (g) showWalkStep($item, g, null, null)
425
+ wk.w = null
426
+ if (activeWalk === wk) activeWalk = null
427
+ walkUI()
428
+ }
429
+ wk.end = walkEnd
430
+ $item.off('.rcnwalk')
431
+ $item.on('click.rcnwalk', '.walk-start', () => {
432
+ const g = graphModel(item)
433
+ if (!g) return
434
+ if (buildIdx !== null) { buildIdx = null; showBuildStep($item, g, null) }
435
+ wk.w = +($item.find('.walk-pick').val() || 0)
436
+ wk.heard = null
437
+ walkShow(0)
438
+ walkToggle() // a click is a user gesture, so the browser will speak
439
+ $item.find('.walk-bar').trigger('focus')
440
+ })
441
+ $item.on('click.rcnwalk', '.walk-play', () => walkToggle())
442
+ $item.on('click.rcnwalk', '.walk-prev', () => walkShow(wk.i - 1))
443
+ $item.on('click.rcnwalk', '.walk-next', () => walkShow(wk.i + 1))
444
+ $item.on('click.rcnwalk', '.walk-close', () => walkEnd())
445
+ $item.on('click.rcnwalk', '.walk-voice', () => {
446
+ const on = !walkPrefs().on
447
+ try { localStorage.setItem('rcnWalkVoice', on ? 'on' : 'off') } catch (e) {}
448
+ if (!on) stopVoice()
449
+ if (wk.playing) walkSpeak()
450
+ walkUI()
451
+ })
452
+ // Clicking the drawing to look closer pauses the voice.
453
+ $item.on('pointerdown.rcnwalk', '.graph-box', () => { if (wk.playing) walkPause() })
454
+ $item.on('keydown.rcnwalk', '.walk-bar', e => {
455
+ if (wk.w === null || $(e.target).is('select')) return
456
+ if (e.key === ' ') { e.preventDefault(); e.stopPropagation(); walkToggle() }
457
+ else if (e.key === 'ArrowRight') { e.preventDefault(); e.stopPropagation(); walkShow(wk.i + 1) }
458
+ else if (e.key === 'ArrowLeft') { e.preventDefault(); e.stopPropagation(); walkShow(wk.i - 1) }
459
+ else if (e.key === 'Escape') { e.preventDefault(); walkEnd() }
460
+ })
204
461
  // Labels in the saved SVG are wrapped as <a class="internal" data-title=…>, but
205
462
  // two things stopped a click reaching the page: the <text> keeps the canvas's
206
463
  // pointer-events="none", so the click fell through to the box behind it; and
@@ -93,18 +93,185 @@
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
+ // A ring on what this step brings in for the first time.
184
+ if (lit && lit.nodes.has(String(n.id)) && !seen.nodes.has(String(n.id)) && n.w && n.h) {
185
+ const r = document.createElementNS('http://www.w3.org/2000/svg', 'ellipse')
186
+ r.setAttribute('class', 'rcn-walk-ring')
187
+ r.setAttribute('cx', '0'); r.setAttribute('cy', '0')
188
+ r.setAttribute('rx', String(n.w / 2 + 9)); r.setAttribute('ry', String(n.h / 2 + 9))
189
+ r.setAttribute('fill', 'none'); r.setAttribute('stroke', BUILD_COLOR)
190
+ r.setAttribute('stroke-width', '3'); r.setAttribute('opacity', '.8')
191
+ r.setAttribute('pointer-events', 'none')
192
+ el.appendChild(r)
193
+ }
194
+ })
195
+ g.edges.forEach(e => {
196
+ const el = groupFor(svg, 'edge', e.id)
197
+ if (el) el.style.opacity = op('edges', String(e.id))
198
+ })
199
+ walkZoom(svg, lit ? [...lit.nodes].map(id => groupFor(svg, 'node', id)).filter(Boolean) : null)
200
+ }
201
+
202
+ // Voice and speed are the listener's: kept in this browser, the same keys the Graph Tool uses.
203
+ function walkPrefs() {
204
+ let p = {}
205
+ try { p = JSON.parse(localStorage.getItem('rcnWalkPrefs') || '{}') } catch (e) {}
206
+ let on = true
207
+ try { on = localStorage.getItem('rcnWalkVoice') !== 'off' } catch (e) {}
208
+ return { voice: p.voice || '', rate: (+p.rate >= 0.6 && +p.rate <= 1.4) ? +p.rate : 0.95, on }
209
+ }
210
+ function walkVoice(name) {
211
+ const vs = window.speechSynthesis ? speechSynthesis.getVoices().filter(v => /^en/i.test(v.lang)) : []
212
+ return vs.find(v => v.name === name) || vs.find(v => /Enhanced|Premium|Natural|Neural/i.test(v.name)) ||
213
+ ['Samantha', 'Daniel', 'Karen', 'Moira', 'Google UK English Female', 'Google US English', 'Microsoft Aria']
214
+ .map(p => vs.find(v => v.name.indexOf(p) === 0)).find(Boolean) || vs[0] || null
215
+ }
216
+ // Say the words; done() runs once when they have been said. One utterance per
217
+ // sentence, because Chrome stops a long one after about fifteen seconds.
218
+ function walkSay(text, isCurrent, done) {
219
+ const prefs = walkPrefs(), words = (text || '').split(/\s+/).length
220
+ let fired = false
221
+ const after = () => { if (fired || !isCurrent()) return; fired = true; done() }
222
+ setTimeout(after, words * (prefs.on ? 650 : 420) + (prefs.on ? 6000 : 2500)) // if the voice never says it finished
223
+ if (!prefs.on || !window.speechSynthesis || !text) { setTimeout(after, text ? words * 400 + 2000 : 1200); return }
224
+ const v = walkVoice(prefs.voice)
225
+ const parts = text.match(/[^.!?]+[.!?]+["'”’)]*\s*|[^.!?]+$/g) || [text]
226
+ parts.forEach((s, k) => {
227
+ const u = new SpeechSynthesisUtterance(s.trim())
228
+ if (v) { u.voice = v; u.lang = v.lang }
229
+ u.rate = prefs.rate
230
+ if (k === parts.length - 1) u.onend = after
231
+ speechSynthesis.speak(u)
232
+ })
233
+ }
234
+
235
+ function walkBar(item) {
236
+ const g = graphModel(item)
237
+ const ws = walkList(g)
238
+ if (!ws.length) return ''
239
+ const b = 'cursor:pointer;color:' + BUILD_COLOR + ';'
240
+ const pick = ws.length > 1
241
+ ? `<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>`
242
+ : `<span style="font-size:12px;color:#475569">${esc(ws[0].name || 'Walk')}</span>`
243
+ return `
244
+ <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;">
245
+ <div style="display:flex;gap:6px;justify-content:center;align-items:center;flex-wrap:wrap">
246
+ <button class="walk-start" style="${b}font-weight:600">▶ Walk</button>
247
+ ${pick}
248
+ <button class="walk-prev" style="${b}display:none" title="Back a step (←)">◀</button>
249
+ <button class="walk-play" style="${b}display:none" title="Play or pause (Space)">❚❚</button>
250
+ <button class="walk-next" style="${b}display:none" title="Next step (→)">▶</button>
251
+ <span class="walk-count" style="display:none;font-size:12px;color:#64748b;min-width:70px;text-align:center"></span>
252
+ <button class="walk-voice" style="${b}display:none" title="Read the words aloud, or show them only"></button>
253
+ <button class="walk-close" style="${b}display:none" title="Stop the walk (Esc)">■ Stop</button>
254
+ </div>
255
+ <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>
256
+ <div class="walk-note" style="font-size:11px;color:#64748b;text-align:center;margin-top:3px"></div>
257
+ </div>`
258
+ }
259
+
260
+ function esc(s) { return String(s).replace(/[&<>"]/g, c => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;' }[c])) }
261
+
96
262
  function renderContent($item, item) {
97
263
  $item.empty()
98
264
  if (item.svgString) {
99
265
  $item.append(`
100
266
  <div style="background:#f5f5f5;padding:8px;">
101
- <div style="overflow:auto;max-height:280px;border:1px solid #ddd;background:#fff;">
267
+ <div class="graph-box" style="overflow:auto;max-height:280px;border:1px solid #ddd;background:#fff;">
102
268
  ${item.svgString}
103
269
  </div>
104
270
  <div style="padding:6px 0 0;display:flex;gap:6px;justify-content:center;">
105
271
  <button class="edit-graph" style="cursor:pointer;">Edit in Graph Tool ↗</button>
106
272
  </div>
107
273
  ${buildBar(item)}
274
+ ${walkBar(item)}
108
275
  </div>
109
276
  `)
110
277
  } else {
@@ -174,8 +341,8 @@
174
341
  if (popup) popup.focus()
175
342
  })
176
343
  $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
344
+ // Stepping a build or a walk is quick clicking — never let it open the editor.
345
+ if ($(e.target).closest('.build-bar, .walk-bar').length) return
179
346
  wiki.textEditor($item, item)
180
347
  })
181
348
  // Build playback. Namespaced and reset, because a save re-runs bind.
@@ -191,6 +358,7 @@
191
358
  $item.on('click.rcnbuild', '.build-play', () => {
192
359
  const g = graphModel(item)
193
360
  if (!g) return
361
+ if (wk.w !== null) walkEnd()
194
362
  buildIdx = buildIdx === null ? 1 : null
195
363
  showBuildStep($item, g, buildIdx)
196
364
  $item.find('.build-bar').trigger('focus')
@@ -203,6 +371,95 @@
203
371
  else if (e.key === 'ArrowLeft' || e.key === 'ArrowUp') { e.preventDefault(); e.stopPropagation(); step(-1) }
204
372
  else if (e.key === 'Escape') { e.preventDefault(); buildIdx = null; showBuildStep($item, graphModel(item), null) }
205
373
  })
374
+ // Walk playback. w === null: no walk open. token guards every voice callback,
375
+ // so a step that was skipped or stopped can never move the walk on.
376
+ const wk = { w: null, i: 0, playing: false, token: 0, heard: null }
377
+ const curWalk = () => { const g = graphModel(item); return g && wk.w !== null ? walkList(g)[wk.w] : null }
378
+ const stopVoice = () => { wk.token++; try { if (window.speechSynthesis) speechSynthesis.cancel() } catch (e) {} }
379
+ const walkUI = note => {
380
+ const walk = curWalk(), open = !!walk
381
+ if (note) wk.heard = wk.i; else if (wk.playing) wk.heard = null
382
+ const $bar = $item.find('.walk-bar')
383
+ $bar.find('.walk-prev, .walk-next, .walk-play, .walk-count, .walk-voice, .walk-close').toggle(open)
384
+ $bar.find('.walk-start, .walk-pick').toggle(!open)
385
+ $bar.find('.walk-say').toggle(open)
386
+ $bar.find('.walk-play').text(wk.playing ? '❚❚' : '▶')
387
+ $bar.find('.walk-voice').text(walkPrefs().on ? '🔈 Voice on' : '🔇 Voice off')
388
+ $bar.find('.walk-count').text(open ? 'Step ' + (wk.i + 1) + ' of ' + walk.steps.length : '')
389
+ $bar.find('.walk-note').text(note || (open && !wk.playing ? 'Space plays · ← → step · Esc stops' : ''))
390
+ }
391
+ const walkShow = i => {
392
+ const g = graphModel(item), walk = curWalk()
393
+ if (!walk) return
394
+ wk.i = Math.max(0, Math.min(walk.steps.length - 1, i))
395
+ stopVoice()
396
+ showWalkStep($item, g, walk, wk.i)
397
+ const st = walk.steps[wk.i]
398
+ $item.find('.walk-say').text(st.say || '').css({ color: st.ask ? '#7c2d12' : '#1e1b4b', fontStyle: st.ask ? 'italic' : 'normal' })
399
+ walkUI()
400
+ if (wk.playing) walkSpeak()
401
+ }
402
+ const walkSpeak = () => {
403
+ const tok = ++wk.token, walk = curWalk(), st = walk && walk.steps[wk.i]
404
+ if (!st) return
405
+ walkSay(st.say, () => tok === wk.token && activeWalk === wk, () => {
406
+ const last = wk.i >= walk.steps.length - 1
407
+ 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 }
408
+ setTimeout(() => { if (tok === wk.token && wk.playing) walkShow(wk.i + 1) }, 800)
409
+ })
410
+ }
411
+ const walkPause = () => { wk.playing = false; stopVoice(); walkUI() }
412
+ const walkToggle = () => {
413
+ if (wk.w === null) return
414
+ if (wk.playing) { walkPause(); return }
415
+ if (activeWalk && activeWalk !== wk && activeWalk.end) activeWalk.end()
416
+ activeWalk = wk
417
+ wk.playing = true
418
+ // Play after a step already heard (an ask step, say) goes on to the next.
419
+ if (wk.heard === wk.i && wk.i < curWalk().steps.length - 1) { walkShow(wk.i + 1); return }
420
+ walkUI(); walkSpeak()
421
+ }
422
+ const walkEnd = () => {
423
+ if (wk.w === null) return
424
+ wk.playing = false; stopVoice()
425
+ const g = graphModel(item)
426
+ if (g) showWalkStep($item, g, null, null)
427
+ wk.w = null
428
+ if (activeWalk === wk) activeWalk = null
429
+ walkUI()
430
+ }
431
+ wk.end = walkEnd
432
+ $item.off('.rcnwalk')
433
+ $item.on('click.rcnwalk', '.walk-start', () => {
434
+ const g = graphModel(item)
435
+ if (!g) return
436
+ if (buildIdx !== null) { buildIdx = null; showBuildStep($item, g, null) }
437
+ wk.w = +($item.find('.walk-pick').val() || 0)
438
+ wk.heard = null
439
+ walkShow(0)
440
+ walkToggle() // a click is a user gesture, so the browser will speak
441
+ $item.find('.walk-bar').trigger('focus')
442
+ })
443
+ $item.on('click.rcnwalk', '.walk-play', () => walkToggle())
444
+ $item.on('click.rcnwalk', '.walk-prev', () => walkShow(wk.i - 1))
445
+ $item.on('click.rcnwalk', '.walk-next', () => walkShow(wk.i + 1))
446
+ $item.on('click.rcnwalk', '.walk-close', () => walkEnd())
447
+ $item.on('click.rcnwalk', '.walk-voice', () => {
448
+ const on = !walkPrefs().on
449
+ try { localStorage.setItem('rcnWalkVoice', on ? 'on' : 'off') } catch (e) {}
450
+ if (!on) stopVoice()
451
+ if (wk.playing) walkSpeak()
452
+ walkUI()
453
+ })
454
+ // Clicking the drawing to look closer pauses the voice.
455
+ $item.on('pointerdown.rcnwalk', '.graph-box', () => { if (wk.playing) walkPause() })
456
+ $item.on('keydown.rcnwalk', '.walk-bar', e => {
457
+ if (wk.w === null || $(e.target).is('select')) return
458
+ if (e.key === ' ') { e.preventDefault(); e.stopPropagation(); walkToggle() }
459
+ else if (e.key === 'ArrowRight') { e.preventDefault(); e.stopPropagation(); walkShow(wk.i + 1) }
460
+ else if (e.key === 'ArrowLeft') { e.preventDefault(); e.stopPropagation(); walkShow(wk.i - 1) }
461
+ else if (e.key === 'Escape') { e.preventDefault(); walkEnd() }
462
+ })
206
463
  // Labels in the saved SVG are wrapped as <a class="internal" data-title=…>, but
207
464
  // two things stopped a click reaching the page: the <text> keeps the canvas's
208
465
  // 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.14",
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
  }