@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.
- package/bin/rly.js +1 -1
- package/package.json +39 -4
- package/schema/spec.v1.json +1368 -0
- package/src/a2ui/compile.js +2 -0
- 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 +104 -0
- package/src/link.js +1 -0
- 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 +15 -21
- package/src/{server.js → server/board.js} +17 -781
- 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/a2ui/compile.d.ts +40 -0
- package/types/index.d.ts +181 -0
- package/types/mcp.d.ts +31 -0
- 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/schema.d.ts +1171 -0
- package/types/spec/sources.d.ts +14 -0
- package/types/store.d.ts +29 -0
- package/types/ui/manifest.d.ts +62 -0
- package/types/util.d.ts +6 -0
- package/src/cli.js +0 -1766
- package/src/mcp-ui/board.js +0 -1162
- package/src/spec.js +0 -1222
- package/src/ui/annotate.js +0 -1055
- package/src/ui/app.js +0 -1643
- package/src/ui/blocks.js +0 -3260
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// ---------- singleton DOM ----------
|
|
2
|
+
function ensureDom() {
|
|
3
|
+
if (dom) return;
|
|
4
|
+
const pin = el('button', { class: 'ann-pin', type: 'button', title: 'Add a comment', 'aria-label': 'Add a comment' });
|
|
5
|
+
pin.innerHTML =
|
|
6
|
+
'<svg viewBox="0 0 16 16" width="11" height="11" aria-hidden="true">' +
|
|
7
|
+
'<path d="M3 2.5h10A1.5 1.5 0 0 1 14.5 4v5a1.5 1.5 0 0 1-1.5 1.5H8.4L5 13.4v-2.9H3A1.5 1.5 0 0 1 1.5 9V4A1.5 1.5 0 0 1 3 2.5Z" fill="currentColor"/></svg>';
|
|
8
|
+
pin.addEventListener('mouseenter', () => clearTimeout(pinTimer));
|
|
9
|
+
pin.addEventListener('mouseleave', scheduleHidePin);
|
|
10
|
+
pin.addEventListener('click', (e) => {
|
|
11
|
+
e.stopPropagation();
|
|
12
|
+
const entry = pinEntry;
|
|
13
|
+
hidePin();
|
|
14
|
+
if (entry) openPopover(entry.info, entry.el);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
const selBtn = el('button', { class: 'ann-selbtn', type: 'button' }, 'Comment');
|
|
18
|
+
// preventDefault keeps the text selection alive through the click.
|
|
19
|
+
selBtn.addEventListener('mousedown', (e) => e.preventDefault());
|
|
20
|
+
selBtn.addEventListener('click', (e) => {
|
|
21
|
+
e.stopPropagation();
|
|
22
|
+
const p = pendingSelection;
|
|
23
|
+
if (p) openPopover(p.info, selBtn); // anchor rect read while still visible
|
|
24
|
+
hideSelBtn();
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const pop = el('div', { class: 'ann-pop', role: 'dialog', 'aria-label': 'Comment' });
|
|
28
|
+
pop.addEventListener('keydown', (e) => {
|
|
29
|
+
if (e.key === 'Escape') {
|
|
30
|
+
e.stopPropagation();
|
|
31
|
+
closePopover();
|
|
32
|
+
} else if (e.key === 'Enter' && (e.metaKey || e.ctrlKey)) {
|
|
33
|
+
e.preventDefault();
|
|
34
|
+
if (popSave) popSave();
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Comments rail (Outline-style right sidebar) + its floating toggle and
|
|
39
|
+
// narrow-screen scrim. The list is a summary target, so renderSummaries()
|
|
40
|
+
// keeps it in sync; chrome (count, visibility) is refreshed separately.
|
|
41
|
+
const railToggleCount = el('span', { class: 'ann-rail-toggle-count' }, '0');
|
|
42
|
+
const railToggle = el('button', { class: 'ann-rail-toggle', type: 'button', 'aria-label': 'Comments' });
|
|
43
|
+
railToggle.innerHTML =
|
|
44
|
+
'<svg viewBox="0 0 16 16" width="13" height="13" aria-hidden="true">' +
|
|
45
|
+
'<path d="M3 2.5h10A1.5 1.5 0 0 1 14.5 4v5a1.5 1.5 0 0 1-1.5 1.5H8.4L5 13.4v-2.9H3A1.5 1.5 0 0 1 1.5 9V4A1.5 1.5 0 0 1 3 2.5Z" fill="currentColor"/></svg>';
|
|
46
|
+
railToggle.append(el('span', { class: 'ann-rail-toggle-label' }, 'Comments'), railToggleCount);
|
|
47
|
+
railToggle.addEventListener('click', toggleRail);
|
|
48
|
+
|
|
49
|
+
const railScrim = el('div', { class: 'ann-rail-scrim' });
|
|
50
|
+
railScrim.addEventListener('click', closeRail);
|
|
51
|
+
|
|
52
|
+
const railClose = el('button', { class: 'ann-rail-close', type: 'button', 'aria-label': 'Close comments', title: 'Close' }, '×');
|
|
53
|
+
railClose.addEventListener('click', closeRail);
|
|
54
|
+
const railHeadCount = el('span', { class: 'ann-rail-headcount' }, '');
|
|
55
|
+
const railList = el('div', { class: 'ann-rail-list' });
|
|
56
|
+
const rail = el('aside', { class: 'ann-rail', 'aria-label': 'Comments' },
|
|
57
|
+
el('div', { class: 'ann-rail-head' },
|
|
58
|
+
el('span', { class: 'ann-rail-title' }, 'Comments'),
|
|
59
|
+
railHeadCount,
|
|
60
|
+
railClose
|
|
61
|
+
),
|
|
62
|
+
railList
|
|
63
|
+
);
|
|
64
|
+
|
|
65
|
+
document.body.append(pin, selBtn, pop, railToggle, railScrim, rail);
|
|
66
|
+
dom = { pin, selBtn, pop, railToggle, railToggleCount, railScrim, rail, railList, railHeadCount };
|
|
67
|
+
summaryEls.add(railList);
|
|
68
|
+
|
|
69
|
+
document.addEventListener('selectionchange', () => {
|
|
70
|
+
if (dom.selBtn.style.display === '' || dom.selBtn.style.display === 'none') return;
|
|
71
|
+
const sel = window.getSelection();
|
|
72
|
+
const txt = sel && !sel.isCollapsed ? sel.toString() : '';
|
|
73
|
+
if (!pendingSelection || txt.slice(0, 500) !== pendingSelection.info.target.quote) hideSelBtn();
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
document.addEventListener('mousedown', (e) => {
|
|
77
|
+
if (popOpen && !dom.pop.contains(e.target)) closePopover();
|
|
78
|
+
if (!dom.selBtn.contains(e.target)) hideSelBtn();
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// Capture-phase: also fires for nested scroll containers (mermaid pane…).
|
|
82
|
+
// Keep an open composer re-anchored instead of closing it: scrolling is a
|
|
83
|
+
// normal review action, and DOM-only draft text must never disappear merely
|
|
84
|
+
// because the user looked elsewhere on the board.
|
|
85
|
+
window.addEventListener('scroll', (e) => {
|
|
86
|
+
hidePin();
|
|
87
|
+
hideSelBtn();
|
|
88
|
+
if (!popOpen) return;
|
|
89
|
+
if (dom.pop.contains(e.target)) return; // textarea scrolling inside
|
|
90
|
+
schedulePositionPopover();
|
|
91
|
+
}, true);
|
|
92
|
+
|
|
93
|
+
window.addEventListener('resize', () => {
|
|
94
|
+
scheduleBadgeRefresh();
|
|
95
|
+
schedulePositionPopover();
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
// ---------- comments rail ----------
|
|
100
|
+
function openRail() {
|
|
101
|
+
if (!dom || torndown || !annotations.length) return;
|
|
102
|
+
railOpen = true;
|
|
103
|
+
dom.rail.classList.add('open');
|
|
104
|
+
dom.railScrim.classList.add('show');
|
|
105
|
+
document.body.classList.add('ann-rail-open');
|
|
106
|
+
}
|
|
107
|
+
function closeRail() {
|
|
108
|
+
railOpen = false;
|
|
109
|
+
if (!dom) return;
|
|
110
|
+
dom.rail.classList.remove('open');
|
|
111
|
+
dom.railScrim.classList.remove('show');
|
|
112
|
+
document.body.classList.remove('ann-rail-open');
|
|
113
|
+
}
|
|
114
|
+
function toggleRail() {
|
|
115
|
+
if (railOpen) closeRail();
|
|
116
|
+
else openRail();
|
|
117
|
+
}
|
|
118
|
+
// Briefly flash the floating Comments button (used when a comment is added so
|
|
119
|
+
// the user sees it landed, without auto-opening the rail / shifting layout).
|
|
120
|
+
function pulseToggle() {
|
|
121
|
+
if (!dom || !dom.railToggle) return;
|
|
122
|
+
dom.railToggle.classList.remove('pulse');
|
|
123
|
+
void dom.railToggle.offsetWidth; // reflow so the animation restarts
|
|
124
|
+
dom.railToggle.classList.add('pulse');
|
|
125
|
+
}
|
|
126
|
+
// Sync the toggle badge + rail count with the live list; hide the whole rail
|
|
127
|
+
// affordance when there are no comments.
|
|
128
|
+
function refreshRailChrome() {
|
|
129
|
+
if (!dom) return;
|
|
130
|
+
const n = annotations.length;
|
|
131
|
+
dom.railToggleCount.textContent = String(n);
|
|
132
|
+
dom.railHeadCount.textContent = n ? `${n}` : '';
|
|
133
|
+
dom.railToggle.style.display = n ? 'inline-flex' : 'none';
|
|
134
|
+
if (!n) closeRail();
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// ---------- hover pin ----------
|
|
138
|
+
function showPin(entry) {
|
|
139
|
+
if (disabled || !permissions.add) return;
|
|
140
|
+
clearTimeout(pinTimer);
|
|
141
|
+
pinEntry = entry;
|
|
142
|
+
const rect = entry.el.getBoundingClientRect();
|
|
143
|
+
const w = 22;
|
|
144
|
+
const pin = dom.pin;
|
|
145
|
+
pin.style.display = 'flex';
|
|
146
|
+
pin.style.left = Math.max(4, Math.min(rect.right - w / 2, window.innerWidth - w - 4)) + 'px';
|
|
147
|
+
pin.style.top = Math.max(4, Math.min(rect.top - w / 2, window.innerHeight - w - 4)) + 'px';
|
|
148
|
+
}
|
|
149
|
+
function scheduleHidePin() {
|
|
150
|
+
clearTimeout(pinTimer);
|
|
151
|
+
pinTimer = setTimeout(hidePin, 250); // delay so the pin itself is clickable
|
|
152
|
+
}
|
|
153
|
+
function hidePin() {
|
|
154
|
+
clearTimeout(pinTimer);
|
|
155
|
+
pinEntry = null;
|
|
156
|
+
if (dom) dom.pin.style.display = 'none';
|
|
157
|
+
}
|
|
158
|
+
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
// ---------- helpers ----------
|
|
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 truncate(s, n) {
|
|
17
|
+
s = String(s);
|
|
18
|
+
return s.length > n ? s.slice(0, n - 1) + '…' : s;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Humanized target label shown in the popover and the summary list.
|
|
22
|
+
function humanize(t) {
|
|
23
|
+
if (!t || typeof t !== 'object') return 'Element';
|
|
24
|
+
switch (t.kind) {
|
|
25
|
+
case 'chart-element': {
|
|
26
|
+
const parts = [];
|
|
27
|
+
if (t.label !== undefined && t.label !== null && t.label !== '') parts.push(truncate(t.label, 40));
|
|
28
|
+
if (typeof t.datasetIndex === 'number') parts.push(`series ${t.datasetIndex + 1}`);
|
|
29
|
+
if (t.value !== undefined && t.value !== null) {
|
|
30
|
+
parts.push(truncate(typeof t.value === 'object' ? JSON.stringify(t.value) : t.value, 30));
|
|
31
|
+
}
|
|
32
|
+
return 'Chart · ' + (parts.join(' · ') || 'element');
|
|
33
|
+
}
|
|
34
|
+
case 'mermaid-node':
|
|
35
|
+
return 'Diagram · ' + truncate(t.text || t.nodeId || 'node', 50);
|
|
36
|
+
case 'graphviz-node':
|
|
37
|
+
return 'Graph · ' + truncate(t.text || t.nodeId || 'node', 50);
|
|
38
|
+
case 'plantuml-node':
|
|
39
|
+
return 'Diagram · ' + truncate(t.text || 'element', 50);
|
|
40
|
+
case 'image':
|
|
41
|
+
return truncate(t.label || 'Image', 50);
|
|
42
|
+
case 'image-point':
|
|
43
|
+
return (t.label ? truncate(t.label, 30) + ' · ' : 'Image · ') + 'pin ' + Math.round((t.x || 0) * 100) + '%, ' + Math.round((t.y || 0) * 100) + '%';
|
|
44
|
+
case 'image-region': {
|
|
45
|
+
const source = t.side ? t.side.charAt(0).toUpperCase() + t.side.slice(1) + ' · ' : '';
|
|
46
|
+
const area = `${Math.round((t.w || 0) * 100)}×${Math.round((t.h || 0) * 100)}% area`;
|
|
47
|
+
return (t.label ? truncate(t.label, 30) + ' · ' : 'Image · ') + source + area;
|
|
48
|
+
}
|
|
49
|
+
case 'table-cell': {
|
|
50
|
+
let s = `Table · row ${(Number(t.row) || 0) + 1} · ${t.col}`;
|
|
51
|
+
if (t.value !== undefined && t.value !== null && t.value !== '') s += ` — “${truncate(t.value, 30)}”`;
|
|
52
|
+
return s;
|
|
53
|
+
}
|
|
54
|
+
case 'code-line':
|
|
55
|
+
return 'Line ' + t.line + (t.file ? ' · ' + truncate(t.file, 40) : '');
|
|
56
|
+
case 'text':
|
|
57
|
+
return `“${truncate(t.quote || '', 60)}”`;
|
|
58
|
+
case 'html-element':
|
|
59
|
+
return truncate(t.label || 'Element', 50) + (t.detail ? ` — ${truncate(t.detail, 40)}` : '');
|
|
60
|
+
case 'block':
|
|
61
|
+
return 'Whole ' + truncate(t.label || 'block', 40);
|
|
62
|
+
default:
|
|
63
|
+
return t.kind || 'Element';
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// Target equality for badge re-binding = stringify equality within a block.
|
|
68
|
+
const sameTarget = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
69
|
+
const sameBlock = (a, b) => (a ?? null) === (b ?? null);
|
|
70
|
+
|
|
71
|
+
// Author chip: "you" (muted) for user comments, "agent" (accent) for agent.
|
|
72
|
+
function chip(author) {
|
|
73
|
+
const agent = author === 'agent';
|
|
74
|
+
return el('span', { class: 'ann-chip' + (agent ? ' ann-chip-agent' : '') }, agent ? 'agent' : 'you');
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Short HH:MM timestamp (empty string when the date is unparsable).
|
|
78
|
+
function fmtTime(iso) {
|
|
79
|
+
const d = new Date(iso);
|
|
80
|
+
if (isNaN(d.getTime())) return '';
|
|
81
|
+
const p = (n) => String(n).padStart(2, '0');
|
|
82
|
+
return p(d.getHours()) + ':' + p(d.getMinutes());
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function timeEl(iso) {
|
|
86
|
+
const t = fmtTime(iso);
|
|
87
|
+
return t ? el('span', { class: 'ann-time' }, t) : null;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// ---------- state ----------
|
|
91
|
+
let annotations = []; // contract annotation objects
|
|
92
|
+
let idCounter = 0; // continues from max existing "aN"
|
|
93
|
+
let onChange = null;
|
|
94
|
+
let registered = []; // [{el, info: {blockId, questionId, target}}]
|
|
95
|
+
const summaryEls = new Set();
|
|
96
|
+
let badges = []; // live badge nodes (rebuilt on every refresh)
|
|
97
|
+
let pendingSelection = null; // {info} captured at mouseup time
|
|
98
|
+
|
|
99
|
+
// Text-selection comments are anchored back into the document as inline
|
|
100
|
+
// <mark> highlights. textRoots maps a blockId (null = intro/board) to the
|
|
101
|
+
// element whose text can be highlighted; highlightMap maps a target
|
|
102
|
+
// signature to its live <mark> nodes so we can flash or unwrap them.
|
|
103
|
+
const textRoots = new Map();
|
|
104
|
+
const highlightMap = new Map();
|
|
105
|
+
|
|
106
|
+
let dom = null; // {pin, selBtn, pop, rail, railList, railToggle, railScrim, ...}
|
|
107
|
+
let pinTimer = null;
|
|
108
|
+
let pinEntry = null;
|
|
109
|
+
let popOpen = false;
|
|
110
|
+
let popSave = null;
|
|
111
|
+
let popCloseHook = null;
|
|
112
|
+
let popAnchorEl = null;
|
|
113
|
+
let popPositionFrame = 0;
|
|
114
|
+
const commentDrafts = new Map();
|
|
115
|
+
// Frozen by the host (e.g. relay's connection-lost block): suppress every way
|
|
116
|
+
// to START a comment, so the user can't type feedback that won't be saved.
|
|
117
|
+
let disabled = false;
|
|
118
|
+
let permissions = { add: true, edit: true, delete: true, reply: true };
|
|
119
|
+
let badgeTimer = 0;
|
|
120
|
+
let railOpen = false;
|
|
121
|
+
|
|
122
|
+
// Stable signature for a (blockId, target) pair — same key matching() groups by.
|
|
123
|
+
const sigOf = (blockId, target) => JSON.stringify({ b: blockId ?? null, t: target });
|
|
124
|
+
|
|
125
|
+
// Brief highlight pulse used by the rail's jump-to.
|
|
126
|
+
function flashEl(target) {
|
|
127
|
+
if (!target) return;
|
|
128
|
+
target.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
|
129
|
+
target.classList.remove('ann-flash');
|
|
130
|
+
void target.getBoundingClientRect(); // restart the animation
|
|
131
|
+
target.classList.add('ann-flash');
|
|
132
|
+
setTimeout(() => target.classList.remove('ann-flash'), 1000);
|
|
133
|
+
}
|
|
134
|
+
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
// ---------- text-selection highlights ----------
|
|
2
|
+
// Re-locate a saved text quote inside a root using its prefix/suffix anchors,
|
|
3
|
+
// returning a live Range (or null when the text can't be found anymore).
|
|
4
|
+
function findRangeByQuote(root, target) {
|
|
5
|
+
const quote = target && target.quote ? String(target.quote) : '';
|
|
6
|
+
if (!quote) return null;
|
|
7
|
+
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, null);
|
|
8
|
+
const map = []; // [{ node, start }] start = offset in `full`
|
|
9
|
+
let full = '';
|
|
10
|
+
let node;
|
|
11
|
+
while ((node = walker.nextNode())) {
|
|
12
|
+
map.push({ node, start: full.length });
|
|
13
|
+
full += node.nodeValue;
|
|
14
|
+
}
|
|
15
|
+
if (!map.length) return null;
|
|
16
|
+
const prefix = target.prefix || '';
|
|
17
|
+
const suffix = target.suffix || '';
|
|
18
|
+
const hits = [];
|
|
19
|
+
let from = 0;
|
|
20
|
+
let idx;
|
|
21
|
+
while ((idx = full.indexOf(quote, from)) !== -1) {
|
|
22
|
+
hits.push(idx);
|
|
23
|
+
from = idx + 1;
|
|
24
|
+
}
|
|
25
|
+
if (!hits.length) return null;
|
|
26
|
+
let chosen = hits[0];
|
|
27
|
+
for (const h of hits) {
|
|
28
|
+
const before = full.slice(Math.max(0, h - prefix.length), h);
|
|
29
|
+
const after = full.slice(h + quote.length, h + quote.length + suffix.length);
|
|
30
|
+
if ((!prefix || before.endsWith(prefix)) && (!suffix || after.startsWith(suffix))) {
|
|
31
|
+
chosen = h;
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const locate = (off) => {
|
|
36
|
+
for (const m of map) {
|
|
37
|
+
if (off <= m.start + m.node.nodeValue.length) return { node: m.node, offset: off - m.start };
|
|
38
|
+
}
|
|
39
|
+
const last = map[map.length - 1];
|
|
40
|
+
return { node: last.node, offset: last.node.nodeValue.length };
|
|
41
|
+
};
|
|
42
|
+
const s = locate(chosen);
|
|
43
|
+
const e = locate(chosen + quote.length);
|
|
44
|
+
const range = document.createRange();
|
|
45
|
+
try {
|
|
46
|
+
range.setStart(s.node, s.offset);
|
|
47
|
+
range.setEnd(e.node, e.offset);
|
|
48
|
+
} catch {
|
|
49
|
+
return null;
|
|
50
|
+
}
|
|
51
|
+
return range;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Wrap every text-node segment inside `range` in its own <mark>; clicking a
|
|
55
|
+
// mark opens the comment popover for that target. Returns the mark nodes.
|
|
56
|
+
function wrapRange(range, sig, info) {
|
|
57
|
+
const sC = range.startContainer;
|
|
58
|
+
const sO = range.startOffset;
|
|
59
|
+
const eC = range.endContainer;
|
|
60
|
+
const eO = range.endOffset;
|
|
61
|
+
const anc = range.commonAncestorContainer;
|
|
62
|
+
const root = anc.nodeType === 1 ? anc : anc.parentNode;
|
|
63
|
+
if (!root) return [];
|
|
64
|
+
const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT, null);
|
|
65
|
+
const all = [];
|
|
66
|
+
let n;
|
|
67
|
+
while ((n = walker.nextNode())) all.push(n);
|
|
68
|
+
// Decide membership BEFORE mutating the DOM (splitText adds new siblings).
|
|
69
|
+
const inRange = all.filter((node) => {
|
|
70
|
+
try {
|
|
71
|
+
return range.intersectsNode(node);
|
|
72
|
+
} catch {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
});
|
|
76
|
+
const marks = [];
|
|
77
|
+
for (const orig of inRange) {
|
|
78
|
+
let fromOff = 0;
|
|
79
|
+
let toOff = orig.nodeValue.length;
|
|
80
|
+
if (orig === sC) fromOff = sO;
|
|
81
|
+
if (orig === eC) toOff = eO;
|
|
82
|
+
if (fromOff >= toOff) continue;
|
|
83
|
+
let seg = orig;
|
|
84
|
+
if (fromOff > 0) seg = seg.splitText(fromOff);
|
|
85
|
+
if (toOff - fromOff < seg.nodeValue.length) seg.splitText(toOff - fromOff);
|
|
86
|
+
const mark = el('mark', { class: 'ann-hl', 'data-sig': sig });
|
|
87
|
+
seg.parentNode.insertBefore(mark, seg);
|
|
88
|
+
mark.appendChild(seg);
|
|
89
|
+
mark.addEventListener('click', (ev) => {
|
|
90
|
+
ev.stopPropagation();
|
|
91
|
+
openPopover(info, mark);
|
|
92
|
+
});
|
|
93
|
+
marks.push(mark);
|
|
94
|
+
}
|
|
95
|
+
return marks;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function unwrapHighlight(sig) {
|
|
99
|
+
const marks = highlightMap.get(sig);
|
|
100
|
+
if (!marks) return;
|
|
101
|
+
for (const m of marks) {
|
|
102
|
+
if (!m.isConnected) continue;
|
|
103
|
+
const parent = m.parentNode;
|
|
104
|
+
while (m.firstChild) parent.insertBefore(m.firstChild, m);
|
|
105
|
+
parent.removeChild(m);
|
|
106
|
+
parent.normalize();
|
|
107
|
+
}
|
|
108
|
+
highlightMap.delete(sig);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// Highlight every saved text annotation whose blockId matches this root and
|
|
112
|
+
// that isn't already wrapped (idempotent — safe to call on every register).
|
|
113
|
+
function applyHighlightsForRoot(root, blockId) {
|
|
114
|
+
const key = blockId ?? null;
|
|
115
|
+
const seen = new Set();
|
|
116
|
+
for (const a of annotations) {
|
|
117
|
+
if (!a.target || a.target.kind !== 'text') continue;
|
|
118
|
+
if ((a.blockId ?? null) !== key) continue;
|
|
119
|
+
const sig = sigOf(a.blockId, a.target);
|
|
120
|
+
if (seen.has(sig) || highlightMap.has(sig)) continue;
|
|
121
|
+
seen.add(sig);
|
|
122
|
+
const range = findRangeByQuote(root, a.target);
|
|
123
|
+
if (!range) continue;
|
|
124
|
+
const info = { blockId: a.blockId ?? null, questionId: a.questionId ?? null, target: a.target };
|
|
125
|
+
const marks = wrapRange(range, sig, info);
|
|
126
|
+
if (marks.length) highlightMap.set(sig, marks);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// ---------- mutations ----------
|
|
2
|
+
function addAnnotation(info, text) {
|
|
3
|
+
if (!permissions.add) return;
|
|
4
|
+
annotations.push({
|
|
5
|
+
id: 'a' + (++idCounter),
|
|
6
|
+
questionId: info.questionId !== undefined ? info.questionId : null,
|
|
7
|
+
blockId: info.blockId !== undefined ? info.blockId : null,
|
|
8
|
+
target: info.target,
|
|
9
|
+
text,
|
|
10
|
+
createdAt: new Date().toISOString(),
|
|
11
|
+
author: 'user',
|
|
12
|
+
replies: [],
|
|
13
|
+
});
|
|
14
|
+
// Highlight the selected text in place (re-located via its quote anchors,
|
|
15
|
+
// since the live selection is already gone by save time).
|
|
16
|
+
if (info.target && info.target.kind === 'text') {
|
|
17
|
+
const sig = sigOf(info.blockId, info.target);
|
|
18
|
+
const root = textRoots.get(info.blockId ?? null);
|
|
19
|
+
if (root && !highlightMap.has(sig)) {
|
|
20
|
+
const range = findRangeByQuote(root, info.target);
|
|
21
|
+
if (range) {
|
|
22
|
+
const marks = wrapRange(range, sig, { blockId: info.blockId ?? null, questionId: info.questionId ?? null, target: info.target });
|
|
23
|
+
if (marks.length) highlightMap.set(sig, marks);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
changed();
|
|
28
|
+
// Don't auto-open the rail — that shifts the board layout. Just flash the
|
|
29
|
+
// floating Comments button so the user notices, and opens it when they want.
|
|
30
|
+
pulseToggle();
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Append a user reply to a top-level annotation's thread (cap 50, like the
|
|
34
|
+
// server). Empty text is ignored by callers.
|
|
35
|
+
function addReply(id, text) {
|
|
36
|
+
if (!permissions.reply) return;
|
|
37
|
+
const a = annotations.find((x) => x.id === id);
|
|
38
|
+
if (!a) return;
|
|
39
|
+
if (!Array.isArray(a.replies)) a.replies = [];
|
|
40
|
+
if (a.replies.length >= 50) return;
|
|
41
|
+
a.replies.push({
|
|
42
|
+
author: 'user',
|
|
43
|
+
text: String(text).slice(0, 5000),
|
|
44
|
+
createdAt: new Date().toISOString(),
|
|
45
|
+
});
|
|
46
|
+
changed();
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// Edit a top-level comment's text in place (author edits their own comment).
|
|
50
|
+
function editAnnotation(id, text) {
|
|
51
|
+
if (!permissions.edit) return;
|
|
52
|
+
const a = annotations.find((x) => x.id === id);
|
|
53
|
+
if (!a) return;
|
|
54
|
+
const t = String(text).slice(0, 5000).trim();
|
|
55
|
+
if (!t) return;
|
|
56
|
+
a.text = t;
|
|
57
|
+
changed();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function removeAnnotation(id) {
|
|
61
|
+
if (!permissions.delete) return;
|
|
62
|
+
const i = annotations.findIndex((a) => a.id === id);
|
|
63
|
+
if (i < 0) return;
|
|
64
|
+
const removed = annotations[i];
|
|
65
|
+
annotations.splice(i, 1);
|
|
66
|
+
// Drop the inline highlight once the last comment on that text is gone.
|
|
67
|
+
if (removed.target && removed.target.kind === 'text') {
|
|
68
|
+
const sig = sigOf(removed.blockId, removed.target);
|
|
69
|
+
if (!annotations.some((a) => sigOf(a.blockId, a.target) === sig)) unwrapHighlight(sig);
|
|
70
|
+
}
|
|
71
|
+
changed();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function changed() {
|
|
75
|
+
scheduleBadgeRefresh();
|
|
76
|
+
renderSummaries();
|
|
77
|
+
refreshRailChrome();
|
|
78
|
+
if (onChange) onChange(annotations.slice());
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ---------- text selection ----------
|
|
82
|
+
function hideSelBtn() {
|
|
83
|
+
pendingSelection = null;
|
|
84
|
+
if (dom) dom.selBtn.style.display = 'none';
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function maybeShowSelBtn(rootEl, baseInfo) {
|
|
88
|
+
if (disabled || !permissions.add) return hideSelBtn();
|
|
89
|
+
const sel = window.getSelection();
|
|
90
|
+
if (!sel || sel.isCollapsed || !sel.rangeCount) return hideSelBtn();
|
|
91
|
+
if (!rootEl.contains(sel.anchorNode) || !rootEl.contains(sel.focusNode)) return hideSelBtn();
|
|
92
|
+
const range = sel.getRangeAt(0);
|
|
93
|
+
const quote = range.toString();
|
|
94
|
+
if (!quote.trim()) return hideSelBtn();
|
|
95
|
+
|
|
96
|
+
const pre = document.createRange();
|
|
97
|
+
pre.selectNodeContents(rootEl);
|
|
98
|
+
pre.setEnd(range.startContainer, range.startOffset);
|
|
99
|
+
const post = document.createRange();
|
|
100
|
+
post.selectNodeContents(rootEl);
|
|
101
|
+
post.setStart(range.endContainer, range.endOffset);
|
|
102
|
+
pendingSelection = {
|
|
103
|
+
info: {
|
|
104
|
+
blockId: baseInfo.blockId !== undefined ? baseInfo.blockId : null,
|
|
105
|
+
questionId: baseInfo.questionId !== undefined ? baseInfo.questionId : null,
|
|
106
|
+
target: {
|
|
107
|
+
kind: 'text',
|
|
108
|
+
quote: quote.slice(0, 500),
|
|
109
|
+
prefix: pre.toString().slice(-30),
|
|
110
|
+
suffix: post.toString().slice(0, 30),
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
// Float the button near the end of the selection.
|
|
116
|
+
const rects = range.getClientRects();
|
|
117
|
+
const last = rects.length ? rects[rects.length - 1] : range.getBoundingClientRect();
|
|
118
|
+
const btn = dom.selBtn;
|
|
119
|
+
btn.style.display = 'block';
|
|
120
|
+
const bw = btn.offsetWidth || 90;
|
|
121
|
+
const bh = btn.offsetHeight || 30;
|
|
122
|
+
let top = last.bottom + 6;
|
|
123
|
+
if (top + bh > window.innerHeight - 8) top = last.top - bh - 6;
|
|
124
|
+
btn.style.left = Math.max(8, Math.min(last.right - bw / 2, window.innerWidth - bw - 8)) + 'px';
|
|
125
|
+
btn.style.top = Math.max(8, top) + 'px';
|
|
126
|
+
}
|
|
127
|
+
|