electron-prompt
Advanced tools
Comparing version 1.5.1 to 1.6.0
@@ -15,3 +15,3 @@ const electron = require('electron'); | ||
const opts = Object.assign( | ||
const options_ = Object.assign( | ||
{ | ||
@@ -33,3 +33,4 @@ width: DEFAULT_WIDTH, | ||
customStylesheet: null, | ||
menuBarVisible: false | ||
menuBarVisible: false, | ||
skipTaskbar: true | ||
}, | ||
@@ -39,3 +40,3 @@ options || {} | ||
if (opts.type === 'select' && (opts.selectOptions === null || typeof opts.selectOptions !== 'object')) { | ||
if (options_.type === 'select' && (options_.selectOptions === null || typeof options_.selectOptions !== 'object')) { | ||
return reject(new Error('"selectOptions" must be an object')); | ||
@@ -45,14 +46,17 @@ } | ||
let promptWindow = new BrowserWindow({ | ||
width: opts.width, | ||
height: opts.height, | ||
minWidth: opts.minWidth, | ||
minHeight: opts.minHeight, | ||
resizable: opts.resizable, | ||
width: options_.width, | ||
height: options_.height, | ||
minWidth: options_.minWidth, | ||
minHeight: options_.minHeight, | ||
resizable: options_.resizable, | ||
minimizable: false, | ||
fullscreenable: false, | ||
maximizable: false, | ||
parent: parentWindow, | ||
skipTaskbar: true, | ||
alwaysOnTop: opts.alwaysOnTop, | ||
useContentSize: opts.resizable, | ||
skipTaskbar: options_.skipTaskbar, | ||
alwaysOnTop: options_.alwaysOnTop, | ||
useContentSize: options_.resizable, | ||
modal: Boolean(parentWindow), | ||
title: opts.title, | ||
icon: opts.icon, | ||
title: options_.title, | ||
icon: options_.icon || undefined, | ||
webPreferences: { | ||
@@ -64,6 +68,6 @@ nodeIntegration: true | ||
promptWindow.setMenu(null); | ||
promptWindow.setMenuBarVisibility(opts.menuBarVisible); | ||
promptWindow.setMenuBarVisibility(options_.menuBarVisible); | ||
const getOptionsListener = event => { | ||
event.returnValue = JSON.stringify(opts); | ||
event.returnValue = JSON.stringify(options_); | ||
}; | ||
@@ -70,0 +74,0 @@ |
@@ -8,25 +8,25 @@ const fs = require('fs'); | ||
const promptError = e => { | ||
if (e instanceof Error) { | ||
e = e.message; | ||
function promptError(error) { | ||
if (error instanceof Error) { | ||
error = error.message; | ||
} | ||
ipcRenderer.sendSync('prompt-error:' + promptId, e); | ||
}; | ||
ipcRenderer.sendSync('prompt-error:' + promptId, error); | ||
} | ||
const promptCancel = () => { | ||
function promptCancel() { | ||
ipcRenderer.sendSync('prompt-post-data:' + promptId, null); | ||
}; | ||
} | ||
const promptSubmit = () => { | ||
const dataEl = document.querySelector('#data'); | ||
function promptSubmit() { | ||
const dataElement = document.querySelector('#data'); | ||
let data = null; | ||
if (promptOptions.type === 'input') { | ||
data = dataEl.value; | ||
data = dataElement.value; | ||
} else if (promptOptions.type === 'select') { | ||
if (promptOptions.selectMultiple) { | ||
data = dataEl.querySelectorAll('option[selected]').map(o => o.getAttribute('value')); | ||
data = dataElement.querySelectorAll('option[selected]').map(o => o.getAttribute('value')); | ||
} else { | ||
data = dataEl.value; | ||
data = dataElement.value; | ||
} | ||
@@ -36,11 +36,63 @@ } | ||
ipcRenderer.sendSync('prompt-post-data:' + promptId, data); | ||
}; | ||
} | ||
window.addEventListener('error', error => { | ||
if (promptId) { | ||
promptError('An error has occured on the prompt window: \n' + error); | ||
function promptCreateInput() { | ||
const dataElement = document.createElement('input'); | ||
dataElement.setAttribute('type', 'text'); | ||
if (promptOptions.value) { | ||
dataElement.value = promptOptions.value; | ||
} else { | ||
dataElement.value = ''; | ||
} | ||
}); | ||
docReady(() => { | ||
if (promptOptions.inputAttrs && typeof (promptOptions.inputAttrs) === 'object') { | ||
for (const k in promptOptions.inputAttrs) { | ||
if (!Object.prototype.hasOwnProperty.call(promptOptions.inputAttrs, k)) { | ||
continue; | ||
} | ||
dataElement.setAttribute(k, promptOptions.inputAttrs[k]); | ||
} | ||
} | ||
dataElement.addEventListener('keyup', event => { | ||
if (event.key === 'Escape') { | ||
promptCancel(); | ||
} | ||
}); | ||
dataElement.addEventListener('keypress', event => { | ||
if (event.key === 'Enter') { | ||
event.preventDefault(); | ||
document.querySelector('#ok').click(); | ||
} | ||
}); | ||
return dataElement; | ||
} | ||
function promptCreateSelect() { | ||
const dataElement = document.createElement('select'); | ||
let optionElement; | ||
for (const k in promptOptions.selectOptions) { | ||
if (!Object.prototype.hasOwnProperty.call(promptOptions.selectOptions, k)) { | ||
continue; | ||
} | ||
optionElement = document.createElement('option'); | ||
optionElement.setAttribute('value', k); | ||
optionElement.textContent = promptOptions.selectOptions[k]; | ||
if (k === promptOptions.value) { | ||
optionElement.setAttribute('selected', 'selected'); | ||
} | ||
dataElement.append(optionElement); | ||
} | ||
return dataElement; | ||
} | ||
function promptRegister() { | ||
promptId = document.location.hash.replace('#', ''); | ||
@@ -85,64 +137,28 @@ | ||
const dataContainerEl = document.querySelector('#data-container'); | ||
const dataContainerElement = document.querySelector('#data-container'); | ||
let dataEl; | ||
let dataElement; | ||
if (promptOptions.type === 'input') { | ||
dataEl = document.createElement('input'); | ||
dataEl.setAttribute('type', 'text'); | ||
if (promptOptions.value) { | ||
dataEl.value = promptOptions.value; | ||
} else { | ||
dataEl.value = ''; | ||
} | ||
if (promptOptions.inputAttrs && typeof (promptOptions.inputAttrs) === 'object') { | ||
for (const k in promptOptions.inputAttrs) { | ||
if (!Object.prototype.hasOwnProperty.call(promptOptions.inputAttrs, k)) { | ||
continue; | ||
} | ||
dataEl.setAttribute(k, promptOptions.inputAttrs[k]); | ||
} | ||
} | ||
dataEl.addEventListener('keyup', e => { | ||
if (e.key === 'Escape') { | ||
promptCancel(); | ||
} | ||
}); | ||
dataEl.addEventListener('keypress', e => { | ||
if (e.key === 'Enter') { | ||
e.preventDefault(); | ||
promptSubmit(); | ||
} | ||
}); | ||
dataElement = promptCreateInput(); | ||
} else if (promptOptions.type === 'select') { | ||
dataEl = document.createElement('select'); | ||
let optionEl; | ||
for (const k in promptOptions.selectOptions) { | ||
if (!Object.prototype.hasOwnProperty.call(promptOptions.selectOptions, k)) { | ||
continue; | ||
} | ||
optionEl = document.createElement('option'); | ||
optionEl.setAttribute('value', k); | ||
optionEl.textContent = promptOptions.selectOptions[k]; | ||
if (k === promptOptions.value) { | ||
optionEl.setAttribute('selected', 'selected'); | ||
} | ||
dataEl.append(optionEl); | ||
} | ||
dataElement = promptCreateSelect(); | ||
} else { | ||
return promptError(`Unhandled input type '${promptOptions.type}'`); | ||
} | ||
dataContainerEl.append(dataEl); | ||
dataEl.setAttribute('id', 'data'); | ||
dataContainerElement.append(dataElement); | ||
dataElement.setAttribute('id', 'data'); | ||
dataEl.focus(); | ||
dataElement.focus(); | ||
if (promptOptions.type === 'input') { | ||
dataEl.select(); | ||
dataElement.select(); | ||
} | ||
} | ||
window.addEventListener('error', error => { | ||
if (promptId) { | ||
promptError('An error has occured on the prompt window: \n' + error); | ||
} | ||
}); | ||
docReady(promptRegister); |
{ | ||
"name": "electron-prompt", | ||
"version": "1.5.1", | ||
"version": "1.6.0", | ||
"description": "Electron helper to prompt for a value via input or select", | ||
@@ -37,4 +37,4 @@ "keywords": [ | ||
"devDependencies": { | ||
"xo": "^0.25.3" | ||
"xo": "^0.32.1" | ||
} | ||
} |
@@ -70,2 +70,3 @@ # electron-prompt | ||
| menuBarVisible | (optional, boolean) Whether to show the menubar or not. Defaults to false. | | ||
| skipTaskbar | (optional, boolean) Whether to show the prompt window icon in taskbar. Defaults to true. | | ||
@@ -72,0 +73,0 @@ If not supplied, it uses the defaults listed in the table above. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
14534
9
291
77