Create readme.md

refactor and css fix

xo --fix

add inline doc

fix typo
This commit is contained in:
Araxeus
2021-04-06 21:57:16 +03:00
parent 6d44a579a4
commit 980ffb45e9
11 changed files with 306 additions and 183 deletions

View File

@ -1,17 +1,19 @@
const electron = require('electron');
const electron = require("electron");
const BrowserWindow = electron.BrowserWindow || electron.remote.BrowserWindow;
const ipcMain = electron.ipcMain || electron.remote.ipcMain;
const url = require('url');
const path = require('path');
const url = require("url");
const path = require("path");
const DEFAULT_WIDTH = 370;
const DEFAULT_HEIGHT = 160;
function electronPrompt(options, parentWindow) {
return new Promise((resolve, reject) => {
//id used to ensure unique listeners per window
const id = `${Date.now()}-${Math.random()}`;
//custom options override default
const options_ = Object.assign(
{
width: DEFAULT_WIDTH,
@ -19,12 +21,12 @@ function electronPrompt(options, parentWindow) {
minWidth: DEFAULT_WIDTH,
minHeight: DEFAULT_HEIGHT,
resizable: false,
title: 'Prompt',
label: 'Please input a value:',
title: "Prompt",
label: "Please input a value:",
buttonLabels: null,
alwaysOnTop: false,
value: null,
type: 'input',
type: "input",
selectOptions: null,
icon: null,
useHtmlLabel: false,
@ -38,7 +40,7 @@ function electronPrompt(options, parentWindow) {
options || {}
);
if (options_.type === 'select' && (options_.selectOptions === null || typeof options_.selectOptions !== 'object')) {
if (options_.type === "select" && (options_.selectOptions === null || typeof options_.selectOptions !== "object")) {
reject(new Error('"selectOptions" must be an object'));
return;
}
@ -50,9 +52,9 @@ function electronPrompt(options, parentWindow) {
minWidth: options_.minWidth,
minHeight: options_.minHeight,
resizable: options_.resizable,
minimizable: false,
fullscreenable: false,
maximizable: false,
minimizable: !options_.skipTaskbar && !parentWindow && !options_.alwaysOnTop,
fullscreenable: options_.resizable,
maximizable: options_.resizable,
parent: parentWindow,
skipTaskbar: options_.skipTaskbar,
alwaysOnTop: options_.alwaysOnTop,
@ -70,14 +72,11 @@ function electronPrompt(options, parentWindow) {
promptWindow.setMenu(null);
promptWindow.setMenuBarVisibility(options_.menuBarVisible);
const getOptionsListener = event => {
event.returnValue = JSON.stringify(options_);
};
//called on exit
const cleanup = () => {
ipcMain.removeListener('prompt-get-options:' + id, getOptionsListener);
ipcMain.removeListener('prompt-post-data:' + id, postDataListener);
ipcMain.removeListener('prompt-error:' + id, errorListener);
ipcMain.removeListener("prompt-get-options:" + id, getOptionsListener);
ipcMain.removeListener("prompt-post-data:" + id, postDataListener);
ipcMain.removeListener("prompt-error:" + id, errorListener);
if (promptWindow) {
promptWindow.close();
@ -85,6 +84,12 @@ function electronPrompt(options, parentWindow) {
}
};
///transfer options to front
const getOptionsListener = event => {
event.returnValue = JSON.stringify(options_);
};
//get input from front
const postDataListener = (event, value) => {
resolve(value);
event.returnValue = null;
@ -92,34 +97,46 @@ function electronPrompt(options, parentWindow) {
};
const unresponsiveListener = () => {
reject(new Error('Window was unresponsive'));
reject(new Error("Window was unresponsive"));
cleanup();
};
//get error from front
const errorListener = (event, message) => {
reject(new Error(message));
event.returnValue = null;
cleanup();
};
ipcMain.on('prompt-get-options:' + id, getOptionsListener);
ipcMain.on('prompt-post-data:' + id, postDataListener);
ipcMain.on('prompt-error:' + id, errorListener);
promptWindow.on('unresponsive', unresponsiveListener);
//attach listeners
ipcMain.on("prompt-get-options:" + id, getOptionsListener);
ipcMain.on("prompt-post-data:" + id, postDataListener);
ipcMain.on("prompt-error:" + id, errorListener);
promptWindow.on("unresponsive", unresponsiveListener);
promptWindow.on('closed', () => {
promptWindow.on("closed", () => {
promptWindow = null;
cleanup();
resolve(null);
});
const promptUrl = url.format({
protocol: 'file',
slashes: true,
pathname: path.join(__dirname, 'page', 'prompt.html'),
hash: id
//should never happen
promptWindow.webContents.on("did-fail-load", (
event,
errorCode,
errorDescription,
validatedURL,
) => {
const log = {
error: "did-fail-load",
errorCode,
errorDescription,
validatedURL,
};
reject(new Error("prompt.html did-fail-load, log:\n", + log.toString()));
});
//Finally, load prompt
promptWindow.loadURL(promptUrl);
});
}