This commit is contained in:
Araxeus
2021-04-05 23:18:54 +03:00
parent a215035d07
commit 724c213af1
8 changed files with 89 additions and 82 deletions

View File

@ -1,6 +1,6 @@
const fs = require('fs');
const {ipcRenderer} = require('electron');
const docReady = require('doc-ready');
const fs = require("fs");
const {ipcRenderer} = require("electron");
const docReady = require("doc-ready");
let promptId = null;
let promptOptions = null;
@ -10,41 +10,39 @@ function promptError(error) {
error = error.message;
}
ipcRenderer.sendSync('prompt-error:' + promptId, error);
ipcRenderer.sendSync("prompt-error:" + promptId, error);
}
function promptCancel() {
ipcRenderer.sendSync('prompt-post-data:' + promptId, null);
ipcRenderer.sendSync("prompt-post-data:" + promptId, null);
}
function promptSubmit() {
const dataElement = document.querySelector('#data');
const dataElement = document.querySelector("#data");
let data = null;
if (promptOptions.type === 'input') {
if (promptOptions.type === "input") {
data = dataElement.value;
} else if (promptOptions.type === 'select') {
if (promptOptions.selectMultiple) {
data = dataElement.querySelectorAll('option[selected]').map(o => o.getAttribute('value'));
} else {
data = dataElement.value;
}
} else if (promptOptions.type === "select") {
data = promptOptions.selectMultiple ?
dataElement.querySelectorAll("option[selected]").map(o => o.getAttribute("value")) :
dataElement.value;
}
ipcRenderer.sendSync('prompt-post-data:' + promptId, data);
ipcRenderer.sendSync("prompt-post-data:" + promptId, data);
}
function promptCreateInput() {
const dataElement = document.createElement('input');
dataElement.setAttribute('type', 'text');
const dataElement = document.createElement("input");
dataElement.setAttribute("type", "text");
if (promptOptions.value) {
dataElement.value = promptOptions.value;
} else {
dataElement.value = '';
dataElement.value = "";
}
if (promptOptions.inputAttrs && typeof (promptOptions.inputAttrs) === 'object') {
if (promptOptions.inputAttrs && typeof (promptOptions.inputAttrs) === "object") {
for (const k in promptOptions.inputAttrs) {
if (!Object.prototype.hasOwnProperty.call(promptOptions.inputAttrs, k)) {
continue;
@ -54,16 +52,16 @@ function promptCreateInput() {
}
}
dataElement.addEventListener('keyup', event => {
if (event.key === 'Escape') {
dataElement.addEventListener("keyup", event => {
if (event.key === "Escape") {
promptCancel();
}
});
dataElement.addEventListener('keypress', event => {
if (event.key === 'Enter') {
dataElement.addEventListener("keypress", event => {
if (event.key === "Enter") {
event.preventDefault();
document.querySelector('#ok').click();
document.querySelector("#ok").click();
}
});
@ -71,7 +69,7 @@ function promptCreateInput() {
}
function promptCreateSelect() {
const dataElement = document.createElement('select');
const dataElement = document.createElement("select");
let optionElement;
for (const k in promptOptions.selectOptions) {
@ -79,11 +77,11 @@ function promptCreateSelect() {
continue;
}
optionElement = document.createElement('option');
optionElement.setAttribute('value', k);
optionElement = document.createElement("option");
optionElement.setAttribute("value", k);
optionElement.textContent = promptOptions.selectOptions[k];
if (k === promptOptions.value) {
optionElement.setAttribute('selected', 'selected');
optionElement.setAttribute("selected", "selected");
}
dataElement.append(optionElement);
@ -93,34 +91,34 @@ function promptCreateSelect() {
}
function promptRegister() {
promptId = document.location.hash.replace('#', '');
promptId = document.location.hash.replace("#", "");
try {
promptOptions = JSON.parse(ipcRenderer.sendSync('prompt-get-options:' + promptId));
promptOptions = JSON.parse(ipcRenderer.sendSync("prompt-get-options:" + promptId));
} catch (error) {
return promptError(error);
}
if (promptOptions.useHtmlLabel) {
document.querySelector('#label').innerHTML = promptOptions.label;
document.querySelector("#label").innerHTML = promptOptions.label;
} else {
document.querySelector('#label').textContent = promptOptions.label;
document.querySelector("#label").textContent = promptOptions.label;
}
if (promptOptions.buttonLabels && promptOptions.buttonLabels.ok) {
document.querySelector('#ok').textContent = promptOptions.buttonLabels.ok;
document.querySelector("#ok").textContent = promptOptions.buttonLabels.ok;
}
if (promptOptions.buttonLabels && promptOptions.buttonLabels.cancel) {
document.querySelector('#cancel').textContent = promptOptions.buttonLabels.cancel;
document.querySelector("#cancel").textContent = promptOptions.buttonLabels.cancel;
}
if (promptOptions.customStylesheet) {
try {
const customStyleContent = fs.readFileSync(promptOptions.customStylesheet);
if (customStyleContent) {
const customStyle = document.createElement('style');
customStyle.setAttribute('rel', 'stylesheet');
const customStyle = document.createElement("style");
customStyle.setAttribute("rel", "stylesheet");
customStyle.append(document.createTextNode(customStyleContent));
document.head.append(customStyle);
}
@ -129,25 +127,25 @@ function promptRegister() {
}
}
document.querySelector('#form').addEventListener('submit', promptSubmit);
document.querySelector('#cancel').addEventListener('click', promptCancel);
document.querySelector("#form").addEventListener("submit", promptSubmit);
document.querySelector("#cancel").addEventListener("click", promptCancel);
const dataContainerElement = document.querySelector('#data-container');
const dataContainerElement = document.querySelector("#data-container");
let dataElement;
if (promptOptions.type === 'input') {
if (promptOptions.type === "input") {
dataElement = promptCreateInput();
} else if (promptOptions.type === 'select') {
} else if (promptOptions.type === "select") {
dataElement = promptCreateSelect();
} else {
return promptError(`Unhandled input type '${promptOptions.type}'`);
}
dataContainerElement.append(dataElement);
dataElement.setAttribute('id', 'data');
dataElement.setAttribute("id", "data");
dataElement.focus();
if (promptOptions.type === 'input') {
if (promptOptions.type === "input") {
dataElement.select();
}
@ -161,10 +159,10 @@ function promptRegister() {
}
}
window.addEventListener('error', error => {
window.addEventListener("error", error => {
if (promptId) {
promptError('An error has occured on the prompt window: \n' + error);
promptError("An error has occured on the prompt window: \n" + error);
}
});
docReady(promptRegister);
docReady(promptRegister);