Skip to content

Commit

Permalink
Include open with
Browse files Browse the repository at this point in the history
  • Loading branch information
d-baranowski committed Oct 4, 2018
1 parent 9de9e5e commit fc71a02
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 39 deletions.
54 changes: 54 additions & 0 deletions app/FileManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import fs from 'fs';
import { dialog } from 'electron';

class FileManager {
savePath = undefined;
mainWindow;

constructor(mainWindow) {
this.mainWindow = mainWindow;
}

openFile(path) {
if (path && path[0]) {
this.savePath = path[0];
fs.readFile(path[0], 'utf8', (err, data) => {
if (err) throw err;

this.mainWindow.webContents.executeJavaScript(`document.store.dispatch({type: "LOAD_CHARACTER", payload: '${data}'})`);
});
}
}

saveFile() {
this.mainWindow.webContents.executeJavaScript(`JSON.stringify(document.store.getState())`, (state) => {
if (this.savePath) {
fs.writeFile(this.savePath, state, (err) => {
// ToDo handle error
});
} else {
dialog.showSaveDialog({filters: [{name: "Character", extensions: ["dnd"]}]}, (fileName) => {
if (fileName === undefined) return;
this.savePath = fileName;
fs.writeFile(fileName, state, (err) => {
// ToDo handle error
});
});
}
});
}

saveAs() {
this.mainWindow.webContents.executeJavaScript(`JSON.stringify(document.store.getState())`, (state) => {
dialog.showSaveDialog({filters: [{name: "Character", extensions: ["dnd"]}]}, (fileName) => {
if (fileName === undefined) return;
this.savePath = fileName;
fs.writeFile(fileName, state, (err) => {
// ToDo handle error
});
});
});
}
}

export default FileManager;
7 changes: 7 additions & 0 deletions app/appEventsHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default (fileManager, process) => {
const path = process.argv;
this.mainWindow.webContents.executeJavaScript(`document.store.dispatch({type: "SELECT_ALIGNMENT", payload: '${path}'})`);

fileManager.openFile(path);
}

12 changes: 11 additions & 1 deletion app/main.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
*/
import { app, BrowserWindow } from 'electron';
import MenuBuilder from './menu';
import FileManager from './FileManager';
import appEventsHandler from './appEventsHandler';
const path = require('path');

let mainWindow = null;

Expand Down Expand Up @@ -84,6 +87,13 @@ app.on('ready', async () => {
mainWindow = null;
});

const menuBuilder = new MenuBuilder(mainWindow);
const fileManager = new FileManager(mainWindow);
const menuBuilder = new MenuBuilder(mainWindow, fileManager);

if (path.parse(process.argv[1])) {
const filePath = path.parse(process.argv[1]);
fileManager.openFile([path.join(filePath.dir, filePath.base)]);
}

menuBuilder.buildMenu();
});
45 changes: 8 additions & 37 deletions app/menu.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// @flow
import fs from 'fs';
import { app, Menu, BrowserWindow, dialog } from 'electron';
const path = require('path');

export default class MenuBuilder {
mainWindow: BrowserWindow;
fileManager;

constructor(mainWindow: BrowserWindow) {
constructor(mainWindow: BrowserWindow, fileManager) {
this.mainWindow = mainWindow;
this.fileManager = fileManager;
}

buildMenu() {
Expand Down Expand Up @@ -139,8 +142,6 @@ export default class MenuBuilder {
return [subMenuAbout, subMenuEdit, subMenuView, subMenuWindow];
}

savePath = undefined;

buildDefaultTemplate() {
const templateDefault = [
{
Expand All @@ -151,50 +152,20 @@ export default class MenuBuilder {
accelerator: 'Ctrl+O',
click: () => {
const path = dialog.showOpenDialog({properties: ['openFile'], filters: [{name: "Character", extensions: ["dnd"]}]});
if (path && path[0]) {
this.savePath = path[0];
fs.readFile(path[0], 'utf8', (err, data) => {
if (err) throw err;

this.mainWindow.webContents.executeJavaScript(`document.store.dispatch({type: "LOAD_CHARACTER", payload: '${data}'})`);
});
}
this.fileManager.openFile(path);
}
},
{
label: '&Save',
accelerator: 'Ctrl+S',
click: () => {
this.mainWindow.webContents.executeJavaScript(`JSON.stringify(document.store.getState())`, (state) => {

if (this.savePath) {
fs.writeFile(this.savePath, state, (err) => {
// ToDo handle error
});
} else {
dialog.showSaveDialog({filters: [{name: "Character", extensions: ["dnd"]}]}, (fileName) => {
if (fileName === undefined) return;
this.savePath = fileName;
fs.writeFile(fileName, state, (err) => {
// ToDo handle error
});
});
}
});
this.fileManager.saveFile();
}
},
{
label: '&Save As',
click: () => {
this.mainWindow.webContents.executeJavaScript(`JSON.stringify(document.store.getState())`, (state) => {
dialog.showSaveDialog({filters: [{name: "Character", extensions: ["dnd"]}]}, (fileName) => {
if (fileName === undefined) return;
this.savePath = fileName;
fs.writeFile(fileName, state, (err) => {
// ToDo handle error
});
});
});
this.fileManager.saveAs();
}
},
{
Expand All @@ -203,7 +174,7 @@ export default class MenuBuilder {
click: () => {
this.mainWindow.close();
}
}
},
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion app/routes/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default () => (
<Switch>
<Route exact path="/" component={PageOne} />
<Route exact path="/equipment" component={Inventory} />
<Route exact path="/test" component={() => "test"} />
<Route exact path="/test" component={() => process.argv[2].toString()} />
</Switch>

</App>
Expand Down

0 comments on commit fc71a02

Please sign in to comment.