Skip to content

Commit

Permalink
Merge pull request #25 from XuluWarrior/import-url
Browse files Browse the repository at this point in the history
Import URL menu command
  • Loading branch information
XuluWarrior authored Sep 23, 2024
2 parents c2d332a + 23ef46a commit 47cbf70
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 5 deletions.
60 changes: 60 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,18 @@
"dependencies": {
"@electron-toolkit/preload": "^3.0.1",
"@electron-toolkit/utils": "^3.0.0",
"electron-prompt": "^1.7.0",
"electron-updater": "^6.3.4",
"is-json": "^2.0.1",
"js-yaml": "^4.1.0",
"phin": "^3.7.1",
"swagger-editor-dist": "^4.13.1"
},
"devDependencies": {
"@electron-toolkit/eslint-config-prettier": "^2.0.0",
"@electron-toolkit/eslint-config-ts": "^2.0.0",
"@electron-toolkit/tsconfig": "^1.0.1",
"@types/electron-prompt": "^1.6.5",
"@types/js-yaml": "^4.0.9",
"@types/node": "^22.5.4",
"@types/react": "^18.3.5",
Expand Down
25 changes: 23 additions & 2 deletions src/main/commands.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
import {BrowserWindow, dialog} from 'electron'
import prompt from "electron-prompt";
import { promises as fs } from 'fs'

import { dump, load } from "js-yaml"
import isJsonObject from "is-json"
import { dump, load } from "js-yaml"
import request from 'phin'

function updateSpec(window: BrowserWindow, content: string) {
const preparedContent = isJsonObject(content) ? dump(load(content)) : content

window.webContents.send('update-spec', preparedContent);
}

export async function importURL() {
const window = BrowserWindow.getFocusedWindow()!
const url = await prompt({
title: 'Import URL',
label: 'Enter the URL to import from',
inputAttrs: {
type: 'url'
},
type: 'input'
}, window)
if (url) {
try {
const res = await request({method: "GET", url});
updateSpec(window, res.body.toString());
} catch(e: any) {
dialog.showErrorBox('Error loading URL', `Oof! There was an error loading your document:\n\n${e.message || e}`)
}
}
}

export async function importFile() {
const window = BrowserWindow.getFocusedWindow()!
const selected = await dialog.showOpenDialog({
Expand Down
4 changes: 2 additions & 2 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'

import { importURL } from './commands'
import './menus'

function createWindow(): void {
Expand Down Expand Up @@ -53,8 +54,7 @@ app.whenReady().then(() => {
optimizer.watchWindowShortcuts(window)
})

// IPC test
ipcMain.on('ping', () => console.log('pong'))
ipcMain.handle('import-url', importURL)

createWindow()

Expand Down
7 changes: 6 additions & 1 deletion src/main/menus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Menu, MenuItemConstructorOptions, shell } from 'electron'

import { clearEditor, importFile } from "./commands";
import { clearEditor, importFile, importURL } from "./commands";

const isMac = process.platform === 'darwin'

Expand All @@ -13,6 +13,11 @@ const template = [
label: 'Import file',
click: importFile
},
{
label: 'Import URL',
click: importURL
},
{ type: 'separator' },
{
label: 'Clear editor',
click: clearEditor
Expand Down
14 changes: 14 additions & 0 deletions src/renderer/src/plugins/electron-menus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,22 @@ class ElectronMenus {

constructor() {
window.electron.ipcRenderer.on("update-spec", this.updateSpec);
this.overridePrompt()
}

overridePrompt() {
const originalPrompt = window.prompt;
window.prompt = message => {
if (message === "Enter the URL to import from:") {
window.electron.ipcRenderer.invoke('import-url');
return "";
} else {
return originalPrompt(message);
}
}
}


updateSpec = (_event, spec) => {
this.system.specActions.updateSpec(spec);
}
Expand Down

0 comments on commit 47cbf70

Please sign in to comment.