Added open recent to menu bar

fixed problem with function loadFile
This commit is contained in:
Matthew Welch 2020-11-22 12:09:08 -08:00
parent 606b358d5d
commit 4ee75511ad
3 changed files with 29 additions and 6 deletions

View File

@ -40,6 +40,10 @@ let menu_template = [
accelerator: "Ctrl+O", accelerator: "Ctrl+O",
click: openFile click: openFile
}, },
{
label: "Open Recent",
submenu:[]
},
{ {
label: "Exit", label: "Exit",
click: function () { click: function () {
@ -193,7 +197,7 @@ ipcMain.on("save", (event, data, new_file) => {
}) })
ipcMain.on("loadFile", (event, file_name) => { ipcMain.on("loadFile", (event, file_name) => {
loadFile(); loadFile(file_name);
}) })
ipcMain.on("newFile", (event) => { ipcMain.on("newFile", (event) => {
@ -230,6 +234,21 @@ const createWindow = () => {
} }
}); });
let recent_files = store.get("recent_files");
for (let menu of menu_template[0].submenu) {
if (menu.label == "Open Recent") {
for (let file of recent_files) {
if (fs.existsSync(file)) {
menu.submenu.push({
label: file, click: () => {
mainWindow.webContents.send("open", file);
}
})
}
}
}
}
let menu = Menu.buildFromTemplate(menu_template); let menu = Menu.buildFromTemplate(menu_template);
Menu.setApplicationMenu(menu); Menu.setApplicationMenu(menu);

View File

@ -618,7 +618,6 @@ async function updateRecentFiles(new_file) {
let recent_files = await ipcRenderer.invoke("getStoreValue", "recent_files"); let recent_files = await ipcRenderer.invoke("getStoreValue", "recent_files");
if (recent_files.includes(new_file)) { if (recent_files.includes(new_file)) {
recent_files.splice(recent_files.indexOf(new_file), 1); recent_files.splice(recent_files.indexOf(new_file), 1);
// delete recent_files[recent_files.indexOf(new_file)];
} }
recent_files.splice(0, 0, new_file); recent_files.splice(0, 0, new_file);
ipcRenderer.invoke("setStoreValue", "recent_files", recent_files); ipcRenderer.invoke("setStoreValue", "recent_files", recent_files);

View File

@ -14,7 +14,7 @@
<script>if (window.module) module = window.module;</script> <script>if (window.module) module = window.module;</script>
</head> </head>
<body> <body>
<div id="recent-files" class="w3-light-gray w3-small" style="width: 300px; height: 500px; float: left; border-right: 1px solid gray; overflow-y: scroll"> <div id="recent-files" class="w3-light-gray w3-small" style="width: 300px; height: 500px; float: left; border-right: 1px solid gray; overflow-y: auto">
</div> </div>
<div class="w3-center" style="width: 500px; height: 500px; float: right; padding-top: 20px"> <div class="w3-center" style="width: 500px; height: 500px; float: right; padding-top: 20px">
<h1 class="">Web Editor</h1> <h1 class="">Web Editor</h1>
@ -24,28 +24,33 @@
</body> </body>
<script> <script>
const {ipcRenderer} = require("electron"); const {ipcRenderer} = require("electron");
const fs = require("fs");
let recent_files = null; let recent_files = null;
async function getRecentFiles() { async function getRecentFiles() {
recent_files = await ipcRenderer.invoke("getStoreValue", "recent_files"); recent_files = await ipcRenderer.invoke("getStoreValue", "recent_files");
let recent_files_elm = $("#recent-files"); let recent_files_elm = $("#recent-files");
if (recent_files.length > 0) { for (let file of recent_files) {
for (let file of recent_files) { if (fs.existsSync(file)) {
let match = RegExp(/[^\\]+.json/).exec(file); let match = RegExp(/[^\\]+.json/).exec(file);
recent_files_elm.append(`<div><button class="w3-btn w3-left-align" style="width: 100%" onclick="loadFile(recent_files[${recent_files.indexOf(file)}])">${match[0]}<br/>${file}</button></div>`) recent_files_elm.append(`<div><button class="w3-btn w3-left-align" style="width: 100%" onclick="loadFile(recent_files[${recent_files.indexOf(file)}])">${match[0]}<br/>${file}</button></div>`)
} }
} else { }
if (recent_files_elm.children().length == 0) {
recent_files_elm.append("<div class='w3-container'><h3>No recent files</h3></div>") recent_files_elm.append("<div class='w3-container'><h3>No recent files</h3></div>")
} }
} }
getRecentFiles(); getRecentFiles();
function loadFile(file_name) { function loadFile(file_name) {
ipcRenderer.send("loadFile", file_name.toString()); ipcRenderer.send("loadFile", file_name.toString());
} }
function newFile(file_name) { function newFile(file_name) {
ipcRenderer.send("newFile"); ipcRenderer.send("newFile");
} }
function openFile() { function openFile() {
ipcRenderer.send("openFile"); ipcRenderer.send("openFile");
} }