-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HtmlGridLayout-Generator.html
259 lines (219 loc) · 10.2 KB
/
HtmlGridLayout-Generator.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Grid Builder</title>
<style>
body {
font-family: 'Arial', sans-serif;
margin: 0;
padding: 0;
height: 100vh;
background-color: #f0f0f0;
}
#tableContainer {
position: relative;
display: grid;
grid-template-columns: repeat(3, calc(33.33vw - 20px));
grid-template-rows: repeat(6, calc(16.67vh - 20px));
gap: 10px;
border: 1px solid black;
overflow: hidden;
}
.grid-item {
background-color: #b3d4fc;
padding: 10px;
border-radius: 4px;
text-align: center;
font-weight: bold;
font-size: 16px;
cursor: move;
border: 2px solid #ccc;
}
#tableOutline {
border: 1px solid #ccc;
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
pointer-events: none;
z-index: -1;
}
.action-button {
position: fixed;
padding: 10px;
background-color: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
margin-right: 10px;
}
#addButton { top: 10px; left: 10px; }
#editButton { top: 10px; left: 140px; }
#addRowButton { top: 10px; left: 270px; }
#addColButton { top: 10px; left: 400px; }
#exportButton { top: 10px; left: 530px; }
#copyAllButton { top: 10px; left: 660px; }
</style>
</head>
<body onmousedown="addGridItem(event)">
<div id="tableContainer" onmousemove="updateTableOutline()">
<div id="tableOutline"></div>
</div>
<button class="action-button" id="addButton" onclick="addElement()">Add Element</button>
<button class="action-button" id="editButton" onclick="editElementCode()">Edit Element Code</button>
<button class="action-button" id="addRowButton" onclick="addRow()">Add Row</button>
<button class="action-button" id="addColButton" onclick="addColumn()">Add Column</button>
<button class="action-button" id="exportButton" onclick="exportGeneratedCode()">Export Generated Code</button>
<button class="action-button" id="copyAllButton" onclick="copyGeneratedCode()">Copy All</button>
<script>
document.addEventListener('DOMContentLoaded', function () {
let currentItemId = 1;
function addGridItem(event) {
const newItem = document.createElement('div');
newItem.classList.add('grid-item');
newItem.textContent = `Item ${currentItemId}`;
newItem.id = `item${currentItemId}`;
newItem.draggable = true;
newItem.style.position = 'absolute';
newItem.style.left = `${event.clientX}px`;
newItem.style.top = `${event.clientY}px`;
currentItemId++;
newItem.onclick = function () {
customizeItem(newItem);
};
newItem.ondragstart = function (e) {
e.dataTransfer.setData('text/plain', '');
const offsetX = e.clientX - newItem.getBoundingClientRect().left;
const offsetY = e.clientY - newItem.getBoundingClientRect().top;
function handleDragMove(e) {
newItem.style.left = `${e.clientX - offsetX}px`;
newItem.style.top = `${e.clientY - offsetY}px`;
}
function handleDragEnd() {
document.removeEventListener('mousemove', handleDragMove);
document.removeEventListener('mouseup', handleDragEnd);
updateTableOutline();
}
document.addEventListener('mousemove', handleDragMove);
document.addEventListener('mouseup', handleDragEnd);
};
document.getElementById('tableContainer').appendChild(newItem);
updateTableOutline();
}
function customizeItem(item) {
const content = prompt('Enter content for this grid item:');
if (content !== null) {
item.textContent = content;
}
}
function updateTableOutline() {
const gridItems = document.querySelectorAll('.grid-item');
const tableOutline = document.getElementById('tableOutline');
tableOutline.innerHTML = '';
gridItems.forEach(item => {
const rect = item.getBoundingClientRect();
const outlineItem = document.createElement('div');
outlineItem.style.position = 'absolute';
outlineItem.style.left = `${rect.left}px`;
outlineItem.style.top = `${rect.top}px`;
outlineItem.style.width = `${rect.width}px`;
outlineItem.style.height = `${rect.height}px`;
outlineItem.style.border = '1px solid #ccc';
tableOutline.appendChild(outlineItem);
});
}
window.addElement = function () {
const newItem = document.createElement('div');
newItem.classList.add('grid-item');
newItem.textContent = `New Element`;
newItem.id = `item${currentItemId}`;
newItem.draggable = true;
newItem.style.position = 'absolute';
newItem.style.left = '50px';
newItem.style.top = '50px';
currentItemId++;
newItem.onclick = function () {
customizeItem(newItem);
};
newItem.ondragstart = function (e) {
e.dataTransfer.setData('text/plain', '');
const offsetX = e.clientX - newItem.getBoundingClientRect().left;
const offsetY = e.clientY - newItem.getBoundingClientRect().top;
function handleDragMove(e) {
newItem.style.left = `${e.clientX - offsetX}px`;
newItem.style.top = `${e.clientY - offsetY}px`;
}
function handleDragEnd() {
document.removeEventListener('mousemove', handleDragMove);
document.removeEventListener('mouseup', handleDragEnd);
updateTableOutline();
}
document.addEventListener('mousemove', handleDragMove);
document.addEventListener('mouseup', handleDragEnd);
};
document.getElementById('tableContainer').appendChild(newItem);
updateTableOutline();
};
window.editElementCode = function () {
const itemId = prompt('Enter the ID of the element you want to edit:');
const element = document.getElementById(itemId);
if (element) {
const newContent = prompt('Enter new content for this element:');
if (newContent !== null) {
element.textContent = newContent;
updateTableOutline();
}
} else {
alert('Element not found. Please enter a valid ID.');
}
};
window.addRow = function () {
const tableContainer = document.getElementById('tableContainer');
const numRows = tableContainer.style.gridTemplateRows.split(' ').length;
tableContainer.style.gridTemplateRows = `repeat(${numRows + 1}, calc(16.67vh - 20px))`;
updateTableOutline();
};
window.addColumn = function () {
const tableContainer = document.getElementById('tableContainer');
const numCols = tableContainer.style.gridTemplateColumns.split(' ').length;
tableContainer.style.gridTemplateColumns = `repeat(${numCols + 1}, calc(33.33vw - 20px))`;
updateTableOutline();
};
window.exportGeneratedCode = function () {
const tableItems = document.querySelectorAll('.grid-item');
let code = '<div id="tableContainer" style="display: grid;';
code += `grid-template-columns: ${getTemplateColumns()};`;
code += `grid-template-rows: ${getTemplateRows()}; gap: 10px; border: 1px solid black; overflow: hidden;">\n`;
tableItems.forEach(item => {
const rect = item.getBoundingClientRect();
code += ` <div id="${item.id}" class="grid-item" style="position: absolute; left: ${rect.left}px; top: ${rect.top}px;">${item.textContent}</div>\n`;
});
code += '</div>';
prompt('Copy the generated code:', code);
};
window.copyGeneratedCode = function () {
const generatedCode = document.querySelector('#tableContainer').outerHTML;
const tempTextArea = document.createElement('textarea');
tempTextArea.value = generatedCode;
document.body.appendChild(tempTextArea);
tempTextArea.select();
document.execCommand('copy');
document.body.removeChild(tempTextArea);
alert('Code copied to clipboard!');
};
function getTemplateColumns() {
const tableContainer = document.getElementById('tableContainer');
return tableContainer.style.gridTemplateColumns;
}
function getTemplateRows() {
const tableContainer = document.getElementById('tableContainer');
return tableContainer.style.gridTemplateRows;
}
});
</script>
</body>
</html>