Skip to content

Commit

Permalink
Save As menu command
Browse files Browse the repository at this point in the history
Supports saving/converting as either JSON or YAML
  • Loading branch information
XuluWarrior committed Sep 23, 2024
1 parent 47cbf70 commit a8785ac
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
42 changes: 41 additions & 1 deletion src/main/commands.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {BrowserWindow, dialog} from 'electron'
import {BrowserWindow, dialog, ipcMain} from 'electron'
import prompt from "electron-prompt";
import { promises as fs } from 'fs'
import path from 'path'
import isJsonObject from "is-json"
import { dump, load } from "js-yaml"
import request from 'phin'
Expand Down Expand Up @@ -51,6 +52,45 @@ export async function importFile() {
}
}

function getSpecLanguage(spec){
return spec.trim()[0] === "{" ? "json" : "yaml"
}

export async function saveAs() {
const window = BrowserWindow.getFocusedWindow()!
window.webContents.send("send-spec")

const spec = await new Promise<string>((resolve, reject) => {
const handleSpecStr = (_event, sentSpec) => {
resolve(sentSpec);
}
ipcMain.once('spec-str', handleSpecStr)
setTimeout(() => {
ipcMain.off('spec-str', handleSpecStr)
reject("Window didn't respond with spec");
}, 1000)
})

const specLang = getSpecLanguage(spec)
const filters = [
{ name: 'YAML', extensions: ['yml'] },
{ name: 'JSON', extensions: ['json'] }
]
const selected = await dialog.showSaveDialog({
filters: specLang === 'yaml' ? filters : filters.reverse(),
properties: ['createDirectory']
})

if (selected.canceled === false) {
const yamlContent = specLang === 'yaml' ? spec : dump(JSON.parse(spec))
const jsonContent = specLang === 'json' ? spec : JSON.stringify(load(spec), null, 2)

const fileContent = (path.extname(selected.filePath) === '.json') ? jsonContent : yamlContent

fs.writeFile(selected.filePath, fileContent, 'utf-8')
}
}

export async function clearEditor() {
BrowserWindow.getFocusedWindow()!.webContents.send('update-spec', '')
}
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, importURL } from "./commands";
import {clearEditor, importFile, importURL, saveAs} from "./commands";

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

Expand All @@ -18,6 +18,11 @@ const template = [
click: importURL
},
{ type: 'separator' },
{
label: 'Save As...',
click: saveAs
},
{ type: 'separator' },
{
label: 'Clear editor',
click: clearEditor
Expand Down
8 changes: 8 additions & 0 deletions src/renderer/src/plugins/electron-menus.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
type System = {
specActions: {
updateSpec: (spec: string) => void
},
specSelectors: {
specStr: () => string
}
}

Expand All @@ -9,6 +12,7 @@ class ElectronMenus {

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

Expand All @@ -24,6 +28,10 @@ class ElectronMenus {
}
}

sendSpec = () => {
const spec = this.system.specSelectors.specStr();
window.electron.ipcRenderer.send('spec-str', spec)
}

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

0 comments on commit a8785ac

Please sign in to comment.