|
1 | 1 | <script lang="ts">
|
2 | 2 | import { goto } from '$app/navigation';
|
3 | 3 | import { languageKeysByName } from '$lib/data';
|
4 |
| - import type { Paste, PasteConfig, PasteCreateResponse } from '$lib/types'; |
| 4 | + import type { Paste, PasteConfig, PasteCreateResponse, UserSettings } from '$lib/types'; |
5 | 5 | import { onMount } from 'svelte';
|
6 | 6 | import Select from 'svelte-select';
|
7 | 7 | import { encrypt, encryptWithPassword } from '$lib/crypto';
|
8 | 8 | import Hamburger from '$lib/components/Hamburger.svelte';
|
9 | 9 | import { PUBLIC_REGISRATION_ENABLED } from '$env/static/public';
|
10 | 10 | import type { PageData } from './$types';
|
| 11 | + import { DHMToSeconds, secondsToDHM } from '$lib/utils/time'; |
11 | 12 |
|
12 | 13 | export let data: PageData;
|
13 | 14 |
|
|
25 | 26 | } = {};
|
26 | 27 |
|
27 | 28 | $: {
|
28 |
| - if (expiresAfter.days) { |
29 |
| - expiresAfter.days = Math.max(0, Math.round(expiresAfter.days)); |
| 29 | + let expiresAfterSeconds = DHMToSeconds(expiresAfter); |
| 30 | + // Don't allow pastes to be saved for more than a year |
| 31 | + expiresAfterSeconds = Math.min(expiresAfterSeconds, 365 * 24 * 60 * 60); |
| 32 | + // Don't allow pastes to be saved for less than 5 minutes |
| 33 | + if (expiresAfterSeconds > 0) { |
| 34 | + expiresAfterSeconds = Math.max(expiresAfterSeconds, 5 * 60); |
| 35 | + expiresAfter = secondsToDHM(expiresAfterSeconds); |
| 36 | + } else { |
| 37 | + expiresAfter = {}; |
30 | 38 | }
|
31 |
| - if (expiresAfter.hours) { |
32 |
| - expiresAfter.hours = Math.max(0, Math.round(expiresAfter.hours)); |
33 |
| - if (expiresAfter.hours > 23) { |
34 |
| - expiresAfter.days ??= 0; |
35 |
| - expiresAfter.days += Math.floor(expiresAfter.hours / 24); |
36 |
| - expiresAfter.hours = expiresAfter.hours % 24; |
37 |
| - } |
38 |
| - } |
39 |
| - if (expiresAfter.minutes) { |
40 |
| - expiresAfter.minutes = Math.max(0, Math.round(expiresAfter.minutes)); |
41 |
| - if (expiresAfter.minutes > 59) { |
42 |
| - expiresAfter.days ??= 0; |
43 |
| - expiresAfter.hours ??= 0; |
44 |
| - expiresAfter.days += Math.floor(expiresAfter.minutes / 1440); |
45 |
| - expiresAfter.hours += Math.floor((expiresAfter.minutes % 1440) / 60); |
46 |
| - expiresAfter.minutes = expiresAfter.minutes % 60; |
47 |
| - } |
48 | 39 |
|
49 |
| - if ( |
50 |
| - !expiresAfter.days && |
51 |
| - !expiresAfter.hours && |
52 |
| - expiresAfter.minutes > 0 && |
53 |
| - expiresAfter.minutes < 5 |
54 |
| - ) { |
55 |
| - expiresAfter.minutes = 5; |
56 |
| - } |
57 |
| - } |
58 |
| -
|
59 |
| - config.expiresAfter = |
60 |
| - ((expiresAfter.days ?? 0) * 1440 + |
61 |
| - (expiresAfter.hours ?? 0) * 60 + |
62 |
| - (expiresAfter.minutes ?? 0)) * |
63 |
| - 60; |
| 40 | + config.expiresAfter = expiresAfterSeconds; |
64 | 41 | }
|
65 | 42 |
|
66 | 43 | let inputRef: HTMLTextAreaElement;
|
|
71 | 48 | let config: PasteConfig = { ...initialConfig };
|
72 | 49 | let sidebarOpen = false;
|
73 | 50 |
|
| 51 | + const updateInitialConfig = (defaults: UserSettings['defaults']) => { |
| 52 | + if (!defaults) return; |
| 53 | + if (defaults?.encrypted !== undefined) config.encrypted = defaults.encrypted; |
| 54 | + if (defaults?.burnAfterRead !== undefined) config.burnAfterRead = defaults.burnAfterRead; |
| 55 | + if (defaults?.expiresAfterSeconds) { |
| 56 | + expiresAfter = secondsToDHM(defaults.expiresAfterSeconds); |
| 57 | + config.expiresAfter = defaults.expiresAfterSeconds; |
| 58 | + } |
| 59 | + }; |
| 60 | + $: updateInitialConfig(data?.settings?.defaults); |
| 61 | +
|
74 | 62 | let _sessionStorage: Storage | undefined;
|
75 | 63 |
|
76 | 64 | $: if (_sessionStorage) {
|
|
84 | 72 | if (contentBackup) {
|
85 | 73 | const data: { content: string; config: PasteConfig } = JSON.parse(contentBackup);
|
86 | 74 | content = data.content;
|
87 |
| - config = data.config; |
| 75 | + config = { ...config, language: data.config.language ?? config.language }; |
88 | 76 | }
|
89 | 77 |
|
90 | 78 | inputRef.focus();
|
|
229 | 217 | {#if PUBLIC_REGISRATION_ENABLED == 'true'}
|
230 | 218 | <div class="flex flex-row gap-4 mb-4 justify-center">
|
231 | 219 | {#if data.loggedIn}
|
| 220 | + <a href="/dashboard/settings" class="underline underline-offset-4 py-1">Dashboard</a> |
232 | 221 | <form action="/logout" method="post">
|
233 | 222 | <button class="underline underline-offset-4 py-1">Logout</button>
|
234 | 223 | </form>
|
|
0 commit comments