@khanglvm/relay 0.17.0 → 0.18.0

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/src/ui/blocks.js CHANGED
@@ -2483,6 +2483,9 @@
2483
2483
  '<svg viewBox="0 0 24 24" width="13" height="13" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">' +
2484
2484
  '<rect x="3" y="4" width="14" height="12" rx="2" stroke-dasharray="3 2"/>' +
2485
2485
  '<path d="M14 14.5h6.5v4H18l-2.5 2v-2H14z" fill="currentColor" stroke="none"/></svg>';
2486
+ const ICON_EYE =
2487
+ '<svg viewBox="0 0 24 24" width="13" height="13" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">' +
2488
+ '<path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7-10-7-10-7z"/><circle cx="12" cy="12" r="3"/></svg>';
2486
2489
 
2487
2490
  function exitFull() {
2488
2491
  if (!fullOpen) return;
@@ -2784,6 +2787,30 @@
2784
2787
  }
2785
2788
  }
2786
2789
 
2790
+ if (opts && opts.comment && opts.hoverComments) {
2791
+ const hoverCtl = opts.hoverComments;
2792
+ const hoverBtn = el('button', {
2793
+ class: 'tool-hover-comments',
2794
+ type: 'button',
2795
+ 'aria-label': 'Show element comment overlays',
2796
+ });
2797
+ hoverBtn.innerHTML = ICON_EYE;
2798
+ hoverBtn.append(el('span', { class: 'tool-hover-label' }, 'Hover comments'));
2799
+ const syncHoverMode = () => {
2800
+ const enabled = hoverCtl.isEnabled();
2801
+ hoverBtn.classList.toggle('is-active', enabled);
2802
+ hoverBtn.setAttribute('aria-pressed', String(enabled));
2803
+ hoverBtn.title = enabled ? 'Hide element comment overlays' : 'Show element comment overlays';
2804
+ };
2805
+ hoverBtn.addEventListener('click', (e) => {
2806
+ e.stopPropagation();
2807
+ hoverCtl.setEnabled(!hoverCtl.isEnabled());
2808
+ syncHoverMode();
2809
+ });
2810
+ syncHoverMode();
2811
+ tools.append(hoverBtn);
2812
+ }
2813
+
2787
2814
  if (opts && opts.region) {
2788
2815
  const regionCtl = opts.region;
2789
2816
  const regionLabel = el('span', { class: 'tool-region-label' }, 'Area');
@@ -2894,19 +2921,47 @@
2894
2921
  }
2895
2922
 
2896
2923
  // ---------- html (sandboxed iframe) ----------
2924
+ function syncHtmlHoverComments(frame) {
2925
+ try {
2926
+ frame.contentWindow.postMessage({ relay: 'annotate-hover', enabled: frame.dataset.relayHoverComments !== 'off' }, '*');
2927
+ } catch (_) {}
2928
+ }
2929
+
2930
+ // The kit can load after the iframe's load event; its ready message also
2931
+ // restores the current toggle when the embedded page reloads.
2932
+ window.addEventListener('message', (e) => {
2933
+ if (!e.data || e.data.relay !== 'annotate-ready') return;
2934
+ for (const frame of document.querySelectorAll('iframe.viz[data-relay-hover-comments]')) {
2935
+ if (frame.contentWindow === e.source) { syncHtmlHoverComments(frame); break; }
2936
+ }
2937
+ });
2938
+
2897
2939
  function renderHtml(block, ctx, blockId) {
2898
2940
  const height = clampHeight(block.height, 360);
2899
2941
  const iframe = el('iframe', {
2900
2942
  class: 'viz',
2901
2943
  'data-block-id': blockId,
2902
2944
  'data-question-id': ctx.questionId || '',
2945
+ 'data-relay-hover-comments': 'on',
2903
2946
  src: ctx.htmlSrc(blockId),
2904
2947
  height: String(height),
2905
2948
  sandbox: 'allow-scripts allow-forms allow-popups allow-modals',
2906
2949
  loading: 'lazy',
2907
2950
  });
2951
+ iframe.addEventListener('load', () => syncHtmlHoverComments(iframe));
2908
2952
  const wrap = el('div', { class: 'blk-htmlwrap' }, iframe);
2909
- attachViewer(wrap, { zoomEl: null, label: 'embed', comment: wholeBlockComment(ctx, blockId, 'embed') });
2953
+ attachViewer(wrap, {
2954
+ zoomEl: null,
2955
+ label: 'embed',
2956
+ comment: wholeBlockComment(ctx, blockId, 'embed'),
2957
+ hoverComments: {
2958
+ isEnabled: () => iframe.dataset.relayHoverComments !== 'off',
2959
+ setEnabled: (enabled) => {
2960
+ iframe.dataset.relayHoverComments = enabled ? 'on' : 'off';
2961
+ syncHtmlHoverComments(iframe);
2962
+ },
2963
+ },
2964
+ });
2910
2965
  return wrap;
2911
2966
  }
2912
2967
 
@@ -2987,6 +3042,10 @@
2987
3042
  inner = renderCompare(block, ctx, blockId);
2988
3043
  wrapper.append(inner);
2989
3044
  break;
3045
+ case 'a2ui':
3046
+ inner = window.RelayA2ui ? window.RelayA2ui.render(block, ctx, blockId) : el('div', { class: 'blk-error' }, 'A2UI renderer is not loaded.');
3047
+ wrapper.append(inner);
3048
+ break;
2990
3049
  default:
