truemap-cli 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +70 -2
- package/bin/truemap.js +10 -0
- package/kit/fonts/OFL.txt +93 -0
- package/kit/fonts/ibm-plex-mono-latin-400-normal.woff2 +0 -0
- package/kit/fonts/ibm-plex-mono-latin-500-normal.woff2 +0 -0
- package/kit/fonts/ibm-plex-sans-latin-400-normal.woff2 +0 -0
- package/kit/fonts/ibm-plex-sans-latin-500-normal.woff2 +0 -0
- package/kit/fonts/ibm-plex-sans-latin-600-normal.woff2 +0 -0
- package/kit/truemap.css +290 -0
- package/package.json +39 -4
- package/skills/truemap/SKILL.md +23 -0
- package/src/check.js +248 -0
- package/src/cli.js +392 -0
- package/src/common.js +99 -0
- package/src/guide.md +143 -0
- package/src/outline.js +71 -0
- package/src/server.js +440 -0
- package/src/ui/sdk.js +470 -0
- package/src/ui/workspace.css +209 -0
- package/src/ui/workspace.html +76 -0
- package/src/ui/workspace.js +476 -0
package/src/ui/sdk.js
ADDED
|
@@ -0,0 +1,470 @@
|
|
|
1
|
+
// Injected into every .truemap page by the local server (never saved into the page file).
|
|
2
|
+
// Runs inside the sandboxed page frame:
|
|
3
|
+
// - draws the wires of .tm-map from data-to
|
|
4
|
+
// - annotate mode: click a box or select text, write a comment, the workspace collects it
|
|
5
|
+
// - decision forms become answers; check marks show boxes whose code moved
|
|
6
|
+
(() => {
|
|
7
|
+
if (window.__truemapSdk) return;
|
|
8
|
+
window.__truemapSdk = true;
|
|
9
|
+
const parentWin = window.parent;
|
|
10
|
+
const framed = parentWin && parentWin !== window;
|
|
11
|
+
const PARENT = '__TRUEMAP_PARENT__';
|
|
12
|
+
const post = (msg) => { if (framed) parentWin.postMessage({ __truemap: true, ...msg }, PARENT); };
|
|
13
|
+
const pagePath = () => decodeURIComponent(location.pathname.replace(/^\/pages\//, ''));
|
|
14
|
+
const clean = (s, n) => String(s || '').replace(/\s+/g, ' ').trim().slice(0, n);
|
|
15
|
+
const T = {
|
|
16
|
+
sent: 'Sent', changed: 'The code changed after this was written', broken: 'The code this points to is gone',
|
|
17
|
+
placeholder: 'Comment or question', add: 'Add comment', save: 'Save', remove: 'Remove', cancel: 'Cancel',
|
|
18
|
+
hint: /Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent) ? '⌘Enter sends now' : 'Ctrl+Enter sends now',
|
|
19
|
+
suggest: [['Explain this', 'Explain how this works.'], ['What depends on it', 'What depends on this, and what does it depend on?'], ['Why this way', 'Why is it built this way? What are the trade-offs?']],
|
|
20
|
+
comment: 'Comment',
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// ---------- page styles for truemap states ----------
|
|
24
|
+
const style = document.createElement('style');
|
|
25
|
+
style.textContent = `
|
|
26
|
+
[data-tm-mark="changed"] { outline: 2px dashed var(--tm-changed, #a2690a) !important; outline-offset: 3px; }
|
|
27
|
+
[data-tm-mark="broken"] { outline: 2px dashed var(--tm-broken, #b03a2e) !important; outline-offset: 3px; }
|
|
28
|
+
svg [data-tm-mark] { outline: none !important; }
|
|
29
|
+
html[data-tm-annotate] body { cursor: default; }
|
|
30
|
+
html[data-tm-annotate] [data-tm-hover] { outline: 1.5px solid var(--tm-route, #2453c4) !important; outline-offset: 3px; cursor: pointer; }
|
|
31
|
+
html[data-tm-annotate] a, html[data-tm-annotate] label { cursor: pointer; }
|
|
32
|
+
[data-tm-noted] { outline: 1.5px solid color-mix(in oklab, var(--tm-route, #2453c4) 55%, transparent); outline-offset: 3px; }
|
|
33
|
+
[data-tm-active] { outline: 2px solid var(--tm-route, #2453c4) !important; outline-offset: 3px; }
|
|
34
|
+
@keyframes tm-flash { 0%,100% { outline-color: transparent } 35% { outline-color: var(--tm-route, #2453c4) } }
|
|
35
|
+
[data-tm-flash] { outline: 2px solid var(--tm-route, #2453c4) !important; outline-offset: 4px; animation: tm-flash 1.1s ease 2; }
|
|
36
|
+
@media (prefers-reduced-motion: reduce) { [data-tm-flash] { animation: none; } }
|
|
37
|
+
::highlight(tm-note) { background-color: color-mix(in oklab, var(--tm-route, #2453c4) 20%, transparent); }
|
|
38
|
+
`;
|
|
39
|
+
(document.head || document.documentElement).appendChild(style);
|
|
40
|
+
|
|
41
|
+
// ---------- wires: <div class="tm-node" id="orders" data-to="payments payments-db:reads"> ----------
|
|
42
|
+
const SVGNS = 'http://www.w3.org/2000/svg';
|
|
43
|
+
function drawWires() {
|
|
44
|
+
document.documentElement.style.setProperty('--tm-vw', `${document.documentElement.clientWidth}px`);
|
|
45
|
+
for (const map of document.querySelectorAll('.tm-map, .tm-blueprint')) {
|
|
46
|
+
map.querySelector(':scope > svg.tm-wires')?.remove();
|
|
47
|
+
const sources = [...map.querySelectorAll('[data-to]')];
|
|
48
|
+
if (!sources.length) continue;
|
|
49
|
+
// Coordinates live in the map's scroll space, so wires scroll with the boxes.
|
|
50
|
+
const box = map.getBoundingClientRect();
|
|
51
|
+
const ox = box.left + map.clientLeft - map.scrollLeft; const oy = box.top + map.clientTop - map.scrollTop;
|
|
52
|
+
const w = map.scrollWidth; const h = map.scrollHeight;
|
|
53
|
+
const svg = document.createElementNS(SVGNS, 'svg');
|
|
54
|
+
svg.setAttribute('class', 'tm-wires'); svg.setAttribute('aria-hidden', 'true');
|
|
55
|
+
svg.setAttribute('width', w); svg.setAttribute('height', h);
|
|
56
|
+
svg.setAttribute('viewBox', `0 0 ${w} ${h}`);
|
|
57
|
+
const rel = (r) => ({ l: r.left - ox, r: r.right - ox, t: r.top - oy, b: r.bottom - oy, cx: (r.left + r.right) / 2 - ox, cy: (r.top + r.bottom) / 2 - oy });
|
|
58
|
+
// Collect edges first so wires sharing a box get their own exit, entry and track (no merged lines).
|
|
59
|
+
const edges = [];
|
|
60
|
+
for (const src of sources) {
|
|
61
|
+
for (const spec of src.getAttribute('data-to').split(/\s+/).filter(Boolean)) {
|
|
62
|
+
const [id, ...labelParts] = spec.split(':');
|
|
63
|
+
const dst = document.getElementById(id);
|
|
64
|
+
if (!dst || dst === src || !map.contains(dst)) continue;
|
|
65
|
+
edges.push({ src, dst, label: labelParts.join(':').replace(/_/g, ' '), a: rel(src.getBoundingClientRect()), b: rel(dst.getBoundingClientRect()) });
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const down = (e) => e.b.t >= e.a.b - 2;
|
|
69
|
+
const slot = (list, e) => { const i = list.indexOf(e); return i - (list.length - 1) / 2; };
|
|
70
|
+
const outOf = new Map(); const into = new Map();
|
|
71
|
+
for (const e of edges.filter(down)) {
|
|
72
|
+
(outOf.get(e.src) || outOf.set(e.src, []).get(e.src)).push(e);
|
|
73
|
+
(into.get(e.dst) || into.set(e.dst, []).get(e.dst)).push(e);
|
|
74
|
+
}
|
|
75
|
+
for (const l of [...outOf.values(), ...into.values()]) l.sort((p, q) => p.b.cx - q.b.cx || p.a.cx - q.a.cx);
|
|
76
|
+
for (const e of edges) {
|
|
77
|
+
const { src, dst, a, b } = e;
|
|
78
|
+
const key = src.classList.contains('is-key') && dst.classList.contains('is-key');
|
|
79
|
+
const ext = dst.classList.contains('is-ext') || src.classList.contains('is-ext');
|
|
80
|
+
let d; let head; let mid;
|
|
81
|
+
const r = 7;
|
|
82
|
+
if (down(e)) { // below: leave from the bottom, enter at the top
|
|
83
|
+
const so = slot(outOf.get(src), e); const ti = slot(into.get(dst), e);
|
|
84
|
+
const sx = a.cx + so * 14; const sy = a.b; const tx = b.cx + ti * 14; const ty = b.t - 6;
|
|
85
|
+
const my = sy + Math.max(12, Math.min(22, (ty - sy) / 2)) + ti * 7;
|
|
86
|
+
if (Math.abs(tx - sx) < 2) d = `M${sx},${sy} V${ty}`;
|
|
87
|
+
else {
|
|
88
|
+
const dir = tx > sx ? 1 : -1; const rr = Math.min(r, Math.abs(tx - sx) / 2);
|
|
89
|
+
d = `M${sx},${sy} V${my - rr} Q${sx},${my} ${sx + dir * rr},${my} H${tx - dir * rr} Q${tx},${my} ${tx},${my + rr} V${ty}`;
|
|
90
|
+
}
|
|
91
|
+
head = `M${tx - 4.5},${ty - 1} L${tx + 4.5},${ty - 1} L${tx},${ty + 6} Z`;
|
|
92
|
+
// the label sits by the box the wire leaves, so it is clear which wire it names
|
|
93
|
+
mid = { x: sx - 2, y: Math.min(sy + 10, my - 6) };
|
|
94
|
+
} else if (b.b <= a.t + 2) { // above
|
|
95
|
+
const sx = a.cx; const sy = a.t; const tx = b.cx; const ty = b.b + 6;
|
|
96
|
+
const my = sy - Math.max(12, Math.min(22, (sy - ty) / 2));
|
|
97
|
+
const dir = tx > sx ? 1 : -1;
|
|
98
|
+
d = Math.abs(tx - sx) < 2 ? `M${sx},${sy} V${ty}` : `M${sx},${sy} V${my + r} Q${sx},${my} ${sx + dir * r},${my} H${tx - dir * r} Q${tx},${my} ${tx},${my - r} V${ty}`;
|
|
99
|
+
head = `M${tx - 4.5},${ty + 1} L${tx + 4.5},${ty + 1} L${tx},${ty - 6} Z`;
|
|
100
|
+
mid = { x: tx, y: (my + ty) / 2 };
|
|
101
|
+
} else { // same row: side to side
|
|
102
|
+
const right = b.l >= a.r;
|
|
103
|
+
const sx = right ? a.r : a.l; const tx = right ? b.l - 6 : b.r + 6;
|
|
104
|
+
const sy = a.cy; const ty = b.cy; const mx = (sx + tx) / 2;
|
|
105
|
+
d = Math.abs(ty - sy) < 2 ? `M${sx},${sy} H${tx}` : `M${sx},${sy} H${mx} V${ty} H${tx}`;
|
|
106
|
+
head = right ? `M${tx - 1},${ty - 4.5} L${tx - 1},${ty + 4.5} L${tx + 6},${ty} Z` : `M${tx + 1},${ty - 4.5} L${tx + 1},${ty + 4.5} L${tx - 6},${ty} Z`;
|
|
107
|
+
mid = { x: mx, y: ty - 6 };
|
|
108
|
+
}
|
|
109
|
+
const path = document.createElementNS(SVGNS, 'path');
|
|
110
|
+
path.setAttribute('d', d); path.setAttribute('class', `tm-wire${key ? ' is-key' : ''}${ext ? ' is-ext' : ''}`);
|
|
111
|
+
const arrow = document.createElementNS(SVGNS, 'path');
|
|
112
|
+
arrow.setAttribute('d', head); arrow.setAttribute('class', `tm-wire-head${key ? ' is-key' : ''}`);
|
|
113
|
+
svg.appendChild(path);
|
|
114
|
+
svg.appendChild(arrow);
|
|
115
|
+
if (e.label) {
|
|
116
|
+
const t = document.createElementNS(SVGNS, 'text');
|
|
117
|
+
t.setAttribute('class', 'tm-wire-label'); t.setAttribute('x', mid.x + 8); t.setAttribute('y', mid.y + 4);
|
|
118
|
+
t.textContent = e.label; svg.appendChild(t);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
// key wires on top, labels above every wire
|
|
122
|
+
[...svg.querySelectorAll('.tm-wire.is-key, .tm-wire-head.is-key')].forEach((el) => svg.appendChild(el));
|
|
123
|
+
[...svg.querySelectorAll('.tm-wire-label')].forEach((el) => svg.appendChild(el));
|
|
124
|
+
map.insertBefore(svg, map.firstChild);
|
|
125
|
+
markScroll(map);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
// A map wider than the window fades at the edge it can scroll towards.
|
|
129
|
+
function markScroll(map) {
|
|
130
|
+
map.classList.toggle('tm-scrolls', map.scrollWidth - map.clientWidth - map.scrollLeft > 4);
|
|
131
|
+
if (!map.__tmScroll) { map.__tmScroll = true; map.addEventListener('scroll', () => markScroll(map), { passive: true }); }
|
|
132
|
+
}
|
|
133
|
+
let wireTimer = null;
|
|
134
|
+
const scheduleWires = () => { clearTimeout(wireTimer); wireTimer = setTimeout(() => { drawWires(); placePins(); }, 40); };
|
|
135
|
+
|
|
136
|
+
// ---------- annotate ----------
|
|
137
|
+
const INTERACTIVE = 'a[href],button,input,select,textarea,label,summary,[contenteditable],[role=button]';
|
|
138
|
+
let annotating = false;
|
|
139
|
+
let hovered = null;
|
|
140
|
+
const notes = new Map(); // id -> { id, kind, el, rect, label, src, excerpt, text, n }
|
|
141
|
+
|
|
142
|
+
function labelOf(el) {
|
|
143
|
+
return clean(el.getAttribute('data-label') || el.getAttribute('aria-label')
|
|
144
|
+
|| el.querySelector?.('.tm-node-title,h1,h2,h3,h4,strong,b')?.textContent || el.getAttribute('title') || el.textContent, 60) || el.tagName.toLowerCase();
|
|
145
|
+
}
|
|
146
|
+
function pickTarget(t) {
|
|
147
|
+
if (!t || t.closest?.('#truemap-layer')) return null;
|
|
148
|
+
const anchored = t.closest('[data-src]');
|
|
149
|
+
if (anchored) return anchored;
|
|
150
|
+
const block = t.closest('.tm-node,.tm-step,.tm-card,.tm-callout,.tm-ask,.tm-option,.tm-link,h1,h2,h3,h4,h5,h6,p,li,td,th,dt,dd,figure,pre,blockquote,svg g,svg');
|
|
151
|
+
if (block && block !== document.body && block !== document.documentElement) return block;
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
function cssPath(el) {
|
|
155
|
+
if (el.id) return `#${CSS.escape(el.id)}`;
|
|
156
|
+
const parts = [];
|
|
157
|
+
for (let n = el; n && n.nodeType === 1 && n !== document.body; n = n.parentElement) {
|
|
158
|
+
if (n.id) { parts.unshift(`#${CSS.escape(n.id)}`); break; }
|
|
159
|
+
let i = 1; for (let s = n.previousElementSibling; s; s = s.previousElementSibling) if (s.tagName === n.tagName) i++;
|
|
160
|
+
parts.unshift(`${n.tagName.toLowerCase()}:nth-of-type(${i})`);
|
|
161
|
+
}
|
|
162
|
+
return parts.join(' > ');
|
|
163
|
+
}
|
|
164
|
+
function setHover(el) {
|
|
165
|
+
if (hovered === el) return;
|
|
166
|
+
hovered?.removeAttribute('data-tm-hover');
|
|
167
|
+
hovered = el; el?.setAttribute('data-tm-hover', '');
|
|
168
|
+
}
|
|
169
|
+
function setAnnotate(on) {
|
|
170
|
+
annotating = !!on;
|
|
171
|
+
document.documentElement.toggleAttribute('data-tm-annotate', annotating);
|
|
172
|
+
if (!annotating) { setHover(null); }
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// Shadow layer for the comment card, pins and the selection button: page CSS can't touch it.
|
|
176
|
+
const host = document.createElement('div');
|
|
177
|
+
host.id = 'truemap-layer';
|
|
178
|
+
host.style.cssText = 'position:absolute;top:0;left:0;width:0;height:0;z-index:2147483646;';
|
|
179
|
+
const shadow = host.attachShadow({ mode: 'open' });
|
|
180
|
+
shadow.innerHTML = `<style>
|
|
181
|
+
:host { all: initial; }
|
|
182
|
+
* { box-sizing: border-box; }
|
|
183
|
+
.card { position: absolute; width: 340px; background: #fff; color: #17202b; border: 1px solid #c2c8c1; border-radius: 10px;
|
|
184
|
+
box-shadow: 0 12px 32px -12px rgba(23,32,43,.28); font: 14px/1.45 "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", sans-serif; padding: 12px; }
|
|
185
|
+
.what { display: flex; align-items: baseline; gap: 8px; min-width: 0; margin: 0 2px 8px; }
|
|
186
|
+
.what b { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
|
|
187
|
+
.what code { font: 11.5px "IBM Plex Mono", ui-monospace, Consolas, monospace; color: #8a939e; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; flex: 1; }
|
|
188
|
+
textarea { width: 100%; min-height: 72px; max-height: 220px; resize: none; font: inherit; color: inherit; background: #f6f7f4; border: 1px solid #dde1db; border-radius: 7px; padding: 9px 10px; outline: none; }
|
|
189
|
+
textarea:focus { border-color: #2453c4; background: #fff; }
|
|
190
|
+
.suggest { display: flex; flex-wrap: wrap; gap: 4px 14px; margin: 8px 2px 0; }
|
|
191
|
+
.suggest button { all: unset; cursor: pointer; font-size: 12.5px; color: #2453c4; }
|
|
192
|
+
.suggest button:hover { text-decoration: underline; text-underline-offset: 3px; }
|
|
193
|
+
.foot { display: flex; align-items: center; gap: 8px; margin-top: 10px; }
|
|
194
|
+
.hint { flex: 1; font-size: 11.5px; color: #8a939e; line-height: 1.3; }
|
|
195
|
+
.btn { all: unset; cursor: pointer; font: 500 13px/1 "IBM Plex Sans", system-ui, sans-serif; padding: 8px 11px; border-radius: 6px; color: #5b6573; }
|
|
196
|
+
.btn:hover { background: #eef0ec; color: #17202b; }
|
|
197
|
+
.btn.primary { background: #2453c4; color: #fff; }
|
|
198
|
+
.btn.primary:hover { background: #1b3f96; }
|
|
199
|
+
.btn.danger { color: #b03a2e; }
|
|
200
|
+
.btn:focus-visible { outline: 2px solid #2453c4; outline-offset: 2px; }
|
|
201
|
+
.pin { position: absolute; width: 20px; height: 20px; border-radius: 50%; background: #2453c4; color: #fff;
|
|
202
|
+
font: 600 11px/20px "IBM Plex Mono", ui-monospace, Consolas, monospace; text-align: center; cursor: pointer; border: 2px solid #fff; box-shadow: 0 1px 3px rgba(23,32,43,.25); }
|
|
203
|
+
.select-btn { position: absolute; all: unset; position: absolute; cursor: pointer; background: #17202b; color: #fff; font: 500 12.5px/1 "IBM Plex Sans", system-ui, sans-serif; padding: 7px 10px; border-radius: 6px; box-shadow: 0 6px 18px -8px rgba(23,32,43,.5); }
|
|
204
|
+
.select-btn:hover { background: #2453c4; }
|
|
205
|
+
@media (prefers-color-scheme: dark) {
|
|
206
|
+
.card { background: #191d23; color: #edf0f3; border-color: #3d4651; box-shadow: 0 12px 32px -12px rgba(0,0,0,.6); }
|
|
207
|
+
textarea { background: #121519; border-color: #2a3039; }
|
|
208
|
+
textarea:focus { background: #121519; border-color: #7ea3ff; }
|
|
209
|
+
.suggest button { color: #7ea3ff; }
|
|
210
|
+
.btn { color: #9ba4af; } .btn:hover { background: #1f242b; color: #edf0f3; }
|
|
211
|
+
.btn.primary { background: #2453c4; color: #fff; }
|
|
212
|
+
.pin { border-color: #121519; }
|
|
213
|
+
}
|
|
214
|
+
</style><div id="pins"></div><div id="slot"></div>`;
|
|
215
|
+
const pinsEl = shadow.getElementById('pins');
|
|
216
|
+
const slot = shadow.getElementById('slot');
|
|
217
|
+
const mount = () => { if (!host.isConnected) document.body.appendChild(host); };
|
|
218
|
+
|
|
219
|
+
let card = null; // { el, target, textRange, noteId }
|
|
220
|
+
// Text comments keep their words highlighted (CSS Custom Highlight API where available).
|
|
221
|
+
function paintHighlights() {
|
|
222
|
+
if (!window.CSS?.highlights || typeof Highlight === 'undefined') return;
|
|
223
|
+
const ranges = [...notes.values()].map((n) => n.range).filter(Boolean);
|
|
224
|
+
if (card?.range) ranges.push(card.range);
|
|
225
|
+
if (ranges.length) CSS.highlights.set('tm-note', new Highlight(...ranges)); else CSS.highlights.delete('tm-note');
|
|
226
|
+
}
|
|
227
|
+
function closeCard() {
|
|
228
|
+
if (!card) return;
|
|
229
|
+
card.target?.removeAttribute('data-tm-active');
|
|
230
|
+
card.el.remove(); card = null;
|
|
231
|
+
paintHighlights();
|
|
232
|
+
}
|
|
233
|
+
function docRect(r) { return { left: r.left + scrollX, top: r.top + scrollY, right: r.right + scrollX, bottom: r.bottom + scrollY, width: r.width, height: r.height }; }
|
|
234
|
+
|
|
235
|
+
function openCard({ target = null, text = null, rect, noteId = null }) {
|
|
236
|
+
closeCard(); hideSelectBtn(); mount(); setHover(null);
|
|
237
|
+
const existing = noteId ? notes.get(noteId) : null;
|
|
238
|
+
const sel = getSelection();
|
|
239
|
+
const range = existing ? existing.range || null : (text && sel?.rangeCount ? sel.getRangeAt(0).cloneRange() : null);
|
|
240
|
+
const label = existing?.label || (target ? labelOf(target) : clean(text, 60));
|
|
241
|
+
const src = existing?.src ?? (target ? target.closest('[data-src]')?.getAttribute('data-src') || '' : (text && rangeSrc()) || '');
|
|
242
|
+
const el = document.createElement('div');
|
|
243
|
+
el.className = 'card'; el.setAttribute('role', 'dialog'); el.setAttribute('aria-label', 'Comment');
|
|
244
|
+
el.innerHTML = `<div class="what"><b></b><code></code></div><textarea rows="3"></textarea>
|
|
245
|
+
<div class="suggest"></div>
|
|
246
|
+
<div class="foot"><span class="hint"></span>${existing ? `<button class="btn danger" data-act="remove" type="button">${T.remove}</button>` : `<button class="btn" data-act="cancel" type="button">${T.cancel}</button>`}<button class="btn primary" data-act="add" type="button">${existing ? T.save : T.add}</button></div>`;
|
|
247
|
+
el.querySelector('b').textContent = text ? `“${label}”` : label;
|
|
248
|
+
el.querySelector('code').textContent = src;
|
|
249
|
+
el.querySelector('.hint').textContent = T.hint;
|
|
250
|
+
const ta = el.querySelector('textarea'); ta.placeholder = T.placeholder; ta.value = existing?.text || '';
|
|
251
|
+
const sug = el.querySelector('.suggest');
|
|
252
|
+
if (!existing) for (const [lbl, prompt] of T.suggest) {
|
|
253
|
+
const b = document.createElement('button'); b.type = 'button'; b.textContent = lbl;
|
|
254
|
+
b.onclick = () => { ta.value = prompt; ta.focus(); };
|
|
255
|
+
sug.appendChild(b);
|
|
256
|
+
} else sug.remove();
|
|
257
|
+
slot.appendChild(el);
|
|
258
|
+
// position: below the target, kept inside the viewport
|
|
259
|
+
const r = rect || docRect(target.getBoundingClientRect());
|
|
260
|
+
const w = 340; const vw = document.documentElement.clientWidth;
|
|
261
|
+
let left = Math.min(Math.max(8 + scrollX, r.left), scrollX + vw - w - 12);
|
|
262
|
+
let top = r.bottom + 10;
|
|
263
|
+
const h = el.offsetHeight || 220;
|
|
264
|
+
if (top + h > scrollY + innerHeight - 8 && r.top - h - 10 > scrollY) top = r.top - h - 10;
|
|
265
|
+
el.style.left = `${left}px`; el.style.top = `${top}px`;
|
|
266
|
+
target?.setAttribute('data-tm-active', '');
|
|
267
|
+
card = { el, target, text, noteId, label, src, rect: r, range };
|
|
268
|
+
paintHighlights();
|
|
269
|
+
const finish = (sendNow) => {
|
|
270
|
+
const value = ta.value.trim();
|
|
271
|
+
if (!value) { ta.focus(); return; }
|
|
272
|
+
const id = noteId || `n${Date.now().toString(36)}${Math.random().toString(36).slice(2, 5)}`;
|
|
273
|
+
const note = {
|
|
274
|
+
id, kind: target ? 'element' : 'text', label, src, text: value,
|
|
275
|
+
excerpt: clean(target ? (target.innerText || target.textContent) : text, 400),
|
|
276
|
+
selector: target ? cssPath(target) : '', rect: target ? null : r,
|
|
277
|
+
};
|
|
278
|
+
notes.set(id, { ...note, el: target, range });
|
|
279
|
+
post({ type: existing ? 'note-update' : 'note', page: pagePath(), note: { ...note, rect: note.rect ? { left: note.rect.left, top: note.rect.top, right: note.rect.right, bottom: note.rect.bottom } : null }, send: !!sendNow });
|
|
280
|
+
closeCard(); placePins();
|
|
281
|
+
getSelection()?.removeAllRanges();
|
|
282
|
+
};
|
|
283
|
+
el.addEventListener('click', (e) => {
|
|
284
|
+
const act = e.target.closest('[data-act]')?.dataset.act;
|
|
285
|
+
if (act === 'add') finish(false);
|
|
286
|
+
else if (act === 'cancel') closeCard();
|
|
287
|
+
else if (act === 'remove') { removeNote(noteId, true); closeCard(); }
|
|
288
|
+
});
|
|
289
|
+
ta.addEventListener('keydown', (e) => {
|
|
290
|
+
if (e.key === 'Escape') { e.preventDefault(); e.stopPropagation(); closeCard(); }
|
|
291
|
+
else if (e.key === 'Enter' && !e.shiftKey && !e.isComposing && e.keyCode !== 229) { e.preventDefault(); finish(e.ctrlKey || e.metaKey); }
|
|
292
|
+
});
|
|
293
|
+
setTimeout(() => ta.focus({ preventScroll: true }), 0);
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
function rangeSrc() {
|
|
297
|
+
const sel = getSelection();
|
|
298
|
+
const node = sel?.anchorNode?.nodeType === 1 ? sel.anchorNode : sel?.anchorNode?.parentElement;
|
|
299
|
+
return node?.closest?.('[data-src]')?.getAttribute('data-src') || '';
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
function removeNote(id, tellParent) {
|
|
303
|
+
const n = notes.get(id); if (!n) return;
|
|
304
|
+
notes.delete(id);
|
|
305
|
+
if (tellParent) post({ type: 'note-remove', page: pagePath(), id });
|
|
306
|
+
placePins();
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function placePins() {
|
|
310
|
+
paintHighlights();
|
|
311
|
+
pinsEl.innerHTML = '';
|
|
312
|
+
for (const el of document.querySelectorAll('[data-tm-noted]')) el.removeAttribute('data-tm-noted');
|
|
313
|
+
if (!notes.size) return;
|
|
314
|
+
mount();
|
|
315
|
+
let i = 0;
|
|
316
|
+
for (const n of notes.values()) {
|
|
317
|
+
i++;
|
|
318
|
+
let r = null;
|
|
319
|
+
if (n.el && !n.el.isConnected && n.selector) { try { n.el = document.querySelector(n.selector); } catch { n.el = null; } }
|
|
320
|
+
if (n.el?.isConnected) { r = docRect(n.el.getBoundingClientRect()); n.el.setAttribute('data-tm-noted', ''); }
|
|
321
|
+
else if (n.rect) r = n.rect;
|
|
322
|
+
if (!r) continue;
|
|
323
|
+
const pin = document.createElement('div');
|
|
324
|
+
pin.className = 'pin'; pin.textContent = String(n.num || i); pin.title = n.text;
|
|
325
|
+
pin.style.left = `${r.left - 10}px`; pin.style.top = `${r.top - 10}px`;
|
|
326
|
+
pin.onclick = (e) => { e.stopPropagation(); openCard({ target: n.el?.isConnected ? n.el : null, text: n.el ? null : n.label, rect: r, noteId: n.id }); };
|
|
327
|
+
pinsEl.appendChild(pin);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// Floating "Comment" button for a text selection (works in any mode).
|
|
332
|
+
let selectBtn = null;
|
|
333
|
+
function hideSelectBtn() { selectBtn?.remove(); selectBtn = null; }
|
|
334
|
+
function showSelectBtn(text, rect) {
|
|
335
|
+
hideSelectBtn(); mount();
|
|
336
|
+
selectBtn = document.createElement('button');
|
|
337
|
+
selectBtn.className = 'select-btn'; selectBtn.type = 'button'; selectBtn.textContent = T.comment;
|
|
338
|
+
selectBtn.style.left = `${rect.left}px`; selectBtn.style.top = `${Math.max(scrollY + 4, rect.top - 38)}px`;
|
|
339
|
+
selectBtn.onmousedown = (e) => e.preventDefault();
|
|
340
|
+
selectBtn.onclick = () => openCard({ text, rect });
|
|
341
|
+
slot.appendChild(selectBtn);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// What a click would comment on. Hover shows exactly this, so the outline never lies.
|
|
345
|
+
// Form controls keep working while annotating (decisions are answered, not annotated).
|
|
346
|
+
function annotTarget(t) {
|
|
347
|
+
if (!t || t.closest('input,select,textarea,button,label,summary,.tm-ask')) return null;
|
|
348
|
+
return t.closest('a.tm-node,a.tm-link') || pickTarget(t);
|
|
349
|
+
}
|
|
350
|
+
document.addEventListener('mousemove', (e) => {
|
|
351
|
+
if (!annotating || card) return;
|
|
352
|
+
const t = e.target instanceof Element ? e.target : null;
|
|
353
|
+
setHover(annotTarget(t));
|
|
354
|
+
}, { passive: true });
|
|
355
|
+
document.addEventListener('mouseleave', () => setHover(null));
|
|
356
|
+
|
|
357
|
+
document.addEventListener('click', (e) => {
|
|
358
|
+
if (!e.isTrusted) return;
|
|
359
|
+
if (e.composedPath().includes(host)) return;
|
|
360
|
+
const t = e.target instanceof Element ? e.target : null;
|
|
361
|
+
if (card) { closeCard(); if (!annotating) return; }
|
|
362
|
+
if (!annotating || !t) return;
|
|
363
|
+
if (String(getSelection?.() || '').trim().length > 1) return;
|
|
364
|
+
const el = annotTarget(t);
|
|
365
|
+
if (!el) return;
|
|
366
|
+
e.preventDefault(); e.stopPropagation();
|
|
367
|
+
const existing = [...notes.values()].find((n) => n.el === el);
|
|
368
|
+
openCard({ target: el, noteId: existing?.id || null });
|
|
369
|
+
}, true);
|
|
370
|
+
|
|
371
|
+
document.addEventListener('mouseup', (e) => {
|
|
372
|
+
if (!e.isTrusted || e.composedPath().includes(host)) return;
|
|
373
|
+
setTimeout(() => {
|
|
374
|
+
const sel = getSelection();
|
|
375
|
+
const text = clean(sel?.toString(), 600);
|
|
376
|
+
if (!text || text.length < 2 || !sel.rangeCount) { if (!card) hideSelectBtn(); return; }
|
|
377
|
+
const r = docRect(sel.getRangeAt(0).getBoundingClientRect());
|
|
378
|
+
if (annotating) openCard({ text, rect: r }); else showSelectBtn(text, r);
|
|
379
|
+
}, 0);
|
|
380
|
+
});
|
|
381
|
+
document.addEventListener('selectionchange', () => { if (!String(getSelection() || '').trim()) hideSelectBtn(); });
|
|
382
|
+
|
|
383
|
+
document.addEventListener('keydown', (e) => {
|
|
384
|
+
const typing = e.target instanceof Element && e.target.closest('input,textarea,select,[contenteditable]');
|
|
385
|
+
if (e.key === 'Escape' && card) { closeCard(); return; }
|
|
386
|
+
if (e.key === 'Escape' && annotating && !typing && !e.defaultPrevented && !e.composedPath().includes(host)) { post({ type: 'annotate-off' }); return; }
|
|
387
|
+
if (!typing && !e.composedPath().includes(host) && !e.ctrlKey && !e.metaKey && !e.altKey && e.key.toLowerCase() === 'a') {
|
|
388
|
+
post({ type: 'toggle-annotate' });
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
// ---------- decisions: <form class="tm-ask" data-tm-ask="question"> ----------
|
|
393
|
+
document.addEventListener('submit', (e) => {
|
|
394
|
+
const form = e.target;
|
|
395
|
+
if (!(form instanceof HTMLFormElement) || !form.hasAttribute('data-tm-ask')) return;
|
|
396
|
+
e.preventDefault();
|
|
397
|
+
const fd = new FormData(form);
|
|
398
|
+
const values = {}; let note = '';
|
|
399
|
+
for (const [k, v] of fd.entries()) {
|
|
400
|
+
if (typeof v !== 'string') continue;
|
|
401
|
+
if (k === 'note') { note = v.trim(); continue; }
|
|
402
|
+
(values[k] ||= []).push(v);
|
|
403
|
+
}
|
|
404
|
+
const question = form.getAttribute('data-tm-ask') || clean(form.querySelector('h3,legend,h2,h4')?.textContent, 200);
|
|
405
|
+
const choice = Object.values(values).flat().join(', ');
|
|
406
|
+
if (!choice && !note) return;
|
|
407
|
+
form.setAttribute('data-tm-sent', '');
|
|
408
|
+
const btn = form.querySelector('button[type=submit],button:not([type])');
|
|
409
|
+
if (btn) btn.textContent = T.sent;
|
|
410
|
+
post({ type: 'answer', page: pagePath(), trusted: e.isTrusted, question, choice, note, src: form.closest('[data-src]')?.getAttribute('data-src') || '' });
|
|
411
|
+
}, true);
|
|
412
|
+
|
|
413
|
+
// Pages may also talk to the agent directly (marked as coming from the page, not the user).
|
|
414
|
+
window.truemap = { send: (text) => post({ type: 'page-send', page: pagePath(), text: clean(text, 4000) }) };
|
|
415
|
+
|
|
416
|
+
// ---------- messages from the workspace ----------
|
|
417
|
+
window.addEventListener('message', (e) => {
|
|
418
|
+
if (e.source !== parentWin || e.origin !== PARENT || !e.data || !e.data.__truemap) return;
|
|
419
|
+
const d = e.data;
|
|
420
|
+
if (d.type === 'marks') {
|
|
421
|
+
document.querySelectorAll('[data-tm-mark]').forEach((el) => el.removeAttribute('data-tm-mark'));
|
|
422
|
+
for (const m of d.items || []) {
|
|
423
|
+
document.querySelectorAll('[data-src]').forEach((el) => {
|
|
424
|
+
if (el.getAttribute('data-src') !== m.src) return;
|
|
425
|
+
el.setAttribute('data-tm-mark', m.state);
|
|
426
|
+
if (!el.hasAttribute('title')) el.setAttribute('title', m.state === 'broken' ? T.broken : T.changed);
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
} else if (d.type === 'annotate') {
|
|
430
|
+
setAnnotate(d.on); if (!d.on) closeCard();
|
|
431
|
+
} else if (d.type === 'notes') {
|
|
432
|
+
// the workspace owns unsent notes; restore pins after a reload, renumber after removals
|
|
433
|
+
const keep = new Map();
|
|
434
|
+
(d.items || []).forEach((n, i) => {
|
|
435
|
+
const old = notes.get(n.id);
|
|
436
|
+
let el = old?.el || null;
|
|
437
|
+
if (!el && n.selector) { try { el = document.querySelector(n.selector); } catch { el = null; } }
|
|
438
|
+
keep.set(n.id, { ...n, el, rect: old?.rect || n.rect || null, range: old?.range || null, num: i + 1 });
|
|
439
|
+
});
|
|
440
|
+
notes.clear(); for (const [k, v] of keep) notes.set(k, v);
|
|
441
|
+
placePins();
|
|
442
|
+
} else if (d.type === 'focus-note') {
|
|
443
|
+
const n = notes.get(d.id);
|
|
444
|
+
const el = n?.el?.isConnected ? n.el : null;
|
|
445
|
+
if (el) { el.scrollIntoView({ block: 'center', behavior: 'smooth' }); el.setAttribute('data-tm-flash', ''); setTimeout(() => el.removeAttribute('data-tm-flash'), 2400); }
|
|
446
|
+
else if (n?.rect) window.scrollTo({ top: n.rect.top - innerHeight / 3, behavior: 'smooth' });
|
|
447
|
+
} else if (d.type === 'scroll') {
|
|
448
|
+
window.scrollTo(0, d.y || 0);
|
|
449
|
+
} else if (d.type === 'highlight') {
|
|
450
|
+
const el = [...document.querySelectorAll('[data-src]')].find((x) => x.getAttribute('data-src') === d.src);
|
|
451
|
+
if (el) { el.scrollIntoView({ block: 'center', behavior: 'smooth' }); el.setAttribute('data-tm-flash', ''); setTimeout(() => el.removeAttribute('data-tm-flash'), 2400); }
|
|
452
|
+
}
|
|
453
|
+
});
|
|
454
|
+
|
|
455
|
+
let scrollTimer = null;
|
|
456
|
+
window.addEventListener('scroll', () => {
|
|
457
|
+
clearTimeout(scrollTimer);
|
|
458
|
+
scrollTimer = setTimeout(() => post({ type: 'scroll', page: pagePath(), y: window.scrollY }), 120);
|
|
459
|
+
}, { passive: true });
|
|
460
|
+
window.addEventListener('resize', scheduleWires);
|
|
461
|
+
|
|
462
|
+
const ready = () => {
|
|
463
|
+
drawWires();
|
|
464
|
+
if ('ResizeObserver' in window) new ResizeObserver(scheduleWires).observe(document.body);
|
|
465
|
+
document.fonts?.ready?.then(scheduleWires);
|
|
466
|
+
const meta = document.querySelector('meta[name="truemap-annotate"]')?.getAttribute('content');
|
|
467
|
+
post({ type: 'loaded', page: pagePath(), title: document.title || '', annotate: meta ? /^(on|true|1)$/i.test(meta) : null });
|
|
468
|
+
};
|
|
469
|
+
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', ready); else ready();
|
|
470
|
+
})();
|