<!DOCTYPE html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interaktywny Edytor Węzłów</title>
<style>
/* Reset podstawowych stylów */
body, html {
margin: 0;
padding: 0;
height: 100%;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
overflow: hidden; /* Zapobiega przewijaniu całego okna */
display: flex;
flex-direction: column; /* Układ pionowy dla nagłówka i kontenera */
background-color: #f0f0f0;
}
/* Nagłówek */
.header {
background-color: #333;
color: white;
padding: 10px 20px;
display: flex;
justify-content: space-between;
align-items: center;
flex-shrink: 0; /* Zapobiega zmniejszaniu nagłówka */
}
.header h1 {
margin: 0;
font-size: 1.4em;
}
.header-buttons button {
background-color: #4CAF50;
color: white;
border: none;
padding: 8px 15px;
margin-left: 10px;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s;
}
.header-buttons button:hover {
background-color: #45a049;
}
/* Główny kontener dzielący ekran */
#container {
display: flex;
flex-grow: 1; /* Pozwala zająć resztę dostępnej przestrzeni */
height: calc(100vh - 50px); /* Dostosowanie wysokości do nagłówka */
overflow: hidden; /* Zapobiega przewijaniu wewnątrz kontenera */
}
/* Panel podglądu */
#preview {
flex-basis: 50%; /* Początkowa szerokość */
background-color: #e9e9e9;
border-right: 1px solid #ccc;
overflow: hidden; /* Węzły będą pozycjonowane absolutnie wewnątrz */
position: relative; /* Konieczne dla absolutnie pozycjonowanych węzłów */
padding: 15px;
box-sizing: border-box;
}
#preview .preview-node {
position: absolute; /* Pozycjonowanie wg danych z edytora */
background-color: lightblue; /* Domyślny styl podglądu */
border: 1px solid #888;
border-radius: 5px;
opacity: 0.8;
cursor: default;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
box-sizing: border-box;
display: flex;
align-items: center;
justify-content: center;
font-size: 0.9em;
color: #333;
}
/* Panel edytora */
#editor {
flex-basis: 50%; /* Początkowa szerokość */
background-color: #fdfdfd;
overflow: hidden; /* Węzły będą pozycjonowane absolutnie wewnątrz */
position: relative; /* Konieczne dla absolutnie pozycjonowanych węzłów */
display: flex;
flex-direction: column;
}
#editor-canvas {
position: relative;
width: 100%;
height: 100%; /* Zajmuje całą przestrzeń panelu edytora */
overflow: hidden; /* Ważne, jeśli węzły wyjdą poza obszar */
cursor: crosshair; /* Wskazuje, że można tu dodawać elementy */
background-color: #ffffff; /* Białe tło dla obszaru roboczego */
background-image: linear-gradient(to right, #eee 1px, transparent 1px),
linear-gradient(to bottom, #eee 1px, transparent 1px);
background-size: 20px 20px;
}
/* Element dzielący (separator) */
#divider {
width: 5px;
background-color: #ccc;
cursor: col-resize;
flex-shrink: 0; /* Zapobiega zmniejszaniu */
position: relative; /* Potrzebne dla pseudo-elementów lub obsługi zdarzeń */
}
#divider:hover {
background-color: #aaa;
}
/* Style dla pojedynczego węzła w edytorze */
.node {
position: absolute; /* Kluczowe dla drag & drop i pozycjonowania */
background-color: #4CAF50; /* Domyślny kolor */
color: white;
border: 1px solid #3a8f3a;
border-radius: 8px;
padding: 10px;
min-width: 100px;
min-height: 60px;
cursor: move;
box-shadow: 0 4px 8px rgba(0,0,0,0.2);
overflow: hidden; /* Ukrywa zawartość wychodzącą poza węzeł */
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-between;
transition: box-shadow 0.2s ease-in-out;
}
.node:hover {
box-shadow: 0 6px 12px rgba(0,0,0,0.3);
}
.node-header {
font-weight: bold;
margin-bottom: 5px;
cursor: grab; /* Wskazuje, że można chwycić */
width: 100%;
text-align: center;
padding-bottom: 5px;
border-bottom: 1px dashed rgba(255, 255, 255, 0.3);
}
.node-content {
font-size: 0.9em;
flex-grow: 1;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
}
/* Menu kontekstowe */
.context-menu {
display: none; /* Domyślnie ukryte */
position: absolute;
background-color: white;
border: 1px solid #ccc;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
border-radius: 4px;
z-index: 1000;
min-width: 150px;
padding: 5px 0;
list-style: none;
margin: 0;
}
.context-menu.show {
display: block;
}
.context-menu li {
padding: 8px 15px;
cursor: pointer;
transition: background-color 0.2s;
}
.context-menu li:hover {
background-color: #f0f0f0;
}
.context-menu li.separator {
border-top: 1px solid #eee;
margin: 5px 0;
}
/* Panel konfiguracyjny węzła */
.config-panel {
display: none; /* Domyślnie ukryty */
position: absolute;
bottom: 5px;
left: 5px;
background-color: rgba(0, 0, 0, 0.7);
color: white;
padding: 10px;
border-radius: 5px;
font-size: 0.85em;
z-index: 10; /* Nad węzłem, ale pod menu kontekstowym */
width: calc(100% - 20px); /* Dostosowanie do paddingu węzła */
box-sizing: border-box;
}
.config-panel.show {
display: block;
}
.config-panel label {
display: block;
margin-bottom: 5px;
}
.config-panel input[type="color"],
.config-panel input[type="range"],
.config-panel input[type="number"] {
width: calc(100% - 5px); /* Dostosowanie do kontenera */
margin-bottom: 8px;
}
.config-panel button {
background-color: #f44336;
color: white;
border: none;
padding: 5px 10px;
border-radius: 3px;
cursor: pointer;
font-size: 0.9em;
}
.config-panel button:hover {
background-color: #d32f2f;
}
/* Uchwyty do zmiany rozmiaru */
.resizer {
position: absolute;
width: 12px;
height: 12px;
background-color: rgba(76, 175, 80, 0.7);
border: 1px solid rgba(58, 143, 58, 0.8);
border-radius: 3px;
cursor: nwse-resize; /* Kursory dla zmiany rozmiaru */
z-index: 20;
}
.resizer.top-left { top: -6px; left: -6px; cursor: nwse-resize; }
.resizer.top-right { top: -6px; right: -6px; cursor: nesw-resize; }
.resizer.bottom-left { bottom: -6px; left: -6px; cursor: nesw-resize; }
.resizer.bottom-right { bottom: -6px; right: -6px; cursor: nwse-resize; }
/* Dodatkowe uchwyty na środkach krawędzi (opcjonalnie) */
.resizer.top { top: -6px; left: 50%; transform: translateX(-50%); cursor: ns-resize; }
.resizer.bottom { bottom: -6px; left: 50%; transform: translateX(-50%); cursor: ns-resize; }
.resizer.left { top: 50%; left: -6px; transform: translateY(-50%); cursor: ew-resize; }
.resizer.right { top: 50%; right: -6px; transform: translateY(-50%); cursor: ew-resize; }
/* Ikona do otwierania konfiguracji */
.config-toggle {
position: absolute;
top: 5px;
right: 5px;
cursor: pointer;
font-size: 1.1em;
font-weight: bold;
padding: 2px 6px;
background-color: rgba(0,0,0,0.4);
border-radius: 3px;
transition: background-color 0.2s;
z-index: 5;
}
.config-toggle:hover {
background-color: rgba(0,0,0,0.6);
}
/* Styl dla uchwytu nagłówka */
.node-header.dragging {
cursor: grabbing;
}
/* Styl dla uchwytu zmiany rozmiaru */
.resizer.active {
background-color: #007bff;
border-color: #0056b3;
}
/* Style dla połączeń (jeśli zostaną dodane) */
.connections-layer {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none; /* Pozwala klikać przez warstwę połączeń */
z-index: 0; /* Pod węzłami */
}
.connection-line {
stroke: #555;
stroke-width: 2;
stroke-linecap: round;
}
</style>
</head>
<body>
<header class="header">
<h1>Edytor Węzłów z Podglądem na Żywo</h1>
<div class="header-buttons">
<button id="add-node-btn">Dodaj Węzeł</button>
<!-- Dodatkowe przyciski można dodać tutaj -->
<!-- <button id="save-btn">Zapisz</button> -->
<!-- <button id="load-btn">Wczytaj</button> -->
</div>
</header>
<div id="container">
<!-- Lewy panel: Podgląd -->
<div id="preview">
<!-- Tutaj będą renderowane uproszczone wersje węzłów z edytora -->
</div>
<!-- Element dzielący -->
<div id="divider"></div>
<!-- Prawy panel: Edytor -->
<div id="editor">
<div id="editor-canvas">
<!-- Węzły będą dynamicznie dodawane tutaj przez JavaScript -->
</div>
</div>
</div>
<!-- Ukryte menu kontekstowe -->
<ul id="context-menu" class="context-menu">
<li data-action="configure">Konfiguruj</li>
<li data-action="delete">Usuń</li>
<!-- Dodatkowe akcje można dodać tutaj -->
<!-- <li class="separator"></li> -->
<!-- <li>Zmień typ</li> -->
</ul>
<script>
// ----- Stan aplikacji -----
let nodes = []; // Tablica przechowująca dane węzłów
let nodeIdCounter = 0; // Licznik do generowania unikalnych ID
let isDragging = false;
let isResizing = false;
let currentDraggedNode = null;
let currentResizer = null;
let dragOffsetX, dragOffsetY;
let resizeStartX, resizeStartY, resizeStartWidth, resizeStartHeight;
let activeNodeElement = null; // Element węzła, który ma otwarte menu lub jest edytowany
let isResizingSplit = false;
let splitResizeStartX;
// ----- Elementy DOM -----
const editorCanvas = document.getElementById('editor-canvas');
const previewPane = document.getElementById('preview');
const addNodeBtn = document.getElementById('add-node-btn');
const divider = document.getElementById('divider');
const contextMenu = document.getElementById('context-menu');
// ----- Funkcje pomocnicze -----
// Generowanie unikalnego ID dla węzła
function generateNodeId() {
return `node-${nodeIdCounter++}`;
}
// Aktualizacja pozycji i rozmiaru elementu węzła w DOM
function updateNodeElementStyle(nodeData, nodeElement) {
nodeElement.style.left = `${nodeData.x}px`;
nodeElement.style.top = `${nodeData.y}px`;
nodeElement.style.width = `${nodeData.width}px`;
nodeElement.style.height = `${nodeData.height}px`;
nodeElement.style.backgroundColor = nodeData.color;
nodeElement.querySelector('.node-header').textContent = nodeData.label || `Węzeł ${nodeData.id.substring(5)}`;
// nodeElement.querySelector('.node-content').textContent = nodeData.description || ''; // Można dodać opis
}
// Znajduje element węzła w edytorze na podstawie ID
function findNodeElementById(id) {
return document.querySelector(`.node[data-id="${id}"]`);
}
// Aktualizuje dane węzła w tablicy 'nodes'
function updateNodeData(id, changes) {
const nodeIndex = nodes.findIndex(n => n.id === id);
if (nodeIndex !== -1) {
nodes[nodeIndex] = { ...nodes[nodeIndex], ...changes };
// Zaktualizuj styl elementu węzła
const nodeElement = findNodeElementById(id);
if (nodeElement) {
updateNodeElementStyle(nodes[nodeIndex], nodeElement);
}
// Aktualizuj podgląd
renderPreview();
return true;
}
return false;
}
// Renderuje podgląd na podstawie danych z tablicy 'nodes'
function renderPreview() {
previewPane.innerHTML = ''; // Wyczyść poprzedni podgląd
nodes.forEach(nodeData => {
const previewNode = document.createElement('div');
previewNode.classList.add('preview-node');
previewNode.id = `preview-${nodeData.id}`; // Unikalne ID dla elementu podglądu
previewNode.style.left = `${nodeData.x}px`;
previewNode.style.top = `${nodeData.y}px`;
previewNode.style.width = `${nodeData.width}px`;
previewNode.style.height = `${nodeData.height}px`;
previewNode.style.backgroundColor = nodeData.color;
previewNode.textContent = nodeData.label || `Węzeł ${nodeData.id.substring(5)}`; // Uproszczony tekst
previewNode.style.opacity = '0.8'; // Lekko przezroczysty dla odróżnienia
previewNode.style.border = `1px solid ${nodeData.color}`; // Obramowanie w kolorze węzła
// Dopasowanie rozmiaru podglądu do rozmiaru edytora
const editorWidth = editorCanvas.offsetWidth;
const editorHeight = editorCanvas.offsetHeight;
const previewWidth = previewPane.offsetWidth;
const previewHeight = previewPane.offsetHeight;
// Proste skalowanie podglądu (można ulepszyć)
const scaleX = previewWidth / editorWidth;
const scaleY = previewHeight / editorHeight;
const scale = Math.min(scaleX, scaleY) * 0.9; // Użyj mniejszej skali, aby zmieścić się z marginesem
// Jeśli węzeł jest za duży, aby się zmieścić, zmniejsz go proporcjonalnie
let displayWidth = nodeData.width * scale;
let displayHeight = nodeData.height * scale;
let displayX = nodeData.x * scale;
let displayY = nodeData.y * scale;
// Zapewnienie, że węzeł pozostaje w granicach podglądu
const maxPreviewWidth = previewWidth - 5; // Mały margines
const maxPreviewHeight = previewHeight - 5;
if (displayWidth > maxPreviewWidth) {
displayHeight *= (maxPreviewWidth / displayWidth);
displayWidth = maxPreviewWidth;
}
if (displayHeight > maxPreviewHeight) {
displayWidth *= (maxPreviewHeight / displayHeight);
displayHeight = maxPreviewHeight;
}
// Center the scaled node if it doesn't fill the preview pane
const offsetX = (previewWidth - (nodes.length > 0 ? nodes.reduce((max, n) => Math.max(max, n.x + n.width), 0) * scale : 0)) / 2;
const offsetY = (previewHeight - (nodes.length > 0 ? nodes.reduce((max, n) => Math.max(max, n.y + n.height), 0) * scale : 0)) / 2;
previewNode.style.width = `${displayWidth}px`;
previewNode.style.height = `${displayHeight}px`;
previewNode.style.left = `${displayX + offsetX}px`;
previewNode.style.top = `${displayY + offsetY}px`;
previewNode.style.fontSize = `${Math.max(10, 12 * scale)}px`; // Skaluj czcionkę
previewPane.appendChild(previewNode);
});
}
// Tworzy i dodaje nowy węzeł do edytora
function addNode(x = 50, y = 50, color = '#4CAF50', width = 120, height = 80, label = 'Nowy Węzeł') {
const newNodeId = generateNodeId();
const newNodeData = {
id: newNodeId,
x: x,
y: y,
width: width,
height: height,
color: color,
label: label
};
nodes.push(newNodeData);
// Tworzenie elementu DOM dla węzła
const nodeElement = document.createElement('div');
nodeElement.classList.add('node');
nodeElement.draggable = true; // Można przeciągać za pomocą atrybutu HTML5
nodeElement.dataset.id = newNodeId; // Przechowywanie ID w atrybucie data-*
const headerElement = document.createElement('div');
headerElement.classList.add('node-header');
headerElement.textContent = label;
nodeElement.appendChild(headerElement);
const contentElement = document.createElement('div');
contentElement.classList.add('node-content');
contentElement.textContent = ''; // Można tu dodać domyślną zawartość
nodeElement.appendChild(contentElement);
const configToggle = document.createElement('div');
configToggle.classList.add('config-toggle');
configToggle.innerHTML = '⚙️'; // Ikona lub tekst
nodeElement.appendChild(configToggle);
// Dodawanie uchwytów do zmiany rozmiaru
const resizerDirections = ['top-left', 'top-right', 'bottom-left', 'bottom-right'];
resizerDirections.forEach(dir => {
const resizer = document.createElement('div');
resizer.classList.add('resizer', dir);
resizer.dataset.direction = dir; // Przechowuje kierunek dla logiki zmiany rozmiaru
nodeElement.appendChild(resizer);
});
updateNodeElementStyle(newNodeData, nodeElement); // Ustawienie początkowych stylów
editorCanvas.appendChild(nodeElement);
// Dodanie nasłuchiwania zdarzeń dla węzła
attachNodeEventListeners(nodeElement);
// Aktualizacja podglądu
renderPreview();
}
// Wyświetlanie lub ukrywanie menu kontekstowego
function showContextMenu(event, nodeElement) {
event.preventDefault(); // Zapobiega domyślnemu menu przeglądarki
// Upewnij się, że tylko jedno menu jest aktywne
hideContextMenu();
const nodeId = nodeElement.dataset.id;
const nodeData = nodes.find(n => n.id === nodeId);
if (!nodeData) return;
// Pozycjonowanie menu
const menuX = event.clientX;
const menuY = event.clientY;
const menuWidth = contextMenu.offsetWidth;
const menuHeight = contextMenu.offsetHeight;
const viewportWidth = window.innerWidth;
const viewportHeight = window.innerHeight;
// Dostosowanie pozycji, aby menu nie wychodziło poza ekran
const finalX = menuX + menuWidth > viewportWidth ? viewportWidth - menuWidth - 10 : menuX;
const finalY = menuY + menuHeight > viewportHeight ? viewportHeight - menuHeight - 10 : menuY;
contextMenu.style.left = `${finalX}px`;
contextMenu.style.top = `${finalY}px`;
contextMenu.classList.add('show');
activeNodeElement = nodeElement; // Zapamiętaj węzeł, dla którego wywołano menu
}
function hideContextMenu() {
if (contextMenu.classList.contains('show')) {
contextMenu.classList.remove('show');
activeNodeElement = null;
}
}
// Funkcja obsługująca akcje z menu kontekstowego
function handleContextMenuAction(action) {
if (!activeNodeElement) return;
const nodeId = activeNodeElement.dataset.id;
switch (action) {
case 'configure':
toggleConfigPanel(activeNodeElement);
break;
case 'delete':
const nodeIndex = nodes.findIndex(n => n.id === nodeId);
if (nodeIndex !== -1) {
// Usuń element DOM
activeNodeElement.remove();
// Usuń z tablicy danych
nodes.splice(nodeIndex, 1);
// Zaktualizuj podgląd
renderPreview();
}
hideContextMenu();
break;
}
}
// Pokazuje/ukrywa panel konfiguracyjny dla danego węzła
function toggleConfigPanel(nodeElement) {
if (!nodeElement) return;
const nodeId = nodeElement.dataset.id;
let configPanel = nodeElement.querySelector('.config-panel');
// Jeśli panel nie istnieje, utwórz go
if (!configPanel) {
configPanel = document.createElement('div');
configPanel.classList.add('config-panel');
// Dodaj pola konfiguracyjne
configPanel.innerHTML = `
<label>Kolor: <input type="color" value="${nodeElement.style.backgroundColor || '#4CAF50'}"></label>
<label>Szerokość: <input type="number" min="50" max="500" value="${parseInt(nodeElement.style.width)}"></label>
<label>Wysokość: <input type="number" min="30" max="500" value="${parseInt(nodeElement.style.height)}"></label>
<label>Etykieta: <input type="text" value="${nodeElement.querySelector('.node-header').textContent}"></label>
<button type="button">Zamknij</button>
`;
nodeElement.appendChild(configPanel);
// Dodaj event listenery do pól formularza
const colorInput = configPanel.querySelector('input[type="color"]');
const widthInput = configPanel.querySelector('input[type="number"][max="500"]'); // Szerokość
const heightInput = configPanel.querySelector('input[type="number"][max="30"]'); // Wysokość
const labelInput = configPanel.querySelector('input[type="text"]');
const closeButton = configPanel.querySelector('button');
colorInput.addEventListener('input', (e) => {
updateNodeData(nodeId, { color: e.target.value });
});
widthInput.addEventListener('input', (e) => {
updateNodeData(nodeId, { width: parseInt(e.target.value) });
});
heightInput.addEventListener('input', (e) => {
updateNodeData(nodeId, { height: parseInt(e.target.value) });
});
labelInput.addEventListener('input', (e) => {
updateNodeData(nodeId, { label: e.target.value });
// Aktualizacja nagłówka w edytorze na żywo
nodeElement.querySelector('.node-header').textContent = e.target.value;
// Aktualizacja tekstu w podglądzie (wymaga renderPreview)
renderPreview(); // Przerenderowanie podglądu, bo tekst może się zmienić
});
closeButton.addEventListener('click', () => {
configPanel.classList.remove('show');
});
}
// Przełącz widoczność panelu
configPanel.classList.toggle('show');
}
// ----- Obsługa zdarzeń (Event Listeners) -----
// Nasłuchiwanie kliknięcia przycisku dodawania węzła
addNodeBtn.addEventListener('click', () => {
// Dodaj nowy węzeł na środku obszaru roboczego (lub w innym miejscu)
const canvasRect = editorCanvas.getBoundingClientRect();
addNode(
Math.max(0, canvasRect.width / 2 - 60), // Pozycja X
Math.max(0, canvasRect.height / 2 - 40), // Pozycja Y
'#' + Math.floor(Math.random()*16777215).toString(16).padStart(6, '0'), // Losowy kolor
120, 80, `Węzeł ${nodeIdCounter + 1}`
);
});
// Nasłuchiwanie kliknięć poza węzłem i menu, aby ukryć menu
document.addEventListener('click', (e) => {
// Ukryj menu kontekstowe, jeśli kliknięcie było poza nim
if (!contextMenu.contains(e.target) && contextMenu.classList.contains('show')) {
hideContextMenu();
}
// Ukryj panel konfiguracyjny, jeśli kliknięcie było poza nim (i nie jest to toggle)
if (activeNodeElement && !activeNodeElement.contains(e.target) && activeNodeElement.querySelector('.config-panel.show')) {
const configPanel = activeNodeElement.querySelector('.config-panel');
if (configPanel) configPanel.classList.remove('show');
}
});
// Nasłuchiwanie kliknięć prawym przyciskiem myszy na płótnie (do ukrywania menu)
editorCanvas.addEventListener('contextmenu', (e) => {
// Jeśli kliknięcie było na pustym obszarze, ukryj menu (jeśli jest widoczne)
if (!e.target.closest('.node') && contextMenu.classList.contains('show')) {
hideContextMenu();
}
});
// Nasłuchiwanie zdarzeń dla menu kontekstowego
contextMenu.addEventListener('click', (e) => {
const action = e.target.dataset.action;
if (action) {
handleContextMenuAction(action);
}
});
// Przypisywanie listenerów do pojedynczego węzła
function attachNodeEventListeners(nodeElement) {
const nodeId = nodeElement.dataset.id;
// --- Przeciąganie (Drag & Drop) ---
const header = nodeElement.querySelector('.node-header');
header.addEventListener('mousedown', (e) => {
// Pozwól na przeciąganie tylko lewym przyciskiem myszy
if (e.button !== 0) return;
// Nie przeciągaj, jeśli kliknięto na uchwyt zmiany rozmiaru lub menu konfiguracyjne
if (e.target.classList.contains('resizer') || e.target.classList.contains('config-toggle') || e.target.closest('.config-panel')) {
return;
}
isDragging = true;
currentDraggedNode = nodeElement;
header.classList.add('dragging'); // Zmień styl kursora podczas przeciągania
// Oblicz przesunięcie względem lewego górnego rogu węzła
const rect = nodeElement.getBoundingClientRect();
dragOffsetX = e.clientX - rect.left;
dragOffsetY = e.clientY - rect.top;
// Dodaj listener na cały dokument, aby śledzić ruch myszy
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('mouseup', onMouseUp);
// Zapobiegaj domyślnemu zachowaniu (np. zaznaczaniu tekstu)
e.preventDefault();
});
// --- Zmiana rozmiaru (Resize) ---
nodeElement.querySelectorAll('.resizer').forEach(resizer => {
resizer.addEventListener('mousedown', (e) => {
// Pozwól na zmianę rozmiaru tylko lewym przyciskiem myszy
if (e.button !== 0) return;
isResizing = true;
currentResizer = resizer;
resizer.classList.add('active'); // Podświetl aktywny uchwyt
const rect = nodeElement.getBoundingClientRect();
resizeStartWidth = rect.width;
resizeStartHeight = rect.height;
resizeStartX = e.clientX;
resizeStartY = e.clientY;
// Oblicz przesunięcie nagłówka dla logiki z "chwytaniem" za nagłówek
const headerRect = header.getBoundingClientRect();
dragOffsetX = e.clientX - headerRect.left;
dragOffsetY = e.clientY - headerRect.top;
document.addEventListener('mousemove', onMouseMove);
document.addEventListener('mouseup', onMouseUp);
e.preventDefault(); // Zapobiegaj domyślnemu zachowaniu
});
});
// --- Menu kontekstowe ---
nodeElement.addEventListener('contextmenu', (e) => {
// Zaznacz węzeł, na którym kliknięto prawym przyciskiem
// Można dodać wizualne zaznaczenie, jeśli potrzeba
showContextMenu(e, nodeElement);
});
// --- Konfiguracja ---
const configToggle = nodeElement.querySelector('.config-toggle');
if (configToggle) {
configToggle.addEventListener('click', (e) => {
toggleConfigPanel(nodeElement);
// Zatrzymaj propagację zdarzenia, aby nie wywołać ukrywania menu
e.stopPropagation();
});
}
}
// Funkcja obsługująca ruch myszy (przeciąganie i zmiana rozmiaru)
function onMouseMove(e) {
if (isDragging && currentDraggedNode) {
const nodeId = currentDraggedNode.dataset.id;
// Oblicz nowe pozycje x, y
let newX = e.clientX - dragOffsetX;
let newY = e.clientY - dragOffsetY;
// Ogranicz ruch do granic canvasu
const canvasRect = editorCanvas.getBoundingClientRect();
const nodeRect = currentDraggedNode.getBoundingClientRect();
newX = Math.max(0, Math.min(newX, canvasRect.width - nodeRect.width));
newY = Math.max(0, Math.min(newY, canvasRect.height - nodeRect.height));
// Aktualizuj styl elementu węzła
currentDraggedNode.style.left = `${newX}px`;
currentDraggedNode.style.top = `${newY}px`;
// Aktualizuj dane węzła (bez wywoływania renderPreview w każdej klatce)
updateNodeData(nodeId, { x: newX, y: newY });
} else if (isResizing && currentResizer) {
const nodeId = currentResizer.parentElement.dataset.id;
const nodeElement = currentResizer.parentElement;
let deltaX = e.clientX - resizeStartX;
let deltaY = e.clientY - resizeStartY;
let newWidth = resizeStartWidth + deltaX;
let newHeight = resizeStartHeight + deltaY;
// Obsługa zmiany rozmiaru w zależności od uchwytu
const direction = currentResizer.dataset.direction;
let adjustedX = nodeElement.offsetLeft;
let adjustedY = nodeElement.offsetTop;
let adjustedWidth = newWidth;
let adjustedHeight = newHeight;
// Dostosowanie pozycji i wymiarów w zależności od uchwytu
if (direction.includes('right')) {
adjustedWidth = Math.max(50, resizeStartWidth + deltaX); // Min szerokość 50px
} else if (direction.includes('left')) {
adjustedWidth = Math.max(50, resizeStartWidth - deltaX); // Min szerokość 50px
adjustedX = nodeElement.offsetLeft + (resizeStartWidth - adjustedWidth);
}
if (direction.includes('bottom')) {
adjustedHeight = Math.max(30, resizeStartHeight + deltaY); // Min wysokość 30px
} else if (direction.includes('top')) {
adjustedHeight = Math.max(30, resizeStartHeight - deltaY); // Min wysokość 30px
adjustedY = nodeElement.offsetTop + (resizeStartHeight - adjustedHeight);
}
// Ograniczenia: Nie pozwól wychodzić poza canvas
const canvasRect = editorCanvas.getBoundingClientRect();
adjustedX = Math.max(0, adjustedX);
adjustedY = Math.max(0, adjustedY);
adjustedWidth = Math.min(adjustedWidth, canvasRect.width - adjustedX);
adjustedHeight = Math.min(adjustedHeight, canvasRect.height - adjustedY);
// Aktualizacja stylu elementu
nodeElement.style.width = `${adjustedWidth}px`;
nodeElement.style.height = `${adjustedHeight}px`;
nodeElement.style.left = `${adjustedX}px`;
nodeElement.style.top = `${adjustedY}px`;
// Aktualizuj dane węzła
updateNodeData(nodeId, { x: adjustedX, y: adjustedY, width: adjustedWidth, height: adjustedHeight });
} else if (isResizingSplit) {
// Obsługa zmiany rozmiaru panelu dzielącego
const deltaX = e.clientX - splitResizeStartX;
const containerWidth = divider.parentElement.offsetWidth;
const previewWidth = previewWidth + deltaX; // Aktualna szerokość podglądu
// Ustaw minimalną szerokość dla paneli (np. 150px)
const minPanelWidth = 150;
if (previewWidth < minPanelWidth || (containerWidth - previewWidth) < minPanelWidth) {
return; // Nie zmieniaj, jeśli któryś panel byłby za mały
}
const previewFlexBasis = (previewWidth / containerWidth) * 100;
const editorFlexBasis = 100 - previewFlexBasis;
previewPane.style.flexBasis = `${previewFlexBasis}%`;
editorPane.style.flexBasis = `${editorFlexBasis}%`;
// Aktualizacja podglądu po zmianie rozmiaru
renderPreview();
}
}
// Funkcja obsługująca zakończenie przeciągania lub zmiany rozmiaru
function onMouseUp(e) {
if (isDragging) {
isDragging = false;
if (currentDraggedNode) {
currentDraggedNode.classList.remove('dragging');
// Finalna aktualizacja danych i podglądu
const nodeId = currentDraggedNode.dataset.id;
const nodeElement = findNodeElementById(nodeId); // Pobierz aktualny element, może być inny niż currentDraggedNode
if (nodeElement) {
updateNodeData(nodeId, {
x: parseFloat(nodeElement.style.left),
y: parseFloat(nodeElement.style.top)
});
}
currentDraggedNode = null;
// renderPreview(); // Wywołaj raz po zakończeniu przeciągania
}
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('mouseup', onMouseUp);
} else if (isResizing) {
isResizing = false;
if (currentResizer) {
currentResizer.classList.remove('active');
// Finalna aktualizacja danych i podglądu po zmianie rozmiaru
const nodeId = currentResizer.parentElement.dataset.id;
const nodeElement = findNodeElementById(nodeId);
if (nodeElement) {
updateNodeData(nodeId, {
x: parseFloat(nodeElement.style.left),
y: parseFloat(nodeElement.style.top),
width: parseFloat(nodeElement.style.width),
height: parseFloat(nodeElement.style.height)
});
}
currentResizer = null;
// renderPreview(); // Wywołaj raz po zakończeniu zmiany rozmiaru
}
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('mouseup', onMouseUp);
} else if (isResizingSplit) {
isResizingSplit = false;
divider.style.cursor = 'col-resize'; // Przywróć domyślny kursor
document.removeEventListener('mousemove', onMouseMove);
document.removeEventListener('mouseup', onMouseUp);
}
}
// ----- Obsługa zmiany rozmiaru podziału ekranu -----
divider.addEventListener('mousedown', (e) => {
// Pozwól na zmianę rozmiaru tylko lewym przyciskiem myszy
if (e.button !== 0) return;
isResizingSplit = true;
splitResizeStartX = e.clientX; // Pozycja X myszy w momencie kliknięcia
divider.style.cursor = 'grabbing'; // Zmień kursor, aby pokazać, że trwa zmiana rozmiaru
document.addEventListener('mousemove', onMouseMove); // Używamy tego samego listenera
document.addEventListener('mouseup', onMouseUp); // Używamy tego samego listenera
e.preventDefault(); // Zapobiegaj domyślnemu zachowaniu
});
// ----- Początkowe ładowanie -----
// Dodaj kilka początkowych węzłów dla demonstracji
addNode(50, 50, '#FF5722', 150, 70, 'Wejście');
addNode(250, 100, '#2196F3', 130, 90, 'Proces');
addNode(150, 200, '#ffeb3b', 110, 60, 'Wyjście');
addNode(400, 250, '#9C27B0', 140, 80, 'Konfiguracja');
// Wywołaj renderPreview na początku, aby upewnić się, że podgląd jest aktualny
renderPreview();
// Dodaj listener na okno, aby reagować na ewentualne zmiany rozmiaru okna przeglądarki
window.addEventListener('resize', () => {
// Ponowne renderowanie podglądu może być potrzebne, jeśli skalowanie zależy od rozmiaru okna
renderPreview();
});
</script>
</body>
</html>