@khanglvm/relay 0.19.0 → 0.20.1
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/README.md +11 -4
- package/bin/rly.js +1 -1
- package/package.json +4 -4
- package/src/cli/args.js +46 -0
- package/src/cli/ask.js +12 -0
- package/src/cli/boards.js +326 -0
- package/src/cli/config.js +56 -0
- package/src/cli/context.js +13 -0
- package/src/cli/git.js +183 -0
- package/src/cli/help.js +91 -0
- package/src/cli/index.js +103 -0
- package/src/cli/install.js +197 -0
- package/src/cli/io.js +186 -0
- package/src/cli/mcp.js +94 -0
- package/src/cli/share.js +45 -0
- package/src/cli/skill.js +192 -0
- package/src/cli/upgrade.js +130 -0
- package/src/cli/wait.js +172 -0
- package/src/file-preview.js +1 -1
- package/src/index.js +1 -1
- package/src/link.js +57 -9
- package/src/mcp-ui/board/block-ctx.js +43 -0
- package/src/mcp-ui/board/bridge.js +165 -0
- package/src/mcp-ui/board/controls.js +310 -0
- package/src/mcp-ui/board/core.js +20 -0
- package/src/mcp-ui/board/lifecycle.js +118 -0
- package/src/mcp-ui/board/normalize.js +95 -0
- package/src/mcp-ui/board/render.js +200 -0
- package/src/mcp-ui/board/state.js +106 -0
- package/src/mcp-ui/board/vendors.js +86 -0
- package/src/mcp.js +12 -18
- package/src/{server.js → server/board.js} +16 -779
- package/src/server/files.js +141 -0
- package/src/server/http.js +114 -0
- package/src/server/index.js +2 -0
- package/src/server/page.js +183 -0
- package/src/server/paths.js +7 -0
- package/src/server/result.js +58 -0
- package/src/server/reviews.js +41 -0
- package/src/server/sanitize.js +231 -0
- package/src/spec/blocks/a2ui.js +25 -0
- package/src/spec/blocks/chart.js +49 -0
- package/src/spec/blocks/code.js +38 -0
- package/src/spec/blocks/diagram.js +39 -0
- package/src/spec/blocks/git-conflict.js +106 -0
- package/src/spec/blocks/image.js +32 -0
- package/src/spec/blocks/index.js +33 -0
- package/src/spec/blocks/markdown.js +13 -0
- package/src/spec/blocks/pdf.js +38 -0
- package/src/spec/blocks/table.js +122 -0
- package/src/spec/blocks/video.js +68 -0
- package/src/spec/blocks/widgets.js +98 -0
- package/src/spec/constants.js +65 -0
- package/src/spec/index.js +5 -0
- package/src/spec/normalize.js +285 -0
- package/src/spec/ready.js +106 -0
- package/src/spec/schema.js +182 -0
- package/src/spec/sources.js +83 -0
- package/src/store.js +1 -1
- package/src/ui/annotate/api.js +149 -0
- package/src/ui/annotate/badges.js +77 -0
- package/src/ui/annotate/chrome.js +158 -0
- package/src/ui/annotate/core.js +134 -0
- package/src/ui/annotate/highlights.js +129 -0
- package/src/ui/annotate/mutations.js +127 -0
- package/src/ui/annotate/popover.js +206 -0
- package/src/ui/annotate/summary.js +67 -0
- package/src/ui/app/annotations.js +88 -0
- package/src/ui/app/autosave.js +44 -0
- package/src/ui/app/core.js +74 -0
- package/src/ui/app/drafts.js +201 -0
- package/src/ui/app/heartbeat.js +150 -0
- package/src/ui/app/iframe-bridge.js +64 -0
- package/src/ui/app/persistence.js +117 -0
- package/src/ui/app/prefilled.js +21 -0
- package/src/ui/app/presence.js +38 -0
- package/src/ui/app/question-color.js +67 -0
- package/src/ui/app/questions.js +360 -0
- package/src/ui/app/render.js +135 -0
- package/src/ui/app/share.js +108 -0
- package/src/ui/app/submit.js +109 -0
- package/src/ui/app/theme.js +63 -0
- package/src/ui/blocks/api.js +1 -0
- package/src/ui/blocks/chart.js +222 -0
- package/src/ui/blocks/code.js +138 -0
- package/src/ui/blocks/compare.js +82 -0
- package/src/ui/blocks/core.js +74 -0
- package/src/ui/blocks/diff.js +361 -0
- package/src/ui/blocks/dispatch.js +111 -0
- package/src/ui/blocks/file-links.js +180 -0
- package/src/ui/blocks/git-conflict.js +151 -0
- package/src/ui/blocks/graphviz.js +57 -0
- package/src/ui/blocks/html.js +45 -0
- package/src/ui/blocks/image.js +371 -0
- package/src/ui/blocks/kpi.js +26 -0
- package/src/ui/blocks/markdown.js +223 -0
- package/src/ui/blocks/mermaid.js +242 -0
- package/src/ui/blocks/palette.js +59 -0
- package/src/ui/blocks/pdf.js +21 -0
- package/src/ui/blocks/plantuml.js +152 -0
- package/src/ui/blocks/table.js +155 -0
- package/src/ui/blocks/typography.js +25 -0
- package/src/ui/blocks/vendor.js +43 -0
- package/src/ui/blocks/video.js +43 -0
- package/src/ui/blocks/viewer-fullscreen.js +146 -0
- package/src/ui/blocks/viewer.js +324 -0
- package/src/ui/kit.js +1 -0
- package/src/ui/manifest.js +140 -0
- package/types/index.d.ts +1 -1
- package/types/spec/blocks/a2ui.d.ts +4 -0
- package/types/spec/blocks/chart.d.ts +4 -0
- package/types/spec/blocks/code.d.ts +10 -0
- package/types/spec/blocks/diagram.d.ts +15 -0
- package/types/spec/blocks/git-conflict.d.ts +7 -0
- package/types/spec/blocks/image.d.ts +5 -0
- package/types/spec/blocks/index.d.ts +30 -0
- package/types/spec/blocks/markdown.d.ts +5 -0
- package/types/spec/blocks/pdf.d.ts +4 -0
- package/types/spec/blocks/table.d.ts +9 -0
- package/types/spec/blocks/video.d.ts +4 -0
- package/types/spec/blocks/widgets.d.ts +21 -0
- package/types/spec/constants.d.ts +67 -0
- package/types/spec/index.d.ts +4 -0
- package/types/spec/normalize.d.ts +17 -0
- package/types/spec/ready.d.ts +1 -0
- package/types/{spec.d.ts → spec/schema.d.ts} +0 -20
- package/types/spec/sources.d.ts +14 -0
- package/types/ui/manifest.d.ts +62 -0
- package/src/cli.js +0 -1767
- package/src/mcp-ui/board.js +0 -1162
- package/src/spec.js +0 -1224
- package/src/ui/annotate.js +0 -1055
- package/src/ui/app.js +0 -1643
- package/src/ui/blocks.js +0 -3260
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// ---------- tiny DOM helper (mirrors app.js idiom) ----------
|
|
2
|
+
function el(tag, attrs = {}, ...children) {
|
|
3
|
+
const n = document.createElement(tag);
|
|
4
|
+
for (const [k, v] of Object.entries(attrs)) {
|
|
5
|
+
if (v === undefined || v === null) continue;
|
|
6
|
+
if (k === 'class') n.className = v;
|
|
7
|
+
else if (k.startsWith('on')) n.addEventListener(k.slice(2), v);
|
|
8
|
+
else n.setAttribute(k, v);
|
|
9
|
+
}
|
|
10
|
+
for (const c of children.flat()) {
|
|
11
|
+
if (c !== null && c !== undefined) n.append(c.nodeType ? c : document.createTextNode(c));
|
|
12
|
+
}
|
|
13
|
+
return n;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function esc(s) {
|
|
17
|
+
return String(s)
|
|
18
|
+
.replace(/&/g, '&')
|
|
19
|
+
.replace(/</g, '<')
|
|
20
|
+
.replace(/>/g, '>')
|
|
21
|
+
.replace(/"/g, '"')
|
|
22
|
+
.replace(/'/g, ''');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// ---------- chart palette (from contract) ----------
|
|
26
|
+
const PALETTE_LIGHT = ['#c2674b', '#4d8a66', '#5a7ca8', '#b9913f', '#8a6da3', '#57534e'];
|
|
27
|
+
// Brighter/more saturated than the surfaces behind them so series stay legible
|
|
28
|
+
// on the dark card (#282624) — the muted set washed out at a glance.
|
|
29
|
+
const PALETTE_DARK = ['#e8a07a', '#74cfa0', '#88b4e8', '#e3c46a', '#c4a0db', '#c2b9ad'];
|
|
30
|
+
|
|
31
|
+
function cssVar(name) {
|
|
32
|
+
return getComputedStyle(document.documentElement).getPropertyValue(name).trim();
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// ---------- local file paths → click-to-open links ----------
|
|
36
|
+
// Agents routinely write a path (~/clip.mp4, ./src/app.js, /abs/file) and the
|
|
37
|
+
// user expects to click it open, not copy it into a terminal. We turn such
|
|
38
|
+
// paths into links that preview files in a dialog, with an explicit app-open
|
|
39
|
+
// fallback. FILE_PATH_RE / looksLikeLocalPath MUST match the same logic
|
|
40
|
+
// in server.js so the page only links what the server will agree to open.
|
|
41
|
+
// The body class also excludes \0 (the placeholder delimiter mdInline uses
|
|
42
|
+
// below) so a path butted against a stashed span can't swallow it; \0 never
|
|
43
|
+
// appears in real text, so this doesn't change which real paths match.
|
|
44
|
+
const FILE_PATH_RE =
|
|
45
|
+
/(?<![\w@:./])(?:file:\/\/\/?[^\s)<>"'`*\0]+|~\/[^\s)<>"'`*\0]+|\.{1,2}\/[^\s)<>"'`*\0]+|\/[^\s)<>"'`*\0]+|[A-Za-z]:[\\/][^\s)<>"'`*\0]+)/g;
|
|
46
|
+
|
|
47
|
+
// Note: candidates here are HTML-escaped (esc() ran first), but escaping only
|
|
48
|
+
// touches & < > " ' — none of which appear in a path's structural prefix, so
|
|
49
|
+
// these tests are safe to run on the escaped text. Mirrors server.js.
|
|
50
|
+
function looksLikeLocalPath(s) {
|
|
51
|
+
if (typeof s !== 'string') return false;
|
|
52
|
+
const t = s.trim();
|
|
53
|
+
if (!t || /\s/.test(t)) return false;
|
|
54
|
+
if (/^file:\/\//i.test(t)) return true;
|
|
55
|
+
if (/^[A-Za-z]:[\\/]/.test(t)) return true; // windows drive
|
|
56
|
+
if (t === '~' || /^~\//.test(t)) return true;
|
|
57
|
+
if (/^\.\.?\//.test(t)) return true; // ./ or ../
|
|
58
|
+
if (t.startsWith('/')) return /\/[^/]+\/[^/]/.test(t) || /\.[A-Za-z0-9]{1,8}$/.test(t);
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Builds the <a> for a file path. `raw` and `label` are already escaped, so
|
|
63
|
+
// they embed safely inside the attribute and text. codeStyle keeps the
|
|
64
|
+
// monospace look when the path came from a `backtick` span.
|
|
65
|
+
function fileLinkHtml(raw, label, codeStyle) {
|
|
66
|
+
const cls = 'rly-filelink' + (codeStyle ? ' rly-filelink-code' : '');
|
|
67
|
+
return (
|
|
68
|
+
`<a class="${cls}" role="link" tabindex="0" data-rly-open="${raw}" ` +
|
|
69
|
+
`title="Preview ${raw}" aria-haspopup="dialog">` +
|
|
70
|
+
`<span class="rly-filelink-ico" aria-hidden="true"></span>` +
|
|
71
|
+
`<span class="rly-filelink-txt">${label}</span></a>`
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
// ---------- diff (unified / git diff) ----------
|
|
2
|
+
// No git is involved — the agent supplies the diff text. Parsed once into
|
|
3
|
+
// rows, then rendered either UNIFIED (one column, +/- signs) or SPLIT (old |
|
|
4
|
+
// new side-by-side). A header toggle flips between the two live; the block's
|
|
5
|
+
// "view" sets the initial mode. Each code line is tinted by lang.
|
|
6
|
+
|
|
7
|
+
// Parse a unified diff into a flat row list: {kind, text, oldNo, newNo}
|
|
8
|
+
// (line numbers stored 0-based; rendered +1).
|
|
9
|
+
function parseDiff(text) {
|
|
10
|
+
const lines = String(text || '').replace(/\r\n?/g, '\n').split('\n');
|
|
11
|
+
const rows = [];
|
|
12
|
+
let oldNo = 0;
|
|
13
|
+
let newNo = 0;
|
|
14
|
+
// Diff content (+/-/context) only has meaning INSIDE a hunk. Lines before the
|
|
15
|
+
// first @@ are either recognized headers (handled below) or the commit
|
|
16
|
+
// header/message of `git show`/`git log -p` output — skip the latter so it
|
|
17
|
+
// isn't rendered as bogus context rows (which split view duplicates on both
|
|
18
|
+
// sides as long unwrapped lines, shoving the new side off-screen).
|
|
19
|
+
let inHunk = false;
|
|
20
|
+
for (const line of lines) {
|
|
21
|
+
if (/^@@/.test(line)) {
|
|
22
|
+
const m = line.match(/@@\s*-(\d+)(?:,\d+)?\s+\+(\d+)(?:,\d+)?\s*@@/);
|
|
23
|
+
if (m) { oldNo = Number(m[1]) - 1; newNo = Number(m[2]) - 1; }
|
|
24
|
+
inHunk = true;
|
|
25
|
+
rows.push({ kind: 'hunk', text: line });
|
|
26
|
+
} else if (/^(diff |index |--- |\+\+\+ |new file|deleted file|old mode|new mode|similarity|rename |copy )/.test(line)) {
|
|
27
|
+
rows.push({ kind: 'meta', text: line });
|
|
28
|
+
} else if (!inHunk) {
|
|
29
|
+
continue; // commit header/message preamble before the first hunk — drop it
|
|
30
|
+
} else if (line[0] === '+') {
|
|
31
|
+
rows.push({ kind: 'add', text: line.slice(1), newNo: newNo++ });
|
|
32
|
+
} else if (line[0] === '-') {
|
|
33
|
+
rows.push({ kind: 'del', text: line.slice(1), oldNo: oldNo++ });
|
|
34
|
+
} else {
|
|
35
|
+
rows.push({ kind: 'ctx', text: line[0] === ' ' ? line.slice(1) : line, oldNo: oldNo++, newNo: newNo++ });
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
return rows;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function diffNoTd(n, extraClass) {
|
|
42
|
+
const td = el('td', { class: 'diff-no' + (extraClass ? ' ' + extraClass : ''), 'aria-hidden': 'true' });
|
|
43
|
+
if (n !== undefined && n !== null) td.textContent = String(n + 1);
|
|
44
|
+
return td;
|
|
45
|
+
}
|
|
46
|
+
function diffCodeTd(html, extraClass) {
|
|
47
|
+
const c = el('code');
|
|
48
|
+
c.innerHTML = html || ' ';
|
|
49
|
+
return el('td', { class: 'diff-code' + (extraClass ? ' ' + extraClass : '') }, c);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Unified: oldNo | newNo | sign | code
|
|
53
|
+
function buildUnifiedDiff(rows, lang) {
|
|
54
|
+
const tbody = el('tbody');
|
|
55
|
+
for (const r of rows) {
|
|
56
|
+
const full = r.kind === 'hunk' || r.kind === 'meta';
|
|
57
|
+
const codeHtml = full ? esc(r.text) : tintCode(r.text, lang);
|
|
58
|
+
const sign = r.kind === 'add' ? '+' : r.kind === 'del' ? '−' : '';
|
|
59
|
+
tbody.append(el('tr', { class: 'diff-row diff-' + r.kind },
|
|
60
|
+
diffNoTd(r.oldNo),
|
|
61
|
+
diffNoTd(r.newNo),
|
|
62
|
+
el('td', { class: 'diff-sign', 'aria-hidden': 'true' }, sign),
|
|
63
|
+
diffCodeTd(codeHtml)
|
|
64
|
+
));
|
|
65
|
+
}
|
|
66
|
+
return el('table', { class: 'blk-difftable' }, tbody);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Split: oldNo | old code || newNo | new code. A run of removed lines is
|
|
70
|
+
// paired row-by-row with the following run of added lines; the shorter side
|
|
71
|
+
// gets filler cells. Context shows identically on both sides.
|
|
72
|
+
function buildSplitDiff(rows, lang) {
|
|
73
|
+
const tbody = el('tbody');
|
|
74
|
+
const fullRow = (cls, text) => {
|
|
75
|
+
const c = el('code');
|
|
76
|
+
c.innerHTML = esc(text) || ' ';
|
|
77
|
+
tbody.append(el('tr', { class: 'diff-row diff-' + cls }, el('td', { class: 'diff-code', colspan: '4' }, c)));
|
|
78
|
+
};
|
|
79
|
+
let dels = [];
|
|
80
|
+
let adds = [];
|
|
81
|
+
const flush = () => {
|
|
82
|
+
const n = Math.max(dels.length, adds.length);
|
|
83
|
+
for (let i = 0; i < n; i++) {
|
|
84
|
+
const d = dels[i];
|
|
85
|
+
const a = adds[i];
|
|
86
|
+
const left = d
|
|
87
|
+
? [diffNoTd(d.oldNo, 'diff-del'), diffCodeTd(tintCode(d.text, lang), 'diff-del')]
|
|
88
|
+
: [diffNoTd(null, 'diff-fill'), diffCodeTd(' ', 'diff-fill')];
|
|
89
|
+
const right = a
|
|
90
|
+
? [diffNoTd(a.newNo, 'diff-newside diff-add'), diffCodeTd(tintCode(a.text, lang), 'diff-add')]
|
|
91
|
+
: [diffNoTd(null, 'diff-fill diff-newside'), diffCodeTd(' ', 'diff-fill')];
|
|
92
|
+
tbody.append(el('tr', { class: 'diff-row' }, left[0], left[1], right[0], right[1]));
|
|
93
|
+
}
|
|
94
|
+
dels = [];
|
|
95
|
+
adds = [];
|
|
96
|
+
};
|
|
97
|
+
for (const r of rows) {
|
|
98
|
+
if (r.kind === 'del') { dels.push(r); continue; }
|
|
99
|
+
if (r.kind === 'add') { adds.push(r); continue; }
|
|
100
|
+
flush();
|
|
101
|
+
if (r.kind === 'hunk') fullRow('hunk', r.text);
|
|
102
|
+
else if (r.kind === 'meta') fullRow('meta', r.text);
|
|
103
|
+
else {
|
|
104
|
+
const html = tintCode(r.text, lang);
|
|
105
|
+
tbody.append(el('tr', { class: 'diff-row diff-ctx' },
|
|
106
|
+
diffNoTd(r.oldNo), diffCodeTd(html),
|
|
107
|
+
diffNoTd(r.newNo, 'diff-newside'), diffCodeTd(html)
|
|
108
|
+
));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
flush();
|
|
112
|
+
return el('table', { class: 'blk-difftable blk-difftable-split' }, tbody);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Split a (possibly multi-file) unified diff into per-file chunks. Each
|
|
116
|
+
// `diff --git a/x b/x` starts a new file; the name comes from that line (or a
|
|
117
|
+
// following `+++ b/...`). A plain hunk-only diff (no `diff --git`) is one
|
|
118
|
+
// unnamed file, preserving the single-file path that uses block.filename.
|
|
119
|
+
function splitDiffFiles(text) {
|
|
120
|
+
const lines = String(text || '').replace(/\r\n?/g, '\n').split('\n');
|
|
121
|
+
const files = [];
|
|
122
|
+
let cur = null;
|
|
123
|
+
for (const line of lines) {
|
|
124
|
+
if (/^diff --git /.test(line)) {
|
|
125
|
+
const m = line.match(/^diff --git a\/(.+?) b\/(.+)$/);
|
|
126
|
+
cur = { name: m ? m[2] : 'file', lines: [line] };
|
|
127
|
+
files.push(cur);
|
|
128
|
+
} else {
|
|
129
|
+
if (!cur) { cur = { name: null, lines: [] }; files.push(cur); }
|
|
130
|
+
cur.lines.push(line);
|
|
131
|
+
if (cur.name === null) {
|
|
132
|
+
const pm = line.match(/^\+\+\+ b?\/(.+)$/);
|
|
133
|
+
if (pm) cur.name = pm[1];
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
return files.map((f) => ({ name: f.name, text: f.lines.join('\n') }));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Per-file header lines are redundant once we render a filename header bar.
|
|
141
|
+
const DIFF_NOISE = /^(diff --git |index |--- |\+\+\+ )/;
|
|
142
|
+
|
|
143
|
+
function buildDiffReviewHunks(files, block) {
|
|
144
|
+
const hunks = [];
|
|
145
|
+
files.forEach((f) => {
|
|
146
|
+
const name = f.name || (files.length === 1 ? (block.filename || '') : '') || 'file';
|
|
147
|
+
let rows = parseDiff(f.text);
|
|
148
|
+
if (name) rows = rows.filter((r) => !(r.kind === 'meta' && DIFF_NOISE.test(r.text)));
|
|
149
|
+
let current = null;
|
|
150
|
+
const push = () => {
|
|
151
|
+
if (!current) return;
|
|
152
|
+
const hasChange = current.rows.some((r) => r.kind === 'add' || r.kind === 'del');
|
|
153
|
+
if (hasChange) hunks.push(current);
|
|
154
|
+
current = null;
|
|
155
|
+
};
|
|
156
|
+
rows.forEach((r) => {
|
|
157
|
+
if (r.kind === 'hunk') {
|
|
158
|
+
push();
|
|
159
|
+
current = { id: 'h' + (hunks.length + 1), file: name, header: r.text, rows: [r] };
|
|
160
|
+
} else if (current) {
|
|
161
|
+
current.rows.push(r);
|
|
162
|
+
} else if (r.kind === 'add' || r.kind === 'del' || r.kind === 'ctx') {
|
|
163
|
+
current = { id: 'h' + (hunks.length + 1), file: name, header: 'Change', rows: [r] };
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
push();
|
|
167
|
+
});
|
|
168
|
+
return hunks;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function renderDiffReview(block, ctx, blockId, files) {
|
|
172
|
+
const hunks = buildDiffReviewHunks(files, block);
|
|
173
|
+
let view = block.view === 'split' ? 'split' : 'unified';
|
|
174
|
+
const prior = ctx && ctx.edits && ctx.edits[blockId] && typeof ctx.edits[blockId] === 'object'
|
|
175
|
+
? ctx.edits[blockId]
|
|
176
|
+
: {};
|
|
177
|
+
const choices = prior.hunks && typeof prior.hunks === 'object' ? { ...prior.hunks } : {};
|
|
178
|
+
const buttonsByHunk = new Map();
|
|
179
|
+
const wrap = el('div', { class: 'blk-codewrap blk-diffwrap diff-review' });
|
|
180
|
+
const status = el('span', { class: 'diff-review-status' }, '');
|
|
181
|
+
const list = el('div', { class: 'diff-review-list' });
|
|
182
|
+
|
|
183
|
+
const emit = () => {
|
|
184
|
+
const picked = {};
|
|
185
|
+
hunks.forEach((h) => {
|
|
186
|
+
const r = choices[h.id];
|
|
187
|
+
if (!r || !['apply', 'skip', 'hold'].includes(r.choice)) return;
|
|
188
|
+
picked[h.id] = { choice: r.choice, file: h.file, header: h.header };
|
|
189
|
+
});
|
|
190
|
+
const touched = Object.keys(picked).length;
|
|
191
|
+
const payload = touched
|
|
192
|
+
? {
|
|
193
|
+
type: 'diff-review',
|
|
194
|
+
reviewKind: block.reviewKind || '',
|
|
195
|
+
commit: block.commit || '',
|
|
196
|
+
title: block.title || block.filename || '',
|
|
197
|
+
resolved: touched === hunks.length,
|
|
198
|
+
hunks: picked,
|
|
199
|
+
}
|
|
200
|
+
: null;
|
|
201
|
+
if (ctx && typeof ctx.onBlockEdit === 'function') ctx.onBlockEdit(blockId, payload);
|
|
202
|
+
wrap.dispatchEvent(new CustomEvent('relay:block-edit', {
|
|
203
|
+
bubbles: true,
|
|
204
|
+
detail: { blockId, value: payload },
|
|
205
|
+
}));
|
|
206
|
+
status.textContent = `${touched}/${hunks.length} reviewed`;
|
|
207
|
+
status.classList.toggle('is-complete', hunks.length > 0 && touched === hunks.length);
|
|
208
|
+
for (const h of hunks) {
|
|
209
|
+
const set = buttonsByHunk.get(h.id) || [];
|
|
210
|
+
const chosen = choices[h.id] && choices[h.id].choice;
|
|
211
|
+
set.forEach((b) => b.el.classList.toggle('sel', b.choice === chosen));
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
const setChoice = (h, choice) => {
|
|
216
|
+
choices[h.id] = { choice, file: h.file, header: h.header };
|
|
217
|
+
emit();
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
const setAll = (choice) => {
|
|
221
|
+
hunks.forEach((h) => {
|
|
222
|
+
choices[h.id] = { choice, file: h.file, header: h.header };
|
|
223
|
+
});
|
|
224
|
+
emit();
|
|
225
|
+
};
|
|
226
|
+
|
|
227
|
+
const makeReviewButton = (label, onClick, extraClass = '') => {
|
|
228
|
+
const b = el('button', { type: 'button', class: 'diff-review-btn' + (extraClass ? ' ' + extraClass : '') }, label);
|
|
229
|
+
b.addEventListener('mousedown', (e) => e.stopPropagation());
|
|
230
|
+
b.addEventListener('click', (e) => {
|
|
231
|
+
e.preventDefault();
|
|
232
|
+
e.stopPropagation();
|
|
233
|
+
onClick();
|
|
234
|
+
});
|
|
235
|
+
return b;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
const repaint = () => {
|
|
239
|
+
list.replaceChildren();
|
|
240
|
+
hunks.forEach((h) => {
|
|
241
|
+
const actionbar = el('div', { class: 'diff-review-hunkbar' },
|
|
242
|
+
el('div', { class: 'diff-review-hunkmeta' },
|
|
243
|
+
el('span', { class: 'diff-review-file' }, h.file),
|
|
244
|
+
el('span', { class: 'diff-review-header' }, h.header)
|
|
245
|
+
)
|
|
246
|
+
);
|
|
247
|
+
const rowButtons = [
|
|
248
|
+
{ choice: 'apply', label: 'Apply hunk' },
|
|
249
|
+
{ choice: 'skip', label: 'Skip hunk' },
|
|
250
|
+
{ choice: 'hold', label: 'Hold' },
|
|
251
|
+
].map((item) => ({
|
|
252
|
+
choice: item.choice,
|
|
253
|
+
el: makeReviewButton(item.label, () => setChoice(h, item.choice), 'diff-review-choice'),
|
|
254
|
+
}));
|
|
255
|
+
buttonsByHunk.set(h.id, rowButtons);
|
|
256
|
+
actionbar.append(el('div', { class: 'diff-review-actions' }, rowButtons.map((b) => b.el)));
|
|
257
|
+
list.append(el('section', { class: 'diff-review-hunk' },
|
|
258
|
+
actionbar,
|
|
259
|
+
view === 'split' ? buildSplitDiff(h.rows, block.lang) : buildUnifiedDiff(h.rows, block.lang)
|
|
260
|
+
));
|
|
261
|
+
});
|
|
262
|
+
emit();
|
|
263
|
+
};
|
|
264
|
+
|
|
265
|
+
const toggle = makeReviewButton('', () => {
|
|
266
|
+
view = view === 'split' ? 'unified' : 'split';
|
|
267
|
+
wrap.classList.toggle('is-split', view === 'split');
|
|
268
|
+
toggle.textContent = view === 'split' ? 'Unified view' : 'Split view';
|
|
269
|
+
repaint();
|
|
270
|
+
}, 'blk-difftoggle');
|
|
271
|
+
toggle.title = 'Toggle side-by-side view';
|
|
272
|
+
toggle.textContent = view === 'split' ? 'Unified view' : 'Split view';
|
|
273
|
+
|
|
274
|
+
const title = block.title || block.filename || (files.length > 1 ? `${files.length} files changed` : 'Diff review');
|
|
275
|
+
wrap.append(el('div', { class: 'blk-codehead diff-review-head' },
|
|
276
|
+
el('span', { class: 'blk-codename' }, title),
|
|
277
|
+
el('div', { class: 'diff-review-topactions' },
|
|
278
|
+
status,
|
|
279
|
+
makeReviewButton('Apply all', () => setAll('apply')),
|
|
280
|
+
makeReviewButton('Skip all', () => setAll('skip')),
|
|
281
|
+
makeReviewButton('Hold all', () => setAll('hold')),
|
|
282
|
+
toggle
|
|
283
|
+
)
|
|
284
|
+
));
|
|
285
|
+
wrap.classList.toggle('is-split', view === 'split');
|
|
286
|
+
wrap.append(list);
|
|
287
|
+
repaint();
|
|
288
|
+
ctx && ctx.annotate && ctx.annotate.enableTextSelection(list, { blockId, questionId: ctx.questionId });
|
|
289
|
+
attachViewer(wrap, { zoomEl: null, label: 'diff review', comment: wholeBlockComment(ctx, blockId, 'diff review') });
|
|
290
|
+
return wrap;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function renderDiff(block, ctx, blockId) {
|
|
294
|
+
const files = splitDiffFiles(block.diff);
|
|
295
|
+
if (block.review === true && ctx.canEditBlocks !== false) return renderDiffReview(block, ctx, blockId, files);
|
|
296
|
+
const named = files.filter((f) => f.name);
|
|
297
|
+
const multi = named.length > 1;
|
|
298
|
+
let view = block.view === 'split' ? 'split' : 'unified';
|
|
299
|
+
const scroll = el('div', { class: 'blk-diffscroll' });
|
|
300
|
+
const sections = [];
|
|
301
|
+
|
|
302
|
+
const paint = () => {
|
|
303
|
+
scroll.replaceChildren();
|
|
304
|
+
sections.length = 0;
|
|
305
|
+
files.forEach((f) => {
|
|
306
|
+
const name = f.name || (files.length === 1 ? (block.filename || '') : '');
|
|
307
|
+
let rows = parseDiff(f.text);
|
|
308
|
+
// drop the pure file-header noise when we show a filename bar
|
|
309
|
+
if (name) rows = rows.filter((r) => !(r.kind === 'meta' && DIFF_NOISE.test(r.text)));
|
|
310
|
+
if (!rows.length) return;
|
|
311
|
+
const sec = el('div', { class: 'diff-file' });
|
|
312
|
+
if (name) sec.append(el('div', { class: 'diff-filehead' }, name));
|
|
313
|
+
sec.append(view === 'split' ? buildSplitDiff(rows, block.lang) : buildUnifiedDiff(rows, block.lang));
|
|
314
|
+
scroll.append(sec);
|
|
315
|
+
sections.push({ name, el: sec });
|
|
316
|
+
});
|
|
317
|
+
};
|
|
318
|
+
paint();
|
|
319
|
+
|
|
320
|
+
const wrap = el('div', { class: 'blk-codewrap blk-diffwrap' });
|
|
321
|
+
// header: file count / single name (left) + a Unified/Split view toggle (right)
|
|
322
|
+
const toggle = el('button', { class: 'blk-difftoggle', type: 'button', title: 'Toggle side-by-side view' });
|
|
323
|
+
const syncToggle = () => { toggle.textContent = view === 'split' ? 'Unified view' : 'Split view'; };
|
|
324
|
+
syncToggle();
|
|
325
|
+
toggle.addEventListener('mousedown', (e) => e.stopPropagation());
|
|
326
|
+
toggle.addEventListener('click', (e) => {
|
|
327
|
+
e.preventDefault();
|
|
328
|
+
e.stopPropagation();
|
|
329
|
+
view = view === 'split' ? 'unified' : 'split';
|
|
330
|
+
wrap.classList.toggle('is-split', view === 'split');
|
|
331
|
+
syncToggle();
|
|
332
|
+
paint();
|
|
333
|
+
});
|
|
334
|
+
const headLabel = multi ? `${named.length} files changed` : (named[0] && named[0].name) || block.filename || '';
|
|
335
|
+
wrap.append(el('div', { class: 'blk-codehead' },
|
|
336
|
+
el('span', { class: 'blk-codename' }, headLabel),
|
|
337
|
+
toggle
|
|
338
|
+
));
|
|
339
|
+
// jump bar of file chips for a multi-file diff (click scrolls to that file)
|
|
340
|
+
if (multi) {
|
|
341
|
+
const bar = el('div', { class: 'diff-files' });
|
|
342
|
+
named.forEach((f) => {
|
|
343
|
+
const chip = el('button', { type: 'button', class: 'diff-file-chip' }, f.name);
|
|
344
|
+
chip.addEventListener('mousedown', (e) => e.stopPropagation());
|
|
345
|
+
chip.addEventListener('click', (e) => {
|
|
346
|
+
e.stopPropagation();
|
|
347
|
+
const s = sections.find((x) => x.name === f.name);
|
|
348
|
+
if (s) s.el.scrollIntoView({ behavior: 'smooth', block: 'start' });
|
|
349
|
+
});
|
|
350
|
+
bar.append(chip);
|
|
351
|
+
});
|
|
352
|
+
wrap.append(bar);
|
|
353
|
+
}
|
|
354
|
+
wrap.classList.toggle('is-split', view === 'split');
|
|
355
|
+
wrap.append(scroll);
|
|
356
|
+
// select-to-comment stays bound to the stable scroll container across toggles
|
|
357
|
+
ctx && ctx.annotate && ctx.annotate.enableTextSelection(scroll, { blockId, questionId: ctx.questionId });
|
|
358
|
+
attachViewer(wrap, { zoomEl: null, label: 'diff', comment: wholeBlockComment(ctx, blockId, 'diff') });
|
|
359
|
+
return wrap;
|
|
360
|
+
}
|
|
361
|
+
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
// ---------- dispatch ----------
|
|
2
|
+
function renderBlock(block, ctx) {
|
|
3
|
+
const blockId = block.id;
|
|
4
|
+
const wrapper = el('div', { class: 'blk blk-' + block.type, 'data-block-id': blockId });
|
|
5
|
+
if (block.ref) wrapper.setAttribute('data-block-ref', block.ref);
|
|
6
|
+
let inner = null;
|
|
7
|
+
switch (block.type) {
|
|
8
|
+
case 'markdown': {
|
|
9
|
+
inner = renderMarkdown(block.md || '');
|
|
10
|
+
wrapper.append(inner);
|
|
11
|
+
ctx.annotate?.enableTextSelection(inner, { blockId, questionId: ctx.questionId });
|
|
12
|
+
break;
|
|
13
|
+
}
|
|
14
|
+
case 'table':
|
|
15
|
+
inner = renderTable(block, ctx, blockId);
|
|
16
|
+
wrapper.append(inner);
|
|
17
|
+
break;
|
|
18
|
+
case 'code':
|
|
19
|
+
inner = renderCode(block, ctx, blockId);
|
|
20
|
+
wrapper.append(inner);
|
|
21
|
+
break;
|
|
22
|
+
case 'diff':
|
|
23
|
+
inner = renderDiff(block, ctx, blockId);
|
|
24
|
+
wrapper.append(inner);
|
|
25
|
+
break;
|
|
26
|
+
case 'git-conflict':
|
|
27
|
+
inner = renderGitConflict(block, ctx, blockId);
|
|
28
|
+
wrapper.append(inner);
|
|
29
|
+
break;
|
|
30
|
+
case 'video':
|
|
31
|
+
inner = renderVideo(block, ctx, blockId);
|
|
32
|
+
wrapper.append(inner);
|
|
33
|
+
break;
|
|
34
|
+
case 'pdf':
|
|
35
|
+
inner = renderPdf(block, ctx, blockId);
|
|
36
|
+
wrapper.append(inner);
|
|
37
|
+
break;
|
|
38
|
+
case 'chart':
|
|
39
|
+
inner = renderChart(block, ctx, blockId);
|
|
40
|
+
wrapper.append(inner);
|
|
41
|
+
break;
|
|
42
|
+
case 'mermaid':
|
|
43
|
+
inner = renderMermaid(block, ctx, blockId);
|
|
44
|
+
wrapper.append(inner);
|
|
45
|
+
break;
|
|
46
|
+
case 'graphviz':
|
|
47
|
+
inner = renderGraphviz(block, ctx, blockId);
|
|
48
|
+
wrapper.append(inner);
|
|
49
|
+
break;
|
|
50
|
+
case 'plantuml':
|
|
51
|
+
inner = renderPlantuml(block, ctx, blockId);
|
|
52
|
+
wrapper.append(inner);
|
|
53
|
+
break;
|
|
54
|
+
case 'html':
|
|
55
|
+
inner = renderHtml(block, ctx, blockId);
|
|
56
|
+
wrapper.append(inner);
|
|
57
|
+
break;
|
|
58
|
+
case 'image':
|
|
59
|
+
inner = renderImage(block, ctx, blockId);
|
|
60
|
+
wrapper.append(inner);
|
|
61
|
+
break;
|
|
62
|
+
case 'palette':
|
|
63
|
+
inner = renderPalette(block, ctx, blockId);
|
|
64
|
+
wrapper.append(inner);
|
|
65
|
+
break;
|
|
66
|
+
case 'kpi':
|
|
67
|
+
inner = renderKpi(block, ctx, blockId);
|
|
68
|
+
wrapper.append(inner);
|
|
69
|
+
break;
|
|
70
|
+
case 'typography':
|
|
71
|
+
inner = renderTypography(block, ctx, blockId);
|
|
72
|
+
wrapper.append(inner);
|
|
73
|
+
break;
|
|
74
|
+
case 'compare':
|
|
75
|
+
inner = renderCompare(block, ctx, blockId);
|
|
76
|
+
wrapper.append(inner);
|
|
77
|
+
break;
|
|
78
|
+
case 'a2ui':
|
|
79
|
+
inner = window.RelayA2ui ? window.RelayA2ui.render(block, ctx, blockId) : el('div', { class: 'blk-error' }, 'A2UI renderer is not loaded.');
|
|
80
|
+
wrapper.append(inner);
|
|
81
|
+
break;
|
|
82
|
+
default:
|
|
83
|
+
wrapper.append(el('div', { class: 'blk-error' }, 'Unknown block type: ' + esc(String(block.type))));
|
|
84
|
+
}
|
|
85
|
+
return wrapper;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// ---------- public API ----------
|
|
89
|
+
async function render(container, blocks, ctx) {
|
|
90
|
+
const list = Array.isArray(blocks) ? blocks : [];
|
|
91
|
+
const safeCtx = {
|
|
92
|
+
theme: ctx && ctx.theme ? ctx.theme : () => 'light',
|
|
93
|
+
htmlSrc: ctx && ctx.htmlSrc ? ctx.htmlSrc : (id) => '/html/b/' + id,
|
|
94
|
+
questionId: ctx && ctx.questionId !== undefined ? ctx.questionId : null,
|
|
95
|
+
annotate: ctx && ctx.annotate ? ctx.annotate : null,
|
|
96
|
+
// editable-mermaid plumbing: edits maps blockId -> edited code; onBlockEdit
|
|
97
|
+
// reports an accepted change (or null to clear back to the original).
|
|
98
|
+
edits: ctx && ctx.edits ? ctx.edits : {},
|
|
99
|
+
canComment: !ctx || ctx.canComment !== false,
|
|
100
|
+
saveArtifact:
|
|
101
|
+
ctx && typeof ctx.saveArtifact === 'function' ? ctx.saveArtifact : null,
|
|
102
|
+
canEditBlocks: !ctx || ctx.canEditBlocks !== false,
|
|
103
|
+
onBlockEdit:
|
|
104
|
+
ctx && typeof ctx.onBlockEdit === 'function' ? ctx.onBlockEdit : () => {},
|
|
105
|
+
};
|
|
106
|
+
for (const block of list) {
|
|
107
|
+
if (!block || !block.type) continue;
|
|
108
|
+
container.append(renderBlock(block, safeCtx));
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|