@khanglvm/relay 0.18.0 → 0.20.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.
Files changed (138) hide show
  1. package/bin/rly.js +1 -1
  2. package/package.json +39 -4
  3. package/schema/spec.v1.json +1368 -0
  4. package/src/a2ui/compile.js +2 -0
  5. package/src/cli/args.js +46 -0
  6. package/src/cli/ask.js +12 -0
  7. package/src/cli/boards.js +326 -0
  8. package/src/cli/config.js +56 -0
  9. package/src/cli/context.js +13 -0
  10. package/src/cli/git.js +183 -0
  11. package/src/cli/help.js +90 -0
  12. package/src/cli/index.js +103 -0
  13. package/src/cli/install.js +197 -0
  14. package/src/cli/io.js +186 -0
  15. package/src/cli/mcp.js +94 -0
  16. package/src/cli/share.js +45 -0
  17. package/src/cli/skill.js +192 -0
  18. package/src/cli/upgrade.js +130 -0
  19. package/src/cli/wait.js +172 -0
  20. package/src/file-preview.js +1 -1
  21. package/src/index.js +104 -0
  22. package/src/link.js +1 -0
  23. package/src/mcp-ui/board/block-ctx.js +43 -0
  24. package/src/mcp-ui/board/bridge.js +165 -0
  25. package/src/mcp-ui/board/controls.js +310 -0
  26. package/src/mcp-ui/board/core.js +20 -0
  27. package/src/mcp-ui/board/lifecycle.js +118 -0
  28. package/src/mcp-ui/board/normalize.js +95 -0
  29. package/src/mcp-ui/board/render.js +200 -0
  30. package/src/mcp-ui/board/state.js +106 -0
  31. package/src/mcp-ui/board/vendors.js +86 -0
  32. package/src/mcp.js +15 -21
  33. package/src/{server.js → server/board.js} +17 -781
  34. package/src/server/files.js +141 -0
  35. package/src/server/http.js +114 -0
  36. package/src/server/index.js +2 -0
  37. package/src/server/page.js +183 -0
  38. package/src/server/paths.js +7 -0
  39. package/src/server/result.js +58 -0
  40. package/src/server/reviews.js +41 -0
  41. package/src/server/sanitize.js +231 -0
  42. package/src/spec/blocks/a2ui.js +25 -0
  43. package/src/spec/blocks/chart.js +49 -0
  44. package/src/spec/blocks/code.js +38 -0
  45. package/src/spec/blocks/diagram.js +39 -0
  46. package/src/spec/blocks/git-conflict.js +106 -0
  47. package/src/spec/blocks/image.js +32 -0
  48. package/src/spec/blocks/index.js +33 -0
  49. package/src/spec/blocks/markdown.js +13 -0
  50. package/src/spec/blocks/pdf.js +38 -0
  51. package/src/spec/blocks/table.js +122 -0
  52. package/src/spec/blocks/video.js +68 -0
  53. package/src/spec/blocks/widgets.js +98 -0
  54. package/src/spec/constants.js +65 -0
  55. package/src/spec/index.js +5 -0
  56. package/src/spec/normalize.js +285 -0
  57. package/src/spec/ready.js +106 -0
  58. package/src/spec/schema.js +182 -0
  59. package/src/spec/sources.js +83 -0
  60. package/src/ui/annotate/api.js +149 -0
  61. package/src/ui/annotate/badges.js +77 -0
  62. package/src/ui/annotate/chrome.js +158 -0
  63. package/src/ui/annotate/core.js +134 -0
  64. package/src/ui/annotate/highlights.js +129 -0
  65. package/src/ui/annotate/mutations.js +127 -0
  66. package/src/ui/annotate/popover.js +206 -0
  67. package/src/ui/annotate/summary.js +67 -0
  68. package/src/ui/app/annotations.js +88 -0
  69. package/src/ui/app/autosave.js +44 -0
  70. package/src/ui/app/core.js +74 -0
  71. package/src/ui/app/drafts.js +201 -0
  72. package/src/ui/app/heartbeat.js +150 -0
  73. package/src/ui/app/iframe-bridge.js +64 -0
  74. package/src/ui/app/persistence.js +117 -0
  75. package/src/ui/app/prefilled.js +21 -0
  76. package/src/ui/app/presence.js +38 -0
  77. package/src/ui/app/question-color.js +67 -0
  78. package/src/ui/app/questions.js +360 -0
  79. package/src/ui/app/render.js +135 -0
  80. package/src/ui/app/share.js +108 -0
  81. package/src/ui/app/submit.js +109 -0
  82. package/src/ui/app/theme.js +63 -0
  83. package/src/ui/blocks/api.js +1 -0
  84. package/src/ui/blocks/chart.js +222 -0
  85. package/src/ui/blocks/code.js +138 -0
  86. package/src/ui/blocks/compare.js +82 -0
  87. package/src/ui/blocks/core.js +74 -0
  88. package/src/ui/blocks/diff.js +361 -0
  89. package/src/ui/blocks/dispatch.js +111 -0
  90. package/src/ui/blocks/file-links.js +180 -0
  91. package/src/ui/blocks/git-conflict.js +151 -0
  92. package/src/ui/blocks/graphviz.js +57 -0
  93. package/src/ui/blocks/html.js +45 -0
  94. package/src/ui/blocks/image.js +371 -0
  95. package/src/ui/blocks/kpi.js +26 -0
  96. package/src/ui/blocks/markdown.js +223 -0
  97. package/src/ui/blocks/mermaid.js +242 -0
  98. package/src/ui/blocks/palette.js +59 -0
  99. package/src/ui/blocks/pdf.js +21 -0
  100. package/src/ui/blocks/plantuml.js +152 -0
  101. package/src/ui/blocks/table.js +155 -0
  102. package/src/ui/blocks/typography.js +25 -0
  103. package/src/ui/blocks/vendor.js +43 -0
  104. package/src/ui/blocks/video.js +43 -0
  105. package/src/ui/blocks/viewer-fullscreen.js +146 -0
  106. package/src/ui/blocks/viewer.js +324 -0
  107. package/src/ui/kit.js +1 -0
  108. package/src/ui/manifest.js +140 -0
  109. package/types/a2ui/compile.d.ts +40 -0
  110. package/types/index.d.ts +181 -0
  111. package/types/mcp.d.ts +31 -0
  112. package/types/spec/blocks/a2ui.d.ts +4 -0
  113. package/types/spec/blocks/chart.d.ts +4 -0
  114. package/types/spec/blocks/code.d.ts +10 -0
  115. package/types/spec/blocks/diagram.d.ts +15 -0
  116. package/types/spec/blocks/git-conflict.d.ts +7 -0
  117. package/types/spec/blocks/image.d.ts +5 -0
  118. package/types/spec/blocks/index.d.ts +30 -0
  119. package/types/spec/blocks/markdown.d.ts +5 -0
  120. package/types/spec/blocks/pdf.d.ts +4 -0
  121. package/types/spec/blocks/table.d.ts +9 -0
  122. package/types/spec/blocks/video.d.ts +4 -0
  123. package/types/spec/blocks/widgets.d.ts +21 -0
  124. package/types/spec/constants.d.ts +67 -0
  125. package/types/spec/index.d.ts +4 -0
  126. package/types/spec/normalize.d.ts +17 -0
  127. package/types/spec/ready.d.ts +1 -0
  128. package/types/spec/schema.d.ts +1171 -0
  129. package/types/spec/sources.d.ts +14 -0
  130. package/types/store.d.ts +29 -0
  131. package/types/ui/manifest.d.ts +62 -0
  132. package/types/util.d.ts +6 -0
  133. package/src/cli.js +0 -1766
  134. package/src/mcp-ui/board.js +0 -1162
  135. package/src/spec.js +0 -1222
  136. package/src/ui/annotate.js +0 -1055
  137. package/src/ui/app.js +0 -1643
  138. package/src/ui/blocks.js +0 -3260
