Skip to content

Commit

Permalink
Merge pull request #66 from YveIce/scripting-save-load
Browse files Browse the repository at this point in the history
  • Loading branch information
Heath123 authored Jun 14, 2022
2 parents 8108542 + 10dfa82 commit 0ebb733
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 0 deletions.
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.

0 comments on commit 0ebb733

Please sign in to comment.