Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds the ability to save and load user scripts. #66

Merged
merged 1 commit into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions html/mainPage/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ <h3>Scripting (beta)</h3>
<input id="enableScripting" name="enableScripting" onchange="sharedVars.scripting.updateScript(true)"
type="checkbox">
<label for="enableScripting">Enable scripting</label>
&nbsp;
<button onclick="saveScript()">Save</button>
<button onclick="loadScript()">Load</button>
<br><br>
<textarea id="scriptEditor">
</textarea>
Expand Down
9 changes: 9 additions & 0 deletions html/mainPage/js/ipcHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,13 @@ exports.setup = function (passedSharedVars) {
sharedVars.packetDom.addPacketToDOM(packet)
}
})

sharedVars.ipcRenderer.on('loadScriptData', (event, arg) => {
window.scriptEditor.getDoc().setValue(arg)
sharedVars.ipcRenderer.send('scriptStateChange', JSON.stringify({ //
scriptingEnabled: document.getElementById('enableScripting').checked,
script: arg
}))
})

}
8 changes: 8 additions & 0 deletions html/mainPage/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,3 +671,11 @@ function saveLog() {
function loadLog() {
sharedVars.ipcRenderer.send('loadLog', '')
}

function saveScript() {
sharedVars.ipcRenderer.send('saveScript', window.scriptEditor.getDoc().getValue() )
}

function loadScript() {
sharedVars.ipcRenderer.send('loadScript', '')
}
42 changes: 42 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,47 @@ ipcMain.on('loadLog', async (event, arg) => {
}
})

ipcMain.on('saveScript', async (event, arg) => {
const win = BrowserWindow.getAllWindows()[0]

const result = await dialog.showSaveDialog(win, {
title: "Save user script",
filters: [
{ name: 'javascript files', extensions: ['js'] }
]
})

if (!result.canceled) {
const realPath = result.filePath.endsWith('.js') ? result.filePath : result.filePath + '.js'
console.log('Saving script to', realPath)
fs.writeFile(realPath, arg, function (err) {
if (err) throw err;
console.log('Saved!');
})
}
})

ipcMain.on('loadScript', async (event, arg) => {
const win = BrowserWindow.getAllWindows()[0]

const result = await dialog.showOpenDialog(win, {
title: "Load user script",
filters: [
{ name: 'javascript files', extensions: ['js'] }
],
properties: ['openFile']
})

if (!result.canceled) {
// It's an array, but we have multi-select off so it should only have one item
console.log('Loading script from', result.filePaths[0])
fs.readFile(result.filePaths[0], 'utf-8', function(err, data) {
if (err) throw err;
console.log('File has been read')
win.send('loadScriptData', data)
})
}
})

// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.