Spaces:
Running
Running
Download index.html from toolmanduck/flu-lvgl: direct link, hf CLI and curl.
- Browser
- Download file 89.4 kB
-
https://huggingface.co/spaces/toolmanduck/flu-lvgl/resolve/main/index.html
- Command line
-
hf download hf://spaces/toolmanduck/flu-lvgl/index.html
-
curl -L -o index.html https://huggingface.co/spaces/toolmanduck/flu-lvgl/resolve/main/index.html
89.4 kB
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flut to LVGL banger</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdn.jsdelivr.net/npm/interactjs/dist/interact.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
.dropzone {
background-color: #f8fafc;
border: 2px dashed #cbd5e1;
transition: all 0.3s ease;
position: relative;
min-height: 480px;
}
.dropzone.active {
border-color: #3b82f6;
background-color: #eff6ff;
}
.component {
cursor: move;
touch-action: none;
user-select: none;
position: absolute;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
z-index: 10;
}
.component.selected {
box-shadow: 0 0 0 2px #3b82f6;
}
.resize-handle {
position: absolute;
width: 10px;
height: 10px;
background: #3b82f6;
border-radius: 50%;
right: -5px;
bottom: -5px;
cursor: se-resize;
z-index: 20;
}
#previewArea {
background-image: linear-gradient(#e5e7eb 1px, transparent 1px),
linear-gradient(90deg, #e5e7eb 1px, transparent 1px);
background-size: 20px 20px;
position: relative;
overflow: visible;
}
.property-group {
border-left: 3px solid #3b82f6;
}
.tab-button.active {
border-bottom: 2px solid #3b82f6;
color: #3b82f6;
}
.floating-toolbar {
position: absolute;
top: -40px;
left: 50%;
transform: translateX(-50%);
background: white;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
border-radius: 4px;
display: none;
z-index: 30;
}
.component:hover .floating-toolbar {
display: flex;
}
#previewArea {
transform-origin: 0 0;
}
.draggable-source {
cursor: grab;
}
.draggable-source:active {
cursor: grabbing;
}
.column {
display: flex;
flex-direction: column;
}
.row {
display: flex;
flex-direction: row;
}
.list-view-item {
padding: 8px;
border-bottom: 1px solid #e5e7eb;
}
.slider-track {
width: 100%;
height: 6px;
background: #e5e7eb;
border-radius: 3px;
position: relative;
}
.slider-thumb {
width: 16px;
height: 16px;
background: #3b82f6;
border-radius: 50%;
position: absolute;
top: -5px;
left: 50%;
transform: translateX(-50%);
}
#coordsDisplay {
position: fixed;
bottom: 10px;
right: 10px;
background: rgba(0,0,0,0.7);
color: white;
padding: 5px 10px;
border-radius: 4px;
font-family: monospace;
z-index: 1000;
}
</style>
</head>
<body class="bg-gray-50 h-screen flex flex-col">
<!-- Header -->
<header class="bg-white shadow-sm py-3 px-4 flex justify-between items-center">
<div class="flex items-center space-x-2">
<i class="fas fa-code text-blue-500 text-xl"></i>
<h1 class="text-xl font-semibold text-gray-800">Flutter to LVGL Editor</h1>
</div>
<div class="flex space-x-3">
<button id="saveBtn" class="px-4 py-2 bg-green-500 text-white rounded hover:bg-green-600 transition flex items-center space-x-2">
<i class="fas fa-save"></i>
<span>Save</span>
</button>
<button id="loadBtn" class="px-4 py-2 bg-yellow-500 text-white rounded hover:bg-yellow-600 transition flex items-center space-x-2">
<i class="fas fa-folder-open"></i>
<span>Load</span>
</button>
<button id="exportBtn" class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 transition flex items-center space-x-2">
<i class="fas fa-file-export"></i>
<span>Export LVGL Code</span>
</button>
<button id="previewBtn" class="px-4 py-2 bg-purple-500 text-white rounded hover:bg-purple-600 transition flex items-center space-x-2">
<i class="fas fa-eye"></i>
<span>Preview</span>
</button>
</div>
</header>
<div class="flex flex-1 overflow-hidden">
<!-- Components Palette -->
<div class="w-64 bg-white border-r border-gray-200 p-4 overflow-y-auto">
<h2 class="font-medium text-gray-700 mb-3 flex items-center">
<i class="fas fa-shapes mr-2 text-blue-500"></i>
Components
</h2>
<div class="space-y-2">
<div class="component-category">
<h3 class="text-sm font-medium text-gray-500 uppercase tracking-wider mb-2">Containers</h3>
<div class="grid grid-cols-2 gap-2">
<div draggable="true" data-type="container" class="draggable-source p-3 bg-gray-100 rounded border border-gray-200 text-center cursor-move hover:bg-gray-200 transition">
<i class="fas fa-square text-blue-500 mb-1"></i>
<p class="text-xs">Container</p>
</div>
<div draggable="true" data-type="column" class="draggable-source p-3 bg-gray-100 rounded border border-gray-200 text-center cursor-move hover:bg-gray-200 transition">
<i class="fas fa-columns text-blue-500 mb-1"></i>
<p class="text-xs">Column</p>
</div>
<div draggable="true" data-type="row" class="draggable-source p-3 bg-gray-100 rounded border border-gray-200 text-center cursor-move hover:bg-gray-200 transition">
<i class="fas fa-grip-lines text-blue-500 mb-1"></i>
<p class="text-xs">Row</p>
</div>
<div draggable="true" data-type="listview" class="draggable-source p-3 bg-gray-100 rounded border border-gray-200 text-center cursor-move hover:bg-gray-200 transition">
<i class="fas fa-list text-blue-500 mb-1"></i>
<p class="text-xs">List View</p>
</div>
</div>
</div>
<div class="component-category mt-4">
<h3 class="text-sm font-medium text-gray-500 uppercase tracking-wider mb-2">Controls</h3>
<div class="grid grid-cols-2 gap-2">
<div draggable="true" data-type="button" class="draggable-source p-3 bg-gray-100 rounded border border-gray-200 text-center cursor-move hover:bg-gray-200 transition">
<i class="fas fa-hand-pointer text-blue-500 mb-1"></i>
<p class="text-xs">Button</p>
</div>
<div draggable="true" data-type="switch" class="draggable-source p-3 bg-gray-100 rounded border border-gray-200 text-center cursor-move hover:bg-gray-200 transition">
<i class="fas fa-toggle-on text-blue-500 mb-1"></i>
<p class="text-xs">Switch</p>
</div>
<div draggable="true" data-type="slider" class="draggable-source p-3 bg-gray-100 rounded border border-gray-200 text-center cursor-move hover:bg-gray-200 transition">
<i class="fas fa-sliders-h text-blue-500 mb-1"></i>
<p class="text-xs">Slider</p>
</div>
<div draggable="true" data-type="dropdown" class="draggable-source p-3 bg-gray-100 rounded border border-gray-200 text-center cursor-move hover:bg-gray-200 transition">
<i class="fas fa-caret-square-down text-blue-500 mb-1"></i>
<p class="text-xs">Dropdown</p>
</div>
</div>
</div>
<div class="component-category mt-4">
<h3 class="text-sm font-medium text-gray-500 uppercase tracking-wider mb-2">Display</h3>
<div class="grid grid-cols-2 gap-2">
<div draggable="true" data-type="text" class="draggable-source p-3 bg-gray-100 rounded border border-gray-200 text-center cursor-move hover:bg-gray-200 transition">
<i class="fas fa-font text-blue-500 mb-1"></i>
<p class="text-xs">Text</p>
</div>
<div draggable="true" data-type="image" class="draggable-source p-3 bg-gray-100 rounded border border-gray-200 text-center cursor-move hover:bg-gray-200 transition">
<i class="fas fa-image text-blue-500 mb-1"></i>
<p class="text-xs">Image</p>
</div>
<div draggable="true" data-type="icon" class="draggable-source p-3 bg-gray-100 rounded border border-gray-200 text-center cursor-move hover:bg-gray-200 transition">
<i class="fas fa-icons text-blue-500 mb-1"></i>
<p class="text-xs">Icon</p>
</div>
<div draggable="true" data-type="progress" class="draggable-source p-3 bg-gray-100 rounded border border-gray-200 text-center cursor-move hover:bg-gray-200 transition">
<i class="fas fa-spinner text-blue-500 mb-1"></i>
<p class="text-xs">Progress</p>
</div>
</div>
</div>
</div>
</div>
<!-- Main Editor Area -->
<div class="flex-1 flex flex-col overflow-hidden">
<!-- Toolbar -->
<div class="bg-white border-b border-gray-200 p-2 flex items-center justify-between">
<div class="flex space-x-2">
<button id="zoomIn" class="p-2 text-gray-600 hover:text-gray-900 hover:bg-gray-100 rounded">
<i class="fas fa-search-plus"></i>
</button>
<button id="zoomOut" class="p-2 text-gray-600 hover:text-gray-900 hover:bg-gray-100 rounded">
<i class="fas fa-search-minus"></i>
</button>
<button id="zoomReset" class="p-2 text-gray-600 hover:text-gray-900 hover:bg-gray-100 rounded">
<i class="fas fa-expand"></i>
</button>
</div>
<div class="flex items-center space-x-4">
<div class="flex items-center">
<span class="text-sm text-gray-500 mr-2">Screen:</span>
<select id="screenSize" class="border border-gray-300 rounded px-2 py-1 text-sm">
<option value="240x320">240x320</option>
<option value="320x480">320x480</option>
<option value="480x800">480x800</option>
<option value="800x480" selected>800x480</option>
<option value="custom">Custom</option>
</select>
</div>
<div id="customSizeInput" class="hidden flex items-center space-x-2">
<input type="number" placeholder="Width" class="border border-gray-300 rounded px-2 py-1 text-sm w-16">
<span>x</span>
<input type="number" placeholder="Height" class="border border-gray-300 rounded px-2 py-1 text-sm w-16">
</div>
</div>
</div>
<!-- Canvas Area -->
<div class="flex-1 overflow-auto p-8 flex items-center justify-center">
<div id="previewArea" class="dropzone relative bg-white shadow-lg" style="width: 800px; height: 480px;">
<!-- Components will be added here -->
</div>
</div>
</div>
<!-- Properties Panel -->
<div class="w-80 bg-white border-l border-gray-200 overflow-y-auto">
<div class="p-4">
<h2 class="font-medium text-gray-700 mb-3 flex items-center">
<i class="fas fa-sliders-h mr-2 text-blue-500"></i>
Properties
</h2>
<div id="noSelection" class="text-center py-8 text-gray-500">
<i class="fas fa-mouse-pointer text-3xl mb-2"></i>
<p>Select a component to edit its properties</p>
</div>
<div id="propertiesPanel" class="hidden">
<div class="mb-4">
<div class="flex border-b">
<button class="tab-button active px-4 py-2 text-sm font-medium" data-tab="properties">Properties</button>
<button class="tab-button px-4 py-2 text-sm font-medium" data-tab="events">Events</button>
<button class="tab-button px-4 py-2 text-sm font-medium" data-tab="style">Style</button>
</div>
</div>
<div id="propertiesTab" class="tab-content space-y-4">
<div class="property-group pl-2">
<h3 class="text-sm font-medium text-gray-700 mb-2">Layout</h3>
<div class="space-y-2">
<div>
<label class="block text-xs text-gray-500 mb-1">X Position</label>
<input type="number" id="propX" class="w-full border border-gray-300 rounded px-2 py-1 text-sm">
</div>
<div>
<label class="block text-xs text-gray-500 mb-1">Y Position</label>
<input type="number" id="propY" class="w-full border border-gray-300 rounded px-2 py-1 text-sm">
</div>
<div>
<label class="block text-xs text-gray-500 mb-1">Width</label>
<input type="number" id="propWidth" class="w-full border border-gray-300 rounded px-2 py-1 text-sm">
</div>
<div>
<label class="block text-xs text-gray-500 mb-1">Height</label>
<input type="number" id="propHeight" class="w-full border border-gray-300 rounded px-2 py-1 text-sm">
</div>
</div>
</div>
<div id="componentSpecificProps" class="property-group pl-2">
<!-- Component specific properties will be loaded here -->
</div>
</div>
<div id="eventsTab" class="tab-content hidden">
<div class="property-group pl-2">
<h3 class="text-sm font-medium text-gray-700 mb-2">Event Handlers</h3>
<div class="space-y-2">
<div>
<label class="block text-xs text-gray-500 mb-1">On Click</label>
<select class="w-full border border-gray-300 rounded px-2 py-1 text-sm">
<option value="">None</option>
<option value="navigate">Navigate to Screen</option>
<option value="toggle">Toggle State</option>
<option value="custom">Custom Function</option>
</select>
</div>
<div id="eventParams" class="hidden">
<label class="block text-xs text-gray-500 mb-1">Parameters</label>
<input type="text" class="w-full border border-gray-300 rounded px-2 py-1 text-sm">
</div>
</div>
</div>
</div>
<div id="styleTab" class="tab-content hidden">
<div class="property-group pl-2">
<h3 class="text-sm font-medium text-gray-700 mb-2">Colors</h3>
<div class="space-y-2">
<div>
<label class="block text-xs text-gray-500 mb-1">Background</label>
<input type="color" class="w-full h-8 border border-gray-300 rounded">
</div>
<div>
<label class="block text-xs text-gray-500 mb-1">Text Color</label>
<input type="color" class="w-full h-8 border border-gray-300 rounded">
</div>
<div>
<label class="block text-xs text-gray-500 mb-1">Border Color</label>
<input type="color" class="w-full h-8 border border-gray-300 rounded">
</div>
</div>
</div>
<div class="property-group pl-2 mt-4">
<h3 class="text-sm font-medium text-gray-700 mb-2">Text</h3>
<div class="space-y-2">
<div>
<label class="block text-xs text-gray-500 mb-1">Font</label>
<select class="w-full border border-gray-300 rounded px-2 py-1 text-sm">
<option value="montserrat_14">Montserrat 14</option>
<option value="montserrat_16">Montserrat 16</option>
<option value="montserrat_20">Montserrat 20</option>
<option value="custom">Custom</option>
</select>
</div>
<div>
<label class="block text-xs text-gray-500 mb-1">Alignment</label>
<select class="w-full border border-gray-300 rounded px-2 py-1 text-sm">
<option value="left">Left</option>
<option value="center">Center</option>
<option value="right">Right</option>
</select>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<!-- Modals -->
<div id="exportModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center hidden z-50">
<div class="bg-white rounded-lg shadow-xl w-1/2 max-w-2xl">
<div class="p-4 border-b border-gray-200 flex justify-between items-center">
<h3 class="text-lg font-medium">Export LVGL Code</h3>
<button id="closeExportModal" class="text-gray-500 hover:text-gray-700">
<i class="fas fa-times"></i>
</button>
</div>
<div class="p-4">
<div class="flex mb-4">
<button id="exportC" class="px-4 py-2 bg-blue-500 text-white rounded-l hover:bg-blue-600 transition">C Code</button>
<button id="exportHeader" class="px-4 py-2 bg-gray-200 text-gray-700 hover:bg-gray-300 transition">Header File</button>
<button id="exportMakefile" class="px-4 py-2 bg-gray-200 text-gray-700 rounded-r hover:bg-gray-300 transition">Makefile</button>
</div>
<div class="border border-gray-300 rounded p-3 bg-gray-50 h-64 overflow-auto">
<pre id="exportCode" class="text-xs font-mono text-gray-800">// LVGL code will appear here</pre>
</div>
</div>
<div class="p-4 border-t border-gray-200 flex justify-end space-x-3">
<button id="copyCode" class="px-4 py-2 bg-gray-200 text-gray-700 rounded hover:bg-gray-300 transition">Copy</button>
<button id="downloadCode" class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 transition">Download</button>
</div>
</div>
</div>
<div id="previewModal" class="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center hidden z-50">
<div class="bg-white rounded-lg shadow-xl w-4/5 max-w-6xl h-5/6 flex flex-col">
<div class="p-4 border-b border-gray-200 flex justify-between items-center">
<h3 class="text-lg font-medium">UI Preview</h3>
<button id="closePreviewModal" class="text-gray-500 hover:text-gray-700">
<i class="fas fa-times"></i>
</button>
</div>
<div class="flex-1 overflow-auto p-4 flex items-center justify-center">
<div id="livePreview" class="border border-gray-300 bg-white relative" style="width: 800px; height: 480px;">
<!-- Preview will be rendered here -->
</div>
</div>
<div class="p-4 border-t border-gray-200 flex justify-end">
<button id="closePreview" class="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600 transition">Close</button>
</div>
</div>
</div>
<div id="coordsDisplay">X: 0, Y: 0</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
// Global variables
let components = [];
let selectedComponent = null;
let zoomLevel = 1;
let nextId = 1;
let isDragging = false;
// DOM elements
const previewArea = document.getElementById('previewArea');
const propertiesPanel = document.getElementById('propertiesPanel');
const noSelection = document.getElementById('noSelection');
const componentSpecificProps = document.getElementById('componentSpecificProps');
const exportModal = document.getElementById('exportModal');
const previewModal = document.getElementById('previewModal');
const exportCode = document.getElementById('exportCode');
const coordsDisplay = document.getElementById('coordsDisplay');
// Initialize drag and drop functionality
function setupDragAndDrop() {
// Make components draggable from palette
interact('.draggable-source').draggable({
inertia: false,
autoScroll: true,
onstart: function(event) {
// Create a ghost image for dragging
const type = event.target.getAttribute('data-type');
const ghost = document.createElement('div');
ghost.className = 'absolute bg-blue-100 opacity-50 border border-blue-300 rounded';
ghost.style.width = getDefaultWidth(type) + 'px';
ghost.style.height = getDefaultHeight(type) + 'px';
ghost.style.pointerEvents = 'none';
document.body.appendChild(ghost);
event.ghost = ghost;
event.ghostType = type;
},
onmove: function(event) {
if (event.ghost) {
const x = event.clientX - event.ghost.offsetWidth / 2;
const y = event.clientY - event.ghost.offsetHeight / 2;
event.ghost.style.left = x + 'px';
event.ghost.style.top = y + 'px';
// Update coordinates display
const rect = previewArea.getBoundingClientRect();
const canvasX = x - rect.left;
const canvasY = y - rect.top;
coordsDisplay.textContent = `X: ${Math.round(canvasX)}, Y: ${Math.round(canvasY)}`;
}
},
onend: function(event) {
if (event.ghost) {
// Remove ghost image
event.ghost.remove();
// Create actual component at drop position
const rect = previewArea.getBoundingClientRect();
const x = event.clientX - rect.left;
const y = event.clientY - rect.top;
// Only create if dropped inside preview area
if (x >= 0 && y >= 0 && x <= rect.width && y <= rect.height) {
createComponent(event.ghostType, x, y);
}
}
}
});
// Make preview area a dropzone
interact(previewArea).dropzone({
accept: '.draggable-source',
overlap: 0.75,
ondropactivate: function(event) {
previewArea.classList.add('active');
},
ondragenter: function(event) {
previewArea.classList.add('active');
},
ondragleave: function(event) {
previewArea.classList.remove('active');
},
ondrop: function(event) {
previewArea.classList.remove('active');
},
ondropdeactivate: function(event) {
previewArea.classList.remove('active');
}
});
}
// Get default width for component type
function getDefaultWidth(type) {
switch(type) {
case 'button': return 120;
case 'text': return 100;
case 'container': return 200;
case 'image': return 150;
case 'switch': return 60;
case 'slider': return 200;
case 'progress': return 200;
case 'column': return 200;
case 'row': return 200;
case 'listview': return 200;
case 'dropdown': return 120;
default: return 100;
}
}
// Get default height for component type
function getDefaultHeight(type) {
switch(type) {
case 'button': return 40;
case 'text': return 30;
case 'container': return 150;
case 'image': return 100;
case 'switch': return 30;
case 'slider': return 40;
case 'progress': return 20;
case 'column': return 150;
case 'row': return 80;
case 'listview': return 200;
case 'dropdown': return 40;
default: return 50;
}
}
// Create a new component
function createComponent(type, x, y) {
const id = nextId++;
const width = getDefaultWidth(type);
const height = getDefaultHeight(type);
const component = {
id,
type,
x,
y,
width,
height,
properties: {}
};
// Create DOM element
const element = document.createElement('div');
element.className = 'component bg-white border border-gray-300 rounded';
element.setAttribute('data-id', id);
element.setAttribute('data-type', type);
element.setAttribute('data-x', x);
element.setAttribute('data-y', y);
element.setAttribute('data-width', width);
element.setAttribute('data-height', height);
element.style.left = '0';
element.style.top = '0';
element.style.width = `${width}px`;
element.style.height = `${height}px`;
element.style.transform = `translate(${x}px, ${y}px)`;
// Add floating toolbar
const toolbar = document.createElement('div');
toolbar.className = 'floating-toolbar space-x-1 p-1';
toolbar.innerHTML = `
<button class="p-1 text-gray-600 hover:text-gray-900 hover:bg-gray-100 rounded" title="Delete">
<i class="fas fa-trash text-xs"></i>
</button>
<button class="p-1 text-gray-600 hover:text-gray-900 hover:bg-gray-100 rounded" title="Duplicate">
<i class="fas fa-copy text-xs"></i>
</button>
<button class="p-1 text-gray-600 hover:text-gray-900 hover:bg-gray-100 rounded" title="Bring to Front">
<i class="fas fa-arrow-up text-xs"></i>
</button>
`;
element.appendChild(toolbar);
// Add resize handle
const resizeHandle = document.createElement('div');
resizeHandle.className = 'resize-handle';
element.appendChild(resizeHandle);
// Set content based on type
switch (type) {
case 'button':
element.innerHTML += '<button class="w-full h-full flex items-center justify-center">Button</button>';
component.properties.text = 'Button';
component.properties.color = '#3b82f6';
break;
case 'text':
element.innerHTML += '<div class="w-full h-full flex items-center justify-center p-2">Text</div>';
component.properties.text = 'Text';
component.properties.fontSize = 16;
break;
case 'container':
element.innerHTML += '<div class="w-full h-full bg-gray-100"></div>';
component.properties.color = '#f3f4f6';
break;
case 'image':
element.innerHTML += '<div class="w-full h-full bg-gray-200 flex items-center justify-center"><i class="fas fa-image text-gray-400"></i></div>';
component.properties.src = '';
break;
case 'switch':
element.innerHTML += '<div class="w-full h-full flex items-center justify-center"><div class="w-12 h-6 bg-gray-300 rounded-full p-1"><div class="w-4 h-4 bg-white rounded-full shadow-md"></div></div></div>';
component.properties.state = false;
break;
case 'slider':
element.innerHTML += '<div class="w-full h-full flex items-center justify-center px-4"><div class="slider-track"><div class="slider-thumb"></div></div></div>';
component.properties.value = 50;
component.properties.min = 0;
component.properties.max = 100;
break;
case 'progress':
element.innerHTML += '<div class="w-full h-full flex items-center justify-center px-4"><div class="w-full h-4 bg-gray-300 rounded-full"><div class="w-1/2 h-4 bg-blue-500 rounded-full"></div></div></div>';
component.properties.value = 50;
break;
case 'column':
element.innerHTML += '<div class="w-full h-full column bg-gray-100 p-2 space-y-2"><div class="bg-blue-100 rounded p-2">Item 1</div><div class="bg-blue-100 rounded p-2">Item 2</div></div>';
component.properties.items = ['Item 1', 'Item 2'];
break;
case 'row':
element.innerHTML += '<div class="w-full h-full row bg-gray-100 p-2 space-x-2"><div class="bg-blue-100 rounded p-2">Item 1</div><div class="bg-blue-100 rounded p-2">Item 2</div></div>';
component.properties.items = ['Item 1', 'Item 2'];
break;
case 'listview':
element.innerHTML += '<div class="w-full h-full bg-gray-100 overflow-y-auto"><div class="list-view-item">Item 1</div><div class="list-view-item">Item 2</div><div class="list-view-item">Item 3</div></div>';
component.properties.items = ['Item 1', 'Item 2', 'Item 3'];
break;
case 'dropdown':
element.innerHTML += '<div class="w-full h-full flex items-center justify-between px-3 border border-gray-300 rounded bg-white"><span>Select</span><i class="fas fa-caret-down"></i></div>';
component.properties.options = ['Option 1', 'Option 2'];
break;
default:
element.innerHTML += `<div class="w-full h-full flex items-center justify-center">${type}</div>`;
}
// Add to DOM and components array
previewArea.appendChild(element);
components.push(component);
// Make the new component draggable and resizable
setupComponentInteractions(element);
// Select the new component
selectComponent(element);
// Update preview
updateLivePreview();
}
// Setup interactions for a component
function setupComponentInteractions(element) {
// Click to select
element.addEventListener('click', function(e) {
if (e.target === element || e.target.classList.contains('component')) {
selectComponent(element);
e.stopPropagation();
}
});
// Delete button
const deleteBtn = element.querySelector('.floating-toolbar button:first-child');
deleteBtn.addEventListener('click', function() {
const id = element.getAttribute('data-id');
components = components.filter(c => c.id != id);
element.remove();
if (selectedComponent && selectedComponent.getAttribute('data-id') == id) {
deselectComponent();
}
updateLivePreview();
});
// Duplicate button
const duplicateBtn = element.querySelector('.floating-toolbar button:nth-child(2)');
duplicateBtn.addEventListener('click', function() {
const id = element.getAttribute('data-id');
const component = components.find(c => c.id == id);
if (component) {
const newX = component.x + 20;
const newY = component.y + 20;
const newComponent = JSON.parse(JSON.stringify(component));
newComponent.id = nextId++;
newComponent.x = newX;
newComponent.y = newY;
const newElement = document.createElement('div');
newElement.className = element.className;
newElement.setAttribute('data-id', newComponent.id);
newElement.setAttribute('data-type', newComponent.type);
newElement.setAttribute('data-x', newX);
newElement.setAttribute('data-y', newY);
newElement.setAttribute('data-width', newComponent.width);
newElement.setAttribute('data-height', newComponent.height);
newElement.style.left = '0';
newElement.style.top = '0';
newElement.style.width = `${newComponent.width}px`;
newElement.style.height = `${newComponent.height}px`;
newElement.style.transform = `translate(${newX}px, ${newY}px)`;
newElement.innerHTML = element.innerHTML;
previewArea.appendChild(newElement);
components.push(newComponent);
setupComponentInteractions(newElement);
selectComponent(newElement);
updateLivePreview();
}
});
// Bring to front button
const bringToFrontBtn = element.querySelector('.floating-toolbar button:last-child');
bringToFrontBtn.addEventListener('click', function() {
element.style.zIndex = '100';
updateLivePreview();
});
// Make component draggable
interact(element).draggable({
inertia: false,
autoScroll: true,
onstart: function(event) {
isDragging = true;
selectComponent(element);
},
onmove: function(event) {
const target = event.target;
const x = (parseFloat(target.getAttribute('data-x')) || 0) + event.dx;
const y = (parseFloat(target.getAttribute('data-y')) || 0) + event.dy;
target.style.transform = `translate(${x}px, ${y}px)`;
target.setAttribute('data-x', x);
target.setAttribute('data-y', y);
// Update coordinates display
coordsDisplay.textContent = `X: ${Math.round(x)}, Y: ${Math.round(y)}`;
// Update component data
const id = target.getAttribute('data-id');
const component = components.find(c => c.id == id);
if (component) {
component.x = x;
component.y = y;
}
// Update property inputs
document.getElementById('propX').value = Math.round(x);
document.getElementById('propY').value = Math.round(y);
// Update preview in real-time
updateLivePreview();
},
onend: function(event) {
isDragging = false;
// Snap to grid
const target = event.target;
const x = Math.round((parseFloat(target.getAttribute('data-x')) || 0) / 10) * 10;
const y = Math.round((parseFloat(target.getAttribute('data-y')) || 0) / 10) * 10;
target.style.transform = `translate(${x}px, ${y}px)`;
target.setAttribute('data-x', x);
target.setAttribute('data-y', y);
// Update component data
const id = target.getAttribute('data-id');
const component = components.find(c => c.id == id);
if (component) {
component.x = x;
component.y = y;
}
// Update property inputs
document.getElementById('propX').value = x;
document.getElementById('propY').value = y;
// Final preview update
updateLivePreview();
}
}).resizable({
edges: { right: true, bottom: true },
inertia: false,
onstart: function(event) {
selectComponent(element);
},
onmove: function(event) {
const target = event.target;
let width = parseFloat(target.getAttribute('data-width')) || 100;
let height = parseFloat(target.getAttribute('data-height')) || 50;
width = Math.max(50, width + event.deltaRect.width);
height = Math.max(30, height + event.deltaRect.height);
target.style.width = `${width}px`;
target.style.height = `${height}px`;
target.setAttribute('data-width', width);
target.setAttribute('data-height', height);
// Update component data
const id = target.getAttribute('data-id');
const component = components.find(c => c.id == id);
if (component) {
component.width = width;
component.height = height;
}
// Update property inputs
document.getElementById('propWidth').value = Math.round(width);
document.getElementById('propHeight').value = Math.round(height);
// Update preview in real-time
updateLivePreview();
},
onend: function(event) {
// Snap to grid
const target = event.target;
const width = Math.round((parseFloat(target.getAttribute('data-width')) || 100) / 10) * 10;
const height = Math.round((parseFloat(target.getAttribute('data-height')) || 50) / 10) * 10;
target.style.width = `${width}px`;
target.style.height = `${height}px`;
target.setAttribute('data-width', width);
target.setAttribute('data-height', height);
// Update component data
const id = target.getAttribute('data-id');
const component = components.find(c => c.id == id);
if (component) {
component.width = width;
component.height = height;
}
// Update property inputs
document.getElementById('propWidth').value = width;
document.getElementById('propHeight').value = height;
// Final preview update
updateLivePreview();
}
});
}
// Select a component
function selectComponent(element) {
// Deselect current component
if (selectedComponent) {
selectedComponent.classList.remove('selected');
}
// Select new component
selectedComponent = element;
element.classList.add('selected');
// Show properties panel
propertiesPanel.classList.remove('hidden');
noSelection.classList.add('hidden');
// Load component properties
const id = element.getAttribute('data-id');
const component = components.find(c => c.id == id);
if (component) {
loadComponentProperties(component);
}
}
// Deselect component
function deselectComponent() {
if (selectedComponent) {
selectedComponent.classList.remove('selected');
selectedComponent = null;
}
// Hide properties panel
propertiesPanel.classList.add('hidden');
noSelection.classList.remove('hidden');
}
// Load component properties into panel
function loadComponentProperties(component) {
// Clear previous properties
componentSpecificProps.innerHTML = '';
// Update position/size inputs
document.getElementById('propX').value = Math.round(component.x);
document.getElementById('propY').value = Math.round(component.y);
document.getElementById('propWidth').value = Math.round(component.width);
document.getElementById('propHeight').value = Math.round(component.height);
// Add event listeners for position/size inputs
document.getElementById('propX').addEventListener('change', function() {
const newX = parseInt(this.value);
component.x = newX;
const element = document.querySelector(`.component[data-id="${component.id}"]`);
if (element) {
element.style.transform = `translate(${newX}px, ${component.y}px)`;
element.setAttribute('data-x', newX);
updateLivePreview();
}
});
document.getElementById('propY').addEventListener('change', function() {
const newY = parseInt(this.value);
component.y = newY;
const element = document.querySelector(`.component[data-id="${component.id}"]`);
if (element) {
element.style.transform = `translate(${component.x}px, ${newY}px)`;
element.setAttribute('data-y', newY);
updateLivePreview();
}
});
document.getElementById('propWidth').addEventListener('change', function() {
const newWidth = parseInt(this.value);
component.width = newWidth;
const element = document.querySelector(`.component[data-id="${component.id}"]`);
if (element) {
element.style.width = `${newWidth}px`;
element.setAttribute('data-width', newWidth);
updateLivePreview();
}
});
document.getElementById('propHeight').addEventListener('change', function() {
const newHeight = parseInt(this.value);
component.height = newHeight;
const element = document.querySelector(`.component[data-id="${component.id}"]`);
if (element) {
element.style.height = `${newHeight}px`;
element.setAttribute('data-height', newHeight);
updateLivePreview();
}
});
// Add type-specific properties
let propsHtml = '';
switch (component.type) {
case 'button':
propsHtml = `
<div class="property-group pl-2">
<h3 class="text-sm font-medium text-gray-700 mb-2">Button</h3>
<div class="space-y-2">
<div>
<label class="block text-xs text-gray-500 mb-1">Text</label>
<input type="text" value="${component.properties.text || 'Button'}" class="w-full border border-gray-300 rounded px-2 py-1 text-sm">
</div>
<div>
<label class="block text-xs text-gray-500 mb-1">Color</label>
<input type="color" value="${component.properties.color || '#3b82f6'}" class="w-full h-8 border border-gray-300 rounded">
</div>
</div>
</div>
`;
break;
case 'text':
propsHtml = `
<div class="property-group pl-2">
<h3 class="text-sm font-medium text-gray-700 mb-2">Text</h3>
<div class="space-y-2">
<div>
<label class="block text-xs text-gray-500 mb-1">Content</label>
<input type="text" value="${component.properties.text || 'Text'}" class="w-full border border-gray-300 rounded px-2 py-1 text-sm">
</div>
<div>
<label class="block text-xs text-gray-500 mb-1">Font Size</label>
<input type="number" value="${component.properties.fontSize || 16}" class="w-full border border-gray-300 rounded px-2 py-1 text-sm">
</div>
</div>
</div>
`;
break;
case 'container':
propsHtml = `
<div class="property-group pl-2">
<h3 class="text-sm font-medium text-gray-700 mb-2">Container</h3>
<div class="space-y-2">
<div>
<label class="block text-xs text-gray-500 mb-1">Background</label>
<input type="color" value="${component.properties.color || '#f3f4f6'}" class="w-full h-8 border border-gray-300 rounded">
</div>
</div>
</div>
`;
break;
case 'image':
propsHtml = `
<div class="property-group pl-2">
<h3 class="text-sm font-medium text-gray-700 mb-2">Image</h3>
<div class="space-y-2">
<div>
<label class="block text-xs text-gray-500 mb-1">Source</label>
<input type="text" placeholder="Image URL" class="w-full border border-gray-300 rounded px-2 py-1 text-sm">
</div>
</极端的
</div>
`;
break;
case 'switch':
propsHtml = `
<div class="property-group pl-2">
<h3 class="text-sm font-medium text-gray-700 mb-2">Switch</h3>
<div class="space-y-2">
<div>
<label class="block text-xs text-gray-500 mb-1">State</label>
<select class="w-full border border-gray-300 rounded px-2 py-1 text-sm">
<option value="false" ${component.properties.state === false ? 'selected' : ''}>Off</option>
<option value="true" ${component.properties.state === true ? 'selected' : ''}>On</option>
</select>
</div>
</div>
</div>
`;
break;
case 'slider':
propsHtml = `
<极端的class="property-group pl-2">
<h3 class="text-sm font-medium text-gray-700 mb-2">Slider</h3>
<div class="space-y-2">
<div>
<label class="block text-xs text-gray-500 mb-1">Value</label>
<input type="number" value="${component.properties.value || 50}" class="w-full border border-gray-300 rounded px-2 py-1 text-sm">
</div>
<div>
<label class="block text-xs text-gray-500 mb-1">Min</label>
<input type="number" value="${component.properties.min || 0}" class="w-full border border-gray-300 rounded px-2 py-1 text-sm">
</div>
<div>
<label class="block text-xs text-gray极端的 mb-1">Max</label>
<input type="number" value="${component.properties.max || 100}" class="w-full border border-gray-300 rounded px-2 py-1 text-sm">
</div>
</div>
</div>
`;
break;
case 'progress':
propsHtml = `
<div class="property-group pl-2">
<h3 class="text-sm font-medium text-gray-700 mb-2">Progress</h3>
<div class="space-y-2">
<div>
<label class="block text-xs text-gray-500 mb-1">Value</label>
<input type="number" value="${component.properties.value || 50}" class="w-full border border-gray-300 rounded px-2 py-1 text-sm">
</div>
</div>
</div>
`;
break;
case 'column':
case 'row':
propsHtml = `
<div class="property-group pl-2">
<h3 class="text-sm font-medium text-gray-700 mb-2">${component.type === 'column' ? 'Column' : 'Row'}</h3>
<div class="space-y-2">
<div>
<label class="block text-xs text-gray-500 mb-1">Items</label>
<div class="space-y-2">
${component.properties.items.map((item, index) => `
<div class="flex items-center">
<input type="text" value="${item}" class="flex-1 border border-gray-300 rounded px-2 py-1 text-sm">
<button class="ml-2 text-red-500">
<i class="fas fa-times"></i>
</button>
</div>
`).join('')}
<button class="w-full mt-2 px-2 py-1 bg-gray-100 text-gray-700 rounded hover:bg-gray-200 text-sm">
<i class="fas fa-plus mr-1"></i> Add Item
</button>
</div>
</div>
</div>
</div>
`;
break;
case 'listview':
propsHtml = `
<div class="property-group pl-2">
<h3 class="text-sm font-medium text-gray-700 mb-2">List View</h3>
<div class="极端的space-y-2">
<div>
<label class="block text-xs text-gray-500 mb-1">Items</label>
<div class="space-y-2">
${component.properties.items.map((item, index) => `
<div class="flex items-center">
<input type="text" value="${item}" class="flex-1 border border-gray-300 rounded px-2 py-1 text-sm">
<button class="ml-2 text-red-500">
<i class="fas fa-times"></i>
</button>
</div>
`).join('')}
<button class="w-full mt-2 px-2 py-1 bg-gray-100 text-gray-700 rounded hover:bg-gray-200 text-sm">
<i class="fas fa-plus mr-1"></i> Add Item
</button>
</div>
</div>
</div>
</div>
`;
break;
case 'dropdown':
propsHtml = `
<div class="property-group pl-2">
<h3 class="text-sm font-medium text-gray-700 mb-2">Dropdown</h3>
<div class="space-y-2">
<div>
<label class="block text-xs text-gray-500 mb-1">Options</label>
<div class="space-y-2">
${component.properties.options.map((option, index) => `
<div class="flex items-center">
<input type="text" value="${option}" class="flex-1 border border-gray-300 rounded px-2 py-1 text-sm">
<button class="ml-2 text-red-500">
<i class="fas fa-times"></极端的</button>
</div>
`).join('')}
<button class="w-full mt-2 px-2 py-1 bg-gray-100 text-gray-700 rounded hover:bg-gray-200 text-sm">
<i class="fas fa-plus mr-1"></i> Add Option
</button>
</div>
</div>
</div>
</div>
`;
break;
default:
propsHtml = `
<div class="property-group pl-2">
<h3 class="text-sm font-medium text-gray-700 mb-2">${component.type}</h3>
<div class="space-y-2">
<div>
<label class="block text-xs text-gray-500 mb-1">Properties</label>
<p class="text-xs text-gray-500">No specific properties for this component</p>
</div>
</div>
</div>
`;
}
componentSpecificProps.innerHTML = propsHtml;
// Setup property change listeners
setupPropertyChangeListeners(component);
}
// Setup listeners for property changes
function setupPropertyChangeListeners(component) {
const element = document.querySelector(`.component[data-id="${component.id}"]`);
// Text properties
const textInput = componentSpecificProps.querySelector('input[type="text"]');
if (textInput) {
textInput.addEventListener('change', function() {
component.properties.text = this.value;
if (element) {
const contentElement = element.querySelector('button, div');
if (contentElement) {
contentElement.textContent = this.value;
}
}
updateLivePreview();
});
}
// Color properties
const colorInput = componentSpecificProps.querySelector('input[type="color"]');
if (colorInput) {
colorInput.addEventListener('change', function() {
component.properties.color = this.value;
if (element) {
if (component.type === 'button') {
element.querySelector('button').style.backgroundColor = this.value;
} else if (component.type === 'container') {
element.style.backgroundColor = this.value;
}
}
updateLivePreview();
});
}
// Number properties
const numberInputs = componentSpecificProps.querySelectorAll('input[type="number"]');
numberInputs.forEach(input => {
input.addEventListener('change', function() {
const propName = this.previousElementSibling.textContent.trim().toLowerCase();
component.properties[propName] = parseFloat(this.value);
// Update visual representation if needed
if (component.type === 'slider' || component.type === 'progress') {
const progressBar = element.querySelector('div > div');
if (progressBar) {
const min = component.properties.min || 0;
const max = component.properties.max || 100;
const value = component.properties.value || 50;
const percentage = ((value - min) / (max - min)) * 100;
progressBar.style.width = `${percentage}%`;
}
}
updateLivePreview();
});
});
// Select properties
const selectInput = componentSpecificProps.querySelector('select');
if (selectInput) {
selectInput.addEventListener('change', function() {
component.properties.state = this.value === 'true';
if (element && component.type === 'switch') {
const switchHandle = element.querySelector('div > div');
if (switchHandle) {
if (this.value === 'true') {
switchHandle.style.transform = 'translateX(24px)';
switchHandle.parentElement.style.backgroundColor = '#3b82f6';
} else {
switchHandle.style.transform = 'translateX(0)';
switchHandle.parentElement.style.backgroundColor = '#d1d5db';
}
}
}
updateLivePreview();
});
}
}
// Tab switching
document.querySelectorAll('.tab-button').forEach(button => {
button.addEventListener('click', function() {
// Update active tab button
document.querySelectorAll('.tab-button').forEach(btn => {
btn.classList.remove('active');
});
this.classList.add('active');
// Show corresponding tab content
const tab = this.getAttribute('data-tab');
document.querySelectorAll('.tab-content').forEach(content => {
content.classList.add('hidden');
});
document.getElementById(`${tab}Tab`).classList.remove('hidden');
});
});
// Zoom controls
document.getElementById('zoomIn').addEventListener('click', function() {
zoomLevel = Math.min(2, zoomLevel + 0.1);
previewArea.style.transform = `scale(${zoomLevel})`;
});
document.getElementById('zoomOut').addEventListener('click', function() {
zoomLevel = Math.max(0.5, zoomLevel - 0.1);
previewArea.style.transform = `scale(${zoomLevel})`;
});
document.getElementById('zoomReset').addEventListener('click', function() {
zoomLevel = 1;
previewArea.style.transform = `scale(${zoomLevel})`;
});
// Screen size selector
document.getElementById('screenSize').addEventListener('change', function() {
if (this.value === 'custom') {
document.getElementById('customSizeInput').classList.remove('hidden');
} else {
document.getElementById('customSizeInput').classList.add('hidden');
const [width, height] = this.value.split('x');
previewArea.style.width = `${width}px`;
previewArea.style.height = `${height}px`;
updateLivePreview();
}
});
// Save button
document.getElementById('saveBtn').addEventListener('click', function() {
const data = {
components: components,
screenSize: {
width: previewArea.style.width,
height: previewArea.style.height
}
};
const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'ui_design.json';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
// Show saved notification
const originalText = this.innerHTML;
this.innerHTML = '<i class="fas fa-check"></i><span>Saved</span>';
setTimeout(() => {
this.innerHTML = originalText;
}, 2000);
});
// Load button
document.getElementById('loadBtn').addEventListener('click', function() {
const input = document.createElement('input');
input.type = 'file';
input.accept = '.json';
input.onchange = e => {
const file = e.target.files[0];
const reader = new FileReader();
reader.readAsText(file, 'UTF-8');
reader.onload = readerEvent => {
try {
const data = JSON.parse(readerEvent.target.result);
// Clear existing components
components = [];
document.querySelectorAll('.component').forEach(el => el.remove());
nextId = 1;
// Set screen size
if (data.screenSize) {
previewArea.style.width = data.screenSize.width;
previewArea.style.height = data.screenSize.height;
}
// Create components from loaded data
if (data.components && Array.isArray(data.components)) {
data.components.forEach(compData => {
const element = document.createElement('div');
element.className = 'component bg-white border border-gray-300 rounded';
element.setAttribute('data-id', compData.id);
element.setAttribute('data-type', compData.type);
element.setAttribute('data-x', compData.x);
element.setAttribute('data-y', compData.y);
element.setAttribute('data-width', compData.width);
element.setAttribute('data-height', compData.height);
element.style.left = '0';
element.style.top = '0';
element.style.width = `${compData.width}px`;
element.style.height = `${compData.height}px`;
element.style.transform = `translate(${compData.x}px, ${compData.y}px)`;
// Add floating toolbar
const toolbar = document.createElement('div');
toolbar.className = 'floating-toolbar space-x-1 p-1';
toolbar.innerHTML = `
<button class="p-1 text-gray-600 hover:text-gray-900 hover:bg-gray-100 rounded" title="Delete">
<i class="fas fa-trash text-xs"></i>
</button>
<button class="p-1 text-gray-600 hover:text-gray-900 hover:bg-gray-100 rounded" title="Duplicate">
<i class="fas fa-copy text-xs"></i>
</button>
<button class="p-1 text-gray-600 hover:text-gray-900 hover:bg-gray-100 rounded" title="Bring to Front">
<i class="fas fa-arrow-up text-xs"></i>
</button>
`;
element.appendChild(toolbar);
// Add resize handle
const resizeHandle = document.createElement('div');
resizeHandle.className = 'resize-handle';
element.appendChild(resizeHandle);
// Set content based on type
switch (compData.type) {
case 'button':
element.innerHTML += `<button class="w-full h-full flex items-center justify-center" style="background-color: ${compData.properties.color || '#3b82f6'}">${compData.properties.text || 'Button'}</button>`;
break;
case 'text':
element.innerHTML += `<div class="w-full h-full flex items-center justify-center p-2" style="font-size: ${compData.properties.fontSize || 16}px">${compData.properties.text || 'Text'}</div>`;
break;
case 'container':
element.innerHTML += `<div class="w-full h-full" style="background-color: ${compData.properties.color || '#f3f4f6'}"></div>`;
break;
case 'image':
element.innerHTML += '<div class="w-full h-full bg-gray-200 flex items-center justify-center"><i class="fas fa-image text-gray-400"></i></div>';
break;
case 'switch':
element.innerHTML += `<div class="w-full h-full flex items-center justify-center">
<div class="w-12 h-6 ${compData.properties.state ? 'bg-blue-500' : 'bg-gray-300'} rounded-full p-1">
<div class="w-4 h-4 bg-white rounded-full shadow-md" style="transform: ${compData.properties.state ? 'translateX(24px)' : 'translateX(0)'}"></div>
</div>
</div>`;
break;
case 'slider':
const sliderValue = compData.properties.value || 50;
const sliderMin = compData.properties.min || 0;
const sliderMax = compData.properties.max || 100;
const sliderPercentage = ((sliderValue - sliderMin) / (sliderMax - sliderMin)) * 100;
element.innerHTML += `<div class="w-full h-full flex items-center justify-center px-4">
<div class="slider-track">
<div class="slider-thumb" style="left: ${sliderPercentage}%"></div>
</div>
</div>`;
break;
case 'progress':
const progressValue = compData.properties.value || 50;
element.innerHTML += `<div class="w-full h-full flex items-center justify-center px-4">
<div class="w-full h-4 bg-gray-300 rounded-full">
<div class="h-4 bg-blue-500 rounded-full" style="width: ${progressValue}%"></div>
</div>
</div>`;
break;
case 'column':
element.innerHTML += `<div class="w-full h-full column bg-gray-100 p-2 space-y-2">
${compData.properties.items.map(item => `<div class="bg-blue-100 rounded p-2">${item}</div>`).join('')}
</div>`;
break;
case 'row':
element.innerHTML += `<div class="w-full h-full row bg-gray-100 p-2 space-x-2">
${compData.properties.items.map(item => `<div class="bg-blue-100 rounded p-2">${item}</div>`).join('')}
</div>`;
break;
case 'listview':
element.innerHTML += `<div class="w-full h-full bg-gray-100 overflow-y-auto">
${compData.properties.items.map(item => `<div class="list-view-item">${item}</div>`).join('')}
</div>`;
break;
case 'dropdown':
element.innerHTML += `<div class="w-full h-full flex items-center justify-between px-3 border border-gray-300 rounded bg-white">
<span>Select</span><i class="fas fa-caret-down"></i>
</div>`;
break;
default:
element.innerHTML += `<div class="w-full h-full flex items-center justify-center">${compData.type}</div>`;
}
// Add to DOM and components array
previewArea.appendChild(element);
components.push(compData);
// Make the new component draggable and resizable
setupComponentInteractions(element);
// Update nextId
if (compData.id >= nextId) {
nextId = compData.id + 1;
}
});
}
// Update preview
updateLivePreview();
// Show loaded notification
const originalText = this.innerHTML;
this.innerHTML = '<i class="fas fa-check"></i><span>Loaded</span>';
setTimeout(() => {
this.innerHTML = originalText;
}, 2000);
} catch (e) {
alert('Error loading file: ' + e.message);
}
};
};
input.click();
});
// Export button
document.getElementById('exportBtn').addEventListener('click', function() {
exportModal.classList.remove('hidden');
generateExportCode();
});
document.getElementById('closeExportModal').addEventListener('click', function() {
exportModal.classList.add('hidden');
});
document.getElementById('copyCode').addEventListener('click', function() {
const range = document.createRange();
range.selectNode(exportCode);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
document.execCommand('copy');
window.getSelection().removeAllRanges();
// Show copied notification
const originalText = this.textContent;
this.textContent = 'Copied!';
setTimeout(() => {
this.textContent = originalText;
}, 2000);
});
document.getElementById('downloadCode').addEventListener('click', function() {
const blob = new Blob([exportCode.textContent], { type: 'text/plain' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = 'lvgl_code.c';
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
// Preview button
document.getElementById('previewBtn').addEventListener('click', function() {
previewModal.classList.remove('hidden');
updateLivePreview();
});
document.getElementById('closePreviewModal').addEventListener('click', function() {
previewModal.classList.add('hidden');
});
document.getElementById('closePreview').addEventListener('click', function() {
previewModal.classList.add('hidden');
});
// Generate export code
function generateExportCode() {
let code = `// LVGL UI Code\n`;
code += `// Generated by Flutter to LVGL Editor\n\n`;
code += `#include "lvgl.h"\n\n`;
// Create screen
code += `void create_ui(lv_obj_t *parent) {\n`;
code += ` lv_obj_t *screen = lv_obj_create(parent);\n`;
code += ` lv_obj_set_size(screen, ${previewArea.style.width}, ${previewArea.style.height});\n`;
code += ` lv_obj_set_style_bg_color(screen, lv_color_hex(0xFFFFFF), LV_PART_MAIN);\n\n`;
// Add components
components.forEach(component => {
code += ` // ${component.type} component\n`;
code += ` l极端的_obj_t *${component.type}_${component.id} = lv_${component.type}_create(screen);\n`;
code += ` lv_obj_set_pos(${component.type}_${component.id}, ${component.x}, ${component.y});\n`;
code += ` lv_obj_set_size(${component.type}_${component.id}, ${component.width}, ${component.height});\n`;
// Add properties
if (component.type === 'button') {
code += ` lv_obj_t *${component.type}_${component.id}_label = lv_label_create(${component.type}_${component.id});\n`;
code += ` lv_label_set_text(${component.type}_${component.id}_label, "${component.properties.text || 'Button'}");\n`;
code += ` lv_obj_set_style_bg_color(${component.type}_${component.id}, lv_color_hex(0x${component.properties.color?.substring(1) || '3b82f6'}), LV_PART_MAIN);\n`;
code += ` lv_obj_center(${component.type}_${component.id}_label);\n`;
} else if (component.type === 'text') {
code += ` lv_label_set_text(${component.type}_${component.id}, "${component.properties.text || 'Text'}");\n`;
code += ` lv_obj_set_style_text_font(${component.type}_${component.id}, &lv_font_montserrat_${component.properties.fontSize || 16}, LV_PART_MAIN);\n`;
} else if (component.type === 'container') {
code += ` lv_obj_set_style_bg_color(${component.type}_${component.id}, lv_color_hex(0x${component.properties.color?.substring(1) || 'f3f4f6'}), LV_PART_MAIN);\n`;
}
code += `\n`;
});
code += `}\n`;
exportCode.textContent = code;
}
// Update live preview
function updateLivePreview() {
const livePreview = document.getElementById('livePreview');
if (!livePreview) return;
livePreview.innerHTML = '';
// Set preview size
livePreview.style.width = previewArea.style.width;
livePreview.style.height = previewArea.style.height;
// Clone components
components.forEach(component => {
const element = document.createElement('div');
element.className = 'absolute';
element.style.left = `${component.x}px`;
element.style.top = `${component.y}px`;
element.style.width = `${component.width}px`;
element.style.height = `${component.height}px`;
switch (component.type) {
case 'button':
element.innerHTML = `<button class="w-full h-full flex items-center justify-center" style="background-color: ${component.properties.color || '#3b82f6'}">${component.properties.text || 'Button'}</button>`;
break;
case '
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Flut to LVGL banger</title>
<script src="https://cdn.tailwindcss.com"></script>
<script src="https://cdn.jsdelivr.net/npm/interactjs/dist/interact.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
.dropzone {
background-color: #f8fafc;
border: 2px dashed #cbd5e1;
transition: all 0.3s ease;
position: relative;
min-height: 480px;
}
.dropzone.active {
border-color: #3b82f6;
background-color: #eff6ff;
}
.component {
cursor: move;
touch-action: none;
user-select: none;
position: absolute;
box-shadow: 0 1px 3px rgba(0,0,0,0.1);
z-index: 10;
}
.component.selected {
box-shadow: 0 0 0 2px #3b82f6;
}
.resize-handle {
position: absolute;
width: 10px;
height: 10px;
background: #3b82f6;
border-radius: 50%;
right: -5px;
bottom: -5px;
cursor: se-resize;
z-index: 20;
}
#previewArea {
background-image: linear-gradient(#e5e7eb 1px, transparent 1px),
linear-gradient(90deg, #e5e7eb 1px, transparent 1px);
background-size: 20px 20px;
position: relative;
overflow: visible;
}
.property-group {
border-left: 3px solid #3b82f6;
}
.tab-button.active {
border-bottom: 2px solid #3b82f6;
color: #3b82f6;
}
.floating-toolbar {
position: absolute;
top: -40px;
left: 50%;
transform: translateX(-50%);
background: white;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
border-radius: 4px;
display: none;
z-index: 30;
}
.component:hover .floating-toolbar {
display: flex;
}
#previewArea {
transform-origin: 0 0;
}
.draggable-source {
cursor: grab;
}
.draggable-source:active {
cursor: grabbing;
}
.column {
display: flex;
flex-direction: column;
}
.row {
display: flex;
flex-direction: row;
}
.list-view-item {
padding: 8px;
border-bottom: 1px solid #e5e7eb;
}
.slider-track {
width: 100%;
height: 6px;
background: #e5e7eb;
border-radius: 3px;
position: relative;
}
.slider-thumb {