refactor: new structure
This commit is contained in:
+73
-14
@@ -3,14 +3,13 @@ const path = require('path');
|
||||
|
||||
module.exports = {
|
||||
server: {
|
||||
baseDir: "output",
|
||||
routes: {
|
||||
"/assets": "assets"
|
||||
}
|
||||
baseDir: "."
|
||||
},
|
||||
files: [
|
||||
"output/index.html",
|
||||
"output/html/**/*.html",
|
||||
"tasks/index.html",
|
||||
"tasks/*/docs/*.template.html",
|
||||
"tasks/*/docs/*.output.html",
|
||||
"tasks/*/editor.html",
|
||||
"assets/**/*"
|
||||
],
|
||||
port: 3300,
|
||||
@@ -19,21 +18,46 @@ module.exports = {
|
||||
ui: false,
|
||||
middleware: [
|
||||
{
|
||||
route: "/api/save-editor",
|
||||
route: "/api/save-edits",
|
||||
handle: function (req, res, next) {
|
||||
if (req.method !== 'POST') return next();
|
||||
let body = '';
|
||||
req.on('data', chunk => body += chunk);
|
||||
req.on('end', () => {
|
||||
try {
|
||||
const data = JSON.parse(body);
|
||||
const name = path.basename(data.file || 'unknown', '.html');
|
||||
const dir = path.join(__dirname, 'output', 'editor-saves');
|
||||
fs.mkdirSync(dir, { recursive: true });
|
||||
const savePath = path.join(dir, name + '.json');
|
||||
fs.writeFileSync(savePath, JSON.stringify(data, null, 2));
|
||||
const payload = JSON.parse(body);
|
||||
const { taskType, docId, data } = payload;
|
||||
|
||||
if (!taskType || !docId || !data) {
|
||||
res.writeHead(400, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: 'Missing taskType, docId, or data' }));
|
||||
return;
|
||||
}
|
||||
|
||||
const docsDir = path.join(__dirname, 'tasks', taskType, 'docs');
|
||||
const tempDir = path.join(__dirname, 'tasks', taskType, 'temp');
|
||||
fs.mkdirSync(tempDir, { recursive: true });
|
||||
|
||||
const dataPath = path.join(docsDir, docId + '.data.json');
|
||||
const diffPath = path.join(tempDir, docId + '.diff.json');
|
||||
|
||||
// Read old data for diff
|
||||
let oldData = null;
|
||||
try {
|
||||
oldData = JSON.parse(fs.readFileSync(dataPath, 'utf-8'));
|
||||
} catch (e) {
|
||||
// No previous data — first save
|
||||
}
|
||||
|
||||
// Write new data
|
||||
fs.writeFileSync(dataPath, JSON.stringify(data, null, 2));
|
||||
|
||||
// Compute and write diff
|
||||
const diff = computeDiff(oldData, data, docId);
|
||||
fs.writeFileSync(diffPath, JSON.stringify(diff, null, 2));
|
||||
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ ok: true, path: savePath }));
|
||||
res.end(JSON.stringify({ ok: true, path: dataPath }));
|
||||
} catch (e) {
|
||||
res.writeHead(400, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: e.message }));
|
||||
@@ -43,3 +67,38 @@ module.exports = {
|
||||
}
|
||||
]
|
||||
};
|
||||
|
||||
function computeDiff(oldData, newData, docId) {
|
||||
const changes = [];
|
||||
const timestamp = new Date().toISOString();
|
||||
|
||||
if (!oldData) {
|
||||
return { timestamp, docId, firstSave: true, changes: [] };
|
||||
}
|
||||
|
||||
const oldPages = oldData.pages || [];
|
||||
const newPages = newData.pages || [];
|
||||
|
||||
for (let i = 0; i < Math.max(oldPages.length, newPages.length); i++) {
|
||||
const oldPage = oldPages[i] || {};
|
||||
const newPage = newPages[i] || {};
|
||||
const pageNum = (newPage.page || oldPage.page || i + 1);
|
||||
|
||||
// Compare all element arrays in the page
|
||||
for (const key of new Set([...Object.keys(oldPage), ...Object.keys(newPage)])) {
|
||||
if (key === 'page') continue;
|
||||
const oldVal = JSON.stringify(oldPage[key]);
|
||||
const newVal = JSON.stringify(newPage[key]);
|
||||
if (oldVal !== newVal) {
|
||||
changes.push({
|
||||
page: pageNum,
|
||||
field: key,
|
||||
from: oldPage[key],
|
||||
to: newPage[key]
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return { timestamp, docId, changes };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user