Use get/set input value binding
This commit is contained in:
@@ -33,31 +33,23 @@
|
|||||||
/** @type {HTMLDialogElement} */
|
/** @type {HTMLDialogElement} */
|
||||||
let storyURLTutorialModal;
|
let storyURLTutorialModal;
|
||||||
|
|
||||||
/**
|
/** @param {string} input */
|
||||||
* @param {string} input
|
const setInputAsValid = (input) => {
|
||||||
* @param {HTMLInputElement} [inputElement]
|
|
||||||
*/
|
|
||||||
const setInputAsValid = (input, inputElement) => {
|
|
||||||
invalidUrl = false;
|
invalidUrl = false;
|
||||||
inputUrl = input;
|
inputUrl = input;
|
||||||
downloadId = input;
|
downloadId = input;
|
||||||
if (inputElement) inputElement.value = input;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/** @param {string} input */
|
||||||
* @param {string} input
|
const setInputAsInvalid = (input) => {
|
||||||
* @param {HTMLInputElement} inputElement
|
|
||||||
*/
|
|
||||||
const setInputAsInvalid = (input, inputElement) => {
|
|
||||||
invalidUrl = true;
|
invalidUrl = true;
|
||||||
inputUrl = input;
|
inputUrl = input;
|
||||||
downloadId = input;
|
downloadId = input;
|
||||||
inputElement.value = input;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/** @type {import("svelte/elements").FormEventHandler<HTMLInputElement>} */
|
/** @param {string} input */
|
||||||
const onInputUrl = (e) => {
|
const setInputUrl = (input) => {
|
||||||
let input = e.currentTarget.value.toLowerCase();
|
input = input.toLowerCase();
|
||||||
|
|
||||||
if (!input) {
|
if (!input) {
|
||||||
setInputAsValid("");
|
setInputAsValid("");
|
||||||
@@ -67,12 +59,12 @@
|
|||||||
if (/^\d+$/.test(input)) {
|
if (/^\d+$/.test(input)) {
|
||||||
// All numbers
|
// All numbers
|
||||||
mode = "story";
|
mode = "story";
|
||||||
setInputAsValid(input, e.currentTarget);
|
setInputAsValid(input);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!input.includes("wattpad.com/")) {
|
if (!input.includes("wattpad.com/")) {
|
||||||
setInputAsInvalid(input.match(/\d+/g)?.join("") ?? "", e.currentTarget);
|
setInputAsInvalid(input.match(/\d+/g)?.join("") ?? "");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,14 +75,12 @@
|
|||||||
mode = "story";
|
mode = "story";
|
||||||
setInputAsValid(
|
setInputAsValid(
|
||||||
input.split("-", 1)[0].split("?", 1)[0].split("/story/")[1], // removes tracking fields and title
|
input.split("-", 1)[0].split("?", 1)[0].split("/story/")[1], // removes tracking fields and title
|
||||||
e.currentTarget,
|
|
||||||
);
|
);
|
||||||
} else if (input.includes("/stories/")) {
|
} else if (input.includes("/stories/")) {
|
||||||
// https://www.wattpad.com/api/v3/stories/237369078?fields=...
|
// https://www.wattpad.com/api/v3/stories/237369078?fields=...
|
||||||
mode = "story";
|
mode = "story";
|
||||||
setInputAsValid(
|
setInputAsValid(
|
||||||
input.split("?", 1)[0].split("/stories/")[1], // removes params
|
input.split("?", 1)[0].split("/stories/")[1], // removes params
|
||||||
e.currentTarget,
|
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
// https://www.wattpad.com/939051741-wattpad-books-presents-the-qb-bad-boy-and-me
|
// https://www.wattpad.com/939051741-wattpad-books-presents-the-qb-bad-boy-and-me
|
||||||
@@ -98,9 +88,9 @@
|
|||||||
if (/^\d+$/.test(input)) {
|
if (/^\d+$/.test(input)) {
|
||||||
// If "wattpad.com/{downloadId}" contains only numbers
|
// If "wattpad.com/{downloadId}" contains only numbers
|
||||||
mode = "part";
|
mode = "part";
|
||||||
setInputAsValid(input, e.currentTarget);
|
setInputAsValid(input);
|
||||||
} else {
|
} else {
|
||||||
setInputAsInvalid("", e.currentTarget);
|
setInputAsInvalid("");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,7 +185,7 @@
|
|||||||
placeholder="Story URL"
|
placeholder="Story URL"
|
||||||
class="input input-bordered"
|
class="input input-bordered"
|
||||||
class:input-warning={invalidUrl}
|
class:input-warning={invalidUrl}
|
||||||
oninput={onInputUrl}
|
bind:value={() => inputUrl, setInputUrl}
|
||||||
required
|
required
|
||||||
name="input_url"
|
name="input_url"
|
||||||
/>
|
/>
|
||||||
|
|||||||
Reference in New Issue
Block a user