File size: 7,650 Bytes
de4b571 11fcc5a de4b571 11fcc5a de4b571 11fcc5a de4b571 11fcc5a de4b571 11fcc5a de4b571 11fcc5a de4b571 11fcc5a de4b571 11fcc5a de4b571 11fcc5a de4b571 11fcc5a de4b571 11fcc5a de4b571 11fcc5a de4b571 11fcc5a de4b571 11fcc5a de4b571 11fcc5a de4b571 11fcc5a de4b571 11fcc5a de4b571 11fcc5a de4b571 11fcc5a de4b571 11fcc5a de4b571 11fcc5a de4b571 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
<script
lang="ts"
generics="
Context extends Exclude<keyof CobaltSettings, 'schemaVersion'>,
Id extends keyof CobaltSettings[Context]
"
>
import { get } from "svelte/store";
import { t } from "$lib/i18n/translations";
import type { CobaltSettings } from "$lib/types/settings";
import settings, { updateSetting } from "$lib/state/settings";
import { customInstanceWarning } from "$lib/api/safety-warning";
import IconX from "@tabler/icons-svelte/IconX.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 IconEyeOff from "@tabler/icons-svelte/IconEyeOff.svelte";
type SettingsInputType = "url" | "uuid";
export let settingId: Id;
export let settingContext: Context;
export let placeholder: string;
export let altText: string;
export let type: "url" | "uuid" = "url";
export let sensitive = false;
export let showInstanceWarning = false;
const regex = {
uuid: "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$",
};
let input: HTMLInputElement;
let inputValue: string = String(get(settings)[settingContext][settingId]);
let inputFocused = false;
let validInput = true;
let inputHidden = true;
$: inputType = sensitive && inputHidden ? "password" : "text";
const checkInput = () => {
// mark input as valid if it's empty to allow wiping
if (inputValue.length === 0) {
validInput = true;
return;
}
if (type === "url") {
try {
new URL(inputValue)?.origin?.toString();
validInput = true;
return;
} catch {
validInput = false;
return;
}
} else {
validInput = new RegExp(regex[type]).test(inputValue);
}
};
const writeToSettings = (value: string, type: SettingsInputType) => {
// we assume that the url is valid and error can't be thrown here
// since it was tested before by checkInput()
updateSetting({
[settingContext]: {
[settingId]:
type === "url" ? new URL(value).origin.toString() : value,
},
});
inputValue = String(get(settings)[settingContext][settingId]);
};
const save = async () => {
if (showInstanceWarning) {
await customInstanceWarning();
if ($settings.processing.seenCustomWarning) {
// fall back to uuid to allow writing empty strings
return writeToSettings(inputValue, inputValue ? type : "uuid");
}
return;
}
return writeToSettings(inputValue, type);
};
</script>
<div id="settings-input-holder">
<div id="input-container" class:focused={inputFocused} aria-hidden="false">
<input
class="input-box"
bind:this={input}
bind:value={inputValue}
on:input={() => {
inputFocused = true;
checkInput();
}}
on:focus={() => (inputFocused = true)}
on:blur={() => (inputFocused = false)}
spellcheck="false"
autocomplete="off"
autocapitalize="off"
maxlength="64"
aria-label={altText}
aria-hidden="false"
aria-invalid={!validInput}
{...{ type: inputType }}
/>
{#if inputValue.length > 0}
<button
class="button input-inner-button"
on:click={() => {
inputValue = "";
checkInput();
}}
aria-label={$t("button.clear_input")}
>
<IconX />
</button>
{#if sensitive}
<button
class="button input-inner-button"
on:click={() => (inputHidden = !inputHidden)}
aria-label={$t(
inputHidden
? "button.show_input"
: "button.hide_input"
)}
>
{#if inputHidden}
<IconEye />
{:else}
<IconEyeOff />
{/if}
</button>
{/if}
{/if}
{#if inputValue.length === 0}
<span class="input-placeholder" aria-hidden="true">
{placeholder}
</span>
{#if String($settings[settingContext][settingId]).length > 0}
<button
class="button input-inner-button"
on:click={() => {
inputValue = String(
$settings[settingContext][settingId]
);
checkInput();
}}
aria-label={$t("button.restore_input")}
>
<IconArrowBack />
</button>
{/if}
{/if}
</div>
<div class="input-outer-buttons">
<button
class="button settings-input-button"
aria-label={$t("button.save")}
disabled={inputValue === $settings[settingContext][settingId] ||
!validInput}
on:click={save}
>
<IconCheck />
</button>
</div>
</div>
<style>
#settings-input-holder {
display: flex;
gap: 6px;
}
#input-container {
border-radius: var(--border-radius);
color: var(--secondary);
background-color: var(--button);
box-shadow: var(--button-box-shadow);
display: flex;
align-items: center;
width: 100%;
position: relative;
overflow: hidden;
}
#input-container,
.input-box {
font-size: 13px;
font-weight: 500;
min-width: 0;
}
.input-box {
flex: 1;
background-color: transparent;
color: var(--secondary);
border: none;
padding-block: 0;
padding-inline: 0;
padding: 11.5px 0;
}
.input-placeholder {
position: absolute;
color: var(--gray);
pointer-events: none;
white-space: nowrap;
}
.input-box,
.input-placeholder {
padding-left: 16px;
}
#input-container.focused {
box-shadow: 0 0 0 2px var(--secondary) inset;
}
.input-outer-buttons {
display: flex;
flex-direction: row;
gap: 6px;
}
.settings-input-button {
width: 40px;
padding: 0;
}
.settings-input-button :global(svg) {
height: 21px;
width: 21px;
stroke-width: 1.8px;
}
.settings-input-button[disabled] {
opacity: 0.5;
pointer-events: none;
}
.input-inner-button {
height: 34px;
width: 34px;
padding: 0;
box-shadow: none;
/* 3px is visual padding outside of the button */
border-radius: calc(var(--border-radius) - 3px);
z-index: 1;
}
.input-inner-button:last-child {
margin-right: 3px;
}
.input-inner-button :global(svg) {
height: 18px;
width: 18px;
stroke-width: 1.8px;
}
:global(svg) {
will-change: transform;
}
</style>
|