@khanglvm/relay 0.19.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.
- 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 +90 -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/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} +15 -778
- 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/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,106 @@
|
|
|
1
|
+
// Checks that need async work (mermaid syntax, A2UI Express). They run once,
|
|
2
|
+
// before a board opens, and mark the spec as ready.
|
|
3
|
+
import fs from 'node:fs';
|
|
4
|
+
import path from 'node:path';
|
|
5
|
+
import { fileURLToPath } from 'node:url';
|
|
6
|
+
import vm from 'node:vm';
|
|
7
|
+
import { resolveA2uiBlock } from '../a2ui/compile.js';
|
|
8
|
+
import { CliError } from '../util.js';
|
|
9
|
+
|
|
10
|
+
const VENDOR_DIR = path.join(path.dirname(fileURLToPath(import.meta.url)), '..', '..', 'vendor');
|
|
11
|
+
|
|
12
|
+
let mermaidValidatorPromise = null;
|
|
13
|
+
|
|
14
|
+
function compactError(err) {
|
|
15
|
+
const raw = err && (err.str || err.message) ? (err.str || err.message) : String(err || 'unknown error');
|
|
16
|
+
const msg = String(raw).replace(/\s+/g, ' ').trim();
|
|
17
|
+
return msg.length > 500 ? msg.slice(0, 497) + '...' : msg;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async function mermaidValidator() {
|
|
21
|
+
if (mermaidValidatorPromise) return mermaidValidatorPromise;
|
|
22
|
+
mermaidValidatorPromise = (async () => {
|
|
23
|
+
const sourcePath = path.join(VENDOR_DIR, 'mermaid.min.js');
|
|
24
|
+
let source;
|
|
25
|
+
try {
|
|
26
|
+
source = fs.readFileSync(sourcePath, 'utf8');
|
|
27
|
+
} catch {
|
|
28
|
+
throw new CliError('mermaid blocks need the vendored parser (vendor/mermaid.min.js is missing).');
|
|
29
|
+
}
|
|
30
|
+
const quietConsole = { log() {}, info() {}, warn() {}, error() {}, debug() {} };
|
|
31
|
+
const context = {
|
|
32
|
+
console: quietConsole,
|
|
33
|
+
setTimeout,
|
|
34
|
+
clearTimeout,
|
|
35
|
+
};
|
|
36
|
+
context.globalThis = context;
|
|
37
|
+
context.window = { addEventListener() {} };
|
|
38
|
+
vm.createContext(context);
|
|
39
|
+
try {
|
|
40
|
+
vm.runInContext(source, context, { filename: sourcePath });
|
|
41
|
+
} catch (err) {
|
|
42
|
+
throw new CliError(`could not load mermaid parser: ${compactError(err)}`);
|
|
43
|
+
}
|
|
44
|
+
if (!context.mermaid || typeof context.mermaid.parse !== 'function') {
|
|
45
|
+
throw new CliError('could not load mermaid parser: vendor/mermaid.min.js did not expose mermaid.parse.');
|
|
46
|
+
}
|
|
47
|
+
try {
|
|
48
|
+
context.mermaid.initialize({
|
|
49
|
+
startOnLoad: false,
|
|
50
|
+
securityLevel: 'strict',
|
|
51
|
+
suppressErrorRendering: true,
|
|
52
|
+
});
|
|
53
|
+
} catch {
|
|
54
|
+
// Some Mermaid versions dislike repeated initialize calls; parse below is
|
|
55
|
+
// the real readiness gate.
|
|
56
|
+
}
|
|
57
|
+
return context.mermaid;
|
|
58
|
+
})();
|
|
59
|
+
return mermaidValidatorPromise;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async function assertMermaidSyntax(code, where) {
|
|
63
|
+
const mermaid = await mermaidValidator();
|
|
64
|
+
try {
|
|
65
|
+
const parsed = mermaid.parse(code);
|
|
66
|
+
if (parsed && typeof parsed.then === 'function') await parsed;
|
|
67
|
+
} catch (err) {
|
|
68
|
+
throw new CliError(`${where}: invalid mermaid syntax — ${compactError(err)}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function* allBlocks(spec) {
|
|
73
|
+
const boardBlocks = Array.isArray(spec.blocks) ? spec.blocks : [];
|
|
74
|
+
for (let i = 0; i < boardBlocks.length; i++) {
|
|
75
|
+
yield { block: boardBlocks[i], where: `board.blocks[${i}]${boardBlocks[i]?.id ? ` (${boardBlocks[i].id})` : ''}` };
|
|
76
|
+
}
|
|
77
|
+
const questions = Array.isArray(spec.questions) ? spec.questions : [];
|
|
78
|
+
for (let qi = 0; qi < questions.length; qi++) {
|
|
79
|
+
const q = questions[qi] || {};
|
|
80
|
+
const qBlocks = Array.isArray(q.blocks) ? q.blocks : [];
|
|
81
|
+
for (let bi = 0; bi < qBlocks.length; bi++) {
|
|
82
|
+
yield { block: qBlocks[bi], where: `questions[${qi}].blocks[${bi}]${qBlocks[bi]?.id ? ` (${qBlocks[bi].id})` : ''}` };
|
|
83
|
+
}
|
|
84
|
+
const opts = Array.isArray(q.options) ? q.options : [];
|
|
85
|
+
for (let oi = 0; oi < opts.length; oi++) {
|
|
86
|
+
const oBlocks = Array.isArray(opts[oi]?.blocks) ? opts[oi].blocks : [];
|
|
87
|
+
for (let bi = 0; bi < oBlocks.length; bi++) {
|
|
88
|
+
yield {
|
|
89
|
+
block: oBlocks[bi],
|
|
90
|
+
where: `questions[${qi}].options[${oi}].blocks[${bi}]${oBlocks[bi]?.id ? ` (${oBlocks[bi].id})` : ''}`,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export async function assertSpecReady(spec) {
|
|
98
|
+
if (!spec || typeof spec !== 'object') return spec;
|
|
99
|
+
if (spec.__relayReady === true) return spec;
|
|
100
|
+
for (const { block, where } of allBlocks(spec)) {
|
|
101
|
+
if (block && block.type === 'mermaid') await assertMermaidSyntax(block.code || '', where);
|
|
102
|
+
if (block && block.type === 'a2ui') await resolveA2uiBlock(block, `${where} (a2ui)`);
|
|
103
|
+
}
|
|
104
|
+
Object.defineProperty(spec, '__relayReady', { value: true, enumerable: false, configurable: true });
|
|
105
|
+
return spec;
|
|
106
|
+
}
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// The JSON Schema of a board spec, shown by `rly schema` and used as the MCP tool input.
|
|
2
|
+
import { BLOCK_HEIGHT, BLOCK_TYPES, CHART_KINDS, HTML_HEIGHT } from './constants.js';
|
|
3
|
+
|
|
4
|
+
const BLOCK_SCHEMA = {
|
|
5
|
+
type: 'array',
|
|
6
|
+
description:
|
|
7
|
+
'Rich content blocks rendered in order. Board-level blocks show above the questions; per-question blocks show above the control. Each is annotatable (element-level comments returned in result.annotations).',
|
|
8
|
+
items: {
|
|
9
|
+
type: 'object',
|
|
10
|
+
required: ['type'],
|
|
11
|
+
properties: {
|
|
12
|
+
type: { type: 'string', enum: BLOCK_TYPES },
|
|
13
|
+
md: { type: 'string', description: 'markdown: built-in mini renderer (no external library) — headings, lists, code, quotes, links, images, and GFM pipe tables. Text selections are commentable. For real tabular data prefer a "table" block (sortable + per-cell comments).' },
|
|
14
|
+
mdFile: { type: 'string', description: 'markdown: path to a local .md file to load + render instead of inline "md" (e.g. view a README/plan/report). Resolved against the CWD. Quick view of one or more files: `rly view a.md b.md`.' },
|
|
15
|
+
code: { type: 'string', description: 'mermaid: diagram source (e.g. "graph TD; A-->B"); plantuml: the @startuml…@enduml source; code: the source to display (syntax-highlighted with line numbers).' },
|
|
16
|
+
codeFile: { type: 'string', description: 'code: path to a local source file to load + display instead of inline "code". Resolved against the CWD; lang defaults from the file extension.' },
|
|
17
|
+
filename: { type: 'string', description: 'code/diff/git-conflict: optional file name/path shown as a header label above the block.' },
|
|
18
|
+
editable: { type: 'boolean', description: 'mermaid: when true, render an "Edit diagram" toggle so the user can edit the diagram source live. The edited source is returned in result.blockEdits[<blockId>].' },
|
|
19
|
+
dot: { type: 'string', description: 'graphviz: DOT source (e.g. "digraph { a -> b }"). Rendered offline via vendored Viz.js; nodes and edges are individually commentable.' },
|
|
20
|
+
server: { type: 'string', description: 'plantuml: PlantUML server base URL (http(s)). Defaults to https://www.plantuml.com/plantuml. Diagrams render via this server (needs network).' },
|
|
21
|
+
lang: { type: 'string', description: 'code/diff/git-conflict block: language hint for syntax highlighting (js, ts, py, go, rust, java, c, cpp, csharp, ruby, php, swift, kotlin, sql, yaml, json, sh, css, html, …).' },
|
|
22
|
+
diff: { type: 'string', description: 'diff: a unified diff (git diff / diff -u output) — rendered as a colored, line-numbered comparison with +added / −removed / context rows and file/hunk headers. No git needed; just write/paste the diff text.' },
|
|
23
|
+
diffFile: { type: 'string', description: 'diff: path to a local file containing a unified diff (alternative to "diff"). Resolved against the CWD.' },
|
|
24
|
+
view: { type: 'string', enum: ['unified', 'split'], description: 'diff: initial layout — "unified" (default, one column) or "split" (side-by-side old vs new). The viewer also has a live toggle either way.' },
|
|
25
|
+
review: { type: 'boolean', description: 'diff: when true, render per-hunk Apply / Skip / Hold controls and return choices in result.blockEdits[blockId] as {type:"diff-review", hunks:{...}}. Used by `rly git cherry-pick --code`.' },
|
|
26
|
+
reviewKind: { type: 'string', description: 'diff review: optional label for the review workflow, such as "cherry-pick" or "pick".' },
|
|
27
|
+
commit: { type: 'string', description: 'diff review: optional commit SHA associated with this diff.' },
|
|
28
|
+
express: { type: 'string', description: 'a2ui: a live widget in A2UI Express, e.g. `$/jobs = [{title:"Build",tone:"ok"}]` then `root = Card("CI", [List($/jobs), Button("Retry", action=Event("retry"))])`. Compiled by the A2UI SDK against Relay\'s catalog (run `rly a2ui` for the grammar and every component). Button events, input values come back in result.blockEdits[blockId] as {type:"a2ui", data, events}.' },
|
|
29
|
+
components: { type: 'array', description: 'a2ui: the same widget as flat A2UI v1.0 JSON components (root id "root", a Card) instead of "express".', items: { type: 'object' } },
|
|
30
|
+
dataModel: { type: 'object', description: 'a2ui: the data model for "components" (use `$/path = value` lines with "express" instead).' },
|
|
31
|
+
content: { type: 'string', description: 'git-conflict: inline file content containing conflict markers (<<<<<<< / ======= / >>>>>>>). The board auto-detects each hunk and returns resolutions in result.blockEdits[blockId].' },
|
|
32
|
+
text: { type: 'string', description: 'git-conflict: alias for inline "content".' },
|
|
33
|
+
file: { type: 'string', description: 'git-conflict: local conflicted file path to load, parse, and resolve. Resolved against the CWD.' },
|
|
34
|
+
path: { type: 'string', description: 'git-conflict: alias for local conflicted file path.' },
|
|
35
|
+
config: { type: 'object', description: 'chart: a full Chart.js config object.' },
|
|
36
|
+
kind: { type: 'string', enum: CHART_KINDS, description: 'chart shorthand: chart kind (alternative to "config").' },
|
|
37
|
+
labels: { type: 'array', description: 'chart shorthand: x-axis / category labels.' },
|
|
38
|
+
series: {
|
|
39
|
+
type: 'array',
|
|
40
|
+
description: 'chart shorthand: [{label, data:[...], color?}]. Chart data points are individually commentable.',
|
|
41
|
+
items: { type: 'object', properties: { label: { type: 'string' }, data: { type: 'array' }, color: { type: 'string' } } },
|
|
42
|
+
},
|
|
43
|
+
title: { type: 'string', description: 'chart shorthand: chart title. video: title/caption shown under the player.' },
|
|
44
|
+
columns: {
|
|
45
|
+
type: 'array',
|
|
46
|
+
description: 'table: strings, or {key, label, align?}. Cells are commentable.',
|
|
47
|
+
items: { anyOf: [{ type: 'string' }, { type: 'object' }] },
|
|
48
|
+
},
|
|
49
|
+
rows: { type: 'array', description: 'table: array of arrays (positional) or array of objects (keyed by column key).' },
|
|
50
|
+
rowsFile: { type: 'string', description: 'table: load rows from a local .csv/.tsv/.json file instead of inline "rows". CSV/TSV first row is the header (becomes "columns" if omitted); JSON is an array of objects. Resolved against the CWD. Quick view: `rly view data.csv`.' },
|
|
51
|
+
sortable: { type: 'boolean', description: 'table: enable click-to-sort headers.' },
|
|
52
|
+
filterable: { type: 'boolean', description: 'table: show a filter box that live-filters rows by substring across all cells. Good for large tables.' },
|
|
53
|
+
exportable: { type: 'boolean', description: 'table: show a "CSV" button that downloads the (filtered) rows as a CSV file.' },
|
|
54
|
+
html: { type: 'string', description: 'html: custom markup rendered in a sandboxed iframe.' },
|
|
55
|
+
htmlFile: { type: 'string', description: 'html: path to an HTML file (alternative to "html").' },
|
|
56
|
+
src: { type: 'string', description: 'image: http(s)/data URL, or a local file path (png/jpg/gif/webp/svg/avif/bmp — embedded at spec time, served offline). video: a YouTube/Vimeo URL (embeds an iframe player), an http(s) media URL, or a local video file (mp4/webm/ogv/mov/mkv/m4v — streamed from the server, never embedded).' },
|
|
57
|
+
alt: { type: 'string', description: 'image: alt text / annotation label. video: accessible title for the player.' },
|
|
58
|
+
palettes: {
|
|
59
|
+
type: 'array',
|
|
60
|
+
description: 'palette: one or more color palettes to display as swatch cards. Each swatch reveals its hex on hover and copies it on click; mark one {featured:true} to show it as a larger spotlight. Each item: {name?, sub? (or mood?), tag? (or badge?), tagTone? (warm|cool|neutral|nature|bold|digital), featured?, colors:[…]}.',
|
|
61
|
+
items: {
|
|
62
|
+
type: 'object',
|
|
63
|
+
properties: {
|
|
64
|
+
name: { type: 'string' },
|
|
65
|
+
sub: { type: 'string' },
|
|
66
|
+
mood: { type: 'string' },
|
|
67
|
+
tag: { type: 'string' },
|
|
68
|
+
badge: { type: 'string' },
|
|
69
|
+
tagTone: { type: 'string', enum: ['warm', 'cool', 'neutral', 'nature', 'bold', 'digital'] },
|
|
70
|
+
featured: { type: 'boolean' },
|
|
71
|
+
colors: { type: 'array', items: { type: 'string' }, description: 'CSS colors — #rrggbb, rgb()/hsl(), or named.' },
|
|
72
|
+
},
|
|
73
|
+
required: ['colors'],
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
colors: { type: 'array', items: { type: 'string' }, description: 'palette shorthand: colors for a single palette (use "palettes" for several named ones). Pairs with block-level "name".' },
|
|
77
|
+
name: { type: 'string', description: 'palette shorthand: name for the single "colors" palette.' },
|
|
78
|
+
items: {
|
|
79
|
+
type: 'array',
|
|
80
|
+
description: 'kpi: stat cards. Each {label, value, delta?, dir? (up|down|flat — tints the delta), sub?}. Big-number metrics at a glance ("Revenue $1.2M ↑12%") with no chart.',
|
|
81
|
+
items: { type: 'object', properties: { label: { type: 'string' }, value: { type: 'string' }, delta: { type: 'string' }, dir: { type: 'string', enum: ['up', 'down', 'flat'] }, sub: { type: 'string' } }, required: ['value'] },
|
|
82
|
+
},
|
|
83
|
+
specimens: {
|
|
84
|
+
type: 'array',
|
|
85
|
+
description: 'typography: type specimens rendered at the given style. Each {label?, size? (e.g. "32px"/"2rem"), weight? (e.g. "600"), font?, lineHeight?, letterSpacing?, text?}. Block-level "font" sets a default family.',
|
|
86
|
+
items: { type: 'object', properties: { label: { type: 'string' }, size: { type: 'string' }, weight: { type: 'string' }, font: { type: 'string' }, lineHeight: { type: 'string' }, letterSpacing: { type: 'string' }, text: { type: 'string' } } },
|
|
87
|
+
},
|
|
88
|
+
ref: { type: 'string', description: 'Any block: a stable name (e.g. "velocity") that a markdown reference link [see chart](#ref:velocity) can open in a full-screen modal — so a question can point back to a visual shown earlier instead of making the user scroll up.' },
|
|
89
|
+
pins: { type: 'boolean', description: 'image: enable coordinate pin-comments — the user clicks any point on the image to drop a comment anchored to that spot (Figma-style), returned as a {kind:"image-point", x, y} annotation.' },
|
|
90
|
+
before: { description: 'compare: the "before" image — an http(s)/data URL, a local file path, or {src, label}.' },
|
|
91
|
+
after: { description: 'compare: the "after" image — an http(s)/data URL, a local file path, or {src, label}.' },
|
|
92
|
+
beforeLabel: { type: 'string', description: 'compare: caption for the before side (default "Before").' },
|
|
93
|
+
afterLabel: { type: 'string', description: 'compare: caption for the after side (default "After").' },
|
|
94
|
+
height: { type: 'integer', minimum: BLOCK_HEIGHT.min, maximum: BLOCK_HEIGHT.max, description: 'Block height in px. Defaults: chart 320, html 360; markdown/table/code flow naturally; mermaid/graphviz/plantuml natural (max 800, scrolls; set a larger height to override); image/compare natural.' },
|
|
95
|
+
},
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
export const SPEC_SCHEMA = {
|
|
100
|
+
$schema: 'https://json-schema.org/draft/2020-12/schema',
|
|
101
|
+
title: 'relay board spec',
|
|
102
|
+
type: 'object',
|
|
103
|
+
description:
|
|
104
|
+
'A relay board. Renders an optional intro, board-level blocks, then questions (each with optional per-question blocks), then a submit button. The result JSON contains "answers", "comment", per-question "notes", "annotations" (element-level comments the user attached to any block — see the annotation shape below), and "blockEdits" (a map blockId→edited source for any editable mermaid block the user changed, null when none).',
|
|
105
|
+
properties: {
|
|
106
|
+
version: { const: 1, description: 'Spec version. Optional: a spec without it is read as version 1.' },
|
|
107
|
+
$schema: { type: 'string', description: 'Optional URL of this schema, for editor completion. Relay ignores it.' },
|
|
108
|
+
title: { type: 'string', description: 'Board title (default "Relay")' },
|
|
109
|
+
intro: { type: 'string', description: 'Intro text shown under the title. Newlines preserved.' },
|
|
110
|
+
blocks: BLOCK_SCHEMA,
|
|
111
|
+
html: { type: 'string', description: 'Legacy: board-level custom HTML. Normalized into a single html block PREPENDED to "blocks".' },
|
|
112
|
+
htmlFile: { type: 'string', description: 'Legacy: path to an HTML file (alternative to "html"). Resolved against the CWD. Normalized into a prepended html block.' },
|
|
113
|
+
htmlHeight: { type: 'integer', minimum: HTML_HEIGHT.min, maximum: HTML_HEIGHT.max, default: HTML_HEIGHT.boardDefault, description: 'Legacy: iframe height for the html/htmlFile block.' },
|
|
114
|
+
allowPartial: { type: 'boolean', default: true, description: 'When true, users may submit with unanswered questions (returned in "skipped").' },
|
|
115
|
+
note: { type: 'boolean', default: true, description: 'Show an optional free-text note box ("Anything else?") returned as "comment".' },
|
|
116
|
+
autoClose: { type: 'boolean', default: true, description: 'Try to close the browser tab automatically after submit.' },
|
|
117
|
+
responseRequired: { type: 'boolean', default: true, description: 'Set false for a display-only board: no questions, note box, comments, or Submit/Acknowledge action are shown, and the presenting agent should continue immediately.' },
|
|
118
|
+
submitLabel: { type: 'string', description: 'Submit button label. Defaults: "Submit", or "Acknowledge" when there are no questions.' },
|
|
119
|
+
questions: {
|
|
120
|
+
type: 'array',
|
|
121
|
+
items: {
|
|
122
|
+
type: 'object',
|
|
123
|
+
required: ['label'],
|
|
124
|
+
properties: {
|
|
125
|
+
id: { type: 'string', description: 'Answer key in the result JSON. Defaults to q1, q2, …' },
|
|
126
|
+
type: { type: 'string', enum: ['single', 'multi', 'yesno', 'text', 'textarea', 'scale', 'color', 'rank', 'checklist', 'allocate'], default: 'text' },
|
|
127
|
+
label: { type: 'string' },
|
|
128
|
+
description: { type: 'string' },
|
|
129
|
+
required: { type: 'boolean', default: false },
|
|
130
|
+
options: {
|
|
131
|
+
type: 'array',
|
|
132
|
+
description: 'For single/multi/rank/checklist/allocate. Strings, or {value, label, description, blocks?}. An option\'s "blocks" render INSIDE that option card — use them to show each choice (image/chart/mermaid/html…) instead of describing it in words. "rank": user reorders (drag/↑↓), answer is the ordered values, ≥2 options. "checklist": each option gets a status (see "statuses"), answer is {value: status}. "allocate": user distributes "total" across options, answer is {value: number}.',
|
|
133
|
+
items: {
|
|
134
|
+
anyOf: [
|
|
135
|
+
{ type: 'string' },
|
|
136
|
+
{
|
|
137
|
+
type: 'object',
|
|
138
|
+
properties: {
|
|
139
|
+
value: { type: 'string' },
|
|
140
|
+
label: { type: 'string' },
|
|
141
|
+
description: { type: 'string' },
|
|
142
|
+
blocks: { ...BLOCK_SCHEMA, description: 'Visuals for THIS option, rendered inside its card. Same block types as everywhere else. Keep them compact (height ~140–260).' },
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
],
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
other: { type: 'boolean', description: 'Add a free-text "Other" option (a multi-line textarea); its text is returned verbatim as the value. Defaults ON for "single" (radio) questions so the user is never boxed in — set other:false to remove it; "multi" stays opt-in (other:true).' },
|
|
149
|
+
note: { type: 'boolean', description: 'Small optional free-text field under the question (to qualify an answer). Returned separately as result.notes[questionId]. Defaults to true for the decision types — single, rank, checklist, allocate — so users can qualify their pick; false for other types. Set note:false to hide it, note:true to add it.' },
|
|
150
|
+
placeholder: { type: 'string', description: 'For text/textarea.' },
|
|
151
|
+
default: { description: 'Pre-selected value. Shape matches the answer shape for the type.' },
|
|
152
|
+
min: { type: 'integer', default: 1, description: 'scale only' },
|
|
153
|
+
max: { type: 'integer', default: 5, maximum: 10, description: 'scale only' },
|
|
154
|
+
minLabel: { type: 'string', description: 'scale only' },
|
|
155
|
+
maxLabel: { type: 'string', description: 'scale only' },
|
|
156
|
+
presets: { type: 'array', items: { type: 'string' }, description: 'color only: optional small preset swatches (CSS colors) shown beside the native picker for one-click selection. The answer is returned as a color string.' },
|
|
157
|
+
palette: { type: 'array', description: 'color only: a "pick from a palette" of labeled swatch CARDS — each click selects that color as the answer, and each card is individually commentable (per-color feedback). Items: a CSS color string (hex/rgb/hsl/named — multiple color systems supported via the browser, no library), or {value (or color), label?}. The native picker stays available for a custom color.', items: { anyOf: [{ type: 'string' }, { type: 'object' }] } },
|
|
158
|
+
statuses: { type: 'array', description: 'checklist only: the per-item statuses (default Pass / Fail / N/A). Strings, or {value, label, tone?} where tone ∈ ok|bad|muted|warn tints the chip. Answer is a map {optionValue: statusValue} for the items the user set.', items: { anyOf: [{ type: 'string' }, { type: 'object' }] } },
|
|
159
|
+
total: { type: 'integer', minimum: 1, default: 100, description: 'allocate only: the budget to distribute across options (default 100). Answer is a map {optionValue: number}.' },
|
|
160
|
+
unit: { type: 'string', description: 'allocate only: optional unit label shown next to the total (e.g. "%", "pts", "hrs").' },
|
|
161
|
+
blocks: BLOCK_SCHEMA,
|
|
162
|
+
html: { type: 'string', description: 'Legacy: per-question custom HTML. Normalized into an html block prepended to this question\'s "blocks".' },
|
|
163
|
+
htmlFile: { type: 'string', description: 'Legacy: path to an HTML file (alternative to "html").' },
|
|
164
|
+
htmlHeight: { type: 'integer', minimum: HTML_HEIGHT.min, maximum: HTML_HEIGHT.max, default: HTML_HEIGHT.questionDefault, description: 'Legacy: iframe height for the html/htmlFile block.' },
|
|
165
|
+
},
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
annotations: {
|
|
169
|
+
type: 'array',
|
|
170
|
+
readOnly: true,
|
|
171
|
+
description:
|
|
172
|
+
'Returned in the result (not part of the input spec). Element-level comments the user attached to blocks. Each: {id, questionId|null, blockId|null, target:{kind:"chart-element"|"mermaid-node"|"graphviz-node"|"table-cell"|"text"|"html-element"|"image", …}, text, createdAt}. For html-element, target carries a stable ref + label; users can hover any element of a custom-HTML block (automatic) or ones the author marked with data-relay-annotate.',
|
|
173
|
+
},
|
|
174
|
+
blockEdits: {
|
|
175
|
+
type: 'object',
|
|
176
|
+
readOnly: true,
|
|
177
|
+
description:
|
|
178
|
+
'Returned in the result (not part of the input spec). A map blockId→edited block payload. Editable mermaid blocks return edited source strings. git-conflict blocks return {type:"git-conflict-resolution", resolutions, content, resolved, filename, file}. diff review blocks return {type:"diff-review", hunks, resolved, commit}. a2ui blocks return {type:"a2ui", data (the widget data model with the user input values), events:[{name, context, at}] (each button click, in order)}. null when the user made no edits.',
|
|
179
|
+
},
|
|
180
|
+
},
|
|
181
|
+
anyOf: [{ required: ['questions'] }, { required: ['blocks'] }, { required: ['html'] }, { required: ['htmlFile'] }],
|
|
182
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Readers for text that a block takes inline or from a file next to the spec.
|
|
2
|
+
import fs from 'node:fs';
|
|
3
|
+
import path from 'node:path';
|
|
4
|
+
import { CliError } from '../util.js';
|
|
5
|
+
import { TEXT_FILE_MAX_BYTES } from './constants.js';
|
|
6
|
+
|
|
7
|
+
export const asStr = (v) => (typeof v === 'string' ? v : v == null ? '' : String(v));
|
|
8
|
+
|
|
9
|
+
export function clampInt(v, min, max, def) {
|
|
10
|
+
const n = Number.parseInt(v, 10);
|
|
11
|
+
if (!Number.isFinite(n)) return def;
|
|
12
|
+
return Math.min(max, Math.max(min, n));
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function readHtml(obj, cwd, where) {
|
|
16
|
+
if (typeof obj.html === 'string' && obj.html.trim()) return obj.html;
|
|
17
|
+
if (typeof obj.htmlFile === 'string' && obj.htmlFile.trim()) {
|
|
18
|
+
const p = path.resolve(cwd, obj.htmlFile);
|
|
19
|
+
try {
|
|
20
|
+
return fs.readFileSync(p, 'utf8');
|
|
21
|
+
} catch {
|
|
22
|
+
throw new CliError(`${where}: cannot read htmlFile "${obj.htmlFile}" (resolved: ${p})`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
return '';
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Reads an html-block body from either an inline string or a file path.
|
|
29
|
+
export function readBlockHtml(block, cwd, where) {
|
|
30
|
+
if (typeof block.html === 'string' && block.html) return block.html;
|
|
31
|
+
if (typeof block.htmlFile === 'string' && block.htmlFile.trim()) {
|
|
32
|
+
const p = path.resolve(cwd, block.htmlFile);
|
|
33
|
+
try {
|
|
34
|
+
return fs.readFileSync(p, 'utf8');
|
|
35
|
+
} catch {
|
|
36
|
+
throw new CliError(`${where}: cannot read htmlFile "${block.htmlFile}" (resolved: ${p})`);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
return '';
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Reads a code/diff block body from an inline string field or a local file.
|
|
43
|
+
// `inlineKey` is the inline field (e.g. "code"/"diff"); `fileKey` its file
|
|
44
|
+
// twin (e.g. "codeFile"/"diffFile"). Returns '' when neither is present.
|
|
45
|
+
export function readTextSource(block, inlineKey, fileKey, cwd, where) {
|
|
46
|
+
if (typeof block[inlineKey] === 'string' && block[inlineKey] !== '') return block[inlineKey];
|
|
47
|
+
if (typeof block[fileKey] === 'string' && block[fileKey].trim()) {
|
|
48
|
+
const p = path.resolve(cwd, block[fileKey]);
|
|
49
|
+
let buf;
|
|
50
|
+
try {
|
|
51
|
+
buf = fs.readFileSync(p);
|
|
52
|
+
} catch {
|
|
53
|
+
throw new CliError(`${where}: cannot read ${fileKey} "${block[fileKey]}" (resolved: ${p})`);
|
|
54
|
+
}
|
|
55
|
+
if (buf.length > TEXT_FILE_MAX_BYTES) {
|
|
56
|
+
throw new CliError(`${where}: ${fileKey} "${block[fileKey]}" is ${(buf.length / 1024).toFixed(0)}KB — max ${TEXT_FILE_MAX_BYTES / 1024}KB.`);
|
|
57
|
+
}
|
|
58
|
+
return buf.toString('utf8');
|
|
59
|
+
}
|
|
60
|
+
return '';
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export function readAnyTextSource(block, inlineKeys, fileKeys, cwd, where, label) {
|
|
64
|
+
for (const key of inlineKeys) {
|
|
65
|
+
if (typeof block[key] === 'string' && block[key] !== '') return { text: block[key], sourceKey: key };
|
|
66
|
+
}
|
|
67
|
+
for (const key of fileKeys) {
|
|
68
|
+
if (typeof block[key] === 'string' && block[key].trim()) {
|
|
69
|
+
const p = path.resolve(cwd, block[key]);
|
|
70
|
+
let buf;
|
|
71
|
+
try {
|
|
72
|
+
buf = fs.readFileSync(p);
|
|
73
|
+
} catch {
|
|
74
|
+
throw new CliError(`${where}: cannot read ${label} "${block[key]}" (resolved: ${p})`);
|
|
75
|
+
}
|
|
76
|
+
if (buf.length > TEXT_FILE_MAX_BYTES) {
|
|
77
|
+
throw new CliError(`${where}: ${label} "${block[key]}" is ${(buf.length / 1024).toFixed(0)}KB — max ${TEXT_FILE_MAX_BYTES / 1024}KB.`);
|
|
78
|
+
}
|
|
79
|
+
return { text: buf.toString('utf8'), sourceKey: key, file: p };
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return { text: '', sourceKey: '' };
|
|
83
|
+
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
// ---------- public API ----------
|
|
2
|
+
function normalizeList(value) {
|
|
3
|
+
return Array.isArray(value)
|
|
4
|
+
? value.map((a) => ({
|
|
5
|
+
...a,
|
|
6
|
+
author: a.author === 'agent' ? 'agent' : 'user',
|
|
7
|
+
replies: Array.isArray(a.replies)
|
|
8
|
+
? a.replies
|
|
9
|
+
.filter((r) => r && typeof r === 'object' && typeof r.text === 'string')
|
|
10
|
+
.map((r) => ({
|
|
11
|
+
author: r.author === 'agent' ? 'agent' : 'user',
|
|
12
|
+
text: r.text,
|
|
13
|
+
createdAt: typeof r.createdAt === 'string' ? r.createdAt : '',
|
|
14
|
+
}))
|
|
15
|
+
: [],
|
|
16
|
+
}))
|
|
17
|
+
: [];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function resetCounter() {
|
|
21
|
+
idCounter = 0;
|
|
22
|
+
for (const a of annotations) {
|
|
23
|
+
const m = /^a(\d+)$/.exec(String(a.id || ''));
|
|
24
|
+
if (m) idCounter = Math.max(idCounter, parseInt(m[1], 10));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function init(opts = {}) {
|
|
29
|
+
ensureDom();
|
|
30
|
+
// Normalize threads: missing author -> 'user', missing replies -> [].
|
|
31
|
+
annotations = normalizeList(opts.initial);
|
|
32
|
+
onChange = typeof opts.onChange === 'function' ? opts.onChange : null;
|
|
33
|
+
permissions = {
|
|
34
|
+
add: !(opts.permissions && opts.permissions.add === false),
|
|
35
|
+
edit: !(opts.permissions && opts.permissions.edit === false),
|
|
36
|
+
delete: !(opts.permissions && opts.permissions.delete === false),
|
|
37
|
+
reply: !(opts.permissions && opts.permissions.reply === false),
|
|
38
|
+
};
|
|
39
|
+
resetCounter();
|
|
40
|
+
scheduleBadgeRefresh();
|
|
41
|
+
renderSummaries();
|
|
42
|
+
refreshRailChrome();
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Remove every floating element (pin, badges, popover, selection button)
|
|
46
|
+
// and stop reacting — called when the board reaches its submitted screen so
|
|
47
|
+
// nothing leaks over it (they live on <body> with elevated z-index).
|
|
48
|
+
let torndown = false;
|
|
49
|
+
function teardown() {
|
|
50
|
+
torndown = true;
|
|
51
|
+
try {
|
|
52
|
+
closePopover();
|
|
53
|
+
} catch {
|
|
54
|
+
// popover may not be open
|
|
55
|
+
}
|
|
56
|
+
hidePin();
|
|
57
|
+
hideSelBtn();
|
|
58
|
+
closeRail();
|
|
59
|
+
for (const b of badges) b.remove();
|
|
60
|
+
badges = [];
|
|
61
|
+
registered = [];
|
|
62
|
+
textRoots.clear();
|
|
63
|
+
highlightMap.clear();
|
|
64
|
+
commentDrafts.clear();
|
|
65
|
+
if (dom) {
|
|
66
|
+
dom.pin.remove();
|
|
67
|
+
dom.selBtn.remove();
|
|
68
|
+
dom.pop.remove();
|
|
69
|
+
dom.railToggle.remove();
|
|
70
|
+
dom.railScrim.remove();
|
|
71
|
+
dom.rail.remove();
|
|
72
|
+
summaryEls.delete(dom.railList);
|
|
73
|
+
dom = null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function register(targetEl, info) {
|
|
78
|
+
if (torndown) return;
|
|
79
|
+
ensureDom();
|
|
80
|
+
targetEl.classList.add('ann-target');
|
|
81
|
+
const entry = { el: targetEl, info };
|
|
82
|
+
registered.push(entry);
|
|
83
|
+
targetEl.addEventListener('mouseenter', () => showPin(entry));
|
|
84
|
+
targetEl.addEventListener('mouseleave', scheduleHidePin);
|
|
85
|
+
scheduleBadgeRefresh();
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function enableTextSelection(rootEl, baseInfo) {
|
|
89
|
+
if (torndown) return;
|
|
90
|
+
ensureDom();
|
|
91
|
+
const blockId = (baseInfo && baseInfo.blockId != null) ? baseInfo.blockId : null;
|
|
92
|
+
textRoots.set(blockId, rootEl);
|
|
93
|
+
rootEl.addEventListener('mouseup', () => {
|
|
94
|
+
if (torndown) return;
|
|
95
|
+
// defer: the selection settles after mouseup
|
|
96
|
+
setTimeout(() => maybeShowSelBtn(rootEl, baseInfo), 0);
|
|
97
|
+
});
|
|
98
|
+
// Re-anchor any saved text comments for this root (reload / prefill path).
|
|
99
|
+
applyHighlightsForRoot(rootEl, blockId);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function openExternal(info, anchorEl) {
|
|
103
|
+
if (torndown) return;
|
|
104
|
+
openPopover(info, anchorEl);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function list() {
|
|
108
|
+
return annotations.slice();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function setList(next) {
|
|
112
|
+
annotations = normalizeList(next);
|
|
113
|
+
resetCounter();
|
|
114
|
+
scheduleBadgeRefresh();
|
|
115
|
+
renderSummaries();
|
|
116
|
+
refreshRailChrome();
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// True while the user is mid-comment: the popover is open, which hosts both
|
|
120
|
+
// the "Add a comment" box and every thread's reply input. That text lives
|
|
121
|
+
// only in the DOM until Save, so the board must not reload it away.
|
|
122
|
+
function isComposing() {
|
|
123
|
+
return popOpen;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Commit a mid-typed comment (the open popover's "Add a comment" box) as a
|
|
127
|
+
// saved annotation, then close it — so freezing the board doesn't throw away
|
|
128
|
+
// text the user was in the middle of writing. ponytail: only the main comment
|
|
129
|
+
// box is flushed, not an in-progress thread reply (the rare case).
|
|
130
|
+
function flushOpen() {
|
|
131
|
+
if (popOpen && popSave) { try { popSave(); } catch { closePopover(); } }
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Freeze/unfreeze comment creation. While disabled, no pin, selection button,
|
|
135
|
+
// or popover appears, so the user can't start feedback that won't be saved.
|
|
136
|
+
function setDisabled(v) {
|
|
137
|
+
disabled = Boolean(v);
|
|
138
|
+
// Also close the rail flushOpen() may have just opened — otherwise it covers
|
|
139
|
+
// the host's top-right "connection lost" notice.
|
|
140
|
+
if (disabled) { closePopover(); hideSelBtn(); hidePin(); closeRail(); }
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function renderSummary(target) {
|
|
144
|
+
ensureDom();
|
|
145
|
+
summaryEls.add(target);
|
|
146
|
+
renderSummaryInto(target);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
window.RelayAnnotate = { init, register, enableTextSelection, openExternal, list, setList, renderSummary, onBadgeRefresh, teardown, isComposing, flushOpen, setDisabled };
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
// ---------- badges ----------
|
|
2
|
+
function matching(info) {
|
|
3
|
+
return annotations.filter((a) => sameBlock(a.blockId, info.blockId) && sameTarget(a.target, info.target));
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
function scheduleBadgeRefresh() {
|
|
7
|
+
if (badgeTimer) return;
|
|
8
|
+
badgeTimer = setTimeout(() => {
|
|
9
|
+
badgeTimer = 0;
|
|
10
|
+
refreshBadges();
|
|
11
|
+
}, 0);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// Hosts (e.g. blocks.js chart badge / comment button) subscribe here to be
|
|
15
|
+
// notified on every badge refresh — i.e. whenever annotations change — so
|
|
16
|
+
// their own indicators stay in sync. Returns an unsubscribe fn.
|
|
17
|
+
const badgeHooks = new Set();
|
|
18
|
+
function onBadgeRefresh(fn) {
|
|
19
|
+
if (typeof fn === 'function') badgeHooks.add(fn);
|
|
20
|
+
return () => badgeHooks.delete(fn);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function refreshBadges() {
|
|
24
|
+
for (const b of badges) b.remove();
|
|
25
|
+
badges = [];
|
|
26
|
+
if (torndown) { for (const fn of badgeHooks) { try { fn(); } catch (_) {} } return; }
|
|
27
|
+
registered = registered.filter((r) => r.el.isConnected);
|
|
28
|
+
// While a block is expanded full-screen, body-level overlay badges for
|
|
29
|
+
// OTHER blocks would float above the overlay at stale positions — only
|
|
30
|
+
// badge elements inside the expanded block.
|
|
31
|
+
const fullEl = document.body.classList.contains('blk-full-open')
|
|
32
|
+
? document.querySelector('.blk-full')
|
|
33
|
+
: null;
|
|
34
|
+
for (const entry of registered) {
|
|
35
|
+
if (fullEl && !fullEl.contains(entry.el)) continue;
|
|
36
|
+
const count = matching(entry.info).length;
|
|
37
|
+
if (!count) continue;
|
|
38
|
+
const badge = el('span', { class: 'ann-badge', title: count + (count === 1 ? ' comment' : ' comments') }, String(count));
|
|
39
|
+
badge.addEventListener('mousedown', (e) => e.stopPropagation());
|
|
40
|
+
badge.addEventListener('click', (e) => {
|
|
41
|
+
e.stopPropagation();
|
|
42
|
+
e.preventDefault();
|
|
43
|
+
hidePin();
|
|
44
|
+
openPopover(entry.info, entry.el);
|
|
45
|
+
});
|
|
46
|
+
if (entry.el instanceof HTMLElement && !(entry.el instanceof HTMLImageElement)) {
|
|
47
|
+
// a normal HTML element hosts the badge as a child — it tracks naturally
|
|
48
|
+
if (getComputedStyle(entry.el).position === 'static') entry.el.classList.add('ann-rel');
|
|
49
|
+
entry.el.append(badge);
|
|
50
|
+
} else {
|
|
51
|
+
// SVG node or <img> — neither can host an HTML child. Anchor the badge
|
|
52
|
+
// INSIDE the scrollable diagram viewport in CONTENT coordinates: as an
|
|
53
|
+
// absolutely-positioned child of the scroll box it rides with the content
|
|
54
|
+
// on drag-pan / container-scroll / page-scroll, with no listener needed.
|
|
55
|
+
// (A document-body overlay stayed put when the diagram itself scrolled.)
|
|
56
|
+
badge.classList.add('ann-badge-overlay');
|
|
57
|
+
const host = entry.el.closest ? entry.el.closest('.blk-viewer') : null;
|
|
58
|
+
const r = entry.el.getBoundingClientRect();
|
|
59
|
+
if (host) {
|
|
60
|
+
const cr = host.getBoundingClientRect();
|
|
61
|
+
const cs = getComputedStyle(host);
|
|
62
|
+
const bL = parseFloat(cs.borderLeftWidth) || 0;
|
|
63
|
+
const bT = parseFloat(cs.borderTopWidth) || 0;
|
|
64
|
+
badge.style.left = (r.right - cr.left - bL + host.scrollLeft - 10) + 'px';
|
|
65
|
+
badge.style.top = (r.top - cr.top - bT + host.scrollTop - 6) + 'px';
|
|
66
|
+
host.append(badge);
|
|
67
|
+
} else {
|
|
68
|
+
badge.style.left = (r.right + window.scrollX - 10) + 'px';
|
|
69
|
+
badge.style.top = (r.top + window.scrollY - 6) + 'px';
|
|
70
|
+
document.body.append(badge);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
badges.push(badge);
|
|
74
|
+
}
|
|
75
|
+
for (const fn of badgeHooks) { try { fn(); } catch (_) {} }
|
|
76
|
+
}
|
|
77
|
+
|