完成 load view 功能

This commit is contained in:
锦恢 2024-10-21 23:45:54 +08:00
parent 779a727505
commit f5f498533b
5 changed files with 1868 additions and 1815 deletions

3598
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -27,4 +27,4 @@
"express": "^4.21.1",
"morgan": "^1.10.0"
}
}
}

View File

@ -3,7 +3,7 @@ import express, { Request, Response } from 'express';
import morgan from 'morgan';
import cors from 'cors';
import { saveView, saveViewAs } from './save-view';
import { loadView, saveView, saveViewAs } from './save-view';
import { createWindow } from './windows';
const corsOptions = {
@ -23,6 +23,7 @@ backendApp.get('/', (req: Request, res: Response) => {
backendApp.post('/save-view', saveView);
backendApp.post('/save-view-as', saveViewAs);
backendApp.post('/load-view', loadView);
const PORT = process.env.PORT || 3000;
backendApp.listen(PORT, () => {

View File

@ -3,7 +3,7 @@ import * as path from 'path';
import * as fs from 'fs';
import { BSON } from 'bson';
import { rootPath } from './global';
import { showSaveViewDialog } from './windows';
import { showOpenViewDialog, showSaveViewDialog } from './windows';
const payloadCache = new Map<string, any>();
@ -34,6 +34,15 @@ export async function saveView(req: Request, res: Response) {
}
}
function getFilename(file: string) {
const base = path.basename(file);
const spls = base.split('.');
if (spls.length === 1) {
return base;
}
return spls[0];
}
export async function saveViewAs(req: Request, res: Response) {
try {
// 先保存原来的文件 payload 一定是 all
@ -43,7 +52,16 @@ export async function saveViewAs(req: Request, res: Response) {
const originPayload = mergePayloadCache(originVcdViewFile, payload);
// 询问新的路径
const savePath = await showSaveViewDialog();
const defaultFilename = getFilename(payload.originVcdFile) + '.vcd.view';
const savePath = await showSaveViewDialog({
title: 'Save As View File',
defaultPath: path.join(path.resolve(rootPath), defaultFilename),
buttonLabel: 'Save',
filters: [
{ name: 'Vcd View File', extensions: ['vcd.view'] },
{ name: 'All Files', extensions: ['*'] },
],
});
if (savePath) {
const buffer = BSON.serialize(originPayload);
@ -64,4 +82,39 @@ export async function saveViewAs(req: Request, res: Response) {
console.log('error happen in /save-view-as, ' + error);
res.send('error');
}
}
export async function loadView(req: Request, res: Response) {
try {
const { originVcdFile } = req.body;
const vcdPath = path.isAbsolute(originVcdFile) ? originVcdFile : path.join(path.resolve(rootPath), originVcdFile).replace(/\\/g, '/');
const defaultFolder = path.dirname(vcdPath);
const viewPath = await showOpenViewDialog({
title: 'Load View File',
defaultPath: defaultFolder,
buttonLabel: 'Load',
filters: [
{ name: 'Vcd View File', extensions: ['vcd.view'] },
{ name: 'All Files', extensions: ['*'] },
],
properties: ['openFile'],
});
if (viewPath) {
const buffer = fs.readFileSync(viewPath);
const recoverJson = BSON.deserialize(buffer);
res.send({
recoverJson
});
} else {
res.send({
recoverJson: undefined
});
}
} catch (error) {
console.log('error happen in /load-view, ' + error);
res.send('error');
}
}

View File

@ -1,4 +1,3 @@
import { BrowserWindow, dialog } from 'electron';
import * as path from 'path';
@ -17,20 +16,20 @@ export function createWindow() {
}
// 在主进程中定义一个方法来显示保存对话框
export async function showSaveViewDialog(): Promise<string | undefined> {
const result = await dialog.showSaveDialog({
title: 'Save File',
defaultPath: path.join(__dirname, 'sample.txt'),
buttonLabel: 'Save',
filters: [
{ name: 'Text Files', extensions: ['txt'] },
{ name: 'All Files', extensions: ['*'] },
],
});
export async function showSaveViewDialog(option: Electron.SaveDialogOptions): Promise<string | undefined> {
const result = await dialog.showSaveDialog(option);
if (!result.canceled && result.filePath) {
return result.filePath;
} else {
return undefined;
}
}
export async function showOpenViewDialog(option: Electron.OpenDialogOptions): Promise<string | undefined> {
const result = await dialog.showOpenDialog(option);
if (!result.canceled && result.filePaths.length > 0) {
return result.filePaths[0];
} else {
return undefined;
}
}