annotask 0.0.3 → 0.0.4

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.
@@ -0,0 +1,1007 @@
1
+ // src/plugin/bridge-client.ts
2
+ function bridgeClientScript() {
3
+ return `
4
+ (function() {
5
+ // Don't run inside the Annotask shell
6
+ if (window.location.pathname.startsWith('/__annotask')) return;
7
+ // Don't run if already initialized
8
+ if (window.__ANNOTASK_BRIDGE__) return;
9
+ window.__ANNOTASK_BRIDGE__ = true;
10
+
11
+ // \u2500\u2500 Element Registry \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
12
+ var eidCounter = 0;
13
+ var eidMap = new Map(); // eid string \u2192 WeakRef<Element>
14
+ var elToEid = new WeakMap(); // Element \u2192 eid string
15
+
16
+ function getEid(el) {
17
+ if (!el) return null;
18
+ var existing = elToEid.get(el);
19
+ if (existing) return existing;
20
+ eidCounter++;
21
+ var eid = 'e-' + eidCounter;
22
+ eidMap.set(eid, new WeakRef(el));
23
+ elToEid.set(el, eid);
24
+ return eid;
25
+ }
26
+
27
+ function getEl(eid) {
28
+ var ref = eidMap.get(eid);
29
+ return ref ? ref.deref() || null : null;
30
+ }
31
+
32
+ // \u2500\u2500 PostMessage Helpers \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
33
+ var shellOrigin = '*'; // Will be tightened on first shell message
34
+
35
+ function sendToShell(type, payload, id) {
36
+ var msg = { type: type, payload: payload || {}, source: 'annotask-client' };
37
+ if (id) msg.id = id;
38
+ window.parent.postMessage(msg, shellOrigin);
39
+ }
40
+
41
+ function respond(id, payload) {
42
+ sendToShell(null, payload, id);
43
+ // type is not needed for responses \u2014 shell matches by id
44
+ }
45
+
46
+ // \u2500\u2500 Source Element Resolution \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
47
+ function hasSourceAttr(el) {
48
+ return el.hasAttribute && (el.hasAttribute('data-annotask-file') || el.hasAttribute('data-astro-source-file'));
49
+ }
50
+
51
+ function findSourceElement(el) {
52
+ var c = el;
53
+ while (c) {
54
+ if (hasSourceAttr(c)) return { sourceEl: c, targetEl: el };
55
+ c = c.parentElement;
56
+ }
57
+ return { sourceEl: el, targetEl: el };
58
+ }
59
+
60
+ function getSourceData(el) {
61
+ // Prefer data-annotask-* attributes, fall back to data-astro-source-* (Astro framework)
62
+ var file = el.getAttribute('data-annotask-file') || '';
63
+ var line = el.getAttribute('data-annotask-line') || '';
64
+ var component = el.getAttribute('data-annotask-component') || '';
65
+
66
+ if (!file && el.getAttribute('data-astro-source-file')) {
67
+ var astroFile = el.getAttribute('data-astro-source-file') || '';
68
+ // Convert absolute path to project-relative by finding src/ prefix
69
+ var srcIdx = astroFile.indexOf('/src/');
70
+ file = srcIdx !== -1 ? astroFile.slice(srcIdx + 1) : astroFile;
71
+ }
72
+ if ((!line || line === '0') && el.getAttribute('data-astro-source-loc')) {
73
+ // data-astro-source-loc format: "line:col"
74
+ line = (el.getAttribute('data-astro-source-loc') || '').split(':')[0];
75
+ }
76
+ if (!component && file) {
77
+ // Derive component name from file path
78
+ var parts = file.split('/');
79
+ var fileName = parts[parts.length - 1] || '';
80
+ component = fileName.replace(/.[^.]+$/, '');
81
+ }
82
+
83
+ var mfe = el.getAttribute('data-annotask-mfe') || '';
84
+
85
+ return { file: file, line: line, component: component, mfe: mfe };
86
+ }
87
+
88
+ function getRect(el) {
89
+ var r = el.getBoundingClientRect();
90
+ return { x: r.x, y: r.y, width: r.width, height: r.height };
91
+ }
92
+
93
+ // \u2500\u2500 Interaction Mode \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
94
+ var storedMode = '';
95
+ try { storedMode = localStorage.getItem('annotask:mode') || ''; } catch(e) {}
96
+ var currentMode = storedMode || 'select';
97
+ var inspectModes = { select: true, pin: true };
98
+
99
+ // \u2500\u2500 Event Handlers \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
100
+ var lastHoverEid = null;
101
+ var rafPending = false;
102
+ var pendingHoverData = null;
103
+
104
+ function onMouseOver(e) {
105
+ if (!inspectModes[currentMode]) return;
106
+ var el = e.target;
107
+ if (!el || el === document.documentElement || el === document.body) return;
108
+ var eid = getEid(el);
109
+ if (eid === lastHoverEid) return;
110
+ lastHoverEid = eid;
111
+
112
+ var source = findSourceElement(el);
113
+ var data = getSourceData(source.sourceEl);
114
+
115
+ pendingHoverData = {
116
+ eid: eid,
117
+ tag: el.tagName.toLowerCase(),
118
+ file: data.file,
119
+ component: data.component,
120
+ rect: getRect(el)
121
+ };
122
+
123
+ if (!rafPending) {
124
+ rafPending = true;
125
+ requestAnimationFrame(function() {
126
+ rafPending = false;
127
+ if (pendingHoverData) sendToShell('hover:enter', pendingHoverData);
128
+ });
129
+ }
130
+ }
131
+
132
+ function onMouseOut(e) {
133
+ if (!inspectModes[currentMode]) return;
134
+ // Only fire leave when truly leaving all elements
135
+ if (e.relatedTarget && e.relatedTarget !== document.documentElement) return;
136
+ lastHoverEid = null;
137
+ pendingHoverData = null;
138
+ sendToShell('hover:leave', {});
139
+ }
140
+
141
+ function onClick(e) {
142
+ if (!inspectModes[currentMode]) return;
143
+ e.preventDefault();
144
+ e.stopPropagation();
145
+
146
+ var el = e.target;
147
+ if (!el) return;
148
+
149
+ var source = findSourceElement(el);
150
+ var data = getSourceData(source.sourceEl);
151
+ var targetEl = source.targetEl;
152
+ var classes = typeof targetEl.className === 'string' ? targetEl.className : '';
153
+
154
+ sendToShell('click:element', {
155
+ eid: getEid(targetEl),
156
+ sourceEid: getEid(source.sourceEl),
157
+ file: data.file,
158
+ line: data.line,
159
+ component: data.component,
160
+ mfe: data.mfe,
161
+ tag: targetEl.tagName.toLowerCase(),
162
+ classes: classes,
163
+ rect: getRect(targetEl),
164
+ shiftKey: e.shiftKey,
165
+ clientX: e.clientX,
166
+ clientY: e.clientY
167
+ });
168
+ }
169
+
170
+ function onMouseDown(e) {
171
+ if (!inspectModes[currentMode]) return;
172
+ e.stopPropagation();
173
+ }
174
+
175
+ function onMouseUp(e) {
176
+ if (currentMode !== 'highlight') return;
177
+ var sel = document.getSelection();
178
+ var text = sel ? sel.toString().trim() : '';
179
+ if (!text || text.length < 2) return;
180
+ var anchorEl = sel.anchorNode ? sel.anchorNode.parentElement : null;
181
+ if (!anchorEl) return;
182
+ var source = findSourceElement(anchorEl);
183
+ var data = getSourceData(source.sourceEl);
184
+
185
+ sendToShell('selection:text', {
186
+ text: text,
187
+ eid: getEid(anchorEl),
188
+ file: data.file,
189
+ line: parseInt(data.line) || 0,
190
+ component: data.component,
191
+ mfe: data.mfe,
192
+ tag: anchorEl.tagName.toLowerCase()
193
+ });
194
+ }
195
+
196
+ function onContextMenu(e) {
197
+ if (currentMode === 'interact') return;
198
+ e.preventDefault();
199
+ var el = e.target;
200
+ if (!el) return;
201
+ var source = findSourceElement(el);
202
+ var data = getSourceData(source.sourceEl);
203
+ var targetEl = source.targetEl;
204
+ var classes = typeof targetEl.className === 'string' ? targetEl.className : '';
205
+
206
+ sendToShell('contextmenu:element', {
207
+ eid: getEid(targetEl),
208
+ sourceEid: getEid(source.sourceEl),
209
+ file: data.file,
210
+ line: data.line,
211
+ component: data.component,
212
+ mfe: data.mfe,
213
+ tag: targetEl.tagName.toLowerCase(),
214
+ classes: classes,
215
+ rect: getRect(targetEl),
216
+ shiftKey: false,
217
+ clientX: e.clientX,
218
+ clientY: e.clientY
219
+ });
220
+ }
221
+
222
+ function onKeyDown(e) {
223
+ if ((e.ctrlKey || e.metaKey) && e.key === 'z' && !e.shiftKey) {
224
+ e.preventDefault();
225
+ sendToShell('keydown', { key: e.key, ctrlKey: e.ctrlKey, metaKey: e.metaKey, shiftKey: e.shiftKey });
226
+ }
227
+ }
228
+
229
+ // Install event listeners
230
+ document.addEventListener('mouseover', onMouseOver, { capture: true });
231
+ document.addEventListener('mouseout', onMouseOut, { capture: true });
232
+ document.addEventListener('mousedown', onMouseDown, { capture: true });
233
+ document.addEventListener('click', onClick, { capture: true });
234
+ document.addEventListener('mouseup', onMouseUp, { capture: true });
235
+ document.addEventListener('keydown', onKeyDown, { capture: true });
236
+ document.addEventListener('contextmenu', onContextMenu, { capture: true });
237
+
238
+ // \u2500\u2500 User Action Tracking (interact mode) \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
239
+ // Tracks meaningful page actions (link/button clicks) so the LLM
240
+ // can understand what the user did to reach the current state.
241
+
242
+ var lastActionTs = 0;
243
+ function onUserAction(e) {
244
+ var el = e.target;
245
+ if (!el || !el.closest) return;
246
+
247
+ // Debounce \u2014 ignore clicks within 50ms (same gesture)
248
+ var now = Date.now();
249
+ if (now - lastActionTs < 50) return;
250
+ lastActionTs = now;
251
+
252
+ // Find the best description of what was clicked
253
+ var actionEl = el.closest('a, button, [role="button"], [role="tab"], [role="menuitem"], [role="link"], [type="submit"], tr[class*="row"], [data-pc-section="bodyrow"]');
254
+ var descEl = actionEl || el;
255
+
256
+ var tag = descEl.tagName.toLowerCase();
257
+ // Get concise text: prefer innerText of the immediate target, fall back to closest actionable
258
+ var text = '';
259
+ if (el.innerText) text = el.innerText.trim();
260
+ if (!text && descEl.innerText) text = descEl.innerText.trim();
261
+ text = text.split(String.fromCharCode(10))[0].substring(0, 60);
262
+
263
+ var href = '';
264
+ if (tag === 'a') href = descEl.getAttribute('href') || '';
265
+ else if (el.closest('a')) href = el.closest('a').getAttribute('href') || '';
266
+
267
+ // Get component context if available
268
+ var source = findSourceElement(descEl);
269
+ var data = getSourceData(source.sourceEl);
270
+
271
+ sendToShell('user:action', {
272
+ tag: tag,
273
+ text: text,
274
+ href: href,
275
+ component: data.component || '',
276
+ });
277
+ }
278
+
279
+ document.addEventListener('click', onUserAction, { capture: true });
280
+
281
+ // \u2500\u2500 Route Tracking \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
282
+ var lastRoute = window.location.pathname;
283
+
284
+ function checkRoute() {
285
+ var path = window.location.pathname;
286
+ if (path !== lastRoute) {
287
+ lastRoute = path;
288
+ sendToShell('route:changed', { path: path });
289
+ }
290
+ }
291
+
292
+ window.addEventListener('popstate', checkRoute);
293
+ window.addEventListener('hashchange', checkRoute);
294
+ setInterval(checkRoute, 2000); // safety net for pushState
295
+
296
+ // \u2500\u2500 Message Handler \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
297
+ window.addEventListener('message', function(event) {
298
+ var msg = event.data;
299
+ if (!msg || msg.source !== 'annotask-shell') return;
300
+ // Tighten origin on first shell message
301
+ if (shellOrigin === '*' && event.origin) shellOrigin = event.origin;
302
+
303
+ var type = msg.type;
304
+ var payload = msg.payload || {};
305
+ var id = msg.id;
306
+
307
+ // \u2500\u2500 Mode \u2500\u2500
308
+ if (type === 'mode:set') {
309
+ currentMode = payload.mode || 'select';
310
+ try { localStorage.setItem('annotask:mode', currentMode); } catch(e) {}
311
+ return;
312
+ }
313
+
314
+ // \u2500\u2500 Ping \u2500\u2500
315
+ if (type === 'bridge:ping') {
316
+ respond(id, {});
317
+ return;
318
+ }
319
+
320
+ // \u2500\u2500 Element Resolution \u2500\u2500
321
+ if (type === 'resolve:at-point') {
322
+ var el = document.elementFromPoint(payload.x, payload.y);
323
+ if (!el || el === document.documentElement || el === document.body) {
324
+ respond(id, null);
325
+ return;
326
+ }
327
+ var src = findSourceElement(el);
328
+ var srcData = getSourceData(src.sourceEl);
329
+ respond(id, {
330
+ eid: getEid(src.targetEl),
331
+ file: srcData.file,
332
+ line: srcData.line,
333
+ component: srcData.component,
334
+ mfe: srcData.mfe,
335
+ tag: src.sourceEl.tagName.toLowerCase(),
336
+ rect: getRect(src.sourceEl),
337
+ classes: typeof src.targetEl.className === 'string' ? src.targetEl.className : ''
338
+ });
339
+ return;
340
+ }
341
+
342
+ if (type === 'resolve:template-group') {
343
+ var all = document.querySelectorAll(
344
+ '[data-annotask-file="' + payload.file + '"][data-annotask-line="' + payload.line + '"]'
345
+ );
346
+ // Also check Astro source attributes
347
+ if (all.length === 0 && payload.file && payload.line) {
348
+ // Try matching by astro source attributes (absolute path ends with file, loc starts with line)
349
+ var astroAll = document.querySelectorAll('[data-astro-source-file]');
350
+ var matched = [];
351
+ for (var ai = 0; ai < astroAll.length; ai++) {
352
+ var af = astroAll[ai].getAttribute('data-astro-source-file') || '';
353
+ var al = (astroAll[ai].getAttribute('data-astro-source-loc') || '').split(':')[0];
354
+ if (af.endsWith('/' + payload.file) && al === payload.line) matched.push(astroAll[ai]);
355
+ }
356
+ if (matched.length > 0) all = matched;
357
+ }
358
+ var eids = [];
359
+ var rects = [];
360
+ for (var i = 0; i < all.length; i++) {
361
+ if (all[i].tagName.toLowerCase() === payload.tagName) {
362
+ eids.push(getEid(all[i]));
363
+ rects.push(getRect(all[i]));
364
+ }
365
+ }
366
+ respond(id, { eids: eids, rects: rects });
367
+ return;
368
+ }
369
+
370
+ if (type === 'resolve:rect') {
371
+ var rEl = getEl(payload.eid);
372
+ respond(id, rEl ? { rect: getRect(rEl) } : null);
373
+ return;
374
+ }
375
+
376
+ if (type === 'resolve:rects') {
377
+ var results = [];
378
+ for (var j = 0; j < payload.eids.length; j++) {
379
+ var re = getEl(payload.eids[j]);
380
+ results.push(re ? getRect(re) : null);
381
+ }
382
+ respond(id, { rects: results });
383
+ return;
384
+ }
385
+
386
+ // \u2500\u2500 Style Operations \u2500\u2500
387
+ if (type === 'style:get-computed') {
388
+ var sEl = getEl(payload.eid);
389
+ if (!sEl) { respond(id, { styles: {} }); return; }
390
+ var cs = window.getComputedStyle(sEl);
391
+ var styles = {};
392
+ for (var k = 0; k < payload.properties.length; k++) {
393
+ styles[payload.properties[k]] = cs.getPropertyValue(payload.properties[k]);
394
+ }
395
+ respond(id, { styles: styles });
396
+ return;
397
+ }
398
+
399
+ if (type === 'style:apply') {
400
+ var aEl = getEl(payload.eid);
401
+ if (!aEl) { respond(id, { before: '' }); return; }
402
+ var before = window.getComputedStyle(aEl).getPropertyValue(payload.property);
403
+ aEl.style.setProperty(payload.property, payload.value);
404
+ respond(id, { before: before });
405
+ return;
406
+ }
407
+
408
+ if (type === 'style:apply-batch') {
409
+ var befores = [];
410
+ for (var m = 0; m < payload.eids.length; m++) {
411
+ var bEl = getEl(payload.eids[m]);
412
+ if (bEl) {
413
+ befores.push(window.getComputedStyle(bEl).getPropertyValue(payload.property));
414
+ bEl.style.setProperty(payload.property, payload.value);
415
+ } else {
416
+ befores.push('');
417
+ }
418
+ }
419
+ respond(id, { befores: befores });
420
+ return;
421
+ }
422
+
423
+ if (type === 'style:undo') {
424
+ var uEl = getEl(payload.eid);
425
+ if (uEl) {
426
+ if (payload.value) uEl.style.setProperty(payload.property, payload.value);
427
+ else uEl.style.removeProperty(payload.property);
428
+ }
429
+ respond(id, {});
430
+ return;
431
+ }
432
+
433
+ if (type === 'class:get') {
434
+ var cEl = getEl(payload.eid);
435
+ respond(id, { classes: cEl ? (typeof cEl.className === 'string' ? cEl.className : '') : '' });
436
+ return;
437
+ }
438
+
439
+ if (type === 'class:set') {
440
+ var csEl = getEl(payload.eid);
441
+ if (!csEl) { respond(id, { before: '' }); return; }
442
+ var classBefore = typeof csEl.className === 'string' ? csEl.className : '';
443
+ csEl.className = payload.classes;
444
+ respond(id, { before: classBefore });
445
+ return;
446
+ }
447
+
448
+ if (type === 'class:set-batch') {
449
+ var classBefores = [];
450
+ for (var n = 0; n < payload.eids.length; n++) {
451
+ var cbEl = getEl(payload.eids[n]);
452
+ if (cbEl) {
453
+ classBefores.push(typeof cbEl.className === 'string' ? cbEl.className : '');
454
+ cbEl.className = payload.classes;
455
+ } else {
456
+ classBefores.push('');
457
+ }
458
+ }
459
+ respond(id, { befores: classBefores });
460
+ return;
461
+ }
462
+
463
+ if (type === 'class:undo') {
464
+ var cuEl = getEl(payload.eid);
465
+ if (cuEl) cuEl.className = payload.classes;
466
+ respond(id, {});
467
+ return;
468
+ }
469
+
470
+ // \u2500\u2500 Classification \u2500\u2500
471
+ if (type === 'classify:element') {
472
+ var clEl = getEl(payload.eid);
473
+ if (!clEl) { respond(id, null); return; }
474
+ var tag = clEl.tagName.toLowerCase();
475
+ var clCs = window.getComputedStyle(clEl);
476
+ var display = clCs.display;
477
+ var childCount = clEl.children.length;
478
+ var isFlex = display.includes('flex');
479
+ var isGrid = display.includes('grid');
480
+ var semanticContainers = ['section','main','aside','nav','header','footer','article'];
481
+ var role = 'content';
482
+ if (clEl.hasAttribute('data-annotask-component')) {
483
+ var comp = clEl.getAttribute('data-annotask-component') || '';
484
+ if (comp && comp[0] === comp[0].toUpperCase() && comp[0] !== comp[0].toLowerCase()) {
485
+ role = 'component';
486
+ }
487
+ }
488
+ if (role !== 'component' && (isFlex || isGrid) && childCount > 0) role = 'container';
489
+ if (role !== 'component' && role !== 'container' && semanticContainers.indexOf(tag) >= 0 && childCount > 0) role = 'container';
490
+
491
+ respond(id, {
492
+ role: role,
493
+ display: display,
494
+ isFlexContainer: isFlex,
495
+ isGridContainer: isGrid,
496
+ flexDirection: isFlex ? clCs.flexDirection : undefined,
497
+ childCount: childCount,
498
+ isComponentUnit: role === 'component'
499
+ });
500
+ return;
501
+ }
502
+
503
+ if (type === 'resolve:element-context') {
504
+ var ctxEl = getEl(payload.eid);
505
+ if (!ctxEl) { respond(id, null); return; }
506
+
507
+ // Ancestors: walk up 3 levels, capture layout-relevant styles
508
+ var ancestors = [];
509
+ var cur = ctxEl.parentElement;
510
+ for (var ai = 0; ai < 3 && cur && cur !== document.body && cur !== document.documentElement; ai++) {
511
+ var aCs = window.getComputedStyle(cur);
512
+ var aTag = cur.tagName.toLowerCase();
513
+ var aClasses = typeof cur.className === 'string' ? cur.className.trim() : '';
514
+ var aData = getSourceData(cur);
515
+ var ancestor = { tag: aTag, display: aCs.display };
516
+ if (aClasses) ancestor.classes = aClasses;
517
+ if (aData.component) ancestor.component = aData.component;
518
+ if (aCs.display.includes('flex')) {
519
+ ancestor.flexDirection = aCs.flexDirection;
520
+ if (aCs.gap && aCs.gap !== 'normal') ancestor.gap = aCs.gap;
521
+ ancestor.childCount = cur.children.length;
522
+ }
523
+ if (aCs.display.includes('grid')) {
524
+ ancestor.gridTemplateColumns = aCs.gridTemplateColumns;
525
+ ancestor.gridTemplateRows = aCs.gridTemplateRows;
526
+ if (aCs.gap && aCs.gap !== 'normal') ancestor.gap = aCs.gap;
527
+ ancestor.childCount = cur.children.length;
528
+ }
529
+ if (aCs.overflow && aCs.overflow !== 'visible') ancestor.overflow = aCs.overflow;
530
+ ancestors.push(ancestor);
531
+ cur = cur.parentElement;
532
+ }
533
+
534
+ // Subtree: simplified HTML of the element and its children (3 levels deep)
535
+ function describeEl(el, depth) {
536
+ var t = el.tagName.toLowerCase();
537
+ var cl = typeof el.className === 'string' ? el.className.trim() : '';
538
+ var txt = '';
539
+ // Only get direct text (not from children)
540
+ for (var ci = 0; ci < el.childNodes.length; ci++) {
541
+ if (el.childNodes[ci].nodeType === 3) {
542
+ var nt = el.childNodes[ci].textContent.trim();
543
+ if (nt) { txt = nt.substring(0, 40); break; }
544
+ }
545
+ }
546
+ var node = { tag: t };
547
+ if (cl) node.classes = cl;
548
+ if (txt) node.text = txt;
549
+ if (depth < 3 && el.children.length > 0) {
550
+ var kids = [];
551
+ for (var ki = 0; ki < el.children.length && ki < 10; ki++) {
552
+ kids.push(describeEl(el.children[ki], depth + 1));
553
+ }
554
+ node.children = kids;
555
+ if (el.children.length > 10) node.childrenTruncated = el.children.length;
556
+ }
557
+ return node;
558
+ }
559
+
560
+ respond(id, {
561
+ ancestors: ancestors,
562
+ subtree: describeEl(ctxEl, 0)
563
+ });
564
+ return;
565
+ }
566
+
567
+ // \u2500\u2500 Accessibility Scan \u2500\u2500
568
+ if (type === 'a11y:scan') {
569
+ var a11yEid = payload.eid;
570
+ var a11yEl = a11yEid ? getEl(a11yEid) : document.body;
571
+ if (!a11yEl) { respond(id, { violations: [] }); return; }
572
+
573
+ function runAxeScan(target) {
574
+ if (!window.axe) { respond(id, { violations: [], error: 'axe not loaded' }); return; }
575
+ window.axe.run(target, { resultTypes: ['violations'] }, function(err, results) {
576
+ if (err) { respond(id, { violations: [], error: err.message }); return; }
577
+ var violations = [];
578
+ for (var vi = 0; vi < results.violations.length; vi++) {
579
+ var v = results.violations[vi];
580
+ var nodeDetails = [];
581
+ for (var ni = 0; ni < v.nodes.length && ni < 5; ni++) {
582
+ var node = v.nodes[ni];
583
+ var detail = {
584
+ html: (node.html || '').substring(0, 200),
585
+ target: node.target ? node.target[0] : '',
586
+ failureSummary: node.failureSummary || ''
587
+ };
588
+ // Try to resolve annotask source for this element
589
+ var selector = node.target ? node.target[0] : null;
590
+ if (selector) {
591
+ try {
592
+ var domEl = document.querySelector(selector);
593
+ if (domEl) {
594
+ var src = findSourceElement(domEl);
595
+ var srcData = getSourceData(src.sourceEl);
596
+ if (srcData.file) {
597
+ detail.file = srcData.file;
598
+ detail.line = srcData.line;
599
+ detail.component = srcData.component;
600
+ }
601
+ }
602
+ } catch(e) {}
603
+ }
604
+ nodeDetails.push(detail);
605
+ }
606
+ violations.push({
607
+ id: v.id,
608
+ impact: v.impact,
609
+ description: v.description,
610
+ help: v.help,
611
+ helpUrl: v.helpUrl,
612
+ nodes: v.nodes.length,
613
+ elements: nodeDetails
614
+ });
615
+ }
616
+ respond(id, { violations: violations });
617
+ });
618
+ }
619
+
620
+ if (window.axe) {
621
+ runAxeScan(a11yEl);
622
+ } else {
623
+ var script = document.createElement('script');
624
+ script.src = 'https://cdnjs.cloudflare.com/ajax/libs/axe-core/4.10.2/axe.min.js';
625
+ script.onload = function() { runAxeScan(a11yEl); };
626
+ script.onerror = function() { respond(id, { violations: [], error: 'failed to load axe-core' }); };
627
+ document.head.appendChild(script);
628
+ }
629
+ return;
630
+ }
631
+
632
+ // \u2500\u2500 Layout Scan \u2500\u2500
633
+ if (type === 'layout:scan') {
634
+ var layoutResults = [];
635
+ var allEls = document.querySelectorAll('*');
636
+ for (var li = 0; li < allEls.length; li++) {
637
+ var lEl = allEls[li];
638
+ if (lEl.nodeType !== 1) continue;
639
+ var lCs = window.getComputedStyle(lEl);
640
+ var lDisplay = lCs.display;
641
+ if (!lDisplay.includes('flex') && !lDisplay.includes('grid')) continue;
642
+ var lRect = lEl.getBoundingClientRect();
643
+ if (lRect.width < 20 || lRect.height < 20) continue;
644
+ var lIsGrid = lDisplay.includes('grid');
645
+ var entry = {
646
+ eid: getEid(lEl),
647
+ display: lIsGrid ? 'grid' : 'flex',
648
+ direction: lIsGrid ? 'grid' : lCs.flexDirection,
649
+ rect: { x: lRect.x, y: lRect.y, width: lRect.width, height: lRect.height },
650
+ columnGap: parseFloat(lCs.columnGap) || 0,
651
+ rowGap: parseFloat(lCs.rowGap) || 0
652
+ };
653
+ if (lIsGrid) {
654
+ var cols = lCs.gridTemplateColumns;
655
+ var rows = lCs.gridTemplateRows;
656
+ entry.templateColumns = cols;
657
+ entry.templateRows = rows;
658
+ if (cols && cols !== 'none') {
659
+ entry.columns = cols.split(/\\s+/).map(function(s) { return parseFloat(s) || 0; }).filter(function(v) { return v > 0; });
660
+ }
661
+ if (rows && rows !== 'none') {
662
+ entry.rows = rows.split(/\\s+/).map(function(s) { return parseFloat(s) || 0; }).filter(function(v) { return v > 0; });
663
+ }
664
+ }
665
+ layoutResults.push(entry);
666
+ }
667
+ respond(id, { containers: layoutResults });
668
+ return;
669
+ }
670
+
671
+ if (type === 'layout:add-track') {
672
+ var ltEl = getEl(payload.eid);
673
+ if (!ltEl) { respond(id, { property: '', before: '', after: '' }); return; }
674
+ var ltCs = window.getComputedStyle(ltEl);
675
+ var cssProp = payload.axis === 'col' ? 'grid-template-columns' : 'grid-template-rows';
676
+ var ltBefore = ltCs.getPropertyValue(cssProp) || '';
677
+ var ltAfter = ltBefore && ltBefore !== 'none' ? ltBefore + ' 1fr' : '1fr';
678
+ ltEl.style.setProperty(cssProp, ltAfter);
679
+ respond(id, { property: cssProp, before: ltBefore, after: ltAfter });
680
+ return;
681
+ }
682
+
683
+ if (type === 'layout:add-child') {
684
+ var lcEl = getEl(payload.eid);
685
+ if (!lcEl) { respond(id, { childEid: '' }); return; }
686
+ var child = document.createElement('div');
687
+ child.style.minWidth = '60px';
688
+ child.style.minHeight = '40px';
689
+ child.style.border = '2px dashed #a855f7';
690
+ child.style.borderRadius = '4px';
691
+ child.style.background = 'rgba(168,85,247,0.05)';
692
+ child.setAttribute('data-annotask-placeholder', 'true');
693
+ lcEl.appendChild(child);
694
+ respond(id, { childEid: getEid(child) });
695
+ return;
696
+ }
697
+
698
+ // \u2500\u2500 Theme \u2500\u2500
699
+ if (type === 'theme:inject-css') {
700
+ var existing = document.getElementById(payload.styleId);
701
+ if (existing) {
702
+ existing.textContent = payload.css;
703
+ } else {
704
+ var style = document.createElement('style');
705
+ style.id = payload.styleId;
706
+ style.textContent = payload.css;
707
+ document.head.appendChild(style);
708
+ }
709
+ respond(id, {});
710
+ return;
711
+ }
712
+
713
+ if (type === 'theme:remove-css') {
714
+ var toRemove = document.getElementById(payload.styleId);
715
+ if (toRemove) toRemove.remove();
716
+ respond(id, {});
717
+ return;
718
+ }
719
+
720
+ // \u2500\u2500 Color Palette \u2500\u2500
721
+ if (type === 'palette:scan-vars') {
722
+ var swatches = [];
723
+ try {
724
+ var rootStyles = window.getComputedStyle(document.documentElement);
725
+ var sheets = document.styleSheets;
726
+ for (var si = 0; si < sheets.length; si++) {
727
+ try {
728
+ var rules = sheets[si].cssRules;
729
+ for (var ri = 0; ri < rules.length; ri++) {
730
+ var rule = rules[ri];
731
+ if (rule.selectorText === ':root' || rule.selectorText === ':root, :host') {
732
+ for (var pi = 0; pi < rule.style.length; pi++) {
733
+ var prop = rule.style[pi];
734
+ if (prop.startsWith('--')) {
735
+ var val = rootStyles.getPropertyValue(prop).trim();
736
+ if (val && isColor(val)) {
737
+ swatches.push({ name: prop, value: val });
738
+ }
739
+ }
740
+ }
741
+ }
742
+ }
743
+ } catch(e) { /* cross-origin stylesheet */ }
744
+ }
745
+ } catch(e) {}
746
+ respond(id, { swatches: swatches });
747
+ return;
748
+ }
749
+
750
+ // \u2500\u2500 Source Mapping Check \u2500\u2500
751
+ if (type === 'check:source-mapping') {
752
+ respond(id, { hasMapping: !!(document.querySelector('[data-annotask-file]') || document.querySelector('[data-astro-source-file]')) });
753
+ return;
754
+ }
755
+
756
+ // \u2500\u2500 Insert Placeholder \u2500\u2500
757
+ if (type === 'insert:placeholder') {
758
+ var ipTarget = getEl(payload.targetEid);
759
+ if (!ipTarget) { respond(id, { placeholderEid: '' }); return; }
760
+ var ipEl = createPlaceholder(payload);
761
+ var ipRef = ipTarget;
762
+ switch (payload.position) {
763
+ case 'before': ipRef.parentElement && ipRef.parentElement.insertBefore(ipEl, ipRef); break;
764
+ case 'after': ipRef.parentElement && ipRef.parentElement.insertBefore(ipEl, ipRef.nextSibling); break;
765
+ case 'append': ipRef.appendChild(ipEl); break;
766
+ case 'prepend': ipRef.insertBefore(ipEl, ipRef.firstChild); break;
767
+ }
768
+ // Match sibling sizes in flex/grid
769
+ var insertParent = (payload.position === 'append' || payload.position === 'prepend') ? ipRef : ipRef.parentElement;
770
+ if (insertParent) {
771
+ var pCs = window.getComputedStyle(insertParent);
772
+ if (pCs.display.includes('flex') || pCs.display.includes('grid')) {
773
+ if (!ipEl.style.width) {
774
+ var isRow = pCs.flexDirection === 'row' || pCs.flexDirection === 'row-reverse' || pCs.display.includes('grid');
775
+ if (isRow) ipEl.style.flex = '1';
776
+ }
777
+ }
778
+ }
779
+ respond(id, { placeholderEid: getEid(ipEl) });
780
+ return;
781
+ }
782
+
783
+ if (type === 'insert:remove') {
784
+ var irEl = getEl(payload.eid);
785
+ if (irEl) {
786
+ var unmount = irEl.__annotask_unmount;
787
+ if (unmount) unmount();
788
+ irEl.remove();
789
+ }
790
+ respond(id, {});
791
+ return;
792
+ }
793
+
794
+ if (type === 'move:element') {
795
+ var meEl = getEl(payload.eid);
796
+ var meTarget = getEl(payload.targetEid);
797
+ if (meEl && meTarget) {
798
+ switch (payload.position) {
799
+ case 'before': meTarget.parentElement && meTarget.parentElement.insertBefore(meEl, meTarget); break;
800
+ case 'after': meTarget.parentElement && meTarget.parentElement.insertBefore(meEl, meTarget.nextSibling); break;
801
+ case 'append': meTarget.appendChild(meEl); break;
802
+ case 'prepend': meTarget.insertBefore(meEl, meTarget.firstChild); break;
803
+ }
804
+ }
805
+ respond(id, {});
806
+ return;
807
+ }
808
+
809
+ if (type === 'insert:vue-component' || type === 'insert:component') {
810
+ var vcTarget = getEl(payload.targetEid);
811
+ if (!vcTarget) { respond(id, { eid: '', mounted: false }); return; }
812
+ var vcContainer = document.createElement('div');
813
+ vcContainer.setAttribute('data-annotask-placeholder', 'true');
814
+ switch (payload.position) {
815
+ case 'before': vcTarget.parentElement && vcTarget.parentElement.insertBefore(vcContainer, vcTarget); break;
816
+ case 'after': vcTarget.parentElement && vcTarget.parentElement.insertBefore(vcContainer, vcTarget.nextSibling); break;
817
+ case 'append': vcTarget.appendChild(vcContainer); break;
818
+ case 'prepend': vcTarget.insertBefore(vcContainer, vcTarget.firstChild); break;
819
+ }
820
+ var mounted = tryMountComponent(vcContainer, payload.componentName, payload.props);
821
+ respond(id, { eid: getEid(vcContainer), mounted: mounted });
822
+ return;
823
+ }
824
+
825
+ // \u2500\u2500 Get source data for an eid \u2500\u2500
826
+ if (type === 'resolve:source') {
827
+ var rsEl = getEl(payload.eid);
828
+ if (!rsEl) { respond(id, null); return; }
829
+ var rsSrc = findSourceElement(rsEl);
830
+ var rsData = getSourceData(rsSrc.sourceEl);
831
+ respond(id, {
832
+ sourceEid: getEid(rsSrc.sourceEl),
833
+ file: rsData.file,
834
+ line: rsData.line,
835
+ component: rsData.component,
836
+ tag: rsSrc.sourceEl.tagName.toLowerCase()
837
+ });
838
+ return;
839
+ }
840
+
841
+ // \u2500\u2500 Route \u2500\u2500
842
+ if (type === 'route:current') {
843
+ respond(id, { path: window.location.pathname });
844
+ return;
845
+ }
846
+ });
847
+
848
+ // \u2500\u2500 Helpers \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
849
+ function isColor(val) {
850
+ if (val.startsWith('#') || val.startsWith('rgb') || val.startsWith('hsl')) return true;
851
+ // Named colors \u2014 quick check with canvas
852
+ try {
853
+ var ctx = document.createElement('canvas').getContext('2d');
854
+ ctx.fillStyle = '#000000';
855
+ ctx.fillStyle = val;
856
+ return ctx.fillStyle !== '#000000' || val === 'black' || val === '#000000';
857
+ } catch(e) { return false; }
858
+ }
859
+
860
+ function createPlaceholder(payload) {
861
+ var el = document.createElement(payload.tag);
862
+ el.setAttribute('data-annotask-placeholder', 'true');
863
+ if (payload.classes) el.className = payload.classes;
864
+ var tag = payload.tag.toLowerCase();
865
+ var isComponent = payload.tag.includes('-') || (payload.tag[0] === payload.tag[0].toUpperCase() && payload.tag[0] !== payload.tag[0].toLowerCase());
866
+
867
+ if (tag === 'button') {
868
+ el.textContent = payload.textContent || 'Button';
869
+ el.style.cssText = 'padding:8px 16px;border-radius:6px;font-size:14px;font-weight:500;cursor:pointer;border:1px solid currentColor;background:var(--accent,#3b82f6);color:white;';
870
+ } else if (tag === 'input') {
871
+ el.type = 'text'; el.placeholder = 'Input field...';
872
+ el.style.cssText = 'padding:8px 12px;border:1px solid #ccc;border-radius:6px;font-size:14px;width:100%;max-width:300px;background:white;color:#333;';
873
+ } else if (tag === 'textarea') {
874
+ el.placeholder = 'Text area...';
875
+ el.style.cssText = 'padding:8px 12px;border:1px solid #ccc;border-radius:6px;font-size:14px;width:100%;min-height:60px;background:white;color:#333;resize:vertical;';
876
+ } else if (tag === 'img') {
877
+ el.style.cssText = 'width:200px;height:120px;background:#e5e7eb;border-radius:8px;display:flex;align-items:center;justify-content:center;';
878
+ } else if (['h1','h2','h3','h4','h5','h6'].indexOf(tag) >= 0) {
879
+ el.textContent = payload.textContent || 'Heading';
880
+ var sizes = { h1:'2em', h2:'1.5em', h3:'1.25em', h4:'1.1em', h5:'1em', h6:'0.9em' };
881
+ el.style.cssText = 'font-size:' + (sizes[tag]||'1em') + ';font-weight:700;margin:0.5em 0;';
882
+ } else if (tag === 'p') {
883
+ el.textContent = payload.textContent || 'Paragraph text goes here.';
884
+ el.style.cssText = 'margin:0.5em 0;line-height:1.5;';
885
+ } else if (tag === 'a') {
886
+ el.textContent = payload.textContent || 'Link';
887
+ el.style.cssText = 'color:var(--accent,#3b82f6);text-decoration:underline;cursor:pointer;';
888
+ } else if (tag === 'section' || tag === 'div' || tag === 'nav' || tag === 'header' || tag === 'footer' || tag === 'aside' || tag === 'main') {
889
+ if (!payload.classes && !payload.textContent) {
890
+ el.style.cssText = 'min-height:40px;padding:12px;border:1.5px dashed rgba(59,130,246,0.3);border-radius:6px;background:rgba(59,130,246,0.03);';
891
+ } else if (payload.textContent) {
892
+ el.textContent = payload.textContent;
893
+ }
894
+ if (payload.category === 'layout-preset') {
895
+ el.style.minHeight = '60px';
896
+ el.style.padding = el.style.padding || '12px';
897
+ el.style.border = '1.5px dashed rgba(34,197,94,0.3)';
898
+ el.style.borderRadius = '6px';
899
+ el.style.background = 'rgba(34,197,94,0.03)';
900
+ }
901
+ } else if (isComponent) {
902
+ var vcMounted = tryMountComponent(el, payload.tag, payload.defaultProps);
903
+ if (!vcMounted) {
904
+ el.style.cssText = 'min-height:80px;padding:16px;border:1px solid rgba(168,85,247,0.2);border-radius:8px;background:rgba(168,85,247,0.03);display:flex;flex-direction:column;gap:8px;overflow:hidden;';
905
+ var hdr = document.createElement('div');
906
+ hdr.style.cssText = 'display:flex;align-items:center;gap:6px;margin-bottom:4px;';
907
+ var dot = document.createElement('span');
908
+ dot.style.cssText = 'width:6px;height:6px;border-radius:50%;background:#a855f7;';
909
+ hdr.appendChild(dot);
910
+ var tagLabel = document.createElement('span');
911
+ tagLabel.style.cssText = 'font-size:11px;font-weight:600;color:#a855f7;';
912
+ tagLabel.textContent = payload.tag;
913
+ hdr.appendChild(tagLabel);
914
+ el.appendChild(hdr);
915
+ }
916
+ } else {
917
+ el.textContent = payload.textContent || '';
918
+ }
919
+ return el;
920
+ }
921
+
922
+ function tryMountComponent(container, componentName, props) {
923
+ // Try Vue
924
+ if (window.__ANNOTASK_VUE__) {
925
+ var mounted = tryMountVueComponent(container, componentName, props);
926
+ if (mounted) return true;
927
+ }
928
+ // Try React
929
+ if (window.__ANNOTASK_REACT__) {
930
+ var mounted = tryMountReactComponent(container, componentName, props);
931
+ if (mounted) return true;
932
+ }
933
+ // Try Svelte
934
+ if (window.__ANNOTASK_SVELTE__) {
935
+ var mounted = tryMountSvelteComponent(container, componentName, props);
936
+ if (mounted) return true;
937
+ }
938
+ return false;
939
+ }
940
+
941
+ function tryMountVueComponent(container, componentName, props) {
942
+ try {
943
+ var appEl = document.querySelector('#app');
944
+ var vueApp = appEl && appEl.__vue_app__;
945
+ if (!vueApp) return false;
946
+ var annotaskVue = window.__ANNOTASK_VUE__;
947
+ if (!annotaskVue || !annotaskVue.createApp || !annotaskVue.h) return false;
948
+ var component = vueApp._context.components[componentName] || (window.__ANNOTASK_COMPONENTS__ && window.__ANNOTASK_COMPONENTS__[componentName]);
949
+ if (!component) return false;
950
+ var mountPoint = document.createElement('div');
951
+ container.appendChild(mountPoint);
952
+ var miniApp = annotaskVue.createApp({
953
+ render: function() { return annotaskVue.h(component, props || {}); }
954
+ });
955
+ miniApp._context = vueApp._context;
956
+ miniApp.mount(mountPoint);
957
+ container.setAttribute('data-annotask-mounted', 'true');
958
+ container.__annotask_unmount = function() { try { miniApp.unmount(); } catch(e) {} };
959
+ return true;
960
+ } catch(e) { return false; }
961
+ }
962
+
963
+ function tryMountReactComponent(container, componentName, props) {
964
+ try {
965
+ var annotaskReact = window.__ANNOTASK_REACT__;
966
+ if (!annotaskReact || !annotaskReact.createElement || !annotaskReact.createRoot) return false;
967
+ var component = window.__ANNOTASK_COMPONENTS__ && window.__ANNOTASK_COMPONENTS__[componentName];
968
+ if (!component) return false;
969
+ var mountPoint = document.createElement('div');
970
+ container.appendChild(mountPoint);
971
+ var root = annotaskReact.createRoot(mountPoint);
972
+ root.render(annotaskReact.createElement(component, props || {}));
973
+ container.setAttribute('data-annotask-mounted', 'true');
974
+ container.__annotask_unmount = function() { try { root.unmount(); } catch(e) {} };
975
+ return true;
976
+ } catch(e) { return false; }
977
+ }
978
+
979
+ function tryMountSvelteComponent(container, componentName, props) {
980
+ try {
981
+ var annotaskSvelte = window.__ANNOTASK_SVELTE__;
982
+ if (!annotaskSvelte || !annotaskSvelte.mount) return false;
983
+ var component = window.__ANNOTASK_COMPONENTS__ && window.__ANNOTASK_COMPONENTS__[componentName];
984
+ if (!component) return false;
985
+ var mountPoint = document.createElement('div');
986
+ container.appendChild(mountPoint);
987
+ var instance = annotaskSvelte.mount(component, { target: mountPoint, props: props || {} });
988
+ container.setAttribute('data-annotask-mounted', 'true');
989
+ container.__annotask_unmount = function() {
990
+ try {
991
+ if (annotaskSvelte.unmount) annotaskSvelte.unmount(instance);
992
+ } catch(e) {}
993
+ };
994
+ return true;
995
+ } catch(e) { return false; }
996
+ }
997
+
998
+ // \u2500\u2500 Ready \u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500
999
+ sendToShell('bridge:ready', { version: '1.0' });
1000
+ })();
1001
+ `.trim();
1002
+ }
1003
+
1004
+ export {
1005
+ bridgeClientScript
1006
+ };
1007
+ //# sourceMappingURL=chunk-O73PGHAA.js.map