Skip to content

Commit

Permalink
Merge pull request #24 from XuluWarrior/electron-menus
Browse files Browse the repository at this point in the history
Add file (import/clear) and help menu items to Electron menus
  • Loading branch information
XuluWarrior authored Sep 23, 2024
2 parents 2d98f4e + 4b7837a commit c2d332a
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 0 deletions.
16 changes: 16 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 @@ -39,12 +39,15 @@
"@electron-toolkit/preload": "^3.0.1",
"@electron-toolkit/utils": "^3.0.0",
"electron-updater": "^6.3.4",
"is-json": "^2.0.1",
"js-yaml": "^4.1.0",
"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/js-yaml": "^4.0.9",
"@types/node": "^22.5.4",
"@types/react": "^18.3.5",
"@types/react-dom": "^18.3.0",
Expand Down
35 changes: 35 additions & 0 deletions src/main/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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: BrowserWindow, content: string) {
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: any) {
dialog.showErrorBox('Error loading file', `Oof! There was an error loading your document:\n\n${e.message || e}`)
}
}
}

export async function clearEditor() {
BrowserWindow.getFocusedWindow()!.webContents.send('update-spec', '')
}
2 changes: 2 additions & 0 deletions src/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { join } from 'path'
import { electronApp, optimizer, is } from '@electron-toolkit/utils'
import icon from '../../resources/icon.png?asset'

import './menus'

function createWindow(): void {
// Create the browser window.
const mainWindow = new BrowserWindow({
Expand Down
69 changes: 69 additions & 0 deletions src/main/menus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { Menu, MenuItemConstructorOptions, shell } from 'electron'

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

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

const template = [
{ role: 'appMenu'},
{
label: 'File',
submenu: [
{
label: 'Import file',
click: importFile
},
{
label: 'Clear editor',
click: clearEditor
},
{ type: 'separator' },
isMac ? { role: 'close' } : { role: 'quit' }
]
},
{ role: 'editMenu' },
{ role: 'viewMenu' },
{ role: 'windowMenu' },
{
role: 'help',
submenu: [
{
label: 'Swagger Editor Offline',
submenu: [
{
label: 'View on GitHub',
click: async () => {
await shell.openExternal('https://github.com/XuluWarrior/swagger-editor-offline')
}
}
]
},
{
label: 'Swagger Editor',
submenu: [
{
label: 'About Swagger Editor',
click: async () => {
await shell.openExternal('https://swagger.io/tools/swagger-editor/')
}
},
{
label: 'View Docs',
click: async () => {
await shell.openExternal('https://swagger.io/docs/open-source-tools/swagger-editor/')
}
},
{
label: 'View on GitHub',
click: async () => {
await shell.openExternal('https://github.com/swagger-api/swagger-editor')
}
}
]
},
]
}
] as MenuItemConstructorOptions[]

const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
5 changes: 5 additions & 0 deletions src/renderer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@ import 'swagger-editor-dist/swagger-editor.css'
import SwaggerEditorBundle from "swagger-editor-dist/swagger-editor-bundle.js";
import SwaggerEditorStandalonePreset from "swagger-editor-dist/swagger-editor-standalone-preset.js";

import electronMenus from './plugins/electron-menus'

window.onload = function() {
// Build a system\
const editor = window.SwaggerEditorBundle({
dom_id: '#swagger-editor',
layout: 'StandaloneLayout',
presets: [
window.SwaggerEditorStandalonePreset
],
plugins: [
electronMenus
]
});

Expand Down
26 changes: 26 additions & 0 deletions src/renderer/src/plugins/electron-menus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
type System = {
specActions: {
updateSpec: (spec: string) => void
}
}

class ElectronMenus {
system!: System;

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

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

setupPlugin = (system) => {
this.system = system;
return {}
}
};

const electronMenus = new ElectronMenus()

export default electronMenus.setupPlugin

0 comments on commit c2d332a

Please sign in to comment.