Spaces:
Running
Running
File size: 18,132 Bytes
e9d5d73 afe3f6e e9d5d73 afe3f6e e9d5d73 622d850 e9d5d73 622d850 e9d5d73 622d850 e9d5d73 afe3f6e e9d5d73 622d850 e9d5d73 eb6a0af afe3f6e b2c7a5b afe3f6e b2c7a5b afe3f6e e9d5d73 622d850 e9d5d73 b2c7a5b e9d5d73 622d850 e9d5d73 afe3f6e e9d5d73 afe3f6e e9d5d73 afe3f6e 622d850 e9d5d73 622d850 e9d5d73 afe3f6e 622d850 e9d5d73 afe3f6e e9d5d73 b2c7a5b e9d5d73 b2c7a5b e9d5d73 b2c7a5b e9d5d73 b2c7a5b e9d5d73 b2c7a5b e9d5d73 | 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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 | class StateFlowBuilder {
constructor() {
this.states = [];
this.connections = [];
this.stateIdCounter = 0;
this.connectionIdCounter = 0;
this.isDragging = false;
this.connectingFrom = null;
this.tempConnection = null;
this.draggedElement = null;
this.dragOffset = { x: 0, y: 0 };
this.isConnecting = false;
this.stateTemplates = [
{ text: 'Eingabemaske Drucker', icon: 'printer', color: 'blue' },
{ text: 'Drucker überprüfen', icon: 'check-circle', color: 'green' },
{ text: 'Dokument verarbeiten', icon: 'file-text', color: 'purple' },
{ text: 'Warten auf Bestätigung', icon: 'clock', color: 'yellow' },
{ text: 'Fehlerbehandlung', icon: 'alert-triangle', color: 'red' },
{ text: 'Abschließen', icon: 'check-square', color: 'indigo' },
{ text: 'Benachrichtigung senden', icon: 'mail', color: 'pink' },
{ text: 'Daten speichern', icon: 'save', color: 'teal' },
];
this.init();
}
init() {
this.setupCanvas();
this.renderTemplates();
this.setupEventListeners();
this.updateConnectionLines();
}
setupCanvas() {
const canvas = document.getElementById('canvas');
canvas.classList.add('canvas-grid');
}
renderTemplates() {
const container = document.getElementById('stateTemplates');
container.innerHTML = '';
this.stateTemplates.forEach((template, index) => {
const templateEl = document.createElement('div');
templateEl.className = `state-template p-4 bg-white border-2 border-gray-200 rounded-lg hover:border-indigo-400`;
templateEl.draggable = true;
templateEl.dataset.templateIndex = index;
templateEl.innerHTML = `
<div class="flex items-center gap-3">
<div class="w-10 h-10 bg-${template.color}-100 rounded-lg flex items-center justify-center">
<i data-feather="${template.icon}" class="w-5 h-5 text-${template.color}-600"></i>
</div>
<span class="text-gray-800 font-medium">${template.text}</span>
</div>
`;
container.appendChild(templateEl);
templateEl.addEventListener('dragstart', (e) => {
e.dataTransfer.effectAllowed = 'copy';
e.dataTransfer.setData('templateIndex', index);
});
});
feather.replace();
}
setupEventListeners() {
// Canvas drag and drop
const canvas = document.getElementById('canvas');
const canvasContent = document.getElementById('canvasContent');
canvas.addEventListener('dragover', (e) => {
e.preventDefault();
e.dataTransfer.dropEffect = 'copy';
});
canvas.addEventListener('drop', (e) => {
e.preventDefault();
const templateIndex = parseInt(e.dataTransfer.getData('templateIndex'));
const rect = canvasContent.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
this.addState(templateIndex, x, y);
});
// Clear canvas
document.getElementById('clearCanvas').addEventListener('click', () => {
if (confirm('Möchtest du wirklich alle Zustände und Verbindungen löschen?')) {
this.clearCanvas();
}
});
// Global mouse events
document.addEventListener('mousemove', (e) => this.handleMouseMove(e));
document.addEventListener('mouseup', () => this.handleMouseUp());
}
addState(templateIndex, x, y) {
const template = this.stateTemplates[templateIndex];
const state = {
id: this.stateIdCounter++,
x: x - 100,
y: y - 30,
width: 200,
height: 60,
...template
};
this.states.push(state);
this.renderState(state);
this.showToast('Zustand hinzugefügt', 'success');
}
renderState(state) {
const canvasContent = document.getElementById('canvasContent');
const stateEl = document.createElement('div');
stateEl.id = `state-${state.id}`;
stateEl.className = `state-block absolute bg-white border-2 border-gray-300 rounded-lg shadow-lg p-4 flex items-center gap-3`;
stateEl.style.left = `${state.x}px`;
stateEl.style.top = `${state.y}px`;
stateEl.style.width = `${state.width}px`;
stateEl.innerHTML = `
<div class="connection-point absolute -left-2 top-1/2 -translate-y-1/2 w-4 h-4 bg-indigo-500 rounded-full cursor-pointer" data-type="input" data-state="${state.id}"></div>
<div class="connection-point absolute -right-2 top-1/2 -translate-y-1/2 w-4 h-4 bg-indigo-500 rounded-full cursor-pointer" data-type="output" data-state="${state.id}"></div>
<div class="w-10 h-10 bg-${state.color}-100 rounded-lg flex items-center justify-center flex-shrink-0">
<i data-feather="${state.icon}" class="w-5 h-5 text-${state.color}-600"></i>
</div>
<span class="text-gray-800 font-medium truncate">${state.text}</span>
<button class="ml-auto opacity-0 hover:opacity-100 transition-opacity" onclick="stateBuilder.removeState(${state.id})">
<i data-feather="x" class="w-4 h-4 text-gray-400 hover:text-red-500"></i>
</button>
`;
canvasContent.appendChild(stateEl);
// Setup drag functionality
stateEl.addEventListener('mousedown', (e) => this.startDrag(e, state));
// Setup connection points
const connectionPoints = stateEl.querySelectorAll('.connection-point');
connectionPoints.forEach(point => {
// Use mousedown instead of click for drag-to-connect
point.addEventListener('mousedown', (e) => {
e.stopPropagation();
const handled = this.handleConnectionPoint(point);
if (handled) {
e.preventDefault();
}
});
// Also handle mouseup for connection completion
point.addEventListener('mouseup', (e) => {
if (this.isConnecting) {
e.stopPropagation();
this.handleConnectionPoint(point);
}
});
// Visual feedback on hover during connection
point.addEventListener('mouseenter', (e) => {
if (this.isConnecting && point.dataset.type === 'input' && parseInt(point.dataset.state) !== this.connectingFrom.stateId) {
point.classList.add('hover-target');
}
});
point.addEventListener('mouseleave', (e) => {
point.classList.remove('hover-target');
});
});
feather.replace();
}
startDrag(e, state) {
if (this.isConnecting || e.target.classList.contains('connection-point')) return;
this.isDragging = true;
this.draggedElement = document.getElementById(`state-${state.id}`);
this.draggedElement.classList.add('dragging');
const rect = this.draggedElement.getBoundingClientRect();
const canvasRect = document.getElementById('canvasContent').getBoundingClientRect();
this.dragOffset = {
x: e.clientX - rect.left,
y: e.clientY - rect.top
};
}
handleMouseMove(e) {
if (this.isConnecting && this.connectingFrom) {
this.updateTempConnection(e);
return;
}
if (this.isDragging && this.draggedElement) {
const canvasRect = document.getElementById('canvasContent').getBoundingClientRect();
const x = e.clientX - canvasRect.left - this.dragOffset.x;
const y = e.clientY - canvasRect.top - this.dragOffset.y;
this.draggedElement.style.left = `${Math.max(0, x)}px`;
this.draggedElement.style.top = `${Math.max(0, y)}px`;
// Update state position
const stateId = parseInt(this.draggedElement.id.replace('state-', ''));
const state = this.states.find(s => s.id === stateId);
if (state) {
state.x = Math.max(0, x);
state.y = Math.max(0, y);
}
// Throttled connection updates for smoother dragging
if (!this.updateTimeout) {
this.updateTimeout = requestAnimationFrame(() => {
this.updateConnectionLinesForState(stateId);
this.updateTimeout = null;
});
}
}
}
handleMouseUp() {
if (this.draggedElement) {
this.draggedElement.classList.remove('dragging');
}
// Cancel any pending animation frame
if (this.updateTimeout) {
cancelAnimationFrame(this.updateTimeout);
this.updateTimeout = null;
}
this.isDragging = false;
this.draggedElement = null;
}
handleConnectionPoint(point) {
const type = point.dataset.type;
const stateId = parseInt(point.dataset.state);
if (!this.isConnecting) {
// Start connection
if (type === 'output') {
this.isConnecting = true;
this.connectingFrom = { stateId, type, element: point };
point.classList.add('valid-target');
// Create temp connection line
const svg = document.getElementById('connectionsSvg');
this.tempConnection = document.createElementNS('http://www.w3.org/2000/svg', 'line');
this.tempConnection.classList.add('connection-temp');
svg.appendChild(this.tempConnection);
return true; // Prevent default drag behavior
} else {
this.showToast('Verbindungen starten nur von Ausgängen', 'error');
return false;
}
} else {
// Complete connection
if (type === 'input' && stateId !== this.connectingFrom.stateId) {
this.addConnection(this.connectingFrom.stateId, stateId);
this.showToast('Verbindung hergestellt', 'success');
} else if (type === 'output') {
this.showToast('Verbindungen enden an Eingängen', 'error');
} else {
this.showToast('Keine Selbstverbindungen erlaubt', 'error');
}
// Reset
this.connectingFrom.element.classList.remove('valid-target');
if (this.tempConnection) {
this.tempConnection.remove();
this.tempConnection = null;
}
this.connectingFrom = null;
this.isConnecting = false;
return true;
}
}
updateTempConnection(e) {
if (!this.tempConnection || !this.connectingFrom) return;
const fromPoint = this.getConnectionPointPosition(this.connectingFrom.stateId, this.connectingFrom.type);
const canvasRect = document.getElementById('canvasContent').getBoundingClientRect();
const toX = e.clientX - canvasRect.left;
const toY = e.clientY - canvasRect.top;
this.tempConnection.setAttribute('x1', fromPoint.x);
this.tempConnection.setAttribute('y1', fromPoint.y);
this.tempConnection.setAttribute('x2', toX);
this.tempConnection.setAttribute('y2', toY);
}
addConnection(fromStateId, toStateId) {
// Check if connection already exists
const exists = this.connections.some(c =>
c.from === fromStateId && c.to === toStateId
);
if (exists) {
this.showToast('Diese Verbindung existiert bereits', 'warning');
return;
}
this.connections.push({
id: this.connectionIdCounter++,
from: fromStateId,
to: toStateId
});
this.updateConnectionLines();
}
updateConnectionLines() {
const svg = document.getElementById('connectionsSvg');
const existingLines = svg.querySelectorAll('.connection-line');
existingLines.forEach(line => line.remove());
this.connections.forEach(connection => {
this.createConnectionLine(connection);
});
}
updateConnectionLinesForState(stateId) {
const svg = document.getElementById('connectionsSvg');
this.connections.forEach(connection => {
if (connection.from === stateId || connection.to === stateId) {
// Find existing line for this connection
const existingLine = svg.querySelector(`[data-connection-id="${connection.id}"]`);
if (existingLine) {
// Update existing line position
const fromPoint = this.getConnectionPointPosition(connection.from, 'output');
const toPoint = this.getConnectionPointPosition(connection.to, 'input');
existingLine.setAttribute('x1', fromPoint.x);
existingLine.setAttribute('y1', fromPoint.y);
existingLine.setAttribute('x2', toPoint.x);
existingLine.setAttribute('y2', toPoint.y);
} else {
// Create new line if not found
this.createConnectionLine(connection);
}
}
});
}
createConnectionLine(connection) {
const svg = document.getElementById('connectionsSvg');
const fromPoint = this.getConnectionPointPosition(connection.from, 'output');
const toPoint = this.getConnectionPointPosition(connection.to, 'input');
const line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
line.classList.add('connection-line');
line.setAttribute('data-connection-id', connection.id);
line.setAttribute('x1', fromPoint.x);
line.setAttribute('y1', fromPoint.y);
line.setAttribute('x2', toPoint.x);
line.setAttribute('y2', toPoint.y);
line.setAttribute('marker-end', 'url(#arrowhead)');
line.style.pointerEvents = 'stroke';
// Add click handler to remove connection
line.addEventListener('click', () => {
if (confirm('Verbindung entfernen?')) {
this.removeConnection(connection.id);
}
});
svg.appendChild(line);
}
getConnectionPointPosition(stateId, type) {
const stateEl = document.getElementById(`state-${stateId}`);
const rect = stateEl.getBoundingClientRect();
const canvasRect = document.getElementById('canvasContent').getBoundingClientRect();
const point = stateEl.querySelector(`.connection-point[data-type="${type}"]`);
const pointRect = point.getBoundingClientRect();
return {
x: pointRect.left + pointRect.width / 2 - canvasRect.left,
y: pointRect.top + pointRect.height / 2 - canvasRect.top
};
}
removeState(stateId) {
// Remove related connections
this.connections = this.connections.filter(c => c.from !== stateId && c.to !== stateId);
// Remove state
this.states = this.states.filter(s => s.id !== stateId);
// Remove element
document.getElementById(`state-${stateId}`).remove();
this.updateConnectionLines();
this.showToast('Zustand entfernt', 'info');
}
removeConnection(connectionId) {
this.connections = this.connections.filter(c => c.id !== connectionId);
// Remove only the specific connection line
const svg = document.getElementById('connectionsSvg');
const lineToRemove = svg.querySelector(`[data-connection-id="${connectionId}"]`);
if (lineToRemove) {
lineToRemove.remove();
}
this.showToast('Verbindung entfernt', 'info');
}
clearCanvas() {
this.states = [];
this.connections = [];
this.stateIdCounter = 0;
this.connectionIdCounter = 0;
document.getElementById('canvasContent').innerHTML = '';
document.getElementById('connectionsSvg').querySelectorAll('.connection-line').forEach(line => line.remove());
this.showToast('Canvas geleert', 'info');
}
showToast(message, type = 'info') {
const container = document.getElementById('toastContainer');
const toast = document.createElement('div');
const colors = {
success: 'bg-green-500',
error: 'bg-red-500',
warning: 'bg-yellow-500',
info: 'bg-blue-500'
};
toast.className = `toast ${colors[type]} text-white px-4 py-3 rounded-lg shadow-lg flex items-center gap-2`;
toast.innerHTML = `
<i data-feather="${type === 'success' ? 'check-circle' : type === 'error' ? 'x-circle' : type === 'warning' ? 'alert-circle' : 'info'}" class="w-4 h-4"></i>
<span>${message}</span>
`;
container.appendChild(toast);
feather.replace();
setTimeout(() => {
toast.style.animation = 'slideIn 0.3s ease-out reverse';
setTimeout(() => toast.remove(), 300);
}, 3000);
}
}
// Initialize the application
const stateBuilder = new StateFlowBuilder(); |