File size: 12,013 Bytes
01236e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
aa4e3c0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Grid Layout Builder</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://unpkg.com/aos@2.3.1/dist/aos.css" rel="stylesheet">
    <script src="https://unpkg.com/aos@2.3.1/dist/aos.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/feather-icons/dist/feather.min.js"></script>
    <script src="https://unpkg.com/feather-icons"></script>
    <script src="https://cdn.jsdelivr.net/npm/interactjs/dist/interact.min.js"></script>
    <style>
        .grid-container {
            touch-action: none;
            position: relative;
        }
        .column {
            position: relative;
            min-width: 50px;
            min-height: 100%;
        }
        .column-resize-handle {
            position: absolute;
            top: 0;
            right: -5px;
            width: 10px;
            height: 100%;
            cursor: col-resize;
            background-color: rgba(59, 130, 246, 0.3);
            z-index: 10;
        }
        .row-resize-handle {
            position: absolute;
            left: 0;
            bottom: -5px;
            width: 100%;
            height: 10px;
            cursor: row-resize;
            background-color: rgba(59, 130, 246, 0.3);
            z-index: 10;
        }
        .row {
            position: relative;
            min-height: 30px;
            background-color: rgba(249, 250, 251, 0.8);
            border: 1px dashed #d1d5db;
        }
        .device-frame {
            box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.1);
            background-color: white;
            overflow: hidden;
        }
        .desktop-frame {
            width: 1200px;
            height: 800px;
            border-radius: 12px;
        }
        .mobile-frame {
            width: 375px;
            height: 667px;
            border-radius: 40px;
            border: 12px solid #1f2937;
        }
    </style>
