Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.
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
37 changes: 33 additions & 4 deletions assets/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,14 @@
var electron = require('electron')
var localLog = console.log
var localError = console.error
var remoteLog = electron.remote.getGlobal('console').log
var remoteError = electron.remote.getGlobal('console').error
var remoteLog = function (o) {
const payload = JSON.parse(JSON.stringify(o, null, 2))
electron.ipcRenderer.invoke('consoleLog', `${payload}`)
}
var remoteError = function (o) {
const payload = JSON.parse(JSON.stringify(o, null, 2))
electron.ipcRenderer.invoke('consoleError', `${payload}`)
}

console.log = function (...args) {
localLog.apply(console, args)
Expand All @@ -20,12 +26,35 @@
remoteError(...args)
}

process.exit = electron.remote.app.quit
process.exit = function(status) {electron.ipcRenderer.sendSync('exit')}
// redirect errors to stderr
window.addEventListener('error', function (e) {
e.preventDefault()
console.error(e.error.stack || 'Uncaught ' + e.error)
console.log(e)
console.error(e.error?.stack || 'Uncaught ' + e.error)
})

electron.ipcRenderer.once('window-setup', (event, msg) => {
const {
config,
rootPath,
data,
title,
} = msg
var rootView = require(rootPath)
var h = require('mutant/h')

electron.webFrame.setVisualZoomLevelLimits(1, 1)

document.documentElement.querySelector('head').appendChild(
h('title', title)
)

document.documentElement.replaceChild(h('body', [
rootView(config, data)
]), document.body)
})

</script>
</body>
</html>
68 changes: 62 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,66 @@ electron.app.on('ready', () => {
quitting = true
})

electron.ipcMain.on('open-background-devtools', function () {
if (windows.background) {
windows.background.webContents.openDevTools({ mode: 'detach' })
}
electron.ipcMain.handle('navigation-menu-popup', (event, data) => {
const {items, x, y} = data
const window = event.sender
const factor = event.sender.zoomFactor
const menuItems = buildMenu(items, window)
const menu = electron.Menu.buildFromTemplate(menuItems);
menu.popup({
window,
x: Math.round(x * factor),
y: Math.round(y * factor) + 4,
});
})

electron.ipcMain.handle('consoleLog', (ev, o) => console.log(o))
electron.ipcMain.handle('consoleError', (ev, o) => console.error(o))
electron.ipcMain.handle('badgeCount', (ev, count) => {
electron.app.badgeCount = count;
});
electron.ipcMain.on('exit', (ev, code) => process.exit(code))

})

function openServerDevTools () {
if (windows.background) {
windows.background.webContents.openDevTools({ mode: 'detach' })
}
}

function buildMenu(items, window) {
const result = []
for (let item of items) {
switch (item.type) {
case 'separator':
result.push(item)
break
case 'submenu':
result.push({
...item,
submenu: buildMenu(item.submenu, window),
})
break
case 'normal':
result.push({
...item,
click: () => navigateTo(item.target)
})
break
default:
throw Error(`Unknown menu item of type "${item.type}": ${JSON.stringify(item, null, 2)}`);
}
}
return result
}

function navigateTo(target) {
if (windows?.main) {
windows.main.send('navigate-to', target)
}
}

function openMainWindow () {
if (!windows.main) {
const windowState = WindowState({
Expand All @@ -174,8 +227,11 @@ function openMainWindow () {
title: 'Patchwork',
show: true,
backgroundColor: '#EEE',
icon: Path.join(__dirname, 'assets/icon.png')
})
icon: Path.join(__dirname, 'assets/icon.png'),
},
openServerDevTools,
navigateTo,
)

windowState.manage(windows.main)
windows.main.setSheetOffset(40)
Expand Down
196 changes: 0 additions & 196 deletions lib/context-menu-and-spellcheck.js

This file was deleted.

Loading