Skip to content

Commit

Permalink
Support for file drop into window
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrekker committed Apr 10, 2024
1 parent 3080b96 commit 0a2ad7e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 52 deletions.
4 changes: 4 additions & 0 deletions src/main/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,8 @@ export class Globals {
public static JULIA_PROJECT: string;
public static PLUTO_LOCATION: string;
public static PLUTO_SECRET: string = generateSecret();
public static PLUTO_URL: URL;
public static get PLUTO_STARTED(): boolean {
return !!this.PLUTO_URL;
}
}
31 changes: 12 additions & 19 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { store } from './store';
import { GlobalWindowManager } from './windowHelpers';
import { initGlobals, startup } from './startup';
import { Globals } from './globals';
import axios from 'axios';

generalLogger.verbose('---------- NEW SESSION ----------');
generalLogger.verbose('Application Version:', app.getVersion());
Expand Down Expand Up @@ -68,25 +69,6 @@ ipcMain.on('ipc-example', async (event, args) => {
*/

const createWindow = () => {
/**
* If window with {pathOrURL} is already open, focus on it
* else open a new one
*/
// if (!forceNew && pathOrURL) {
// const id = Pluto.notebook.getId(pathOrURL);
// if (id) {
// const windows = BrowserWindow.getAllWindows();
// const windowId = windows.findIndex((window) =>
// window.webContents.getURL().includes(id)
// );
// if (windowId !== -1) {
// windows[windowId].focus();
// return;
// }
// } else {
// generalLogger.log(`Opening ${pathOrURL} in new window.`);
// }
// }
generalLogger.announce('Creating a new window.');

const firstPluto = new Pluto();
Expand Down Expand Up @@ -115,6 +97,8 @@ app.on('open-file', async (_event, file) => {
// TODO: Implement filesystem open
_event.preventDefault();
console.log(file);
console.log(app.isReady());
console.log(GlobalWindowManager.getInstance().plutoWindows.length);
// await createWindow(file);
});

Expand Down Expand Up @@ -226,6 +210,15 @@ function createRequestListener() {
});
return;
}
// this route gets called when pasting a notebook into the welcome page
if (tail === 'notebookupload') {
next({
redirectURL: new URL(
`notebookupload?secret=${Globals.PLUTO_SECRET}`,
Globals.PLUTO_URL
).toString(),
});
}
}

