insomnia/packages/insomnia-app/app/plugins/context/app.js

80 lines
2.1 KiB
JavaScript
Raw Normal View History

// @flow
import * as electron from 'electron';
2018-06-25 17:42:50 +00:00
import { showAlert, showPrompt } from '../../ui/components/modals/index';
import type { RenderPurpose } from '../../common/render';
import {
RENDER_PURPOSE_GENERAL,
RENDER_PURPOSE_SEND
} from '../../common/render';
2018-06-25 17:42:50 +00:00
export function init(
renderPurpose: RenderPurpose = RENDER_PURPOSE_GENERAL
): { app: Object } {
return {
app: {
2018-06-25 17:42:50 +00:00
alert(title: string, message?: string): Promise<void> {
if (renderPurpose !== RENDER_PURPOSE_SEND) {
return Promise.resolve();
}
2018-06-25 17:42:50 +00:00
return showAlert({ title, message });
},
2018-06-25 17:42:50 +00:00
prompt(
2017-12-30 12:39:50 +00:00
title: string,
options?: {
2017-12-30 12:39:50 +00:00
label?: string,
defaultValue?: string,
submitName?: string,
cancelable?: boolean
2017-12-30 12:39:50 +00:00
}
): Promise<string> {
options = options || {};
if (renderPurpose !== RENDER_PURPOSE_SEND) {
return Promise.resolve(options.defaultValue || '');
}
return new Promise((resolve, reject) => {
showPrompt({
2017-12-30 12:39:50 +00:00
title,
...(options || {}),
2018-06-25 17:42:50 +00:00
onCancel() {
reject(new Error(`Prompt ${title} cancelled`));
},
2018-06-25 17:42:50 +00:00
onComplete(value: string) {
resolve(value);
}
});
});
},
2018-06-25 17:42:50 +00:00
getPath(name: string): string {
switch (name.toLowerCase()) {
case 'desktop':
return electron.remote.app.getPath('desktop');
default:
throw new Error(`Unknown path name ${name}`);
}
},
2018-06-25 17:42:50 +00:00
async showSaveDialog(
options: { defaultPath?: string } = {}
): Promise<string | null> {
if (renderPurpose !== RENDER_PURPOSE_SEND) {
return Promise.resolve(null);
}
return new Promise(resolve => {
const saveOptions = {
title: 'Save File',
buttonLabel: 'Save',
defaultPath: options.defaultPath
};
electron.remote.dialog.showSaveDialog(saveOptions, filename => {
resolve(filename || null);
});
});
}
}
};
}