2991
3050
  wrapper.append(el('div', { class: 'blk-error' }, 'Unknown block type: ' + esc(String(block.type))));
2992
3051
  }
package/src/ui/kit.js CHANGED
@@ -401,6 +401,7 @@
401
401
  let started = false;
402
402
  let hot = null; // currently outlined element
403
403
  let pin = null; // floating pin button
404
+ let hoverEnabled = true;
404
405
  let hideTimer = 0;
405
406
  let lastCounts = {};
406
407
  let overlayBadges = []; // [{el, badge}] for replaced elements
@@ -453,18 +454,25 @@
453
454
  function ensurePin() {
454
455
  if (pin || !document.body) return pin;
455
456
  pin = document.createElement('button');
457
+ pin.className = 'relay-ann-pin';
456
458
  pin.type = 'button';
457
459
  pin.setAttribute('aria-label', 'Add a comment');
458
460
  pin.title = 'Add a comment';
459
461
  pin.style.cssText = [
460
- 'position:fixed', 'z-index:2147483647', 'display:none', 'width:22px', 'height:22px',
462
+ 'all:initial', 'position:fixed', 'z-index:2147483647', 'display:none', 'width:22px', 'height:22px',
463
+ 'box-sizing:border-box', 'margin:0',
461
464
  'padding:0', 'border:none', 'border-radius:50%', 'cursor:pointer', 'background:' + accent(),
462
465
  'color:#fff', 'align-items:center', 'justify-content:center', 'line-height:0',
463
466
  'box-shadow:0 1px 4px rgba(0,0,0,.3)',
464
- ].join(';');
467
+ ].map((rule) => rule + ' !important').join(';');
465
468
  pin.innerHTML =
466
469
  '<svg viewBox="0 0 16 16" width="11" height="11" aria-hidden="true">' +
467
470
  '<path d="M3 2.5h10A1.5 1.5 0 0 1 14.5 4v5a1.5 1.5 0 0 1-1.5 1.5H8.4L5 13.4v-2.9H3A1.5 1.5 0 0 1 1.5 9V4A1.5 1.5 0 0 1 3 2.5Z" fill="currentColor"/></svg>';
471
+ pin.querySelector('svg').style.cssText =
472
+ 'all:initial !important;display:block !important;width:11px !important;height:11px !important;fill:currentColor !important;color:inherit !important;';
473
+ pin.querySelector('path').style.setProperty('fill', 'currentColor', 'important');
474
+ pin.addEventListener('focus', () => pin.style.setProperty('outline', '2px solid ' + accent(), 'important'));
475
+ pin.addEventListener('blur', () => pin.style.removeProperty('outline'));
468
476
  pin.addEventListener('mouseenter', () => clearTimeout(hideTimer));
469
477
  pin.addEventListener('mouseleave', scheduleHide);
470
478
  pin.addEventListener('click', (e) => {
@@ -478,16 +486,16 @@
478
486
  }
479
487
 
480
488
  function showPinFor(el) {
481
- if (!ensurePin()) return;
489
+ if (!hoverEnabled || !ensurePin()) return;
482
490
  clearTimeout(hideTimer);
483
491
  if (hot && hot !== el) hot.classList.remove('relay-ann-hot');
484
492
  hot = el;
485
493
  el.classList.add('relay-ann-hot');
486
494
  const r = el.getBoundingClientRect();
487
495
  const w = 22;
488
- pin.style.display = 'flex';
489
- pin.style.left = Math.max(2, Math.min(r.right - w / 2, window.innerWidth - w - 2)) + 'px';
490
- pin.style.top = Math.max(2, Math.min(r.top - w / 2, window.innerHeight - w - 2)) + 'px';
496
+ pin.style.setProperty('display', 'flex', 'important');
497
+ pin.style.setProperty('left', Math.max(2, Math.min(r.right - w / 2, window.innerWidth - w - 2)) + 'px', 'important');
498
+ pin.style.setProperty('top', Math.max(2, Math.min(r.top - w / 2, window.innerHeight - w - 2)) + 'px', 'important');
491
499
  }
492
500
 
493
501
  function scheduleHide() {
@@ -499,7 +507,7 @@
499
507
  clearTimeout(hideTimer);
500
508
  if (hot) hot.classList.remove('relay-ann-hot');
501
509
  hot = null;
502
- if (pin) pin.style.display = 'none';
510
+ if (pin) pin.style.setProperty('display', 'none', 'important');
503
511
  }
504
512
 
505
513
  // Ask the parent to open its annotation popover for this element.
@@ -631,8 +639,13 @@
631
639
  }, true);
632
640
  window.addEventListener('resize', () => { hidePin(); repositionOverlays(); });
633
641
  window.addEventListener('message', (e) => {
642
+ if (e.source !== parent) return;
634
643
  const m = e.data;
635
644
  if (m && typeof m === 'object' && m.relay === 'annotate-counts') renderBadges(m.counts || {});
645
+ if (m && typeof m === 'object' && m.relay === 'annotate-hover' && typeof m.enabled === 'boolean') {
646
+ hoverEnabled = m.enabled;
647
+ if (!hoverEnabled) hidePin();
648
+ }
636
649
  });
637
650
  // Re-pick up DOM that author JS builds after load. We observe childList
638
651
  // only (not attributes, so our own hover-class toggles don't fire it) and