Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(save/saveas): adds save menu item and updates save as to remember path #858

Merged
merged 5 commits into from
May 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 65 additions & 31 deletions src/background/menu-bar.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from "fs";
import path from "path";
import { dialog, shell, app, ipcMain, Menu } from "electron";
import { windows } from "./windows";
import { openFile } from "./open-file";
Expand All @@ -7,6 +8,54 @@ import { projectNames, setCurrentProject, currentProject } from "./projects";

const isMac = process.platform === "darwin";

let lastFileSavedPath = null;

async function save(filePath) {
let result;
if (!filePath) {
result = await dialog.showSaveDialog(windows["mainWindow"], {
defaultPath: lastFileSavedPath || "preset.json",
filters: [{ name: "Presets", extensions: ["json"] }]
});

if (result.canceled) {
return;
}
}

try {
await writePresetToFile(filePath ?? result.filePath);
lastFileSavedPath = path.resolve(filePath ?? result.filePath);
updateMenu();
} catch (e) {
console.error(e);
}
}

async function writePresetToFile(filePath) {
ipcMain.once("preset-data", async (_, presetData) => {
try {
await fs.promises.writeFile(filePath, presetData);
} catch (e) {
dialog.showMessageBox(windows["mainWindow"], {
type: "error",
message: "Could not save preset to file",
detail: e.toString()
});
}
});

try {
windows["mainWindow"].webContents.send("generate-preset");
} catch (e) {
dialog.showMessageBox(windows["mainWindow"], {
type: "error",
message: "Could not generate preset",
detail: e.toString()
});
}
}

export function generateMenuTemplate() {
const mediaManager = getMediaManager();

Expand Down Expand Up @@ -70,38 +119,16 @@ export function generateMenuTemplate() {
{ type: "separator" },
{
label: "Save Preset",
accelerator: "CmdOrCtrl+S",
async click() {
save(lastFileSavedPath);
}
},
{
label: "Save Preset As…",
accelerator: "CmdOrCtrl+Shift+S",
async click() {
const result = await dialog.showSaveDialog(windows["mainWindow"], {
defaultPath: "preset.json",
filters: [{ name: "Presets", extensions: ["json"] }]
});

if (result.canceled) {
return;
}

ipcMain.once("preset-data", async (event, presetData) => {
try {
await fs.promises.writeFile(result.filePath, presetData);
} catch (e) {
dialog.showMessageBox(windows["mainWindow"], {
type: "error",
message: "Could not save preset to file",
detail: e.toString()
});
}
});

try {
windows["mainWindow"].webContents.send("generate-preset");
} catch (e) {
dialog.showMessageBox(windows["mainWindow"], {
type: "error",
message: "Could not generate preset",
detail: e.toString()
});
}
save();
}
},

Expand Down Expand Up @@ -239,7 +266,14 @@ export function generateMenuTemplate() {
];
}

export function updateMenu() {
export function updateMenu(setWindowListener) {
if (setWindowListener) {
windows["mainWindow"].on("ready-to-show", () => {
lastFileSavedPath = null;
updateMenu();
});
}

const menu = Menu.buildFromTemplate(generateMenuTemplate());
Menu.setApplicationMenu(menu);
}
4 changes: 2 additions & 2 deletions src/background/windows.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import { windowPrefs } from "./window-prefs";
const windows = {};

function createWindow({ windowName, options = {} }, event) {
updateMenu();

if (windowPrefs[windowName].unique && windows[windowName]) {
windows[windowName].focus();
windows[windowName].show();
Expand Down Expand Up @@ -36,6 +34,8 @@ function createWindow({ windowName, options = {} }, event) {
...options
});

updateMenu(true);

if (typeof windowPrefs[windowName].create === "function") {
windowPrefs[windowName].create(windows[windowName]);
}
Expand Down
10 changes: 10 additions & 0 deletions vue.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ module.exports = {
config
.plugin("define")
.use(DefinePlugin, [{ "process.env.FLUENTFFMPEG_COV": false }]);

config.module
.rule("babel")
.test(/\.m?js$/)
.exclude.add(/node_modules/)
.end()
.use("babelloader")
.loader("babel-loader", {
presets: [["@babel/preset-env", { targets: "defaults" }]]
});
},

chainWebpackRendererProcess: config => {
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4657,6 +4657,11 @@ csstype@^2.6.8:
resolved "https://registry.yarnpkg.com/csstype/-/csstype-2.6.21.tgz#2efb85b7cc55c80017c66a5ad7cbd931fda3a90e"
integrity sha512-Z1PhmomIfypOpoMjRQB70jfvy/wxT50qW08YXO5lMIJkrdq4yOTR+AW7FqutScmB9NkLwxo+jU+kZLbofZZq/w==

csstype@^3.1.0:
version "3.1.2"
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.1.2.tgz#1d4bf9d572f11c14031f0436e1c10bc1f571f50b"
integrity sha512-I7K1Uu0MBPzaFKg4nI5Q7Vs2t+3gWWW648spaF+Rg7pI9ds18Ugn+lvg4SHczUdKlHI5LWBXyqfS8+DufyBsgQ==

current-script-polyfill@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/current-script-polyfill/-/current-script-polyfill-1.0.0.tgz#f31cf7e4f3e218b0726e738ca92a02d3488ef615"
Expand Down