From 1815e145e465c3f0f7cec665219e84ea1f424361 Mon Sep 17 00:00:00 2001 From: XuluWarrior Date: Mon, 23 Sep 2024 01:27:20 +0200 Subject: [PATCH] Import file menu command Derived from https://github.com/swagger-api/swagger-editor/blob/master/src/standalone/topbar-menu-file-import_file/components/ImportFileMenuItem.jsx --- src/main/commands.ts | 31 +++++++++++++++++++++++++++++++ src/main/menus.ts | 16 +++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/main/commands.ts diff --git a/src/main/commands.ts b/src/main/commands.ts new file mode 100644 index 0000000..872c0f2 --- /dev/null +++ b/src/main/commands.ts @@ -0,0 +1,31 @@ +import {BrowserWindow, dialog} from 'electron' +import { promises as fs } from 'fs' + +import { dump, load } from "js-yaml" +import isJsonObject from "is-json" + +function updateSpec(window, content) { + const preparedContent = isJsonObject(content) ? dump(load(content)) : content + + window.webContents.send('update-spec', preparedContent); +} + +export async function importFile() { + const window = BrowserWindow.getFocusedWindow() + const selected = await dialog.showOpenDialog({ + filters: [ + { name: 'OpenAPI', extensions: ['json', 'yml', 'yaml'] }, + { name: 'All Files', extensions: ['*'] } + ], + properties: ['openFile'] + }) + + if (!selected.canceled) { + try { + const content = (await fs.readFile(selected.filePaths[0])).toString() + updateSpec(window, content) + } catch(e) { + dialog.showErrorBox('Error loading file', `Oof! There was an error loading your document:\n\n${e.message || e}`) + } + } +} diff --git a/src/main/menus.ts b/src/main/menus.ts index 75048ef..941a317 100644 --- a/src/main/menus.ts +++ b/src/main/menus.ts @@ -1,8 +1,22 @@ import { Menu, shell } from 'electron' +import { importFile } from "./commands"; + +const isMac = process.platform === 'darwin' + const template = [ { role: 'appMenu'}, - { role: 'fileMenu' }, + { + label: 'File', + submenu: [ + { + label: 'Import file', + click: importFile + }, + { type: 'separator' }, + isMac ? { role: 'close' } : { role: 'quit' } + ] + }, { role: 'editMenu' }, { role: 'viewMenu' }, { role: 'windowMenu' },