wiki-plugin-rcngraph 0.1.19 → 0.1.20

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 +136 -0
  2. package/package.json +1 -1
@@ -410,8 +410,144 @@
410
410
  new MutationObserver(add).observe(el, { childList: true })
411
411
  }
412
412
 
413
+ // ── Notes you can edit from their popup (Nov 8 2026) ──────────────────────
414
+ // The drawing's hover text is the browser's own <title> tooltip, which cannot hold a
415
+ // button. So the plugin shows its own popup instead (the <title> text moves to
416
+ // data-note while the page is open, and stays in the saved picture), with
417
+ // "✎ Edit note". Saving writes the note into graphJSON — the same field the Graph
418
+ // Tool's side panel edits — and into that box's hover text in svgString, then saves
419
+ // the page. A box's label and the picture itself are not touched.
420
+ const NOTE_DOT = '#f59e0b'
421
+ function graphOf(item) {
422
+ const asText = typeof item.graphJSON === 'string'
423
+ let g = item.graphJSON
424
+ if (asText) { try { g = JSON.parse(g) } catch (e) { g = null } }
425
+ return { g, asText }
426
+ }
427
+ // "📌 note" leads the hover text when there is a note; properties follow it.
428
+ function retitle(title, oldNote, newNote) {
429
+ const t = String(title || '')
430
+ const pin = '\uD83D\uDCCC '
431
+ const rest = oldNote && t.indexOf(pin + oldNote) === 0 ? t.slice((pin + oldNote).length).replace(/^\n/, '')
432
+ : t.indexOf(pin) === 0 ? t.replace(/^[^\n]*\n?/, '') : t
433
+ return (newNote ? pin + newNote + (rest ? '\n' : '') : '') + rest
434
+ }
435
+ function svgWithNote(svgString, kind, id, oldNote, newNote) {
436
+ const doc = new DOMParser().parseFromString(svgString, 'image/svg+xml')
437
+ if (doc.getElementsByTagName('parsererror').length) return null
438
+ const g = doc.getElementById(kind + '-' + id)
439
+ if (!g) return null
440
+ const NS = 'http://www.w3.org/2000/svg'
441
+ let t = Array.from(g.children).find(c => c.localName === 'title')
442
+ const text = retitle(t ? t.textContent : '', oldNote, newNote)
443
+ if (text) {
444
+ if (!t) { t = doc.createElementNS(NS, 'title'); g.insertBefore(t, g.firstChild) }
445
+ t.textContent = text
446
+ } else if (t) t.remove()
447
+ if (kind === 'node') {
448
+ const dot = Array.from(g.children).find(c => c.localName === 'circle' && c.getAttribute('fill') === NOTE_DOT && c.getAttribute('r') === '4')
449
+ const rect = Array.from(g.children).find(c => c.localName === 'rect')
450
+ if (newNote && !dot && rect) {
451
+ const c = doc.createElementNS(NS, 'circle')
452
+ c.setAttribute('cx', (+rect.getAttribute('x') + +rect.getAttribute('width') - 4).toFixed(1))
453
+ c.setAttribute('cy', (+rect.getAttribute('y') + 4).toFixed(1))
454
+ c.setAttribute('r', '4'); c.setAttribute('fill', NOTE_DOT); c.setAttribute('stroke', '#fff'); c.setAttribute('stroke-width', '1')
455
+ rect.after(c)
456
+ } else if (!newNote && dot) dot.remove()
457
+ }
458
+ return new XMLSerializer().serializeToString(doc.documentElement)
459
+ }
460
+ function bindNotes($item, item) {
461
+ const box = $item.find('.graph-box').get(0)
462
+ if (!box || !graphOf(item).g) return
463
+ let card = null, hideT = 0, editing = false
464
+ const groups = box.querySelectorAll('g[id^="node-"], g[id^="edge-"]')
465
+ // Move each <title> into data-note so the browser's own tooltip does not double ours.
466
+ groups.forEach(gEl => {
467
+ const t = Array.from(gEl.children).find(c => c.localName === 'title')
468
+ if (t) { gEl.setAttribute('data-note', t.textContent); t.remove() }
469
+ })
470
+ const close = () => { editing = false; if (card) { card.remove(); card = null } }
471
+ const place = (ev) => {
472
+ if (!card || editing) return
473
+ const w = card.offsetWidth || 300, h = card.offsetHeight || 0
474
+ let x = ev.clientX + 14, y = ev.clientY - 10
475
+ if (x + w > window.innerWidth - 8) x = Math.max(8, ev.clientX - w - 14)
476
+ if (y + h > window.innerHeight - 8) y = Math.max(8, window.innerHeight - h - 8)
477
+ card.style.left = x + 'px'; card.style.top = y + 'px'
478
+ }
479
+ const show = (gEl, ev) => {
480
+ if (editing) return
481
+ clearTimeout(hideT)
482
+ const kind = gEl.id.startsWith('node-') ? 'node' : 'edge', id = gEl.id.slice(kind.length + 1)
483
+ const { g } = graphOf(item)
484
+ const obj = g && (kind === 'node' ? g.nodes : g.edges).find(o => String(o.id) === id)
485
+ const text = gEl.getAttribute('data-note') || ''
486
+ if (!obj || !text) { close(); return } // like the Graph Tool: a popup only where there is hover text
487
+ if (!card) {
488
+ card = document.createElement('div')
489
+ card.className = 'rcng-note-card'
490
+ card.style.cssText = 'position:fixed;z-index:3000;max-width:320px;background:rgba(255,255,255,.96);border:1px solid rgba(0,0,0,.1);border-radius:8px;box-shadow:0 2px 12px rgba(0,0,0,.18);padding:8px 12px;font:12px/1.55 system-ui,sans-serif;color:#0f172a;white-space:pre-wrap;word-break:break-word'
491
+ card.addEventListener('mouseenter', () => clearTimeout(hideT))
492
+ card.addEventListener('mouseleave', () => { if (!editing) hideT = setTimeout(close, 120) })
493
+ document.body.appendChild(card)
494
+ }
495
+ card.innerHTML = `<div>${esc(text)}</div>` +
496
+ `<div style="margin-top:6px;white-space:normal"><button class="rcng-note-edit" style="font:inherit;font-size:11px;padding:2px 8px;border:1px solid #c7d2fe;border-radius:5px;background:#fff;color:#3730a3;cursor:pointer" title="Change this note. It is saved into the drawing on this page.">✎ Edit note</button></div>`
497
+ card.querySelector('.rcng-note-edit').onclick = () => edit(gEl, kind, id, obj)
498
+ place(ev)
499
+ }
500
+ const edit = (gEl, kind, id, obj) => {
501
+ editing = true
502
+ const label = String(obj.label || '').replace(/\n/g, ' ')
503
+ card.style.width = '360px'; card.style.maxWidth = 'calc(100vw - 16px)'; card.style.whiteSpace = 'normal'
504
+ card.innerHTML = `<div style="font-weight:600;margin-bottom:4px">Note on “${esc(label || '(no label)')}”</div>
505
+ <textarea rows="6" style="width:100%;box-sizing:border-box;font:13px/1.4 system-ui,sans-serif;padding:5px 7px;border:1px solid #c7d2fe;border-radius:5px;resize:vertical" placeholder="What a reader should know about this: a quote, a source, a reason."></textarea>
506
+ <div style="display:flex;gap:6px;align-items:center;margin-top:6px">
507
+ <button class="rcng-note-save" style="font:inherit;font-size:11px;padding:2px 10px;border:1px solid #4f46e5;border-radius:5px;background:#4f46e5;color:#fff;font-weight:600;cursor:pointer">Save</button>
508
+ <button class="rcng-note-cancel" style="font:inherit;font-size:11px;padding:2px 8px;border:1px solid #c7d2fe;border-radius:5px;background:#fff;color:#3730a3;cursor:pointer">Cancel</button>
509
+ <span style="font-size:10px;color:#64748b;margin-left:auto">⌘/Ctrl-Enter saves · Esc cancels</span></div>`
510
+ const r = card.getBoundingClientRect()
511
+ if (r.right > window.innerWidth - 8) card.style.left = Math.max(8, window.innerWidth - r.width - 8) + 'px'
512
+ if (r.bottom > window.innerHeight - 8) card.style.top = Math.max(8, window.innerHeight - r.height - 8) + 'px'
513
+ const ta = card.querySelector('textarea')
514
+ ta.value = obj.note || ''
515
+ const save = () => {
516
+ const v = ta.value.replace(/\s+$/, '')
517
+ const old = obj.note || ''
518
+ close()
519
+ if (v === old) return
520
+ const { g, asText } = graphOf(item)
521
+ const o = g && (kind === 'node' ? g.nodes : g.edges).find(x => String(x.id) === id)
522
+ if (!o) return
523
+ o.note = v
524
+ const svg = item.svgString ? svgWithNote(item.svgString, kind, id, old, v) : null
525
+ item.graphJSON = asText ? JSON.stringify(g) : g
526
+ if (svg) item.svgString = svg
527
+ wiki.pageHandler.put($item.parents('.page:first'), { type: 'edit', id: item.id, item })
528
+ renderContent($item, item)
529
+ bind($item, item)
530
+ }
531
+ card.querySelector('.rcng-note-save').onclick = save
532
+ card.querySelector('.rcng-note-cancel').onclick = close
533
+ ta.addEventListener('keydown', e => {
534
+ e.stopPropagation()
535
+ if (e.key === 'Escape') { e.preventDefault(); close() }
536
+ else if (e.key === 'Enter' && (e.metaKey || e.ctrlKey)) { e.preventDefault(); save() }
537
+ })
538
+ ta.addEventListener('dblclick', e => e.stopPropagation())
539
+ ta.focus()
540
+ }
541
+ groups.forEach(gEl => {
542
+ gEl.addEventListener('mouseenter', ev => show(gEl, ev))
543
+ gEl.addEventListener('mousemove', ev => place(ev))
544
+ gEl.addEventListener('mouseleave', () => { if (!editing) { clearTimeout(hideT); hideT = setTimeout(close, 150) } })
545
+ })
546
+ }
547
+
413
548
  function bind($item, item) {
414
549
  aboutMark($item, 'rcngraph')
550
+ bindNotes($item, item)
415
551
  $item.on('click', '.open-graph, .edit-graph', () => {
416
552
  pendingItem = item
417
553
  pending$item = $item
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wiki-plugin-rcngraph",
3
- "version": "0.1.19",
3
+ "version": "0.1.20",
4
4
  "description": "Federated Wiki - RCN Graph Plug-in",
5
5
  "keywords": [
6
6
  "wiki",