next({ cancel });
Expand Down
38 changes: 17 additions & 21 deletions src/main/pluto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ class Pluto {
private id: string | undefined;

constructor(landingUrl: string | null = Pluto.resolveHtmlPath('index.html')) {
// currently Pluto functions as a singleton
// TODO: refactor to support arbitrary window counts

Pluto.url ??= null;
this.win = _createPlutoBrowserWindow();
if (landingUrl) {
this.win.loadURL(landingUrl);
Expand Down Expand Up @@ -127,14 +123,14 @@ class Pluto {

const loader = new Loader(window);

if (Pluto.url) {
if (Globals.PLUTO_URL) {
let params = {};
if (pathOrURL) {
generalLogger.log(`Trying to open ${pathOrURL}`);
if (type === 'path') {
setBlockScreenText(pathOrURL);
window.webContents.send('pluto-url', `Trying to open ${pathOrURL}`);
params = { secret: Pluto.url?.secret, path: pathOrURL };
params = { secret: Globals.PLUTO_SECRET, path: pathOrURL };
} else if (type === 'url') {
const newURL = new URL(pathOrURL);
if (newURL.searchParams.has('path')) {
Expand All @@ -144,7 +140,7 @@ class Pluto {
`Trying to open ${newURL.searchParams.get('path')}`
);
params = {
secret: Pluto.url?.secret,
secret: Globals.PLUTO_SECRET,
path: newURL.searchParams.get('path'),
};
} else {
Expand All @@ -154,14 +150,14 @@ class Pluto {
`Trying to open ${pathOrURL}`
);
params = {
secret: Pluto.url?.secret,
secret: Globals.PLUTO_SECRET,
url: pathOrURL,
};
}
}
} else {
params = {
secret: Pluto.url?.secret,
secret: Globals.PLUTO_SECRET,
};
}

Expand Down Expand Up @@ -224,7 +220,7 @@ class Pluto {
* @returns nothing
*/
private static exportNotebook = async (id: string, type: PlutoExport) => {
if (!this.url) {
if (!Globals.PLUTO_STARTED) {
dialog.showErrorBox(
'Pluto not intialized',
'Please wait for pluto to initialize first'
Expand All @@ -245,13 +241,13 @@ class Pluto {
let url: string | null;
switch (type) {
case PlutoExport.FILE:
url = `http://localhost:${this.url.port}/notebookfile?secret=${this.url.secret}&id=${id}`;
url = `http://localhost:${Globals.PLUTO_URL.port}/notebookfile?secret=${Globals.PLUTO_SECRET}&id=${id}`;
break;
case PlutoExport.HTML:
url = `http://localhost:${this.url.port}/notebookexport?secret=${this.url.secret}&id=${id}`;
url = `http://localhost:${Globals.PLUTO_URL.port}/notebookexport?secret=${Globals.PLUTO_SECRET}&id=${id}`;
break;
case PlutoExport.STATE:
url = `http://localhost:${this.url.port}/statefile?secret=${this.url.secret}&id=${id}`;
url = `http://localhost:${Globals.PLUTO_URL.port}/statefile?secret=${Globals.PLUTO_SECRET}&id=${id}`;
break;
default:
window.webContents.print();
Expand All @@ -270,7 +266,7 @@ class Pluto {
*/
private static shutdownNotebook = async (_id?: string) => {
try {
if (!this.url) {
if (!Globals.PLUTO_STARTED) {
dialog.showErrorBox(
'Pluto not intialized',
'Please wait for pluto to initialize first'
Expand All @@ -285,7 +281,7 @@ class Pluto {

const res = await axios.get('shutdown', {
params: {
secret: Pluto.url?.secret,
secret: Globals.PLUTO_SECRET,
id,
},
});
Expand Down Expand Up @@ -313,7 +309,7 @@ class Pluto {
*/
private static moveNotebook = async (_id?: string) => {
try {
if (!this.url) {
if (!Globals.PLUTO_STARTED) {
dialog.showErrorBox(
'Pluto not intialized',
'Please wait for pluto to initialize first'
Expand Down Expand Up @@ -342,7 +338,7 @@ class Pluto {
{},
{
params: {
secret: Pluto.url?.secret,
secret: Globals.PLUTO_SECRET,
id,
newpath: filePath,
},
Expand Down Expand Up @@ -376,7 +372,7 @@ class Pluto {
let result;

try {
if (!this.url) {
if (!Globals.PLUTO_STARTED) {
dialog.showErrorBox(
'Pluto not intialized',
'Please wait for pluto to initialize first'
Expand All @@ -387,7 +383,7 @@ class Pluto {
const res = await axios.get('notebooklist', {
responseType: 'arraybuffer',
params: {
secret: Pluto.url?.secret,
secret: Globals.PLUTO_SECRET,
},
});

Expand Down Expand Up @@ -415,7 +411,7 @@ class Pluto {
let result: string | boolean = false;

try {
if (!this.url) {
if (!Globals.PLUTO_STARTED) {
dialog.showErrorBox(
'Pluto not intialized',
'Please wait for pluto to initialize first'
Expand All @@ -426,7 +422,7 @@ class Pluto {
const res = await axios.get('notebooklist', {
responseType: 'arraybuffer',
params: {
secret: Pluto.url?.secret,
secret: Globals.PLUTO_SECRET,
},
});

Expand Down
10 changes: 3 additions & 7 deletions src/main/startup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,17 @@ export async function startup(app: App) {
if (dataString.includes('Loading') || dataString.includes('loading'))
statusUpdate('loading');

if (Pluto.url === null) {
if (!Globals.PLUTO_URL) {
const plutoLog = dataString;
if (plutoLog.includes('?secret=')) {
const urlMatch = plutoLog.match(/http\S+/g);
const entryUrl = urlMatch[0];

const tempURL = new URL(entryUrl);
Pluto.url = {
url: entryUrl,
port: tempURL.port,
secret: tempURL.searchParams.get('secret')!,
};
Globals.PLUTO_URL = new URL(`${tempURL.protocol}//${tempURL.host}`);

statusUpdate('loaded');
setAxiosDefaults(Pluto.url);
setAxiosDefaults(Globals.PLUTO_URL);

generalLogger.announce('Entry url found:', Pluto.url);
} else if (
Expand Down
9 changes: 4 additions & 5 deletions src/main/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,13 @@ const isUrlOrPath = (text: string) => {
return 'none';
};

const setAxiosDefaults = (url: PlutoURL) => {
const baseURL = new URL(url.url);
if (baseURL.hostname === 'localhost') {
const setAxiosDefaults = (url: URL) => {
if (url.hostname === 'localhost') {
// there are issues with IPv6 and Node.JS on certain hardware / operating systems
// the loopback IP is generally safer
baseURL.hostname = '127.0.0.1';
url.hostname = '127.0.0.1';
}
axios.defaults.baseURL = baseURL.origin;
axios.defaults.baseURL = url.origin;
axios.defaults.headers.common.Connection = 'keep-alive';
generalLogger.verbose('Base URL set to', axios.defaults.baseURL);
};
Expand Down

0 comments on commit 0a2ad7e

Please sign in to comment.