File size: 8,860 Bytes
ce2d6ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93a8ee2
ce2d6ca
 
 
 
 
d081604
 
 
 
 
 
 
 
 
 
 
ce2d6ca
d081604
 
 
 
 
 
 
 
 
 
 
 
ce2d6ca
 
 
 
 
 
d081604
ce2d6ca
 
 
d081604
ce2d6ca
 
 
 
4b3d13d
ce2d6ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93a8ee2
ce2d6ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
93a8ee2
ce2d6ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4b3d13d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ce2d6ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import { BlockDefinition, ProjectFile, VisualElement } from '../types/blocks';

interface CompileOptions {
  blocks: BlockDefinition[];
  fileTree: ProjectFile[];
  visualElements: VisualElement[];
  activeFile?: ProjectFile;
  projectName: string;
}

export function compileWeb(options: CompileOptions): string {
  const { fileTree, activeFile, blocks } = options;

  if (!activeFile) {
    // Generate default index.html with all visual elements
    return generateDefaultHTML(options);
  }

  switch (activeFile.type) {
    case 'html':
      return activeFile.content || generateDefaultHTML(options);
    case 'css':
      return activeFile.content || '/* Styles */\n';
    case 'js':
      return activeFile.content || '// JavaScript\n';
    default:
      return activeFile.content || '';
  }
}

function findFileInTree(files: ProjectFile[], id: string): ProjectFile | null {
  for (const f of files) {
    if (f.id === id) return f;
    if (f.children) {
      const found = findFileInTree(f.children, id);
      if (found) return found;
    }
  }
  return null;
}

function generateDefaultHTML(options: CompileOptions): string {
  const { visualElements, projectName, fileTree, activeFile } = options;

  // Find linked CSS and JS files
  const linkedIds = activeFile?.linkedFiles || [];
  const linkedFiles = linkedIds.map(id => findFileInTree(fileTree, id)).filter(Boolean) as ProjectFile[];
  const linkedCSS = linkedFiles.filter(f => f.type === 'css');
  const linkedJS = linkedFiles.filter(f => f.type === 'js');

  // Build link and script tags
  const cssLinks = linkedCSS.map(f => `  <link rel="stylesheet" href="${f.name}" />`).join('\n');
  const jsScripts = linkedJS.map(f => `  <script src="${f.name}"></script>`).join('\n');

  return `<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>${projectName || 'My Web App'}</title>
${cssLinks}
</head>
<body>
${visualElements.map(el => renderVisualElement(el, 0)).join('\n')}
${jsScripts}
</body>
</html>`;
}

export function renderVisualElement(el: VisualElement, depth: number): string {
  const indent = '  '.repeat(depth + 1);
  const props = Object.entries(el.props)
    .filter(([k, v]) => v !== undefined && v !== null && v !== '' && k !== 'textContent' && k !== 'children')
    .map(([k, v]) => {
      if (typeof v === 'boolean') return v ? k : '';
      if (k === 'style') return '';
      return `${k}="${String(v).replace(/"/g, '&quot;')}"`;
    })
    .filter(Boolean)
    .join(' ');

  const styles = Object.entries(el.styles)
    .map(([k, v]) => `${k.replace(/[A-Z]/g, m => '-' + m.toLowerCase())}: ${v}`)
    .join('; ');

  const styleAttr = styles ? ` style="${styles}"` : '';
  const classAttr = el.classes.length ? ` class="${el.classes.join(' ')}"` : '';
  const propsStr = props ? ' ' + props : '';
  const attrs = propsStr + styleAttr + classAttr;

  const content = el.props.textContent || '';

  if (el.children && el.children.length > 0) {
    return `${indent}<${el.tagName}${attrs}>
${el.children.map(c => renderVisualElement(c, depth + 1)).join('\n')}
${indent}</${el.tagName}>`;
  }

  if (el.tagName === 'img' || el.tagName === 'input' || el.tagName === 'br') {
    return `${indent}<${el.tagName}${attrs} />`;
  }

  return `${indent}<${el.tagName}${attrs}>${content || ''}</${el.tagName}>`;
}

export function compileElectron(options: CompileOptions): string {
  const { activeFile } = options;
  if (!activeFile) return '// Electron main process\n';

  switch (activeFile.type) {
    case 'typescript':
      return activeFile.content || generateDefaultElectronMain(options);
    case 'html':
      return activeFile.content || generateDefaultHTML(options);
    case 'css':
      return activeFile.content || '/* Styles */\n';
    default:
      return activeFile.content || '';
  }
}

