Skip to content

Commit

Permalink
upgrade to Electron 9
Browse files Browse the repository at this point in the history
  • Loading branch information
mb21 committed Jul 18, 2020
1 parent 1795139 commit 86ce8c0
Show file tree
Hide file tree
Showing 5 changed files with 265 additions and 651 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"react-dom": "^16.6.1"
},
"devDependencies": {
"electron": "5.0.1",
"electron": "9.1.0",
"electron-builder": "^22.7.0",
"psc-package": "^3.0.1",
"purescript": "^0.12.1"
Expand Down
55 changes: 29 additions & 26 deletions src/Panwriter/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,35 +54,38 @@ ipcRenderer.on('fileSave', function(_event, opts) {
opts = {};
}
var filePath = Document.getPath();
if (filePath === undefined || opts.saveAsNewFile) {
var win = remote.getCurrentWindow();
filePath = remote.dialog.showSaveDialog(win, {
defaultPath: 'Untitled.md'
, filters: [
{ name: 'Markdown', extensions: ['md', 'txt', 'markdown'] }
]
});
if (filePath === undefined) {
var filePathPromise = filePath === undefined || opts.saveAsNewFile
? remote.dialog.showSaveDialog(remote.getCurrentWindow(), {
defaultPath: 'Untitled.md'
, filters: [
{ name: 'Markdown', extensions: ['md', 'txt', 'markdown'] }
]
})
: filePathPromise = Promise.resolve({filePath: filePath});

filePathPromise.then(function(res) {
filePath = res.filePath
if (!filePath) {
return;
}
}
fs.writeFile(filePath, Document.getMd(), function(err){
if (err) {
alert("Could not save file.\n" + err.message);
} else {
var win = remote.getCurrentWindow()
, name = filename(filePath)
;
Document.setPath(filePath);
win.setTitle(name);
win.setRepresentedFilename(filePath);
win.fileIsDirty = false;
onFileSaveCb(name)();
if (opts.closeWindowAfterSave) {
win.close();
fs.writeFile(filePath, Document.getMd(), function(err){
if (err) {
alert("Could not save file.\n" + err.message);
} else {
var win = remote.getCurrentWindow()
, name = filename(filePath)
;
Document.setPath(filePath);
win.setTitle(name);
win.setRepresentedFilename(filePath);
win.fileIsDirty = false;
onFileSaveCb(name)();
if (opts.closeWindowAfterSave) {
win.close();
}
}
}
});
});
})
});

function filename(filePath) {
Expand Down
24 changes: 13 additions & 11 deletions src/js/Exporter.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,22 @@ ipcRenderer.on('fileExport', function() {
defaultPath = path.basename(inputPath, path.extname(inputPath))
}

const outputPath = remote.dialog.showSaveDialog(win, {
remote.dialog.showSaveDialog(win, {
defaultPath: defaultPath
, buttonLabel: 'Export'
, filters: exportFormats()
});
if (outputPath !== undefined){
let exp = {
outputPath: outputPath
, spawnOpts: spawnOpts
};
fileExport(exp).then( () => {
previousExportConfig = exp;
});
}
}).then(res => {
const outputPath = res.filePath
if (outputPath){
const exp = {
outputPath: outputPath
, spawnOpts: spawnOpts
};
fileExport(exp).then( () => {
previousExportConfig = exp;
});
}
})
});

ipcRenderer.on('fileExportLikePrevious', function() {
Expand Down
51 changes: 25 additions & 26 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,31 +50,30 @@ function createWindow(filePath, toImport=false, wasCreatedOnStartup=false) {
// Open the DevTools.
// win.webContents.openDevTools()

win.on('close', function(e) {
win.on('close', async function(e) {
// this does not intercept a reload
// see https://github.com/electron/electron/blob/master/docs/api/browser-window.md#event-close
// and https://github.com/electron/electron/issues/9966
if (win.fileIsDirty) {
const selected = dialog.showMessageBox(win, {
e.preventDefault();
const selected = await dialog.showMessageBox(win, {
type: "question"
, message: "This document has unsaved changes."
, buttons: ["Save", "Cancel", "Don't Save"]
})
switch (selected) {
switch (selected.response) {
case 0:
// Save
win.webContents.send('fileSave', {closeWindowAfterSave: true});
e.preventDefault();
break;
case 1:
// Cancel
e.preventDefault();
break;
case 2:
// Don't Save
win.fileIsDirty = false;
win.close()
break;
default:
e.preventDefault();
}
}
fetchRecentFiles(); // call to localStorage while we still have a window
Expand Down Expand Up @@ -150,17 +149,17 @@ app.on('activate', function() {
}
})

function openDialog(toImport=false) {
async function openDialog(toImport=false) {
const formats = toImport ? [] : [
{ name: 'Markdown', extensions: mdExtensions }
]
, fileNames = dialog.showOpenDialog({
, fileNames = await dialog.showOpenDialog({
filters: formats
, buttonLabel: toImport ? 'Import' : undefined
})
;
if (fileNames !== undefined && fileNames.length > 0) {
createWindow( fileNames[0], toImport);
if (fileNames && fileNames.filePaths.length > 0) {
createWindow(fileNames.filePaths[0], toImport);
}
}

Expand Down Expand Up @@ -202,27 +201,27 @@ function setMenuQuick(aWindowIsOpen=true) {
, {type: 'separator'}
, { label: 'Save'
, accelerator: 'CmdOrCtrl+S'
, click: windowSend.bind(this, 'fileSave')
, click: () => windowSend('fileSave')
, enabled: aWindowIsOpen
}
, { label: 'Save As…'
, accelerator: 'CmdOrCtrl+Shift+S'
, click: windowSend.bind(this, 'fileSave', {saveAsNewFile: true})
, click: () => windowSend('fileSave', {saveAsNewFile: true})
, enabled: aWindowIsOpen
}
, { label: 'Print / PDF'
, accelerator: 'CmdOrCtrl+P'
, click: windowSend.bind(this, 'filePrint')
, click: () => windowSend('filePrint')
, enabled: aWindowIsOpen
}
, { label: 'Export…'
, accelerator: 'CmdOrCtrl+Shift+E'
, click: windowSend.bind(this, 'fileExport')
, click: () => windowSend('fileExport')
, enabled: aWindowIsOpen
}
, { label: 'Export like previous'
, accelerator: 'CmdOrCtrl+E'
, click: windowSend.bind(this, 'fileExportLikePrevious')
, click: () => windowSend('fileExportLikePrevious')
, enabled: aWindowIsOpen
}
, { label: 'Import…'
Expand All @@ -244,17 +243,17 @@ function setMenuQuick(aWindowIsOpen=true) {
, {type: 'separator'}
, { label: 'Find'
, accelerator: 'CmdOrCtrl+F'
, click: windowSend.bind(this, 'find')
, click: () => windowSend('find')
, enabled: aWindowIsOpen
}
, { label: 'Find Next'
, accelerator: 'CmdOrCtrl+G'
, click: windowSend.bind(this, 'findNext')
, click: () => windowSend('findNext')
, enabled: aWindowIsOpen
}
, { label: 'Find Previous'
, accelerator: 'CmdOrCtrl+Shift+G'
, click: windowSend.bind(this, 'findPrevious')
, click: () => windowSend('findPrevious')
, enabled: aWindowIsOpen
}
]
Expand All @@ -263,21 +262,21 @@ function setMenuQuick(aWindowIsOpen=true) {
, submenu: [
{ label: 'Bold'
, accelerator: 'CmdOrCtrl+B'
, click: windowSend.bind(this, 'addBold')
, click: () => windowSend('addBold')
, enabled: aWindowIsOpen
}
, { label: 'Italic'
, accelerator: 'CmdOrCtrl+I'
, click: windowSend.bind(this, 'addItalic')
, click: () => windowSend('addItalic')
, enabled: aWindowIsOpen
}
, { label: 'Strikethrough'
, click: windowSend.bind(this, 'addStrikethrough')
, click: () => windowSend('addStrikethrough')
, enabled: aWindowIsOpen
}
, {type: 'separator'}
, { label: 'Add CSS style'
, click: windowSend.bind(this, 'addMetadataStyle')
, click: () => windowSend('addMetadataStyle')
, enabled: aWindowIsOpen
}
]
Expand All @@ -286,17 +285,17 @@ function setMenuQuick(aWindowIsOpen=true) {
, submenu: [
{ label: 'Show Only Editor'
, accelerator: 'CmdOrCtrl+1'
, click: windowSend.bind(this, 'splitViewOnlyEditor')
, click: () => windowSend('splitViewOnlyEditor')
, enabled: aWindowIsOpen
}
, { label: 'Show Split View'
, accelerator: 'CmdOrCtrl+2'
, click: windowSend.bind(this, 'splitViewSplit')
, click: () => windowSend('splitViewSplit')
, enabled: aWindowIsOpen
}
, { label: 'Show Only Preview'
, accelerator: 'CmdOrCtrl+3'
, click: windowSend.bind(this, 'splitViewOnlyPreview')
, click: () => windowSend('splitViewOnlyPreview')
, enabled: aWindowIsOpen
}
, {type: 'separator'}
Expand Down
Loading

0 comments on commit 86ce8c0

Please sign in to comment.