</head>
<body class="bg-gray-100 min-h-screen">
    <div class="container mx-auto p-6">
        <header class="mb-8 text-center">
            <h1 class="text-3xl font-bold text-gray-800 mb-2">Grid Layout Builder</h1>
            <p class="text-gray-600">Create responsive UI layouts with drag-and-drop columns and rows</p>
        </header>

        <div class="bg-white rounded-xl shadow-lg p-6 mb-6">
            <div class="flex flex-wrap gap-4 justify-center mb-6">
                <button id="desktop-mode" class="px-4 py-2 bg-blue-500 text-white rounded-lg hover:bg-blue-600 transition-colors flex items-center gap-2">
                    <i data-feather="monitor"></i> Desktop Mode
                </button>
                <button id="mobile-mode" class="px-4 py-2 bg-purple-500 text-white rounded-lg hover:bg-purple-600 transition-colors flex items-center gap-2">
                    <i data-feather="smartphone"></i> Mobile Mode
                </button>
                <button id="add-column" class="px-4 py-2 bg-green-500 text-white rounded-lg hover:bg-green-600 transition-colors flex items-center gap-2">
                    <i data-feather="plus"></i> Add Column
                </button>
                <button id="reset-grid" class="px-4 py-2 bg-red-500 text-white rounded-lg hover:bg-red-600 transition-colors flex items-center gap-2">
                    <i data-feather="trash-2"></i> Reset Grid
                </button>
            </div>

            <div id="device-container" class="flex justify-center">
                <div id="desktop-frame" class="device-frame desktop-frame">
                    <div id="grid-container" class="grid-container w-full h-full flex"></div>
                </div>
            </div>
        </div>

        <div class="bg-white rounded-xl shadow-lg p-6">
            <h2 class="text-xl font-semibold mb-4">Instructions</h2>
            <ul class="list-disc list-inside space-y-2 text-gray-700">
                <li>Select Desktop or Mobile mode to change the canvas size</li>
                <li>Click "Add Column" to create new layout columns</li>
                <li>Click the "+" button on any column to add rows</li>
                <li>Drag column edges to resize columns</li>
                <li>Drag row edges to resize rows</li>
                <li>Create your perfect UI layout structure</li>
            </ul>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            feather.replace();
            AOS.init();
            
            const gridContainer = document.getElementById('grid-container');
            const desktopModeBtn = document.getElementById('desktop-mode');
            const mobileModeBtn = document.getElementById('mobile-mode');
            const addColumnBtn = document.getElementById('add-column');
            const resetGridBtn = document.getElementById('reset-grid');
            const deviceContainer = document.getElementById('device-container');
            const desktopFrame = document.getElementById('desktop-frame');
            
            let currentDeviceMode = 'desktop';
            let columnCount = 0;
            
            // Set up device mode switching
            desktopModeBtn.addEventListener('click', () => switchDeviceMode('desktop'));
            mobileModeBtn.addEventListener('click', () => switchDeviceMode('mobile'));
            
            function switchDeviceMode(mode) {
                currentDeviceMode = mode;
                deviceContainer.innerHTML = '';
                
                if (mode === 'desktop') {
                    const frame = desktopFrame.cloneNode(true);
                    deviceContainer.appendChild(frame);
                    gridContainer = frame.querySelector('#grid-container');
                } else {
                    const mobileFrame = document.createElement('div');
                    mobileFrame.className = 'device-frame mobile-frame';
                    mobileFrame.innerHTML = '<div id="grid-container" class="grid-container w-full h-full flex"></div>';
                    deviceContainer.appendChild(mobileFrame);
                    gridContainer = mobileFrame.querySelector('#grid-container');
                }
                
                // Reinitialize interact.js for the new container
                initInteract();
            }
            
            // Add column functionality
            addColumnBtn.addEventListener('click', addColumn);
            
            function addColumn() {
                columnCount++;
                const column = document.createElement('div');
                column.className = 'column bg-gray-50 border border-gray-200 relative';
                column.id = `column-${columnCount}`;
                
                // Add column resize handle
                const resizeHandle = document.createElement('div');
                resizeHandle.className = 'column-resize-handle';
                column.appendChild(resizeHandle);
                
                // Add row container
                const rowContainer = document.createElement('div');
                rowContainer.className = 'row-container h-full';
                column.appendChild(rowContainer);
                
                // Add add row button
                const addRowBtn = document.createElement('button');
                addRowBtn.className = 'absolute top-2 right-2 p-1 bg-blue-500 text-white rounded-full hover:bg-blue-600 transition-colors z-20';
                addRowBtn.innerHTML = '<i data-feather="plus" width="16" height="16"></i>';
                addRowBtn.addEventListener('click', () => addRow(column.id));
                column.appendChild(addRowBtn);
                
                gridContainer.appendChild(column);
                feather.replace();
                
                // Make column resizable
                initColumnResize(column);
            }
            
            function addRow(columnId) {
                const column = document.getElementById(columnId);
                const rowContainer = column.querySelector('.row-container');
                
                const row = document.createElement('div');
                row.className = 'row mb-2 rounded';
                
                // Add row resize handle
                const resizeHandle = document.createElement('div');
                resizeHandle.className = 'row-resize-handle';
                row.appendChild(resizeHandle);
                
                rowContainer.appendChild(row);
                
                // Make row resizable
                initRowResize(row);
            }
            
            // Reset grid functionality
            resetGridBtn.addEventListener('click', () => {
                gridContainer.innerHTML = '';
                columnCount = 0;
            });
            
            // Initialize interact.js for resizing
            function initInteract() {
                document.querySelectorAll('.column').forEach(initColumnResize);
                document.querySelectorAll('.row').forEach(initRowResize);
            }
            
            function initColumnResize(column) {
                interact(column).resizable({
                    edges: { right: true },
                    listeners: {
                        move: function (event) {
                            let target = event.target;
                            let x = (parseFloat(target.getAttribute('data-x')) || 0);
                            
                            // Update the element's style
                            target.style.width = event.rect.width + 'px';
                            
                            // Translate when resizing from top or left edges
                            x += event.deltaRect.left;
                            
                            target.style.transform = 'translate(' + x + 'px)';
                            
                            target.setAttribute('data-x', x);
                        }
                    },
                    modifiers: [
                        interact.modifiers.restrictEdges({
                            outer: 'parent'
                        }),
                        interact.modifiers.restrictSize({
                            min: { width: 50 }
                        })
                    ],
                    inertia: true
                });
            }
            
            function initRowResize(row) {
                interact(row).resizable({
                    edges: { bottom: true },
                    listeners: {
                        move: function (event) {
                            let target = event.target;
                            let y = (parseFloat(target.getAttribute('data-y')) || 0);
                            
                            // Update the element's style
                            target.style.height = event.rect.height + 'px';
                            
                            // Translate when resizing from top or left edges
                            y += event.deltaRect.top;
                            
                            target.style.transform = 'translate(' + y + 'px)';
                            
                            target.setAttribute('data-y', y);
                        }
                    },
                    modifiers: [
                        interact.modifiers.restrictEdges({
                            outer: 'parent'
                        }),
                        interact.modifiers.restrictSize({
                            min: { height: 30 }
                        })
                    ],
                    inertia: true
                });
            }
            
            // Initialize with one column
            addColumn();
        });
    </script>
</body>
</html>