Skip to content

Commit 814a0cd

Browse files
committed
fix: fixing dialog handling after electron upgrade
1 parent d006cb4 commit 814a0cd

File tree

2 files changed

+40
-53
lines changed

2 files changed

+40
-53
lines changed

scripts/main/app-prompts.js

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,42 +9,37 @@ class AppPrompts {
99
* Listens for the application events.
1010
*/
1111
listen() {
12-
ipcMain.on('save-dialog', this._saveDialogHandler.bind(this));
12+
ipcMain.handle('save-dialog', this._saveDialogHandler.bind(this));
1313
}
1414
/**
1515
* Hasnles save action dialog. Opens "save as..." dialog and returns
1616
* the result to the app.
1717
*
18-
* This method emmits `saved-file` event to the sender window with
19-
* file location as it's only argument. The argument is `undefined`
20-
* when the user cancels the action.
21-
*
2218
* @param {Event} e Event emitted by the BrowserWindow
2319
* @param {Object} args Prompt dialog options:
2420
* - file {String} - File name to suggest to the user.
21+
* @return {Promise}
2522
*/
26-
_saveDialogHandler(e, args) {
23+
_saveDialogHandler(e, args={}) {
2724
log.debug('Save dialog requested', args);
28-
args = args || {};
2925
const options = {
3026
title: 'Save to file'
3127
};
3228
if (args.file) {
3329
options.defaultPath = args.file;
3430
}
35-
dialog.showSaveDialog(options, function(filename) {
36-
e.sender.send('saved-file', filename);
37-
});
31+
return dialog.showSaveDialog(options);
3832
}
3933
/**
4034
* A dialog that renders error message about missing workspace file.
4135
* @param {String} file Requested workspace file location.
36+
* @return {Promise}
4237
*/
43-
static workspaceMissing(file) {
38+
static async workspaceMissing(file) {
4439
let message = 'Workspace file cannot be located. Probably it was deleted or';
4540
message += ' renamed.\n\n';
4641
message += 'Requested file: ' + file;
47-
dialog.showMessageBox({
42+
return await dialog.showMessageBox({
4843
type: 'error',
4944
message
5045
});

scripts/renderer/filesystem-proxy.js

Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -15,72 +15,64 @@ const path = require('path');
1515
class FilesystemProxy {
1616
constructor() {
1717
this._exportHandler = this._exportHandler.bind(this);
18-
this._fileSavedHandler = this._fileSavedHandler.bind(this);
1918
}
2019

2120
listen() {
2221
window.addEventListener('file-data-save', this._exportHandler);
23-
ipc.on('saved-file', this._fileSavedHandler);
2422
}
2523

2624
unlisten() {
2725
window.removeEventListener('file-data-save', this._exportHandler);
28-
ipc.removeListener('saved-file', this._fileSavedHandler);
29-
}
30-
31-
_clear() {
32-
this.lastType = undefined;
33-
this.lastContent = undefined;
34-
this.lastResolve = undefined;
35-
this.lastReject = undefined;
3626
}
3727

3828
_exportHandler(e) {
3929
if (e.defaultPrevented) {
4030
return;
4131
}
4232
e.preventDefault();
43-
const { content, file, options } = e.detail;
44-
e.detail.result = this.exportFileData(content, options && options.contentType, file);
33+
const { content, file, options={} } = e.detail;
34+
e.detail.result = this.exportFileData(content, options.contentType, file);
4535
}
46-
47-
exportFileData(content, mime, file) {
48-
ipc.send('save-dialog', {
36+
/**
37+
* Requests a save file dialog and saves the data to selected path if not cancelled.
38+
* This does nothing when dialog is canceled.
39+
*
40+
* @param {String|Object|Buffer} content Data to write
41+
* @param {String=} mime Content media type. Currently only `application/json` is
42+
* supported when the `content` is an object or an array.
43+
* @param {String=} file Suggested file name
44+
* @return {Promise}
45+
*/
46+
async exportFileData(content, mime, file) {
47+
const opts = {
4948
file
50-
});
51-
this.lastContent = content;
52-
this.lastType = mime;
53-
new Promise((resolve, reject) => {
54-
this.lastResolve = resolve;
55-
this.lastReject = reject;
56-
});
57-
}
58-
59-
_fileSavedHandler(e, selectedPath) {
60-
if (!selectedPath) {
61-
this.lastResolve();
62-
this._clear();
49+
};
50+
const result = await ipc.invoke('save-dialog', opts)
51+
const { filePath, canceled } = result;
52+
if (canceled) {
6353
return;
6454
}
65-
return this._writeContent(selectedPath)
66-
.then(() => this.lastResolve())
67-
.catch((cause) => this.lastReject(cause))
68-
.then(() => this._clear());
55+
await this._writeContent(filePath, content, mime);
6956
}
70-
71-
_writeContent(path) {
72-
let data = this.lastContent;
73-
if (typeof data !== 'string') {
74-
data = this._prepareData(data);
57+
/**
58+
* Writes content to a file
59+
* @param {String} path Absolute path to a file
60+
* @param {String|Object|Buffer} content Data to be written
61+
* @param {String=} mime Content media type.
62+
* @return {Promise}
63+
*/
64+
_writeContent(path, content, mime) {
65+
if (typeof content !== 'string') {
66+
content = this._prepareData(content, mime);
7567
}
76-
return fs.writeFile(path, data, 'utf8');
68+
return fs.writeFile(path, content, 'utf8');
7769
}
7870

79-
_prepareData(data) {
80-
switch (this.lastType) {
71+
_prepareData(data, mime) {
72+
switch (mime) {
8173
case 'application/json': return JSON.stringify(data);
74+
default: return String(data);
8275
}
83-
return String(data);
8476
}
8577
/**
8678
* Allows to read file from user filesystem.

0 commit comments

Comments
 (0)