function generateDefaultElectronMain(options: CompileOptions): string {
  return `import { app, BrowserWindow, ipcMain } from 'electron';
import path from 'path';

let mainWindow: BrowserWindow | null = null;

function createWindow(): void {
  mainWindow = new BrowserWindow({
    width: 1200,
    height: 800,
    webPreferences: {
      preload: path.join(__dirname, 'preload.js'),
      nodeIntegration: false,
      contextIsolation: true,
    },
  });

  mainWindow.loadFile('index.html');
  mainWindow.on('closed', () => { mainWindow = null; });
}

app.whenReady().then(() => {
  createWindow();

  app.on('activate', () => {
    if (BrowserWindow.getAllWindows().length === 0) {
      createWindow();
    }
  });
});

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit();
  }
});
`;
}

export function compileMaui(options: CompileOptions): string {
  const { activeFile } = options;
  if (!activeFile) return '<!-- .NET MAUI Page -->\n';

  switch (activeFile.type) {
    case 'xaml':
      return activeFile.content || generateDefaultXaml(options);
    case 'csharp':
      return activeFile.content || generateDefaultCSharp(options);
    default:
      return activeFile.content || '';
  }
}

function generateDefaultXaml(options: CompileOptions): string {
  return `<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             x:Class="${options.projectName}.MainPage"
             Title="Main Page"
             BackgroundColor="White">
  <VerticalStackLayout Spacing="16" Padding="24">
    <Label Text="Welcome to ${options.projectName}!"
           FontSize="24"
           FontAttributes="Bold"
           HorizontalTextAlignment="Center" />

    <Entry x:Name="inputField"
           Placeholder="Enter text here..." />

    <Button x:Name="submitButton"
            Text="Submit"
            BackgroundColor="Blue"
            TextColor="White"
            Clicked="OnSubmitClicked" />
  </VerticalStackLayout>
</ContentPage>`;
}

function generateDefaultCSharp(options: CompileOptions): string {
  return `namespace ${options.projectName};

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();
    }

    private void OnSubmitClicked(object sender, EventArgs e)
    {
        DisplayAlert("Hello", $"You entered: {inputField.Text}", "OK");
    }
}`;
}

export function compileNodeJS(options: CompileOptions): string {
  const { activeFile } = options;
  if (!activeFile) return '// Node.js server\n';

  switch (activeFile.type) {
    case 'js':
      return activeFile.content || generateDefaultExpress(options);
    case 'json':
      return activeFile.content || generateDefaultPackageJson(options);
    case 'env':
      return activeFile.content || '# Environment Variables\nPORT=3000\n';
    default:
      return activeFile.content || '';
  }
}

function generateDefaultExpress(options: CompileOptions): string {
  return `const express = require('express');
const cors = require('cors');

const app = express();
const PORT = process.env.PORT || 3000;

// Middleware
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// Routes
app.get('/', (req, res) => {
  res.json({
    message: 'Welcome to ${options.projectName}!',
    version: '1.0.0',
  });
});

// Start server
app.listen(PORT, () => {
  console.log(\`Server running on port \${PORT}\`);
});
`;
}

/**
 * Generate a complete HTML document for preview purposes.
 * Renders visual elements and inlines linked CSS content.
 * Does NOT include script tags — the caller injects those.
 */
export function generatePreviewHtml(options: {
  visualElements: VisualElement[];
  projectName: string;
  linkedCssContent: { content: string }[];
}): string {
  const { visualElements, projectName, linkedCssContent } = options;

  const cssBlocks = linkedCssContent
    .filter(f => f.content)
    .map(f => f.content)
    .join('\n\n');

  const styleTag = cssBlocks
    ? `  <style>\n${cssBlocks}\n  </style>`
    : '';

  return `<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>${projectName || 'My Web App'}</title>
${styleTag}
</head>
<body>
${visualElements.map(el => renderVisualElement(el, 0)).join('\n')}
</body>
</html>`;
}

function generateDefaultPackageJson(options: CompileOptions): string {
  return JSON.stringify({
    name: options.projectName.toLowerCase().replace(/\s+/g, '-'),
    version: '1.0.0',
    description: `${options.projectName} - built with RealBlocks`,
    main: 'server.js',
    scripts: {
      start: 'node server.js',
      dev: 'nodemon server.js',
    },
    dependencies: {
      express: '^4.18.2',
      cors: '^2.8.5',
    },
    devDependencies: {
      nodemon: '^3.0.0',
    },
  }, null, 2);
}