@@ -0,0 +1,180 @@
1
+ // ---------- file-link open behavior ----------
2
+ let fileDialog = null;
3
+ const fileAuthHeaders = () => {
4
+ const token = new URLSearchParams(location.search).get('token');
5
+ return token ? { 'x-relay-share-token': token } : {};
6
+ };
7
+
8
+ async function previewFilePath(a) {
9
+ const p = a.dataset.rlyOpen;
10
+ if (!p) return;
11
+ if (fileDialog) fileDialog.close();
12
+ const dialog = el('dialog', { class: 'rly-file-dialog', 'aria-label': 'File preview' });
13
+ const title = el('h2', {}, p.split(/[\\/]/).pop());
14
+ const pathLabel = el('div', { class: 'rly-file-location', title: p }, p);
15
+ const close = el('button', { type: 'button', class: 'btn', autofocus: '', 'aria-label': 'Close file preview' }, 'Close');
16
+ const open = el('button', { type: 'button', class: 'btn' }, 'Open in app');
17
+ open.dataset.rlyOpen = p;
18
+ open.addEventListener('click', () => openFilePath(open));
19
+ const actions = el('div', { class: 'rly-file-actions' }, open, close);
20
+ const body = el('div', { class: 'rly-file-body', tabindex: '0', 'aria-busy': 'true' });
21
+ const status = el('p', { role: 'status' }, 'Loading preview…');
22
+ body.append(status);
23
+ dialog.append(el('header', { class: 'rly-file-header' }, el('div', { class: 'rly-file-heading' }, title, pathLabel), actions), body);
24
+ const controller = new AbortController();
25
+ const previousFocus = document.activeElement;
26
+ close.addEventListener('click', () => dialog.close());
27
+ dialog.addEventListener('close', () => {
28
+ controller.abort();
29
+ dialog.querySelectorAll('video,audio').forEach((media) => { media.pause(); media.removeAttribute('src'); media.load(); });
30
+ dialog.remove();
31
+ if (fileDialog === dialog) fileDialog = null;
32
+ if (previousFocus && previousFocus.isConnected) previousFocus.focus({ preventScroll: true });
33
+ });
34
+ document.body.append(dialog);
35
+ fileDialog = dialog;
36
+ dialog.showModal();
37
+ try {
38
+ const response = await fetch('/api/file-preview', {
39
+ method: 'POST', headers: { 'content-type': 'application/json', ...fileAuthHeaders() },
40
+ body: JSON.stringify({ path: p }), signal: controller.signal,
41
+ });
42
+ let data;
43
+ try { data = await response.json(); }
44
+ catch { throw new Error('File previews need a current Relay browser board. Reconnect this board with the updated CLI.'); }
45
+ if (!response.ok || !data.ok) {
46
+ if (response.status === 403) open.disabled = true;
47
+ throw new Error(data.error || 'Preview unavailable. Reconnect this board with the updated CLI.');
48
+ }
49
+ if (!dialog.open) return;
50
+ title.textContent = data.name;
51
+ dialog.setAttribute('aria-label', 'File preview: ' + data.name);
52
+ body.replaceChildren();
53
+ const source = () => {
54
+ body.replaceChildren();
55
+ const lines = String(data.text || '').replace(/\r\n?/g, '\n').replace(/\n$/, '').split('\n');
56
+ const list = el('div', { class: 'rly-file-source' });
57
+ lines.forEach((line, i) => {
58
+ const number = i + 1;
59
+ const code = el('code');
60
+ code.innerHTML = tintCode(line, data.lang) || ' ';
61
+ const row = el('div', { class: 'rly-file-line', 'data-line': String(number) },
62
+ el('span', { class: 'rly-file-line-number', 'aria-hidden': 'true' }, String(number)), code);
63
+ if (data.line && number >= data.line && number <= data.endLine) row.classList.add('is-target');
64
+ list.append(row);
65
+ });
66
+ if (data.line > lines.length) body.append(el('p', { role: 'status' }, 'Line ' + data.line + ' is beyond this file’s ' + lines.length + ' lines.'));
67
+ body.append(list);
68
+ requestAnimationFrame(() => {
69
+ const target = list.querySelector('.is-target');
70
+ if (!target || !dialog.open) return;
71
+ body.scrollTop += target.getBoundingClientRect().top - body.getBoundingClientRect().top - body.clientHeight / 2 + target.clientHeight / 2;
72
+ body.scrollLeft = 0;
73
+ });
74
+ };
75
+ const rendered = () => {
76
+ body.replaceChildren();
77
+ if (data.kind === 'markdown') {
78
+ const md = el('div', { class: 'blk-markdown' });
79
+ md.append(renderMarkdown(data.text));
80
+ body.append(md);
81
+ } else if (data.kind === 'table') {
82
+ body.append(renderTable(data.block, { canComment: false }, 'file-preview'));
83
+ } else if (data.kind === 'html') {
84
+ // Empty sandbox prevents scripts, forms and same-origin access. CSP
85
+ // also prevents authored markup from fetching local or remote URLs.
86
+ const frame = el('iframe', { class: 'rly-file-frame', title: data.name, sandbox: '', referrerpolicy: 'no-referrer' });
87
+ frame.srcdoc = '<meta http-equiv="Content-Security-Policy" content="default-src &#39;none&#39;; style-src &#39;unsafe-inline&#39;; img-src data:; font-src data:">' + data.text;
88
+ body.append(frame);
89
+ }
90
+ };
91
+ if (['markdown', 'html', 'table'].includes(data.kind)) {
92
+ let viewingSource = !!data.line;
93
+ const toggle = el('button', { type: 'button', class: 'btn' }, viewingSource ? 'Preview' : 'Source');
94
+ toggle.addEventListener('click', () => {
95
+ viewingSource = !viewingSource;
96
+ toggle.textContent = viewingSource ? 'Preview' : 'Source';
97
+ viewingSource ? source() : rendered();
98
+ });
99
+ actions.prepend(toggle);
100
+ viewingSource ? source() : rendered();
101
+ } else if (data.kind === 'code') source();
102
+ else if (data.kind === 'unsupported') body.append(el('p', { role: 'status' }, data.reason));
103
+ else {
104
+ const params = new URLSearchParams({ path: p });
105
+ const token = new URLSearchParams(location.search).get('token');
106
+ if (token) params.set('token', token);
107
+ const url = '/api/file-content?' + params;
108
+ const tag = { image: 'img', pdf: 'iframe', video: 'video', audio: 'audio' }[data.kind];
109
+ if (!tag) throw new Error('This file type cannot be previewed.');
110
+ const media = el(tag, { class: 'rly-file-media rly-file-' + data.kind, src: url, title: data.name });
111
+ if (tag === 'img') media.alt = data.name;
112
+ if (tag === 'video' || tag === 'audio') { media.controls = true; media.preload = 'metadata'; }
113
+ media.addEventListener('error', () => body.append(el('p', { role: 'status' }, 'Your browser could not display this file. Use Open in app.')), { once: true });
114
+ body.append(media);
115
+ }
116
+ } catch (err) {
117
+ if (err.name !== 'AbortError' && dialog.open) body.replaceChildren(el('p', { role: 'alert' }, err.message || 'Could not reach the board server.'));
118
+ } finally { body.setAttribute('aria-busy', 'false'); }
119
+ }
120
+
121
+ // A small toast pinned top-center (reuses the .toast style from style.css).
122
+ // tone 'err' adds .toast-err so failures read as a problem, not a success.
123
+ function fileToast(message, tone) {
124
+ const t = el('div', { class: 'toast' + (tone === 'err' ? ' toast-err' : '') }, message);
125
+ document.body.append(t);
126
+ setTimeout(() => t.remove(), 3500);
127
+ }
128
+
129
+ // POST the clicked path to /api/open; the server opens it in the OS default
130
+ // app. The link is disabled mid-flight so a double-click can't double-open.
131
+ async function openFilePath(a) {
132
+ const p = a.dataset.rlyOpen;
133
+ if (!p || a.classList.contains('is-opening')) return;
134
+ a.classList.add('is-opening');
135
+ try {
136
+ const r = await fetch('/api/open', {
137
+ method: 'POST',
138
+ headers: { 'content-type': 'application/json', ...fileAuthHeaders() },
139
+ body: JSON.stringify({ path: p }),
140
+ });
141
+ let data = {};
142
+ try {
143
+ data = await r.json();
144
+ } catch {
145
+ data = {};
146
+ }
147
+ if (r.ok && data.ok) fileToast('Opened ' + (data.name || p));
148
+ else fileToast((data.error || 'Could not open') + ' — ' + p, 'err');
149
+ } catch {
150
+ fileToast('Could not reach the server to open ' + p, 'err');
151
+ } finally {
152
+ a.classList.remove('is-opening');
153
+ }
154
+ }
155
+
156
+ // Delegated once on the document so it catches file-links anywhere they are
157
+ // rendered — the intro, any markdown block, or a markdown pipe-table cell.
158
+ // Keyboard-activatable (Enter/Space) since the links are role="link" anchors
159
+ // with no href (the open happens via fetch, not navigation).
160
+ function initFileLinks() {
161
+ if (window.__relayFileLinksReady) return;
162
+ window.__relayFileLinksReady = true;
163
+ const hit = (e) => (e.target.closest ? e.target.closest('a.rly-filelink') : null);
164
+ document.addEventListener('click', (e) => {
165
+ const a = hit(e);
166
+ if (!a) return;
167
+ e.preventDefault();
168
+ previewFilePath(a);
169
+ });
170
+ document.addEventListener('keydown', (e) => {
171
+ if (e.key !== 'Enter' && e.key !== ' ' && e.key !== 'Spacebar') return;
172
+ const a = hit(e);
173
+ if (!a) return;
174
+ e.preventDefault();
175
+ previewFilePath(a);
176
+ });
177
+ }
178
+ initFileLinks();
179
+ initRefLinks();
180
+
@@ -0,0 +1,151 @@
1
+ // ---------- git conflict resolver ----------
2
+ function markerConflictText(h) {
3
+ const chunks = [
4
+ '<<<<<<< ' + (h.oursLabel || 'ours'),
5
+ h.ours || '',
6
+ ];
7
+ if (h.baseLabel) chunks.push('||||||| ' + h.baseLabel, h.base || '');
8
+ chunks.push('=======', h.theirs || '', '>>>>>>> ' + (h.theirsLabel || 'theirs'));
9
+ return chunks.join('\n');
10
+ }
11
+
12
+ function conflictChoiceText(h, choice, custom) {
13
+ if (choice === 'ours') return h.ours || '';
14
+ if (choice === 'theirs') return h.theirs || '';
15
+ if (choice === 'both') {
16
+ const left = h.ours || '';
17
+ const right = h.theirs || '';
18
+ return left && right ? left + '\n' + right : left || right;
19
+ }
20
+ if (choice === 'custom') return custom || '';
21
+ return markerConflictText(h);
22
+ }
23
+
24
+ function joinResolvedParts(block, resolutions) {
25
+ const byId = new Map((block.conflicts || []).map((h) => [h.id, h]));
26
+ return (block.parts || []).map((part) => {
27
+ if (!part || part.type === 'text') return part && part.text ? part.text : '';
28
+ const h = byId.get(part.conflictId);
29
+ if (!h) return '';
30
+ const r = resolutions && resolutions[h.id] ? resolutions[h.id] : {};
31
+ return conflictChoiceText(h, r.choice, r.value);
32
+ }).join('\n');
33
+ }
34
+
35
+ function renderConflictCode(text, lang) {
36
+ const pre = el('pre', { class: 'gitconf-pre' });
37
+ const code = el('code');
38
+ code.innerHTML = tintCode(text || ' ', lang);
39
+ pre.append(code);
40
+ return pre;
41
+ }
42
+
43
+ function renderGitConflict(block, ctx, blockId) {
44
+ const editable = ctx.canEditBlocks !== false;
45
+ const conflicts = Array.isArray(block.conflicts) ? block.conflicts : [];
46
+ const prior = ctx && ctx.edits && ctx.edits[blockId] && typeof ctx.edits[blockId] === 'object'
47
+ ? ctx.edits[blockId]
48
+ : {};
49
+ const resolutions = prior.resolutions && typeof prior.resolutions === 'object' ? { ...prior.resolutions } : {};
50
+ const wrap = el('div', { class: 'gitconf' });
51
+ const count = conflicts.length;
52
+ const title = block.title || block.filename || 'Git conflict';
53
+ const status = el('span', { class: 'gitconf-status' }, `${count} conflict${count === 1 ? '' : 's'}`);
54
+ const resolvedPreview = el('pre', { class: 'gitconf-resolved' }, el('code'));
55
+
56
+ function emit() {
57
+ const touched = Object.keys(resolutions).filter((id) => resolutions[id] && resolutions[id].choice);
58
+ const payload = touched.length
59
+ ? {
60
+ type: 'git-conflict-resolution',
61
+ filename: block.filename || '',
62
+ file: block.file || '',
63
+ resolved: touched.length === count,
64
+ resolutions,
65
+ content: joinResolvedParts(block, resolutions),
66
+ }
67
+ : null;
68
+ if (ctx && typeof ctx.onBlockEdit === 'function') ctx.onBlockEdit(blockId, payload);
69
+ wrap.dispatchEvent(new CustomEvent('relay:block-edit', {
70
+ bubbles: true,
71
+ detail: { blockId, value: payload },
72
+ }));
73
+ resolvedPreview.querySelector('code').textContent = joinResolvedParts(block, resolutions);
74
+ status.textContent = `${touched.length}/${count} resolved`;
75
+ status.classList.toggle('is-complete', touched.length === count);
76
+ }
77
+
78
+ wrap.append(el('div', { class: 'gitconf-head' },
79
+ el('div', {},
80
+ el('div', { class: 'gitconf-title' }, title),
81
+ block.file ? el('div', { class: 'gitconf-file' }, block.file) : null),
82
+ status
83
+ ));
84
+
85
+ conflicts.forEach((h, idx) => {
86
+ const body = el('div', { class: 'gitconf-body' });
87
+ const custom = el('textarea', {
88
+ class: 'gitconf-custom',
89
+ spellcheck: 'false',
90
+ rows: '5',
91
+ placeholder: 'Write the resolved content for this hunk',
92
+ disabled: editable ? null : '',
93
+ });
94
+ const buttons = [];
95
+ const setChoice = (choice) => {
96
+ const current = resolutions[h.id] || {};
97
+ const next = { choice, value: choice === 'custom' ? custom.value : conflictChoiceText(h, choice, custom.value) };
98
+ resolutions[h.id] = next;
99
+ for (const b of buttons) b.el.classList.toggle('sel', b.choice === choice);
100
+ body.classList.toggle('is-custom', choice === 'custom');
101
+ emit();
102
+ };
103
+ const makeButton = (choice, label) => {
104
+ const b = el('button', { type: 'button', class: 'gitconf-choice', disabled: editable ? null : '' }, label);
105
+ b.addEventListener('mousedown', (e) => e.stopPropagation());
106
+ b.addEventListener('click', (e) => { e.preventDefault(); e.stopPropagation(); setChoice(choice); });
107
+ buttons.push({ choice, el: b });
108
+ return b;
109
+ };
110
+ const existing = resolutions[h.id] || {};
111
+ if (existing.choice === 'custom') custom.value = existing.value || '';
112
+ else custom.value = conflictChoiceText(h, existing.choice || 'ours', '');
113
+ custom.addEventListener('input', () => setChoice('custom'));
114
+
115
+ body.append(el('div', { class: 'gitconf-choicebar' },
116
+ makeButton('ours', 'Use ours'),
117
+ makeButton('theirs', 'Use theirs'),
118
+ makeButton('both', 'Keep both'),
119
+ makeButton('custom', 'Custom')
120
+ ));
121
+ body.append(el('div', { class: 'gitconf-sides' },
122
+ el('section', { class: 'gitconf-side gitconf-ours' },
123
+ el('div', { class: 'gitconf-label' }, h.oursLabel || 'ours'),
124
+ renderConflictCode(h.ours || '', block.lang)),
125
+ h.baseLabel ? el('section', { class: 'gitconf-side gitconf-base' },
126
+ el('div', { class: 'gitconf-label' }, h.baseLabel),
127
+ renderConflictCode(h.base || '', block.lang)) : null,
128
+ el('section', { class: 'gitconf-side gitconf-theirs' },
129
+ el('div', { class: 'gitconf-label' }, h.theirsLabel || 'theirs'),
130
+ renderConflictCode(h.theirs || '', block.lang))
131
+ ));
132
+ body.append(custom);
133
+
134
+ const card = el('section', { class: 'gitconf-card' },
135
+ el('div', { class: 'gitconf-cardhead' },
136
+ el('span', { class: 'gitconf-badge' }, `Conflict ${idx + 1}`),
137
+ el('span', { class: 'gitconf-hint' }, 'Choose a side, keep both, or write the final hunk.')),
138
+ body
139
+ );
140
+ wrap.append(card);
141
+ if (existing.choice) setChoice(existing.choice);
142
+ });
143
+
144
+ wrap.append(el('details', { class: 'gitconf-preview' },
145
+ el('summary', {}, 'Resolved file preview'),
146
+ resolvedPreview
147
+ ));
148
+ emit();
149
+ ctx && ctx.annotate && ctx.annotate.enableTextSelection(wrap, { blockId, questionId: ctx.questionId });
150
+ return wrap;
151
+ }
@@ -0,0 +1,57 @@
1
+ // ---------- graphviz (offline, vendored Viz.js -> SVG) ----------
2
+ // Same sizing rule as mermaid: never upscale past the diagram's natural
3
+ // width; shrink on narrow screens. Authors set their own colors so there is
4
+ // no theme re-render.
5
+ function sizeDiagramSvg(svgEl) {
6
+ if (!svgEl) return;
7
+ svgEl.removeAttribute('height');
8
+ svgEl.removeAttribute('width');
9
+ const vb = svgEl.viewBox && svgEl.viewBox.baseVal;
10
+ if (vb && vb.width > 0) {
11
+ svgEl.style.width = '100%';
12
+ svgEl.style.maxWidth = Math.ceil(vb.width) + 'px';
13
+ svgEl.style.height = 'auto';
14
+ } else {
15
+ svgEl.style.maxWidth = '100%';
16
+ }
17
+ }
18
+
19
+ function renderGraphviz(block, ctx, blockId) {
20
+ const container = el('div', { class: 'blk-graphviz' });
21
+ // honor authored height as the scrollable viewport (see renderMermaid)
22
+ if (block.height != null) container.style.maxHeight = clampHeight(block.height, 1200) + 'px';
23
+ loadViz()
24
+ .then((Viz) => Viz.instance())
25
+ .then((viz) => {
26
+ const svgEl = viz.renderSVGElement(block.dot || '');
27
+ sizeDiagramSvg(svgEl);
28
+ container.replaceChildren(svgEl);
29
+ attachViewer(container, { zoomEl: svgEl, natural: svgNatural(svgEl), label: 'graph', comment: wholeBlockComment(ctx, blockId, 'graph') });
30
+ if (!ctx.annotate) return;
31
+ const parts = svgEl.querySelectorAll('g.node, g.edge');
32
+ parts.forEach((g) => {
33
+ const titleEl = g.querySelector('title');
34
+ const nodeId = (g.id || (titleEl && titleEl.textContent) || '').trim();
35
+ // Label text lives in <text> elements; g.textContent would also
36
+ // include the <title> child and duplicate the label.
37
+ const labels = Array.from(g.querySelectorAll('text')).map((t) => t.textContent.trim()).filter(Boolean);
38
+ const text = (labels.join(' ') || (titleEl && titleEl.textContent) || '').trim().slice(0, 120);
39
+ ctx.annotate.register(g, {
40
+ blockId,
41
+ questionId: ctx.questionId,
42
+ target: {
43
+ kind: 'graphviz-node',
44
+ nodeId,
45
+ text,
46
+ },
47
+ });
48
+ });
49
+ })
50
+ .catch((err) => {
51
+ container.replaceChildren(
52
+ el('div', { class: 'blk-error' }, 'Graphviz error: ' + (err && err.message ? err.message : String(err)))
53
+ );
54
+ });
55
+ return container;
56
+ }
57
+
@@ -0,0 +1,45 @@
1
+ // ---------- html (sandboxed iframe) ----------
2
+ function syncHtmlHoverComments(frame) {
3
+ try {
4
+ frame.contentWindow.postMessage({ relay: 'annotate-hover', enabled: frame.dataset.relayHoverComments !== 'off' }, '*');
5
+ } catch (_) {}
6
+ }
7
+
8
+ // The kit can load after the iframe's load event; its ready message also
9
+ // restores the current toggle when the embedded page reloads.
10
+ window.addEventListener('message', (e) => {
11
+ if (!e.data || e.data.relay !== 'annotate-ready') return;
12
+ for (const frame of document.querySelectorAll('iframe.viz[data-relay-hover-comments]')) {
13
+ if (frame.contentWindow === e.source) { syncHtmlHoverComments(frame); break; }
14
+ }
15
+ });
16
+
17
+ function renderHtml(block, ctx, blockId) {
18
+ const height = clampHeight(block.height, 360);
19
+ const iframe = el('iframe', {
20
+ class: 'viz',
21
+ 'data-block-id': blockId,
22
+ 'data-question-id': ctx.questionId || '',
23
+ 'data-relay-hover-comments': 'on',
24
+ src: ctx.htmlSrc(blockId),
25
+ height: String(height),
26
+ sandbox: 'allow-scripts allow-forms allow-popups allow-modals',
27
+ loading: 'lazy',
28
+ });
29
+ iframe.addEventListener('load', () => syncHtmlHoverComments(iframe));
30
+ const wrap = el('div', { class: 'blk-htmlwrap' }, iframe);
31
+ attachViewer(wrap, {
32
+ zoomEl: null,
33
+ label: 'embed',
34
+ comment: wholeBlockComment(ctx, blockId, 'embed'),
35
+ hoverComments: {
36
+ isEnabled: () => iframe.dataset.relayHoverComments !== 'off',
37
+ setEnabled: (enabled) => {
38
+ iframe.dataset.relayHoverComments = enabled ? 'on' : 'off';
39
+ syncHtmlHoverComments(iframe);
40
+ },
41
+ },
42
+ });
43
+ return wrap;
44
+ }
45
+