<!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>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
body {
height: 100vh;
display: flex;
flex-direction: column;
overflow: hidden;
}
header {
background-color: #2c3e50;
color: white;
padding: 10px 20px;
display: flex;
justify-content: space-between;
align-items: center;
}
.logo {
font-weight: bold;
font-size: 1.2rem;
}
.controls {
display: flex;
gap: 10px;
}
button {
background-color: #3498db;
color: white;
border: none;
padding: 5px 10px;
border-radius: 3px;
cursor: pointer;
transition: background-color 0.2s;
}
button:hover {
background-color: #2980b9;
}
.container {
display: flex;
flex: 1;
overflow: hidden;
}
.splitter {
width: 5px;
background-color: #bdc3c7;
cursor: col-resize;
}
.preview, .editor {
padding: 10px;
overflow: auto;
position: relative;
}
.preview {
background-color: #f8f9fa;
flex: 1;
display: flex;
flex-direction: column;
}
.editor {
background-color: #ecf0f1;
flex: 1;
display: flex;
flex-direction: column;
}
.preview-title, .editor-title {
margin-bottom: 10px;
font-weight: bold;
color: #2c3e50;
}
.preview-area {
flex: 1;
border: 1px dashed #bdc3c7;
position: relative;
background-color: white;
overflow: hidden;
}
.editor-area {
flex: 1;
border: 1px dashed #bdc3c7;
position: relative;
background-color: white;
overflow: hidden;
}
.node {
position: absolute;
background-color: #3498db;
color: white;
border-radius: 5px;
padding: 10px;
cursor: move;
user-select: none;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
transition: transform 0.1s;
min-width: 100px;
min-height: 50px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.node:hover {
transform: scale(1.02);
}
.node-header {
width: 100%;
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 5px;
}
.node-title {
font-weight: bold;
}
.node-content {
text-align: center;
}
.node-input, .node-output {
position: absolute;
width: 10px;
height: 10px;
background-color: #2c3e50;
border-radius: 50%;
z-index: 10;
}
.node-input {
left: -5px;
top: 50%;
transform: translateY(-50%);
}
.node-output {
right: -5px;
top: 50%;
transform: translateY(-50%);
}
.connection {
position: absolute;
background-color: #2c3e50;
z-index: 1;
pointer-events: none;
}
.node-menu {
position: absolute;
background-color: white;
border: 1px solid #bdc3c7;
border-radius: 3px;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
padding: 5px;
z-index: 100;
display: none;
}
.node-menu-item {
padding: 5px 10px;
cursor: pointer;
}
.node-menu-item:hover {
background-color: #f0f0f0;
}
.node-config {
position: absolute;
background-color: white;
border: 1px solid #bdc3c7;
border-radius: 3px;
box-shadow: 0 2px 5px rgba(0,0,0,0.2);
padding: 10px;
z-index: 100;
display: none;
width: 200px;
}
.config-group {
margin-bottom: 10px;
}
.config-group label {
display: block;
margin-bottom: 3px;
font-size: 0.8rem;
}
.config-group input, .config-group select {
width: 100%;
padding: 5px;
border: 1px solid #bdc3c7;
border-radius: 3px;
}
.config-actions {
display: flex;
justify-content: flex-end;
gap: 5px;
margin-top: 10px;
}
.toolbar {
display: flex;
gap: 5px;
margin-bottom: 10px;
flex-wrap: wrap;
}
.node-type {
padding: 5px 10px;
background-color: #3498db;
color: white;
border-radius: 3px;
cursor: pointer;
transition: background-color 0.2s;
}
.node-type:hover {
background-color: #2980b9;
}
.node-type.active {
background-color: #2980b9;
}
.status-bar {
background-color: #2c3e50;
color: white;
padding: 5px 10px;
font-size: 0.8rem;
display: flex;
justify-content: space-between;
}
.modal {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0,0,0,0.5);
display: none;
justify-content: center;
align-items: center;
z-index: 1000;
}
.modal-content {
background-color: white;
padding: 20px;
border-radius: 5px;
width: 80%;
max-width: 500px;
}
.modal-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
}
.modal-title {
font-weight: bold;
}
.close-modal {
background-color: #e74c3c;
}
.close-modal:hover {
background-color: #c0392b;
}
textarea {
width: 100%;
height: 200px;
margin-bottom: 10px;
padding: 5px;
border: 1px solid #bdc3c7;
border-radius: 3px;
}
@media (max-width: 768px) {
.container {
flex-direction: column;
}
.splitter {
height: 5px;
width: 100%;
cursor: row-resize;
}
.preview, .editor {
height: 50vh;
}
}
</style>
</head>
<body>
<header>
<div class="logo">Edytor Węzłów</div>
<div class="controls">
<button id="save-btn">Zapisz</button>
<button id="load-btn">Wczytaj</button>
<button id="export-btn">Eksportuj</button>
<button id="import-btn">Importuj</button>
</div>
</header>
<div class="container">
<div class="preview">
<div class="preview-title">Podgląd</div>
<div class="preview-area" id="preview-area"></div>
</div>
<div class="splitter" id="splitter"></div>
<div class="editor">
<div class="editor-title">Edytor Węzłów</div>
<div class="toolbar">
<div class="node-type active" data-type="basic">Podstawowy</div>
<div class="node-type" data-type="input">Wejście</div>
<div class="node-type" data-type="output">Wyjście</div>
<div class="node-type" data-type="processor">Procesor</div>
</div>
<div class="editor-area" id="editor-area"></div>
</div>
</div>
<div class="status-bar">
<span>Gotowy</span>
<span id="node-count">Węzły: 0 | Połączenia: 0</span>
</div>
<!-- Modal do zapisywania/wczytywania -->
<div class="modal" id="save-modal">
<div class="modal-content">
<div class="modal-header">
<div class="modal-title">Zapisz konfigurację</div>
<button class="close-modal" id="close-save-modal">X</button>
</div>
<textarea id="save-data" readonly></textarea>
<button id="copy-save-data">Kopiuj do schowka</button>
</div>
</div>
<div class="modal" id="load-modal">
<div class="modal-content">
<div class="modal-header">
<div class="modal-title">Wczytaj konfigurację</div>
<button class="close-modal" id="close-load-modal">X</button>
</div>
<textarea id="load-data" placeholder="Wklej tutaj zapisane dane..."></textarea>
<button id="apply-load-data">Zastosuj</button>
</div>
</div>
<div class="modal" id="export-modal">
<div class="modal-content">
<div class="modal-header">
<div class="modal-title">Eksportuj do JSON</div>
<button class="close-modal" id="close-export-modal">X</button>
</div>
<textarea id="export-data" readonly></textarea>
<button id="copy-export-data">Kopiuj do schowka</button>
</div>
</div>
<div class="modal" id="import-modal">
<div class="modal-content">
<div class="modal-header">
<div class="modal-title">Importuj z JSON</div>
<button class="close-modal" id="close-import-modal">X</button>
</div>
<textarea id="import-data" placeholder="Wklej tutaj dane JSON..."></textarea>
<button id="apply-import-data">Zastosuj</button>
</div>
</div>
<!-- Menu kontekstowe węzła -->
<div class="node-menu" id="node-menu">
<div class="node-menu-item" data-action="edit">Edytuj</div>
<div class="node-menu-item" data-action="delete">Usuń</div>
<div class="node-menu-item" data-action="duplicate">Duplikuj</div>
</div>
<!-- Okno konfiguracji węzła -->
<div class="node-config" id="node-config">
<div class="config-group">
<label for="node-title">Tytuł:</label>
<input type="text" id="node-title">
</div>
<div class="config-group">
<label for="node-content">Treść:</label>
<input type="text" id="node-content">
</div>
<div class="config-group">
<label for="node-color">Kolor:</label>
<input type="color" id="node-color" value="#3498db">
</div>
<div class="config-group">
<label for="node-width">Szerokość:</label>
<input type="range" id="node-width" min="100" max="300" value="150">
</div>
<div class="config-group">
<label for="node-height">Wysokość:</label>
<input type="range" id="node-height" min="50" max="200" value="80">
</div>
<div class="config-actions">
<button id="cancel-config">Anuluj</button>
<button id="apply-config">Zastosuj</button>
</div>
</div>
<script>
// Stan aplikacji
const state = {
nodes: [],
connections: [],
selectedNode: null,
activeNodeType: 'basic',
isDragging: false,
dragStartX: 0,
dragStartY: 0,
dragStartWidth: 0,
dragStartHeight: 0,
isResizing: false,
resizeDirection: null,
isConnecting: false,
connectionStart: null,
isDraggingSplitter: false,
splitterStartX: 0,
splitterStartWidth: 0
};
// Elementy DOM
const editorArea = document.getElementById('editor-area');
const previewArea = document.getElementById('preview-area');
const nodeMenu = document.getElementById('node-menu');
const nodeConfig = document.getElementById('node-config');
const nodeCount = document.getElementById('node-count');
const splitter = document.getElementById('splitter');
const nodeTypes = document.querySelectorAll('.node-type');
const saveBtn = document.getElementById('save-btn');
const loadBtn = document.getElementById('load-btn');
const exportBtn = document.getElementById('export-btn');
const importBtn = document.getElementById('import-btn');
const saveModal = document.getElementById('save-modal');
const loadModal = document.getElementById('load-modal');
const exportModal = document.getElementById('export-modal');
const importModal = document.getElementById('import-modal');
const closeSaveModal = document.getElementById('close-save-modal');
const closeLoadModal = document.getElementById('close-load-modal');
const closeExportModal = document.getElementById('close-export-modal');
const closeImportModal = document.getElementById('close-import-modal');
const saveData = document.getElementById('save-data');
const loadData = document.getElementById('load-data');
const exportData = document.getElementById('export-data');
const importData = document.getElementById('import-data');
const copySaveData = document.getElementById('copy-save-data');
const copyExportData = document.getElementById('copy-export-data');
const applyLoadData = document.getElementById('apply-load-data');
const applyImportData = document.getElementById('apply-import-data');
// Inicjalizacja
function init() {
// Ustawienie aktywnego typu węzła
nodeTypes.forEach(type => {
type.addEventListener('click', () => {
nodeTypes.forEach(t => t.classList.remove('active'));
type.classList.add('active');
state.activeNodeType = type.dataset.type;
});
});
// Obsługa przycisków modalnych
saveBtn.addEventListener('click', () => {
saveModal.style.display = 'flex';
saveData.value = saveToString();
});
loadBtn.addEventListener('click', () => {
loadModal.style.display = 'flex';
});
exportBtn.addEventListener('click', () => {
exportModal.style.display = 'flex';
exportData.value = JSON.stringify(exportToJSON(), null, 2);
});
importBtn.addEventListener('click', () => {
importModal.style.display = 'flex';
});
closeSaveModal.addEventListener('click', () => {
saveModal.style.display = 'none';
});
closeLoadModal.addEventListener('click', () => {
loadModal.style.display = 'none';
});
closeExportModal.addEventListener('click', () => {
exportModal.style.display = 'none';
});
closeImportModal.addEventListener('click', () => {
importModal.style.display = 'none';
});
copySaveData.addEventListener('click', () => {
navigator.clipboard.writeText(saveData.value);
alert('Dane skopiowane do schowka!');
});
copyExportData.addEventListener('click', () => {
navigator.clipboard.writeText(exportData.value);
alert('Dane skopiowane do schowka!');
});
applyLoadData.addEventListener('click', () => {
loadFromString(loadData.value);
loadModal.style.display = 'none';
});
applyImportData.addEventListener('click', () => {
try {
const data = JSON.parse(importData.value);
importFromJSON(data);
importModal.style.display = 'none';
} catch (e) {
alert('Błędny format JSON: ' + e.message);
}
});
// Obsługa podziału ekranu
splitter.addEventListener('mousedown', startDragSplitter);
// Obsługa dodawania węzłów
editorArea.addEventListener('click', (e) => {
if (e.target === editorArea) {
addNode(e.clientX - editorArea.getBoundingClientRect().left,
e.clientY - editorArea.getBoundingClientRect().top);
}
});
// Obsługa menu kontekstowego
document.addEventListener('contextmenu', (e) => {
if (e.target.classList.contains('node')) {
e.preventDefault();
state.selectedNode = e.target;
showNodeMenu(e.clientX, e.clientY);
} else {
hideNodeMenu();
}
});
// Obsługa kliknięć poza menu
document.addEventListener('click', (e) => {
if (!nodeMenu.contains(e.target) && e.target !== state.selectedNode) {
hideNodeMenu();
}
if (!nodeConfig.contains(e.target) && !e.target.classList.contains('node')) {
hideNodeConfig();
}
});
// Aktualizacja licznika węzłów
updateNodeCount();
}
// Dodawanie nowego węzła
function addNode(x, y) {
const nodeId = 'node-' + Date.now();
const node = document.createElement('div');
node.className = 'node';
node.id = nodeId;
node.style.left = x + 'px';
node.style.top = y + 'px';
node.style.width = '150px';
node.style.height = '80px';
node.style.backgroundColor = getNodeColor(state.activeNodeType);
// Dodanie wejścia i wyjścia
const input = document.createElement('div');
input.className = 'node-input';
input.dataset.nodeId = nodeId;
input.dataset.type = 'input';
const output = document.createElement('div');
output.className = 'node-output';
output.dataset.nodeId = nodeId;
output.dataset.type = 'output';
// Dodanie nagłówka
const header = document.createElement('div');
header.className = 'node-header';
const title = document.createElement('div');
title.className = 'node-title';
title.textContent = getNodeTitle(state.activeNodeType);
header.appendChild(title);
node.appendChild(header);
// Dodanie treści
const content = document.createElement('div');
content.className = 'node-content';
content.textContent = getNodeContent(state.activeNodeType);
node.appendChild(content);
// Dodanie wejścia i wyjścia
node.appendChild(input);
node.appendChild(output);
// Dodanie do obszaru edytora
editorArea.appendChild(node);
// Dodanie do stanu
state.nodes.push({
id: nodeId,
x: x,
y: y,
width: 150,
height: 80,
type: state.activeNodeType,
title: getNodeTitle(state.activeNodeType),
content: getNodeContent(state.activeNodeType),
color: getNodeColor(state.activeNodeType),
inputs: [],
outputs: []
});
// Obsługa przeciągania
makeDraggable(node);
makeResizable(node);
// Obsługa połączeń
input.addEventListener('mousedown', startConnection);
output.addEventListener('mousedown', startConnection);
// Aktualizacja podglądu
updatePreview();
// Aktualizacja licznika
updateNodeCount();
}
// Funkcje pomocnicze dla węzłów
function getNodeTitle(type) {
const titles = {
'basic': 'Węzeł',
'input': 'Wejście',
'output': 'Wyjście',
'processor': 'Procesor'
};
return titles[type] || 'Węzeł';
}
function getNodeContent(type) {
const contents = {
'basic': 'Podstawowy węzeł',
'input': 'Dane wejściowe',
'output': 'Wynik',
'processor': 'Przetwarzanie'
};
return contents[type] || 'Podstawowy węzeł';
}
function getNodeColor(type) {
const colors = {
'basic': '#3498db',
'input': '#2ecc71',
'output': '#e74c3c',
'processor': '#f39c12'
};
return colors[type] || '#3498db';
}
// Przeciąganie węzłów
function makeDraggable(node) {
let offsetX, offsetY;
let isDragging = false;
node.addEventListener('mousedown', (e) => {
if (e.target.classList.contains('node-input') ||
e.target.classList.contains('node-output')) {
return;
}
isDragging = true;
offsetX = e.clientX - node.getBoundingClientRect().left;
offsetY = e.clientY - node.getBoundingClientRect().top;
node.style.zIndex = '1000';
e.preventDefault();
});
document.addEventListener('mousemove', (e) => {
if (!isDragging) return;
const editorRect = editorArea.getBoundingClientRect();
let x = e.clientX - editorRect.left - offsetX;
let y = e.clientY - editorRect.top - offsetY;
// Ograniczenie do obszaru edytora
x = Math.max(0, Math.min(x, editorRect.width - node.offsetWidth));
y = Math.max(0, Math.min(y, editorRect.height - node.offsetHeight));
node.style.left = x + 'px';
node.style.top = y + 'px';
// Aktualizacja stanu
const nodeId = node.id;
const nodeIndex = state.nodes.findIndex(n => n.id === nodeId);
if (nodeIndex !== -1) {
state.nodes[nodeIndex].x = x;
state.nodes[nodeIndex].y = y;
}
// Aktualizacja połączeń
updateConnections();
});
document.addEventListener('mouseup', () => {
isDragging = false;
node.style.zIndex = '';
updatePreview();
});
}
// Zmiana rozmiaru węzłów
function makeResizable(node) {
const resizeHandles = ['nw', 'ne', 'sw', 'se'];
resizeHandles.forEach(handle => {
const resizeHandle = document.createElement('div');
resizeHandle.className = 'resize-handle';
resizeHandle.style.position = 'absolute';
resizeHandle.style.width = '10px';
resizeHandle.style.height = '10px';
resizeHandle.style.backgroundColor = '#2c3e50';
resizeHandle.style.zIndex = '1001';
if (handle === 'nw') {
resizeHandle.style.top = '-5px';
resizeHandle.style.left = '-5px';
resizeHandle.style.cursor = 'nwse-resize';
} else if (handle === 'ne') {
resizeHandle.style.top = '-5px';
resizeHandle.style.right = '-5px';
resizeHandle.style.cursor = 'nesw-resize';
} else if (handle === 'sw') {
resizeHandle.style.bottom = '-5px';
resizeHandle.style.left = '-5px';
resizeHandle.style.cursor = 'nesw-resize';
} else if (handle === 'se') {
resizeHandle.style.bottom = '-5px';
resizeHandle.style.right = '-5px';
resizeHandle.style.cursor = 'nwse-resize';
}
resizeHandle.dataset.direction = handle;
node.appendChild(resizeHandle);
resizeHandle.addEventListener('mousedown', (e) => {
e.stopPropagation();
state.isResizing = true;
state.resizeDirection = handle;
state.selectedNode = node;
state.dragStartX = e.clientX;
state.dragStartY = e.clientY;
state.dragStartWidth = node.offsetWidth;
state.dragStartHeight = node.offsetHeight;
e.preventDefault();
});
});
}
// Obsługa zmiany rozmiaru
document.addEventListener('mousemove', (e) => {
if (!state.isResizing || !state.selectedNode) return;
const node = state.selectedNode;
const direction = state.resizeDirection;
const editorRect = editorArea.getBoundingClientRect();
let width = state.dragStartWidth;
let height = state.dragStartHeight;
let x = parseInt(node.style.left);
let y = parseInt(node.style.top);
if (direction.includes('e')) {
width = state.dragStartWidth + (e.clientX - state.dragStartX);
}
if (direction.includes('s')) {
height = state.dragStartHeight + (e.clientY - state.dragStartY);
}
if (direction.includes('w')) {
width = state.dragStartWidth - (e.clientX - state.dragStartX);
x = x + (e.clientX - state.dragStartX);
}
if (direction.includes('n')) {
height = state.dragStartHeight - (e.clientY - state.dragStartY);
y = y + (e.clientY - state.dragStartY);
}
// Ograniczenia minimalne
width = Math.max(100, width);
height = Math.max(50, height);
// Ograniczenia do obszaru edytora
if (x < 0) {
width += x;
x = 0;
}
if (y < 0) {
height += y;
y = 0;
}
if (x + width > editorRect.width) {
width = editorRect.width - x;
}
if (y + height > editorRect.height) {
height = editorRect.height - y;
}
node.style.width = width + 'px';
node.style.height = height + 'px';
node.style.left = x + 'px';
node.style.top = y + 'px';
// Aktualizacja stanu
const nodeId = node.id;
const nodeIndex = state.nodes.findIndex(n => n.id === nodeId);
if (nodeIndex !== -1) {
state.nodes[nodeIndex].x = x;
state.nodes[nodeIndex].y = y;
state.nodes[nodeIndex].width = width;
state.nodes[nodeIndex].height = height;
}
});
document.addEventListener('mouseup', () => {
if (state.isResizing) {
state.isResizing = false;
state.selectedNode = null;
updatePreview();
}
});
// Obsługa połączeń
function startConnection(e) {
if (e.target.classList.contains('node-input') || e.target.classList.contains('node-output')) {
state.isConnecting = true;
state.connectionStart = {
x: e.clientX,
y: e.clientY,
nodeId: e.target.dataset.nodeId,
type: e.target.dataset.type
};
// Tymczasowe połączenie
const tempConnection = document.createElement('div');
tempConnection.className = 'connection';
tempConnection.style.position = 'absolute';
tempConnection.style.width = '0';
tempConnection.style.height = '2px';
tempConnection.style.backgroundColor = '#2c3e50';
tempConnection.style.transformOrigin = '0 0';
tempConnection.style.zIndex = '1000';
tempConnection.id = 'temp-connection';
editorArea.appendChild(tempConnection);
e.preventDefault();
}
}
document.addEventListener('mousemove', (e) => {
if (state.isConnecting) {
const tempConnection = document.getElementById('temp-connection');
if (tempConnection) {
const startX = state.connectionStart.x;
const startY = state.connectionStart.y;
const endX = e.clientX;
const endY = e.clientY;
const dx = endX - startX;
const dy = endY - startY;
const distance = Math.sqrt(dx * dx + dy * dy);
tempConnection.style.left = startX + 'px';
tempConnection.style.top = startY + 'px';
tempConnection.style.width = distance + 'px';
tempConnection.style.transform = `rotate(${Math.atan2(dy, dx)}rad)`;
}
}
});
document.addEventListener('mouseup', (e) => {
if (state.isConnecting) {
state.isConnecting = false;
const tempConnection = document.getElementById('temp-connection');
if (tempConnection) {
tempConnection.remove();
}
if (e.target.classList.contains('node-input') || e.target.classList.contains('node-output')) {
const endNodeId = e.target.dataset.nodeId;
const endType = e.target.dataset.type;
// Sprawdzenie, czy połączenie jest dozwolone (wejście do wyjścia)
if (state.connectionStart.type === 'output' && endType === 'input') {
// Dodanie połączenia
const connection = {
from: state.connectionStart.nodeId,
to: endNodeId,
fromX: state.connectionStart.x,
fromY: state.connectionStart.y,
toX: e.clientX,
toY: e.clientY
};
state.connections.push(connection);
updateConnections();
updatePreview();
}
}
}
});
// Aktualizacja połączeń
function updateConnections() {
// Usunięcie istniejących połączeń
document.querySelectorAll('.connection:not(#temp-connection)').forEach(conn => conn.remove());
// Dodanie nowych połączeń
state.connections.forEach(conn => {
const fromNode = document.getElementById(conn.from);
const toNode = document.getElementById(conn.to);
if (fromNode && toNode) {
const fromRect = fromNode.getBoundingClientRect();
const toRect = toNode.getBoundingClientRect();
const fromX = fromRect.right;
const fromY = fromRect.top + fromRect.height / 2;
const toX = toRect.left;
const toY = toRect.top + toRect.height / 2;
const dx = toX - fromX;
const dy = toY - fromY;
const distance = Math.sqrt(dx * dx + dy * dy);
const connection = document.createElement('div');
connection.className = 'connection';
connection.style.position = 'absolute';
connection.style.width = distance + 'px';
connection.style.height = '2px';
connection.style.backgroundColor = '#2c3e50';
connection.style.left = fromX + 'px';
connection.style.top = fromY + 'px';
connection.style.transform = `rotate(${Math.atan2(dy, dx)}rad)`;
connection.style.transformOrigin = '0 0';
connection.style.zIndex = '1';
editorArea.appendChild(connection);
}
});
}
// Aktualizacja podglądu
function updatePreview() {
// Wyczyść podgląd
previewArea.innerHTML = '';
// Dodaj węzły do podglądu
state.nodes.forEach(node => {
const previewNode = document.createElement('div');
previewNode.className = 'node';
previewNode.style.left = node.x + 'px';
previewNode.style.top = node.y + 'px';
previewNode.style.width = node.width + 'px';
previewNode.style.height = node.height + 'px';
previewNode.style.backgroundColor = node.color;
const header = document.createElement('div');
header.className = 'node-header';
const title = document.createElement('div');
title.className = 'node-title';
title.textContent = node.title;
header.appendChild(title);
previewNode.appendChild(header);
const content = document.createElement('div');
content.className = 'node-content';
content.textContent = node.content;
previewNode.appendChild(content);
previewArea.appendChild(previewNode);
});
// Dodaj połączenia do podglądu
state.connections.forEach(conn => {
const fromNode = state.nodes.find(n => n.id === conn.from);
const toNode = state.nodes.find(n => n.id === conn.to);
if (fromNode && toNode) {
const fromX = fromNode.x + fromNode.width;
const fromY = fromNode.y + fromNode.height / 2;
const toX = toNode.x;
const toY = toNode.y + toNode.height / 2;
const dx = toX - fromX;
const dy = toY - fromY;
const distance = Math.sqrt(dx * dx + dy * dy);
const connection = document.createElement('div');
connection.className = 'connection';
connection.style.position = 'absolute';
connection.style.width = distance + 'px';
connection.style.height = '2px';
connection.style.backgroundColor = '#2c3e50';
connection.style.left = fromX + 'px';
connection.style.top = fromY + 'px';
connection.style.transform = `rotate(${Math.atan2(dy, dx)}rad)`;
connection.style.transformOrigin = '0 0';
connection.style.zIndex = '1';
previewArea.appendChild(connection);
}
});
}
// Menu kontekstowe węzła
function showNodeMenu(x, y) {
nodeMenu.style.display = 'block';
nodeMenu.style.left = x + 'px';
nodeMenu.style.top = y + 'px';
// Obsługa opcji menu
const menuItems = nodeMenu.querySelectorAll('.node-menu-item');
menuItems.forEach(item => {
item.addEventListener('click', () => {
const action = item.dataset.action;
handleNodeMenuAction(action);
hideNodeMenu();
});
});
}
function hideNodeMenu() {
nodeMenu.style.display = 'none';
}
function handleNodeMenuAction(action) {
if (!state.selectedNode) return;
switch (action) {
case 'edit':
showNodeConfig(state.selectedNode);
break;
case 'delete':
deleteNode(state.selectedNode);
break;
case 'duplicate':
duplicateNode(state.selectedNode);
break;
}
}
// Konfiguracja węzła
function showNodeConfig(node) {
const nodeId = node.id;
const nodeData = state.nodes.find(n => n.id === nodeId);
if (!nodeData) return;
// Ustawienie pozycji
const rect = node.getBoundingClientRect();
nodeConfig.style.left = rect.left + 'px';
nodeConfig.style.top = rect.bottom + 'px';
nodeConfig.style.display = 'block';
// Wypełnienie formularza
document.getElementById('node-title').value = nodeData.title;
document.getElementById('node-content').value = nodeData.content;
document.getElementById('node-color').value = nodeData.color;
document.getElementById('node-width').value = nodeData.width;
document.getElementById('node-height').value = nodeData.height;
// Obsługa przycisków
document.getElementById('cancel-config').addEventListener('click', hideNodeConfig);
document.getElementById('apply-config').addEventListener('click', () => {
applyNodeConfig(nodeId);
hideNodeConfig();
});
}
function hideNodeConfig() {
nodeConfig.style.display = 'none';
}
function applyNodeConfig(nodeId) {
const node = document.getElementById(nodeId);
if (!node) return;
const nodeIndex = state.nodes.findIndex(n => n.id === nodeId);
if (nodeIndex === -1) return;
// Aktualizacja danych
state.nodes[nodeIndex].title = document.getElementById('node-title').value;
state.nodes[nodeIndex].content = document.getElementById('node-content').value;
state.nodes[nodeIndex].color = document.getElementById('node-color').value;
state.nodes[nodeIndex].width = parseInt(document.getElementById('node-width').value);
state.nodes[nodeIndex].height = parseInt(document.getElementById('node-height').value);
// Aktualizacja wizualna
node.style.backgroundColor = state.nodes[nodeIndex].color;
node.style.width = state.nodes[nodeIndex].width + 'px';
node.style.height = state.nodes[nodeIndex].height + 'px';
// Aktualizacja nagłówka i treści
const titleElement = node.querySelector('.node-title');
const contentElement = node.querySelector('.node-content');
if (titleElement) titleElement.textContent = state.nodes[nodeIndex].title;
if (contentElement) contentElement.textContent = state.nodes[nodeIndex].content;
// Aktualizacja podglądu
updatePreview();
}
// Usuwanie węzła
function deleteNode(node) {
const nodeId = node.id;
// Usunięcie węzła z DOM
node.remove();
// Usunięcie węzła ze stanu
state.nodes = state.nodes.filter(n => n.id !== nodeId);
// Usunięcie połączeń związanych z węzłem
state.connections = state.connections.filter(conn =>
conn.from !== nodeId && conn.to !== nodeId
);
// Aktualizacja połączeń
updateConnections();
// Aktualizacja podglądu
updatePreview();
// Aktualizacja licznika
updateNodeCount();
}
// Duplikowanie węzła
function duplicateNode(node) {
const nodeId = node.id;
const nodeData = state.nodes.find(n => n.id === nodeId);
if (!nodeData) return;
// Nowy identyfikator
const newNodeId = 'node-' + Date.now();
// Duplikowanie węzła
const newNode = node.cloneNode(true);
newNode.id = newNodeId;
// Aktualizacja identyfikatorów wejść/wyjść
const input = newNode.querySelector('.node-input');
const output = newNode.querySelector('.node-output');
if (input) input.dataset.nodeId = newNodeId;
if (output) output.dataset.nodeId = newNodeId;
// Dodanie do obszaru edytora
editorArea.appendChild(newNode);
// Dodanie do stanu
const newNodeData = {
...nodeData,
id: newNodeId,
x: nodeData.x + 20,
y: nodeData.y + 20
};
state.nodes.push(newNodeData);
// Aktualizacja pozycji
newNode.style.left = newNodeData.x + 'px';
newNode.style.top = newNodeData.y + 'px';
// Obsługa przeciągania
makeDraggable(newNode);
makeResizable(newNode);
// Obsługa połączeń
const newInput = newNode.querySelector('.node-input');
const newOutput = newNode.querySelector('.node-output');
if (newInput) newInput.addEventListener('mousedown', startConnection);
if (newOutput) newOutput.addEventListener('mousedown', startConnection);
// Aktualizacja podglądu
updatePreview();
// Aktualizacja licznika
updateNodeCount();
}
// Aktualizacja licznika węzłów
function updateNodeCount() {
nodeCount.textContent = `Węzły: ${state.nodes.length} | Połączenia: ${state.connections.length}`;
}
// Obsługa podziału ekranu
function startDragSplitter(e) {
state.isDraggingSplitter = true;
state.splitterStartX = e.clientX;
const containerRect = document.querySelector('.container').getBoundingClientRect();
state.splitterStartWidth = containerRect.width;
e.preventDefault();
}
document.addEventListener('mousemove', (e) => {
if (!state.isDraggingSplitter) return;
const container = document.querySelector('.container');
const containerRect = container.getBoundingClientRect();
const dx = e.clientX - state.splitterStartX;
const newWidth = state.splitterStartWidth + dx;
// Ograniczenia minimalne
const minWidth = 200;
const maxWidth = containerRect.width - minWidth - splitter.offsetWidth;
let previewWidth = newWidth;
let editorWidth = containerRect.width - newWidth - splitter.offsetWidth;
if (previewWidth < minWidth) {
previewWidth = minWidth;
editorWidth = containerRect.width - minWidth - splitter.offsetWidth;
}
if (editorWidth < minWidth) {
editorWidth = minWidth;
previewWidth = containerRect.width - minWidth - splitter.offsetWidth;
}
document.querySelector('.preview').style.flex = `0 0 ${previewWidth}px`;
document.querySelector('.editor').style.flex = `0 0 ${editorWidth}px`;
});
document.addEventListener('mouseup', () => {
state.isDraggingSplitter = false;
});
// Zapisywanie i wczytywanie
function saveToString() {
return JSON.stringify({
nodes: state.nodes,
connections: state.connections
});
}
function loadFromString(data) {
try {
const parsedData = JSON.parse(data);
// Wyczyść obecny stan
state.nodes = [];
state.connections = [];
editorArea.innerHTML = '';
previewArea.innerHTML = '';
// Dodaj węzły
parsedData.nodes.forEach(nodeData => {
const node = document.createElement('div');
node.className = 'node';
node.id = nodeData.id;
node.style.left = nodeData.x + 'px';
node.style.top = nodeData.y + 'px';
node.style.width = nodeData.width + 'px';
node.style.height = nodeData.height + 'px';
node.style.backgroundColor = nodeData.color;
const header = document.createElement('div');
header.className = 'node-header';
const title = document.createElement('div');
title.className = 'node-title';
title.textContent = nodeData.title;
header.appendChild(title);
node.appendChild(header);
const content = document.createElement('div');
content.className = 'node-content';
content.textContent = nodeData.content;
node.appendChild(content);
const input = document.createElement('div');
input.className = 'node-input';
input.dataset.nodeId = nodeData.id;
input.dataset.type = 'input';
const output = document.createElement('div');
output.className = 'node-output';
output.dataset.nodeId = nodeData.id;
output.dataset.type = 'output';
node.appendChild(input);
node.appendChild(output);
editorArea.appendChild(node);
// Obsługa przeciągania
makeDraggable(node);
makeResizable(node);
// Obsługa połączeń
input.addEventListener('mousedown', startConnection);
output.addEventListener('mousedown', startConnection);
});
// Dodaj połączenia
state.connections = parsedData.connections;
// Aktualizacja połączeń
updateConnections();
// Aktualizacja podglądu
updatePreview();
// Aktualizacja licznika
updateNodeCount();
} catch (e) {
alert('Błąd podczas wczytywania danych: ' + e.message);
}
}
// Eksport i import JSON
function exportToJSON() {
return {
nodes: state.nodes,
connections: state.connections,
metadata: {
format: 'node-editor',
version: '1.0',
timestamp: new Date().toISOString()
}
};
}
function importFromJSON(data) {
if (!data || !data.nodes || !data.connections) {
alert('Nieprawidłowy format danych');
return;
}
// Wyczyść obecny stan
state.nodes = [];
state.connections = [];
editorArea.innerHTML = '';
previewArea.innerHTML = '';
// Dodaj węzły
data.nodes.forEach(nodeData => {
const node = document.createElement('div');
node.className = 'node';
node.id = nodeData.id;
node.style.left = nodeData.x + 'px';
node.style.top = nodeData.y + 'px';
node.style.width = nodeData.width + 'px';
node.style.height = nodeData.height + 'px';
node.style.backgroundColor = nodeData.color;
const header = document.createElement('div');
header.className = 'node-header';
const title = document.createElement('div');
title.className = 'node-title';
title.textContent = nodeData.title;
header.appendChild(title);
node.appendChild(header);
const content = document.createElement('div');
content.className = 'node-content';
content.textContent = nodeData.content;
node.appendChild(content);
const input = document.createElement('div');
input.className = 'node-input';
input.dataset.nodeId = nodeData.id;
input.dataset.type = 'input';
const output = document.createElement('div');
output.className = 'node-output';
output.dataset.nodeId = nodeData.id;
output.dataset.type = 'output';
node.appendChild(input);
node.appendChild(output);
editorArea.appendChild(node);
// Obsługa przeciągania
makeDraggable(node);
makeResizable(node);
// Obsługa połączeń
input.addEventListener('mousedown', startConnection);
output.addEventListener('mousedown', startConnection);
});
// Dodaj połączenia
state.connections = data.connections;
// Aktualizacja stanu
state.nodes = data.nodes;
// Aktualizacja połączeń
updateConnections();
// Aktualizacja podglądu
updatePreview();
// Aktualizacja licznika
updateNodeCount();
}
// Inicjalizacja aplikacji
document.addEventListener('DOMContentLoaded', init);
</script>
</body>
</html>