Skip to content

Commit

Permalink
feat: support open using url
Browse files Browse the repository at this point in the history
  • Loading branch information
Illusion47586 committed Jul 15, 2022
1 parent 253999e commit 1c63734
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 23 deletions.
8 changes: 6 additions & 2 deletions src/main/baseEventListeners.ts
Expand Up @@ -14,8 +14,12 @@ app.on('open-url', (_, url) => {

ipcMain.on(
'PLUTO-OPEN-NOTEBOOK',
async (_event, path?: string, forceNew?: boolean): Promise<void> =>
openNotebook(path, forceNew)
async (
_event,
pathOrURL?: string,
forceNew = false,
type: 'path' | 'url' = 'path'
): Promise<void> => openNotebook(pathOrURL, forceNew, type)
);

ipcMain.on(
Expand Down
47 changes: 29 additions & 18 deletions src/main/pluto.ts
Expand Up @@ -119,42 +119,53 @@ let plutoURL: PlutoURL | null = null;
* * a URL to a new notebook if no path passed
* * an Error in all other cases
*/
const openNotebook = async (path?: string, forceNew = false) => {
const openNotebook = async (
pathOrURL?: string,
forceNew = false,
type: 'url' | 'path' = 'path'
) => {
const window = BrowserWindow.getFocusedWindow()!;

if (path && !isExtMatch(path)) {
if (type === 'path' && pathOrURL && !isExtMatch(pathOrURL)) {
dialog.showErrorBox(
'PLUTO-CANNOT-OPEN-NOTEBOOK',
'Not a supported file type.'
);
return;
}

if (!forceNew && !path) {
const r = await dialog.showOpenDialog(window, {
message: 'Please select a Pluto Notebook.',
filters: [
{
name: 'Pluto Notebook',
extensions: PLUTO_FILE_EXTENSIONS.map((v) => v.slice(1)),
},
],
properties: ['openFile'],
});
if (!forceNew && !pathOrURL) {
if (type === 'path') {
const r = await dialog.showOpenDialog(window, {
message: 'Please select a Pluto Notebook.',
filters: [
{
name: 'Pluto Notebook',
extensions: PLUTO_FILE_EXTENSIONS.map((v) => v.slice(1)),
},
],
properties: ['openFile'],
});

if (r.canceled) return;
if (r.canceled) return;

// eslint-disable-next-line no-param-reassign
[path] = r.filePaths;
// eslint-disable-next-line no-param-reassign
[pathOrURL] = r.filePaths;
}
}

const loader = new Loader(window);

if (plutoURL) {
let query = '';
if (pathOrURL) {
if (type === 'path') query = `&path=${pathOrURL}`;
else query = `&url=${pathOrURL}`;
}
const res = await axios.post(
`http://localhost:${plutoURL.port}/${path ? 'open' : 'new'}?secret=${
`http://localhost:${plutoURL.port}/${pathOrURL ? 'open' : 'new'}?secret=${
plutoURL.secret
}${path ? `&path=${path}` : ''}`
}${query}`
);
if (res.status === 200) {
await window.loadURL(
Expand Down
8 changes: 6 additions & 2 deletions src/main/preload.ts
Expand Up @@ -20,8 +20,12 @@ contextBridge.exposeInMainWorld('plutoDesktop', {
},
},
fileSystem: {
openNotebook(path?: string, forceNew?: boolean) {
ipcRenderer.send('PLUTO-OPEN-NOTEBOOK', path, forceNew);
openNotebook(
pathOrURL?: string,
forceNew?: boolean,
type?: 'url' | 'path'
) {
ipcRenderer.send('PLUTO-OPEN-NOTEBOOK', pathOrURL, forceNew, type);
},
shutdownNotebook(id?: string) {
ipcRenderer.send('PLUTO-SHUTDOWN-NOTEBOOK', id);
Expand Down
6 changes: 5 additions & 1 deletion src/renderer/preload.d.ts
Expand Up @@ -19,7 +19,11 @@ declare global {
* opens that notebook. If false and no path is there, opens the file selector.
* If true, opens a new blank notebook.
*/
openNotebook(path?: string, forceNew?: boolean): void;
openNotebook(
pathOrURL?: string,
forceNew?: boolean,
type?: 'url' | 'path'
): void;
shutdownNotebook(id?: string): void;
moveNotebook(id?: string): void;
exportNotebook(id: string, type: PlutoExport): void;
Expand Down

0 comments on commit 1c63734

Please sign in to comment.