#!/usr/bin/env node
/**
* mozdIT Content Editor Server v2
* Szerkesztő felület a JSON tartalom fájlokhoz
* Támogatja: szöveg szerkesztés, tömbelem hozzáadás/törlés
* Futtatás: node content-editor.js
* Megnyitás: http://localhost:4001
*/
const http = require('http');
const fs = require('fs');
const path = require('path');
const PORT = 4001;
const CONTENT_DIR = path.join(__dirname, 'proto', 'src', 'content');
const FILES = {
common: path.join(CONTENT_DIR, 'common.json'),
home: path.join(CONTENT_DIR, 'pages', 'home.json'),
about: path.join(CONTENT_DIR, 'pages', 'about.json'),
services: path.join(CONTENT_DIR, 'pages', 'services.json'),
contact: path.join(CONTENT_DIR, 'pages', 'contact.json'),
};
const FILE_LABELS = {
common: '⚙️ Közös szövegek',
home: '🏠 Kezdőlap',
about: '👥 Rólunk',
services: '🛠️ Szolgáltatások',
contact: '📬 Kapcsolat',
};
const HTML = (activeFile, jsonData, message) => `
mozdIT — Tartalom Szerkesztő
${message ? `${message.text}
` : ''}
mozdIT Tartalom Szerkesztő
— JSON fájlok szerkesztése vizuálisan
📝 Szerkeszd a mezőket. Tömbökből elemet törölhetsz (❌) vagy hozzáadhatsz (➕). Mentés gomb menti a fájlt.
`;
// ── Server ───────────────────────────────────────────────────────────────────
const server = http.createServer(async (req, res) => {
const u = new URL(req.url, `http://localhost:${PORT}`);
const fileKey = u.searchParams.get('file') || 'home';
const activeFile = FILES[fileKey] ? fileKey : 'home';
// POST /save — JSON body
if (req.method === 'POST' && u.pathname === '/save') {
let body = '';
req.on('data', c => body += c);
req.on('end', () => {
try {
const data = JSON.parse(body);
fs.writeFileSync(FILES[activeFile], JSON.stringify(data, null, 2) + '\n', 'utf8');
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ ok: true }));
} catch (e) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ ok: false, error: e.message }));
}
});
return;
}
// GET / — editor UI
let message = null;
let jsonData = '{}';
try {
jsonData = fs.readFileSync(FILES[activeFile], 'utf8').trim();
} catch (e) {
message = { type: 'err', text: 'Fájl olvasási hiba: ' + e.message };
}
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
res.end(HTML(activeFile, jsonData, message));
});
server.listen(PORT, () => {
console.log(`\n✅ mozdIT Tartalom Szerkesztő fut: http://localhost:${PORT}\n`);
console.log(' Szerkeszthető fájlok:');
Object.entries(FILE_LABELS).forEach(([k, l]) => {
const rel = k === 'common' ? 'common.json' : `pages/${k}.json`;
console.log(` • ${l}: proto/src/content/${rel}`);
});
console.log('\n Ctrl+C a leállításhoz\n');
});