Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
fix: open files via double-click & cli args (#619)
Browse files Browse the repository at this point in the history
* fix: open file cli arg

* fix: open file on mac
  • Loading branch information
faselbaum authored and tilmx committed Sep 20, 2018
1 parent fd259fe commit 6b982e1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
18 changes: 17 additions & 1 deletion src/electron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,16 @@ const CONTEXT: AppContext = Mobx.observable({
sender: undefined,
win: undefined,
hot: undefined,
middlewares: []
middlewares: [],
fileToOpen: undefined
});

async function main(): Promise<void> {
if (process.argv.length > 1) {
const fileToOpenArg = process.argv[1];
CONTEXT.fileToOpen = fileToOpenArg;
}

const args = yargsParser(process.argv.slice(2));
CONTEXT.hot = args.hot || false;
CONTEXT.base = args.base || '';
Expand Down Expand Up @@ -67,6 +73,16 @@ async function main(): Promise<void> {
});
}

Electron.app.on('open-file', async (event, path) => {
event.preventDefault();

if (!path) {
return;
}

CONTEXT.fileToOpen = path;
});

Electron.app.on('ready', main);

Electron.app.on('window-all-closed', () => {
Expand Down
39 changes: 24 additions & 15 deletions src/electron/start-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as getPort from 'get-port';
import * as Message from '../message';
import * as Mobx from 'mobx';
import * as Model from '../model';
import * as Fs from 'fs';
import { Sender } from '../sender/server';
import { showMainMenu } from './show-main-menu';
import { createServer } from '../server';
Expand All @@ -26,6 +27,7 @@ export interface AppContext {
sender: undefined | Sender;
win: undefined | Electron.BrowserWindow;
middlewares: Express.RequestHandler[];
fileToOpen: string | undefined;
}

export async function startApp(ctx: AppContext): Promise<{ emitter: Events.EventEmitter }> {
Expand Down Expand Up @@ -66,30 +68,37 @@ export async function startApp(ctx: AppContext): Promise<{ emitter: Events.Event

server.on('client-message', e => sender.send(e));

const onClose = e => {
e.preventDefault();
sender.send({
type: Message.MessageType.WindowClose,
id: uuid.v4(),
payload: undefined
});
};

Electron.app.on('will-finish-launching', () => {
Electron.app.on('open-file', async (event, path) => {
event.preventDefault();
Mobx.reaction(
() => ctx.fileToOpen,
() => {
if (!ctx.fileToOpen) {
return;
}

if (!path) {
// TODO: Move check to persistence.read and return appropriate error message.
if (!Fs.existsSync(ctx.fileToOpen)) {
return;
}

sender.send({
id: uuid.v4(),
type: Message.MessageType.OpenFileRequest,
payload: { path }
payload: { path: ctx.fileToOpen }
});
},
{
fireImmediately: true
}
);

const onClose = e => {
e.preventDefault();
sender.send({
type: Message.MessageType.WindowClose,
id: uuid.v4(),
payload: undefined
});
});
};

Electron.app.on('before-quit', async () => {
await Mobx.when(() => typeof ctx.project === 'undefined');
Expand Down

0 comments on commit 6b982e1

Please sign in to comment.