1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-11-22 14:33:51 +00:00

electron: fixed binary encoding, reload, args

This commit is contained in:
Steven Hugg 2020-08-02 20:22:20 -05:00
parent 40af6b2602
commit 7712119bca
2 changed files with 52 additions and 32 deletions

View File

@ -3,27 +3,27 @@ const { ipcRenderer } = require('electron');
const fs = require('fs');
const modpath = require('path');
// workspace root path
// TODO: reloading clears this
var wsroot = null;
// from browser: read workspace file synchronously
window.getWorkspaceFile = function(path, filetype) {
process.once('loaded', () => {
// workspace root path
// reload() clears this, so we have to set it every time
var wsroot;
// from browser: read workspace file synchronously
window.getWorkspaceFile = function(path, filetype) {
if (wsroot == null) throw Error("no workspace root set");
try {
// TODO: detect text or binary, or decide based on incbin vs. source file
var fullpath = modpath.join(wsroot, modpath.normalize(path));
var data = fs.readFileSync(fullpath, {encoding:filetype=='text'?'utf8':'binary'});
console.log("getWorkspaceFile", path, filetype);
var encoding = filetype == 'text' ? 'utf8' : null;
var data = fs.readFileSync(fullpath, {encoding:encoding});
if (encoding == null) data = new Uint8Array(data);
console.log("getWorkspaceFile", path, filetype, data.length);
// TODO: add to watched files
return data;
} catch (e) {
console.log(e);
return null;
}
}
process.once('loaded', () => {
}
// from electron.js: set workspace root directory
ipcRenderer.on('setWorkspaceRoot', (event, data) => {
wsroot = data.root;

View File

@ -38,7 +38,7 @@ class Workspace {
}
}
function openFile(path) {
function openFile(path, wnd) {
if (!fs.existsSync(path)) {
dialog.showMessageBox({
type: "error",
@ -49,24 +49,38 @@ function openFile(path) {
}
var dirname = modpath.dirname(path);
var filename = modpath.basename(path);
var wnd = BrowserWindow.getFocusedWindow();
if (wnd.workspace) { wnd.workspace.close(); }
if (!wnd) wnd = BrowserWindow.getFocusedWindow();
var ws = new Workspace(dirname, filename, wnd);
if (wnd.workspace) { wnd.workspace.close(); }
wnd.workspace = ws;
wnd.on('closed', () => {
ws.close();
});
var qs = new URLSearchParams();
qs.set('electron_ws', 1);
qs.set('repo', dirname);
qs.set('file', filename);
wnd.loadURL(`file://${__dirname}/electron.html?${qs}`).then(() => {
wnd.webContents.send('setWorkspaceRoot', {root:dirname});
openWorkspace(wnd, ws);
app.addRecentDocument(path);
store.set(KEY_lastWorkspaceFilePath, path);
}
function openWorkspace(wnd, ws) {
var qs = new URLSearchParams();
qs.set('electron_ws', 1);
qs.set('repo', ws.directory);
qs.set('file', ws.mainfile);
wnd.loadURL(`file://${__dirname}/electron.html?${qs}`).then(() => {
wnd.webContents.send('setWorkspaceRoot', {root:ws.directory});
});
}
// TODO: doesn't work if browser window reloads itself
function reloadCurrentWindow() {
var wnd = BrowserWindow.getFocusedWindow();
if (wnd.workspace) {
openWorkspace(wnd, wnd.workspace);
} else {
wnd.reload();
}
}
function openFileDialog() {
dialog.showOpenDialog({
title: "Open File",
@ -80,8 +94,11 @@ function openFileDialog() {
}
function openDefaultFile() {
createWindow();
/*
var wnd = createWindow();
if (process.argv.length >= 3) {
openFile(process.argv[2], wnd);
}
/* TODO
var lastfile = store.get(KEY_lastWorkspaceFilePath);
if (lastfile != null) {
openFile(lastfile);
@ -182,8 +199,10 @@ function buildMenu() {
{
label: 'View',
submenu: [
{ role: 'reload' },
{ role: 'forcereload' },
{
label: 'Reload Window',
click: reloadCurrentWindow
},
{ role: 'toggledevtools' },
{ type: 'separator' },
{ role: 'resetzoom' },
@ -300,3 +319,4 @@ ipcMain.on('hello', (event, message) => {
console.log(event);
});
*/