truemap-cli 0.1.0 → 0.3.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/README.md +31 -27
- package/bin/truemap.js +3 -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 +248 -186
- package/package.json +1 -1
- package/skills/truemap/SKILL.md +4 -3
- package/src/check.js +32 -1
- package/src/cli.js +75 -20
- package/src/export.js +62 -0
- package/src/guide.md +67 -32
- package/src/outline.js +71 -0
- package/src/server.js +453 -394
- package/src/ui/sdk.js +350 -58
- package/src/ui/wires.js +237 -0
- package/src/ui/workspace.css +194 -128
- package/src/ui/workspace.html +59 -45
- package/src/ui/workspace.js +277 -104
package/src/ui/sdk.js
CHANGED
|
@@ -1,95 +1,362 @@
|
|
|
1
1
|
// Injected into every .truemap page by the local server (never saved into the page file).
|
|
2
|
-
// Runs inside
|
|
3
|
-
//
|
|
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
|
|
4
6
|
(() => {
|
|
5
7
|
if (window.__truemapSdk) return;
|
|
6
8
|
window.__truemapSdk = true;
|
|
7
9
|
const parentWin = window.parent;
|
|
8
|
-
|
|
10
|
+
const framed = parentWin && parentWin !== window;
|
|
9
11
|
const PARENT = '__TRUEMAP_PARENT__';
|
|
10
|
-
const post = (msg) => parentWin.postMessage({ __truemap: true, ...msg }, PARENT);
|
|
11
|
-
const
|
|
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
|
+
};
|
|
12
22
|
|
|
23
|
+
// ---------- page styles for truemap states ----------
|
|
13
24
|
const style = document.createElement('style');
|
|
14
25
|
style.textContent = `
|
|
15
|
-
[data-
|
|
16
|
-
[data-
|
|
17
|
-
[data-tm-
|
|
18
|
-
|
|
19
|
-
[data-tm-
|
|
20
|
-
[data-tm-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
@
|
|
24
|
-
|
|
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] { cursor: pointer; }
|
|
31
|
+
html[data-tm-annotate] a, html[data-tm-annotate] label { cursor: pointer; }
|
|
32
|
+
@keyframes tm-flash { 0%,100% { outline-color: transparent } 35% { outline-color: var(--tm-route, #2453c4) } }
|
|
33
|
+
[data-tm-flash] { outline: 2px solid var(--tm-route, #2453c4) !important; outline-offset: 4px; animation: tm-flash 1.1s ease 2; }
|
|
34
|
+
@media (prefers-reduced-motion: reduce) { [data-tm-flash] { animation: none; } }
|
|
35
|
+
::highlight(tm-note) { background-color: color-mix(in oklab, var(--tm-route, #2453c4) 20%, transparent); }
|
|
25
36
|
`;
|
|
26
37
|
(document.head || document.documentElement).appendChild(style);
|
|
27
38
|
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
const
|
|
39
|
+
const drawWires = () => window.truemapWires?.draw();
|
|
40
|
+
let wireTimer = null;
|
|
41
|
+
const scheduleWires = () => { clearTimeout(wireTimer); wireTimer = setTimeout(() => { drawWires(); placePins(); }, 40); };
|
|
42
|
+
|
|
43
|
+
// ---------- annotate ----------
|
|
44
|
+
const INTERACTIVE = 'a[href],button,input,select,textarea,label,summary,[contenteditable],[role=button]';
|
|
45
|
+
let annotating = false;
|
|
46
|
+
let hovered = null;
|
|
47
|
+
const notes = new Map(); // id -> { id, kind, el, rect, label, src, excerpt, text, n }
|
|
31
48
|
|
|
32
49
|
function labelOf(el) {
|
|
33
|
-
return clean(el.getAttribute('data-label') || el.getAttribute('aria-label')
|
|
34
|
-
|| el.querySelector?.('.tm-node-title,h1,h2,h3,h4,strong,b')?.textContent || el.textContent, 60) || el.tagName.toLowerCase();
|
|
50
|
+
return clean(el.getAttribute('data-label') || el.getAttribute('aria-label')
|
|
51
|
+
|| el.querySelector?.('.tm-node-title,h1,h2,h3,h4,strong,b')?.textContent || el.getAttribute('title') || el.textContent, 60) || el.tagName.toLowerCase();
|
|
35
52
|
}
|
|
36
|
-
|
|
37
53
|
function pickTarget(t) {
|
|
54
|
+
if (!t || t.closest?.('#truemap-layer')) return null;
|
|
38
55
|
const anchored = t.closest('[data-src]');
|
|
39
56
|
if (anchored) return anchored;
|
|
40
|
-
const block = t.closest('h1,h2,h3,h4,h5,h6,p,li,td,th,figure,pre,blockquote
|
|
41
|
-
if (block && block !== document.body) return block;
|
|
57
|
+
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');
|
|
58
|
+
if (block && block !== document.body && block !== document.documentElement) return block;
|
|
42
59
|
return null;
|
|
43
60
|
}
|
|
61
|
+
function cssPath(el) {
|
|
62
|
+
if (el.id) return `#${CSS.escape(el.id)}`;
|
|
63
|
+
const parts = [];
|
|
64
|
+
for (let n = el; n && n.nodeType === 1 && n !== document.body; n = n.parentElement) {
|
|
65
|
+
if (n.id) { parts.unshift(`#${CSS.escape(n.id)}`); break; }
|
|
66
|
+
let i = 1; for (let s = n.previousElementSibling; s; s = s.previousElementSibling) if (s.tagName === n.tagName) i++;
|
|
67
|
+
parts.unshift(`${n.tagName.toLowerCase()}:nth-of-type(${i})`);
|
|
68
|
+
}
|
|
69
|
+
return parts.join(' > ');
|
|
70
|
+
}
|
|
71
|
+
let rings = null;
|
|
72
|
+
function setHover(el) {
|
|
73
|
+
if (hovered === el) return;
|
|
74
|
+
hovered?.removeAttribute('data-tm-hover');
|
|
75
|
+
hovered = el; el?.setAttribute('data-tm-hover', '');
|
|
76
|
+
paintRings();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Highlights are drawn in the top layer, never as CSS outlines, so a scrolling or clipped container
|
|
80
|
+
// can't cut them off. A ring covers the element and everything inside it (a badge that sticks out too).
|
|
81
|
+
function extentOf(el) {
|
|
82
|
+
const r0 = el.getBoundingClientRect();
|
|
83
|
+
let l = r0.left; let t = r0.top; let r = r0.right; let b = r0.bottom;
|
|
84
|
+
const kids = [...el.querySelectorAll('*')].slice(0, 400);
|
|
85
|
+
// Content scrolled out of view inside a scrolling block (a long code line, a map) doesn't count.
|
|
86
|
+
const clips = [el, ...kids].filter((n) => { const s = getComputedStyle(n); return s.overflowX !== 'visible' || s.overflowY !== 'visible'; });
|
|
87
|
+
for (const kid of kids) {
|
|
88
|
+
const k = kid.getBoundingClientRect();
|
|
89
|
+
if (!k.width || !k.height) continue;
|
|
90
|
+
let kl = k.left; let kt = k.top; let kr = k.right; let kb = k.bottom;
|
|
91
|
+
for (const c of clips) {
|
|
92
|
+
if (c === kid || !c.contains(kid)) continue;
|
|
93
|
+
const cr = c.getBoundingClientRect();
|
|
94
|
+
kl = Math.max(kl, cr.left); kt = Math.max(kt, cr.top); kr = Math.min(kr, cr.right); kb = Math.min(kb, cr.bottom);
|
|
95
|
+
}
|
|
96
|
+
if (kr <= kl || kb <= kt) continue;
|
|
97
|
+
l = Math.min(l, kl); t = Math.min(t, kt); r = Math.max(r, kr); b = Math.max(b, kb);
|
|
98
|
+
}
|
|
99
|
+
// An element half scrolled out of its own scrolling parent is cut at that parent's edge too.
|
|
100
|
+
for (let a = el.parentElement; a && a !== document.body; a = a.parentElement) {
|
|
101
|
+
const s = getComputedStyle(a);
|
|
102
|
+
if (s.overflowX === 'visible' && s.overflowY === 'visible') continue;
|
|
103
|
+
const ar = a.getBoundingClientRect();
|
|
104
|
+
l = Math.max(l, ar.left - 2); t = Math.max(t, ar.top - 2); r = Math.min(r, ar.right + 2); b = Math.min(b, ar.bottom + 2);
|
|
105
|
+
}
|
|
106
|
+
return { left: l + scrollX - 4, top: t + scrollY - 4, width: r - l + 8, height: b - t + 8 };
|
|
107
|
+
}
|
|
108
|
+
function paintRings() {
|
|
109
|
+
if (!rings) return;
|
|
110
|
+
rings.innerHTML = '';
|
|
111
|
+
const ring = (el, cls) => {
|
|
112
|
+
if (!el?.isConnected) return;
|
|
113
|
+
mount();
|
|
114
|
+
const x = extentOf(el); const d = document.createElement('div');
|
|
115
|
+
d.className = `ring ${cls}`;
|
|
116
|
+
Object.assign(d.style, { left: `${x.left}px`, top: `${x.top}px`, width: `${x.width}px`, height: `${x.height}px` });
|
|
117
|
+
rings.appendChild(d);
|
|
118
|
+
};
|
|
119
|
+
for (const n of notes.values()) if (n.el && n.el !== card?.target) ring(n.el, 'noted');
|
|
120
|
+
if (card?.target) ring(card.target, 'active');
|
|
121
|
+
else if (annotating && hovered) ring(hovered, 'hover');
|
|
122
|
+
}
|
|
123
|
+
function setAnnotate(on) {
|
|
124
|
+
annotating = !!on;
|
|
125
|
+
document.documentElement.toggleAttribute('data-tm-annotate', annotating);
|
|
126
|
+
if (!annotating) { setHover(null); }
|
|
127
|
+
paintRings();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// Shadow layer for the comment card, pins and the selection button: page CSS can't touch it.
|
|
131
|
+
const host = document.createElement('div');
|
|
132
|
+
host.id = 'truemap-layer';
|
|
133
|
+
host.style.cssText = 'position:absolute;top:0;left:0;width:0;height:0;z-index:2147483646;';
|
|
134
|
+
const shadow = host.attachShadow({ mode: 'open' });
|
|
135
|
+
shadow.innerHTML = `<style>
|
|
136
|
+
:host { all: initial; }
|
|
137
|
+
* { box-sizing: border-box; }
|
|
138
|
+
.card { position: absolute; width: 340px; background: #fff; color: #17202b; border: 1px solid #c2c8c1; border-radius: 10px;
|
|
139
|
+
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; }
|
|
140
|
+
.what { display: flex; align-items: baseline; gap: 8px; min-width: 0; margin: 0 2px 8px; }
|
|
141
|
+
.what b { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
|
|
142
|
+
.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; }
|
|
143
|
+
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; }
|
|
144
|
+
textarea:focus { border-color: #2453c4; background: #fff; }
|
|
145
|
+
.suggest { display: flex; flex-wrap: wrap; gap: 4px 14px; margin: 8px 2px 0; }
|
|
146
|
+
.suggest button { all: unset; cursor: pointer; font-size: 12.5px; color: #2453c4; }
|
|
147
|
+
.suggest button:hover { text-decoration: underline; text-underline-offset: 3px; }
|
|
148
|
+
.foot { display: flex; align-items: center; gap: 8px; margin-top: 10px; }
|
|
149
|
+
.hint { flex: 1; font-size: 11.5px; color: #8a939e; line-height: 1.3; }
|
|
150
|
+
.btn { all: unset; cursor: pointer; font: 500 13px/1 "IBM Plex Sans", system-ui, sans-serif; padding: 8px 11px; border-radius: 6px; color: #5b6573; }
|
|
151
|
+
.btn:hover { background: #eef0ec; color: #17202b; }
|
|
152
|
+
.btn.primary { background: #2453c4; color: #fff; }
|
|
153
|
+
.btn.primary:hover { background: #1b3f96; }
|
|
154
|
+
.btn.danger { color: #b03a2e; }
|
|
155
|
+
.btn:focus-visible { outline: 2px solid #2453c4; outline-offset: 2px; }
|
|
156
|
+
.ring { position: absolute; border: 1.5px solid #2453c4; border-radius: 8px; pointer-events: none; }
|
|
157
|
+
.ring.active { border-width: 2px; }
|
|
158
|
+
.ring.noted { border-color: rgba(36, 83, 196, .5); }
|
|
159
|
+
@media (prefers-color-scheme: dark) { .ring { border-color: #7ea3ff; } .ring.noted { border-color: rgba(126, 163, 255, .55); } }
|
|
160
|
+
.pin { position: absolute; width: 20px; height: 20px; border-radius: 50%; background: #2453c4; color: #fff;
|
|
161
|
+
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); }
|
|
162
|
+
.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); }
|
|
163
|
+
.select-btn:hover { background: #2453c4; }
|
|
164
|
+
@media (prefers-color-scheme: dark) {
|
|
165
|
+
.card { background: #191d23; color: #edf0f3; border-color: #3d4651; box-shadow: 0 12px 32px -12px rgba(0,0,0,.6); }
|
|
166
|
+
textarea { background: #121519; border-color: #2a3039; }
|
|
167
|
+
textarea:focus { background: #121519; border-color: #7ea3ff; }
|
|
168
|
+
.suggest button { color: #7ea3ff; }
|
|
169
|
+
.btn { color: #9ba4af; } .btn:hover { background: #1f242b; color: #edf0f3; }
|
|
170
|
+
.btn.primary { background: #2453c4; color: #fff; }
|
|
171
|
+
.pin { border-color: #121519; }
|
|
172
|
+
}
|
|
173
|
+
</style><div id="rings"></div><div id="pins"></div><div id="slot"></div>`;
|
|
174
|
+
const pinsEl = shadow.getElementById('pins');
|
|
175
|
+
const slot = shadow.getElementById('slot');
|
|
176
|
+
rings = shadow.getElementById('rings');
|
|
177
|
+
const mount = () => { if (!host.isConnected) document.body.appendChild(host); };
|
|
178
|
+
|
|
179
|
+
let card = null; // { el, target, textRange, noteId }
|
|
180
|
+
// Text comments keep their words highlighted (CSS Custom Highlight API where available).
|
|
181
|
+
function paintHighlights() {
|
|
182
|
+
if (!window.CSS?.highlights || typeof Highlight === 'undefined') return;
|
|
183
|
+
const ranges = [...notes.values()].map((n) => n.range).filter(Boolean);
|
|
184
|
+
if (card?.range) ranges.push(card.range);
|
|
185
|
+
if (ranges.length) CSS.highlights.set('tm-note', new Highlight(...ranges)); else CSS.highlights.delete('tm-note');
|
|
186
|
+
}
|
|
187
|
+
function closeCard() {
|
|
188
|
+
if (!card) return;
|
|
189
|
+
card.target?.removeAttribute('data-tm-active');
|
|
190
|
+
card.el.remove(); card = null;
|
|
191
|
+
paintHighlights(); paintRings();
|
|
192
|
+
}
|
|
193
|
+
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 }; }
|
|
44
194
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
195
|
+
function openCard({ target = null, text = null, rect, noteId = null }) {
|
|
196
|
+
closeCard(); hideSelectBtn(); mount(); setHover(null);
|
|
197
|
+
const existing = noteId ? notes.get(noteId) : null;
|
|
198
|
+
const sel = getSelection();
|
|
199
|
+
const range = existing ? existing.range || null : (text && sel?.rangeCount ? sel.getRangeAt(0).cloneRange() : null);
|
|
200
|
+
const label = existing?.label || (target ? labelOf(target) : clean(text, 60));
|
|
201
|
+
const src = existing?.src ?? (target ? target.closest('[data-src]')?.getAttribute('data-src') || '' : (text && rangeSrc()) || '');
|
|
202
|
+
const el = document.createElement('div');
|
|
203
|
+
el.className = 'card'; el.setAttribute('role', 'dialog'); el.setAttribute('aria-label', 'Comment');
|
|
204
|
+
el.innerHTML = `<div class="what"><b></b><code></code></div><textarea rows="3"></textarea>
|
|
205
|
+
<div class="suggest"></div>
|
|
206
|
+
<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>`;
|
|
207
|
+
el.querySelector('b').textContent = text ? `“${label}”` : label;
|
|
208
|
+
el.querySelector('code').textContent = src;
|
|
209
|
+
el.querySelector('.hint').textContent = T.hint;
|
|
210
|
+
const ta = el.querySelector('textarea'); ta.placeholder = T.placeholder; ta.value = existing?.text || '';
|
|
211
|
+
const sug = el.querySelector('.suggest');
|
|
212
|
+
if (!existing) for (const [lbl, prompt] of T.suggest) {
|
|
213
|
+
const b = document.createElement('button'); b.type = 'button'; b.textContent = lbl;
|
|
214
|
+
b.onclick = () => { ta.value = prompt; ta.focus(); };
|
|
215
|
+
sug.appendChild(b);
|
|
216
|
+
} else sug.remove();
|
|
217
|
+
slot.appendChild(el);
|
|
218
|
+
// position: below the target, kept inside the viewport
|
|
219
|
+
const r = rect || docRect(target.getBoundingClientRect());
|
|
220
|
+
const w = 340; const vw = document.documentElement.clientWidth;
|
|
221
|
+
let left = Math.min(Math.max(8 + scrollX, r.left), scrollX + vw - w - 12);
|
|
222
|
+
let top = r.bottom + 10;
|
|
223
|
+
const h = el.offsetHeight || 220;
|
|
224
|
+
if (top + h > scrollY + innerHeight - 8 && r.top - h - 10 > scrollY) top = r.top - h - 10;
|
|
225
|
+
el.style.left = `${left}px`; el.style.top = `${top}px`;
|
|
226
|
+
target?.setAttribute('data-tm-active', '');
|
|
227
|
+
card = { el, target, text, noteId, label, src, rect: r, range };
|
|
228
|
+
paintHighlights(); paintRings();
|
|
229
|
+
const finish = (sendNow) => {
|
|
230
|
+
const value = ta.value.trim();
|
|
231
|
+
if (!value) { ta.focus(); return; }
|
|
232
|
+
const id = noteId || `n${Date.now().toString(36)}${Math.random().toString(36).slice(2, 5)}`;
|
|
233
|
+
const note = {
|
|
234
|
+
id, kind: target ? 'element' : 'text', label, src, text: value,
|
|
235
|
+
excerpt: clean(target ? (target.innerText || target.textContent) : text, 400),
|
|
236
|
+
selector: target ? cssPath(target) : '', rect: target ? null : r,
|
|
237
|
+
};
|
|
238
|
+
notes.set(id, { ...note, el: target, range });
|
|
239
|
+
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 });
|
|
240
|
+
closeCard(); placePins();
|
|
241
|
+
getSelection()?.removeAllRanges();
|
|
242
|
+
};
|
|
243
|
+
el.addEventListener('click', (e) => {
|
|
244
|
+
const act = e.target.closest('[data-act]')?.dataset.act;
|
|
245
|
+
if (act === 'add') finish(false);
|
|
246
|
+
else if (act === 'cancel') closeCard();
|
|
247
|
+
else if (act === 'remove') { removeNote(noteId, true); closeCard(); }
|
|
248
|
+
});
|
|
249
|
+
ta.addEventListener('keydown', (e) => {
|
|
250
|
+
if (e.key === 'Escape') { e.preventDefault(); e.stopPropagation(); closeCard(); }
|
|
251
|
+
else if (e.key === 'Enter' && !e.shiftKey && !e.isComposing && e.keyCode !== 229) { e.preventDefault(); finish(e.ctrlKey || e.metaKey); }
|
|
57
252
|
});
|
|
253
|
+
setTimeout(() => ta.focus({ preventScroll: true }), 0);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
function rangeSrc() {
|
|
257
|
+
const sel = getSelection();
|
|
258
|
+
const node = sel?.anchorNode?.nodeType === 1 ? sel.anchorNode : sel?.anchorNode?.parentElement;
|
|
259
|
+
return node?.closest?.('[data-src]')?.getAttribute('data-src') || '';
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function removeNote(id, tellParent) {
|
|
263
|
+
const n = notes.get(id); if (!n) return;
|
|
264
|
+
notes.delete(id);
|
|
265
|
+
if (tellParent) post({ type: 'note-remove', page: pagePath(), id });
|
|
266
|
+
placePins();
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function placePins() {
|
|
270
|
+
paintHighlights();
|
|
271
|
+
pinsEl.innerHTML = '';
|
|
272
|
+
for (const el of document.querySelectorAll('[data-tm-noted]')) el.removeAttribute('data-tm-noted');
|
|
273
|
+
paintRings();
|
|
274
|
+
if (!notes.size) return;
|
|
275
|
+
mount();
|
|
276
|
+
let i = 0;
|
|
277
|
+
for (const n of notes.values()) {
|
|
278
|
+
i++;
|
|
279
|
+
let r = null;
|
|
280
|
+
if (n.el && !n.el.isConnected && n.selector) { try { n.el = document.querySelector(n.selector); } catch { n.el = null; } }
|
|
281
|
+
if (n.el?.isConnected) { const x = extentOf(n.el); r = { left: x.left + 4, top: x.top + 4 }; n.el.setAttribute('data-tm-noted', ''); }
|
|
282
|
+
else if (n.rect) r = n.rect;
|
|
283
|
+
if (!r) continue;
|
|
284
|
+
const pin = document.createElement('div');
|
|
285
|
+
pin.className = 'pin'; pin.textContent = String(n.num || i); pin.title = n.text;
|
|
286
|
+
pin.style.left = `${r.left - 10}px`; pin.style.top = `${r.top - 10}px`;
|
|
287
|
+
pin.onclick = (e) => { e.stopPropagation(); openCard({ target: n.el?.isConnected ? n.el : null, text: n.el ? null : n.label, rect: r, noteId: n.id }); };
|
|
288
|
+
pinsEl.appendChild(pin);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// Floating "Comment" button for a text selection (works in any mode).
|
|
293
|
+
let selectBtn = null;
|
|
294
|
+
function hideSelectBtn() { selectBtn?.remove(); selectBtn = null; }
|
|
295
|
+
function showSelectBtn(text, rect) {
|
|
296
|
+
hideSelectBtn(); mount();
|
|
297
|
+
selectBtn = document.createElement('button');
|
|
298
|
+
selectBtn.className = 'select-btn'; selectBtn.type = 'button'; selectBtn.textContent = T.comment;
|
|
299
|
+
selectBtn.style.left = `${rect.left}px`; selectBtn.style.top = `${Math.max(scrollY + 4, rect.top - 38)}px`;
|
|
300
|
+
selectBtn.onmousedown = (e) => e.preventDefault();
|
|
301
|
+
selectBtn.onclick = () => openCard({ text, rect });
|
|
302
|
+
slot.appendChild(selectBtn);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
// What a click would comment on. Hover shows exactly this, so the outline never lies.
|
|
306
|
+
// Form controls keep working while annotating (decisions are answered, not annotated).
|
|
307
|
+
function annotTarget(t) {
|
|
308
|
+
if (!t || t.closest('input,select,textarea,button,label,summary,.tm-ask')) return null;
|
|
309
|
+
return t.closest('a.tm-node,a.tm-link') || pickTarget(t);
|
|
58
310
|
}
|
|
311
|
+
document.addEventListener('mousemove', (e) => {
|
|
312
|
+
if (!annotating || card) return;
|
|
313
|
+
const t = e.target instanceof Element ? e.target : null;
|
|
314
|
+
setHover(annotTarget(t));
|
|
315
|
+
}, { passive: true });
|
|
316
|
+
document.addEventListener('mouseleave', () => setHover(null));
|
|
59
317
|
|
|
60
318
|
document.addEventListener('click', (e) => {
|
|
61
319
|
if (!e.isTrusted) return;
|
|
320
|
+
if (e.composedPath().includes(host)) return;
|
|
62
321
|
const t = e.target instanceof Element ? e.target : null;
|
|
63
|
-
if (
|
|
64
|
-
if (
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
}
|
|
322
|
+
if (card) { closeCard(); if (!annotating) return; }
|
|
323
|
+
if (!annotating || !t) return;
|
|
324
|
+
if (String(getSelection?.() || '').trim().length > 1) return;
|
|
325
|
+
const el = annotTarget(t);
|
|
326
|
+
if (!el) return;
|
|
327
|
+
e.preventDefault(); e.stopPropagation();
|
|
328
|
+
const existing = [...notes.values()].find((n) => n.el === el);
|
|
329
|
+
openCard({ target: el, noteId: existing?.id || null });
|
|
72
330
|
}, true);
|
|
73
331
|
|
|
74
332
|
document.addEventListener('mouseup', (e) => {
|
|
75
|
-
if (!e.isTrusted) return;
|
|
333
|
+
if (!e.isTrusted || e.composedPath().includes(host)) return;
|
|
76
334
|
setTimeout(() => {
|
|
77
|
-
const sel =
|
|
335
|
+
const sel = getSelection();
|
|
78
336
|
const text = clean(sel?.toString(), 600);
|
|
79
|
-
if (!text || text.length < 2) return;
|
|
80
|
-
const
|
|
81
|
-
|
|
337
|
+
if (!text || text.length < 2 || !sel.rangeCount) { if (!card) hideSelectBtn(); return; }
|
|
338
|
+
const r = docRect(sel.getRangeAt(0).getBoundingClientRect());
|
|
339
|
+
if (annotating) openCard({ text, rect: r }); else showSelectBtn(text, r);
|
|
82
340
|
}, 0);
|
|
83
341
|
});
|
|
342
|
+
document.addEventListener('selectionchange', () => { if (!String(getSelection() || '').trim()) hideSelectBtn(); });
|
|
343
|
+
|
|
344
|
+
document.addEventListener('keydown', (e) => {
|
|
345
|
+
const typing = e.target instanceof Element && e.target.closest('input,textarea,select,[contenteditable]');
|
|
346
|
+
if (e.key === 'Escape' && card) { closeCard(); return; }
|
|
347
|
+
if (e.key === 'Escape' && annotating && !typing && !e.defaultPrevented && !e.composedPath().includes(host)) { post({ type: 'annotate-off' }); return; }
|
|
348
|
+
if (!typing && !e.composedPath().includes(host) && !e.ctrlKey && !e.metaKey && !e.altKey && e.key.toLowerCase() === 'a') {
|
|
349
|
+
post({ type: 'toggle-annotate' });
|
|
350
|
+
}
|
|
351
|
+
});
|
|
84
352
|
|
|
85
|
-
//
|
|
353
|
+
// ---------- decisions: <form class="tm-ask" data-tm-ask="question"> ----------
|
|
86
354
|
document.addEventListener('submit', (e) => {
|
|
87
355
|
const form = e.target;
|
|
88
356
|
if (!(form instanceof HTMLFormElement) || !form.hasAttribute('data-tm-ask')) return;
|
|
89
357
|
e.preventDefault();
|
|
90
358
|
const fd = new FormData(form);
|
|
91
|
-
const values = {};
|
|
92
|
-
let note = '';
|
|
359
|
+
const values = {}; let note = '';
|
|
93
360
|
for (const [k, v] of fd.entries()) {
|
|
94
361
|
if (typeof v !== 'string') continue;
|
|
95
362
|
if (k === 'note') { note = v.trim(); continue; }
|
|
@@ -100,7 +367,6 @@
|
|
|
100
367
|
if (!choice && !note) return;
|
|
101
368
|
form.setAttribute('data-tm-sent', '');
|
|
102
369
|
const btn = form.querySelector('button[type=submit],button:not([type])');
|
|
103
|
-
if (btn && !btn.dataset.tmLabel) { btn.dataset.tmLabel = btn.textContent; }
|
|
104
370
|
if (btn) btn.textContent = T.sent;
|
|
105
371
|
post({ type: 'answer', page: pagePath(), trusted: e.isTrusted, question, choice, note, src: form.closest('[data-src]')?.getAttribute('data-src') || '' });
|
|
106
372
|
}, true);
|
|
@@ -108,11 +374,12 @@
|
|
|
108
374
|
// Pages may also talk to the agent directly (marked as coming from the page, not the user).
|
|
109
375
|
window.truemap = { send: (text) => post({ type: 'page-send', page: pagePath(), text: clean(text, 4000) }) };
|
|
110
376
|
|
|
377
|
+
// ---------- messages from the workspace ----------
|
|
111
378
|
window.addEventListener('message', (e) => {
|
|
112
379
|
if (e.source !== parentWin || e.origin !== PARENT || !e.data || !e.data.__truemap) return;
|
|
113
380
|
const d = e.data;
|
|
114
381
|
if (d.type === 'marks') {
|
|
115
|
-
document.querySelectorAll('[data-tm-mark]').forEach((el) =>
|
|
382
|
+
document.querySelectorAll('[data-tm-mark]').forEach((el) => el.removeAttribute('data-tm-mark'));
|
|
116
383
|
for (const m of d.items || []) {
|
|
117
384
|
document.querySelectorAll('[data-src]').forEach((el) => {
|
|
118
385
|
if (el.getAttribute('data-src') !== m.src) return;
|
|
@@ -120,13 +387,29 @@
|
|
|
120
387
|
if (!el.hasAttribute('title')) el.setAttribute('title', m.state === 'broken' ? T.broken : T.changed);
|
|
121
388
|
});
|
|
122
389
|
}
|
|
390
|
+
} else if (d.type === 'annotate') {
|
|
391
|
+
setAnnotate(d.on); if (!d.on) closeCard();
|
|
392
|
+
} else if (d.type === 'notes') {
|
|
393
|
+
// the workspace owns unsent notes; restore pins after a reload, renumber after removals
|
|
394
|
+
const keep = new Map();
|
|
395
|
+
(d.items || []).forEach((n, i) => {
|
|
396
|
+
const old = notes.get(n.id);
|
|
397
|
+
let el = old?.el || null;
|
|
398
|
+
if (!el && n.selector) { try { el = document.querySelector(n.selector); } catch { el = null; } }
|
|
399
|
+
keep.set(n.id, { ...n, el, rect: old?.rect || n.rect || null, range: old?.range || null, num: i + 1 });
|
|
400
|
+
});
|
|
401
|
+
notes.clear(); for (const [k, v] of keep) notes.set(k, v);
|
|
402
|
+
placePins();
|
|
403
|
+
} else if (d.type === 'focus-note') {
|
|
404
|
+
const n = notes.get(d.id);
|
|
405
|
+
const el = n?.el?.isConnected ? n.el : null;
|
|
406
|
+
if (el) { el.scrollIntoView({ block: 'center', behavior: 'smooth' }); el.setAttribute('data-tm-flash', ''); setTimeout(() => el.removeAttribute('data-tm-flash'), 2400); }
|
|
407
|
+
else if (n?.rect) window.scrollTo({ top: n.rect.top - innerHeight / 3, behavior: 'smooth' });
|
|
123
408
|
} else if (d.type === 'scroll') {
|
|
124
409
|
window.scrollTo(0, d.y || 0);
|
|
125
|
-
} else if (d.type === 'clear-selection') {
|
|
126
|
-
setSelected([]);
|
|
127
410
|
} else if (d.type === 'highlight') {
|
|
128
411
|
const el = [...document.querySelectorAll('[data-src]')].find((x) => x.getAttribute('data-src') === d.src);
|
|
129
|
-
if (el) { el.scrollIntoView({ block: 'center', behavior: 'smooth' }); el.setAttribute('data-tm-flash', ''); setTimeout(() => el.removeAttribute('data-tm-flash'),
|
|
412
|
+
if (el) { el.scrollIntoView({ block: 'center', behavior: 'smooth' }); el.setAttribute('data-tm-flash', ''); setTimeout(() => el.removeAttribute('data-tm-flash'), 2400); }
|
|
130
413
|
}
|
|
131
414
|
});
|
|
132
415
|
|
|
@@ -135,7 +418,16 @@
|
|
|
135
418
|
clearTimeout(scrollTimer);
|
|
136
419
|
scrollTimer = setTimeout(() => post({ type: 'scroll', page: pagePath(), y: window.scrollY }), 120);
|
|
137
420
|
}, { passive: true });
|
|
421
|
+
window.addEventListener('resize', scheduleWires);
|
|
422
|
+
// Rings follow their element when anything scrolls, including a map scrolling sideways.
|
|
423
|
+
document.addEventListener('scroll', () => { if (rings && (hovered || card || notes.size)) { placePins(); } }, { capture: true, passive: true });
|
|
138
424
|
|
|
139
|
-
const ready = () =>
|
|
425
|
+
const ready = () => {
|
|
426
|
+
drawWires();
|
|
427
|
+
if ('ResizeObserver' in window) new ResizeObserver(scheduleWires).observe(document.body);
|
|
428
|
+
document.fonts?.ready?.then(scheduleWires);
|
|
429
|
+
const meta = document.querySelector('meta[name="truemap-annotate"]')?.getAttribute('content');
|
|
430
|
+
post({ type: 'loaded', page: pagePath(), title: document.title || '', annotate: meta ? /^(on|true|1)$/i.test(meta) : null });
|
|
431
|
+
};
|
|
140
432
|
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', ready); else ready();
|
|
141
433
|
})();
|