mirror of
https://github.com/th-ch/youtube-music.git
synced 2026-01-16 04:41:47 +00:00
add rapidFire option to counter prompt
This commit is contained in:
@ -18,8 +18,8 @@ function electronPrompt(options, parentWindow) {
|
|||||||
//custom options override default
|
//custom options override default
|
||||||
const options_ = Object.assign(
|
const options_ = Object.assign(
|
||||||
{
|
{
|
||||||
width: DEFAULT_WIDTH,
|
width: options?.type === "counter" ? DEFAULT_COUNTER_WIDTH : DEFAULT_WIDTH,
|
||||||
height: DEFAULT_HEIGHT,
|
height:options?.type === "counter" ? DEFAULT_COUNTER_HEIGHT: DEFAULT_HEIGHT,
|
||||||
resizable: false,
|
resizable: false,
|
||||||
title: "Prompt",
|
title: "Prompt",
|
||||||
label: "Please input a value:",
|
label: "Please input a value:",
|
||||||
@ -28,6 +28,7 @@ function electronPrompt(options, parentWindow) {
|
|||||||
value: null,
|
value: null,
|
||||||
type: "input",
|
type: "input",
|
||||||
selectOptions: null,
|
selectOptions: null,
|
||||||
|
counterOptions: {minimum: null, maximum: null, multiFire: false},
|
||||||
icon: null,
|
icon: null,
|
||||||
useHtmlLabel: false,
|
useHtmlLabel: false,
|
||||||
customStylesheet: null,
|
customStylesheet: null,
|
||||||
@ -40,10 +41,13 @@ function electronPrompt(options, parentWindow) {
|
|||||||
options || {}
|
options || {}
|
||||||
);
|
);
|
||||||
|
|
||||||
options_.minWidth = options.minWidth || options.width ||
|
options_.minWidth = options?.minWidth || options?.width || options_.width;
|
||||||
options_.type === "counter" ? DEFAULT_COUNTER_WIDTH : DEFAULT_WIDTH;
|
options_.minHeight = options?.minHeight || options?.height || options_.height;
|
||||||
options_.minHeight = options.minHeight || options.height ||
|
|
||||||
options_.type === "counter" ? DEFAULT_COUNTER_HEIGHT : DEFAULT_HEIGHT;
|
if (options_.type === "counter" && (options_.counterOptions !== null && typeof options_.selectOptions !== "object")) {
|
||||||
|
reject(new Error('"counterOptions" must be an object if specified'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
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'));
|
reject(new Error('"selectOptions" must be an object'));
|
||||||
|
|||||||
@ -62,6 +62,8 @@ function promptRegister() {
|
|||||||
|
|
||||||
switch (promptOptions.type) {
|
switch (promptOptions.type) {
|
||||||
case "counter":
|
case "counter":
|
||||||
|
dataElement = promptCreateCounter();
|
||||||
|
break;
|
||||||
case "input":
|
case "input":
|
||||||
dataElement = promptCreateInput();
|
dataElement = promptCreateInput();
|
||||||
break;
|
break;
|
||||||
@ -73,8 +75,6 @@ function promptRegister() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (promptOptions.type === "counter") {
|
if (promptOptions.type === "counter") {
|
||||||
dataElement.style.width = "unset";
|
|
||||||
dataElement.style["text-align"] = "center";
|
|
||||||
dataContainerElement.append(createMinusButton(dataElement));
|
dataContainerElement.append(createMinusButton(dataElement));
|
||||||
dataContainerElement.append(dataElement);
|
dataContainerElement.append(dataElement);
|
||||||
dataContainerElement.append(createPlusButton(dataElement));
|
dataContainerElement.append(createPlusButton(dataElement));
|
||||||
@ -212,27 +212,74 @@ function promptCreateSelect() {
|
|||||||
return dataElement;
|
return dataElement;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let pressed = false;
|
||||||
|
function multiFire(timer, scaleSpeed, callback, ...args) {
|
||||||
|
if (!pressed) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (timer > scaleSpeed) {
|
||||||
|
timer -= scaleSpeed;
|
||||||
|
}
|
||||||
|
callback(...args);
|
||||||
|
setTimeout(multiFire, timer, timer, scaleSpeed, callback, ...args)
|
||||||
|
}
|
||||||
|
|
||||||
function createMinusButton(dataElement) {
|
function createMinusButton(dataElement) {
|
||||||
|
function doMinus() {
|
||||||
|
dataElement.value = validateCounterInput(parseInt(dataElement.value) - 1);
|
||||||
|
}
|
||||||
const minusBtn = document.createElement("span");
|
const minusBtn = document.createElement("span");
|
||||||
minusBtn.textContent = "-";
|
minusBtn.textContent = "-";
|
||||||
minusBtn.classList.add("minus");
|
minusBtn.classList.add("minus");
|
||||||
minusBtn.onmousedown = () => {
|
if (promptOptions.counterOptions?.multiFire) {
|
||||||
dataElement.value = validateCounterInput(parseInt(dataElement.value) - 1);
|
minusBtn.onmousedown = () => {
|
||||||
};
|
pressed = true;
|
||||||
|
multiFire(500, 100, doMinus);
|
||||||
|
};
|
||||||
|
|
||||||
|
} else {
|
||||||
|
minusBtn.onmousedown = () => {
|
||||||
|
doMinus();
|
||||||
|
};
|
||||||
|
}
|
||||||
return minusBtn;
|
return minusBtn;
|
||||||
}
|
}
|
||||||
|
|
||||||
function createPlusButton(dataElement) {
|
function createPlusButton(dataElement) {
|
||||||
|
function doPlus() {
|
||||||
|
dataElement.value = validateCounterInput(parseInt(dataElement.value) + 1);
|
||||||
|
}
|
||||||
const plusBtn = document.createElement("span");
|
const plusBtn = document.createElement("span");
|
||||||
plusBtn.textContent = "+";
|
plusBtn.textContent = "+";
|
||||||
plusBtn.classList.add("plus");
|
plusBtn.classList.add("plus");
|
||||||
plusBtn.onmousedown = () => {
|
if (promptOptions.counterOptions?.multiFire) {
|
||||||
dataElement.value = validateCounterInput(parseInt(dataElement.value) + 1);
|
plusBtn.onmousedown = () => {
|
||||||
};
|
pressed = true;
|
||||||
|
multiFire(500, 100, doPlus);
|
||||||
|
};
|
||||||
|
} else {
|
||||||
|
plusBtn.onmousedown = () => {
|
||||||
|
doPlus();
|
||||||
|
};
|
||||||
|
}
|
||||||
return plusBtn;
|
return plusBtn;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function promptCreateCounter() {
|
||||||
|
if (promptOptions.counterOptions?.multiFire) {
|
||||||
|
document.onmouseup = () => {
|
||||||
|
pressed = false;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
const dataElement = promptCreateInput();
|
||||||
|
|
||||||
|
dataElement.style.width = "unset";
|
||||||
|
dataElement.style["text-align"] = "center";
|
||||||
|
|
||||||
|
return dataElement;
|
||||||
|
}
|
||||||
|
|
||||||
//validate counter
|
//validate counter
|
||||||
function validateCounterInput(input) {
|
function validateCounterInput(input) {
|
||||||
const min = promptOptions.counterOptions?.minimum;
|
const min = promptOptions.counterOptions?.minimum;
|
||||||
|
|||||||
@ -45,7 +45,7 @@ prompt({
|
|||||||
| value | (optional, string) The default value for the input field. Defaults to null. |
|
| value | (optional, string) The default value for the input field. Defaults to null. |
|
||||||
| type | (optional, string) The type of input field, either 'input' for a standard text input field or 'select' for a dropdown type input or 'counter' for a number counter with buttons. Defaults to 'input'. |
|
| type | (optional, string) The type of input field, either 'input' for a standard text input field or 'select' for a dropdown type input or 'counter' for a number counter with buttons. Defaults to 'input'. |
|
||||||
| inputAttrs | (optional, object) The attributes of the input field, analagous to the HTML attributes: `{type: 'text', required: true}` -> `<input type="text" required>`. Used if the type is 'input'
|
| inputAttrs | (optional, object) The attributes of the input field, analagous to the HTML attributes: `{type: 'text', required: true}` -> `<input type="text" required>`. Used if the type is 'input'
|
||||||
| counterOptions | (optional, object) minimum and maximum of counter in format: `{minimum: %int%, maximum: %int%} ` |
|
| counterOptions | (optional, object) minimum and maximum of counter, and if continuous input is enabled. format: `{minimum: %int%, maximum: %int%, multiFire: %boolean%`. min+max values defaults to null and multiFire defaults to false. |
|
||||||
| selectOptions | (optional, object) The items for the select dropdown if using the 'select' type in the format 'value': 'display text', where the value is what will be given to the then block and the display text is what the user will see. |
|
| selectOptions | (optional, object) The items for the select dropdown if using the 'select' type in the format 'value': 'display text', where the value is what will be given to the then block and the display text is what the user will see. |
|
||||||
| useHtmlLabel | (optional, boolean) Whether the label should be interpreted as HTML or not. Defaults to false. |
|
| useHtmlLabel | (optional, boolean) Whether the label should be interpreted as HTML or not. Defaults to false. |
|
||||||
| width | (optional, integer) The width of the prompt window. Defaults to 370. |
|
| width | (optional, integer) The width of the prompt window. Defaults to 370. |
|
||||||
|
|||||||
Reference in New Issue
Block a user