insomnia/packages/insomnia-app/app/plugins/context/app.js
Gregory Schier aba3c8ed86
Prompt Template Tag and Plugin arg validation (#673)
* Plugin arg validation, prompt tag, and some changes needed

* Version bumps
2017-12-21 06:01:51 -08:00

62 lines
1.8 KiB
JavaScript

// @flow
import * as electron from 'electron';
import {showAlert} from '../../ui/components/modals/index';
import {showPrompt} from '../../ui/components/modals';
import {RENDER_PURPOSE_SEND} from '../../common/render';
export function init (renderPurpose?: string): {app: Object} {
return {
app: {
alert (options: {title: string, message: string}): Promise<void> {
if (renderPurpose !== RENDER_PURPOSE_SEND) {
return Promise.resolve();
}
return showAlert(options || {});
},
prompt (options: {title: string, label?: string, defaultValue?: string, submitName?: string}): Promise<string> {
options = options || {};
if (renderPurpose !== RENDER_PURPOSE_SEND) {
return Promise.resolve(options.defaultValue || '');
}
return new Promise(resolve => {
showPrompt({
...(options || {}),
cancelable: false,
onComplete (value: string) {
resolve(value);
}
});
});
},
getPath (name: string): string {
switch (name.toLowerCase()) {
case 'desktop':
return electron.remote.app.getPath('desktop');
default:
throw new Error(`Unknown path name ${name}`);
}
},
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);
});
});
}
}
};
}