mirror of
https://github.com/Kong/insomnia
synced 2024-11-12 17:26:32 +00:00
68b9221b80
* New dialog() method to show generic dialogs from Plugins * Clarified deprecated api method * Body now required in dialog() * Made options arg optional too * Fix tests
116 lines
3.3 KiB
JavaScript
116 lines
3.3 KiB
JavaScript
// @flow
|
|
import * as React from 'react';
|
|
import * as electron from 'electron';
|
|
import { showAlert, showModal, showPrompt } from '../../ui/components/modals';
|
|
import type { RenderPurpose } from '../../common/render';
|
|
import {
|
|
RENDER_PURPOSE_GENERAL,
|
|
RENDER_PURPOSE_NO_RENDER,
|
|
RENDER_PURPOSE_SEND,
|
|
} from '../../common/render';
|
|
import WrapperModal from '../../ui/components/modals/wrapper-modal';
|
|
import HtmlElementWrapper from '../../ui/components/html-element-wrapper';
|
|
|
|
export function init(renderPurpose: RenderPurpose = RENDER_PURPOSE_GENERAL): { app: Object } {
|
|
const canShowDialogs =
|
|
renderPurpose === RENDER_PURPOSE_SEND || renderPurpose === RENDER_PURPOSE_NO_RENDER;
|
|
|
|
return {
|
|
app: {
|
|
alert(title: string, message?: string): Promise<void> {
|
|
if (!canShowDialogs) {
|
|
return Promise.resolve();
|
|
}
|
|
|
|
return showAlert({ title, message });
|
|
},
|
|
dialog(
|
|
title,
|
|
body: HTMLElement,
|
|
options?: {
|
|
onHide?: () => void,
|
|
tall?: boolean,
|
|
} = {},
|
|
): void {
|
|
if (renderPurpose !== RENDER_PURPOSE_SEND && renderPurpose !== RENDER_PURPOSE_NO_RENDER) {
|
|
return;
|
|
}
|
|
|
|
showModal(WrapperModal, {
|
|
title,
|
|
body: <HtmlElementWrapper el={body} onUnmount={options.onHide} />,
|
|
tall: options.tall,
|
|
});
|
|
},
|
|
prompt(
|
|
title: string,
|
|
options?: {
|
|
label?: string,
|
|
defaultValue?: string,
|
|
submitName?: string,
|
|
cancelable?: boolean,
|
|
},
|
|
): Promise<string> {
|
|
options = options || {};
|
|
|
|
if (!canShowDialogs) {
|
|
return Promise.resolve(options.defaultValue || '');
|
|
}
|
|
|
|
return new Promise((resolve, reject) => {
|
|
showPrompt({
|
|
title,
|
|
...(options || {}),
|
|
onCancel() {
|
|
reject(new Error(`Prompt ${title} cancelled`));
|
|
},
|
|
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 (!canShowDialogs) {
|
|
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);
|
|
});
|
|
});
|
|
},
|
|
|
|
// ~~~~~~~~~~~~~~~~~~ //
|
|
// Deprecated Methods //
|
|
// ~~~~~~~~~~~~~~~~~~ //
|
|
|
|
/** @deprecated as it was never officially supported */
|
|
showGenericModalDialog(title: string, options?: { html: string } = {}): void {
|
|
console.warn('app.showGenericModalDialog() is deprecated. Use app.dialog() instead.');
|
|
|
|
// Create DOM node so we can adapt to the new dialog() method
|
|
const body = document.createElement('div');
|
|
body.innerHTML = options.html;
|
|
|
|
return this.dialog(title, body);
|
|
},
|
|
},
|
|
};
|
|
}
|