Spaces:
Paused
Paused
File size: 4,945 Bytes
43a06dc |
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 |
<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";
export let settingId: Id;
export let settingContext: Context;
export let placeholder: string;
export let altText: string;
export let type: "url" | "uuid" = "url";
export let showInstanceWarning = false;
const regex = {
url: "https?:\\/\\/[a-z0-9.\\-]+(:\\d+)?/?",
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 = false;
const writeToSettings = (value: string, type: "url" | "uuid" | "text") => {
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 && inputValue) {
return writeToSettings(inputValue, type);
}
return;
}
return writeToSettings(inputValue, type);
};
</script>
<div id="settings-input-holder">
<div id="input-container" class:focused={inputFocused} aria-hidden="false">
<input
id="input-box"
bind:this={input}
bind:value={inputValue}
on:input={() => (validInput = input.checkValidity())}
on:input={() => (inputFocused = true)}
on:focus={() => (inputFocused = true)}
on:blur={() => (inputFocused = false)}
spellcheck="false"
autocomplete="off"
autocapitalize="off"
maxlength="64"
pattern={regex[type]}
aria-label={altText}
aria-hidden="false"
/>
{#if inputValue.length === 0}
<span class="input-placeholder" aria-hidden="true">
{placeholder}
</span>
{/if}
</div>
<div id="settings-input-buttons">
<button
class="settings-input-button"
aria-label={$t("button.save")}
disabled={inputValue == $settings[settingContext][settingId] || !validInput}
on:click={save}
>
<IconCheck />
</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>
<style>
#settings-input-holder {
display: flex;
gap: 6px;
}
#input-container {
padding: 0 18px;
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: 13.5px;
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: 12px 0;
}
#input-box::placeholder {
color: var(--gray);
/* fix for firefox */
opacity: 1;
}
.input-placeholder {
position: absolute;
color: var(--gray);
pointer-events: none;
white-space: nowrap;
}
#input-box:focus-visible {
box-shadow: unset !important;
}
#input-container.focused {
box-shadow: 0 0 0 2px var(--secondary) inset;
}
#settings-input-buttons {
display: flex;
flex-direction: row;
gap: 6px;
}
.settings-input-button {
height: 42px;
width: 42px;
padding: 0;
}
.settings-input-button :global(svg) {
height: 21px;
width: 21px;
stroke-width: 1.5px;
}
.settings-input-button[disabled] {
opacity: 0.5;
pointer-events: none;
}
</style>
|