vite-plugin-specter 0.3.2 → 0.3.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.
- package/dist/client.js +11 -14
- package/dist/extension-chrome/background.js +4 -0
- package/dist/extension-chrome/content.js +982 -0
- package/dist/extension-chrome/icons/icon128.png +0 -0
- package/dist/extension-chrome/icons/icon16.png +0 -0
- package/dist/extension-chrome/icons/icon32.png +0 -0
- package/dist/extension-chrome/icons/icon48.png +0 -0
- package/dist/extension-chrome/manifest.json +38 -0
- package/dist/extension-firefox/content.js +11 -14
- package/dist/extension-firefox/manifest.json +8 -2
- package/dist/index.cjs +11 -14
- package/dist/index.js +11 -14
- package/extension/background.js +1 -5
- package/extension/content.js +11 -14
- package/extension/manifest.firefox.json +21 -7
- package/extension/manifest.json +8 -6
- package/package.json +1 -1
|
@@ -0,0 +1,982 @@
|
|
|
1
|
+
(function() {
|
|
2
|
+
'use strict';
|
|
3
|
+
if (window.__specter) return; window.__specter = true;
|
|
4
|
+
|
|
5
|
+
// ─── Constants ──────────────────────────────────────────────────────────────
|
|
6
|
+
var PURPLE = '#AD24D3';
|
|
7
|
+
var RED = '#ED3E61';
|
|
8
|
+
var TIP_BG = '#292B32';
|
|
9
|
+
var GREEN = '#22C55E';
|
|
10
|
+
var LABEL = '#8B8D94';
|
|
11
|
+
var MONO = "'JetBrains Mono', 'SF Mono', 'Fira Code', monospace";
|
|
12
|
+
var ZAP = '<svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2.5" stroke-linecap="round" stroke-linejoin="round"><polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2"></polygon></svg>';
|
|
13
|
+
var ACTIVATE = "ctrl+alt+z";
|
|
14
|
+
|
|
15
|
+
// ─── State ────────────────────────────────────────────────────────────────
|
|
16
|
+
var fiActive = false;
|
|
17
|
+
var measureMode = false;
|
|
18
|
+
var pinEl = null;
|
|
19
|
+
var pinHighlight = null;
|
|
20
|
+
var lastHovered = null;
|
|
21
|
+
var optionHeld = false;
|
|
22
|
+
var pillExpanded = false;
|
|
23
|
+
var sideRight = false;
|
|
24
|
+
var outlinedEl = null;
|
|
25
|
+
var prevOutline = '';
|
|
26
|
+
var measureHL = null;
|
|
27
|
+
var copiedTimer = null;
|
|
28
|
+
var flashTimer = null;
|
|
29
|
+
var lastMouse = { x: 0, y: 0 };
|
|
30
|
+
var selectedEls = [];
|
|
31
|
+
var selectedBadges = [];
|
|
32
|
+
|
|
33
|
+
// ─── Tooltip ──────────────────────────────────────────────────────────────
|
|
34
|
+
var tooltip = document.createElement('div');
|
|
35
|
+
Object.assign(tooltip.style, {
|
|
36
|
+
position: 'fixed',
|
|
37
|
+
zIndex: '2147483646',
|
|
38
|
+
pointerEvents: 'none',
|
|
39
|
+
fontFamily: MONO,
|
|
40
|
+
fontSize: '13px',
|
|
41
|
+
lineHeight: '20px',
|
|
42
|
+
background: TIP_BG,
|
|
43
|
+
color: '#FFFFFF',
|
|
44
|
+
padding: '24px',
|
|
45
|
+
borderRadius: '8px',
|
|
46
|
+
maxWidth: '480px',
|
|
47
|
+
boxShadow: '0 4px 16px rgba(0,0,0,0.3)',
|
|
48
|
+
display: 'none',
|
|
49
|
+
});
|
|
50
|
+
document.body.appendChild(tooltip);
|
|
51
|
+
|
|
52
|
+
// Measure overlay
|
|
53
|
+
var measureOverlay = document.createElement('div');
|
|
54
|
+
Object.assign(measureOverlay.style, {
|
|
55
|
+
position: 'fixed',
|
|
56
|
+
inset: '0',
|
|
57
|
+
zIndex: '2147483645',
|
|
58
|
+
pointerEvents: 'none',
|
|
59
|
+
display: 'none',
|
|
60
|
+
});
|
|
61
|
+
document.body.appendChild(measureOverlay);
|
|
62
|
+
|
|
63
|
+
// ─── Pill ─────────────────────────────────────────────────────────────────
|
|
64
|
+
var pillWrap = document.createElement('div');
|
|
65
|
+
Object.assign(pillWrap.style, {
|
|
66
|
+
position: 'fixed',
|
|
67
|
+
bottom: '4px',
|
|
68
|
+
left: '4px',
|
|
69
|
+
zIndex: '2147483647',
|
|
70
|
+
padding: '12px',
|
|
71
|
+
display: 'none',
|
|
72
|
+
boxSizing: 'border-box',
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
var pill = document.createElement('div');
|
|
76
|
+
Object.assign(pill.style, {
|
|
77
|
+
display: 'flex',
|
|
78
|
+
alignItems: 'center',
|
|
79
|
+
gap: '8px',
|
|
80
|
+
background: PURPLE,
|
|
81
|
+
borderRadius: '16px',
|
|
82
|
+
height: '32px',
|
|
83
|
+
padding: '0 9px',
|
|
84
|
+
fontFamily: MONO,
|
|
85
|
+
fontSize: '11px',
|
|
86
|
+
fontWeight: '400',
|
|
87
|
+
color: '#fff',
|
|
88
|
+
cursor: 'default',
|
|
89
|
+
userSelect: 'none',
|
|
90
|
+
boxShadow: '0 4px 12px rgba(173,36,211,0.4)',
|
|
91
|
+
overflow: 'hidden',
|
|
92
|
+
maxWidth: '32px',
|
|
93
|
+
transition: 'max-width 0.2s ease',
|
|
94
|
+
boxSizing: 'border-box',
|
|
95
|
+
whiteSpace: 'nowrap',
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
var iconBtn = document.createElement('div');
|
|
99
|
+
Object.assign(iconBtn.style, {
|
|
100
|
+
position: 'relative',
|
|
101
|
+
width: '14px',
|
|
102
|
+
height: '14px',
|
|
103
|
+
flexShrink: '0',
|
|
104
|
+
display: 'flex',
|
|
105
|
+
alignItems: 'center',
|
|
106
|
+
justifyContent: 'center',
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
var zapEl = document.createElement('span');
|
|
110
|
+
zapEl.innerHTML = ZAP;
|
|
111
|
+
Object.assign(zapEl.style, {
|
|
112
|
+
position: 'absolute',
|
|
113
|
+
display: 'flex',
|
|
114
|
+
transition: 'transform 0.4s ease, opacity 0.2s ease',
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
var closeEl = document.createElement('span');
|
|
118
|
+
closeEl.textContent = '×';
|
|
119
|
+
Object.assign(closeEl.style, {
|
|
120
|
+
position: 'absolute',
|
|
121
|
+
fontSize: '17px',
|
|
122
|
+
lineHeight: '1',
|
|
123
|
+
opacity: '0',
|
|
124
|
+
transition: 'opacity 0.2s ease',
|
|
125
|
+
cursor: 'pointer',
|
|
126
|
+
});
|
|
127
|
+
closeEl.addEventListener('click', function (e) { e.stopPropagation(); deactivate(); });
|
|
128
|
+
|
|
129
|
+
iconBtn.appendChild(zapEl);
|
|
130
|
+
iconBtn.appendChild(closeEl);
|
|
131
|
+
|
|
132
|
+
var pillText = document.createElement('span');
|
|
133
|
+
Object.assign(pillText.style, { display: 'none', color: '#f3d9fb' });
|
|
134
|
+
|
|
135
|
+
var chevron = document.createElement('span');
|
|
136
|
+
chevron.textContent = '›';
|
|
137
|
+
chevron.title = 'Move to other side';
|
|
138
|
+
Object.assign(chevron.style, {
|
|
139
|
+
display: 'none',
|
|
140
|
+
cursor: 'pointer',
|
|
141
|
+
color: '#fff',
|
|
142
|
+
fontSize: '14px',
|
|
143
|
+
flexShrink: '0',
|
|
144
|
+
padding: '0 2px',
|
|
145
|
+
opacity: '0.8',
|
|
146
|
+
});
|
|
147
|
+
chevron.addEventListener('click', function (e) { e.stopPropagation(); moveSide(); });
|
|
148
|
+
|
|
149
|
+
pill.appendChild(iconBtn);
|
|
150
|
+
pill.appendChild(pillText);
|
|
151
|
+
pill.appendChild(chevron);
|
|
152
|
+
pillWrap.appendChild(pill);
|
|
153
|
+
document.body.appendChild(pillWrap);
|
|
154
|
+
|
|
155
|
+
pillWrap.addEventListener('mouseenter', function () {
|
|
156
|
+
clearMeasureOverlay();
|
|
157
|
+
clearMeasureTargetHL();
|
|
158
|
+
hideTooltip();
|
|
159
|
+
expandPill();
|
|
160
|
+
zapEl.style.transform = 'rotate(360deg)';
|
|
161
|
+
zapEl.style.opacity = '0';
|
|
162
|
+
closeEl.style.opacity = '1';
|
|
163
|
+
});
|
|
164
|
+
pillWrap.addEventListener('mouseleave', function () {
|
|
165
|
+
if (selectedEls.length === 0 && !pinEl) collapsePill();
|
|
166
|
+
zapEl.style.transform = 'rotate(0deg)';
|
|
167
|
+
zapEl.style.opacity = '1';
|
|
168
|
+
closeEl.style.opacity = '0';
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
function moveSide() {
|
|
172
|
+
sideRight = !sideRight;
|
|
173
|
+
if (sideRight) {
|
|
174
|
+
pillWrap.style.left = 'auto';
|
|
175
|
+
pillWrap.style.right = '4px';
|
|
176
|
+
chevron.textContent = '‹';
|
|
177
|
+
} else {
|
|
178
|
+
pillWrap.style.right = 'auto';
|
|
179
|
+
pillWrap.style.left = '4px';
|
|
180
|
+
chevron.textContent = '›';
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
function expandPill(text) {
|
|
185
|
+
pill.style.maxWidth = '760px';
|
|
186
|
+
pillText.style.display = 'inline';
|
|
187
|
+
chevron.style.display = 'inline';
|
|
188
|
+
pillExpanded = true;
|
|
189
|
+
if (text) { pillText.textContent = text; return; }
|
|
190
|
+
if (selectedEls.length > 0) {
|
|
191
|
+
pillText.textContent = selectedEls.length + ' selected · Cmd+C copy all · Esc clear';
|
|
192
|
+
} else if (measureMode) {
|
|
193
|
+
pillText.textContent = 'Measure · hover distances · M pin · Cmd+C copy · Option toggle';
|
|
194
|
+
} else {
|
|
195
|
+
pillText.textContent = 'Properties · hover inspect · P pick · Cmd+C copy · Option measure';
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function collapsePill() {
|
|
200
|
+
pill.style.maxWidth = '32px';
|
|
201
|
+
pillText.style.display = 'none';
|
|
202
|
+
chevron.style.display = 'none';
|
|
203
|
+
pillExpanded = false;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function flashMode() {
|
|
207
|
+
if (pillExpanded && pillWrap.matches(':hover')) return;
|
|
208
|
+
var text = measureMode ? '⬡ Measure mode' : '◉ Properties mode';
|
|
209
|
+
expandPill(text);
|
|
210
|
+
clearTimeout(flashTimer);
|
|
211
|
+
flashTimer = setTimeout(function () {
|
|
212
|
+
if (!pillWrap.matches(':hover') && selectedEls.length === 0 && !pinEl) collapsePill();
|
|
213
|
+
else expandPill();
|
|
214
|
+
}, 1200);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// ─── Activate / Deactivate ────────────────────────────────────────────────
|
|
218
|
+
function activate() {
|
|
219
|
+
fiActive = true;
|
|
220
|
+
measureMode = false;
|
|
221
|
+
pillWrap.style.display = 'block';
|
|
222
|
+
document.body.style.cursor = 'crosshair';
|
|
223
|
+
collapsePill();
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function deactivate() {
|
|
227
|
+
fiActive = false;
|
|
228
|
+
measureMode = false;
|
|
229
|
+
optionHeld = false;
|
|
230
|
+
lastHovered = null;
|
|
231
|
+
clearSelection();
|
|
232
|
+
clearPin();
|
|
233
|
+
clearHoverOutline();
|
|
234
|
+
clearMeasureTargetHL();
|
|
235
|
+
pillWrap.style.display = 'none';
|
|
236
|
+
document.body.style.cursor = '';
|
|
237
|
+
hideTooltip();
|
|
238
|
+
clearMeasureOverlay();
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
// ─── Helpers ──────────────────────────────────────────────────────────────
|
|
242
|
+
function hideTooltip() { tooltip.style.display = 'none'; }
|
|
243
|
+
|
|
244
|
+
function esc(s) {
|
|
245
|
+
return String(s).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function positionTooltip(x, y) {
|
|
249
|
+
tooltip.style.display = 'block';
|
|
250
|
+
var tw = tooltip.offsetWidth;
|
|
251
|
+
var th = tooltip.offsetHeight;
|
|
252
|
+
var vw = window.innerWidth;
|
|
253
|
+
var vh = window.innerHeight;
|
|
254
|
+
var margin = 16;
|
|
255
|
+
var left = x + 16;
|
|
256
|
+
var top = y + 16;
|
|
257
|
+
if (left + tw > vw - margin) left = x - tw - 16;
|
|
258
|
+
if (top + th > vh - margin) top = y - th - 16;
|
|
259
|
+
if (left < margin) left = margin;
|
|
260
|
+
if (top < margin) top = margin;
|
|
261
|
+
tooltip.style.left = left + 'px';
|
|
262
|
+
tooltip.style.top = top + 'px';
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
function toHex(r, g, b) {
|
|
266
|
+
return '#' + [r, g, b].map(function (v) { return Math.round(v).toString(16).padStart(2, '0'); }).join('');
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
function parseColor(str) {
|
|
270
|
+
if (!str || str === 'transparent' || str === 'rgba(0, 0, 0, 0)') return null;
|
|
271
|
+
var m = str.match(/rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*([\d.]+))?\)/);
|
|
272
|
+
if (!m) return null;
|
|
273
|
+
return { hex: toHex(m[1], m[2], m[3]), alpha: m[4] !== undefined ? parseFloat(m[4]) : 1 };
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
function colorObj(str) {
|
|
277
|
+
var c = parseColor(str);
|
|
278
|
+
if (!c) return null;
|
|
279
|
+
var label = c.alpha < 1 ? c.hex + ' (' + Math.round(c.alpha * 100) + '% opacity)' : c.hex;
|
|
280
|
+
return { raw: str, hex: c.hex, alpha: c.alpha, label: label };
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function edge(t, r, b, l) {
|
|
284
|
+
if ([t, r, b, l].every(function (v) { return v === '0px'; })) return null;
|
|
285
|
+
return { value: t + ' ' + r + ' ' + b + ' ' + l };
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
function weightName(w) {
|
|
289
|
+
var m = { '100': 'Thin', '200': 'ExtraLight', '300': 'Light', '400': 'Regular', '500': 'Medium', '600': 'Semibold', '700': 'Bold', '800': 'ExtraBold', '900': 'Black', 'normal': 'Regular', 'bold': 'Bold' };
|
|
290
|
+
return m[w] || w;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
function swatch(hex) {
|
|
294
|
+
return '<span style="display:inline-block;width:9px;height:9px;border-radius:2px;background:' + hex + ';margin-right:6px;vertical-align:middle;border:1px solid rgba(255,255,255,0.2)"></span>';
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
function getComponentName(el) {
|
|
298
|
+
for (var key in el) {
|
|
299
|
+
if (key.startsWith('__reactFiber$') || key.startsWith('__reactInternalInstance$')) {
|
|
300
|
+
var fiber = el[key];
|
|
301
|
+
while (fiber) {
|
|
302
|
+
var name = (fiber.type && (fiber.type.displayName || fiber.type.name));
|
|
303
|
+
if (name && name.length > 3 && /^[A-Z]/.test(name)) return name;
|
|
304
|
+
fiber = fiber.return;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
if (el.__vueParentComponent) {
|
|
309
|
+
var vname = el.__vueParentComponent.type && (el.__vueParentComponent.type.name || el.__vueParentComponent.type.__name);
|
|
310
|
+
if (vname && vname.length > 3) return vname;
|
|
311
|
+
}
|
|
312
|
+
return null;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
function getMatchingRules(el) {
|
|
316
|
+
var results = [];
|
|
317
|
+
try {
|
|
318
|
+
for (var s = 0; s < document.styleSheets.length; s++) {
|
|
319
|
+
var sheet = document.styleSheets[s];
|
|
320
|
+
var rules;
|
|
321
|
+
try { rules = sheet.cssRules; } catch (e) { continue; }
|
|
322
|
+
if (!rules) continue;
|
|
323
|
+
for (var r = 0; r < rules.length; r++) {
|
|
324
|
+
var rule = rules[r];
|
|
325
|
+
if (rule.type !== 1) continue;
|
|
326
|
+
var sel = rule.selectorText;
|
|
327
|
+
if (!sel) continue;
|
|
328
|
+
if (/^[*,]|^:root|^html|^body$|^::before|^::after/.test(sel.trim())) continue;
|
|
329
|
+
try {
|
|
330
|
+
if (el.matches(sel)) {
|
|
331
|
+
var props = [];
|
|
332
|
+
for (var i = 0; i < rule.style.length; i++) {
|
|
333
|
+
var prop = rule.style[i];
|
|
334
|
+
var val = rule.style.getPropertyValue(prop);
|
|
335
|
+
if (val) props.push(' ' + prop + ': ' + val + ';');
|
|
336
|
+
}
|
|
337
|
+
if (props.length) results.push(sel + ' {\n' + props.join('\n') + '\n}');
|
|
338
|
+
}
|
|
339
|
+
} catch (e) {}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
} catch (e) {}
|
|
343
|
+
return results;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function getSelector(el) {
|
|
347
|
+
var parts = [];
|
|
348
|
+
var cur = el;
|
|
349
|
+
for (var i = 0; i < 3 && cur && cur !== document.body; i++) {
|
|
350
|
+
var part = cur.tagName.toLowerCase();
|
|
351
|
+
if (cur.id) { part += '#' + cur.id; parts.unshift(part); break; }
|
|
352
|
+
var cls = Array.prototype.slice.call(cur.classList).filter(function (c) { return c.indexOf('__specter') !== 0; });
|
|
353
|
+
if (cls.length) {
|
|
354
|
+
part += '.' + cls.slice(0, 2).join('.');
|
|
355
|
+
} else if (cur.parentElement) {
|
|
356
|
+
var sameTag = Array.prototype.slice.call(cur.parentElement.children).filter(function (c) { return c.tagName === cur.tagName; });
|
|
357
|
+
if (sameTag.length > 1) {
|
|
358
|
+
part += ':nth-child(' + (Array.prototype.indexOf.call(cur.parentElement.children, cur) + 1) + ')';
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
parts.unshift(part);
|
|
362
|
+
cur = cur.parentElement;
|
|
363
|
+
}
|
|
364
|
+
return parts.join(' > ');
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// ─── Structured data model ──────────────────────────────────────────────────
|
|
368
|
+
function buildInfo(el) {
|
|
369
|
+
var cs = getComputedStyle(el);
|
|
370
|
+
var rect = el.getBoundingClientRect();
|
|
371
|
+
var ff = cs.fontFamily.split(',')[0].replace(/['"]/g, '').trim();
|
|
372
|
+
var raw = el.textContent ? el.textContent.trim() : '';
|
|
373
|
+
return {
|
|
374
|
+
el: el,
|
|
375
|
+
tag: el.tagName.toLowerCase(),
|
|
376
|
+
id: el.id || null,
|
|
377
|
+
classes: Array.prototype.slice.call(el.classList).filter(function (c) { return c.indexOf('__specter') !== 0; }),
|
|
378
|
+
component: getComponentName(el),
|
|
379
|
+
styleKey: el.dataset ? el.dataset.style : null,
|
|
380
|
+
width: Math.round(rect.width),
|
|
381
|
+
height: Math.round(rect.height),
|
|
382
|
+
text: raw.slice(0, 40),
|
|
383
|
+
textTrunc: raw.length > 40,
|
|
384
|
+
font: { family: ff, weight: cs.fontWeight, size: cs.fontSize, lineHeight: cs.lineHeight },
|
|
385
|
+
color: colorObj(cs.color),
|
|
386
|
+
bg: colorObj(cs.backgroundColor),
|
|
387
|
+
padding: edge(cs.paddingTop, cs.paddingRight, cs.paddingBottom, cs.paddingLeft),
|
|
388
|
+
margin: edge(cs.marginTop, cs.marginRight, cs.marginBottom, cs.marginLeft),
|
|
389
|
+
radius: (cs.borderRadius && cs.borderRadius !== '0px') ? cs.borderRadius : null,
|
|
390
|
+
display: ['flex', 'grid', 'inline-flex', 'inline-grid'].indexOf(cs.display) >= 0 ? cs.display : null,
|
|
391
|
+
gap: (cs.gap && cs.gap !== 'normal') ? cs.gap : null,
|
|
392
|
+
flexDir: (cs.display.indexOf('flex') >= 0 && cs.flexDirection !== 'row') ? cs.flexDirection : null,
|
|
393
|
+
rules: getMatchingRules(el),
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function row(label, val) {
|
|
398
|
+
return '<div style="display:flex;margin-bottom:8px"><span style="color:' + LABEL + ';min-width:76px;flex-shrink:0">' + label + '</span><span style="color:#fff;word-break:break-word">' + val + '</span></div>';
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
function buildHumanDisplay(data) {
|
|
402
|
+
var h = '';
|
|
403
|
+
var id = '<span style="color:#fff"><' + data.tag + '></span>';
|
|
404
|
+
if (data.id) id += '<span style="color:#F0B86E">#' + esc(data.id) + '</span>';
|
|
405
|
+
if (data.classes.length) id += '<span style="color:#5FD3C0">.' + data.classes.map(esc).join('.') + '</span>';
|
|
406
|
+
if (data.component) id += ' <span style="color:#E0A3F5;font-weight:600">' + esc(data.component) + '</span>';
|
|
407
|
+
if (data.styleKey) id += ' <span style="color:' + LABEL + '">[' + esc(data.styleKey) + ']</span>';
|
|
408
|
+
id += ' <span style="color:' + LABEL + '">' + data.width + '×' + data.height + '</span>';
|
|
409
|
+
h += '<div style="margin-bottom:8px">' + id + '</div>';
|
|
410
|
+
if (data.text) h += '<div style="color:' + LABEL + ';font-style:italic;margin-bottom:8px">"' + esc(data.text) + (data.textTrunc ? '…' : '') + '"</div>';
|
|
411
|
+
h += '<div style="height:8px"></div>';
|
|
412
|
+
h += row('Font', esc(data.font.family) + ' · ' + weightName(data.font.weight) + ' · ' + data.font.size + '/' + data.font.lineHeight);
|
|
413
|
+
if (data.color) h += row('Color', swatch(data.color.hex) + data.color.label);
|
|
414
|
+
if (data.bg) h += row('Bg', swatch(data.bg.hex) + data.bg.label);
|
|
415
|
+
if (data.padding) h += row('Padding', data.padding.value);
|
|
416
|
+
if (data.margin) h += row('Margin', data.margin.value);
|
|
417
|
+
if (data.radius) h += row('Radius', data.radius);
|
|
418
|
+
if (data.display) {
|
|
419
|
+
var d = data.display;
|
|
420
|
+
if (data.gap) d += ' · gap ' + data.gap;
|
|
421
|
+
if (data.flexDir) d += ' · ' + data.flexDir;
|
|
422
|
+
h += row('Display', d);
|
|
423
|
+
}
|
|
424
|
+
return h;
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function buildLLMClipboard(data) {
|
|
428
|
+
var lines = ['[Specter]'];
|
|
429
|
+
var head = '<' + data.tag + '>';
|
|
430
|
+
if (data.id) head += '#' + data.id;
|
|
431
|
+
if (data.classes.length) head += '.' + data.classes.join('.');
|
|
432
|
+
if (data.component) head += ' ' + data.component;
|
|
433
|
+
if (data.styleKey) head += ' [' + data.styleKey + ']';
|
|
434
|
+
head += ' ' + data.width + '×' + data.height;
|
|
435
|
+
lines.push(head);
|
|
436
|
+
if (data.text) lines.push('"' + data.text + (data.textTrunc ? '…' : '') + '"');
|
|
437
|
+
lines.push('font: ' + data.font.family + ' ' + data.font.weight + ' ' + data.font.size + '/' + data.font.lineHeight);
|
|
438
|
+
if (data.color) lines.push('color: ' + data.color.label);
|
|
439
|
+
if (data.bg) lines.push('bg: ' + data.bg.label);
|
|
440
|
+
if (data.padding) lines.push('padding: ' + data.padding.value);
|
|
441
|
+
if (data.margin) lines.push('margin: ' + data.margin.value);
|
|
442
|
+
if (data.radius) lines.push('radius: ' + data.radius);
|
|
443
|
+
if (data.display) {
|
|
444
|
+
var d = 'display: ' + data.display;
|
|
445
|
+
if (data.gap) d += ' gap: ' + data.gap;
|
|
446
|
+
if (data.flexDir) d += ' dir: ' + data.flexDir;
|
|
447
|
+
lines.push(d);
|
|
448
|
+
}
|
|
449
|
+
lines.push('selector: ' + getSelector(data.el));
|
|
450
|
+
var out = lines.join('\n');
|
|
451
|
+
if (data.rules && data.rules.length) out += '\n\n--- CSS Rules ---\n' + data.rules.join('\n\n');
|
|
452
|
+
return out;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
function buildMultiSelectCopyText() {
|
|
456
|
+
return selectedEls.map(function (el, i) {
|
|
457
|
+
var body = buildLLMClipboard(buildInfo(el));
|
|
458
|
+
return body.replace('[Specter]', '[Specter ' + (i + 1) + '/' + selectedEls.length + ']');
|
|
459
|
+
}).join('\n\n' + Array(41).join('─') + '\n\n');
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
function linesToHTML(str) {
|
|
463
|
+
return str.split('\n').map(function (l) {
|
|
464
|
+
return '<div style="margin-bottom:4px;color:#fff">' + esc(l) + '</div>';
|
|
465
|
+
}).join('');
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// ─── Hover outline ──────────────────────────────────────────────────────────
|
|
469
|
+
function setHoverOutline(el) {
|
|
470
|
+
if (outlinedEl === el) return;
|
|
471
|
+
clearHoverOutline();
|
|
472
|
+
if (!el) return;
|
|
473
|
+
outlinedEl = el;
|
|
474
|
+
prevOutline = el.style.outline;
|
|
475
|
+
el.style.outline = '2px solid ' + PURPLE;
|
|
476
|
+
el.style.outlineOffset = '-1px';
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
function clearHoverOutline() {
|
|
480
|
+
if (outlinedEl) {
|
|
481
|
+
outlinedEl.style.outline = prevOutline;
|
|
482
|
+
outlinedEl.style.outlineOffset = '';
|
|
483
|
+
outlinedEl = null;
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
// ─── Copied feedback ────────────────────────────────────────────────────────
|
|
488
|
+
function showCopied() {
|
|
489
|
+
tooltip.innerHTML = '<div style="color:' + GREEN + ';font-weight:600;display:flex;align-items:center;gap:6px"><span>✓</span><span>Copied!</span></div>';
|
|
490
|
+
tooltip.style.display = 'block';
|
|
491
|
+
clearTimeout(copiedTimer);
|
|
492
|
+
copiedTimer = setTimeout(function () {
|
|
493
|
+
if (fiActive && lastHovered) reRenderTooltip();
|
|
494
|
+
else hideTooltip();
|
|
495
|
+
}, 1200);
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
function reRenderTooltip() {
|
|
499
|
+
if (measureMode) {
|
|
500
|
+
var text = (pinEl && pinEl !== lastHovered) ? measureBetween(pinEl, lastHovered) : measureToNeighbor(lastHovered);
|
|
501
|
+
tooltip.innerHTML = linesToHTML(text);
|
|
502
|
+
} else {
|
|
503
|
+
tooltip.innerHTML = buildHumanDisplay(buildInfo(lastHovered));
|
|
504
|
+
}
|
|
505
|
+
positionTooltip(lastMouse.x, lastMouse.y);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// ─── Multi-select ─────────────────────────────────────────────────────────
|
|
509
|
+
function makeBadge(el, index) {
|
|
510
|
+
var rect = el.getBoundingClientRect();
|
|
511
|
+
var badge = document.createElement('div');
|
|
512
|
+
badge.textContent = String(index + 1);
|
|
513
|
+
Object.assign(badge.style, {
|
|
514
|
+
position: 'fixed',
|
|
515
|
+
left: (rect.left - 4) + 'px',
|
|
516
|
+
top: (rect.top - 4) + 'px',
|
|
517
|
+
width: '20px',
|
|
518
|
+
height: '20px',
|
|
519
|
+
background: PURPLE,
|
|
520
|
+
color: '#fff',
|
|
521
|
+
fontSize: '11px',
|
|
522
|
+
fontWeight: '700',
|
|
523
|
+
fontFamily: MONO,
|
|
524
|
+
borderRadius: '50%',
|
|
525
|
+
display: 'flex',
|
|
526
|
+
alignItems: 'center',
|
|
527
|
+
justifyContent: 'center',
|
|
528
|
+
zIndex: '2147483644',
|
|
529
|
+
pointerEvents: 'none',
|
|
530
|
+
boxShadow: '0 2px 6px rgba(0,0,0,0.3)',
|
|
531
|
+
});
|
|
532
|
+
document.body.appendChild(badge);
|
|
533
|
+
return badge;
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
function rebuildBadges() {
|
|
537
|
+
selectedBadges.forEach(function (b) { b.remove(); });
|
|
538
|
+
selectedBadges.length = 0;
|
|
539
|
+
selectedEls.forEach(function (el, i) {
|
|
540
|
+
selectedBadges.push(makeBadge(el, i));
|
|
541
|
+
});
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
function toggleSelection(el) {
|
|
545
|
+
var idx = selectedEls.indexOf(el);
|
|
546
|
+
if (idx >= 0) {
|
|
547
|
+
selectedEls.splice(idx, 1);
|
|
548
|
+
el.style.outline = '';
|
|
549
|
+
} else {
|
|
550
|
+
selectedEls.push(el);
|
|
551
|
+
el.style.outline = '2px solid ' + PURPLE;
|
|
552
|
+
el.style.outlineOffset = '-1px';
|
|
553
|
+
}
|
|
554
|
+
rebuildBadges();
|
|
555
|
+
updatePillForSelection();
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
function clearSelection() {
|
|
559
|
+
selectedEls.forEach(function (el) { el.style.outline = ''; el.style.outlineOffset = ''; });
|
|
560
|
+
selectedEls.length = 0;
|
|
561
|
+
selectedBadges.forEach(function (b) { b.remove(); });
|
|
562
|
+
selectedBadges.length = 0;
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
function updatePillForSelection() {
|
|
566
|
+
if (selectedEls.length > 0) expandPill();
|
|
567
|
+
else if (!pillWrap.matches(':hover')) collapsePill();
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// ─── Pin (measure) ──────────────────────────────────────────────────────────
|
|
571
|
+
function setPin(el) {
|
|
572
|
+
pinEl = el;
|
|
573
|
+
updatePinHL();
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
function updatePinHL() {
|
|
577
|
+
if (!pinEl) return;
|
|
578
|
+
var r = pinEl.getBoundingClientRect();
|
|
579
|
+
if (!pinHighlight) {
|
|
580
|
+
pinHighlight = document.createElement('div');
|
|
581
|
+
Object.assign(pinHighlight.style, {
|
|
582
|
+
position: 'fixed',
|
|
583
|
+
border: '2px dashed ' + RED,
|
|
584
|
+
background: 'rgba(237,62,97,0.05)',
|
|
585
|
+
boxSizing: 'border-box',
|
|
586
|
+
pointerEvents: 'none',
|
|
587
|
+
zIndex: '2147483643',
|
|
588
|
+
});
|
|
589
|
+
document.body.appendChild(pinHighlight);
|
|
590
|
+
}
|
|
591
|
+
pinHighlight.style.left = r.left + 'px';
|
|
592
|
+
pinHighlight.style.top = r.top + 'px';
|
|
593
|
+
pinHighlight.style.width = r.width + 'px';
|
|
594
|
+
pinHighlight.style.height = r.height + 'px';
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
function clearPin() {
|
|
598
|
+
if (pinHighlight) { pinHighlight.remove(); pinHighlight = null; }
|
|
599
|
+
pinEl = null;
|
|
600
|
+
}
|
|
601
|
+
|
|
602
|
+
function clearMeasureOverlay() {
|
|
603
|
+
measureOverlay.innerHTML = '';
|
|
604
|
+
measureOverlay.style.display = 'none';
|
|
605
|
+
}
|
|
606
|
+
|
|
607
|
+
function showMeasureTargetHL(el) {
|
|
608
|
+
clearMeasureTargetHL();
|
|
609
|
+
var r = el.getBoundingClientRect();
|
|
610
|
+
measureHL = document.createElement('div');
|
|
611
|
+
Object.assign(measureHL.style, {
|
|
612
|
+
position: 'fixed',
|
|
613
|
+
left: r.left + 'px',
|
|
614
|
+
top: r.top + 'px',
|
|
615
|
+
width: r.width + 'px',
|
|
616
|
+
height: r.height + 'px',
|
|
617
|
+
background: 'rgba(173,36,211,0.08)',
|
|
618
|
+
border: '1.5px solid ' + PURPLE,
|
|
619
|
+
boxSizing: 'border-box',
|
|
620
|
+
pointerEvents: 'none',
|
|
621
|
+
zIndex: '2147483643',
|
|
622
|
+
});
|
|
623
|
+
document.body.appendChild(measureHL);
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
function clearMeasureTargetHL() {
|
|
627
|
+
if (measureHL) { measureHL.remove(); measureHL = null; }
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
// ─── Measurement drawing ────────────────────────────────────────────────────
|
|
631
|
+
function cap(x, y, mainHoriz) {
|
|
632
|
+
var c = document.createElement('div');
|
|
633
|
+
Object.assign(c.style, { position: 'absolute', background: RED, pointerEvents: 'none' });
|
|
634
|
+
if (mainHoriz) {
|
|
635
|
+
c.style.left = (x - 0.5) + 'px';
|
|
636
|
+
c.style.top = (y - 4) + 'px';
|
|
637
|
+
c.style.width = '1px';
|
|
638
|
+
c.style.height = '8px';
|
|
639
|
+
} else {
|
|
640
|
+
c.style.left = (x - 4) + 'px';
|
|
641
|
+
c.style.top = (y - 0.5) + 'px';
|
|
642
|
+
c.style.width = '8px';
|
|
643
|
+
c.style.height = '1px';
|
|
644
|
+
}
|
|
645
|
+
measureOverlay.appendChild(c);
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
function drawLine(x1, y1, x2, y2, label) {
|
|
649
|
+
var isHoriz = Math.abs(y2 - y1) < 1;
|
|
650
|
+
var line = document.createElement('div');
|
|
651
|
+
Object.assign(line.style, { position: 'absolute', background: RED, pointerEvents: 'none' });
|
|
652
|
+
if (isHoriz) {
|
|
653
|
+
Object.assign(line.style, {
|
|
654
|
+
left: Math.min(x1, x2) + 'px', top: (y1 - 0.5) + 'px',
|
|
655
|
+
width: Math.abs(x2 - x1) + 'px', height: '1px',
|
|
656
|
+
});
|
|
657
|
+
} else {
|
|
658
|
+
Object.assign(line.style, {
|
|
659
|
+
left: (x1 - 0.5) + 'px', top: Math.min(y1, y2) + 'px',
|
|
660
|
+
width: '1px', height: Math.abs(y2 - y1) + 'px',
|
|
661
|
+
});
|
|
662
|
+
}
|
|
663
|
+
measureOverlay.appendChild(line);
|
|
664
|
+
cap(x1, y1, isHoriz);
|
|
665
|
+
cap(x2, y2, isHoriz);
|
|
666
|
+
|
|
667
|
+
if (label && label !== '0px') {
|
|
668
|
+
var lbl = document.createElement('div');
|
|
669
|
+
lbl.textContent = label;
|
|
670
|
+
Object.assign(lbl.style, {
|
|
671
|
+
position: 'absolute',
|
|
672
|
+
background: RED,
|
|
673
|
+
color: '#fff',
|
|
674
|
+
fontSize: '10px',
|
|
675
|
+
fontFamily: MONO,
|
|
676
|
+
padding: '1px 4px',
|
|
677
|
+
borderRadius: '3px',
|
|
678
|
+
pointerEvents: 'none',
|
|
679
|
+
whiteSpace: 'nowrap',
|
|
680
|
+
});
|
|
681
|
+
if (isHoriz) {
|
|
682
|
+
var cxl = (Math.min(x1, x2) + Math.max(x1, x2)) / 2;
|
|
683
|
+
lbl.style.left = cxl + 'px';
|
|
684
|
+
lbl.style.top = (y1 - 16) + 'px';
|
|
685
|
+
lbl.style.transform = 'translateX(-50%)';
|
|
686
|
+
} else {
|
|
687
|
+
var cyl = (Math.min(y1, y2) + Math.max(y1, y2)) / 2;
|
|
688
|
+
lbl.style.left = (x1 + 6) + 'px';
|
|
689
|
+
lbl.style.top = cyl + 'px';
|
|
690
|
+
lbl.style.transform = 'translateY(-50%)';
|
|
691
|
+
}
|
|
692
|
+
measureOverlay.appendChild(lbl);
|
|
693
|
+
}
|
|
694
|
+
}
|
|
695
|
+
|
|
696
|
+
// ─── Neighbor computation (parent-first w/ container fallback) ───────────────
|
|
697
|
+
function edgeToAncestor(targetEl, tr, dir) {
|
|
698
|
+
var cur = targetEl.parentElement;
|
|
699
|
+
var ctx = 'parent';
|
|
700
|
+
for (var i = 0; i < 4 && cur && cur !== document.body; i++) {
|
|
701
|
+
var pr = cur.getBoundingClientRect();
|
|
702
|
+
var gap;
|
|
703
|
+
if (dir === 'top') gap = tr.top - pr.top;
|
|
704
|
+
else if (dir === 'bottom') gap = pr.bottom - tr.bottom;
|
|
705
|
+
else if (dir === 'left') gap = tr.left - pr.left;
|
|
706
|
+
else gap = pr.right - tr.right;
|
|
707
|
+
if (gap > 0.5) {
|
|
708
|
+
var ed;
|
|
709
|
+
if (dir === 'top') ed = pr.top;
|
|
710
|
+
else if (dir === 'bottom') ed = pr.bottom;
|
|
711
|
+
else if (dir === 'left') ed = pr.left;
|
|
712
|
+
else ed = pr.right;
|
|
713
|
+
return { gap: gap, ctx: ctx, edge: ed };
|
|
714
|
+
}
|
|
715
|
+
cur = cur.parentElement;
|
|
716
|
+
ctx = 'container';
|
|
717
|
+
}
|
|
718
|
+
return null;
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
function computeNeighbors(targetEl, tr) {
|
|
722
|
+
var out = { top: null, bottom: null, left: null, right: null };
|
|
723
|
+
var parent = targetEl.parentElement;
|
|
724
|
+
if (parent) {
|
|
725
|
+
var sibs = Array.prototype.slice.call(parent.children).filter(function (c) { return c !== targetEl && c.offsetParent !== null; });
|
|
726
|
+
sibs.forEach(function (sib) {
|
|
727
|
+
var sr = sib.getBoundingClientRect();
|
|
728
|
+
if (sr.bottom <= tr.top) { var g = tr.top - sr.bottom; if (!out.top || g < out.top.gap) out.top = { gap: g, ctx: 'sibling', edge: sr.bottom }; }
|
|
729
|
+
if (sr.top >= tr.bottom) { var g2 = sr.top - tr.bottom; if (!out.bottom || g2 < out.bottom.gap) out.bottom = { gap: g2, ctx: 'sibling', edge: sr.top }; }
|
|
730
|
+
if (sr.right <= tr.left) { var g3 = tr.left - sr.right; if (!out.left || g3 < out.left.gap) out.left = { gap: g3, ctx: 'sibling', edge: sr.right }; }
|
|
731
|
+
if (sr.left >= tr.right) { var g4 = sr.left - tr.right; if (!out.right || g4 < out.right.gap) out.right = { gap: g4, ctx: 'sibling', edge: sr.left }; }
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
['top', 'bottom', 'left', 'right'].forEach(function (k) { if (!out[k]) out[k] = edgeToAncestor(targetEl, tr, k); });
|
|
735
|
+
return out;
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
function measureToNeighbor(targetEl) {
|
|
739
|
+
clearMeasureOverlay();
|
|
740
|
+
var tr = targetEl.getBoundingClientRect();
|
|
741
|
+
var dirs = computeNeighbors(targetEl, tr);
|
|
742
|
+
var cx = (tr.left + tr.right) / 2;
|
|
743
|
+
var cy = (tr.top + tr.bottom) / 2;
|
|
744
|
+
if (dirs.top) drawLine(cx, dirs.top.edge, cx, tr.top, Math.round(dirs.top.gap) + 'px');
|
|
745
|
+
if (dirs.bottom) drawLine(cx, tr.bottom, cx, dirs.bottom.edge, Math.round(dirs.bottom.gap) + 'px');
|
|
746
|
+
if (dirs.left) drawLine(dirs.left.edge, cy, tr.left, cy, Math.round(dirs.left.gap) + 'px');
|
|
747
|
+
if (dirs.right) drawLine(tr.right, cy, dirs.right.edge, cy, Math.round(dirs.right.gap) + 'px');
|
|
748
|
+
measureOverlay.style.display = 'block';
|
|
749
|
+
var tag = targetEl.tagName.toLowerCase();
|
|
750
|
+
var lines = ['⬡ Measure (M pin · Cmd+C copy)', '<' + tag + '> ' + Math.round(tr.width) + '×' + Math.round(tr.height)];
|
|
751
|
+
['top', 'right', 'bottom', 'left'].forEach(function (k) {
|
|
752
|
+
if (dirs[k]) lines.push(k + ': ' + Math.round(dirs[k].gap) + 'px (to ' + dirs[k].ctx + ')');
|
|
753
|
+
});
|
|
754
|
+
return lines.join('\n');
|
|
755
|
+
}
|
|
756
|
+
|
|
757
|
+
function buildNeighborCopyText(el) {
|
|
758
|
+
var tr = el.getBoundingClientRect();
|
|
759
|
+
var dirs = computeNeighbors(el, tr);
|
|
760
|
+
var lines = ['[Specter Measure]', '<' + el.tagName.toLowerCase() + '> ' + Math.round(tr.width) + '×' + Math.round(tr.height), 'selector: ' + getSelector(el)];
|
|
761
|
+
['top', 'right', 'bottom', 'left'].forEach(function (k) {
|
|
762
|
+
if (dirs[k]) lines.push(k + ': ' + Math.round(dirs[k].gap) + 'px (to ' + dirs[k].ctx + ')');
|
|
763
|
+
});
|
|
764
|
+
return lines.join('\n');
|
|
765
|
+
}
|
|
766
|
+
|
|
767
|
+
function measureBetween(fromEl, toEl) {
|
|
768
|
+
clearMeasureOverlay();
|
|
769
|
+
var fr = fromEl.getBoundingClientRect();
|
|
770
|
+
var tr = toEl.getBoundingClientRect();
|
|
771
|
+
var fTag = fromEl.tagName.toLowerCase(), tTag = toEl.tagName.toLowerCase();
|
|
772
|
+
var fComp = getComponentName(fromEl), tComp = getComponentName(toEl);
|
|
773
|
+
|
|
774
|
+
var lines = ['⬡ Measure (Cmd+C copy)'];
|
|
775
|
+
lines.push('From: <' + fTag + '>' + (fComp ? ' ' + fComp : '') + ' ' + Math.round(fr.width) + '×' + Math.round(fr.height));
|
|
776
|
+
lines.push('To: <' + tTag + '>' + (tComp ? ' ' + tComp : '') + ' ' + Math.round(tr.width) + '×' + Math.round(tr.height));
|
|
777
|
+
|
|
778
|
+
var fromContainsTo = fr.left <= tr.left && fr.top <= tr.top && fr.right >= tr.right && fr.bottom >= tr.bottom;
|
|
779
|
+
var toContainsFrom = tr.left <= fr.left && tr.top <= fr.top && tr.right >= fr.right && tr.bottom >= fr.bottom;
|
|
780
|
+
|
|
781
|
+
if (fromContainsTo || toContainsFrom) {
|
|
782
|
+
var outer = fromContainsTo ? fr : tr;
|
|
783
|
+
var inner = fromContainsTo ? tr : fr;
|
|
784
|
+
var cx = (inner.left + inner.right) / 2, cy = (inner.top + inner.bottom) / 2;
|
|
785
|
+
var iT = inner.top - outer.top, iB = outer.bottom - inner.bottom;
|
|
786
|
+
var iL = inner.left - outer.left, iR = outer.right - inner.right;
|
|
787
|
+
if (iT > 0) drawLine(cx, outer.top, cx, inner.top, Math.round(iT) + 'px');
|
|
788
|
+
if (iB > 0) drawLine(cx, inner.bottom, cx, outer.bottom, Math.round(iB) + 'px');
|
|
789
|
+
if (iL > 0) drawLine(outer.left, cy, inner.left, cy, Math.round(iL) + 'px');
|
|
790
|
+
if (iR > 0) drawLine(inner.right, cy, outer.right, cy, Math.round(iR) + 'px');
|
|
791
|
+
lines.push('inset: ' + Math.round(iT) + 'px ' + Math.round(iR) + 'px ' + Math.round(iB) + 'px ' + Math.round(iL) + 'px');
|
|
792
|
+
} else {
|
|
793
|
+
var vGap = fr.bottom <= tr.top ? tr.top - fr.bottom : tr.bottom <= fr.top ? fr.top - tr.bottom : 0;
|
|
794
|
+
var hGap = fr.right <= tr.left ? tr.left - fr.right : tr.right <= fr.left ? fr.left - tr.right : 0;
|
|
795
|
+
var cx2 = (fr.left + fr.right) / 2, cy2 = (fr.top + fr.bottom) / 2;
|
|
796
|
+
if (vGap > 0) {
|
|
797
|
+
var y1 = fr.bottom <= tr.top ? fr.bottom : tr.bottom;
|
|
798
|
+
var y2 = fr.bottom <= tr.top ? tr.top : fr.top;
|
|
799
|
+
drawLine(cx2, y1, cx2, y2, Math.round(vGap) + 'px');
|
|
800
|
+
lines.push('vertical gap: ' + Math.round(vGap) + 'px');
|
|
801
|
+
}
|
|
802
|
+
if (hGap > 0) {
|
|
803
|
+
var x1 = fr.right <= tr.left ? fr.right : tr.right;
|
|
804
|
+
var x2 = fr.right <= tr.left ? tr.left : fr.left;
|
|
805
|
+
drawLine(x1, cy2, x2, cy2, Math.round(hGap) + 'px');
|
|
806
|
+
lines.push('horizontal gap: ' + Math.round(hGap) + 'px');
|
|
807
|
+
}
|
|
808
|
+
if (vGap === 0 && hGap === 0) lines.push('Elements overlap');
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
measureOverlay.style.display = 'block';
|
|
812
|
+
return lines.join('\n');
|
|
813
|
+
}
|
|
814
|
+
|
|
815
|
+
function buildMeasureCopyText(fromEl, toEl) {
|
|
816
|
+
var fr = fromEl.getBoundingClientRect(), tr = toEl.getBoundingClientRect();
|
|
817
|
+
var fromContainsTo = fr.left <= tr.left && fr.top <= tr.top && fr.right >= tr.right && fr.bottom >= tr.bottom;
|
|
818
|
+
var toContainsFrom = tr.left <= fr.left && tr.top <= fr.top && tr.right >= fr.right && tr.bottom >= fr.bottom;
|
|
819
|
+
var fTag = fromEl.tagName.toLowerCase(), tTag = toEl.tagName.toLowerCase();
|
|
820
|
+
var fComp = getComponentName(fromEl), tComp = getComponentName(toEl);
|
|
821
|
+
|
|
822
|
+
var lines = ['[Specter Measure]'];
|
|
823
|
+
lines.push('From: <' + fTag + '>' + (fComp ? ' ' + fComp : '') + ' ' + Math.round(fr.width) + '×' + Math.round(fr.height));
|
|
824
|
+
lines.push(' selector: ' + getSelector(fromEl));
|
|
825
|
+
lines.push('To: <' + tTag + '>' + (tComp ? ' ' + tComp : '') + ' ' + Math.round(tr.width) + '×' + Math.round(tr.height));
|
|
826
|
+
lines.push(' selector: ' + getSelector(toEl));
|
|
827
|
+
|
|
828
|
+
if (fromContainsTo || toContainsFrom) {
|
|
829
|
+
var outer = fromContainsTo ? fr : tr, inner = fromContainsTo ? tr : fr;
|
|
830
|
+
lines.push('inset: ' + Math.round(inner.top - outer.top) + 'px ' + Math.round(outer.right - inner.right) + 'px ' + Math.round(outer.bottom - inner.bottom) + 'px ' + Math.round(inner.left - outer.left) + 'px');
|
|
831
|
+
} else {
|
|
832
|
+
var vGap = fr.bottom <= tr.top ? tr.top - fr.bottom : tr.bottom <= fr.top ? fr.top - tr.bottom : 0;
|
|
833
|
+
var hGap = fr.right <= tr.left ? tr.left - fr.right : tr.right <= fr.left ? fr.left - tr.right : 0;
|
|
834
|
+
if (vGap > 0) lines.push('vertical gap: ' + Math.round(vGap) + 'px');
|
|
835
|
+
if (hGap > 0) lines.push('horizontal gap: ' + Math.round(hGap) + 'px');
|
|
836
|
+
if (vGap === 0 && hGap === 0) lines.push('Elements overlap');
|
|
837
|
+
}
|
|
838
|
+
return lines.join('\n');
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
// ─── Shortcut parser ──────────────────────────────────────────────────────
|
|
842
|
+
function matchesActivate(e) {
|
|
843
|
+
var parts = ACTIVATE.toLowerCase().split('+');
|
|
844
|
+
var needCtrl = parts.indexOf('ctrl') >= 0;
|
|
845
|
+
var needAlt = parts.indexOf('alt') >= 0;
|
|
846
|
+
var needShift = parts.indexOf('shift') >= 0;
|
|
847
|
+
var needMeta = parts.indexOf('meta') >= 0 || parts.indexOf('cmd') >= 0;
|
|
848
|
+
var key = parts.filter(function (p) { return ['ctrl', 'alt', 'shift', 'meta', 'cmd'].indexOf(p) < 0; })[0];
|
|
849
|
+
if (needCtrl !== e.ctrlKey) return false;
|
|
850
|
+
if (needAlt !== e.altKey) return false;
|
|
851
|
+
if (needShift !== e.shiftKey) return false;
|
|
852
|
+
if (needMeta !== e.metaKey) return false;
|
|
853
|
+
if (!key) return false;
|
|
854
|
+
var eKey = e.key.toLowerCase();
|
|
855
|
+
var eCode = e.code.toLowerCase();
|
|
856
|
+
return eKey === key || eCode === 'key' + key || (key === 'period' && (eKey === '.' || eCode === 'period'));
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
// ─── Mouse ────────────────────────────────────────────────────────────────
|
|
860
|
+
function onMouseMove(e) {
|
|
861
|
+
if (!fiActive) return;
|
|
862
|
+
lastMouse.x = e.clientX; lastMouse.y = e.clientY;
|
|
863
|
+
var target = e.target;
|
|
864
|
+
if (!target || target === pillWrap || pillWrap.contains(target)) return;
|
|
865
|
+
if (target === tooltip || tooltip.contains(target)) return;
|
|
866
|
+
|
|
867
|
+
lastHovered = target;
|
|
868
|
+
|
|
869
|
+
if (measureMode) {
|
|
870
|
+
clearHoverOutline();
|
|
871
|
+
showMeasureTargetHL(target);
|
|
872
|
+
var text = (pinEl && pinEl !== target) ? measureBetween(pinEl, target) : measureToNeighbor(target);
|
|
873
|
+
tooltip.innerHTML = linesToHTML(text);
|
|
874
|
+
positionTooltip(e.clientX, e.clientY);
|
|
875
|
+
if (pinEl) updatePinHL();
|
|
876
|
+
} else {
|
|
877
|
+
clearMeasureOverlay();
|
|
878
|
+
clearMeasureTargetHL();
|
|
879
|
+
setHoverOutline(target);
|
|
880
|
+
tooltip.innerHTML = buildHumanDisplay(buildInfo(target));
|
|
881
|
+
positionTooltip(e.clientX, e.clientY);
|
|
882
|
+
}
|
|
883
|
+
}
|
|
884
|
+
|
|
885
|
+
// ─── Keyboard ─────────────────────────────────────────────────────────────
|
|
886
|
+
function isInputFocused() {
|
|
887
|
+
var el = document.activeElement;
|
|
888
|
+
return !!el && (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA' || el.isContentEditable);
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
document.addEventListener('keydown', function (e) {
|
|
892
|
+
if (matchesActivate(e)) {
|
|
893
|
+
e.preventDefault();
|
|
894
|
+
if (fiActive) deactivate(); else activate();
|
|
895
|
+
return;
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
if (!fiActive) return;
|
|
899
|
+
|
|
900
|
+
if (e.key === 'Alt' && !e.ctrlKey && !e.metaKey && !e.shiftKey) {
|
|
901
|
+
optionHeld = true;
|
|
902
|
+
return;
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
if ((e.metaKey || e.ctrlKey) && e.key === 'c' && !e.shiftKey && !e.altKey) {
|
|
906
|
+
e.preventDefault();
|
|
907
|
+
var text;
|
|
908
|
+
if (selectedEls.length > 0) {
|
|
909
|
+
text = buildMultiSelectCopyText();
|
|
910
|
+
} else if (measureMode && pinEl && lastHovered && lastHovered !== pinEl) {
|
|
911
|
+
text = buildMeasureCopyText(pinEl, lastHovered);
|
|
912
|
+
} else if (measureMode && lastHovered) {
|
|
913
|
+
text = buildNeighborCopyText(lastHovered);
|
|
914
|
+
} else if (lastHovered) {
|
|
915
|
+
text = buildLLMClipboard(buildInfo(lastHovered));
|
|
916
|
+
}
|
|
917
|
+
if (text) navigator.clipboard.writeText(text).then(showCopied).catch(function () {});
|
|
918
|
+
return;
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
if (isInputFocused()) return;
|
|
922
|
+
|
|
923
|
+
if (e.key === 'p' || e.key === 'P') {
|
|
924
|
+
if (!lastHovered) return;
|
|
925
|
+
clearHoverOutline();
|
|
926
|
+
toggleSelection(lastHovered);
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
929
|
+
|
|
930
|
+
if (e.key === 'm' || e.key === 'M') {
|
|
931
|
+
if (!measureMode || !lastHovered) return;
|
|
932
|
+
if (pinEl) {
|
|
933
|
+
clearPin();
|
|
934
|
+
collapsePill();
|
|
935
|
+
} else {
|
|
936
|
+
setPin(lastHovered);
|
|
937
|
+
expandPill('Pinned · hover another element · Cmd+C copy · Esc clear');
|
|
938
|
+
}
|
|
939
|
+
return;
|
|
940
|
+
}
|
|
941
|
+
|
|
942
|
+
if (e.key === 'Escape') {
|
|
943
|
+
if (pinEl || selectedEls.length > 0) {
|
|
944
|
+
clearPin();
|
|
945
|
+
clearSelection();
|
|
946
|
+
clearMeasureOverlay();
|
|
947
|
+
hideTooltip();
|
|
948
|
+
collapsePill();
|
|
949
|
+
} else {
|
|
950
|
+
deactivate();
|
|
951
|
+
}
|
|
952
|
+
return;
|
|
953
|
+
}
|
|
954
|
+
}, true);
|
|
955
|
+
|
|
956
|
+
document.addEventListener('keyup', function (e) {
|
|
957
|
+
if (!fiActive) return;
|
|
958
|
+
if (e.key === 'Alt' && optionHeld) {
|
|
959
|
+
optionHeld = false;
|
|
960
|
+
measureMode = !measureMode;
|
|
961
|
+
clearMeasureOverlay();
|
|
962
|
+
clearHoverOutline();
|
|
963
|
+
clearMeasureTargetHL();
|
|
964
|
+
if (!measureMode) clearPin();
|
|
965
|
+
hideTooltip();
|
|
966
|
+
flashMode();
|
|
967
|
+
}
|
|
968
|
+
}, true);
|
|
969
|
+
|
|
970
|
+
document.addEventListener('mousemove', onMouseMove, { passive: true });
|
|
971
|
+
|
|
972
|
+
window.__specterToggle = function() { if (fiActive) deactivate(); else activate(); };
|
|
973
|
+
|
|
974
|
+
var _rt = (typeof browser !== 'undefined' && browser.runtime) || (typeof chrome !== 'undefined' && chrome.runtime);
|
|
975
|
+
if (_rt && _rt.onMessage) {
|
|
976
|
+
_rt.onMessage.addListener(function(msg) {
|
|
977
|
+
if (msg && msg.type === 'specter-toggle') window.__specterToggle();
|
|
978
|
+
});
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
console.log('%c👻 Specter — Ctrl+Option+Z to toggle', 'color:#aaa;font-size:11px;');
|
|
982
|
+
})();
|