diff --git a/src/Ation.js b/src/Ation.js index 18e85aa..43852cb 100644 --- a/src/Ation.js +++ b/src/Ation.js @@ -1,17 +1,21 @@ "use strict"; -const {app, protocol} = require("electron"); -const url = require("url"); -const path = require("path"); +const {app, protocol, dialog} = require("electron"); +const path = require("path"); +const fs = require("fs/promises"); const WindowManager = require("./WindowManager"); +const MainMenu = require("./MainMenu"); +const {parser} = require("./Parser"); class Ation { windowManager; + mainMenu; constructor() { if (Ation.Instances > 0) throw new Error("Only one Instance of Ation possible"); - this.windowManager = new WindowManager(); + this.windowManager = new WindowManager(this); + this.mainMenu = new MainMenu(this); app.whenReady().then(async () => { protocol.registerFileProtocol("slideimg", (request, callback) => { @@ -23,6 +27,30 @@ class Ation { }); }); } + + async openFile() { + if (!this.windowManager.mainWindow) + return null; + + const result = await dialog.showOpenDialog(this.mainWindow, { + title : "open file", + filters : [ + { + name : "Markdown files", + extensions : [".md"] + } + ] + }); + + if (result.canceled || result.filePaths.length < 1) + return null; + + const fileContents = await fs.readFile(result.filePaths[0], {encoding : "utf-8"}); + const basePath = path.dirname(result.filePaths[0]); + const data = parser(fileContents); + + return [basePath, data]; + } } Ation.Instances = 0; diff --git a/src/WindowManager.js b/src/WindowManager.js index e7eee28..11a6967 100644 --- a/src/WindowManager.js +++ b/src/WindowManager.js @@ -1,46 +1,24 @@ "use strict"; -const {app, BrowserWindow, ipcMain, dialog} = require("electron"); -const path = require("path"); -const fs = require("fs/promises"); -const util = require("util"); +const {app, BrowserWindow, ipcMain, dialog, Menu} = require("electron"); +const path = require("path"); +const fs = require("fs/promises"); +const util = require("util"); const {isDevelopment} = require("./Util"); -const {parser} = require("./Parser"); class WindowManager { mainWindow; windows; - constructor() { + constructor(parent) { + this.app = parent; this.mainWindow = null; this.windows = []; app.whenReady().then(() => this.init()); - ipcMain.handle("WindowManager::openFile", async () => { - if (!this.mainWindow) - return null; - - const result = await dialog.showOpenDialog(this.mainWindow, { - title : "open file", - filters : [ - { - name : "Markdown files", - extensions : [".md"] - } - ] - }); - - if (result.canceled || result.filePaths.length < 1) - return null; - - const fileContents = await fs.readFile(result.filePaths[0], {encoding : "utf-8"}); - const basePath = path.dirname(result.filePaths[0]); - const data = parser(fileContents); - - return [basePath, data]; - }); + ipcMain.handle("WindowManager::openFile", async () => this.app.openFile()); } init() { @@ -53,7 +31,7 @@ class WindowManager { if (isDevelopment()) this.mainWindow.loadURL("http://localhost:3000"); else - this.mainWindow.loadFile(`file://${path.join(__dirname, "ui", "build", "index.html")}`); + this.mainWindow.loadURL(`file://${__dirname}/ui/build/index.html`); } static _CreateWindow(options = null, parent = null) { @@ -61,6 +39,7 @@ class WindowManager { width : 800, height : 600, show : false, + devTools : isDevelopment(), webPreferences : { contextIsolation : true, preload : path.join(__dirname, "..", "contextAPI.js")