create-interview-cockpit 0.15.0 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/template/client/src/App.tsx +3 -0
- package/template/client/src/components/CanvasLabModal.tsx +585 -0
- package/template/client/src/components/LabsPanel.tsx +63 -0
- package/template/client/src/components/Sidebar.tsx +12 -1
- package/template/client/src/components/WorkspaceSwitcher.tsx +36 -0
- package/template/client/src/reactLab.ts +1206 -0
- package/template/client/src/store.ts +24 -1
- package/template/client/src/types.ts +3 -1
- package/template/client/vite.config.ts +15 -8
- package/template/cockpit.json +1 -1
- package/template/server/src/google-drive.ts +4 -1
- package/template/server/src/index.ts +63 -6
- package/template/server/src/storage.ts +1 -0
package/package.json
CHANGED
|
@@ -10,6 +10,7 @@ import AiSettingsModal from "./components/AiSettingsModal";
|
|
|
10
10
|
import CodeRunnerModal from "./components/CodeRunnerModal";
|
|
11
11
|
import InfraLabModal from "./components/InfraLabModal";
|
|
12
12
|
import DeploymentLabModal from "./components/DeploymentLabModal";
|
|
13
|
+
import CanvasLabModal from "./components/CanvasLabModal";
|
|
13
14
|
import {
|
|
14
15
|
Code,
|
|
15
16
|
FlaskConical,
|
|
@@ -44,6 +45,7 @@ export default function App() {
|
|
|
44
45
|
showCodeRunner,
|
|
45
46
|
showInfraLab,
|
|
46
47
|
showDeploymentLab,
|
|
48
|
+
showCanvasLab,
|
|
47
49
|
closeCodeRunner,
|
|
48
50
|
} = useStore();
|
|
49
51
|
|
|
@@ -190,6 +192,7 @@ export default function App() {
|
|
|
190
192
|
{showCodeRunner && <CodeRunnerModal />}
|
|
191
193
|
{showInfraLab && <InfraLabModal />}
|
|
192
194
|
{showDeploymentLab && <DeploymentLabModal />}
|
|
195
|
+
{showCanvasLab && <CanvasLabModal />}
|
|
193
196
|
</div>
|
|
194
197
|
);
|
|
195
198
|
}
|
|
@@ -0,0 +1,585 @@
|
|
|
1
|
+
import {
|
|
2
|
+
useCallback,
|
|
3
|
+
useEffect,
|
|
4
|
+
useLayoutEffect,
|
|
5
|
+
useRef,
|
|
6
|
+
useState,
|
|
7
|
+
} from "react";
|
|
8
|
+
import {
|
|
9
|
+
Check,
|
|
10
|
+
GripVertical,
|
|
11
|
+
Loader2,
|
|
12
|
+
Maximize2,
|
|
13
|
+
Minimize2,
|
|
14
|
+
Play,
|
|
15
|
+
RotateCcw,
|
|
16
|
+
Save,
|
|
17
|
+
X,
|
|
18
|
+
} from "lucide-react";
|
|
19
|
+
import { useStore } from "../store";
|
|
20
|
+
import Editor from "react-simple-code-editor";
|
|
21
|
+
import Prism from "prismjs";
|
|
22
|
+
import "prismjs/components/prism-clike";
|
|
23
|
+
import "prismjs/components/prism-javascript";
|
|
24
|
+
|
|
25
|
+
// ─── Constants ────────────────────────────────────────────────────────────────
|
|
26
|
+
|
|
27
|
+
const MIN_W = 600;
|
|
28
|
+
const MIN_H = 400;
|
|
29
|
+
const DEFAULT_W = Math.min(1100, window.innerWidth - 48);
|
|
30
|
+
const DEFAULT_H = Math.min(720, window.innerHeight - 48);
|
|
31
|
+
const MIN_EDITOR_FRAC = 0.2; // editor pane can shrink to 20% of modal width
|
|
32
|
+
const MAX_EDITOR_FRAC = 0.8;
|
|
33
|
+
|
|
34
|
+
type ResizeDir = "e" | "s" | "se" | "sw" | "w" | "ne" | "nw" | "n" | null;
|
|
35
|
+
|
|
36
|
+
// ─── Default starter code ─────────────────────────────────────────────────────
|
|
37
|
+
|
|
38
|
+
const DEFAULT_CODE = `// canvas and ctx are pre-defined for you.
|
|
39
|
+
// canvas.width / canvas.height match the preview size.
|
|
40
|
+
// requestAnimationFrame works — the frame is restarted on each Run.
|
|
41
|
+
|
|
42
|
+
// ── Background ────────────────────────────────────────────
|
|
43
|
+
ctx.fillStyle = '#0f172a';
|
|
44
|
+
ctx.fillRect(0, 0, canvas.width, canvas.height);
|
|
45
|
+
|
|
46
|
+
// ── Circles ───────────────────────────────────────────────
|
|
47
|
+
const cx = canvas.width / 2;
|
|
48
|
+
const cy = canvas.height / 2;
|
|
49
|
+
|
|
50
|
+
ctx.fillStyle = '#06b6d4';
|
|
51
|
+
ctx.beginPath();
|
|
52
|
+
ctx.arc(cx, cy, 80, 0, Math.PI * 2);
|
|
53
|
+
ctx.fill();
|
|
54
|
+
|
|
55
|
+
ctx.strokeStyle = '#7c3aed';
|
|
56
|
+
ctx.lineWidth = 4;
|
|
57
|
+
ctx.beginPath();
|
|
58
|
+
ctx.arc(cx, cy, 110, 0, Math.PI * 2);
|
|
59
|
+
ctx.stroke();
|
|
60
|
+
|
|
61
|
+
// ── Rectangle ─────────────────────────────────────────────
|
|
62
|
+
ctx.fillStyle = 'rgba(245,158,11,0.7)';
|
|
63
|
+
ctx.fillRect(cx - 200, cy - 60, 100, 60);
|
|
64
|
+
|
|
65
|
+
// ── Line ──────────────────────────────────────────────────
|
|
66
|
+
ctx.strokeStyle = '#f43f5e';
|
|
67
|
+
ctx.lineWidth = 3;
|
|
68
|
+
ctx.beginPath();
|
|
69
|
+
ctx.moveTo(40, canvas.height - 40);
|
|
70
|
+
ctx.lineTo(canvas.width - 40, 40);
|
|
71
|
+
ctx.stroke();
|
|
72
|
+
|
|
73
|
+
// ── Text ──────────────────────────────────────────────────
|
|
74
|
+
ctx.fillStyle = '#e2e8f0';
|
|
75
|
+
ctx.font = 'bold 28px system-ui, sans-serif';
|
|
76
|
+
ctx.textAlign = 'center';
|
|
77
|
+
ctx.textBaseline = 'middle';
|
|
78
|
+
ctx.fillText('Canvas Lab ✏️', cx, cy + 140);
|
|
79
|
+
|
|
80
|
+
ctx.font = '13px monospace';
|
|
81
|
+
ctx.fillStyle = '#64748b';
|
|
82
|
+
ctx.fillText('Edit the code on the left — click Run to see changes', cx, cy + 175);
|
|
83
|
+
`;
|
|
84
|
+
|
|
85
|
+
// ─── srcdoc builder ───────────────────────────────────────────────────────────
|
|
86
|
+
|
|
87
|
+
function buildSrcdoc(code: string): string {
|
|
88
|
+
return `<!DOCTYPE html>
|
|
89
|
+
<html><head>
|
|
90
|
+
<meta charset="utf-8">
|
|
91
|
+
<style>
|
|
92
|
+
* { margin: 0; padding: 0; box-sizing: border-box; }
|
|
93
|
+
body { background: #0f172a; width: 100vw; height: 100vh; overflow: hidden; }
|
|
94
|
+
canvas { display: block; }
|
|
95
|
+
#error-overlay {
|
|
96
|
+
position: fixed; bottom: 0; left: 0; right: 0;
|
|
97
|
+
background: rgba(220,38,38,0.93); color: #fef2f2;
|
|
98
|
+
font: 12px/1.6 monospace; padding: 10px 14px;
|
|
99
|
+
white-space: pre-wrap; word-break: break-all;
|
|
100
|
+
border-top: 1px solid rgba(248,113,113,0.4);
|
|
101
|
+
}
|
|
102
|
+
</style>
|
|
103
|
+
</head>
|
|
104
|
+
<body>
|
|
105
|
+
<canvas id="canvas"></canvas>
|
|
106
|
+
<script>
|
|
107
|
+
const canvas = document.getElementById('canvas');
|
|
108
|
+
canvas.width = window.innerWidth;
|
|
109
|
+
canvas.height = window.innerHeight;
|
|
110
|
+
const ctx = canvas.getContext('2d');
|
|
111
|
+
|
|
112
|
+
let _rafId = null;
|
|
113
|
+
const _origRAF = requestAnimationFrame;
|
|
114
|
+
window.requestAnimationFrame = function(cb) {
|
|
115
|
+
_rafId = _origRAF(cb);
|
|
116
|
+
return _rafId;
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
window.addEventListener('resize', () => {
|
|
120
|
+
canvas.width = window.innerWidth;
|
|
121
|
+
canvas.height = window.innerHeight;
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
try {
|
|
125
|
+
${code}
|
|
126
|
+
} catch (e) {
|
|
127
|
+
if (_rafId !== null) { cancelAnimationFrame(_rafId); _rafId = null; }
|
|
128
|
+
const div = document.createElement('div');
|
|
129
|
+
div.id = 'error-overlay';
|
|
130
|
+
div.textContent = '\\u26a0\\ufe0f ' + (e.stack || e.message);
|
|
131
|
+
document.body.appendChild(div);
|
|
132
|
+
}
|
|
133
|
+
</script>
|
|
134
|
+
</body></html>`;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// ─── Component ────────────────────────────────────────────────────────────────
|
|
138
|
+
|
|
139
|
+
export default function CanvasLabModal() {
|
|
140
|
+
const {
|
|
141
|
+
closeCanvasLab,
|
|
142
|
+
canvasLabInitialCode,
|
|
143
|
+
canvasLabInitialFileId,
|
|
144
|
+
currentQuestion,
|
|
145
|
+
saveCodeSnippetToQuestion,
|
|
146
|
+
overwriteContextFileContent,
|
|
147
|
+
} = useStore();
|
|
148
|
+
|
|
149
|
+
// ── Editor state ──────────────────────────────────────────
|
|
150
|
+
const [code, setCode] = useState(canvasLabInitialCode ?? DEFAULT_CODE);
|
|
151
|
+
const [fileId, setFileId] = useState<string | null>(
|
|
152
|
+
canvasLabInitialFileId ?? null,
|
|
153
|
+
);
|
|
154
|
+
|
|
155
|
+
// ── Preview state ─────────────────────────────────────────
|
|
156
|
+
const [srcdoc, setSrcdoc] = useState(() =>
|
|
157
|
+
buildSrcdoc(canvasLabInitialCode ?? DEFAULT_CODE),
|
|
158
|
+
);
|
|
159
|
+
const [runKey, setRunKey] = useState(0);
|
|
160
|
+
|
|
161
|
+
// ── Save state ────────────────────────────────────────────
|
|
162
|
+
const [saving, setSaving] = useState(false);
|
|
163
|
+
const [saved, setSaved] = useState(false);
|
|
164
|
+
|
|
165
|
+
// ── Editor / preview split ────────────────────────────────
|
|
166
|
+
const [editorFrac, setEditorFrac] = useState(0.5);
|
|
167
|
+
const splitRef = useRef<HTMLDivElement>(null);
|
|
168
|
+
const draggingDivider = useRef(false);
|
|
169
|
+
|
|
170
|
+
const onDividerMouseDown = useCallback((e: React.MouseEvent) => {
|
|
171
|
+
e.preventDefault();
|
|
172
|
+
draggingDivider.current = true;
|
|
173
|
+
const onMove = (ev: MouseEvent) => {
|
|
174
|
+
if (!splitRef.current) return;
|
|
175
|
+
const rect = splitRef.current.getBoundingClientRect();
|
|
176
|
+
const frac = Math.max(
|
|
177
|
+
MIN_EDITOR_FRAC,
|
|
178
|
+
Math.min(MAX_EDITOR_FRAC, (ev.clientX - rect.left) / rect.width),
|
|
179
|
+
);
|
|
180
|
+
setEditorFrac(frac);
|
|
181
|
+
};
|
|
182
|
+
const onUp = () => {
|
|
183
|
+
draggingDivider.current = false;
|
|
184
|
+
window.removeEventListener("mousemove", onMove);
|
|
185
|
+
window.removeEventListener("mouseup", onUp);
|
|
186
|
+
};
|
|
187
|
+
window.addEventListener("mousemove", onMove);
|
|
188
|
+
window.addEventListener("mouseup", onUp);
|
|
189
|
+
}, []);
|
|
190
|
+
|
|
191
|
+
// ── Window drag + resize ──────────────────────────────────
|
|
192
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
193
|
+
const [pos, setPos] = useState({
|
|
194
|
+
x: Math.round((window.innerWidth - DEFAULT_W) / 2),
|
|
195
|
+
y: Math.round((window.innerHeight - DEFAULT_H) / 2),
|
|
196
|
+
});
|
|
197
|
+
const [size, setSize] = useState({ w: DEFAULT_W, h: DEFAULT_H });
|
|
198
|
+
const [maximized, setMaximized] = useState(false);
|
|
199
|
+
const [prevGeom, setPrevGeom] = useState<{
|
|
200
|
+
x: number;
|
|
201
|
+
y: number;
|
|
202
|
+
w: number;
|
|
203
|
+
h: number;
|
|
204
|
+
} | null>(null);
|
|
205
|
+
|
|
206
|
+
const drag = useRef<{
|
|
207
|
+
startX: number;
|
|
208
|
+
startY: number;
|
|
209
|
+
origX: number;
|
|
210
|
+
origY: number;
|
|
211
|
+
} | null>(null);
|
|
212
|
+
const resize = useRef<{
|
|
213
|
+
dir: ResizeDir;
|
|
214
|
+
startX: number;
|
|
215
|
+
startY: number;
|
|
216
|
+
origX: number;
|
|
217
|
+
origY: number;
|
|
218
|
+
origW: number;
|
|
219
|
+
origH: number;
|
|
220
|
+
} | null>(null);
|
|
221
|
+
|
|
222
|
+
const onHeaderMouseDown = useCallback(
|
|
223
|
+
(e: React.MouseEvent) => {
|
|
224
|
+
if (maximized) return;
|
|
225
|
+
e.preventDefault();
|
|
226
|
+
drag.current = {
|
|
227
|
+
startX: e.clientX,
|
|
228
|
+
startY: e.clientY,
|
|
229
|
+
origX: pos.x,
|
|
230
|
+
origY: pos.y,
|
|
231
|
+
};
|
|
232
|
+
const onMove = (ev: MouseEvent) => {
|
|
233
|
+
if (!drag.current) return;
|
|
234
|
+
setPos({
|
|
235
|
+
x: Math.max(0, drag.current.origX + ev.clientX - drag.current.startX),
|
|
236
|
+
y: Math.max(0, drag.current.origY + ev.clientY - drag.current.startY),
|
|
237
|
+
});
|
|
238
|
+
};
|
|
239
|
+
const onUp = () => {
|
|
240
|
+
drag.current = null;
|
|
241
|
+
window.removeEventListener("mousemove", onMove);
|
|
242
|
+
window.removeEventListener("mouseup", onUp);
|
|
243
|
+
};
|
|
244
|
+
window.addEventListener("mousemove", onMove);
|
|
245
|
+
window.addEventListener("mouseup", onUp);
|
|
246
|
+
},
|
|
247
|
+
[maximized, pos],
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
const onResizeMouseDown = useCallback(
|
|
251
|
+
(e: React.MouseEvent, dir: ResizeDir) => {
|
|
252
|
+
if (maximized) return;
|
|
253
|
+
e.preventDefault();
|
|
254
|
+
e.stopPropagation();
|
|
255
|
+
resize.current = {
|
|
256
|
+
dir,
|
|
257
|
+
startX: e.clientX,
|
|
258
|
+
startY: e.clientY,
|
|
259
|
+
origX: pos.x,
|
|
260
|
+
origY: pos.y,
|
|
261
|
+
origW: size.w,
|
|
262
|
+
origH: size.h,
|
|
263
|
+
};
|
|
264
|
+
const onMove = (ev: MouseEvent) => {
|
|
265
|
+
if (!resize.current) return;
|
|
266
|
+
const dx = ev.clientX - resize.current.startX;
|
|
267
|
+
const dy = ev.clientY - resize.current.startY;
|
|
268
|
+
const { dir: d, origX, origY, origW, origH } = resize.current;
|
|
269
|
+
let nx = origX,
|
|
270
|
+
ny = origY,
|
|
271
|
+
nw = origW,
|
|
272
|
+
nh = origH;
|
|
273
|
+
if (d?.includes("e")) nw = Math.max(MIN_W, origW + dx);
|
|
274
|
+
if (d?.includes("s")) nh = Math.max(MIN_H, origH + dy);
|
|
275
|
+
if (d?.includes("w")) {
|
|
276
|
+
nw = Math.max(MIN_W, origW - dx);
|
|
277
|
+
nx = origX + (origW - nw);
|
|
278
|
+
}
|
|
279
|
+
if (d?.includes("n")) {
|
|
280
|
+
nh = Math.max(MIN_H, origH - dy);
|
|
281
|
+
ny = origY + (origH - nh);
|
|
282
|
+
}
|
|
283
|
+
setPos({ x: nx, y: ny });
|
|
284
|
+
setSize({ w: nw, h: nh });
|
|
285
|
+
};
|
|
286
|
+
const onUp = () => {
|
|
287
|
+
resize.current = null;
|
|
288
|
+
window.removeEventListener("mousemove", onMove);
|
|
289
|
+
window.removeEventListener("mouseup", onUp);
|
|
290
|
+
};
|
|
291
|
+
window.addEventListener("mousemove", onMove);
|
|
292
|
+
window.addEventListener("mouseup", onUp);
|
|
293
|
+
},
|
|
294
|
+
[maximized, pos, size],
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
const toggleMaximize = useCallback(() => {
|
|
298
|
+
if (maximized) {
|
|
299
|
+
const g = prevGeom ?? { x: pos.x, y: pos.y, w: size.w, h: size.h };
|
|
300
|
+
setPos({ x: g.x, y: g.y });
|
|
301
|
+
setSize({ w: g.w, h: g.h });
|
|
302
|
+
setMaximized(false);
|
|
303
|
+
} else {
|
|
304
|
+
setPrevGeom({ x: pos.x, y: pos.y, w: size.w, h: size.h });
|
|
305
|
+
setPos({ x: 0, y: 0 });
|
|
306
|
+
setSize({ w: window.innerWidth, h: window.innerHeight });
|
|
307
|
+
setMaximized(true);
|
|
308
|
+
}
|
|
309
|
+
}, [maximized, prevGeom, pos, size]);
|
|
310
|
+
|
|
311
|
+
// ── Keyboard ──────────────────────────────────────────────
|
|
312
|
+
useEffect(() => {
|
|
313
|
+
const handler = (e: KeyboardEvent) => {
|
|
314
|
+
if (e.key === "Escape") closeCanvasLab();
|
|
315
|
+
};
|
|
316
|
+
window.addEventListener("keydown", handler);
|
|
317
|
+
return () => window.removeEventListener("keydown", handler);
|
|
318
|
+
}, [closeCanvasLab]);
|
|
319
|
+
|
|
320
|
+
// ── Run ───────────────────────────────────────────────────
|
|
321
|
+
const handleRun = () => {
|
|
322
|
+
setSrcdoc(buildSrcdoc(code));
|
|
323
|
+
setRunKey((k) => k + 1);
|
|
324
|
+
};
|
|
325
|
+
|
|
326
|
+
// ── Save ──────────────────────────────────────────────────
|
|
327
|
+
const handleSave = async () => {
|
|
328
|
+
if (!currentQuestion) return;
|
|
329
|
+
setSaving(true);
|
|
330
|
+
try {
|
|
331
|
+
if (fileId) {
|
|
332
|
+
await overwriteContextFileContent(currentQuestion.id, fileId, code);
|
|
333
|
+
} else {
|
|
334
|
+
const cf = await saveCodeSnippetToQuestion(
|
|
335
|
+
currentQuestion.id,
|
|
336
|
+
code,
|
|
337
|
+
"javascript",
|
|
338
|
+
"Canvas Lab",
|
|
339
|
+
"canvas",
|
|
340
|
+
);
|
|
341
|
+
setFileId(cf.id);
|
|
342
|
+
}
|
|
343
|
+
setSaved(true);
|
|
344
|
+
setTimeout(() => setSaved(false), 2000);
|
|
345
|
+
} finally {
|
|
346
|
+
setSaving(false);
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
// ── Reset ─────────────────────────────────────────────────
|
|
351
|
+
const handleReset = () => {
|
|
352
|
+
if (
|
|
353
|
+
!window.confirm(
|
|
354
|
+
"Reset to default starter code? Your current code will be lost.",
|
|
355
|
+
)
|
|
356
|
+
)
|
|
357
|
+
return;
|
|
358
|
+
setCode(DEFAULT_CODE);
|
|
359
|
+
setSrcdoc(buildSrcdoc(DEFAULT_CODE));
|
|
360
|
+
setRunKey((k) => k + 1);
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
const style: React.CSSProperties = maximized
|
|
364
|
+
? { position: "fixed", inset: 0, width: "100vw", height: "100vh" }
|
|
365
|
+
: {
|
|
366
|
+
position: "fixed",
|
|
367
|
+
left: pos.x,
|
|
368
|
+
top: pos.y,
|
|
369
|
+
width: size.w,
|
|
370
|
+
height: size.h,
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
const HANDLE = "absolute z-10";
|
|
374
|
+
|
|
375
|
+
return (
|
|
376
|
+
<div
|
|
377
|
+
ref={containerRef}
|
|
378
|
+
style={style}
|
|
379
|
+
className="z-[200] flex flex-col bg-slate-900 border border-slate-700 rounded-xl shadow-2xl overflow-hidden"
|
|
380
|
+
>
|
|
381
|
+
{/* Resize handles */}
|
|
382
|
+
{!maximized && (
|
|
383
|
+
<>
|
|
384
|
+
<div
|
|
385
|
+
className={`${HANDLE} top-0 left-0 right-0 h-1.5 cursor-n-resize`}
|
|
386
|
+
onMouseDown={(e) => onResizeMouseDown(e, "n")}
|
|
387
|
+
/>
|
|
388
|
+
<div
|
|
389
|
+
className={`${HANDLE} bottom-0 left-0 right-0 h-1.5 cursor-s-resize`}
|
|
390
|
+
onMouseDown={(e) => onResizeMouseDown(e, "s")}
|
|
391
|
+
/>
|
|
392
|
+
<div
|
|
393
|
+
className={`${HANDLE} left-0 top-0 bottom-0 w-1.5 cursor-w-resize`}
|
|
394
|
+
onMouseDown={(e) => onResizeMouseDown(e, "w")}
|
|
395
|
+
/>
|
|
396
|
+
<div
|
|
397
|
+
className={`${HANDLE} right-0 top-0 bottom-0 w-1.5 cursor-e-resize`}
|
|
398
|
+
onMouseDown={(e) => onResizeMouseDown(e, "e")}
|
|
399
|
+
/>
|
|
400
|
+
<div
|
|
401
|
+
className={`${HANDLE} top-0 left-0 w-3 h-3 cursor-nw-resize`}
|
|
402
|
+
onMouseDown={(e) => onResizeMouseDown(e, "nw")}
|
|
403
|
+
/>
|
|
404
|
+
<div
|
|
405
|
+
className={`${HANDLE} top-0 right-0 w-3 h-3 cursor-ne-resize`}
|
|
406
|
+
onMouseDown={(e) => onResizeMouseDown(e, "ne")}
|
|
407
|
+
/>
|
|
408
|
+
<div
|
|
409
|
+
className={`${HANDLE} bottom-0 left-0 w-3 h-3 cursor-sw-resize`}
|
|
410
|
+
onMouseDown={(e) => onResizeMouseDown(e, "sw")}
|
|
411
|
+
/>
|
|
412
|
+
<div
|
|
413
|
+
className={`${HANDLE} bottom-0 right-0 w-3 h-3 cursor-se-resize`}
|
|
414
|
+
onMouseDown={(e) => onResizeMouseDown(e, "se")}
|
|
415
|
+
/>
|
|
416
|
+
</>
|
|
417
|
+
)}
|
|
418
|
+
|
|
419
|
+
{/* Toolbar */}
|
|
420
|
+
<div
|
|
421
|
+
className="flex items-center gap-2 px-3 py-2 border-b border-slate-700 bg-slate-900/90 shrink-0 select-none"
|
|
422
|
+
onMouseDown={onHeaderMouseDown}
|
|
423
|
+
style={{ cursor: maximized ? "default" : "move" }}
|
|
424
|
+
>
|
|
425
|
+
{/* Title */}
|
|
426
|
+
<span className="text-[11px] font-bold uppercase tracking-widest text-orange-400 mr-1">
|
|
427
|
+
🎨 Canvas Lab
|
|
428
|
+
</span>
|
|
429
|
+
{fileId && (
|
|
430
|
+
<span className="text-[10px] text-slate-600 italic truncate max-w-[160px]">
|
|
431
|
+
{currentQuestion?.contextFiles?.find((f) => f.id === fileId)
|
|
432
|
+
?.label ?? "Saved"}
|
|
433
|
+
</span>
|
|
434
|
+
)}
|
|
435
|
+
|
|
436
|
+
<div className="flex-1" />
|
|
437
|
+
|
|
438
|
+
{/* Actions */}
|
|
439
|
+
<button
|
|
440
|
+
onClick={handleRun}
|
|
441
|
+
className="flex items-center gap-1 px-2.5 py-1 bg-orange-500/15 border border-orange-500/25 text-orange-300 hover:bg-orange-500/25 rounded text-[11px] font-medium transition-colors"
|
|
442
|
+
title="Run (render canvas)"
|
|
443
|
+
>
|
|
444
|
+
<Play className="w-3 h-3" /> Run
|
|
445
|
+
</button>
|
|
446
|
+
|
|
447
|
+
{currentQuestion && (
|
|
448
|
+
<button
|
|
449
|
+
onClick={handleSave}
|
|
450
|
+
disabled={saving}
|
|
451
|
+
className="flex items-center gap-1 px-2.5 py-1 bg-cyan-500/15 border border-cyan-500/25 text-cyan-300 hover:bg-cyan-500/25 rounded text-[11px] font-medium transition-colors disabled:opacity-50"
|
|
452
|
+
title="Save to question"
|
|
453
|
+
>
|
|
454
|
+
{saving ? (
|
|
455
|
+
<Loader2 className="w-3 h-3 animate-spin" />
|
|
456
|
+
) : saved ? (
|
|
457
|
+
<Check className="w-3 h-3" />
|
|
458
|
+
) : (
|
|
459
|
+
<Save className="w-3 h-3" />
|
|
460
|
+
)}
|
|
461
|
+
{saved ? "Saved" : "Save"}
|
|
462
|
+
</button>
|
|
463
|
+
)}
|
|
464
|
+
|
|
465
|
+
<button
|
|
466
|
+
onClick={handleReset}
|
|
467
|
+
className="flex items-center gap-1 px-2 py-1 text-slate-500 hover:text-slate-300 hover:bg-slate-700 rounded text-[11px] transition-colors"
|
|
468
|
+
title="Reset to default"
|
|
469
|
+
>
|
|
470
|
+
<RotateCcw className="w-3 h-3" />
|
|
471
|
+
</button>
|
|
472
|
+
|
|
473
|
+
<button
|
|
474
|
+
onClick={toggleMaximize}
|
|
475
|
+
className="p-1 rounded text-slate-500 hover:text-slate-300 hover:bg-slate-700 transition-colors"
|
|
476
|
+
title={maximized ? "Restore" : "Maximize"}
|
|
477
|
+
>
|
|
478
|
+
{maximized ? (
|
|
479
|
+
<Minimize2 className="w-3.5 h-3.5" />
|
|
480
|
+
) : (
|
|
481
|
+
<Maximize2 className="w-3.5 h-3.5" />
|
|
482
|
+
)}
|
|
483
|
+
</button>
|
|
484
|
+
|
|
485
|
+
<button
|
|
486
|
+
onClick={closeCanvasLab}
|
|
487
|
+
className="p-1 rounded text-slate-500 hover:text-slate-300 hover:bg-slate-700 transition-colors"
|
|
488
|
+
title="Close"
|
|
489
|
+
>
|
|
490
|
+
<X className="w-3.5 h-3.5" />
|
|
491
|
+
</button>
|
|
492
|
+
</div>
|
|
493
|
+
|
|
494
|
+
{/* Body: editor | divider | preview */}
|
|
495
|
+
<div ref={splitRef} className="flex flex-1 min-h-0 overflow-hidden">
|
|
496
|
+
{/* Code editor */}
|
|
497
|
+
<div
|
|
498
|
+
className="flex flex-col min-w-0 overflow-hidden border-r border-slate-700/60"
|
|
499
|
+
style={{ width: `${editorFrac * 100}%` }}
|
|
500
|
+
>
|
|
501
|
+
{/* Editor header */}
|
|
502
|
+
<div className="flex items-center px-3 py-1.5 border-b border-slate-800 shrink-0">
|
|
503
|
+
<span className="text-[10px] uppercase tracking-wide text-slate-500">
|
|
504
|
+
JavaScript
|
|
505
|
+
</span>
|
|
506
|
+
<span className="ml-2 text-[9px] text-slate-700 italic">
|
|
507
|
+
— <code className="text-slate-600">canvas</code> &{" "}
|
|
508
|
+
<code className="text-slate-600">ctx</code> are pre-defined
|
|
509
|
+
</span>
|
|
510
|
+
</div>
|
|
511
|
+
|
|
512
|
+
<div className="flex-1 min-h-0 overflow-auto bg-slate-950">
|
|
513
|
+
<Editor
|
|
514
|
+
value={code}
|
|
515
|
+
onValueChange={setCode}
|
|
516
|
+
highlight={(src) =>
|
|
517
|
+
Prism.highlight(src, Prism.languages.javascript, "javascript")
|
|
518
|
+
}
|
|
519
|
+
padding={14}
|
|
520
|
+
style={{
|
|
521
|
+
fontFamily: '"Fira Code", "Fira Mono", monospace',
|
|
522
|
+
fontSize: 13,
|
|
523
|
+
lineHeight: 1.6,
|
|
524
|
+
minHeight: "100%",
|
|
525
|
+
color: "#e2e8f0",
|
|
526
|
+
background: "transparent",
|
|
527
|
+
}}
|
|
528
|
+
textareaClassName="focus:outline-none"
|
|
529
|
+
onKeyDown={(e) => {
|
|
530
|
+
if ((e.metaKey || e.ctrlKey) && e.key === "Enter") {
|
|
531
|
+
e.preventDefault();
|
|
532
|
+
handleRun();
|
|
533
|
+
}
|
|
534
|
+
if ((e.metaKey || e.ctrlKey) && e.key === "s") {
|
|
535
|
+
e.preventDefault();
|
|
536
|
+
handleSave();
|
|
537
|
+
}
|
|
538
|
+
}}
|
|
539
|
+
/>
|
|
540
|
+
</div>
|
|
541
|
+
|
|
542
|
+
{/* Editor footer hint */}
|
|
543
|
+
<div className="px-3 py-1 border-t border-slate-800 shrink-0 flex justify-between">
|
|
544
|
+
<span className="text-[9px] text-slate-700">⌘ Enter — Run</span>
|
|
545
|
+
<span className="text-[9px] text-slate-700">⌘ S — Save</span>
|
|
546
|
+
</div>
|
|
547
|
+
</div>
|
|
548
|
+
|
|
549
|
+
{/* Drag divider */}
|
|
550
|
+
<div
|
|
551
|
+
className="w-2 flex items-center justify-center bg-slate-800/40 hover:bg-slate-700/60 cursor-col-resize shrink-0 transition-colors"
|
|
552
|
+
onMouseDown={onDividerMouseDown}
|
|
553
|
+
>
|
|
554
|
+
<GripVertical className="w-3 h-3 text-slate-600" />
|
|
555
|
+
</div>
|
|
556
|
+
|
|
557
|
+
{/* Canvas preview */}
|
|
558
|
+
<div className="flex-1 flex flex-col min-w-0 bg-slate-950">
|
|
559
|
+
{/* Preview header */}
|
|
560
|
+
<div className="flex items-center justify-between px-3 py-1.5 border-b border-slate-800 shrink-0">
|
|
561
|
+
<span className="text-[10px] uppercase tracking-wide text-slate-500">
|
|
562
|
+
Preview
|
|
563
|
+
</span>
|
|
564
|
+
<button
|
|
565
|
+
onClick={handleRun}
|
|
566
|
+
className="text-[9px] text-orange-400/70 hover:text-orange-400 transition-colors"
|
|
567
|
+
title="Re-run"
|
|
568
|
+
>
|
|
569
|
+
<Play className="w-3 h-3 inline mr-0.5" />
|
|
570
|
+
Re-run
|
|
571
|
+
</button>
|
|
572
|
+
</div>
|
|
573
|
+
|
|
574
|
+
<iframe
|
|
575
|
+
key={runKey}
|
|
576
|
+
srcDoc={srcdoc}
|
|
577
|
+
sandbox="allow-scripts"
|
|
578
|
+
className="flex-1 w-full border-0"
|
|
579
|
+
title="Canvas preview"
|
|
580
|
+
/>
|
|
581
|
+
</div>
|
|
582
|
+
</div>
|
|
583
|
+
</div>
|
|
584
|
+
);
|
|
585
|
+
}
|