Permalink
Browse files

🔧 Move to ES6 modules

  • Loading branch information...
felixrieseberg committed Aug 25, 2018
1 parent 9018207 commit 86674b6090cc43a4535536e87fcba28a5666e5a6
View
@@ -1,4 +1,4 @@
node_modules
out
src/renderer/images
src/images
.DS_Store
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -9,7 +9,7 @@
"package": "electron-forge package",
"make": "electron-forge make",
"publish": "electron-forge publish",
"lint": "standard \"src/**/*.js\""
"lint": "standard \"src/**/*.mjs\""
},
"keywords": [],
"author": "Felix Rieseberg, felix@felixrieseberg.com",
@@ -19,6 +19,7 @@
},
"standard": {
"globals": [
"appState",
"V86Starter",
"windows95"
],
@@ -29,7 +30,8 @@
"dependencies": {
"electron-default-menu": "^1.0.1",
"electron-squirrel-startup": "^1.0.0",
"fs-extra": "^7.0.0"
"fs-extra": "^7.0.0",
"update-electron-app": "^1.3.0"
},
"devDependencies": {
"@electron-forge/cli": "^6.0.0-beta.22",

This file was deleted.

Oops, something went wrong.
View
@@ -0,0 +1,26 @@
const { protocol } = require('electron')
const fs = require('fs-extra')
const path = require('path')
const ES6_PATH = path.join(__dirname, 'renderer')
protocol.registerStandardSchemes(['es6'])
async function setupProtocol () {
protocol.registerBufferProtocol('es6', async (req, cb) => {
console.log(req)
try {
const filePath = path.join(ES6_PATH, req.url.replace('es6://', '' ))
const fileContent = await fs.readFile(filePath.replace('.js/', '.js'))
cb({ mimeType: 'text/javascript', data: fileContent })
} catch (error) {
console.warn(error)
}
})
}
module.exports = {
setupProtocol
}
View
@@ -3,6 +3,7 @@ const path = require('path')
const { clearCaches } = require('./cache')
const { createMenu } = require('./menu')
const { setupProtocol } = require('./es6')
if (require('electron-squirrel-startup')) { // eslint-disable-line global-require
app.quit()
@@ -16,22 +17,24 @@ const createWindow = () => {
width: 1280,
height: 800,
useContentSize: true,
nodeIntegration: false,
webPreferences: {
nodeIntegration: false,
preload: path.join(__dirname, 'preload.js')
}
})
mainWindow.loadURL(`file://${__dirname}/renderer/index.html?system=win98`)
mainWindow.loadURL(`file://${__dirname}/renderer/index.html`)
mainWindow.on('closed', () => {
mainWindow = null
})
}
app.on('ready', async () => {
await setupProtocol()
await createMenu()
await clearCaches()
createWindow()
})
View
@@ -1,43 +1,13 @@
const { remote } = require('electron')
const fs = require('fs-extra')
const { STATE_PATH, getState, resetState } = require('./state')
const { STATE_PATH, resetState, restoreState, saveState } = require('./state')
window.windows95 = {
STATE_PATH,
restoreState,
resetState,
saveState,
async saveState () {
return new Promise((resolve) => {
if (!window.emulator || !window.emulator.save_state) {
return resolve()
}
window.emulator.save_state(async (error, newState) => {
if (error) {
console.log(error)
return
}
await fs.outputFile(STATE_PATH, Buffer.from(newState))
console.log(`Saved state to ${STATE_PATH}`)
resolve()
})
})
},
async restoreState () {
try {
window.emulator.restore_state(getState())
} catch (error) {
console.log(`Could not read state file. Maybe none exists?`, error)
}
},
quit () {
remote.app.quit()
}
quit: () => remote.app.quit()
}
@@ -0,0 +1,7 @@
export function setupState () {
window.appState = {
cursorCaptured: false,
floppyFile: null,
bootFresh: false
}
}
View
@@ -0,0 +1,41 @@
const $ = document.querySelector.bind(document)
export function setupButtons (start) {
// Start
$('.btn-start').addEventListener('click', () => start())
// Reset
$('#reset').addEventListener('click', () => windows95.resetState())
$('#discard-state').addEventListener('click', () => {
window.appState.bootFresh = true
start('win95')
})
// Floppy
$('#floppy').addEventListener('click', () => {
$('#file-input').click()
})
// Floppy (Hidden Input)
$('#file-input').addEventListener('change', (event) => {
window.appState.floppyFile = event.target.files && event.target.files.length > 0
? event.target.files[0]
: null
if (window.appState.floppyFile) {
$('#floppy-path').innerHTML = `Inserted Floppy Disk: ${window.appState.floppyFile.path}`
}
})
}
export function toggleButtons (forceTo) {
const buttonElements = $('#buttons')
if (buttonElements.style.display !== 'none' || forceTo === false) {
buttonElements.style.display = 'none'
} else {
buttonElements.style.display = undefined
}
}
View
@@ -11,6 +11,7 @@
</head>
<body class="paused">
<div id="computer-info"></div>
<div id="buttons">
<div id="start-buttons">
<!-- <div class="btn" id="win98">Windows 98</div> -->
@@ -41,8 +42,8 @@
<div style="white-space: pre; font: 14px monospace; line-height: 14px"></div>
<canvas style="display: none"></canvas>
</div>
<script>
require('./renderer.js')
<script type="module">
import("es6://renderer.js")
</script>
</body>
View
@@ -0,0 +1,41 @@
export function setupCloseListener () {
window.appState.isQuitting = false
const handleClose = async () => {
await windows95.saveState()
window.appState.isQuitting = true
windows95.quit()
}
window.onbeforeunload = (event) => {
if (window.appState.isQuitting) return
handleClose()
event.preventDefault()
event.returnValue = false
}
}
export function setupEscListener () {
document.onkeydown = function (evt) {
evt = evt || window.event
if (evt.keyCode === 27) {
if (window.appState.cursorCaptured) {
window.appState.cursorCaptured = false
document.exitPointerLock()
} else {
window.appState.cursorCaptured = true
window.emulator.lock_mouse()
}
}
}
}
export function setupClickListener () {
document.addEventListener('click', () => {
if (!window.appState.cursorCaptured) {
window.appState.cursorCaptured = true
window.emulator.lock_mouse()
}
})
}
Oops, something went wrong.

0 comments on commit 86674b6

Please sign in to comment.