Spaces:
Sleeping
Sleeping
File size: 10,102 Bytes
e0139c6 fa14350 e0139c6 fa14350 e0139c6 39d7539 e0139c6 3ff344a e0139c6 5d1d174 3ff344a e0139c6 fa14350 e0139c6 fa14350 e0139c6 fa14350 39d7539 837c995 5d1d174 5b20ea5 837c995 39d7539 5b20ea5 fa14350 5b20ea5 39d7539 837c995 5b20ea5 837c995 e0139c6 837c995 39d7539 fa14350 39d7539 e0139c6 5b20ea5 e0139c6 39d7539 5b20ea5 ad2d527 5b20ea5 ad2d527 5b20ea5 ad2d527 e0139c6 fa14350 e0139c6 39d7539 e0139c6 5d1d174 ccd3bcf 5b20ea5 e0139c6 fa14350 ccd3bcf 5b20ea5 e0139c6 39d7539 fa14350 39d7539 fa14350 39d7539 fa14350 39d7539 ccd3bcf fa14350 ccd3bcf fa14350 39d7539 ccd3bcf fa14350 39d7539 5b20ea5 fa14350 39d7539 fa14350 e0139c6 39d7539 e0139c6 ccd3bcf 39d7539 fa14350 ccd3bcf e0139c6 5b20ea5 ccd3bcf 5b20ea5 fa14350 39d7539 fa14350 e0139c6 39d7539 3ff344a 39d7539 e0139c6 3ff344a e0139c6 fa14350 |
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 |
// --- Globals & Config ---
const CONFIG = {
viewportPadding: 200,
nodeWidth: 200,
nodeHeight: 50,
indentWidth: 60,
rowHeight: 80
};
let currentTab = 'visualizer';
let sourceLines = [];
let allNodes = [];
let isTicking = false;
// --- Konva Setup ---
const stage = new Konva.Stage({
container: 'container',
width: window.innerWidth - 400, // Adjust for sidebar width
height: window.innerHeight,
draggable: true
});
const layer = new Konva.Layer();
stage.add(layer);
// --- UI Logic: Tabs ---
function switchTab(tabId) {
document.querySelectorAll('.tab-pane').forEach(el => el.classList.remove('active'));
document.getElementById(`tab-${tabId}`).classList.add('active');
document.querySelectorAll('.nav-item').forEach(el => el.classList.remove('active'));
// Find the button that triggered this or match by ID if needed
const btn = document.querySelector(`button[onclick="switchTab('${tabId}')"]`);
if(btn) btn.classList.add('active');
currentTab = tabId;
if (tabId === 'dataset') {
loadDatasetTable();
}
}
// --- UI Logic: Status ---
function setStatus(msg, state) {
const el = document.getElementById('statusIndicator');
el.innerText = msg;
el.className = '';
if(state === 'ready') el.classList.add('status-ready');
if(state === 'working') el.classList.add('status-working');
if(state === 'success') el.classList.add('status-success');
if(state === 'error') el.classList.add('status-error');
}
// --- Core: Culling & Rendering Optimization ---
function updateVisibleNodes() {
isTicking = false;
const scale = stage.scaleX();
const stageX = stage.x();
const stageY = stage.y();
const viewX = -(stageX / scale) - CONFIG.viewportPadding;
const viewY = -(stageY / scale) - CONFIG.viewportPadding;
const viewW = (stage.width() / scale) + (CONFIG.viewportPadding * 2);
const viewH = (stage.height() / scale) + (CONFIG.viewportPadding * 2);
const viewRight = viewX + viewW;
const viewBottom = viewY + viewH;
let nodesChanged = false;
for (let i = 0; i < allNodes.length; i++) {
const node = allNodes[i];
const isVisible = (
node.x < viewRight &&
node.x + CONFIG.nodeWidth > viewX &&
node.y < viewBottom &&
node.y + CONFIG.nodeHeight > viewY
);
if (node.visible !== isVisible) {
node.group.visible(isVisible);
node.visible = isVisible;
nodesChanged = true;
}
}
if (nodesChanged) {
layer.batchDraw();
}
}
function requestUpdate() {
if (!isTicking) {
requestAnimationFrame(updateVisibleNodes);
isTicking = true;
}
}
// --- Canvas Interactions ---
stage.on('dragmove', requestUpdate);
stage.on('wheel', (e) => {
e.evt.preventDefault();
const oldScale = stage.scaleX();
const pointer = stage.getPointerPosition();
const scaleBy = 1.05;
const newScale = e.evt.deltaY > 0 ? oldScale / scaleBy : oldScale * scaleBy;
stage.scale({ x: newScale, y: newScale });
const newPos = {
x: pointer.x - (pointer.x - stage.x()) / oldScale * newScale,
y: pointer.y - (pointer.y - stage.y()) / oldScale * newScale
};
stage.position(newPos);
requestUpdate();
});
window.addEventListener('resize', () => {
stage.width(window.innerWidth - 400);
stage.height(window.innerHeight);
requestUpdate();
});
// --- API: Visualizer ---
function visualize() {
const code = document.getElementById('codeInput').value;
setStatus('Parsing...', 'working');
fetch('/parse', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ code: code })
})
.then(res => res.json())
.then(data => {
if(data.error) {
alert(data.error);
setStatus('Error', 'error');
} else {
drawGraph(data, code);
setStatus(`Rendered ${data.nodes.length} nodes`, 'ready');
}
})
.catch(err => setStatus('Network Error', 'error'));
}
function drawGraph(data, fullSourceCode) {
layer.destroyChildren();
allNodes = [];
sourceLines = fullSourceCode.split('\n');
const nodeMap = {};
const colors = {
'function': '#a29bfe', 'class': '#e84393', 'if': '#fab1a0',
'for': '#fdcb6e', 'while': '#fdcb6e', 'return': '#55efc4',
'assigned_variable': '#74b9ff', 'import': '#b2bec3', 'try': '#ff7675'
};
// Draw Nodes
data.nodes.forEach((node, index) => {
const x = 100 + (node.lvl * CONFIG.indentWidth);
const y = 50 + (index * CONFIG.rowHeight);
const group = new Konva.Group({
x: x,
y: y,
listening: true
});
const rect = new Konva.Rect({
width: CONFIG.nodeWidth,
height: CONFIG.nodeHeight,
fill: '#2d3436',
stroke: colors[node.type] || '#636e72',
strokeWidth: 2,
cornerRadius: 8,
shadowColor: 'black',
shadowBlur: 10,
shadowOpacity: 0.3,
hitStrokeWidth: 0
});
const text = new Konva.Text({
x: 10, y: 10,
text: node.lbl,
fontSize: 14, fontFamily: 'JetBrains Mono', fill: '#fff',
width: 180, ellipsis: true,
listening: false
});
const vecText = new Konva.Text({
x: 10, y: 32,
text: `V:[${node.vec[0]}, ${node.vec[2]}...]`,
fontSize: 10, fontFamily: 'JetBrains Mono', fill: '#636e72',
listening: false
});
group.add(rect);
group.add(text);
group.add(vecText);
// Interaction
group.on('click', () => {
const start = node.loc[0] - 1;
const end = node.loc[1];
const snippet = sourceLines.slice(start, end).join('\n');
console.log(snippet); // Optional: log to console
// Could add a toast or modal to show snippet here
});
group.on('mouseover', () => {
document.body.style.cursor = 'pointer';
rect.fill('#353b48');
layer.batchDraw();
});
group.on('mouseout', () => {
document.body.style.cursor = 'default';
rect.fill('#2d3436');
layer.batchDraw();
});
layer.add(group);
nodeMap[node.id] = { x, y };
allNodes.push({ group, x, y, visible: true });
});
// Draw Connections
data.connections.forEach(conn => {
const f = nodeMap[conn.f];
const t = nodeMap[conn.t];
if (f && t) {
const line = new Konva.Line({
points: [f.x+20, f.y+50, f.x+20, t.y-10, t.x+20, t.y-10, t.x+20, t.y],
stroke: '#636e72',
strokeWidth: 2,
tension: 0.2,
opacity: 0.5,
listening: false
});
layer.add(line);
line.moveToBottom();
}
});
layer.batchDraw();
stage.x(50);
stage.y(50);
requestUpdate();
}
// --- API: Dataset ---
function addToDataset() {
const code = document.getElementById('codeInput').value;
setStatus('Saving...', 'working');
fetch('/dataset/add', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ code: code })
})
.then(res => res.json())
.then(data => {
if(data.status === 'success') {
setStatus('Saved to Dataset!', 'success');
} else {
alert(data.message);
setStatus('Save Failed', 'error');
}
})
.catch(() => setStatus('Network Error', 'error'));
}
function loadDatasetTable() {
const tbody = document.getElementById('datasetTableBody');
tbody.innerHTML = '<tr><td colspan="4">Loading...</td></tr>';
fetch('/dataset/list')
.then(res => res.json())
.then(entries => {
tbody.innerHTML = '';
if(entries.length === 0) {
tbody.innerHTML = '<tr><td colspan="4" style="text-align:center; opacity:0.5">Dataset is empty</td></tr>';
return;
}
entries.forEach(entry => {
const row = `
<tr>
<td class="mono">${entry.id.split('_')[1]}</td>
<td>${new Date(entry.timestamp).toLocaleTimeString()}</td>
<td><span class="badge">${entry.node_count} nodes</span></td>
<td class="mono-dim">${entry.snippet}</td>
</tr>
`;
tbody.innerHTML += row;
});
});
}
// --- API: Hugging Face Upload ---
function openUploadModal() {
document.getElementById('uploadModal').classList.remove('hidden');
}
function closeUploadModal() {
document.getElementById('uploadModal').classList.add('hidden');
}
function performUpload() {
const token = document.getElementById('hfToken').value;
const repo = document.getElementById('hfRepo').value;
if(!token || !repo) return alert("Please fill in both fields");
const btn = document.querySelector('#uploadModal .btn-primary');
const originalText = btn.innerText;
btn.innerText = "Pushing...";
btn.disabled = true;
fetch('/dataset/upload_hf', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ token: token, repo_id: repo })
})
.then(res => res.json())
.then(data => {
btn.innerText = originalText;
btn.disabled = false;
if(data.status === 'success') {
closeUploadModal();
alert(data.message);
} else {
alert("Error: " + data.message);
}
})
.catch(err => {
btn.innerText = originalText;
btn.disabled = false;
alert("Network Error");
});
} |