Restructure Saving Logic to extra class and save on Strg+S + display changed savestatus in editor
All checks were successful
E2E Testing / test (push) Successful in 1m31s

This commit is contained in:
Sebastian Böckelmann 2024-02-17 07:30:29 +01:00
parent 599a7cdd1b
commit d56166b245
8 changed files with 73 additions and 33 deletions

30
app/SaveProject.js Normal file
View File

@ -0,0 +1,30 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.SaveProject = void 0;
const fs = require("fs");
const path = require("node:path");
class SaveProject {
static saveProject(projectDir, storageModels) {
const directoryPath = 'testModel/';
if (!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath, { recursive: true });
}
storageModels.forEach(storageModel => {
const modelDir = path.join(directoryPath, storageModel.storageDir);
if (!fs.existsSync(modelDir)) {
fs.mkdirSync(modelDir, { recursive: true });
}
const filePath = path.join(directoryPath, storageModel.storageDir, storageModel.fileName + ".json");
fs.writeFile(filePath, storageModel.jsonString, 'utf-8', (err) => {
if (err) {
console.error('Error writing JSON to file:', err);
}
else {
console.log('JSON file saved successfully:', filePath);
}
});
});
}
}
exports.SaveProject = SaveProject;
//# sourceMappingURL=SaveProject.js.map

29
app/SaveProject.ts Normal file
View File

@ -0,0 +1,29 @@
import {StorageModel} from "../src/app/game-model/StorageModel";
import * as fs from "fs";
import * as path from "node:path";
export class SaveProject {
static saveProject(projectDir: string, storageModels: StorageModel[]) {
const directoryPath = 'testModel/'
if(!fs.existsSync(directoryPath)) {
fs.mkdirSync(directoryPath, {recursive: true});
}
storageModels.forEach(storageModel => {
const modelDir = path.join(directoryPath, storageModel.storageDir);
if(!fs.existsSync(modelDir)) {
fs.mkdirSync(modelDir, {recursive: true});
}
const filePath = path.join(directoryPath, storageModel.storageDir, storageModel.fileName + ".json");
fs.writeFile(filePath, storageModel.jsonString ,'utf-8', (err) => {
if (err) {
console.error('Error writing JSON to file:', err);
} else {
console.log('JSON file saved successfully:', filePath);
}
})
})
}
}

View File

@ -1,8 +1,9 @@
import {app, BrowserWindow, screen, Menu, ipcMain, ipcRenderer} from 'electron'; import {app, BrowserWindow, screen, Menu, ipcMain, ipcRenderer, globalShortcut} from 'electron';
import * as path from 'path'; import * as path from 'path';
import * as fs from 'fs'; import * as fs from 'fs';
import {json} from "node:stream/consumers"; import {json} from "node:stream/consumers";
import {StorageModel} from "../src/app/game-model/StorageModel"; import {StorageModel} from "../src/app/game-model/StorageModel";
import {SaveProject} from "./SaveProject";
let win: BrowserWindow | null = null; let win: BrowserWindow | null = null;
const args = process.argv.slice(1), const args = process.argv.slice(1),
@ -94,26 +95,8 @@ function createWindow(): BrowserWindow {
}) })
ipcMain.on('save-model', (event, storageModels: StorageModel[]) => { ipcMain.on('save-model', (event, storageModels: StorageModel[]) => {
const directoryPath = 'testModel/' console.log("Save Model")
if(!fs.existsSync(directoryPath)) { SaveProject.saveProject(projectDirectory, storageModels);
fs.mkdirSync(directoryPath, {recursive: true});
}
storageModels.forEach(storageModel => {
const modelDir = path.join(directoryPath, storageModel.storageDir);
if(!fs.existsSync(modelDir)) {
fs.mkdirSync(modelDir, {recursive: true});
}
const filePath = path.join(directoryPath, storageModel.storageDir, storageModel.fileName + ".json");
fs.writeFile(filePath, storageModel.jsonString ,'utf-8', (err) => {
if (err) {
console.error('Error writing JSON to file:', err);
} else {
console.log('JSON file saved successfully:', filePath);
}
})
})
}) })
const menuTemplate = [ const menuTemplate = [
@ -171,7 +154,11 @@ try {
} }
}); });
app.whenReady().then(() => {
globalShortcut.register('CommandOrControl+S', () => {
saveProject();
})
})
} catch (e) { } catch (e) {
// Catch Error // Catch Error
@ -188,6 +175,5 @@ function openProject() {
} }
function saveProject() { function saveProject() {
console.log("Clicked SaveProject")
win!.webContents.send('get-project-data') win!.webContents.send('get-project-data')
} }

View File

@ -10,7 +10,7 @@
<app-model-component-editor [modelComponent]="modelComponent" (onModelNameUpdated)="onModelNameUpdate()"></app-model-component-editor> <app-model-component-editor [modelComponent]="modelComponent" (onModelNameUpdated)="onModelNameUpdate()"></app-model-component-editor>
<app-script-account-editor *ngIf="modelComponent.type === ModelComponentType.SCRIPTACCOUNT" <app-script-account-editor *ngIf="modelComponent.type === ModelComponentType.SCRIPTACCOUNT"
[scriptAccount]="convertModelComponentToScriptAccount(modelComponent)"></app-script-account-editor> [scriptAccount]="convertModelComponentToScriptAccount(modelComponent)"></app-script-account-editor>
<app-gamesystem-editor *ngIf="modelComponent.type == ModelComponentType.GAMESYTEM" <app-gamesystem-editor *ngIf="modelComponent.type === ModelComponentType.GAMESYTEM"
[gamesystem]="convertModelComponentToGamesystem(modelComponent)" [gamesystem]="convertModelComponentToGamesystem(modelComponent)"
(onOpenGamesystemEditor)="openGameModelComponent($event)"></app-gamesystem-editor> (onOpenGamesystemEditor)="openGameModelComponent($event)"></app-gamesystem-editor>

View File

@ -100,6 +100,8 @@ export class GameModel {
jsonString: scriptAccountJson, jsonString: scriptAccountJson,
storageDir: "script-accounts" storageDir: "script-accounts"
}) })
console.log(scriptAccount)
}) })
return storageModels; return storageModels;

View File

@ -10,6 +10,7 @@ export class ScriptAccount extends ModelComponent{
} }
save(): string { save(): string {
this.unsaved = false;
return JSON.stringify(this, null, SaveComponent.JSON_INDENT) return JSON.stringify(this, null, SaveComponent.JSON_INDENT)
} }
} }

View File

@ -1,7 +1,7 @@
{ {
"unsaved": false, "unsaved": false,
"componentName": "Temperature", "componentName": "Temperature",
"componentDescription": "", "componentDescription": "ddada",
"type": 0, "type": 0,
"minValue": 0, "minValue": 0,
"maxValue": 100 "maxValue": 100

View File

@ -1,8 +0,0 @@
{
"unsaved": false,
"componentName": "Temperature",
"componentDescription": "",
"type": 0,
"minValue": 0,
"maxValue": 100
}