|
const { app, dialog, BrowserWindow, Menu } = require("electron") |
|
const fs = require("fs") |
|
|
|
// Opt-in to new behaviour: https://github.com/electron/electron/issues/18397. |
|
app.allowRendererProcessReuse = true |
|
|
|
function getFilePath(window, argPos, extensions, title, message) { |
|
if (process.argv.length >= argPos + 1) { |
|
return process.argv[argPos] |
|
} |
|
const result = dialog.showOpenDialogSync(window, { |
|
title, |
|
message, |
|
properties: ["openFile"], |
|
filters: [{ name: extensions[0], extensions }, { name: "json", extensions: ["json"] }] |
|
}) |
|
return result === undefined ? undefined : result[0] |
|
} |
|
|
|
function loadJson(path) { |
|
try { |
|
return fs.readFileSync(path, "utf8") |
|
} catch (e) { |
|
console.error(`Failed to load json from path: ${path}, error: ${e}`) |
|
return undefined |
|
} |
|
} |
|
|
|
app.on("ready", async () => { |
|
// Create window. |
|
const window = new BrowserWindow({ width: 1280, height: 1024 }) |
|
|
|
// Create function to load the editor. |
|
const loadEditor = async () => { |
|
console.log("Loading editor") |
|
await window.loadURL("https://www.bastian.tech/tree/?mode=integration") |
|
} |
|
|
|
// Create function to load a scheme into the editor. |
|
const loadScheme = async path => { |
|
console.log(`Loading scheme from path: ${path}`) |
|
const schemeJson = loadJson(path) |
|
if (schemeJson === undefined) { |
|
app.quit() |
|
return |
|
} |
|
await window.webContents.executeJavaScript( |
|
`window.enqueueLoadScheme(decodeURIComponent(\`${encodeURIComponent(schemeJson)}\`))`) |
|
} |
|
|
|
// Create function to load a tree into the editor. |
|
const loadTree = async (path, focusTree) => { |
|
console.log(`Loading tree from path: ${path}`) |
|
const treeJson = loadJson(path) |
|
if (treeJson === undefined) { |
|
app.quit() |
|
return |
|
} |
|
const name = '' |
|
await window.webContents.executeJavaScript( |
|
`window.enqueueLoadTree(decodeURIComponent(\`${encodeURIComponent(treeJson)}\`), name, ${focusTree})`) |
|
} |
|
|
|
// Create function to save the tree json. |
|
const saveTree = async path => { |
|
const treeJson = await window.webContents.executeJavaScript("window.getCurrentTreeJson()") |
|
fs.writeFile(path, treeJson, "utf8", () => { |
|
console.log(`Saved tree-json to path: ${path}`) |
|
}) |
|
} |
|
|
|
// Create menu. |
|
const menu = Menu.buildFromTemplate([{ |
|
label: "Tree", |
|
submenu: [ |
|
{ |
|
label: "Save", |
|
accelerator: "CmdOrCtrl+S", |
|
click: () => { |
|
// Expects treeJson path being defined as soon as its known. |
|
if (treeJsonPath === undefined) { |
|
console.warn("Failed to save: No 'treeJsonPath' known yet") |
|
} else { |
|
saveTree(treeJsonPath) |
|
} |
|
} |
|
}, |
|
{ |
|
label: "Quit", |
|
accelerator: "CmdOrCtrl+Q", |
|
click: app.quit |
|
} |
|
] |
|
}]) |
|
Menu.setApplicationMenu(menu) |
|
|
|
// Load editor. |
|
loadEditor() |
|
|
|
// Load scheme file into editor. |
|
const schemeJsonPath = getFilePath(window, 2, ["btscheme","btschema"], "scheme", "Select the scheme json file to use") |
|
if (schemeJsonPath === undefined) { |
|
app.quit() |
|
return |
|
} |
|
await loadScheme(schemeJsonPath) |
|
|
|
// Automatically reload scheme if it changes in the background. |
|
fs.watchFile(schemeJsonPath, { persistent: false, interval: 1000 }, () => { |
|
console.log("Scheme changed, reloading file") |
|
loadScheme(schemeJsonPath) |
|
}) |
|
|
|
// Load tree file into editor. |
|
const treeJsonPath = getFilePath(window, 3, ["bt"], "tree", "Select the tree json file to edit") |
|
if (treeJsonPath === undefined) { |
|
app.quit() |
|
return |
|
} |
|
await loadTree(treeJsonPath, true) |
|
|
|
// Automatically reload tree if it changes in the background. |
|
fs.watchFile(treeJsonPath, { persistent: false, interval: 1000 }, () => { |
|
console.log("Tree changed, reloading file") |
|
loadTree(treeJsonPath, false) |
|
}) |
|
}) |
|
|
|
app.on("window-all-closed", () => { |
|
console.log("Shutting down app") |
|
app.quit() |
|
}) |
Updated to Electron 24