Repository navigation
Expand file tree
/
Copy pathinject.js
More file actions
366 lines (326 loc) · 11.7 KB
/
Copy pathinject.js
File metadata and controls
366 lines (326 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
(function() {
var DEFAULT_PORT = 3456;
var SIDECAR = 'http://localhost:' + DEFAULT_PORT;
var portLoaded = false;
// Port discovery strategy (ordered by priority):
// 1. Global variable (set by Next.js/bundler plugin with known port)
// 2. fetch('/__cc-port') from dev server middleware (Vite)
// 3. Probe sidecar directly at http://localhost:${port}/__cc-port (Next.js / other)
// Strategy 1: Check global variable
if (typeof window !== 'undefined' && window.__CC_PROMPTER_PORT__) {
SIDECAR = 'http://localhost:' + window.__CC_PROMPTER_PORT__;
portLoaded = true;
}
// Strategy 2: Fetch from dev server middleware
if (!portLoaded) {
fetch('/__cc-port').then(function(r) { return r.text(); }).then(function(p) {
if (p && parseInt(p) > 0) {
SIDECAR = 'http://localhost:' + p;
portLoaded = true;
}
}).catch(function() {
// Strategy 3: Probe sidecar directly
var start = window.__CC_PROMPTER_START_PORT__ || DEFAULT_PORT;
probeSidecar(start, start + 10);
});
}
function probeSidecar(port, maxPort) {
function tryNext() {
if (port > maxPort || portLoaded) return;
fetch('http://localhost:' + port + '/__cc-port').then(function(r) {
return r.text();
}).then(function(p) {
if (p && parseInt(p) > 0) {
SIDECAR = 'http://localhost:' + p;
portLoaded = true;
} else {
port++;
tryNext();
}
}).catch(function() {
port++;
tryNext();
});
}
tryNext();
}
var container = null;
var iframe = null;
var iframeReady = false;
var dragBar = null;
var resizeHandle = null;
// Drag state
var dragging = false;
var dragOffsetX = 0;
var dragOffsetY = 0;
// Resize state
var resizing = false;
var resizeStartX = 0;
var resizeStartY = 0;
var resizeStartW = 0;
var resizeStartH = 0;
// Edge resize state (top/left/bottom/right)
var edgeResizing = false;
var edgeDir = '';
var edgeStartX = 0;
var edgeStartY = 0;
var edgeStartRect = null;
var MIN_W = 320;
var MIN_H = 280;
var EDGE_THRESHOLD = 6;
function getOrCreatePanel() {
if (container) return container;
// Container
container = document.createElement('div');
container.id = 'cc-prompter-container';
container.style.cssText =
'position:fixed;bottom:16px;right:16px;width:480px;height:540px;' +
'border:1px solid #e0e0e0;border-radius:10px;z-index:99999;' +
'box-shadow:0 4px 24px rgba(0,0,0,0.10);display:none;background:#fff;' +
'overflow:visible;';
// Drag bar
dragBar = document.createElement('div');
dragBar.style.cssText =
'height:28px;background:#fafafa;border-bottom:1px solid #ebebeb;' +
'cursor:move;display:flex;align-items:center;justify-content:space-between;' +
'padding:0 10px;user-select:none;border-radius:10px 10px 0 0;';
dragBar.innerHTML =
'<span style="font-size:11px;font-weight:600;color:#888;letter-spacing:0.3px;">CC Prompter</span>' +
'<span style="font-size:11px;color:#bbb;" id="cc-drag-hint">drag to move</span>';
// Iframe wrapper (holds iframe + resize handle, clips corners)
var wrap = document.createElement('div');
wrap.style.cssText = 'position:relative;width:100%;height:calc(100% - 28px);overflow:hidden;border-radius:0 0 10px 10px;';
// Iframe
iframe = document.createElement('iframe');
iframe.id = 'cc-prompter-panel';
iframe.src = SIDECAR + '/__panel/';
iframe.style.cssText = 'width:100%;height:100%;border:none;';
// Resize handle (bottom-right corner)
resizeHandle = document.createElement('div');
resizeHandle.style.cssText =
'position:absolute;right:0;bottom:0;width:16px;height:16px;' +
'cursor:nwse-resize;z-index:10;';
// SVG grip icon
resizeHandle.innerHTML =
'<svg width="16" height="16" viewBox="0 0 16 16" style="opacity:0.3;">' +
'<path d="M14 14L14 10M14 14L10 14M14 14L14 6M14 14L6 14M14 14L14 2M14 14L2 14" ' +
'stroke="#888" stroke-width="1.2" stroke-linecap="round"/></svg>';
wrap.appendChild(iframe);
wrap.appendChild(resizeHandle);
container.appendChild(dragBar);
container.appendChild(wrap);
document.body.appendChild(container);
iframe.onload = function() { iframeReady = true; };
// Drag events
dragBar.addEventListener('mousedown', function(e) {
dragging = true;
var rect = container.getBoundingClientRect();
dragOffsetX = e.clientX - rect.left;
dragOffsetY = e.clientY - rect.top;
container.style.transition = 'none';
e.preventDefault();
});
// Hide drag hint after first drag
dragBar.addEventListener('mousedown', function() {
var hint = document.getElementById('cc-drag-hint');
if (hint) setTimeout(function() { hint.style.display = 'none'; }, 2000);
});
// Corner resize events
resizeHandle.addEventListener('mousedown', function(e) {
resizing = true;
resizeStartX = e.clientX;
resizeStartY = e.clientY;
resizeStartW = container.offsetWidth;
resizeStartH = container.offsetHeight;
container.style.transition = 'none';
e.preventDefault();
e.stopPropagation();
});
// Edge resize — detect mouse near edges of container
container.addEventListener('mousedown', function(e) {
if (e.target === resizeHandle || e.target.closest('#cc-prompter-panel')) return;
var rect = container.getBoundingClientRect();
var x = e.clientX - rect.left;
var y = e.clientY - rect.top;
var dir = '';
if (x < EDGE_THRESHOLD) dir = 'w';
else if (x > rect.width - EDGE_THRESHOLD) dir = 'e';
if (y < EDGE_THRESHOLD) dir += 'n';
else if (y > rect.height - EDGE_THRESHOLD) dir += 's';
// Only edge-resize from container border, not drag bar or iframe
if (dir && e.target === container) {
edgeResizing = true;
edgeDir = dir;
edgeStartX = e.clientX;
edgeStartY = e.clientY;
edgeStartRect = { left: rect.left, top: rect.top, width: rect.width, height: rect.height };
container.style.transition = 'none';
e.preventDefault();
}
});
return container;
}
// Global mouse handlers
document.addEventListener('mousemove', function(e) {
if (dragging && container) {
var x = e.clientX - dragOffsetX;
var y = e.clientY - dragOffsetY;
x = Math.max(0, Math.min(x, window.innerWidth - container.offsetWidth));
y = Math.max(0, Math.min(y, window.innerHeight - container.offsetHeight));
container.style.left = x + 'px';
container.style.top = y + 'px';
container.style.right = 'auto';
container.style.bottom = 'auto';
}
if (resizing && container) {
var dw = e.clientX - resizeStartX;
var dh = e.clientY - resizeStartY;
var nw = Math.max(MIN_W, resizeStartW + dw);
var nh = Math.max(MIN_H, resizeStartH + dh);
// Clamp to viewport
var rect = container.getBoundingClientRect();
nw = Math.min(nw, window.innerWidth - rect.left);
nh = Math.min(nh, window.innerHeight - rect.top);
container.style.width = nw + 'px';
container.style.height = nh + 'px';
}
if (edgeResizing && container && edgeStartRect) {
var dx = e.clientX - edgeStartX;
var dy = e.clientY - edgeStartY;
var nl = edgeStartRect.left;
var nt = edgeStartRect.top;
var nw = edgeStartRect.width;
var nh = edgeStartRect.height;
if (edgeDir.indexOf('e') >= 0) {
nw = Math.max(MIN_W, edgeStartRect.width + dx);
nw = Math.min(nw, window.innerWidth - nl);
}
if (edgeDir.indexOf('w') >= 0) {
var proposedW = edgeStartRect.width - dx;
if (proposedW >= MIN_W && nl + dx >= 0) {
nw = proposedW;
nl = edgeStartRect.left + dx;
}
}
if (edgeDir.indexOf('s') >= 0) {
nh = Math.max(MIN_H, edgeStartRect.height + dy);
nh = Math.min(nh, window.innerHeight - nt);
}
if (edgeDir.indexOf('n') >= 0) {
var proposedH = edgeStartRect.height - dy;
if (proposedH >= MIN_H && nt + dy >= 0) {
nh = proposedH;
nt = edgeStartRect.top + dy;
}
}
container.style.left = nl + 'px';
container.style.top = nt + 'px';
container.style.right = 'auto';
container.style.bottom = 'auto';
container.style.width = nw + 'px';
container.style.height = nh + 'px';
}
// Update cursor on container edges
if (container && !dragging && !resizing && !edgeResizing) {
var rect = container.getBoundingClientRect();
var x = e.clientX - rect.left;
var y = e.clientY - rect.top;
var cursor = '';
if (x < EDGE_THRESHOLD || x > rect.width - EDGE_THRESHOLD) {
if (y < EDGE_THRESHOLD || y > rect.height - EDGE_THRESHOLD) {
cursor = 'nwse-resize';
} else {
cursor = x < EDGE_THRESHOLD ? 'ew-resize' : 'ew-resize';
}
} else if (y < EDGE_THRESHOLD || y > rect.height - EDGE_THRESHOLD) {
cursor = 'ns-resize';
}
if (cursor && e.target === container) {
container.style.cursor = cursor;
} else if (e.target === container) {
container.style.cursor = 'default';
}
}
});
document.addEventListener('mouseup', function() {
if (dragging) {
dragging = false;
if (container) container.style.transition = '';
}
if (resizing) {
resizing = false;
if (container) container.style.transition = '';
}
if (edgeResizing) {
edgeResizing = false;
edgeDir = '';
edgeStartRect = null;
if (container) container.style.transition = '';
}
});
function showPanel(source) {
var c = getOrCreatePanel();
c.style.display = 'block';
function send() {
try {
iframe.contentWindow.postMessage({
type: 'cc-prompter:source-info',
source: source
}, SIDECAR);
} catch(e) {
setTimeout(send, 100);
}
}
if (iframeReady) { send(); }
else { iframe.onload = function() { iframeReady = true; send(); }; }
}
// Listen for hide message from iframe (Escape key)
window.addEventListener('message', function(e) {
if (e.data && e.data.type === 'cc-prompter:hide') {
if (container && container.style.display !== 'none') {
container.style.display = 'none';
}
}
});
// Track mouse for element detection
var mx = 0, my = 0;
document.addEventListener('mousemove', function(e) { mx = e.clientX; my = e.clientY; });
// code-inspector click handler
window.addEventListener('code-inspector:trackCode', function(e) {
var d = e.detail || {};
var el = document.elementFromPoint(mx, my);
var info = '';
if (el) {
var tag = el.tagName.toLowerCase();
var cls = (el.className && typeof el.className === 'string')
? el.className.replace(/\s+/g, ' ').trim().slice(0, 80) : '';
var txt = (el.textContent || '').trim().slice(0, 50);
info = '<' + tag
+ (el.id ? ' id="' + el.id + '"' : '')
+ (cls ? ' class="' + cls + '"' : '')
+ (txt ? ' text~"' + txt + '"' : '')
+ '>';
}
showPanel({
name: d.name || '',
path: d.path || '',
line: d.line || 0,
column: d.column || 0,
elementInfo: info
});
});
// Escape to hide
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape') {
if (container && container.style.display !== 'none') container.style.display = 'none';
}
});
// Ctrl+Shift+P toggle
document.addEventListener('keydown', function(e) {
if (e.ctrlKey && e.shiftKey && e.key === 'P') {
e.preventDefault();
var c = getOrCreatePanel();
c.style.display = c.style.display === 'none' ? 'block' : 'none';
}
});
})();