mirror of
https://github.com/wukko/cobalt.git
synced 2025-05-08 17:54:27 +02:00
web/SettingsInput: make the clear button non-destructive
clear button now clears data only in the input box, not actual data if you accidentally clear old data and don't save it, you can restore it with one click :3
This commit is contained in:
parent
c5d8d33870
commit
4f50b44e68
@ -19,5 +19,7 @@
|
|||||||
"no": "no",
|
"no": "no",
|
||||||
"clear": "clear",
|
"clear": "clear",
|
||||||
"show_input": "show input",
|
"show_input": "show input",
|
||||||
"hide_input": "hide input"
|
"hide_input": "hide input",
|
||||||
|
"restore_input": "restore input",
|
||||||
|
"clear_input": "clear input"
|
||||||
}
|
}
|
||||||
|
@ -14,10 +14,13 @@
|
|||||||
|
|
||||||
import IconX from "@tabler/icons-svelte/IconX.svelte";
|
import IconX from "@tabler/icons-svelte/IconX.svelte";
|
||||||
import IconCheck from "@tabler/icons-svelte/IconCheck.svelte";
|
import IconCheck from "@tabler/icons-svelte/IconCheck.svelte";
|
||||||
|
import IconArrowBack from "@tabler/icons-svelte/IconArrowBack.svelte";
|
||||||
|
|
||||||
import IconEye from "@tabler/icons-svelte/IconEye.svelte";
|
import IconEye from "@tabler/icons-svelte/IconEye.svelte";
|
||||||
import IconEyeClosed from "@tabler/icons-svelte/IconEyeClosed.svelte";
|
import IconEyeClosed from "@tabler/icons-svelte/IconEyeClosed.svelte";
|
||||||
|
|
||||||
|
type SettingsInputType = "url" | "uuid";
|
||||||
|
|
||||||
export let settingId: Id;
|
export let settingId: Id;
|
||||||
export let settingContext: Context;
|
export let settingContext: Context;
|
||||||
export let placeholder: string;
|
export let placeholder: string;
|
||||||
@ -41,7 +44,11 @@
|
|||||||
|
|
||||||
$: inputType = sensitive && inputHidden ? "password" : "text";
|
$: inputType = sensitive && inputHidden ? "password" : "text";
|
||||||
|
|
||||||
const writeToSettings = (value: string, type: "url" | "uuid" | "text") => {
|
const checkInput = () => {
|
||||||
|
validInput = input.checkValidity() || inputValue === "";
|
||||||
|
}
|
||||||
|
|
||||||
|
const writeToSettings = (value: string, type: SettingsInputType) => {
|
||||||
updateSetting({
|
updateSetting({
|
||||||
[settingContext]: {
|
[settingContext]: {
|
||||||
[settingId]:
|
[settingId]:
|
||||||
@ -55,8 +62,9 @@
|
|||||||
if (showInstanceWarning) {
|
if (showInstanceWarning) {
|
||||||
await customInstanceWarning();
|
await customInstanceWarning();
|
||||||
|
|
||||||
if ($settings.processing.seenCustomWarning && inputValue) {
|
if ($settings.processing.seenCustomWarning) {
|
||||||
return writeToSettings(inputValue, type);
|
// allow writing empty strings
|
||||||
|
return writeToSettings(inputValue, inputValue ? type : "uuid");
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return;
|
||||||
@ -70,7 +78,6 @@
|
|||||||
<div
|
<div
|
||||||
id="input-container"
|
id="input-container"
|
||||||
class:focused={inputFocused}
|
class:focused={inputFocused}
|
||||||
class:extra-button={sensitive && inputValue.length > 0}
|
|
||||||
aria-hidden="false"
|
aria-hidden="false"
|
||||||
>
|
>
|
||||||
<input
|
<input
|
||||||
@ -78,8 +85,8 @@
|
|||||||
bind:this={input}
|
bind:this={input}
|
||||||
bind:value={inputValue}
|
bind:value={inputValue}
|
||||||
on:input={() => {
|
on:input={() => {
|
||||||
validInput = input.checkValidity();
|
|
||||||
inputFocused = true;
|
inputFocused = true;
|
||||||
|
checkInput();
|
||||||
}}
|
}}
|
||||||
on:focus={() => (inputFocused = true)}
|
on:focus={() => (inputFocused = true)}
|
||||||
on:blur={() => (inputFocused = false)}
|
on:blur={() => (inputFocused = false)}
|
||||||
@ -93,26 +100,52 @@
|
|||||||
{...{ type: inputType }}
|
{...{ type: inputType }}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
|
{#if inputValue.length > 0}
|
||||||
|
<button
|
||||||
|
class="input-inner-button"
|
||||||
|
on:click={() => {
|
||||||
|
inputValue = "";
|
||||||
|
checkInput();
|
||||||
|
}}
|
||||||
|
aria-label={$t("button.clear_input")}
|
||||||
|
>
|
||||||
|
<IconX />
|
||||||
|
</button>
|
||||||
|
|
||||||
|
{#if sensitive}
|
||||||
|
<button
|
||||||
|
class="input-inner-button"
|
||||||
|
on:click={() => (inputHidden = !inputHidden)}
|
||||||
|
aria-label={$t(
|
||||||
|
inputHidden ? "button.show_input" : "button.hide_input"
|
||||||
|
)}
|
||||||
|
>
|
||||||
|
{#if inputHidden}
|
||||||
|
<IconEye />
|
||||||
|
{:else}
|
||||||
|
<IconEyeClosed />
|
||||||
|
{/if}
|
||||||
|
</button>
|
||||||
|
{/if}
|
||||||
|
{/if}
|
||||||
|
|
||||||
{#if inputValue.length === 0}
|
{#if inputValue.length === 0}
|
||||||
<span class="input-placeholder" aria-hidden="true">
|
<span class="input-placeholder" aria-hidden="true">
|
||||||
{placeholder}
|
{placeholder}
|
||||||
</span>
|
</span>
|
||||||
{/if}
|
|
||||||
|
|
||||||
{#if sensitive && inputValue.length > 0}
|
{#if String($settings[settingContext][settingId]).length > 0}
|
||||||
<button
|
<button
|
||||||
class="input-inner-button"
|
class="input-inner-button"
|
||||||
on:click={() => (inputHidden = !inputHidden)}
|
on:click={() => {
|
||||||
aria-label={$t(
|
inputValue = String($settings[settingContext][settingId]);
|
||||||
inputHidden ? "button.show_input" : "button.hide_input"
|
checkInput();
|
||||||
)}
|
}}
|
||||||
>
|
aria-label={$t("button.restore_input")}
|
||||||
{#if inputHidden}
|
>
|
||||||
<IconEye />
|
<IconArrowBack />
|
||||||
{:else}
|
</button>
|
||||||
<IconEyeClosed />
|
{/if}
|
||||||
{/if}
|
|
||||||
</button>
|
|
||||||
{/if}
|
{/if}
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -120,20 +153,11 @@
|
|||||||
<button
|
<button
|
||||||
class="settings-input-button"
|
class="settings-input-button"
|
||||||
aria-label={$t("button.save")}
|
aria-label={$t("button.save")}
|
||||||
disabled={inputValue == $settings[settingContext][settingId] || !validInput}
|
disabled={inputValue === $settings[settingContext][settingId] || !validInput}
|
||||||
on:click={save}
|
on:click={save}
|
||||||
>
|
>
|
||||||
<IconCheck />
|
<IconCheck />
|
||||||
</button>
|
</button>
|
||||||
|
|
||||||
<button
|
|
||||||
class="settings-input-button"
|
|
||||||
aria-label={$t("button.reset")}
|
|
||||||
disabled={String($settings[settingContext][settingId]).length <= 0}
|
|
||||||
on:click={() => writeToSettings("", "text")}
|
|
||||||
>
|
|
||||||
<IconX />
|
|
||||||
</button>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
@ -145,6 +169,7 @@
|
|||||||
|
|
||||||
#input-container {
|
#input-container {
|
||||||
padding: 0 16px;
|
padding: 0 16px;
|
||||||
|
padding-right: 4px;
|
||||||
border-radius: var(--border-radius);
|
border-radius: var(--border-radius);
|
||||||
color: var(--secondary);
|
color: var(--secondary);
|
||||||
background-color: var(--button);
|
background-color: var(--button);
|
||||||
@ -156,10 +181,6 @@
|
|||||||
overflow: hidden;
|
overflow: hidden;
|
||||||
}
|
}
|
||||||
|
|
||||||
#input-container.extra-button {
|
|
||||||
padding-right: 4px;
|
|
||||||
}
|
|
||||||
|
|
||||||
#input-container,
|
#input-container,
|
||||||
#input-box {
|
#input-box {
|
||||||
font-size: 13.5px;
|
font-size: 13.5px;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user